0) { $stmt = db()->prepare('SELECT * FROM sales_orders WHERE id = :id'); $stmt->execute([':id' => $editSaleId]); $editSale = $stmt->fetch(); } if (!$editSale) { die(tr('الفاتورة غير موجودة.', 'Invoice not found.')); } if ($user['role'] !== 'owner' && $editSale['branch_code'] !== $user['branch_code']) { die(tr('غير مصرح لك.', 'Unauthorized.')); } $pageTitle = tr('تعديل فاتورة', 'Edit Invoice') . ' #' . h($editSale['receipt_no']); $activeNav = 'sales'; $error = ''; $catalog = catalog(); $allowedBranches = get_user_branches($user); try { $customers = db()->query('SELECT id, name, phone FROM customers ORDER BY name ASC')->fetchAll(); } catch (Throwable $e) { $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'; $saleStatus = trim((string) ($_POST['sale_status'] ?? 'completed')); $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 invoice.'); } 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; $vatPercent = (float) ($product['vat'] ?? 0); $itemVat = $lineTotal * ($vatPercent / 100); $totalVat += $itemVat; $normalized[] = [ 'sku' => $sku, 'name_ar' => $product['name_ar'], 'name_en' => $product['name_en'], 'qty' => $qty, 'price' => $price, 'line_total' => $lineTotal, 'vat_percent' => $vatPercent, 'vat_amount' => $itemVat ]; $subtotal += $lineTotal; $itemCount += $qty; } if ($normalized === []) { $error = tr('الفاتورة غير صالحة بعد التحقق من الأصناف.', 'The invoice is invalid after product validation.'); } else { $cashierName = current_lang() === 'ar' ? $user['name_ar'] : $user['name_en']; $stmt = db()->prepare('UPDATE sales_orders SET branch_code = :branch_code, customer_id = :customer_id, customer_name = :customer_name, payment_method = :payment_method, payment_status = :payment_status, items_json = :items_json, item_count = :item_count, subtotal = :subtotal, vat_amount = :vat_amount, total_amount = :total_amount, status = :status, notes = :notes WHERE id = :id'); $stmt->execute([ ':branch_code' => $branchCode, ':customer_id' => $customerId, ':customer_name' => $customerName !== '' ? $customerName : null, ':payment_method' => $paymentMethod, ':payment_status' => $paymentStatus, ':items_json' => json_encode($normalized, JSON_UNESCAPED_UNICODE), ':item_count' => $itemCount, ':subtotal' => $subtotal, ':total_amount' => $subtotal, ':status' => $saleStatus, ':notes' => $notes !== '' ? $notes : null, ':id' => $editSaleId, ]); set_flash('success', tr('تم تحديث الفاتورة بنجاح.', 'Invoice updated successfully.')); redirect_to('sale.php', ['id' => $editSaleId]); } } } require __DIR__ . '/includes/header.php'; ?>

0.000
0.000
0.000