From 5a929e099e164041adc907c3682a6b700b74941c Mon Sep 17 00:00:00 2001 From: Flatlogic Bot Date: Sun, 22 Feb 2026 07:26:21 +0000 Subject: [PATCH] fix: restore missing add_quotation logic and correct db insert columns --- index.php | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/index.php b/index.php index d95e2b8..2edf0a4 100644 --- a/index.php +++ b/index.php @@ -926,6 +926,61 @@ function getPromotionalPrice($item) { } catch (Exception $e) { $db->rollBack(); $message = "Error: " . $e->getMessage(); } } + if (isset($_POST["add_quotation"])) { + $db = db(); + try { + $db->beginTransaction(); + $cust_id = (int)$_POST["customer_id"]; + $quot_date = $_POST["quotation_date"] ?: date("Y-m-d"); + $valid_until = $_POST["valid_until"] ?: null; + $status = $_POST["status"] ?? "pending"; + + $items = $_POST["item_ids"] ?? []; + $qtys = $_POST["quantities"] ?? []; + $prices = $_POST["prices"] ?? []; + + $total_subtotal = 0; + $total_vat = 0; + + foreach ($items as $i => $item_id) { + if (!$item_id) continue; + $qty = (float)$qtys[$i]; + $price = (float)$prices[$i]; + $subtotal = $qty * $price; + + $stmtVat = $db->prepare("SELECT vat_rate FROM stock_items WHERE id = ?"); + $stmtVat->execute([$item_id]); + $vatRate = (float)$stmtVat->fetchColumn(); + + $vatAmount = $subtotal * ($vatRate / 100); + $total_subtotal += $subtotal; + $total_vat += $vatAmount; + } + + $total_with_vat = $total_subtotal + $total_vat; + + $stmt = $db->prepare("INSERT INTO quotations (customer_id, quotation_date, valid_until, status, total_amount, vat_amount, total_with_vat) VALUES (?, ?, ?, ?, ?, ?, ?)"); + $stmt->execute([$cust_id, $quot_date, $valid_until, $status, $total_subtotal, $total_vat, $total_with_vat]); + $quot_id = $db->lastInsertId(); + + foreach ($items as $i => $item_id) { + if (!$item_id) continue; + $qty = (float)$qtys[$i]; + $price = (float)$prices[$i]; + $subtotal = $qty * $price; + + $stmtVat = $db->prepare("SELECT vat_rate FROM stock_items WHERE id = ?"); + $stmtVat->execute([$item_id]); + $vatRate = (float)$stmtVat->fetchColumn(); + $vatAmount = $subtotal * ($vatRate / 100); + $db->prepare("INSERT INTO quotation_items (quotation_id, item_id, quantity, unit_price, total_price) VALUES (?, ?, ?, ?, ?)")->execute([$quot_id, $item_id, $qty, $price, $subtotal]); + } + $db->commit(); + $msg = "Quotation #$quot_id created!"; + redirectWithMessage($msg, "index.php?page=quotations"); + } catch (Exception $e) { $db->rollBack(); $message = "Error: " . $e->getMessage(); } + } + if (isset($_POST['edit_quotation'])) { $db = db(); try { @@ -977,7 +1032,7 @@ function getPromotionalPrice($item) { $vatRate = (float)$stmtVat->fetchColumn(); $vatAmount = $subtotal * ($vatRate / 100); - $db->prepare("INSERT INTO quotation_items (quotation_id, item_id, quantity, unit_price, vat_amount, total_price) VALUES (?, ?, ?, ?, ?, ?)")->execute([$quot_id, $item_id, $qty, $price, $vatAmount, $subtotal]); + $db->prepare("INSERT INTO quotation_items (quotation_id, item_id, quantity, unit_price, total_price) VALUES (?, ?, ?, ?, ?)")->execute([$quot_id, $item_id, $qty, $price, $subtotal]); } $db->commit(); $msg = "Quotation #$quot_id updated!";