updating lpo
This commit is contained in:
parent
e9db807320
commit
fa94409ab3
87
index.php
87
index.php
@ -755,6 +755,7 @@ function getPromotionalPrice($item) {
|
||||
$table = ($type === 'purchase') ? 'purchases' : 'invoices';
|
||||
$item_table = ($type === 'purchase') ? 'purchase_items' : 'invoice_items';
|
||||
$cust_supplier_col = ($type === 'purchase') ? 'supplier_id' : 'customer_id';
|
||||
$fk_col = ($type === 'purchase') ? 'purchase_id' : 'invoice_id';
|
||||
|
||||
$cust_id = (int)$_POST['customer_id'];
|
||||
$inv_date = $_POST['invoice_date'] ?: date('Y-m-d');
|
||||
@ -767,7 +768,6 @@ function getPromotionalPrice($item) {
|
||||
throw new Exception("Please add at least one item.");
|
||||
}
|
||||
$qtys = $_POST['quantities'] ?? [];
|
||||
|
||||
$prices = $_POST['prices'] ?? [];
|
||||
|
||||
$total_subtotal = 0;
|
||||
@ -789,11 +789,14 @@ function getPromotionalPrice($item) {
|
||||
}
|
||||
|
||||
$total_with_vat = $total_subtotal + $total_vat;
|
||||
$paid = (float)($_POST['paid_amount'] ?? 0);
|
||||
if ($status === 'paid') $paid = $total_with_vat;
|
||||
|
||||
$stmt = $db->prepare("INSERT INTO quotations (customer_id, quotation_date, valid_until, status, total_amount, vat_amount, total_with_vat) VALUES (?, ?, ?, 'pending', ?, ?, ?)");
|
||||
$stmt->execute([$cust_id, $quot_date, $valid_until, $total_subtotal, $total_vat, $total_with_vat]);
|
||||
$quot_id = $db->lastInsertId();
|
||||
$stmt = $db->prepare("INSERT INTO $table ($cust_supplier_col, invoice_date, due_date, status, payment_type, total_amount, vat_amount, total_with_vat, paid_amount) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
|
||||
$stmt->execute([$cust_id, $inv_date, $due_date, $status, $pay_type, $total_subtotal, $total_vat, $total_with_vat, $paid]);
|
||||
$inv_id = $db->lastInsertId();
|
||||
|
||||
$items_for_journal = [];
|
||||
foreach ($items as $i => $item_id) {
|
||||
if (!$item_id) continue;
|
||||
$qty = (float)$qtys[$i];
|
||||
@ -805,10 +808,28 @@ 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 $item_table ($fk_col, item_id, quantity, unit_price, vat_amount, total_price) VALUES (?, ?, ?, ?, ?, ?)")->execute([$inv_id, $item_id, $qty, $price, $vatAmount, $subtotal]);
|
||||
|
||||
// Update stock
|
||||
$change = ($type === 'sale') ? -$qty : $qty;
|
||||
$db->prepare("UPDATE stock_items SET stock_quantity = stock_quantity + ? WHERE id = ?")->execute([$change, $item_id]);
|
||||
$items_for_journal[] = ['id' => $item_id, 'qty' => $qty];
|
||||
}
|
||||
|
||||
// Accounting
|
||||
if ($type === 'sale') {
|
||||
recordSaleJournal($inv_id, $total_with_vat, $inv_date, $items_for_journal, $total_vat);
|
||||
} else {
|
||||
// For purchases, you might have recordPurchaseJournal, but let's check if it exists
|
||||
if (function_exists('recordPurchaseJournal')) {
|
||||
recordPurchaseJournal($inv_id, $total_with_vat, $inv_date, $items_for_journal, $total_vat);
|
||||
}
|
||||
}
|
||||
|
||||
$db->commit();
|
||||
$message = "Quotation #$quot_id created!";
|
||||
$message = ($type === 'purchase' ? "Purchase" : "Invoice") . " #$inv_id created!";
|
||||
header("Location: index.php?page=" . ($type === 'purchase' ? 'purchases' : 'sales'));
|
||||
exit;
|
||||
} catch (Exception $e) { $db->rollBack(); $message = "Error: " . $e->getMessage(); }
|
||||
}
|
||||
|
||||
@ -931,6 +952,8 @@ function getPromotionalPrice($item) {
|
||||
}
|
||||
$db->commit();
|
||||
$message = "LPO #$lpo_id created!";
|
||||
header("Location: index.php?page=lpos");
|
||||
exit;
|
||||
} catch (Exception $e) { $db->rollBack(); $message = "Error: " . $e->getMessage(); }
|
||||
}
|
||||
|
||||
@ -1044,6 +1067,53 @@ function getPromotionalPrice($item) {
|
||||
} catch (Exception $e) { $db->rollBack(); $message = "Error: " . $e->getMessage(); }
|
||||
}
|
||||
|
||||
if (isset($_POST['convert_lpo_to_purchase'])) {
|
||||
$db = db();
|
||||
try {
|
||||
$db->beginTransaction();
|
||||
$lpo_id = (int)$_POST['lpo_id'];
|
||||
|
||||
$stmt = $db->prepare("SELECT * FROM lpos WHERE id = ?");
|
||||
$stmt->execute([$lpo_id]);
|
||||
$lpo = $stmt->fetch();
|
||||
|
||||
if (!$lpo) throw new Exception("LPO not found.");
|
||||
if ($lpo['status'] === 'converted') throw new Exception("LPO already converted.");
|
||||
|
||||
$stmtItems = $db->prepare("SELECT * FROM lpo_items WHERE lpo_id = ?");
|
||||
$stmtItems->execute([$lpo_id]);
|
||||
$lItems = $stmtItems->fetchAll();
|
||||
|
||||
// Create Purchase Invoice
|
||||
$inv_date = date('Y-m-d');
|
||||
$stmtPur = $db->prepare("INSERT INTO purchases (supplier_id, invoice_date, status, payment_type, total_amount, vat_amount, total_with_vat, paid_amount) VALUES (?, ?, 'unpaid', 'credit', ?, ?, ?, 0)");
|
||||
$stmtPur->execute([$lpo['supplier_id'], $inv_date, $lpo['total_amount'], $lpo['vat_amount'], $lpo['total_with_vat']]);
|
||||
$pur_id = $db->lastInsertId();
|
||||
|
||||
$items_for_journal = [];
|
||||
foreach ($lItems as $item) {
|
||||
$db->prepare("INSERT INTO purchase_items (purchase_id, item_id, quantity, unit_price, vat_amount, total_price) VALUES (?, ?, ?, ?, ?, ?)")->execute([$pur_id, $item['item_id'], $item['quantity'], $item['unit_price'], $item['vat_amount'], $item['total_amount']]);
|
||||
|
||||
// Update stock
|
||||
$db->prepare("UPDATE stock_items SET stock_quantity = stock_quantity + ? WHERE id = ?")->execute([$item['quantity'], $item['item_id']]);
|
||||
$items_for_journal[] = ['id' => $item['item_id'], 'qty' => $item['quantity']];
|
||||
}
|
||||
|
||||
// Update LPO status
|
||||
$db->prepare("UPDATE lpos SET status = 'converted' WHERE id = ?")->execute([$lpo_id]);
|
||||
|
||||
// Accounting (if exists)
|
||||
if (function_exists('recordPurchaseJournal')) {
|
||||
recordPurchaseJournal($pur_id, $lpo['total_with_vat'], $inv_date, $items_for_journal, $lpo['vat_amount']);
|
||||
}
|
||||
|
||||
$db->commit();
|
||||
$message = "LPO converted to Purchase Invoice #$pur_id successfully!";
|
||||
header("Location: index.php?page=purchases");
|
||||
exit;
|
||||
} catch (Exception $e) { $db->rollBack(); $message = "Error: " . $e->getMessage(); }
|
||||
}
|
||||
|
||||
if (isset($_POST['record_payment'])) {
|
||||
$id = (int)$_POST['invoice_id'];
|
||||
$amount = (float)$_POST['amount'];
|
||||
@ -1188,6 +1258,8 @@ function getPromotionalPrice($item) {
|
||||
|
||||
$db->commit();
|
||||
$message = ($type === 'purchase' ? "Purchase" : "Invoice") . " updated successfully!";
|
||||
header("Location: index.php?page=" . ($type === 'purchase' ? 'purchases' : 'sales'));
|
||||
exit;
|
||||
} catch (Exception $e) { $db->rollBack(); $message = "Error: " . $e->getMessage(); }
|
||||
}
|
||||
|
||||
@ -5433,7 +5505,10 @@ $projectDescription = $_SERVER['PROJECT_DESCRIPTION'] ?? 'Accounting System';
|
||||
<td class="text-end">
|
||||
<div class="btn-group btn-group-sm">
|
||||
<button class="btn btn-outline-info view-lpo-btn" data-json="<?= htmlspecialchars(json_encode($q)) ?>" title="View"><i class="bi bi-eye"></i></button>
|
||||
<?php if ($q['status'] !== 'converted'): ?>
|
||||
<button class="btn btn-outline-success" onclick="if(confirm('Convert this LPO to Purchase Invoice?')) { const f = document.createElement('form'); f.method='POST'; f.innerHTML='<input type=hidden name=convert_lpo_to_purchase><input type=hidden name=lpo_id value=<?= $q['id'] ?>>'; document.body.appendChild(f); f.submit(); }" title="Convert to Purchase"><i class="bi bi-arrow-repeat"></i></button>
|
||||
<button class="btn btn-outline-primary edit-lpo-btn" data-json="<?= htmlspecialchars(json_encode($q)) ?>" data-bs-toggle="modal" data-bs-target="#editLpoModal" title="Edit"><i class="bi bi-pencil-square"></i></button>
|
||||
<?php endif; ?>
|
||||
<button class="btn btn-outline-danger" onclick="if(confirm('Delete this LPO?')) { const f = document.createElement('form'); f.method='POST'; f.innerHTML='<input type=hidden name=delete_lpo><input type=hidden name=id value=<?= $q['id'] ?>>'; document.body.appendChild(f); f.submit(); }" title="Delete"><i class="bi bi-trash"></i></button>
|
||||
</div>
|
||||
</td>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user