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; +} + +?> + +
+ = is_arabic() ? ($company_info['address_ar'] ?? '') : ($company_info['address_en'] ?? '') ?>
+ = __('phone') ?>: = $company_info['phone'] ?? '' ?> | = __('email') ?>: = $company_info['email'] ?? '' ?>
+
+ = __('vat_no') ?>: = $company_info['vat_no'] ?>
+
+
+ | = __('ctr_no') ?>: = $company_info['ctr_no'] ?>
+
+
= __('date') ?>: = date('d/m/Y') ?>
+ ++ = $from_date ? __('from_date') . ': ' . date('d/m/Y', strtotime($from_date)) : '' ?> + = $to_date ? ' ' . __('to_date') . ': ' . date('d/m/Y', strtotime($to_date)) : '' ?> +
+ += $customer['phone'] ?> | = $customer['email'] ?>
+= $customer['phone'] ?> | = $customer['email'] ?>
+| = __('date') ?> | += __('description') ?> | += __('ref') ?? 'Ref' ?> | += __('debit') ?> | += __('credit') ?> | += __('balance') ?> | +
|---|---|---|---|---|---|
| + = __('no_transactions_found') ?? 'No transactions found' ?> + | +|||||
| = date('d/m/Y H:i', strtotime($t['date'])) ?> | += $t['description'] ?> | += $t['ref'] ?> | += $t['debit'] > 0 ? format_amount($t['debit']) : '-' ?> | += $t['credit'] > 0 ? format_amount($t['credit']) : '-' ?> | += format_amount($t['running_balance']) ?> | +
| = __('total') ?> | += format_amount($total_debit) ?> | += format_amount($total_credit) ?> | += format_amount($balance) ?> | +||