diff --git a/index.php b/index.php index b8f23eb..ad7964f 100644 --- a/index.php +++ b/index.php @@ -419,10 +419,12 @@ if (isset($_GET['action']) || isset($_POST['action'])) { if ($action === 'get_payment_details') { header('Content-Type: application/json'); $payment_id = (int)$_GET['payment_id']; - $stmt = db()->prepare("SELECT p.*, i.customer_id, c.name as customer_name + $stmt = db()->prepare("SELECT p.*, i.customer_id, i.id as inv_id, c.name as customer_name, + o.name as outlet_name, o.address as outlet_address, o.phone as outlet_phone FROM payments p JOIN invoices i ON p.invoice_id = i.id - JOIN customers c ON i.customer_id = c.id + LEFT JOIN customers c ON i.customer_id = c.id + LEFT JOIN outlets o ON i.outlet_id = o.id WHERE p.id = ?"); $stmt->execute([$payment_id]); echo json_encode($stmt->fetch(PDO::FETCH_ASSOC)); @@ -540,7 +542,22 @@ if (isset($_GET['action']) || isset($_POST['action'])) { recordSaleJournal($transaction_id, $net_amount, date('Y-m-d'), $items_for_journal, $tax_amount); $db->commit(); - echo json_encode(['success' => true, 'invoice_id' => $transaction_id, 'transaction_no' => $transaction_no]); + + $outlet = []; + if (!empty($_SESSION['outlet_id'])) { + $stmtO = $db->prepare("SELECT name, phone, address FROM outlets WHERE id = ?"); + $stmtO->execute([$_SESSION['outlet_id']]); + $outlet = $stmtO->fetch(PDO::FETCH_ASSOC); + } + + echo json_encode([ + 'success' => true, + 'invoice_id' => $transaction_id, + 'transaction_no' => $transaction_no, + 'outlet_name' => $outlet['name'] ?? null, + 'outlet_phone' => $outlet['phone'] ?? null, + 'outlet_address' => $outlet['address'] ?? null + ]); } catch (Exception $e) { $db->rollBack(); echo json_encode(['success' => false, 'error' => $e->getMessage()]); @@ -3614,9 +3631,10 @@ switch ($page) { $data['total_pages'] = ceil($total_records / $limit); $data['current_page'] = $page_num; - $stmt = db()->prepare("SELECT q.*, c.name as customer_name + $stmt = db()->prepare("SELECT q.*, c.name as customer_name, o.name as outlet_name, o.address as outlet_address, o.phone as outlet_phone FROM quotations q JOIN customers c ON q.customer_id = c.id + LEFT JOIN outlets o ON q.outlet_id = o.id WHERE $whereSql ORDER BY q.id DESC LIMIT $limit OFFSET $offset"); @@ -3665,9 +3683,10 @@ switch ($page) { $data['total_pages'] = ceil($total_records / $limit); $data['current_page'] = $page_num; - $stmt = db()->prepare("SELECT q.*, s.name as supplier_name + $stmt = db()->prepare("SELECT q.*, s.name as supplier_name, o.name as outlet_name, o.address as outlet_address, o.phone as outlet_phone FROM lpos q JOIN suppliers s ON q.supplier_id = s.id + LEFT JOIN outlets o ON q.outlet_id = o.id WHERE $whereSql ORDER BY q.id DESC LIMIT $limit OFFSET $offset"); @@ -3736,9 +3755,10 @@ switch ($page) { $data['total_pages'] = ceil($total_records / $limit); $data['current_page'] = $page_num; - $stmt = db()->prepare("SELECT v.*, c.name as customer_name, c.tax_id as customer_tax_id, c.phone as customer_phone + $stmt = db()->prepare("SELECT v.*, c.name as customer_name, c.tax_id as customer_tax_id, c.phone as customer_phone, o.name as outlet_name, o.phone as outlet_phone, o.address as outlet_address 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 WHERE $whereSql ORDER BY v.id DESC LIMIT $limit OFFSET $offset"); $stmt->execute($params); @@ -6909,7 +6929,7 @@ $projectDescription = $_SERVER['PROJECT_DESCRIPTION'] ?? 'Accounting System'; if (result.success) { const payModal = bootstrap.Modal.getInstance(document.getElementById('posPaymentModal')); if (payModal) payModal.hide(); - this.showReceipt(result.invoice_id, discountAmount, loyaltyRedeemed, result.transaction_no); + this.showReceipt(result.invoice_id, discountAmount, loyaltyRedeemed, result.transaction_no, result.outlet_name, result.outlet_phone, result.outlet_address); } else { Swal.fire('Error', result.error, 'error'); btn.disabled = false; @@ -6922,7 +6942,7 @@ $projectDescription = $_SERVER['PROJECT_DESCRIPTION'] ?? 'Accounting System'; btn.innerText = originalText; } }, - showReceipt(invId, discountAmount, loyaltyRedeemed, transactionNo) { + showReceipt(invId, discountAmount, loyaltyRedeemed, transactionNo, outletName, outletPhone, outletAddress) { const container = document.getElementById('posReceiptContent'); const customerSelect = document.getElementById('posCustomer'); const customerName = (customerSelect && customerSelect.selectedIndex >= 0 && customerSelect.value !== '') ? customerSelect.options[customerSelect.selectedIndex].text : ' / '; @@ -6963,8 +6983,9 @@ $projectDescription = $_SERVER['PROJECT_DESCRIPTION'] ?? 'Accounting System'; return sum + ((item.price * item.qty) * (vatRate / (100 + vatRate))); }, 0); const total = subtotal - discountAmount - loyaltyRedeemed; - const companyName = ""; - const companyPhone = ""; + const companyName = outletName || ""; + const companyPhone = outletPhone || ""; + const companyAddress = outletAddress ? outletAddress.replace(/\n/g, '
') : ""; const companyVat = ""; const companyLogo = ""; @@ -6973,6 +6994,7 @@ $projectDescription = $_SERVER['PROJECT_DESCRIPTION'] ?? 'Accounting System';
${companyLogo ? `Logo` : ''}
${companyName}
+ ${companyAddress ? `
${companyAddress}
` : ''} ${companyPhone ? `
هاتف / Tel: ${companyPhone}
` : ''} ${companyVat ? `
الرقم الضريبي / VAT No: ${companyVat}
` : ''}
@@ -11869,6 +11891,14 @@ document.addEventListener('DOMContentLoaded', function() { document.getElementById('receiptAmount').textContent = parseFloat(data.amount).toFixed(3); document.getElementById('receiptAmountWords').textContent = data.amount_words; + const rcn = document.getElementById('receiptCompanyName'); + if(rcn) rcn.textContent = data.outlet_name || ""; + const rca = document.getElementById('receiptCompanyAddress'); + if(rca) { + rca.textContent = data.outlet_address || "".replace(//gi, '\n'); + rca.style.whiteSpace = 'pre-line'; + } + // Update labels for Purchase vs Sale const partyLabel = document.getElementById('receiptPartyLabel'); const againstLabel = document.getElementById('receiptAgainstLabel'); @@ -12186,10 +12216,10 @@ document.addEventListener('DOMContentLoaded', function() {
${logoUrl ? `Logo` : ''} -

${companySettings.company_name || 'Your Company'}

+

${data.outlet_name || companySettings.company_name || 'Your Company'}

- ${companySettings.company_address || ''}
- Phone: ${companySettings.company_phone || ''} | Email: ${companySettings.company_email || ''} + ${data.outlet_address ? data.outlet_address.replace(/\n/g, '
') : (companySettings.company_address || '').replace(/\n/g, '
')}
+ Phone: ${data.outlet_phone || companySettings.company_phone || ''} | Email: ${companySettings.company_email || ''} ${companySettings.tax_number ? `
TRN: ${companySettings.tax_number}` : ''}

@@ -12377,10 +12407,10 @@ document.addEventListener('DOMContentLoaded', function() { // Company Logo and Header Construction const logoUrl = companySettings.company_logo || ''; const logoImg = logoUrl ? `` : ''; - const companyName = companySettings.company_name || 'Accounting System'; - const companyAddress = (companySettings.company_address || '').replace(/\n/g, '
'); + const companyName = data.outlet_name || companySettings.company_name || 'Accounting System'; + const companyAddress = data.outlet_address ? data.outlet_address.replace(/\n/g, '
') : (companySettings.company_address || '').replace(/\n/g, '
'); const companyVat = companySettings.vat_number ? `

VAT: ${companySettings.vat_number}

` : ''; - const companyPhone = companySettings.company_phone ? `

Tel: ${companySettings.company_phone}

` : ''; + const companyPhone = data.outlet_phone ? `

Tel: ${data.outlet_phone}

` : (companySettings.company_phone ? `

Tel: ${companySettings.company_phone}

` : ''); // Quotation Header Construction const quotDate = data.quotation_date; @@ -13839,12 +13869,10 @@ document.addEventListener('DOMContentLoaded', function() { ?> -

-

+

+

VAT:

- -

Tel:

- +

Tel:

Tax Invoice / فاتورة ضريبية

@@ -14037,8 +14065,8 @@ document.addEventListener('DOMContentLoaded', function() { if ($logo): ?> -

-

+

+


Payment Receipt / سند قبض

@@ -14665,6 +14693,17 @@ document.addEventListener('DOMContentLoaded', function() { window.viewAndPrintA4Invoice = function(data, autoPrint = true) { if (!data) return; // Reuse view logic + document.getElementById('printCompanyName').textContent = data.outlet_name || ""; + document.getElementById('printCompanyAddress').textContent = data.outlet_address || "".replace(//gi, '\n'); + document.getElementById('printCompanyAddress').style.whiteSpace = 'pre-line'; + const cPhone = data.outlet_phone || ""; + if (cPhone) { + document.getElementById('printCompanyPhone').textContent = cPhone; + document.getElementById('printCompanyPhoneContainer').style.display = 'block'; + } else { + document.getElementById('printCompanyPhoneContainer').style.display = 'none'; + } + document.getElementById('invNumber').textContent = 'INV-' + data.id.toString().padStart(5, '0'); document.getElementById('invDate').textContent = data.invoice_date; document.getElementById('invPaymentType').textContent = data.payment_type ? data.payment_type.toUpperCase() : 'CASH'; @@ -14737,7 +14776,7 @@ document.addEventListener('DOMContentLoaded', function() { if (document.getElementById('invBalanceInfo')) document.getElementById('invBalanceInfo').innerHTML = ' ' + balance.toFixed(3); // Generate QR Code for Zakat, Tax and Customs Authority (ZATCA) style or simple formal - const companyName = ; + const companyName = data.outlet_name || ; const vatNo = ; const qrData = `Seller: ${companyName}\nVAT: ${vatNo}\nInvoice: INV-${data.id.toString().padStart(5, '0')}\nDate: ${data.invoice_date}\nTotal: ${grandTotalValue.toFixed(3)}`; const qrUrl = `https://api.qrserver.com/v1/create-qr-code/?size=100x100&data=${encodeURIComponent(qrData)}`; @@ -14795,8 +14834,9 @@ document.addEventListener('DOMContentLoaded', function() { }, 0); const subtotal = inv.items.reduce((sum, item) => sum + (item.unit_price * item.quantity), 0); - const companyName = ""; - const companyPhone = ""; + const companyName = inv.outlet_name || ""; + const companyPhone = inv.outlet_phone || ""; + const companyAddress = inv.outlet_address ? inv.outlet_address.replace(/\n/g, '
') : ""; const companyVat = ""; const companyLogo = ""; @@ -14805,6 +14845,7 @@ document.addEventListener('DOMContentLoaded', function() {
${companyLogo ? `Logo` : ''}
${companyName}
+ ${companyAddress ? `
${companyAddress}
` : ''} ${companyPhone ? `
Tel: ${companyPhone}
` : ''} ${companyVat ? `
VAT: ${companyVat}
` : ''}