diff --git a/api/search_customers.php b/api/search_customers.php new file mode 100644 index 0000000..54b949d --- /dev/null +++ b/api/search_customers.php @@ -0,0 +1,25 @@ +prepare("SELECT id, name_en, name_ar, phone FROM customers WHERE name_en LIKE ? OR name_ar LIKE ? OR phone LIKE ? LIMIT 10"); + $stmt->execute(["%$query%", "%$query%", "%$query%"]); + $customers = $stmt->fetchAll(); + echo json_encode($customers); +} catch (Exception $e) { + echo json_encode([]); +} diff --git a/customer_statement.php b/customer_statement.php new file mode 100644 index 0000000..319807e --- /dev/null +++ b/customer_statement.php @@ -0,0 +1,304 @@ +prepare("SELECT * FROM customers WHERE id = ?"); +$stmt->execute([$customer_id]); +$customer = $stmt->fetch(); + +if (!$customer) { + header('Location: customers.php'); + exit; +} + +$from_date = $_GET['from_date'] ?? ''; +$to_date = $_GET['to_date'] ?? ''; + +// Base queries +$orders_sql = "SELECT id, order_number, total_price, created_at FROM orders WHERE customer_id = ?"; +$payments_sql = "SELECT p.id, p.amount, p.payment_method, p.created_at, o.order_number + FROM payments p + JOIN orders o ON p.order_id = o.id + WHERE o.customer_id = ?"; +$params = [$customer_id]; + +if ($from_date) { + $orders_sql .= " AND DATE(created_at) >= ?"; + $payments_sql .= " AND DATE(p.created_at) >= ?"; + $params[] = $from_date; +} +if ($to_date) { + $orders_sql .= " AND DATE(created_at) <= ?"; + $payments_sql .= " AND DATE(p.created_at) <= ?"; + $params[] = $to_date; +} + +$stmt_orders = db()->prepare($orders_sql); +// We need to be careful with params if both filters are set +$orders_params = [$customer_id]; +if ($from_date) $orders_params[] = $from_date; +if ($to_date) $orders_params[] = $to_date; +$stmt_orders->execute($orders_params); +$orders = $stmt_orders->fetchAll(); + +$stmt_payments = db()->prepare($payments_sql); +$payments_params = [$customer_id]; +if ($from_date) $payments_params[] = $from_date; +if ($to_date) $payments_params[] = $to_date; +$stmt_payments->execute($payments_params); +$payments = $stmt_payments->fetchAll(); + +// Combine and sort +$transactions = []; +foreach ($orders as $o) { + $transactions[] = [ + 'date' => $o['created_at'], + 'type' => 'order', + 'ref' => $o['order_number'], + 'debit' => $o['total_price'], + 'credit' => 0, + 'description' => __('order') . ' #' . $o['order_number'] + ]; +} +foreach ($payments as $p) { + $transactions[] = [ + 'date' => $p['created_at'], + 'type' => 'payment', + 'ref' => $p['order_number'], + 'debit' => 0, + 'credit' => $p['amount'], + 'description' => __('payment') . ' (' . __($p['payment_method']) . ') - ' . __('order') . ' #' . $p['order_number'] + ]; +} + +usort($transactions, function($a, $b) { + return strtotime($a['date']) - strtotime($b['date']); +}); + +$total_debit = 0; +$total_credit = 0; +$balance = 0; +foreach ($transactions as &$t) { + $total_debit += $t['debit']; + $total_credit += $t['credit']; + $balance += ($t['debit'] - $t['credit']); + $t['running_balance'] = $balance; +} + +?> + +
+ +
+ +
+
+ +
+ + +
+
+ + +
+
+ + +
+
+
+ +
+ +
+
+
+ + Logo + +

+

+
+ : | :
+ + : + + + | : + +

+
+
+

+
+

:

+ +

+ + +

+ +
+
+
+ +
+
+
+

+

|

+
+
+
+ + +
+

+

|

+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
0 ? format_amount($t['debit']) : '-' ?> 0 ? format_amount($t['credit']) : '-' ?>
+
+ +
+
+
+
+
+
+ : + +
+
+ : + +
+
+ : + +
+
+
+
+
+
+ + + + \ No newline at end of file diff --git a/customers.php b/customers.php index d264b95..2ea97e6 100644 --- a/customers.php +++ b/customers.php @@ -1,8 +1,84 @@ prepare("INSERT INTO customers (phone, name_en, name_ar, email, address_en, address_ar) VALUES (?, ?, ?, ?, ?, ?)"); + $stmt->execute([$phone, $name_en, $name_ar, $email, $address_en, $address_ar]); + header('Location: customers.php?success=customer_added'); + exit; + } + } elseif ($action === 'edit_customer') { + $id = $_POST['id']; + $phone = $_POST['phone'] ?? ''; + $name_en = $_POST['name_en'] ?? ''; + $name_ar = $_POST['name_ar'] ?? ''; + $email = $_POST['email'] ?? ''; + $address_en = $_POST['address_en'] ?? ''; + $address_ar = $_POST['address_ar'] ?? ''; + + $stmt = db()->prepare("UPDATE customers SET phone = ?, name_en = ?, name_ar = ?, email = ?, address_en = ?, address_ar = ? WHERE id = ?"); + $stmt->execute([$phone, $name_en, $name_ar, $email, $address_en, $address_ar, $id]); + header('Location: customers.php?success=customer_updated'); + exit; + } elseif ($action === 'delete_customer') { + $id = $_POST['id']; + + // Check if customer has orders + $stmt = db()->prepare("SELECT COUNT(*) FROM orders WHERE customer_id = ?"); + $stmt->execute([$id]); + if ($stmt->fetchColumn() > 0) { + header('Location: customers.php?error=customer_has_orders'); + exit; + } + + $stmt = db()->prepare("DELETE FROM customers WHERE id = ?"); + $stmt->execute([$id]); + header('Location: customers.php?success=customer_deleted'); + exit; + } +} + $title = 'customers'; require_once __DIR__ . '/includes/header.php'; -$branch_id = $_SESSION['branch_id']; $search = $_GET['search'] ?? ''; $sql = "SELECT * FROM customers WHERE 1=1"; @@ -34,14 +110,29 @@ $customers = $stmt->fetchAll(); - - + + + + +
@@ -55,8 +146,8 @@ $customers = $stmt->fetchAll(); - - + - - +
@@ -65,16 +156,26 @@ $customers = $stmt->fetchAll();
- - - +
+ + + + + + + +
@@ -86,39 +187,129 @@ $customers = $stmt->fetchAll(); - - -