From a4b8ae74c2ebe5a6f0900e1a43cefad34834f76a Mon Sep 17 00:00:00 2001 From: Flatlogic Bot Date: Mon, 16 Feb 2026 11:24:21 +0000 Subject: [PATCH] Autosave: 20260216-112421 --- index.php | 66 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 46 insertions(+), 20 deletions(-) diff --git a/index.php b/index.php index f783867..2e961b0 100644 --- a/index.php +++ b/index.php @@ -378,6 +378,11 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $stmt->execute([$customer_id, $invoice_date, $type, $payment_type, $status, $subtotal, $total_vat, $total_with_vat, $paid_amount]); $invoice_id = $db->lastInsertId(); + if ($paid_amount > 0) { + $stmt = $db->prepare("INSERT INTO payments (invoice_id, payment_date, amount, payment_method, notes) VALUES (?, ?, ?, ?, ?)"); + $stmt->execute([$invoice_id, $invoice_date, $paid_amount, 'Cash', 'Initial payment']); + } + foreach ($items_data as $item) { $stmt = $db->prepare("INSERT INTO invoice_items (invoice_id, item_id, quantity, unit_price, total_price) VALUES (?, ?, ?, ?, ?)"); $stmt->execute([$invoice_id, $item['id'], $item['qty'], $item['price'], $item['total']]); @@ -454,9 +459,11 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $db->beginTransaction(); try { // Get old invoice type and items to revert stock - $stmt = $db->prepare("SELECT type FROM invoices WHERE id = ?"); + $stmt = $db->prepare("SELECT type, paid_amount FROM invoices WHERE id = ?"); $stmt->execute([$invoice_id]); - $type = $stmt->fetchColumn(); + $inv_info = $stmt->fetch(); + $type = $inv_info['type']; + $old_paid_amount = (float)$inv_info['paid_amount']; $stmt = $db->prepare("SELECT item_id, quantity FROM invoice_items WHERE invoice_id = ?"); $stmt->execute([$invoice_id]); @@ -502,7 +509,13 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { } $total_with_vat = $subtotal + $total_vat; - $paid_amount = ($status === 'paid') ? $total_with_vat : (($status === 'unpaid') ? 0 : $_POST['paid_amount'] ?? 0); + $paid_amount = ($status === 'paid') ? $total_with_vat : (($status === 'unpaid') ? 0 : ($old_paid_amount > 0 ? $old_paid_amount : ($_POST['paid_amount'] ?? 0))); + + if ($paid_amount > $old_paid_amount) { + $diff = $paid_amount - $old_paid_amount; + $stmt = $db->prepare("INSERT INTO payments (invoice_id, payment_date, amount, payment_method, notes) VALUES (?, ?, ?, ?, ?)"); + $stmt->execute([$invoice_id, $invoice_date, $diff, 'Cash', 'Payment via edit']); + } // Update invoice $stmt = $db->prepare("UPDATE invoices SET customer_id = ?, invoice_date = ?, payment_type = ?, status = ?, total_amount = ?, vat_amount = ?, total_with_vat = ?, paid_amount = ? WHERE id = ?"); @@ -673,11 +686,11 @@ if ($page === 'export') { if (!empty($_GET['start_date'])) { $where[] = "v.invoice_date >= ?"; $params[] = $_GET['start_date']; } if (!empty($_GET['end_date'])) { $where[] = "v.invoice_date <= ?"; $params[] = $_GET['end_date']; } $whereSql = implode(" AND ", $where); - $stmt = db()->prepare("SELECT v.id, c.name as customer_name, v.invoice_date, v.payment_type, v.status, v.total_amount, v.vat_amount, v.total_with_vat + $stmt = db()->prepare("SELECT v.id, c.name as customer_name, v.invoice_date, v.payment_type, v.status, v.total_with_vat, v.paid_amount, (v.total_with_vat - v.paid_amount) as balance FROM invoices v LEFT JOIN customers c ON v.customer_id = c.id WHERE $whereSql ORDER BY v.id DESC"); $stmt->execute($params); - fputcsv($output, ['Invoice ID', 'Customer/Supplier', 'Date', 'Payment', 'Status', 'Subtotal', 'VAT', 'Total']); + fputcsv($output, ['Invoice ID', 'Customer/Supplier', 'Date', 'Payment', 'Status', 'Total', 'Paid', 'Balance']); while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) fputcsv($output, $row); } elseif ($type === 'customers' || $type === 'suppliers') { $custType = ($type === 'suppliers') ? 'supplier' : 'customer'; @@ -822,7 +835,10 @@ switch ($page) { $data['stats'] = [ 'total_customers' => db()->query("SELECT COUNT(*) FROM customers WHERE type = 'customer'")->fetchColumn(), 'total_items' => db()->query("SELECT COUNT(*) FROM stock_items")->fetchColumn(), + 'total_sales' => db()->query("SELECT SUM(total_with_vat) FROM invoices WHERE type = 'sale'")->fetchColumn() ?: 0, + 'total_received' => db()->query("SELECT SUM(amount) FROM payments p JOIN invoices i ON p.invoice_id = i.id WHERE i.type = 'sale'")->fetchColumn() ?: 0, ]; + $data['stats']['total_receivable'] = $data['stats']['total_sales'] - $data['stats']['total_received']; break; } @@ -982,28 +998,34 @@ $projectDescription = $_SERVER['PROJECT_DESCRIPTION'] ?? 'Accounting System';
-
-
+
+
Total Sales
-
OMR 24,500.000
+
OMR
-
-
+
+
Total Customers
-
+
-
-
+
+
Total Items
-
+
-
-
-
Net Profit
-
OMR 20,300.000
+
+
+
Total Received
+
OMR
+
+
+
+
+
Total Balance
+
OMR
@@ -1542,7 +1564,9 @@ $projectDescription = $_SERVER['PROJECT_DESCRIPTION'] ?? 'Accounting System'; Date Status - Total Amount + Total + Paid + Balance Actions @@ -1583,7 +1607,9 @@ $projectDescription = $_SERVER['PROJECT_DESCRIPTION'] ?? 'Accounting System'; - OMR + OMR + OMR + OMR