|
@@ -0,0 +1,35 @@
|
|
1
|
+<?php
|
|
2
|
+$rest_json = file_get_contents("php://input");
|
|
3
|
+$_POST = json_decode($rest_json, true);
|
|
4
|
+
|
|
5
|
+// Must have a code and password field
|
|
6
|
+$fp = fopen('.htpassajax', 'r');
|
|
7
|
+$password = trim(fread($fp, filesize('.htpassajax')));
|
|
8
|
+fclose($fp);
|
|
9
|
+
|
|
10
|
+if ($_POST["password"] != $password)
|
|
11
|
+ die("Wrong Password");
|
|
12
|
+
|
|
13
|
+// open the file and get the stock
|
|
14
|
+$file = '../data/stock.json';
|
|
15
|
+$fp = fopen($file, 'r');
|
|
16
|
+$result = json_decode(fread($fp, filesize($file)));
|
|
17
|
+fclose($fp);
|
|
18
|
+
|
|
19
|
+$price = 'N/A';
|
|
20
|
+
|
|
21
|
+// Get the price from the given code and remove 1 in quantity. Do not get the price if quantity is at 0
|
|
22
|
+foreach ($result->articles as $articleObject) {
|
|
23
|
+ if ($articleObject->code === $_POST["code"] && intval($articleObject->quantity) > 0) {
|
|
24
|
+ $price = $articleObject->price;
|
|
25
|
+ $articleObject->quantity = strval(intval($articleObject->quantity) - 1);
|
|
26
|
+ }
|
|
27
|
+}
|
|
28
|
+
|
|
29
|
+// open the file and write the updated stock
|
|
30
|
+$fp = fopen('../data/stock.json', 'w');
|
|
31
|
+fwrite($fp, json_encode($result));
|
|
32
|
+fclose($fp);
|
|
33
|
+
|
|
34
|
+echo $price;
|
|
35
|
+
|