288 lines
11 KiB
JavaScript
288 lines
11 KiB
JavaScript
$(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);
|
|
if (d.header?.deliveryDate) {
|
|
let deliveryDate = d.header.deliveryDate.split("T")[0];
|
|
$('#deliveryDate').val(deliveryDate);
|
|
}
|
|
|
|
if (d.header?.profInvoiceDate) {
|
|
let profInvoiceDate = d.header.profInvoiceDate.split("T")[0];
|
|
$('#piDate').val(profInvoiceDate);
|
|
}
|
|
|
|
$('#deliverTo').val(d.header.deliverTo || '');
|
|
|
|
$('#piNo').val(d.header.profInvoiceNo || '');
|
|
$('#countryOrigin').val(d.header.countryOrigin || '');
|
|
$('#currencyCER').val(d.header.currencyCER || '59.00');
|
|
$('#podId').val(d.header.podId || 0);
|
|
|
|
$('#currency').val(d.header.currencyName || '');
|
|
$('#currencyId').val(d.header.currencyId || 1);
|
|
|
|
$('#shippingInstructionId').val(d.header.shippingInstructionId);
|
|
$('#discount').val(d.header.discount || 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);
|
|
|
|
$('#finalAmount').val(d.header.finalAmountPHP || 0);
|
|
$('#grossAmount').val(d.header.grossAmountPHP || 0);
|
|
|
|
$('#incoTermsId').val(d.header.incoTermsId);
|
|
$('#incotermsName').val(d.header.incotermsName || '');
|
|
}
|
|
|
|
// --- 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();
|
|
|
|
d.charges.forEach(function (charge) {
|
|
var newRow = '<tr>' +
|
|
'<td style="display: none;">' + charge.otherChargesId + '</td>' +
|
|
'<td>' + charge.otherChargesName + '</td>' +
|
|
'<td>' + charge.amount + '</td>' +
|
|
'<td>' +
|
|
'<button class="btn btn-default btn-trash-sm" ' +
|
|
'onclick="removeRow(this)" ' +
|
|
'data-otherChargesId="' + charge.otherChargesId + '">' +
|
|
'<i class="fa-regular fa-trash-can" style="color: #ff0000;" aria-hidden="true"></i>' +
|
|
'</button>' +
|
|
'</td>' +
|
|
'</tr>';
|
|
$tbody.append(newRow);
|
|
});
|
|
}
|
|
|
|
// --- 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() {
|
|
|
|
clearCustomTable();
|
|
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 clearCustomTable() {
|
|
poDataTable.clear().draw();
|
|
}
|
|
// =====================================================================
|
|
// UTILITIES
|
|
// =====================================================================
|
|
function getGrossFromTable() {
|
|
let gross = 0;
|
|
|
|
const table = $('#PODataTable').DataTable();
|
|
|
|
table.rows().every(function () {
|
|
const rowNode = this.node();
|
|
const cellText = $(rowNode).find('td').eq(8).text().replace(/,/g, '');
|
|
const val = parseFloat(cellText) || 0;
|
|
gross += val;
|
|
});
|
|
|
|
return gross;
|
|
}
|
|
function getChargesFromTable() {
|
|
let charges = 0;
|
|
$('#DestChargesTable tbody tr').each(function () {
|
|
const val = parseFloat($(this).find('td').eq(2).text().replace(/,/g, '')) || 0;
|
|
charges += val;
|
|
});
|
|
|
|
return charges;
|
|
}
|
|
function getDiscount() {
|
|
return parseFloat($('#discount').val()) || 0;
|
|
}
|
|
function getPoTypeId() {
|
|
return parseInt($('#poTypeId').val()) || 0;
|
|
}
|
|
function recalculateAll() {
|
|
const poTypeId = getPoTypeId();
|
|
const grossAmount = getGrossFromTable();
|
|
const chargesAmount = getChargesFromTable();
|
|
|
|
const discount = getDiscount();
|
|
const currencyCER = parseFloat($('#currencyCER').val()) || 1;
|
|
|
|
const grossAfterDiscount = grossAmount - parseFloat(discount.toFixed(4));
|
|
|
|
if (poTypeId === 3) {
|
|
|
|
const finalAmountUSD = grossAfterDiscount + chargesAmount;
|
|
const grossAmountPHP = grossAfterDiscount * currencyCER;
|
|
const finalAmountPHP = finalAmountUSD * currencyCER;
|
|
|
|
|
|
$('#grossAmountUSD').val(numberWithCommas(grossAfterDiscount.toFixed(4)));
|
|
$('#finalAmountUSD').val(numberWithCommas(finalAmountUSD.toFixed(4)));
|
|
$('#grossAmountPHP').val(numberWithCommas(grossAmountPHP.toFixed(4)));
|
|
$('#finalAmountPHP').val(numberWithCommas(finalAmountPHP.toFixed(4)));
|
|
|
|
// Hide PHP-only fields, show USD fields
|
|
$('#grossAmount, #finalAmount').val('');
|
|
|
|
} else if (poTypeId === 1 ) {
|
|
|
|
const vatRate = parseFloat($('#vatRate').val()) || 0;
|
|
const vatAmount = (vatRate / 100) * grossAfterDiscount;
|
|
const finalAmountPHP = grossAfterDiscount + vatAmount + chargesAmount;
|
|
|
|
$('#grossAmount').val(numberWithCommas(grossAfterDiscount.toFixed(4)));
|
|
$('#finalAmount').val(numberWithCommas(finalAmountPHP.toFixed(4)));
|
|
|
|
$('#grossAmountUSD, #finalAmountUSD, #grossAmountPHP, #finalAmountPHP').val('');
|
|
|
|
} else {
|
|
|
|
const finalAmount = grossAfterDiscount + chargesAmount;
|
|
|
|
$('#grossAmount').val(numberWithCommas(grossAfterDiscount.toFixed(4)));
|
|
$('#finalAmount').val(numberWithCommas(finalAmount.toFixed(4)));
|
|
|
|
$('#grossAmountUSD, #finalAmountUSD, #grossAmountPHP, #finalAmountPHP').val('');
|
|
}
|
|
}
|
|
|
|
// =====================================================================
|
|
// ROW-LEVEL: Update TotalAmount cell then trigger recalc
|
|
// =====================================================================
|
|
|
|
function bindTableInputEvents() {
|
|
$(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;
|
|
|
|
$row.find('td').eq(8).text((qty * price).toFixed(4));
|
|
|
|
recalculateAll();
|
|
});
|
|
}
|
|
|
|
// =====================================================================
|
|
// SECONDARY INPUT TRIGGERS
|
|
// =====================================================================
|
|
|
|
function bindSecondaryInputEvents() {
|
|
// Discount changed → recalculate
|
|
$('#discount').on('input', function () {
|
|
recalculateAll();
|
|
});
|
|
|
|
// VAT rate changed → recalculate (only relevant for POTypeId=1)
|
|
$('#vatRate').on('input', function () {
|
|
recalculateAll();
|
|
});
|
|
|
|
// Currency exchange rate changed → recalculate (only relevant for POTypeId=3)
|
|
$('#currencyCER').on('input', function () {
|
|
recalculateAll();
|
|
});
|
|
|
|
// PO Type changed → recalculate with new logic branch
|
|
$('#poTypeId').on('change', function () {
|
|
recalculateAll();
|
|
});
|
|
|
|
// Charges table changes → recalculate
|
|
$('#DestChargesTable').on('input change', function () {
|
|
recalculateAll();
|
|
});
|
|
}
|
|
|
|
// =====================================================================
|
|
// INIT — call on page load / form load
|
|
// =====================================================================
|
|
|
|
function initPOCalculations() {
|
|
bindTableInputEvents();
|
|
bindSecondaryInputEvents();
|
|
recalculateAll(); // Populate on load from existing data
|
|
}
|
|
|
|
// ─── Kept for backward compatibility if called elsewhere ────────────
|
|
function lessDiscount() { recalculateAll(); }
|
|
function calculateFinalPesoAmount() { recalculateAll(); }
|
|
function calculateFinalUsdAmount() { recalculateAll(); }
|
|
function calculateTotalPesoAmount() { bindTableInputEvents(); }
|
|
function calculateTotalUsdAmount() { bindTableInputEvents(); } |