0.0005) { apply_sale_payment($id, $summary['due_amount'], true); } } } catch (Throwable $e) {} $redirect = $_GET['redirect'] ?? 'sales.php'; header('Location: ' . $redirect); exit; } if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'delete_sale') { if (!$canDeleteSales) { set_flash('danger', tr('ليس لديك صلاحية حذف الفواتير.', 'You do not have permission to delete invoices.')); redirect_to('sales.php', $_GET); } $id = (int) ($_POST['id'] ?? 0); try { ensure_sales_table(); $sale = fetch_sale($id); if (!$sale) { set_flash('warning', tr('الفاتورة غير موجودة.', 'Invoice was not found.')); } elseif ($user['role'] !== 'owner' && !in_array((string) ($sale['branch_code'] ?? ''), $allowedBranches, true)) { set_flash('danger', tr('لا يمكنك حذف هذه الفاتورة.', 'You cannot delete this invoice.')); } else { $stmt = db()->prepare('DELETE FROM sales_orders WHERE id = :id'); $stmt->execute([':id' => $id]); set_flash('success', tr('تم حذف الفاتورة نهائياً.', 'Invoice deleted permanently.')); } } catch (Throwable $e) { set_flash('danger', tr('تعذر حذف الفاتورة.', 'Failed to delete invoice.')); } redirect_to('sales.php', $_GET); } $dbError = null; $sales = []; $totalPages = 1; $page = max(1, (int)($_GET['p'] ?? 1)); $limit = 10; $offset = ($page - 1) * $limit; try { ensure_sales_table(); $params = []; $where = ' WHERE 1=1 '; if ($mode) { $where .= ' AND sale_mode = :sale_mode '; $params[':sale_mode'] = $mode; } if ($branch) { $where .= ' AND branch_code = :branch_code '; $params[':branch_code'] = $branch; } if ($user && $user['role'] !== 'owner') { $ubranches = get_user_branches($user); if (empty($ubranches)) { $where .= ' AND 1=0 '; } else { $namedParams = []; foreach ($ubranches as $i => $ub) { $key = ':v_branch_' . $i; $namedParams[] = $key; $params[$key] = $ub; } $where .= ' AND branch_code IN (' . implode(', ', $namedParams) . ') '; } } if ($search !== '') { $where .= ' AND (receipt_no LIKE :search OR cashier_name LIKE :search OR customer_name LIKE :search)'; $params[':search'] = "%$search%"; } if ($paymentStatus !== '') { $where .= ' AND payment_status = :payment_status '; $params[':payment_status'] = $paymentStatus; } if ($deliveryStatus !== '') { $where .= ' AND delivery_status = :delivery_status '; $params[':delivery_status'] = $deliveryStatus; } if ($dateFrom !== '') { $where .= ' AND DATE(sale_date) >= :date_from '; $params[':date_from'] = $dateFrom; } if ($dateTo !== '') { $where .= ' AND DATE(sale_date) <= :date_to '; $params[':date_to'] = $dateTo; } if ($statusFilter === 'order') { $where .= " AND status = 'order' "; } elseif ($statusFilter === 'completed') { $where .= " AND COALESCE(status, 'completed') = 'completed' "; } else { $where .= " AND COALESCE(status, 'completed') <> 'order' "; } // Pagination counts $countSql = 'SELECT COUNT(*) FROM sales_orders' . $where; $countStmt = db()->prepare($countSql); foreach ($params as $key => $value) { $countStmt->bindValue($key, $value); } $countStmt->execute(); $total = $countStmt->fetchColumn(); $totalPages = max(1, ceil($total / $limit)); // Fetch Data $sql = 'SELECT * FROM sales_orders' . $where . ' ORDER BY sale_date DESC LIMIT :limit OFFSET :offset'; $stmt = db()->prepare($sql); foreach ($params as $key => $value) { $stmt->bindValue($key, $value); } $stmt->bindValue(':limit', $limit, PDO::PARAM_INT); $stmt->bindValue(':offset', $offset, PDO::PARAM_INT); $stmt->execute(); $sales = $stmt->fetchAll(); } catch (Throwable $e) { $dbError = $e->getMessage(); } $queryState = static function (array $extra = []) use ($search, $branch, $mode, $statusFilter, $paymentStatus, $deliveryStatus, $dateFrom, $dateTo): array { return array_filter(array_merge([ 'q' => $search, 'branch' => $branch, 'mode' => $mode, 'status' => $statusFilter, 'payment_status' => $paymentStatus, 'delivery_status' => $deliveryStatus, 'date_from' => $dateFrom, 'date_to' => $dateTo, ], $extra), static fn ($value) => $value !== null && $value !== ''); }; $hasAdvancedFilters = $mode !== null || $paymentStatus !== '' || $branch !== null || $deliveryStatus !== '' || $dateFrom !== '' || $dateTo !== '' || $statusFilter !== ''; require __DIR__ . '/includes/header.php'; ?>

POS
>

POS
8) { $displayPhone = substr($displayPhone, 3); } } ?>
0.0005): ?>
1): ?>