|
|
|
@ -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";
|
|
|
|
|