Add transaction percentage discount (close #13)

master
Skylar Ittner 6 years ago
parent e46d89de7b
commit ef5a72d2a5

@ -37,6 +37,7 @@ switch ($VARS['action']) {
$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)]));
@ -69,6 +70,13 @@ switch ($VARS['action']) {
}
}
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)]));
}
@ -83,7 +91,8 @@ switch ($VARS['action']) {
'customerid' => ($customer != "" ? $customer : null),
'type' => 1,
'cashier' => $_SESSION['uid'],
'cashid' => $cashid
'cashid' => $cashid,
'discountpercent' => $discountpercent
]);
$txid = $database->id();
@ -128,7 +137,7 @@ switch ($VARS['action']) {
exit(json_encode(["status" => "ERROR", "txid" => null]));
}
$tx = $database->get('transactions', ['txid', 'txdate', 'customerid', 'type', 'cashier'], ['txid' => $VARS['txid']]);
$tx = $database->get('transactions', ['txid', 'txdate', 'customerid', 'type', 'cashier', 'discountpercent'], ['txid' => $VARS['txid']]);
$txid = $tx['txid'];
$datetime = date(DATETIME_FORMAT, strtotime($tx['txdate']));
@ -142,7 +151,7 @@ switch ($VARS['action']) {
$itemhtml = "";
$items = $database->select('lines', ['amount', 'name', 'itemid', 'qty'], ['txid' => $txid]);
$total = 0.0;
$subtotal = 0.0;
$paid = 0.0;
foreach ($items as $i) {
$itemhtml .= "\n";
@ -152,9 +161,11 @@ switch ($VARS['action']) {
$itemhtml .= '<div>x' . (float) $i['qty'] . '</div>';
$itemhtml .= '<div>$' . number_format($i['qty'] * $i['amount'] * 1.0, 2) . '</div>';
$itemhtml .= '</div>';
$total += $i['qty'] * $i['amount'] * 1.0;
$subtotal += $i['qty'] * $i['amount'] * 1.0;
}
$total = $subtotal * (1.0 - ((float) $tx['discountpercent'] / 100));
$paymenthtml = "";
$payments = $database->select('payments', [
'[>]payment_types' => ['type' => 'typeid']
@ -177,9 +188,15 @@ switch ($VARS['action']) {
$change = 0.0;
}
$totalstr = number_format($total, 2);
$subtotalstr = number_format($subtotal, 2);
$paidstr = number_format($paid, 2);
$changestr = number_format($change, 2);
$totalstr = $subtotalstr;
$discountstr = "";
if ($tx['discountpercent'] > 0) {
$discountstr = '<div class="flexrow"><span>Discount: </span><span>' . (float) $tx['discountpercent'] . '% off</span></div>';
$totalstr = number_format($total, 2);
}
$html = <<<END
<!DOCTYPE html>
@ -203,6 +220,8 @@ $customerline
$itemhtml
</div>
<hr />
<div class="flexrow"><span>Subtotal: </span><span>$$subtotalstr</span></div>
$discountstr
<b class="flexrow"><span>Total: </span><span>$$totalstr</span></b>
<hr />
<div id="payments">

Binary file not shown.

@ -47,4 +47,5 @@ define("STRINGS", [
"print" => "Print",
"customer" => "Customer",
"customer search" => "Search customers",
"new sale" => "New Sale",
]);

@ -19,7 +19,7 @@
<iframe class="w-100" id="receiptframe"></iframe>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal"><?php lang("close"); ?></button>
<button type="button" class="btn btn-secondary" data-dismiss="modal"><?php lang("new sale"); ?></button>
<button type="button" class="btn btn-primary" id="receiptprintbtn"><i class="fas fa-print"></i> <?php lang("print"); ?></button>
</div>
</div>
@ -83,7 +83,8 @@
<span id="customerbtnlabel"></span>
<span class="sr-only"><?php lang("customer"); ?></span>
</div>
<div class="btn m-1">
<div class="btn m-1" id="discountpercentbtn" data-percent="0">
<span id="discountpercentbtnlabel"></span>
<i class="fas fa-percent"></i>
<span class="sr-only"><?php lang("transaction discount"); ?></span>
</div>

@ -25,6 +25,11 @@ function recalculate() {
paid += line;
});
var discountpercent = $("#discountpercentbtn").data("percent");
if (discountpercent > 0 && discountpercent < 100) {
total *= 1.0 - ((discountpercent * 1.0) / 100);
}
remaining = total - paid;
change = (total - paid) * -1.0;
if (remaining <= 0) {

@ -9,6 +9,10 @@ function sendTransactionToServer(callback) {
var payments = [];
var customer = customerid;
var register = '';
var discountpercent = $("#discountpercentbtn").data("percent");
if (discountpercent <= 0) {
discountpercent = 0.0;
}
$("#pos-lines-box .list-group-item").each(function () {
var each = $(".item-price", this).val() * 1.0;
var qty = $(".item-qty", this).val() * 1.0;
@ -40,7 +44,8 @@ function sendTransactionToServer(callback) {
items: items,
payments: payments,
customer: customer,
register: register
register: register,
discountpercent: discountpercent
}, function (data) {
if (data.status == "OK") {
callback(data);

@ -108,4 +108,22 @@ $("#paymentbtn").click(function () {
$(".payment-method-button").click(function () {
addPayment($(this).data("payment-method"), $(this).data("icon"), $(this).data("text"));
});
});
$("#discountpercentbtn").click(function () {
bsprompt("Sale Discount",
"Enter a percentage to discount the transaction",
"Discount",
"Cancel",
"number",
function (result) {
if (result <= 0 || result >= 100) {
$("#discountpercentbtn").data("percent", 0.0);
$("#discountpercentbtnlabel").text("");
return;
}
$("#discountpercentbtn").data("percent", result);
$("#discountpercentbtnlabel").text(result);
recalculate();
});
})
Loading…
Cancel
Save