diff --git a/index.php b/index.php index 1773430..660bbd5 100644 --- a/index.php +++ b/index.php @@ -73,6 +73,37 @@ if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['action'])) { echo json_encode($payment); exit; } + + if ($_GET['action'] === 'get_held_carts') { + header('Content-Type: application/json'); + $stmt = db()->query("SELECT h.*, c.name as customer_name FROM pos_held_carts h LEFT JOIN customers c ON h.customer_id = c.id ORDER BY h.id DESC"); + echo json_encode($stmt->fetchAll(PDO::FETCH_ASSOC)); + exit; + } + + if ($_GET['action'] === 'validate_discount') { + header('Content-Type: application/json'); + $code = $_GET['code'] ?? ''; + $stmt = db()->prepare("SELECT * FROM discount_codes WHERE code = ? AND status = 'active' AND (expiry_date IS NULL OR expiry_date >= CURDATE())"); + $stmt->execute([$code]); + $discount = $stmt->fetch(PDO::FETCH_ASSOC); + if ($discount) { + echo json_encode(['success' => true, 'discount' => $discount]); + } else { + echo json_encode(['success' => false, 'error' => 'Invalid or expired code']); + } + exit; + } + + if ($_GET['action'] === 'get_customer_loyalty') { + header('Content-Type: application/json'); + $id = (int)($_GET['customer_id'] ?? 0); + $stmt = db()->prepare("SELECT loyalty_points FROM customers WHERE id = ?"); + $stmt->execute([$id]); + $points = $stmt->fetchColumn(); + echo json_encode(['success' => true, 'points' => (float)$points]); + exit; + } } if ($_SERVER['REQUEST_METHOD'] === 'POST') { @@ -118,10 +149,23 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { // Loyalty Calculation: 1 point per 1 OMR spent on net amount $loyalty_earned = floor($net_amount); - // Check if credit is used for walk-in + // Check if credit is used for walk-in or exceeds limit + $credit_total = 0; foreach ($payments as $p) { - if ($p['method'] === 'credit' && !$customer_id) { - throw new Exception("Credit payment is only allowed for registered customers"); + if ($p['method'] === 'credit') { + if (!$customer_id) { + throw new Exception("Credit payment is only allowed for registered customers"); + } + $credit_total += (float)$p['amount']; + } + } + + if ($customer_id && $credit_total > 0) { + $stmt = $db->prepare("SELECT balance, credit_limit FROM customers WHERE id = ?"); + $stmt->execute([$customer_id]); + $cust = $stmt->fetch(PDO::FETCH_ASSOC); + if ($cust['credit_limit'] > 0 && (abs($cust['balance'] - $credit_total) > $cust['credit_limit'])) { + throw new Exception("Credit limit exceeded. Current Debt: " . number_format(abs($cust['balance']), 3) . ", Limit: " . number_format($cust['credit_limit'], 3)); } } @@ -197,13 +241,6 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { exit; } - if (isset($_GET['action']) && $_GET['action'] === 'get_held_carts') { - header('Content-Type: application/json'); - $stmt = db()->query("SELECT h.*, c.name as customer_name FROM pos_held_carts h LEFT JOIN customers c ON h.customer_id = c.id ORDER BY h.id DESC"); - echo json_encode($stmt->fetchAll(PDO::FETCH_ASSOC)); - exit; - } - if (isset($_POST['action']) && $_POST['action'] === 'delete_held_cart') { header('Content-Type: application/json'); $id = (int)$_POST['id']; @@ -213,31 +250,8 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { exit; } - if (isset($_GET['action']) && $_GET['action'] === 'validate_discount') { - header('Content-Type: application/json'); - $code = $_GET['code'] ?? ''; - $stmt = db()->prepare("SELECT * FROM discount_codes WHERE code = ? AND status = 'active' AND (expiry_date IS NULL OR expiry_date >= CURDATE())"); - $stmt->execute([$code]); - $discount = $stmt->fetch(PDO::FETCH_ASSOC); - if ($discount) { - echo json_encode(['success' => true, 'discount' => $discount]); - } else { - echo json_encode(['success' => false, 'error' => 'Invalid or expired code']); - } - exit; - } - - if (isset($_GET['action']) && $_GET['action'] === 'get_customer_loyalty') { - header('Content-Type: application/json'); - $id = (int)($_GET['customer_id'] ?? 0); - $stmt = db()->prepare("SELECT loyalty_points FROM customers WHERE id = ?"); - $stmt->execute([$id]); - $points = $stmt->fetchColumn(); - echo json_encode(['success' => true, 'points' => (float)$points]); - exit; - } - if (isset($_POST['edit_customer'])) { + $id = (int)$_POST['id']; $name = $_POST['name'] ?? ''; $email = $_POST['email'] ?? ''; @@ -760,7 +774,6 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { } $message = "Settings updated successfully!"; } - } if (isset($_POST['record_payment'])) { $invoice_id = (int)$_POST['invoice_id']; @@ -807,6 +820,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { } } } +} // Routing & Data Fetching @@ -1644,10 +1658,14 @@ $projectDescription = $_SERVER['PROJECT_DESCRIPTION'] ?? 'Accounting System'; -
No held carts
'; - carts.forEach(c => { - html += ` -${lang === 'ar' ? 'لا توجد طلبات معلقة' : 'No held carts found'}
+