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'] ?? ''; $active_tab = $_GET['tab'] ?? 'financial'; // Financial Logic $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 = ?"; if ($from_date) { $orders_sql .= " AND DATE(created_at) >= ?"; $payments_sql .= " AND DATE(p.created_at) >= ?"; } if ($to_date) { $orders_sql .= " AND DATE(created_at) <= ?"; $payments_sql .= " AND DATE(p.created_at) <= ?"; } $stmt_orders = db()->prepare($orders_sql); $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(); $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; } // Loyalty Logic $loyalty_sql = "SELECT lt.*, o.order_number FROM loyalty_transactions lt LEFT JOIN orders o ON lt.order_id = o.id WHERE lt.customer_id = ?"; if ($from_date) $loyalty_sql .= " AND DATE(lt.created_at) >= ?"; if ($to_date) $loyalty_sql .= " AND DATE(lt.created_at) <= ?"; $loyalty_sql .= " ORDER BY lt.created_at ASC"; $stmt_loyalty = db()->prepare($loyalty_sql); $loyalty_params = [$customer_id]; if ($from_date) $loyalty_params[] = $from_date; if ($to_date) $loyalty_params[] = $to_date; $stmt_loyalty->execute($loyalty_params); $loyalty_transactions = $stmt_loyalty->fetchAll(); $loyalty_balance = 0; foreach ($loyalty_transactions as &$lt) { $loyalty_balance += $lt['points']; $lt['running_balance'] = $loyalty_balance; } $display_company_name = ($lang === 'ar' ? ($company_info['name_ar'] ?: $company_info['name_en']) : $company_info['name_en']) ?: 'Laundry Brand'; $display_address = ($lang === 'ar' ? ($company_info['address_ar'] ?: $company_info['address_en']) : $company_info['address_en']); ?>
= __('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['email'] : '' ?>
= $customer['phone'] ?> = $customer['email'] ? ' | '.$customer['email'] : '' ?>
| = __('date') ?> | = __('description') ?> | = __('ref') ?> | = __('debit') ?> | = __('credit') ?> | = __('balance') ?> |
|---|---|---|---|---|---|
| = __('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) ?> | ||
| = __('date') ?> | = __('description') ?> | = __('ref') ?> | = __('earned') ?? 'Earned' ?> | = __('redeemed') ?? 'Redeemed' ?> | = __('balance') ?> |
|---|---|---|---|---|---|
| = __('no_transactions_found') ?> | |||||
| = date('d/m/Y H:i', strtotime($lt['created_at'])) ?> | = $lt['description'] ?> = __($lt['type']) ?> | = $lt['order_number'] ? ''.$lt['order_number'].'' : '-' ?> | = $lt['points'] > 0 ? '+'.number_format($lt['points'], 2) : '-' ?> | = $lt['points'] < 0 ? number_format($lt['points'], 2) : '-' ?> | = number_format($lt['running_balance'], 2) ?> |
| = __('current_balance') ?? 'Current Balance' ?> | = number_format($customer['loyalty_points'], 2) ?> | ||||