$(document).ready(function () { loader = $('#overlay, #loader'); tableName = '#PODataTable'; totalSelectedLabel = $('#totalSelected'); tableElement = $(tableName); poDataTable = $(tableName).DataTable({ initComplete: function () { var api = this.api(); var data = api.ajax.json(); if (!data || !data.data || data.data === "No Data") { $('.dataTables_empty').html("No record available"); toggleSubmitButton(); } calculateTotalPesoAmount(); }, columns: colCustomPO, columnDefs:colDefCustomPO, language: { emptyTable: "No items added yet" } }); }); async function fetchAndPopulatePOFormData(poType, poId = null) { loader = $('#overlay, #loader').css('z-index', 1060); return new Promise((resolve, reject) => { $.ajax({ url: '/POMgmt/GetPOFormData', type: 'GET', data: { poId: poId }, beforeSend: function () { loader.show(); }, complete: function () { loader.hide(); }, success: function (response) { const d = response; document.getElementById("poType").value = poType; $('#poNoFinal').val($('#poNo').val()); // --- Header Fields --- if (d.header) { document.getElementById("supplierName").value = d.header.supplierName || ''; $('#supplierId').val(d.header.supplierId || 0); console.log(d.header.supplierId); console.log(d.header.supplierName); $('#deliveryDate').val(d.header.deliveryDate || ''); $('#piDate').val(d.header.profInvoiceDate || ''); $('#piNo').val(d.header.profInvoiceNo || ''); $('#currencyCER').val(d.header.currencyCER || '59.00'); $('#podId').val(d.header.podId || 0); $('#shippingInstructionId').val(1 || 0); $('#incotermsName').val(d.header.incoTermsId || 0); $('#incoTermsId').val(d.header.incoTermsId || 0); $('#C-paymentTerms').val(d.header.paymentTerms || ''); $('#C-paymentTermsId').val(d.header.paymentTermsId || 0); $('#remarks').val(d.header.remarks || ''); $('#grossAmountUSD').val(d.header.grossAmountUSD || 0); $('#finalAmountUSD').val(d.header.finalAmountUSD || 0); $('#grossAmountPHP').val(d.header.grossAmountPHP || 0); $('#finalAmountPHP').val(d.header.finalAmountPHP || 0); } // --- DataTable: Line Items --- if (poDataTable) { poDataTable.clear(); if (d.lineItems && d.lineItems.length > 0) { poDataTable.rows.add(d.lineItems).draw(); } } // --- DataTable: Charges --- if (d.charges && d.charges.length > 0) { const $tbody = $('#DestChargesTable tbody'); $tbody.empty(); // clear existing rows first d.charges.forEach(function (charge) { var newRow = '' + '' + charge.otherChargesId + '' + '' + charge.otherChargesName + '' + '' + charge.amount + '' + '' + ''; $tbody.append(newRow); }); // Recalculate after populating charges calculateFinalPesoAmount($('#poTypeId').val()); calculateFinalUsdAmount(); lessDiscount(); } // --- Docs Required --- if (d.docsRequired && d.docsRequired.length > 0) { $('#docRequiredId').val( d.docsRequired.map(x => x.docName).join('\n') ); } resolve(); }, error: function (xhr, status, error) { console.error("Error loading PO form data:", error); reject(error); } }); }); } function enablePONoAutocomplete() { document.getElementById('customPOHeading').innerHTML = 'Custom P.O. Modification'; customFormPOElemComponent(4); document.getElementById("poNo").removeAttribute("readonly"); document.getElementById("poNo").value =''; document.getElementById("poNo").focus(); document.getElementById("poType").value = "0"; popPONo(); } function lessDiscount() { let amount = parseFloat($('#finalAmount').val()) || 0; let discount = parseFloat($('#discount').val()) || 0; if (!isNaN(discount) && discount > 0) { let finalAmount = amount - discount; $('#finalAmount').val(numberWithCommas(finalAmount)); } else { $('#finalAmount').val(numberWithCommas(amount)); } } function calculateTotalPesoAmount(poTypeId) { $(tableName + ' tbody').on('input', '.unitPrice,.qty', function () { const $row = $(this).closest('tr'); const qty = parseFloat($row.find('.qty').eq(0).val()) || 0; const price = parseFloat($row.find('.unitPrice').eq(0).val()) || 0; const grossAmount = (qty * price).toFixed(4); $row.find('td').eq(8).text(grossAmount); var updatedPoTypeId = $('#poTypeId').val(); if (updatedPoTypeId == 1) { $('#vatRate').on('input', calculateFinalPesoAmount); } calculateFinalPesoAmount(updatedPoTypeId); }); } function calculateFinalPesoAmount(poTypeId) { let grossAmount = 0; let chargesAmount = 0; let finalAmount = 0; $('#PODataTable tbody tr').each(function () { const $row = $(this); const totalAmountStr = $row.find('td').eq(8).text().replace(/,/g, ''); const totalAmount = parseFloat(totalAmountStr) || 0; grossAmount += totalAmount; }); $('#DestChargesTable tbody tr').each(function () { const $row = $(this); const totalAmountStr = $row.find('td').eq(4).text().replace(/,/g, ''); const totalAmount = parseFloat(totalAmountStr) || 0; chargesAmount += totalAmount; }); $('#grossAmount').val(numberWithCommas(grossAmount)); if (poTypeId == 1) { const vatRateInput = $('#vatRate').val(); const vatAmount = (vatRateInput / 100) * grossAmount; const grossWithVat = parseFloat(grossAmount) + parseFloat(vatAmount) + parseFloat(chargesAmount); finalAmount = grossWithVat.toFixed(4); } else { const grossWithVat = parseFloat(grossAmount) + parseFloat(chargesAmount); finalAmount = grossWithVat.toFixed(4); } $('#finalAmount').val(numberWithCommas(finalAmount)); } function calculateFinalUsdAmount() { const currencyCer = parseFloat($('#currencyCER').val()) || 0; let grossAmount = 0; let chargesAmount = 0; let finalAmount = 0; let finalConvertedAmount = 0; $('#PODataTable tbody tr').each(function () { const $row = $(this); const totalAmountStr = $row.find('td').eq(8).text().replace(/,/g, ''); const totalAmount = parseFloat(totalAmountStr) || 0; grossAmount += totalAmount; }); $('#DestChargesTable tbody tr').each(function () { const $row = $(this); const totalAmountStr = $row.find('td').eq(4).text().replace(/,/g, ''); const totalAmount = parseFloat(totalAmountStr) || 0; chargesAmount += totalAmount; }); const convertedPhpAmount = parseFloat(grossAmount) * currencyCer; const convertedChargesAmount = parseFloat(chargesAmount) * currencyCer; // Update the total PHP amount $('#grossAmountPHP').val(numberWithCommas(convertedPhpAmount)); finalConvertedAmount = parseFloat(convertedPhpAmount) + parseFloat(convertedChargesAmount); $('#finalAmountPHP').val(numberWithCommas(finalConvertedAmount)); // Update the total USD amount $('#grossAmountUSD').val(numberWithCommas(grossAmount)); finalAmount = parseFloat(grossAmount) + parseFloat(chargesAmount); $('#finalAmountUSD').val(numberWithCommas(finalAmount)); } function calculateTotalUsdAmount() { let totalAmountUSD = 0; $('#PODataTable tbody').on('input', '.unitPrice,.qty', function () { const $row = $(this).closest('tr'); const qty = parseFloat($row.find('.qty').eq(0).val()) || 0; const price = parseFloat($row.find('.unitPrice').eq(0).val()) || 0; const grossAmount = (qty * price).toFixed(4); $row.find('td').eq(8).text(grossAmount); totalAmountUSD += grossAmount; calculateFinalUsdAmount(); $('#currencyCER').on('input', calculateFinalUsdAmount); }); }