updating sales list
This commit is contained in:
parent
df0eee1043
commit
76b078746f
94
index.php
94
index.php
@ -1564,6 +1564,9 @@ function getPromotionalPrice($item) {
|
||||
$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();
|
||||
if (db_column_exists($table, 'outlet_id')) {
|
||||
$db->prepare("UPDATE $table SET outlet_id = ? WHERE id = ?")->execute([current_outlet_id(), $inv_id]);
|
||||
}
|
||||
|
||||
$items_for_journal = [];
|
||||
foreach ($items as $i => $item_id) {
|
||||
@ -1596,6 +1599,9 @@ function getPromotionalPrice($item) {
|
||||
}
|
||||
|
||||
$db->commit();
|
||||
$_SESSION['trigger_invoice_modal'] = true;
|
||||
$_SESSION['show_invoice_id'] = (int)$inv_id;
|
||||
$_SESSION['show_invoice_page'] = ($type === 'purchase') ? 'purchases' : 'sales';
|
||||
$msg = ($type === 'purchase' ? "Purchase" : "Invoice") . " #$inv_id created!";
|
||||
redirectWithMessage($msg, "index.php?page=" . ($type === 'purchase' ? 'purchases' : 'sales'));
|
||||
} catch (Exception $e) { $db->rollBack(); $message = "Error: " . $e->getMessage(); }
|
||||
@ -2579,6 +2585,9 @@ function getPromotionalPrice($item) {
|
||||
|
||||
$db->prepare("UPDATE $table SET $cust_supplier_col = ?, invoice_date = ?, due_date = ?, status = ?, payment_type = ?, total_amount = ?, vat_amount = ?, total_with_vat = ?, paid_amount = ? WHERE id = ?")
|
||||
->execute([$cust_id, $date, $due_date, $status, $pay_type, $total_subtotal, $total_vat, $total_with_vat, $paid, $id]);
|
||||
if (db_column_exists($table, 'outlet_id')) {
|
||||
$db->prepare("UPDATE $table SET outlet_id = COALESCE(outlet_id, ?) WHERE id = ?")->execute([current_outlet_id(), $id]);
|
||||
}
|
||||
|
||||
// Revert stock for old items
|
||||
$stmtOld = $db->prepare("SELECT item_id, quantity FROM $item_table WHERE $fk_col = ?");
|
||||
@ -4106,9 +4115,10 @@ switch ($page) {
|
||||
$params[] = $_GET['end_date'];
|
||||
}
|
||||
|
||||
$tableHasOutlet = db_column_exists($table, 'outlet_id');
|
||||
$oid = current_outlet_id();
|
||||
if ($oid !== -1) {
|
||||
$where[] = "v.outlet_id = ?";
|
||||
if ($tableHasOutlet && $oid !== -1) {
|
||||
$where[] = "(v.outlet_id = ? OR v.outlet_id IS NULL)";
|
||||
$params[] = $oid;
|
||||
}
|
||||
|
||||
@ -4122,10 +4132,16 @@ switch ($page) {
|
||||
|
||||
$customerTaxColumn = entity_tax_column($cust_supplier_table);
|
||||
$customerTaxSelect = $customerTaxColumn !== null ? "c.$customerTaxColumn" : "''";
|
||||
$stmt = db()->prepare("SELECT v.*, c.name as customer_name, $customerTaxSelect as customer_tax_id, c.phone as customer_phone, o.name as outlet_name
|
||||
$outletSelectSql = "'' AS outlet_name";
|
||||
$outletJoinSql = '';
|
||||
if ($tableHasOutlet && db_table_exists('outlets')) {
|
||||
$outletSelectSql = "o.name AS outlet_name";
|
||||
$outletJoinSql = "LEFT JOIN outlets o ON v.outlet_id = o.id";
|
||||
}
|
||||
$stmt = db()->prepare("SELECT v.*, c.name as customer_name, $customerTaxSelect as customer_tax_id, c.phone as customer_phone, $outletSelectSql
|
||||
FROM $table v
|
||||
LEFT JOIN $cust_supplier_table c ON v.$cust_supplier_col = c.id
|
||||
LEFT JOIN outlets o ON v.outlet_id = o.id
|
||||
$outletJoinSql
|
||||
WHERE $whereSql
|
||||
ORDER BY v.id DESC LIMIT $limit OFFSET $offset");
|
||||
$stmt->execute($params);
|
||||
@ -4148,6 +4164,7 @@ switch ($page) {
|
||||
|
||||
$inv['party_name'] = trim((string)($inv['customer_name'] ?? '')) !== '' ? (string)$inv['customer_name'] : $partyFallback;
|
||||
$inv['document_no'] = ($type === 'sale' && $transactionNo !== '') ? $transactionNo : $documentPrefix . '-' . str_pad((string)$inv['id'], 5, '0', STR_PAD_LEFT);
|
||||
$inv['type'] = $type;
|
||||
$inv['payment_type'] = $normalizedPaymentType;
|
||||
$inv['payment_type_label'] = $paymentTypeLabel;
|
||||
$inv['total_with_vat'] = (float)($inv['total_with_vat'] ?? (($inv['total_amount'] ?? 0) + ($inv['vat_amount'] ?? 0)));
|
||||
@ -14589,7 +14606,8 @@ document.addEventListener('DOMContentLoaded', function() {
|
||||
window.viewAndPrintA4Invoice = function(data, autoPrint = true) {
|
||||
if (!data) return;
|
||||
// Reuse view logic
|
||||
document.getElementById('invNumber').textContent = 'INV-' + data.id.toString().padStart(5, '0');
|
||||
const invoiceDisplayNo = data.document_no || data.transaction_no || ((data.type === 'purchase' ? 'PUR-' : 'INV-') + data.id.toString().padStart(5, '0'));
|
||||
document.getElementById('invNumber').textContent = invoiceDisplayNo;
|
||||
document.getElementById('invDate').textContent = data.invoice_date;
|
||||
document.getElementById('invPaymentType').textContent = data.payment_type ? data.payment_type.toUpperCase() : 'CASH';
|
||||
document.getElementById('invCustomerName').textContent = data.customer_name || '---';
|
||||
@ -14669,7 +14687,7 @@ document.addEventListener('DOMContentLoaded', function() {
|
||||
// Generate QR Code for Zakat, Tax and Customs Authority (ZATCA) style or simple formal
|
||||
const companyName = <?= json_encode($data['settings']['company_name'] ?? 'Accounting System') ?>;
|
||||
const vatNo = <?= json_encode($data['settings']['vat_number'] ?? '') ?>;
|
||||
const qrData = `Seller: ${companyName}\nVAT: ${vatNo}\nInvoice: INV-${data.id.toString().padStart(5, '0')}\nDate: ${data.invoice_date}\nTotal: ${grandTotalValue.toFixed(3)}`;
|
||||
const qrData = `Seller: ${companyName}\nVAT: ${vatNo}\nInvoice: ${invoiceDisplayNo}\nDate: ${data.invoice_date}\nTotal: ${grandTotalValue.toFixed(3)}`;
|
||||
const qrUrl = `https://api.qrserver.com/v1/create-qr-code/?size=100x100&data=${encodeURIComponent(qrData)}`;
|
||||
if (document.getElementById('invQrCode')) {
|
||||
document.getElementById('invQrCode').innerHTML = `<img src="${qrUrl}" alt="QR Code" style="width: 100px; height: 100px;" class="border p-1 bg-white">`;
|
||||
@ -14703,6 +14721,70 @@ document.addEventListener('DOMContentLoaded', function() {
|
||||
}).catch(err => console.error('Error fetching payments:', err));
|
||||
};
|
||||
|
||||
<?php
|
||||
$autoInvoicePayload = null;
|
||||
if (
|
||||
isset($_SESSION['trigger_invoice_modal'], $_SESSION['show_invoice_id'], $_SESSION['show_invoice_page']) &&
|
||||
in_array($page, ['sales', 'purchases'], true) &&
|
||||
$_SESSION['show_invoice_page'] === $page
|
||||
) {
|
||||
$autoInvoiceId = (int)$_SESSION['show_invoice_id'];
|
||||
$autoInvoiceType = $page === 'purchases' ? 'purchase' : 'sale';
|
||||
unset($_SESSION['trigger_invoice_modal'], $_SESSION['show_invoice_id'], $_SESSION['show_invoice_page']);
|
||||
|
||||
try {
|
||||
$autoTable = $autoInvoiceType === 'purchase' ? 'purchases' : 'invoices';
|
||||
$autoPartyTable = $autoInvoiceType === 'purchase' ? 'suppliers' : 'customers';
|
||||
$autoPartyCol = $autoInvoiceType === 'purchase' ? 'supplier_id' : 'customer_id';
|
||||
$autoTaxColumn = entity_tax_column($autoPartyTable);
|
||||
$autoTaxSelect = $autoTaxColumn !== null ? "c.$autoTaxColumn AS customer_tax_id" : "'' AS customer_tax_id";
|
||||
$autoOutletSelect = "'' AS outlet_name";
|
||||
$autoOutletJoin = '';
|
||||
|
||||
if (db_column_exists($autoTable, 'outlet_id') && db_table_exists('outlets')) {
|
||||
$autoOutletSelect = 'o.name AS outlet_name';
|
||||
$autoOutletJoin = 'LEFT JOIN outlets o ON doc.outlet_id = o.id';
|
||||
}
|
||||
|
||||
$autoStmt = db()->prepare("SELECT doc.*, c.name AS customer_name, c.phone AS customer_phone, $autoTaxSelect, $autoOutletSelect
|
||||
FROM $autoTable doc
|
||||
LEFT JOIN $autoPartyTable c ON doc.$autoPartyCol = c.id
|
||||
$autoOutletJoin
|
||||
WHERE doc.id = ?
|
||||
LIMIT 1");
|
||||
$autoStmt->execute([$autoInvoiceId]);
|
||||
$autoInvoicePayload = $autoStmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if ($autoInvoicePayload) {
|
||||
$autoItemSql = $autoInvoiceType === 'purchase'
|
||||
? "SELECT pi.*, i.name_en, i.name_ar, i.vat_rate FROM purchase_items pi LEFT JOIN stock_items i ON pi.item_id = i.id WHERE pi.purchase_id = ?"
|
||||
: "SELECT ii.*, i.name_en, i.name_ar, i.vat_rate FROM invoice_items ii LEFT JOIN stock_items i ON ii.item_id = i.id WHERE ii.invoice_id = ?";
|
||||
$autoItemsStmt = db()->prepare($autoItemSql);
|
||||
$autoItemsStmt->execute([$autoInvoiceId]);
|
||||
$autoInvoicePayload['items'] = $autoItemsStmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
$autoInvoicePayload['type'] = $autoInvoiceType;
|
||||
$autoInvoicePayload['total_with_vat'] = (float)($autoInvoicePayload['total_with_vat'] ?? (($autoInvoicePayload['total_amount'] ?? 0) + ($autoInvoicePayload['vat_amount'] ?? 0)));
|
||||
$autoInvoicePayload['paid_amount'] = (float)($autoInvoicePayload['paid_amount'] ?? 0);
|
||||
$autoInvoicePayload['total_in_words'] = numberToWordsOMR($autoInvoicePayload['total_with_vat']);
|
||||
$autoTransactionNo = trim((string)($autoInvoicePayload['transaction_no'] ?? ''));
|
||||
$autoPrefix = $autoInvoiceType === 'purchase' ? 'PUR' : 'INV';
|
||||
$autoInvoicePayload['document_no'] = ($autoInvoiceType === 'sale' && $autoTransactionNo !== '')
|
||||
? $autoTransactionNo
|
||||
: $autoPrefix . '-' . str_pad((string)$autoInvoicePayload['id'], 5, '0', STR_PAD_LEFT);
|
||||
}
|
||||
} catch (Throwable $e) {
|
||||
$autoInvoicePayload = null;
|
||||
}
|
||||
}
|
||||
?>
|
||||
<?php if (!empty($autoInvoicePayload)): ?>
|
||||
setTimeout(() => {
|
||||
if (window.viewAndPrintA4Invoice) {
|
||||
window.viewAndPrintA4Invoice(<?= json_encode($autoInvoicePayload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) ?>, true);
|
||||
}
|
||||
}, 300);
|
||||
<?php endif; ?>
|
||||
|
||||
window.printPosReceiptFromInvoice = function(inv) {
|
||||
const container = document.getElementById('posReceiptContent');
|
||||
const itemsHtml = inv.items.map(item => {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user