query('SELECT id, name_ar, name_en FROM categories ORDER BY name_ar ASC')->fetchAll(); $customers = $pdo->query('SELECT id, name, phone FROM customers ORDER BY name ASC')->fetchAll(); } catch (Throwable $e) { $categories = []; $customers = []; } if ($_SERVER['REQUEST_METHOD'] === 'POST') { $branchCode = trim((string) ($_POST['branch_code'] ?? '')); $customerId = isset($_POST['customer_id']) && $_POST['customer_id'] !== '' ? (int)$_POST['customer_id'] : null; $customerName = trim((string) ($_POST['customer_name'] ?? '')); $paymentMethod = trim((string) ($_POST['payment_method'] ?? 'cash')); $paymentStatus = ($paymentMethod === 'pay_later') ? 'unpaid' : 'paid'; $notes = trim((string) ($_POST['notes'] ?? '')); $cartJson = (string) ($_POST['cart_json'] ?? '[]'); $items = json_decode($cartJson, true); if (!in_array($branchCode, $allowedBranches, true)) { $error = tr('اختر فرعاً صالحاً لهذه الصلاحية.', 'Choose a valid branch for this role.'); } elseif (!in_array($paymentMethod, ['cash', 'card', 'transfer', 'pay_later'], true)) { $error = tr('اختر طريقة دفع صحيحة.', 'Choose a valid payment method.'); } elseif ($paymentMethod === 'pay_later' && !$customerId) { $error = tr('يجب اختيار عميل مسجل للدفع الآجل.', 'You must select a registered customer for pay later.'); } elseif (!is_array($items) || $items === []) { $error = tr('أضف صنفاً واحداً على الأقل إلى السلة.', 'Add at least one item to the cart.'); } else { $normalized = []; $subtotal = 0.0; $totalVat = 0.0; $itemCount = 0; foreach ($items as $item) { $sku = (string) ($item['sku'] ?? ''); $qty = (int) ($item['qty'] ?? 0); if (!isset($catalog[$sku]) || $qty < 1) { continue; } $product = $catalog[$sku]; $price = (float) $product['price']; $lineTotal = $price * $qty; $normalized[] = [ 'sku' => $sku, 'name_ar' => $product['name_ar'], 'name_en' => $product['name_en'], 'qty' => $qty, 'price' => $price, 'line_total' => $lineTotal, ]; $subtotal += $lineTotal; $vatPercent = (float) ($product['vat'] ?? 0); $itemVat = $lineTotal * ($vatPercent / 100); $totalVat += $itemVat; $itemCount += $qty; } if ($normalized === []) { $error = tr('السلة غير صالحة بعد التحقق من الأصناف.', 'The cart is invalid after product validation.'); } else { $cashierName = current_lang() === 'ar' ? $user['name_ar'] : $user['name_en']; $saleId = create_sale([ 'receipt_no' => receipt_code(), 'sale_mode' => $saleMode, 'branch_code' => $branchCode, 'cashier_username' => $user['username'], 'cashier_name' => $cashierName, 'role_name' => $user['role'], 'customer_id' => $customerId, 'customer_name' => $customerName !== '' ? $customerName : null, 'payment_method' => $paymentMethod, 'payment_status' => $paymentStatus, 'items' => $normalized, 'item_count' => $itemCount, 'subtotal' => $subtotal, 'vat_amount' => $totalVat, 'total_amount' => $subtotal + $totalVat, 'notes' => $notes !== '' ? $notes : null, ]); set_flash('success', tr('تم حفظ عملية POS بنجاح.', 'POS sale saved successfully.')); redirect_to('print_receipt.php', ['id' => $saleId]); } } } require __DIR__ . '/includes/header.php'; ?>
$item): $itemSkuRaw = (string) $sku; $primaryNameRaw = trim((string) (current_lang() === 'ar' ? ($item['name_ar'] ?? '') : ($item['name_en'] ?? ''))); $fallbackNameRaw = trim((string) (current_lang() === 'ar' ? ($item['name_en'] ?? '') : ($item['name_ar'] ?? ''))); $itemNameRaw = $primaryNameRaw !== '' ? $primaryNameRaw : ($fallbackNameRaw !== '' ? $fallbackNameRaw : ('SKU ' . $itemSkuRaw)); $searchBits = [ (string) ($item['name_ar'] ?? ''), (string) ($item['name_en'] ?? ''), $itemSkuRaw, ]; $searchTextRaw = implode(' ', array_filter($searchBits, static fn($value) => trim((string) $value) !== '')); $searchText = function_exists('mb_strtolower') ? mb_strtolower($searchTextRaw, 'UTF-8') : strtolower($searchTextRaw); $itemSku = h($itemSkuRaw); $itemName = h($itemNameRaw); $itemPrice = h($item['price']); $itemCat = h($item['category_id'] ?? ''); $imageUrl = !empty($item['image_url']) ? h($item['image_url']) : ''; $createdAtRaw = (string)($item['created_at'] ?? ''); $createdAtStamp = $createdAtRaw !== '' ? strtotime($createdAtRaw) : false; $isRecentlyAdded = $createdAtStamp && $createdAtStamp >= strtotime('-7 days'); $createdLabel = $createdAtStamp ? date('Y-m-d', $createdAtStamp) : ''; ?>
<?= $imgAlt ?>
SKU:
:
0