// LPO Form Logic
initInvoiceForm('lpoProductSearchInput', 'lpoSearchSuggestions', 'lpoItemsTableBody', 'lpo_grand_display', 'lpo_subtotal_display', 'lpo_vat_display');
initInvoiceForm('editLpoProductSearchInput', 'editLpoSearchSuggestions', 'editLpoItemsTableBody', 'edit_lpo_grand_display', 'edit_lpo_subtotal_display', 'edit_lpo_vat_display');
document.querySelectorAll('.edit-lpo-btn').forEach(btn => {
btn.addEventListener('click', function() {
const data = JSON.parse(this.dataset.json);
document.getElementById('edit_lpo_id').value = data.id;
const supplierSelect = document.getElementById('edit_lpo_supplier_id');
supplierSelect.value = data.supplier_id;
if (window.jQuery && $(supplierSelect).data('select2')) {
$(supplierSelect).trigger('change');
}
document.getElementById('edit_lpo_date').value = data.lpo_date;
document.getElementById('edit_lpo_delivery_date').value = data.delivery_date || '';
document.getElementById('edit_lpo_status').value = data.status || 'pending';
document.getElementById('edit_lpo_terms').value = data.terms_conditions || '';
const tableBody = document.getElementById('editLpoItemsTableBody');
tableBody.innerHTML = '';
data.items.forEach(item => {
addItemToTable({
id: item.item_id,
name_en: item.name_en,
name_ar: item.name_ar,
sku: '',
vat_rate: item.vat_rate || 0
}, tableBody, null, null,
document.getElementById('edit_lpo_grand_display'),
document.getElementById('edit_lpo_subtotal_display'),
document.getElementById('edit_lpo_vat_display'),
{ quantity: item.quantity, unit_price: item.unit_price });
});
});
});
document.querySelectorAll('.view-lpo-btn').forEach(btn => {
btn.addEventListener('click', function() {
const data = JSON.parse(this.dataset.json);
window.viewAndPrintLPO(data);
});
});
window.viewAndPrintLPO = function(data) {
const modal = new bootstrap.Modal(document.getElementById('viewLpoModal'));
const content = document.getElementById('lpoDetailsContent');
const logoUrl = companySettings.company_logo || '';
const companyHeader = `
${logoUrl ? `

` : ''}
${companySettings.company_name || 'Your Company'}
${companySettings.company_address || ''}
Phone: ${companySettings.company_phone || ''} | Email: ${companySettings.company_email || ''}
${companySettings.tax_number ? `
TRN: ${companySettings.tax_number}` : ''}
LOCAL PURCHASE ORDER
LPO-${data.id.toString().padStart(5, '0')}
`;
let itemsHtml = '';
data.items.forEach((item, index) => {
itemsHtml += `
| ${index + 1} |
${item.name_en} ${item.name_ar} |
${item.quantity} |
${parseFloat(item.unit_price).toFixed(3)} |
${parseFloat(item.vat_rate || 0).toFixed(2)}% |
${parseFloat(item.total_amount).toFixed(3)} |
`;
});
content.innerHTML = `
${companyHeader}
Supplier
${data.supplier_name}
${data.supplier_phone ? `Phone: ${data.supplier_phone}` : ''}
Details
Date:
${data.lpo_date}
Delivery:
${data.delivery_date || '---'}
Status:
${data.status.toUpperCase()}
| # |
Description |
Qty |
Unit Price |
VAT |
Total |
${itemsHtml}
| Subtotal |
OMR ${parseFloat(data.total_amount).toFixed(3)} |
| VAT Amount |
OMR ${parseFloat(data.vat_amount).toFixed(2)} |
| Grand Total |
OMR ${parseFloat(data.total_with_vat).toFixed(3)} |
${data.terms_conditions ? `
Terms & Conditions
${data.terms_conditions.replace(/\n/g, '
')}
` : ''}
`;
window.printLPO = function() {
const printWindow = window.open('', '_blank');
printWindow.document.write('LPO-' + data.id + '');
printWindow.document.write('');
printWindow.document.write('');
printWindow.document.write('');
printWindow.document.write(content.innerHTML);
printWindow.document.write('');
printWindow.document.close();
setTimeout(() => {
printWindow.print();
printWindow.close();
}, 500);
};
modal.show();
};
// Quotation Form Logic
initInvoiceForm('quotProductSearchInput', 'quotSearchSuggestions', 'quotItemsTableBody', 'quot_grand_display', 'quot_subtotal_display', 'quot_vat_display');
initInvoiceForm('editQuotProductSearchInput', 'editQuotSearchSuggestions', 'editQuotItemsTableBody', 'edit_quot_grand_display', 'edit_quot_subtotal_display', 'edit_quot_vat_display');
document.querySelectorAll('.edit-quotation-btn').forEach(btn => {
btn.addEventListener('click', function() {
const data = JSON.parse(this.dataset.json);
document.getElementById('edit_quotation_id').value = data.id;
document.getElementById('edit_quot_customer_id').value = data.customer_id;
document.getElementById('edit_quot_date').value = data.quotation_date;
document.getElementById('edit_quot_valid').value = data.valid_until || '';
document.getElementById('edit_quot_status').value = data.status || 'pending';
const tableBody = document.getElementById('editQuotItemsTableBody');
tableBody.innerHTML = '';
data.items.forEach(item => {
addItemToTable({
id: item.item_id,
name_en: item.name_en,
name_ar: item.name_ar,
sku: '',
vat_rate: item.vat_rate || 0
}, tableBody, null, null,
document.getElementById('edit_quot_grand_display'),
document.getElementById('edit_quot_subtotal_display'),
document.getElementById('edit_quot_vat_display'),
{ quantity: item.quantity, unit_price: item.unit_price });
});
});
});
document.querySelectorAll('.convert-quotation-btn').forEach(btn => {
btn.addEventListener('click', function() {
if (confirm('Convert this quotation to an invoice? This will reduce stock.')) {
const f = document.createElement('form');
f.method = 'POST';
f.innerHTML = ``;
document.body.appendChild(f);
f.submit();
}
});
});
// View Quotation Logic
window.viewAndPrintQuotation = function(data, autoPrint = false) {
const modal = new bootstrap.Modal(document.getElementById('viewQuotationModal'));
const content = document.getElementById('quotationPrintableArea');
let itemsHtml = '';
data.items.forEach((item, index) => {
itemsHtml += `
| ${index + 1} |
${item.name_en} ${item.name_ar} |
${item.quantity} |
${parseFloat(item.unit_price).toFixed(3)} |
${parseFloat(item.vat_rate || 0).toFixed(2)}% |
${parseFloat(item.total_price).toFixed(3)} |
`;
});
// 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 companyVat = companySettings.vat_number ? `VAT: ${companySettings.vat_number}
` : '';
const companyPhone = companySettings.company_phone ? `Tel: ${companySettings.company_phone}
` : '';
// Quotation Header Construction
const quotDate = data.quotation_date;
const quotValid = data.valid_until || 'N/A';
const quotNo = 'QUO-' + data.id.toString().padStart(5, '0');
const customerName = data.customer_name || 'Walk-in Customer';
const statusBadge = `${data.status.toUpperCase()}`;
content.innerHTML = `
Terms & Conditions / الشروط والأحكام:
- Quotation is valid until the date mentioned above. / عرض السعر صالح لغاية التاريخ المذكور أعلاه.
- Prices are inclusive of VAT where applicable. / الأسعار تشمل ضريبة القيمة المضافة حيثما ينطبق ذلك.
Authorized Signature / التوقيع المعتمد
Generated by / تم إنشاؤه بواسطة ${companyName}
`;
const actionButtons = document.getElementById('quotationActionButtons');
actionButtons.innerHTML = '';
if (data.status === 'pending') {
const convertBtn = document.createElement('button');
convertBtn.className = 'btn btn-success me-2';
convertBtn.innerHTML = ' Convert to Invoice';
convertBtn.onclick = function() {
if (confirm('Convert this quotation to an invoice?')) {
const f = document.createElement('form');
f.method = 'POST';
f.innerHTML = ``;
document.body.appendChild(f);
f.submit();
}
};
actionButtons.appendChild(convertBtn);
const editBtn = document.createElement('button');
editBtn.className = 'btn btn-primary';
editBtn.innerHTML = ' Edit';
editBtn.onclick = function() {
const editModal = new bootstrap.Modal(document.getElementById('editQuotationModal'));
modal.hide();
const originalEditBtn = document.querySelector(`.edit-quotation-btn[data-json*='"id":${data.id},']`) ||
document.querySelector(`.edit-quotation-btn[data-json*='"id":${data.id}']`);
if (originalEditBtn) originalEditBtn.click();
};
actionButtons.appendChild(editBtn);
}
modal.show();
if (autoPrint) {
setTimeout(() => { window.print(); }, 500);
}
};
document.querySelectorAll('.view-quotation-btn').forEach(btn => {
btn.addEventListener('click', function() {
const data = JSON.parse(this.dataset.json);
window.viewAndPrintQuotation(data, false);
});
});