Browse Source

Add functionality to delete transaction from db when editing, close #21

Skylar Ittner 5 months ago
parent
commit
819ce856a4
2 changed files with 64 additions and 1 deletions
  1. 46
    0
      action.php
  2. 18
    1
      static/js/pos.js

+ 46
- 0
action.php View File

@@ -312,6 +312,52 @@ switch ($VARS['action']) {
312 312
             exit(json_encode(["status" => "OK", "txid" => $oktx]));
313 313
         }
314 314
         break;
315
+    case "delete_transaction":
316
+        header("Content-Type: application/json");
317
+        $error = null;
318
+        if (isset($VARS['txid']) && $database->has('transactions', ['txid' => $VARS['txid']])) {
319
+            $txid = $VARS['txid'];
320
+            $cashid = $database->get('transactions', 'cashid', ['txid' => $txid]);
321
+            if (!$database->has('cash_drawer', ['AND' => ['cashid' => $cashid, 'close' => null]])) {
322
+                $error = lang("cash already closed", false);
323
+            }
324
+
325
+            $database->action(function ($database) {
326
+                global $VARS, $binstack, $error, $txid;
327
+
328
+                // Delete payments
329
+                $payments = $database->select('payments', ['payid', 'amount', 'type', 'certid'], ['txid' => $txid]);
330
+                foreach ($payments as $p) {
331
+                    // Reset gift card balances
332
+                    if (!is_null($p['certid'])) {
333
+                        $database->update('certificates', ['amount[+]' => $p['amount']], ['certid' => $p['certid']]);
334
+                    }
335
+                    $database->delete('payments', ['payid' => $p['payid']]);
336
+                }
337
+
338
+                // Delete items/lines
339
+                $items = $database->select('lines', ['itemid (id)', 'qty', 'lineid'], ['txid' => $txid]);
340
+                foreach ($items as $i) {
341
+                    $database->delete('lines', ['lineid' => $i['lineid']]);
342
+                    $binstack->update('items', [
343
+                        'qty[+]' => $i['qty']
344
+                            ], [
345
+                        'itemid' => $i['id']
346
+                    ]);
347
+                }
348
+
349
+                // Delete transaction
350
+                $database->delete('transactions', ['txid' => $txid, 'LIMIT' => 1]);
351
+            });
352
+        } else {
353
+            $error = lang("invalid parameters", false);
354
+        }
355
+        if (!is_null($error)) {
356
+            exit(json_encode(["status" => "ERROR", "message" => $error]));
357
+        } else {
358
+            exit(json_encode(["status" => "OK"]));
359
+        }
360
+        break;
315 361
     case "getreceipt":
316 362
         require_once __DIR__ . "/lib/generatereceipt.php";
317 363
         $format = "html";

+ 18
- 1
static/js/pos.js View File

@@ -58,7 +58,24 @@ function recalculate() {
58 58
 
59 59
 $("#deletetxbtn").click(function () {
60 60
     bsalert("Confirm", "Really delete transaction?", "Yes", "No", function () {
61
-        window.location.href = "app.php?page=pos";
61
+        if ($("#txid").length) {
62
+            $.post("action.php", {
63
+                action: "delete_transaction",
64
+                txid: $("#txid").val()
65
+            }, function (data) {
66
+                console.log(data);
67
+                console.log(data.status == "OK");
68
+                if (data.status == "OK") {
69
+                    window.location.href = "app.php?page=pos";
70
+                } else {
71
+                    bsalert("Error", data.message);
72
+                }
73
+            }).fail(function () {
74
+                bsalert("Error", "An unknown error occurred.");
75
+            });
76
+        } else {
77
+            window.location.href = "app.php?page=pos";
78
+        }
62 79
     });
63 80
 });
64 81