diff --git a/api/checkout.php b/api/checkout.php index 864e2e9..195ea64 100644 --- a/api/checkout.php +++ b/api/checkout.php @@ -14,6 +14,7 @@ $customer_id = $input['customer_id'] ?: null; $items = $input['items'] ?? []; $vat_total = (float)($input['vat_total'] ?? 0); $total_price = (float)($input['total_price'] ?? 0); +$payment_method = $input['payment_method'] ?? null; $branch_id = $_SESSION['branch_id']; $user_id = $_SESSION['user_id']; @@ -26,10 +27,12 @@ try { $pdo = db(); $pdo->beginTransaction(); + $payment_status = ($payment_method && $payment_method !== 'pay_later') ? 'paid' : 'unpaid'; + if ($order_id) { // Update existing order - $stmt = $pdo->prepare("UPDATE orders SET customer_id = ?, total_price = ?, vat_total = ? WHERE id = ? AND branch_id = ?"); - $stmt->execute([$customer_id, $total_price, $vat_total, $order_id, $branch_id]); + $stmt = $pdo->prepare("UPDATE orders SET customer_id = ?, total_price = ?, vat_total = ?, payment_status = ? WHERE id = ? AND branch_id = ?"); + $stmt->execute([$customer_id, $total_price, $vat_total, $payment_status, $order_id, $branch_id]); // Remove existing items $stmt = $pdo->prepare("DELETE FROM order_items WHERE order_id = ?"); @@ -37,8 +40,8 @@ try { } else { // Create new order $stmt = $pdo->prepare("INSERT INTO orders (branch_id, customer_id, user_id, order_number, total_price, vat_total, status, payment_status) - VALUES (?, ?, ?, NULL, ?, ?, 'received', 'unpaid')"); - $stmt->execute([$branch_id, $customer_id, $user_id, $total_price, $vat_total]); + VALUES (?, ?, ?, NULL, ?, ?, 'received', ?)"); + $stmt->execute([$branch_id, $customer_id, $user_id, $total_price, $vat_total, $payment_status]); $order_id = $pdo->lastInsertId(); // Get branch prefix @@ -49,14 +52,12 @@ try { $prefix = strtoupper(str_pad($prefix, 3, 'X')); // Determine the next serial number for this branch - // We look at existing order numbers for this branch that follow the XXX-###### format $stmt_max = $pdo->prepare("SELECT order_number FROM orders WHERE branch_id = ? AND order_number LIKE ? ORDER BY id DESC LIMIT 100"); $stmt_max->execute([$branch_id, "$prefix-%"]); $existing_orders = $stmt_max->fetchAll(PDO::FETCH_COLUMN); $max_serial = 0; foreach ($existing_orders as $onum) { - // Format is XXX-######. if (preg_match('/' . preg_quote($prefix) . '-(\d{6})/', $onum, $matches)) { $serial = (int)$matches[1]; if ($serial > $max_serial) $max_serial = $serial; @@ -88,6 +89,12 @@ try { ]); } + // Handle Payment recording + if ($payment_method && $payment_method !== 'pay_later') { + $stmt_payment = $pdo->prepare("INSERT INTO payments (order_id, amount, payment_method) VALUES (?, ?, ?)"); + $stmt_payment->execute([$order_id, $total_price, $payment_method]); + } + $pdo->commit(); echo json_encode(['success' => true, 'order_id' => $order_id]); } catch (Exception $e) { diff --git a/pos.php b/pos.php index 2742f81..6436f39 100644 --- a/pos.php +++ b/pos.php @@ -288,6 +288,48 @@ $pageTitle = $edit_order ? ($lang == 'en' ? 'Edit Order #' . $edit_order['order_ + +
+ + + +