Add transaction loading, editing, and updating (issue #18)

master
Skylar Ittner 6 years ago
parent 0c72450c19
commit 61f77f5001

@ -33,115 +33,178 @@ function returnToSender($msg, $arg = "") {
switch ($VARS['action']) {
case "finish_transaction":
header("Content-Type: application/json");
$items = $VARS['items'];
$payments = $VARS['payments'];
$customer = $VARS['customer'];
$register = $VARS['register'];
$discountpercent = $VARS['discountpercent'];
if ($customer != "" && !$database->has('customers', ['customerid' => $customer])) {
exit(json_encode(["status" => "ERROR", "message" => lang("invalid customer", false)]));
// exit(json_encode(["status" => "ERROR", "message" => lang("", false)]));
}
if ($register != "" && !$database->has('registers', ['registerid' => $register])) {
exit(json_encode(["status" => "ERROR", "message" => lang("invalid register", false)]));
}
if ($register != "" && !$database->has('cash_drawer', ['AND' => ['registerid' => $register, 'close' => null]])) {
exit(json_encode(["status" => "ERROR", "message" => lang("cash not open", false)]));
}
$totalcharge = 0.00;
$totalpaid = 0.00;
$change = 0.0;
foreach ($items as $i) {
$totalcharge += $i['each'] * $i['qty'];
if (!$binstack->has('items', ['itemid' => $i['id']])) {
exit(json_encode(["status" => "ERROR", "message" => lang("invalid item", false)]));
$error = null;
$oktx = null;
$database->action(function ($database) {
global $VARS, $binstack, $error, $oktx;
$items = $VARS['items'];
$payments = $VARS['payments'];
$customer = $VARS['customer'];
$register = $VARS['register'];
$discountpercent = $VARS['discountpercent'];
$cashid = null;
$editing = false;
if (isset($VARS['txid']) && $database->has('transactions', ['txid' => $VARS['txid']])) {
$editing = true;
$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);
return false;
}
// Nuke the payments to make room for their replacements
// Delete payments
$oldpayments = $database->select('payments', ['payid', 'amount', 'type', 'certid'], ['txid' => $txid]);
foreach ($oldpayments 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']]);
}
}
}
foreach ($payments as $p) {
if (!$database->has('payment_types', ['typename' => $p['type']])) {
exit(json_encode(["status" => "ERROR", "message" => lang("invalid payment type", false)]));
if ($customer != "" && !$database->has('customers', ['customerid' => $customer])) {
$error = lang("invalid customer", false);
return false;
}
$totalpaid += $p['amount'];
if ($p['type'] == "giftcard") {
if (!$database->has('certificates', ['AND' => ['amount[>=]' => $p['amount'], 'deleted[!]' => 1, 'certcode' => $p['code']]])) {
exit(json_encode(["status" => "ERROR", "message" => lang("invalid giftcard", false)]));
if ($register != "" && !$database->has('registers', ['registerid' => $register])) {
$error = lang("invalid register", false);
return false;
}
if ($register != "" && !$database->has('cash_drawer', ['AND' => ['registerid' => $register, 'close' => null]])) {
$error = lang("cash not open", false);
return false;
}
if ($register != "" && $editing === false) {
$cashid = $database->get('cash_drawer', 'cashid', ['AND' => ['registerid' => $register, 'close' => null]]);
}
$totalcharge = 0.00;
$totalpaid = 0.00;
$change = 0.0;
foreach ($items as $i) {
$totalcharge += $i['each'] * $i['qty'];
if (!$binstack->has('items', ['itemid' => $i['id']])) {
$error = lang("invalid item", false);
return false;
}
}
foreach ($payments as $p) {
if (!$database->has('payment_types', ['typename' => $p['type']])) {
$error = lang("invalid payment type", false);
return false;
}
$totalpaid += $p['amount'];
if ($p['type'] == "giftcard") {
if (!$database->has('certificates', ['AND' => ['amount[>=]' => $p['amount'], 'deleted[!]' => 1, 'certcode' => $p['code']]])) {
$error = lang("invalid giftcard", false);
return false;
}
}
}
}
if (is_numeric($discountpercent) && $discountpercent > 0 && $discountpercent < 100) {
$discountpercent = $discountpercent * 1.0;
$totalcharge *= 1.0 - ($discountpercent / 100.0);
} else {
$discountpercent = 0.0;
}
if (is_numeric($discountpercent) && $discountpercent > 0 && $discountpercent < 100) {
$discountpercent = $discountpercent * 1.0;
$totalcharge *= 1.0 - ($discountpercent / 100.0);
} else {
$discountpercent = 0.0;
}
if ($totalcharge > $totalpaid) {
exit(json_encode(["status" => "ERROR", "message" => lang("insufficient payment", false)]));
}
if ($totalcharge > $totalpaid) {
$error = lang("insufficient payment", false);
return false;
}
$cashid = null;
if ($register != "") {
$cashid = $database->get('cash_drawer', 'cashid', ['AND' => ['registerid' => $register, 'close' => null]]);
}
$database->insert('transactions', [
'txdate' => date('Y-m-d H:i:s'),
'customerid' => ($customer != "" ? $customer : null),
'type' => 1,
'cashier' => $_SESSION['uid'],
'cashid' => $cashid,
'discountpercent' => $discountpercent
]);
$txid = $database->id();
foreach ($items as $i) {
$item = $binstack->get('items', ['name', 'qty'], ['itemid' => $i['id']]);
$database->insert('lines', [
'txid' => $txid,
'amount' => $i['each'],
'name' => $item['name'],
'itemid' => $i['id'],
'qty' => $i['qty']
]);
$binstack->update('items', [
'qty[-]' => $i['qty']
], [
'itemid' => $i['id']
]);
}
foreach ($payments as $p) {
$certid = null;
if ($p['type'] == "giftcard") {
$certid = $database->get('certificates', 'certid', ['certcode' => $p['code']]);
if ($editing === true) {
$database->update('transactions', [
'txdate' => date('Y-m-d H:i:s'),
'customerid' => ($customer != "" ? $customer : null),
'type' => 1,
'cashier' => $_SESSION['uid'],
'cashid' => $cashid,
'discountpercent' => $discountpercent
], [
'txid' => $txid
]);
} else {
$database->insert('transactions', [
'txdate' => date('Y-m-d H:i:s'),
'customerid' => ($customer != "" ? $customer : null),
'type' => 1,
'cashier' => $_SESSION['uid'],
'cashid' => $cashid,
'discountpercent' => $discountpercent
]);
$txid = $database->id();
}
$type = $database->get('payment_types', 'typeid', ['typename' => $p['type']]);
$database->insert('payments', [
'amount' => $p['amount'],
'data' => '',
'type' => $type,
'txid' => $txid,
'certid' => $certid
]);
}
if ($totalcharge < $totalpaid) {
$change = $totalpaid - $totalcharge;
$database->insert('payments', [
'amount' => $change * -1.0,
'data' => '',
'type' => 1,
'txid' => $txid,
'certid' => null
]);
}
$olditems = $database->select('lines', ['itemid (id)', 'qty', 'lineid'], ['txid' => $txid]);
foreach ($items as $i) {
$item = $binstack->get('items', ['name', 'qty'], ['itemid' => $i['id']]);
$database->insert('lines', [
'txid' => $txid,
'amount' => $i['each'],
'name' => $item['name'],
'itemid' => $i['id'],
'qty' => $i['qty']
]);
$binstack->update('items', [
'qty[-]' => $i['qty']
], [
'itemid' => $i['id']
]);
}
exit(json_encode(["status" => "OK", "txid" => $txid]));
foreach ($payments as $p) {
$certid = null;
if ($p['type'] == "giftcard") {
$certid = $database->get('certificates', 'certid', ['certcode' => $p['code']]);
}
$type = $database->get('payment_types', 'typeid', ['typename' => $p['type']]);
$database->insert('payments', [
'amount' => $p['amount'],
'data' => '',
'type' => $type,
'txid' => $txid,
'certid' => $certid
]);
}
if ($totalcharge < $totalpaid) {
$change = $totalpaid - $totalcharge;
$database->insert('payments', [
'amount' => $change * -1.0,
'data' => '',
'type' => 1,
'txid' => $txid,
'certid' => null
]);
}
foreach ($olditems as $i) {
$database->delete('lines', ['lineid' => $i['lineid']]);
$binstack->update('items', [
'qty[+]' => $i['qty']
], [
'itemid' => $i['id']
]);
}
$oktx = $txid;
return true;
});
if (!is_null($error)) {
exit(json_encode(["status" => "ERROR", "message" => $error]));
} else {
exit(json_encode(["status" => "OK", "txid" => $oktx]));
}
break;
case "getreceipt":
require_once __DIR__ . "/lib/generatereceipt.php";

@ -118,4 +118,6 @@ define("STRINGS", [
"x report" => "X Report",
"z report" => "Z Report",
"pick cash" => "Choose",
"cash already closed" => "Cash already closed, cannot edit this transaction. Process a return instead.",
"update" => "Update",
]);

@ -19,6 +19,16 @@ if (isset($_GET['switch']) || !isset($_SESSION['register']) || !$registeropen) {
} else {
$register = $database->get('registers', ['registerid (id)', 'registername (name)'], ['registerid' => $_SESSION['register']]);
$showgridbydefault = $binstack->count('items', ['AND' => ['price[!]' => null, 'price[!]' => 0]]) <= GRID_BY_DEFAULT_MAX_ITEMS;
$items = [];
$payments = [];
$editing = false;
if (isset($VARS['txid']) && $database->has('transactions', ['txid' => $VARS['txid']])) {
$editing = true;
$items = $database->select('lines', ['lineid', 'amount', 'name', 'itemid', 'qty'], ['txid' => $VARS['txid']]);
$payments = $database->select('payments', ['[>]certificates' => 'certid', '[>]payment_types' => ['type' => 'typeid']], ['payments.amount', 'typename', 'icon', 'text', 'certcode'], ['txid' => $VARS['txid']]);
$tx = $database->get('transactions', ['[>]customers' => 'customerid'], ['txid', 'discountpercent', 'transactions.customerid', 'customers.name (customername)'], ['txid' => $VARS['txid']]);
echo "<input type=\"hidden\" id=\"txid\" value=\"$VARS[txid]\">";
}
?>
<div class="modal fade" tabindex="-1" role="dialog" id="receiptmodal">
<div class="modal-dialog" role="document">
@ -122,7 +132,44 @@ if (isset($_GET['switch']) || !isset($_SESSION['register']) || !$registeropen) {
</div>
<div>
<div class="list-group list-group-flush" id="pos-lines-box">
<!-- Items go here -->
<?php
foreach ($items as $i) {
?>
<div class="list-group-item" data-itemid="<?php echo $i['itemid']; ?>">
<div class="d-flex w-100 justify-content-between mb-2">
<h5 class="item-name"><?php echo $i['name']; ?></h5>
<h5>
<span class="badge badge-light">
$<span class="line-total"><?php echo number_format($i['amount'] * $i['qty'], 2); ?></span>
</span>
</h5>
</div>
<div class="d-inline-flex">
<div class="input-group qty-control">
<div class="input-group-prepend">
<span class="input-group-text pr-1"><b>$</b></span>
</div>
<input type="money" class="form-control item-price" value="<?php echo number_format($i['amount'], 2); ?>"/>
<div class="input-group-prepend">
<span class="input-group-text px-2"><i class="fas fa-times"></i></span>
<button class="btn btn-red qty-minus" type="button"><i class="fas <?php
if ($i['qty'] > 1) {
echo "fa-minus";
} else {
echo "fa-trash";
}
?>"></i></button>
</div>
<input type="number" class="form-control item-qty px-2" value="<?php echo $i['qty']; ?>" />
<div class="input-group-append">
<button class="btn btn-light-green qty-plus" type="button"><i class="fas fa-plus"></i></button>
</div>
</div>
</div>
</div>
<?php
}
?>
</div>
</div>
</div>
@ -138,11 +185,25 @@ if (isset($_GET['switch']) || !isset($_SESSION['register']) || !$registeropen) {
<div class="card-body d-flex justify-content-center flex-wrap py-0 my-0">
<div class="btn m-1" id="addcustomerbtn">
<i class="fas fa-user-circle"></i>
<span id="customerbtnlabel"></span>
<span id="customerbtnlabel"><?php
if ($editing && isset($tx['customername'])) {
echo $tx['customername'];
}
?></span>
<span class="sr-only"><?php lang("customer"); ?></span>
</div>
<div class="btn m-1" id="discountpercentbtn" data-percent="0">
<span id="discountpercentbtnlabel"></span>
<div class="btn m-1" id="discountpercentbtn" data-percent="<?php
if ($editing && isset($tx['discountpercent']) && $tx['discountpercent'] != 0) {
echo (float) $tx['discountpercent'];
} else {
echo "0";
}
?>">
<span id="discountpercentbtnlabel"><?php
if ($editing && isset($tx['discountpercent']) && $tx['discountpercent'] != 0) {
echo (float) $tx['discountpercent'];
}
?></span>
<i class="fas fa-percent"></i>
<span class="sr-only"><?php lang("transaction discount"); ?></span>
</div>
@ -179,18 +240,66 @@ if (isset($_GET['switch']) || !isset($_SESSION['register']) || !$registeropen) {
</div>
</div>
<div class="list-group list-group-flush" id="payment-lines">
<?php
foreach ($payments as $p) {
if ($p['amount'] <= 0) {
continue;
}
?>
<div class="list-group-item">
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text">
<i class="<?php echo $p['icon']; ?> fa-fw mr-1"></i>
<?php lang($p['text']); ?>
</span>
</div>
<div class="input-group-prepend">
<span class="input-group-text">
$
</span>
</div>
<input class="form-control payment-entry" type="money" data-type="<?php echo $p['typename']; ?>" value="<?php echo number_format($p['amount'], 2); ?>" />
<?php
if ($p['typename'] == 'giftcard') {
?>
<div class="input-group-prepend input-group-append">
<span class="input-group-text">
#
</span>
</div>
<input class="form-control giftcard-number" type="number" value="<?php echo $p['certcode']; ?>" />
<?php
}
?>
<div class="input-group-append">
<span class="btn btn-outline-danger remove-payment-btn">
<i class="fas fa-trash"></i>
</span>
</div>
</div>
</div>
<?php
}
?>
<!-- Payments go here -->
</div>
<div class="card-body">
<span class="btn btn-green btn-lg btn-block" id="finishbtn"><i class="fas fa-receipt"></i> <?php lang("finish"); ?></span>
<span class="btn btn-green btn-lg btn-block" id="finishbtn"><i class="fas fa-receipt"></i> <?php
if ($editing) {
lang("update");
} else {
lang("finish");
}
?></span>
</div>
</div>
</div>
</div>
</div>
<script nonce="<?php echo $SECURE_NONCE; ?>">
var showgridbydefault = <?php echo $showgridbydefault === true ? "true" : "false" ?>;
var showgridbydefault = <?php echo $showgridbydefault === true && $editing !== true ? "true" : "false" ?>;
</script>
<?php
}

@ -56,4 +56,11 @@ function recalculate() {
// Make sure the session doesn't expire
setInterval(function () {
$.get("action.php", {action: "session_keepalive"});
}, 1000 * 60);
}, 1000 * 60);
$(document).ready(function () {
if ($("#txid").length) {
recalculate();
$("#paymentui").removeClass("d-none");
}
});

@ -13,6 +13,10 @@ function sendTransactionToServer(callback) {
if (discountpercent <= 0) {
discountpercent = 0.0;
}
var txid = "";
if ($("#txid").length) {
txid = $("#txid").val();
}
$("#pos-lines-box .list-group-item").each(function () {
var each = $(".item-price", this).val() * 1.0;
var qty = $(".item-qty", this).val() * 1.0;
@ -39,13 +43,16 @@ function sendTransactionToServer(callback) {
});
});
console.log(payments);
$.post("action.php", {
action: "finish_transaction",
items: items,
payments: payments,
customer: customer,
register: register,
discountpercent: discountpercent
discountpercent: discountpercent,
txid: txid
}, function (data) {
if (data.status == "OK") {
callback(data);
@ -83,5 +90,5 @@ $("#receiptprintbtn").click(function () {
});
$("#receiptmodal").on("hide.bs.modal", function () {
window.location.reload();
window.location.href = "app.php?page=pos";
});
Loading…
Cancel
Save