diff --git a/action.php b/action.php index 6c0a4e6..54e85c5 100644 --- a/action.php +++ b/action.php @@ -312,6 +312,52 @@ switch ($VARS['action']) { exit(json_encode(["status" => "OK", "txid" => $oktx])); } break; + case "delete_transaction": + header("Content-Type: application/json"); + $error = null; + if (isset($VARS['txid']) && $database->has('transactions', ['txid' => $VARS['txid']])) { + $txid = $VARS['txid']; + $cashid = $database->get('transactions', 'cashid', ['txid' => $txid]); + if (!$database->has('cash_drawer', ['AND' => ['cashid' => $cashid, 'close' => null]])) { + $error = lang("cash already closed", false); + } + + $database->action(function ($database) { + global $VARS, $binstack, $error, $txid; + + // Delete payments + $payments = $database->select('payments', ['payid', 'amount', 'type', 'certid'], ['txid' => $txid]); + foreach ($payments as $p) { + // Reset gift card balances + if (!is_null($p['certid'])) { + $database->update('certificates', ['amount[+]' => $p['amount']], ['certid' => $p['certid']]); + } + $database->delete('payments', ['payid' => $p['payid']]); + } + + // Delete items/lines + $items = $database->select('lines', ['itemid (id)', 'qty', 'lineid'], ['txid' => $txid]); + foreach ($items as $i) { + $database->delete('lines', ['lineid' => $i['lineid']]); + $binstack->update('items', [ + 'qty[+]' => $i['qty'] + ], [ + 'itemid' => $i['id'] + ]); + } + + // Delete transaction + $database->delete('transactions', ['txid' => $txid, 'LIMIT' => 1]); + }); + } else { + $error = lang("invalid parameters", false); + } + if (!is_null($error)) { + exit(json_encode(["status" => "ERROR", "message" => $error])); + } else { + exit(json_encode(["status" => "OK"])); + } + break; case "getreceipt": require_once __DIR__ . "/lib/generatereceipt.php"; $format = "html"; diff --git a/static/js/pos.js b/static/js/pos.js index e48a3f7..8e372cc 100644 --- a/static/js/pos.js +++ b/static/js/pos.js @@ -58,7 +58,24 @@ function recalculate() { $("#deletetxbtn").click(function () { bsalert("Confirm", "Really delete transaction?", "Yes", "No", function () { - window.location.href = "app.php?page=pos"; + if ($("#txid").length) { + $.post("action.php", { + action: "delete_transaction", + txid: $("#txid").val() + }, function (data) { + console.log(data); + console.log(data.status == "OK"); + if (data.status == "OK") { + window.location.href = "app.php?page=pos"; + } else { + bsalert("Error", data.message); + } + }).fail(function () { + bsalert("Error", "An unknown error occurred."); + }); + } else { + window.location.href = "app.php?page=pos"; + } }); });