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; 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)])); } } foreach ($payments as $p) { if (!$database->has('payment_types', ['typename' => $p['type']])) { exit(json_encode(["status" => "ERROR", "message" => lang("invalid payment type", 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 ($totalcharge > $totalpaid) { exit(json_encode(["status" => "ERROR", "message" => lang("insufficient payment", 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 ]); $txid = $database->id(); foreach ($items as $i) { $itemname = $binstack->get('items', 'name', ['itemid' => $i['id']]); $database->insert('lines', [ 'txid' => $txid, 'amount' => $i['each'], 'name' => $itemname, 'itemid' => $i['id'], 'qty' => $i['qty'] ]); } 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 ]); } exit(json_encode(["status" => "OK", "txid" => $txid])); break; case "getreceipt": header("Content-Type: text/html"); if (!$database->has('transactions', ['txid' => $VARS['txid']])) { exit(json_encode(["status" => "ERROR", "txid" => null])); } $tx = $database->get('transactions', ['txid', 'txdate', 'customerid', 'type', 'cashier'], ['txid' => $VARS['txid']]); $txid = $tx['txid']; $datetime = date(DATETIME_FORMAT, strtotime($tx['txdate'])); $type = $tx['type']; $cashier = getUserByID($tx['cashier'])['name']; $customerid = $tx['customerid']; $customerline = (is_null($customerid) ? "" : "
Customer: $customerid"); $itemhtml = ""; $items = $database->select('lines', ['amount', 'name', 'itemid', 'qty'], ['txid' => $txid]); $total = 0.0; foreach ($items as $i) { $itemhtml .= "\n"; $itemhtml .= '
'; $itemhtml .= '
' . $i['name'] . '
'; $itemhtml .= '
$' . $i['amount'] . '
'; $itemhtml .= '
x' . $i['qty'] . '
'; $itemhtml .= '
$' . ($i['qty'] * $i['amount']) . '
'; $itemhtml .= '
'; $total += ($i['qty'] * $i['amount']); } $paymenthtml = ""; $payments = $database->select('payments', [ '[>]payment_types' => ['type' => 'typeid'] ], [ 'amount', 'type', 'typename', 'text' ], [ 'txid' => $txid ]); foreach ($payments as $p) { $paymenthtml .= "\n"; $paymenthtml .= '
'; $paymenthtml .= '
' . lang($p['text'], false) . '
'; $paymenthtml .= '
$' . $p['amount'] . '
'; $paymenthtml .= '
'; } $html = << Tx #$txid
Date: $datetime
Tx. ID: $txid
Cashier: $cashier $customerline
$itemhtml

$paymenthtml

Total: $$total END; exit($html); break; case "itemsearch": header("Content-Type: application/json"); if (!is_empty($VARS['q'])) { $where["AND"]["OR"] = [ "name[~]" => $VARS['q'], "code1[~]" => $VARS['q'], "code2[~]" => $VARS['q'] ]; } else { exit(json_encode(["status" => "ERROR", "items" => false])); } $items = $binstack->select('items', [ 'itemid (id)', 'name', 'code1', 'code2', 'cost', 'price' ], $where); $items = (count($items) > 0 ? $items : false); exit(json_encode(["status" => "OK", "items" => $items])); case "giftcard_lookup": header("Content-Type: application/json"); $code = $VARS['code']; if (empty($code)) { exit(json_encode(["status" => "ERROR", "cards" => []])); } $cards = $database->select('certificates', ['certid (id)', 'certcode (code)', 'amount (balance)', 'start_amount (amount)'], ['certcode' => $code]); exit(json_encode(["status" => "OK", "cards" => $cards])); break; case "session_keepalive": header("Content-Type: application/json"); exit(json_encode(["status" => "OK"])); case "signout": session_destroy(); header('Location: index.php'); die("Logged out."); }