fix: restore missing add_quotation logic and correct db insert columns
This commit is contained in:
parent
f42ddae7e9
commit
5a929e099e
57
index.php
57
index.php
@ -926,6 +926,61 @@ function getPromotionalPrice($item) {
|
|||||||
} catch (Exception $e) { $db->rollBack(); $message = "Error: " . $e->getMessage(); }
|
} 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'])) {
|
if (isset($_POST['edit_quotation'])) {
|
||||||
$db = db();
|
$db = db();
|
||||||
try {
|
try {
|
||||||
@ -977,7 +1032,7 @@ function getPromotionalPrice($item) {
|
|||||||
$vatRate = (float)$stmtVat->fetchColumn();
|
$vatRate = (float)$stmtVat->fetchColumn();
|
||||||
$vatAmount = $subtotal * ($vatRate / 100);
|
$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();
|
$db->commit();
|
||||||
$msg = "Quotation #$quot_id updated!";
|
$msg = "Quotation #$quot_id updated!";
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user