$(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" } }); }); 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); }); }