Added ability to make post request to scan articles
This commit is contained in:
parent
1e928ef85e
commit
f1355b4969
2 changed files with 55 additions and 0 deletions
35
ajax/scan_article.php
Normal file
35
ajax/scan_article.php
Normal file
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
$rest_json = file_get_contents("php://input");
|
||||
$_POST = json_decode($rest_json, true);
|
||||
|
||||
// Must have a code and password field
|
||||
$fp = fopen('.htpassajax', 'r');
|
||||
$password = trim(fread($fp, filesize('.htpassajax')));
|
||||
fclose($fp);
|
||||
|
||||
if ($_POST["password"] != $password)
|
||||
die("Wrong Password");
|
||||
|
||||
// open the file and get the stock
|
||||
$file = '../data/stock.json';
|
||||
$fp = fopen($file, 'r');
|
||||
$result = json_decode(fread($fp, filesize($file)));
|
||||
fclose($fp);
|
||||
|
||||
$price = 'N/A';
|
||||
|
||||
// Get the price from the given code and remove 1 in quantity. Do not get the price if quantity is at 0
|
||||
foreach ($result->articles as $articleObject) {
|
||||
if ($articleObject->code === $_POST["code"] && intval($articleObject->quantity) > 0) {
|
||||
$price = $articleObject->price;
|
||||
$articleObject->quantity = strval(intval($articleObject->quantity) - 1);
|
||||
}
|
||||
}
|
||||
|
||||
// open the file and write the updated stock
|
||||
$fp = fopen('../data/stock.json', 'w');
|
||||
fwrite($fp, json_encode($result));
|
||||
fclose($fp);
|
||||
|
||||
echo $price;
|
||||
|
|
@ -205,3 +205,23 @@ function saveDataset() {
|
|||
},
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
function scanArticle(code) {
|
||||
let data = {
|
||||
password: 'coucou',
|
||||
code : code,
|
||||
};
|
||||
$.ajax({
|
||||
type: "POST",
|
||||
url: "../ajax/scan_article.php",
|
||||
data: JSON.stringify(data),
|
||||
dataType: "json",
|
||||
contentType: "application/json; charset=utf-8",
|
||||
complete: function (data) {
|
||||
// alert(data.responseText);
|
||||
console.log(data.responseText);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue