Browse Source

Added ability to make post request to scan articles

keplyx 4 years ago
parent
commit
f1355b4969
2 changed files with 55 additions and 0 deletions
  1. 35
    0
      ajax/scan_article.php
  2. 20
    0
      assets/js/stock.js

+ 35
- 0
ajax/scan_article.php View File

@@ -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
+

+ 20
- 0
assets/js/stock.js View File

@@ -205,3 +205,23 @@ function saveDataset() {
205 205
         },
206 206
     });
207 207
 }
208
+
209
+
210
+
211
+function scanArticle(code) {
212
+    let data = {
213
+        password: 'coucou',
214
+        code : code,
215
+    };
216
+    $.ajax({
217
+        type: "POST",
218
+        url: "../ajax/scan_article.php",
219
+        data: JSON.stringify(data),
220
+        dataType: "json",
221
+        contentType: "application/json; charset=utf-8",
222
+        complete: function (data) {
223
+            // alert(data.responseText);
224
+            console.log(data.responseText);
225
+        },
226
+    });
227
+}

Loading…
Cancel
Save