NonInventPurchasingSystem/CPRNIMS.WebApps/wwwroot/JsFunctions/PO/POPutPostV4.js
2026-03-11 14:26:41 +08:00

976 lines
37 KiB
JavaScript

function putPOCancel() {
loader = $('#overlay, #loader').css('z-index', 1070);
var PONo = $('#c-old-poNo').val();
const reasonInput = document.getElementById('reason');
const otherReason = document.getElementById('otherReason');
const POTypeId = $('#potype-cancel').val();
reasonInput.addEventListener('input', removeErrorClass);
otherReason.addEventListener('input', removeErrorClass);
var Remarks = reasonInput.value;
if (Remarks == 'otherReason') {
Remarks = otherReason.value;
}
if (Remarks == 0 || !Remarks) {
if (Remarks == null || !Remarks) {
otherReason.classList.add('error-input');
} else {
reasonInput.classList.add('error-input');
}
showToast('error', 'Please fill the required fields !', 'Cancellation failed!', 4000);
return;
}
showConfirmation({
title: 'Cancel Purchase Order',
message: 'Are you sure you want to cancel this PO? This action cannot be undone.',
type: 'danger',
confirmText: 'Yes, Cancel PO',
cancelText: 'Keep PO'
}).then((confirmed) => {
if (confirmed) {
$.ajax($.extend({
url: endpoint.PutPOCancel,
type: 'POST',
data: { POTypeId, PONo, Remarks },
success: function (response) {
if (response.success) {
approvedPOTable.ajax.reload();
showToast('success','PO cancelled successfully', 'Success', 4000);
} else {
showToast('error', response.response, 'Cancellation failed',4000);
}
},
error: errorHandler
}, beforeComplete(loader)));
}
});
}
function putPOItemDetail() {
loader = $('#overlay, #loader').css('z-index', 1075);
const itemDescription = document.getElementById('itemDescription');
const specification = document.getElementById('specification');
const qty = document.getElementById('itemQty');
const uomId = document.getElementById('uomId');
const unitPrice = document.getElementById('unitPrice');
const paymentTermsId = document.getElementById('P-paymentTermsId');
const paymentTerms = document.getElementById('P-paymentTerms');
const podId = document.getElementById('P-portOfDischarge-id');
const portOfDischarge = document.getElementById('P-portOfDischarge');
itemDescription.addEventListener('input', removeErrorClass);
specification.addEventListener('input', removeErrorClass);
qty.addEventListener('input', removeErrorClass);
uomId.addEventListener('input', removeErrorClass);
unitPrice.addEventListener('input', removeErrorClass);
paymentTerms.addEventListener('input', removeErrorClass);
portOfDischarge.addEventListener('input', removeErrorClass);
const UOMId = uomId.value;
const ItemDescription = itemDescription.value;
const Specification = specification.value;
const Qty = qty.value;
const UnitPrice = unitPrice.value;
const PaymentTermsId = paymentTermsId.value;
const PodId = podId.value;
const PortOfDischarge = portOfDischarge.value;
var poTypeId = $('#potype-cancel').val();
if (!UOMId || !Specification || !Qty || !UnitPrice || !PaymentTermsId || PaymentTermsId == 0 || !ItemDescription) {
showToast('warning', 'Please fill the required fields !', 'Updating failed!', 4000);
if (!UOMId) {
uomId.classList.add('error-input');
}
if (!ItemDescription) {
itemDescription.classList.add('error-input');
}
if (!Specification) {
specification.classList.add('error-input');
}
if (!Qty) {
qty.classList.add('error-input');
}
if (!UnitPrice) {
unitPrice.classList.add('error-input');
}
if (!PaymentTermsId || PaymentTermsId == 0) {
paymentTerms.classList.add('error-input');
}
return;
}
if (poTypeId == 3) {
if (!PodId || PodId == 0) {
showToast('warning', 'Please fill the required fields !', 'Updating failed!', 4000);
portOfDischarge.classList.add('error-input');
return;
}
}
PRDetailsId = $('#prDetailsId').val();
let PONo = $('#c-old-poNo2').val();
let PORemarks = $('#c-poRemarks').val();
showConfirmation({
title: 'Update Item Detail',
message: 'Are you sure you want to update this item? This action cannot be undone.',
type: 'warning',
confirmText: 'Yes, Update Item',
cancelText: 'Keep Item'
}).then((confirmed) => {
if (confirmed) {
$.ajax($.extend({
url: endpoint.PutPOItemDetail,
type: 'POST',
data: { PRDetailsId, UOMId, Specification, Qty, PONo, PORemarks, UnitPrice, PaymentTermsId, ItemDescription, PodId },
success: function (response) {
if (response.success) {
$('#viewPRItemDetail').modal('hide');
if (typeof updatePOItemTable !== 'undefined' && $.fn.dataTable.isDataTable(updatePOItemTable.table().node())) {
updatePOItemTable.ajax.reload();
}
if (typeof PRItemTable !== 'undefined' && $.fn.dataTable.isDataTable(PRItemTable.table().node())) {
PRItemTable.ajax.reload();
}
showToast('success', 'PR Item Details Updated!', 'Success', 4000);
} else {
showToast('error', response.response, 'Updating failed', 4000);
}
},
error: errorHandler
}, beforeComplete(loader)));
}
});
}
function putMyPONo() {
loader = $('#overlay, #loader').css('z-index', 1070);
var PONo = $('#c-old-poNo').val();
const reasonInput = document.getElementById('reason');
const poTypeInput = document.getElementById('poType');
poTypeInput.addEventListener('input', removeErrorClass);
reasonInput.addEventListener('input', removeErrorClass);
const PORemarks = reasonInput.value;
const POTypeId = poTypeInput.value;
if (POTypeId==0 || PORemarks ==0) {
showToast('error', 'Please fill the required fields !', 'PO update failed!', 4000);
if (POTypeId == 0) {
poTypeInput.classList.add('error-input');
}
if (PORemarks ==0) {
reasonInput.classList.add('error-input');
}
return;
}
showConfirmation({
title: 'Update Purchase Order',
message: 'Are you sure you want to update this PO? This action cannot be undone.',
type: 'danger',
confirmText: 'Yes, Update PO',
cancelText: 'Keep PO'
}).then((confirmed) => {
if (confirmed) {
$.ajax($.extend({
url: endpoint.PutMyPONo,
type: 'POST',
data: { PONo, PORemarks, POTypeId },
success: function (response) {
if (response.success) {
$('#viewPONo').modal('hide');
approvedPOTable.ajax.reload();
$('#c-poNo').val('');
$('#poType').val(0);
showToast('success', 'PO Updated successfully', 'Success', 4000);
} else {
showToast('error', response.response, 'Cancellation failed', 4000);
}
},
error: errorHandler
}, beforeComplete(loader)));
}
});
}
function postPOToSupplier() {
loader = $('#overlay, #loader').css('z-index', 1070);
PONo = document.getElementById('poNoHidden').value;
const confirmation = confirm('Are you sure you want to proceed?');
if (confirmation) {
$.ajax($.extend({
url: endpoint.PostPOToSupplier,
type: 'POST',
data: { POTypeId, EmailAddress, PONo },
success: function (response) {
if (response.success) {
$('#viewPrintImportPO').modal('hide');
$('#viewPOFormSIPO').modal('hide');
approvedPOTable.ajax.reload();
alert('PO Forwarded Successfully!');
} else {
alert('Failed: ' + response.response);
}
},
error: errorHandler
}, beforeComplete(loader)));
}
}
function postPutCustomPO() {
loader = $('#overlay, #loader').css('z-index', 1070);
if (isUpdate) {
updateExistingPO()
} else {
docRequiredList();
otherCharges();
POTypeId = $('#poTypeId').val();
PONo = $('#poNo').val();
var PoRemarks = $('#remarks').val() ?? 'N/A';
if (!prItemList()) {
return;
}
if (!poValidateParameters(POTypeId, 'customPO', false)) {
return false;
}
let Discount = $('#discount').val();
showConfirmation({
title: 'Create Purchase Order',
message: 'Are you sure you want to create this PO? This action cannot be undone.',
type: 'warning',
confirmText: 'Yes, Create PO',
cancelText: 'Cancel'
}).then((confirmed) => {
if (confirmed) {
$.ajax($.extend({
url: endpoint.PostPutCustomPO,
type: 'POST',
data: {
DocRequiredList, OtherChargesList, PRItemList,
POTypeId, PONo, IncotermsId, ProfInvoiceNo, ProfInvoiceDate,
ShippingInstructionId, PodId, PaymentTermsId,
PoRemarks, Discount, SupplierId, DeliveryDate, DeliverTo
},
success: function (response) {
if (response.success) {
clearCustomPOParam();
const poNo = response.data?.customPOs?.[0]?.poNo || 'N/A';
$('#poNoFinal').val(poNo);
if (typeof PRItemTable !== "undefined" && PRItemTable) {
PRItemTable.ajax.reload();
}
isLoaded = false;
showToast('success', 'PO Created successfully with final PO#: ' + poNo, 'Success', 4000);
} else {
showToast('error', response.response, 'PO Creation failed', 4000);
}
},
error: errorHandler
}, beforeComplete(loader)));
}
});
}
}
function updateExistingPO() {
docRequiredList();
otherCharges();
POTypeId = $('#poTypeId').val();
PONo = $('#poNo').val();
var PoRemarks = $('#remarks').val() ?? 'N/A';
if (!prItemList()) {
return;
}
if (!poValidateParameters(POTypeId, 'customPO', false)) {
return false;
}
let Discount = $('#discount').val();
let IsUpdate = true;
showConfirmation({
title: 'Update Purchase Order',
message: 'Are you sure you want to update this PO? This action cannot be undone.',
type: 'warning',
confirmText: 'Yes, Update PO',
cancelText: 'Cancel'
}).then((confirmed) => {
if (confirmed) {
$.ajax($.extend({
url: endpoint.PostPutCustomPO,
type: 'POST',
data: {
DocRequiredList, OtherChargesList, PRItemList,
POTypeId, PONo, IncotermsId, ProfInvoiceNo, ProfInvoiceDate,
ShippingInstructionId, PodId, PaymentTermsId,
PoRemarks, Discount, SupplierId, DeliveryDate, DeliverTo, IsUpdate
},
success: function (response) {
if (response.success) {
clearCustomPOParam();
const poNo = $('#poNoFinal').val();
if (typeof PRItemTable !== "undefined" && PRItemTable) {
PRItemTable.ajax.reload();
}
isLoaded = false;
showToast('success', 'PO Updated successfully with final PO#: ' + poNo, 'Success', 4000);
} else {
showToast('error', response.response, 'PO Updating failed', 4000);
}
},
error: errorHandler
}, beforeComplete(loader)));
}
});
}
function postPutDocRequired() {
loader = $('#overlay, #loader').css('z-index', 1075);
let DocRequirementId = $('#docRequirementId').val();
const docNameInput = document.getElementById('docName');
docNameInput.addEventListener('input', removeErrorClass);
const DocName = docNameInput.value;
if (!DocName) {
showToast('warning', 'Please fill the required fields!', 'warning', 4000);
docNameInput.classList.add('error-input');
return;
}
const isUpdate = DocRequirementId == 0 || !DocRequirementId ? 'create' : 'update';
const title = DocRequirementId == 0 || !DocRequirementId ? 'Other Charges Creation' : 'Other Charges Update';
showConfirmation({
title: title,
message: `Are you sure you want to ${isUpdate} this Other Charges? This action cannot be undone.`,
type: 'warning',
confirmText: `Yes, ${isUpdate}`,
cancelText: 'No'
}).then((confirmed) => {
if (confirmed) {
$.ajax($.extend({
url: endpoint.PostPutDocRequired,
type: 'POST',
data: { DocRequirementId, DocName },
success: function (response) {
if (response.success) {
$('#viewDocReqModal').modal('hide');
docRequiredTable.ajax.reload(null, false);
showToast('success', `${title} successfully!`, 'success', 4000);
} else {
showToast('error', response.response, 'failed', 4000);
}
},
error: errorHandler
}, beforeComplete(loader)));
}
});
}
function postPutOtherCharges() {
loader = $('#overlay, #loader').css('z-index', 1075);
let OtherChargesId = $('#otherChargesId').val();
const otherChargesNamenput = document.getElementById('otherChargesName');
otherChargesNamenput.addEventListener('input', removeErrorClass);
const OtherChargesName = otherChargesNamenput.value;
if (!OtherChargesName) {
showToast('warning','Please fill the required fields!','warning',4000);
otherChargesNamenput.classList.add('error-input');
return;
}
const isUpdate = OtherChargesId == 0 || !OtherChargesId ? 'create' : 'update';
const title = OtherChargesId == 0 || !OtherChargesId ? 'Other Charges Creation' : 'Other Charges Update';
showConfirmation({
title: title,
message: `Are you sure you want to ${isUpdate} this Other Charges? This action cannot be undone.`,
type: 'warning',
confirmText: `Yes, ${isUpdate}`,
cancelText: 'No'
}).then((confirmed) => {
if (confirmed) {
$.ajax($.extend({
url: endpoint.PostPutOtherCharges,
type: 'POST',
data: { OtherChargesId, OtherChargesName },
success: function (response) {
if (response.success) {
$('#viewOtherChargesModal').modal('hide');
OtherChargesTable.ajax.reload(null, false);
showToast('success', `${title} successfully!`, 'success', 4000);
} else {
showToast('error', response.response, 'failed', 4000);
}
},
error: errorHandler
}, beforeComplete(loader)));
}
});
}
function postPutIncoterms() {
loader = $('#overlay, #loader').css('z-index', 1075);
const IncotermsId = $('#update-incotermsId').val();
const incotermsName = document.getElementById('update-incotermsName');
incotermsName.addEventListener('input', removeErrorClass);
const IncotermsName = incotermsName.value;
if (!IncotermsName) {
showToast('warning', 'Please fill the required fields!', 'warning', 4000);
incotermsName.classList.add('error-input');
return;
}
const isUpdate = IncotermsId == 0 || !IncotermsId ? 'create' : 'update';
const title = IncotermsId == 0 || !IncotermsId ? 'Incoterms Creation' : 'Incoterms Update';
showConfirmation({
title: title,
message: `Are you sure you want to ${isUpdate} this Incoterms? This action cannot be undone.`,
type: 'warning',
confirmText: `Yes, ${isUpdate}`,
cancelText: 'No'
}).then((confirmed) => {
if (confirmed) {
$.ajax($.extend({
url: endpoint.PostPutIncoterms,
type: 'POST',
data: { IncotermsId, IncotermsName },
success: function (response) {
if (response.success) {
$('#viewIncotermsModal').modal('hide');
incotermsTable.ajax.reload(null, false);
showToast('success', `${title} successfully!`, 'success', 4000);
} else {
showToast('error', response.response, 'failed', 4000);
}
},
error: errorHandler
}, beforeComplete(loader)));
}
});
}
function prItemList() {
var isValid = true;
PRItemList = [];
poDataTable = $('#PODataTable').DataTable();
poDataTable.rows().data().each(function (data, index) {
var $row = $(poDataTable.row(index).node());
var Quantity = parseFloat($row.find('.qty').eq(0).val());
var UnitPrice = parseFloat($row.find('.unitPrice').eq(0).val());
var Specification = $row.find('.specification').eq(0).val();
if (isNaN(Quantity) || Quantity <= 0 || Quantity == '0') {
showToast('error', 'Item for ' + data.specification + ' Qty cannot be empty or zero!',
'Creation of purchase order failed!', 4000);
isValid = false;
}
if (isNaN(UnitPrice) || UnitPrice <= 0 || UnitPrice == '0') {
showToast('error', 'Item for ' + data.itemNo + ' Unit Price cannot be empty or zero!',
'Creation of purchase order failed!', 4000);
isValid = false;
}
if (!Specification || Specification=='undefined') {
showToast('error', 'Item for ' + data.itemNo + ' Specification cannot be empty or undefined!',
'Creation of purchase order failed!', 4000);
isValid = false;
}
PRItemList.push({
PRNo: data.prNo,
PRDetailsId: data.prDetailsId,
Specification: Specification,
Quantity: Quantity,
UnitPrice: UnitPrice,
Amount: parseFloat(Quantity * UnitPrice)
});
});
if (PRItemList.length === 0) {
showToast('error', 'Please select an item in the list!',
'Creation of purchase order failed!', 4000);
return false;
}
return isValid;
}
function otherCharges() {
OtherChargesList = [];
$('#DestChargesTable tbody tr').each(function () {
var row = $(this);
OtherChargesList.push({
OtherChargesId: row.find('td:eq(0)').text().trim(),
Amount: row.find('td:eq(2)').text().trim()
});
});
return OtherChargesList;
}
function docRequiredList() {
DocRequiredList = [];
var selectedCheckboxes = $('.select-DocRequired-checkbox:checked');
selectedCheckboxes.each(function () {
var $row = $(this).closest('tr');
var rowIndex = docRequiredTable.row($row).index();
var rowData = docRequiredTable.row(rowIndex).data();
var itemData = {
DocRequirementId: rowData.docRequirementId,
DocName: rowData.docName
};
DocRequiredList.push(itemData);
});
return DocRequiredList;
}
function postPutPO() {
loader = $('#overlay, #loader').css('z-index', 1070);
var selectedCheckboxes = $('.select-DocRequired-checkbox:checked');
var DocRequiredList = [];
selectedCheckboxes.each(function () {
var $row = $(this).closest('tr');
var rowIndex = docRequiredTable.row($row).index();
var rowData = docRequiredTable.row(rowIndex).data();
var itemData = {
DocRequirementId: rowData.docRequirementId,
DocName: rowData.docName
};
DocRequiredList.push(itemData);
});
POTypeId = $('#poTypeId').val();
SupplierId = $('#supplierId').val();
if (!POTypeId) {
POTypeId = POTypeIdStored;
}
PONo = $('#poNoHidden').val();
var PORemarks = 'N/A';
const incotermsIdInput = document.getElementById('incoTermsId');
const profInvoiceNoInput = document.getElementById('piNo');
const profInvoiceDateInput = document.getElementById('piDate');
const shippingInstructionIdInput = document.getElementById('shippingInstructionId');
const podIdInput = document.getElementById('podId');
const paymentTermsIdInput = document.getElementById('C-paymentTermsId');
const deliverToInput = document.getElementById('deliverTo');
incotermsIdInput.addEventListener('input', removeErrorClass);
profInvoiceNoInput.addEventListener('input', removeErrorClass);
profInvoiceDateInput.addEventListener('input', removeErrorClass);
shippingInstructionIdInput.addEventListener('input', removeErrorClass);
podIdInput.addEventListener('input', removeErrorClass);
paymentTermsIdInput.addEventListener('input', removeErrorClass);
deliverToInput.addEventListener('input', removeErrorClass);
const IncotermsId = incotermsIdInput.value;
const ProfInvoiceNo = profInvoiceNoInput.value;
const ProfInvoiceDate = profInvoiceDateInput.value;
const ShippingInstructionId = shippingInstructionIdInput.value;
const PodId = podIdInput.value;
const DeliverTo = deliverToInput.value;
const PaymentTermsId = paymentTermsIdInput ? paymentTermsIdInput.value : '';
if (UserRights == 'CnvssAppver') {
if (POTypeId == 3) {
PORemarks = $('#poRemarksImport').val();
if (!IncotermsId || !ProfInvoiceNo || !ProfInvoiceDate || !ShippingInstructionId || !PodId || !PaymentTermsId || !DeliverTo) {
showToast('error', 'Please fill the required fields!', 'Update failed', 4000);
if (!IncotermsId) {
incotermsIdInput.classList.add('error-input');
}
if (!ProfInvoiceNo) {
profInvoiceNoInput.classList.add('error-input');
}
if (!ProfInvoiceDate) {
profInvoiceDateInput.classList.add('error-input');
}
if (!ShippingInstructionId) {
shippingInstructionIdInput.classList.add('error-input');
}
if (!PodId) {
podIdInput.classList.add('error-input');
}
if (!PaymentTermsId) {
paymentTermsIdInput.classList.add('error-input');
}
if (!DeliverTo) {
deliverToInput.classList.add('error-input');
}
return;
}
if (selectedCheckboxes.length <= 0) {
showToast('error', 'No selected item!','Update failed', 4000);
return;
}
}
else {
PORemarks = $('#poRemarksSIDRPO').val();
}
}
var title = (UserRights == 'CnvssAppver') ? 'Create' : 'Approve';
showConfirmation({
title: title +' Purchase Order',
message: 'Are you sure you want to ' + title +' this PO? This action cannot be undone.',
type: 'danger',
confirmText: 'Yes, ' + title +' PO',
cancelText: 'No'
}).then((confirmed) => {
if (confirmed) {
$.ajax($.extend({
url: endpoint.PostPutPO,
type: 'POST',
data: {
DocRequiredList, POTypeId, SupplierId, PONo, IncotermsId, ProfInvoiceNo, ProfInvoiceDate,
ShippingInstructionId, PodId, PaymentTermsId, PORemarks, DeliverTo
},
success: function (response) {
if (response.success) {
if (POTypeId == 3) {
$('#addNewImportPO').modal('hide');
viewApprovedPOFormImport(poData, 0);
let btnaddNewPO = document.getElementById('btnaddNewPO');
btnaddNewPO.style.display = 'none';
} else {
showToast('success', 'PO '+ title +' Successfully!', 'Success', 4000);
if (UserRights == 'CnvssAppver') {
let btnPrintSIPOApprove = document.getElementById('btnPrintSIPOApprove');
btnPrintSIPOApprove.style.display = 'none';
updateESignature(POTypeId, EmailAddress);
poDataTable.ajax.reload();
$('#addRemarks').modal('hide');
} else {
updateESignatureApproved(POTypeId, EmailAddress, PONo);
}
}
} else {
showToast('error', response.response, title + ' failed', 4000);
}
},
error: errorHandler
}, beforeComplete(loader)));
}
});
}
function AddItem() {
const selectedItems = Object.values(selectedProductsMap);
if (selectedItems.length === 0) {
showToast('error', 'Please select items first.', 'Adding item failed', 4000);
return;
}
// Transform data if needed (optional)
const transformedData = selectedItems.map(item => {
return {
prDetailsId: item.prDetailsId,
unitPrice: item.unitPrice,
prDetailsId: item.prDetailsId,
itemNo: item.itemNo,
itemName: item.itemName,
uomName: item.uomName,
qty: item.qty,
prNo: item.prNo,
totalAmount: item.totalAmount,
specification: item.specification,
};
});
poDataTable.rows.add(transformedData);
poDataTable.draw();
clearTableSelection('#PRItemTable', selectedProductsMap, function () {
$('#totalSelected').text('0');
});
$('#addItemWOCanvass').modal('hide');
showToast('success',`${transformedData.length} item(s) successfully`,'success',4000);
isAdded = true;
toggleSubmitButton();
}
function AddDocRequired() {
var selectedCheckboxes = $('.select-DocRequired-checkbox:checked');
var DocRequiredList = [];
selectedCheckboxes.each(function () {
var $row = $(this).closest('tr');
var rowIndex = docRequiredTable.row($row).index();
var rowData = docRequiredTable.row(rowIndex).data();
var itemData = {
DocRequirementId: rowData.docRequirementId,
DocName: rowData.docName
};
DocRequiredList.push(itemData);
});
if (selectedCheckboxes.length <= 0) {
alert('No selected item!');
return;
}
// Extract the DocName values and join them with commas
var docNames = DocRequiredList.map(function (item) {
return item.DocName;
}).join(', ');
// Set the value of the textarea to the comma-separated list of document names
$('#docRequiredId').val(docNames);
$('#docsRequired').modal('hide');
}
function AddOtherCharges() {
var selectedRows = $('#SourceChargesTable tbody tr.selected-row');
let isDuplicate = false;
selectedRows.each(function () {
var row = $(this);
var checkbox = row.find('td:eq(0) input.selected-Item-checkbox');
var otherChargesName = row.find('td:eq(1)').text();
let amount = row.find('.o-amount').val();
let otherChargesId = checkbox.data('otherchargesid');
if (!amount || amount <= 0) {
isDuplicate = true;
showToast('error', `${otherChargesName} amount must be greater than 0!`, 'failed', 4000);
return;
}
$('#DestChargesTable tbody tr').each(function () {
var existingotherChargesId = $(this).find('td:eq(0)').text().trim();
if (String(existingotherChargesId).trim() === String(otherChargesId).trim()) {
isDuplicate = true;
return false;
}
});
if (isDuplicate) {
showToast('error', "This charges name: " + otherChargesName + " already exists in the list!", 'failed', 4000);
isDuplicate = true;
return;
}
var newRow = '<tr>' +
'<td style="display: none;">' + otherChargesId + '</td>' +
'<td>' + otherChargesName + '</td>' +
'<td>' + amount + '</td>' +
'<td><button class="btn btn-default" onclick="removeRow(this)" data-otherChargesId="' + otherChargesId + '">' +
'<i class="fa-solid fa-trash fa-xl" style="color: #ff0000;" aria-hidden="true"></i>' +
'</button></td>' +
'</tr>';
$('.selected-Item-checkbox').prop('checked', false);
$('#SourceChargesTable tbody tr').removeClass('selected-row');
$('#DestChargesTable tbody').append(newRow);
});
if (!isDuplicate) {
showToast('success', 'Other Charges Added Successfuly!', 'Success', 4000);
$('#addCharges').modal('hide');
}
calculateFinalPesoAmount($('#poTypeId').val());
calculateFinalUsdAmount();
lessDiscount();
}
function AddDiscount() {
let discountValue = $('#discountAmount').val();
if (isNaN(discountValue) || discountValue.trim() === "") {
showToast('error', `Please input a valid discount value!`, 'failed', 4000);
return;
}
$('#discount').val(numberWithCommas(discountValue));
$('#discountAmount').val('');
calculateFinalPesoAmount($('#poTypeId').val());
calculateFinalUsdAmount();
lessDiscount();
}
function removeRowMain(rowData) {
var poDataTable = $('#PODataTable').DataTable();
// Remove the row from PODataTable
if (typeof poDataTable !== "undefined" && poDataTable) {
poDataTable
.rows(function (idx, data, node) {
return data.prDetailsId === rowData.prDetailsId;
})
.remove()
.draw();
calculateTotalPesoAmount($('#poTypeId').val());
calculateFinalPesoAmount($('#poTypeId').val());
toggleSubmitButton();
}
// Check if the row already exists in PRItemTable
if (typeof prItemTable !== "undefined" && prItemTable) {
var prItemTable = $('#PRItemTable').DataTable({
destroy: true,
columns: [
{ data: 'prDetailsId', visible: false },
{ data: 'itemNo' },
{ data: 'itemName' },
{ data: 'specification' },
{ data: 'qty' },
{ data: 'uomName' },
{
data: 'unitPrice',
render: $.fn.dataTable.render.number(',', '.', 2)
},
{
data: 'totalAmount',
render: $.fn.dataTable.render.number(',', '.', 2)
}
]
});
}
}
function removeRow(button) {
$(button).closest('tr').remove();
calculateFinalPesoAmount($('#poTypeId').val());
}
function postApprovedSuggested() {
loader = $('#overlay, #loader').css('z-index', 1070);
var CanvassId = document.getElementById('canvassId').value;
var CanvassDetailId = document.getElementById('canvassDetailId').value;
var SupplierId = document.getElementById('supplierId').value;
showConfirmation({
title: 'Supplier Approval',
message: 'Are you sure you want to approve this supplier? This action cannot be undone.',
type: 'warning',
confirmText: 'Yes, Approve',
cancelText: 'No'
}).then((confirmed) => {
if (confirmed) {
$.ajax({
url: endpoint.PostApprovedSuggested,
type: 'POST',
data: { CanvassId, PODetailId, CanvassDetailId, SupplierId, ItemNo },
success: function (response) {
if (response.success) {
$('#viewSupplierBid').modal('hide');
bidDataTable.ajax.reload();
showToast('success', 'Supplier Successfully Approved!', 'Success', 4000);
} else {
showToast('error', response.response, 'Cancellation failed', 4000);
}
},
error: errorHandler
}, beforeComplete(loader));
}
});
}
function postApprovedSupplier() {
loader = $('#overlay, #loader').css('z-index', 1070);
var CanvassId = document.getElementById('canvassId').value;
var CanvassDetailId = document.getElementById('canvassDetailId').value;
var SupplierId = document.getElementById('supplierId').value;
showConfirmation({
title: 'Supplier Approval',
message: 'Are you sure you want to approve this supplier? This action cannot be undone.',
type: 'warning',
confirmText: 'Yes, Approve',
cancelText: 'No'
}).then((confirmed) => {
if (confirmed) {
$.ajax({
url: endpoint.PostApprovedSupplier,
type: 'POST',
data: { CanvassId, PODetailId, CanvassDetailId, SupplierId, ItemNo },
success: function (response) {
if (response.success) {
$('#viewSupplierBid').modal('hide');
$('#viewSupplierByItem').modal('hide');
refreshTable();
showToast('success', 'Canvass Forwarded', 'Success', 4000);
} else {
refreshTable();
showToast('error', response.response, 'Cancellation failed', 4000);
}
},
error: errorHandler
}, beforeComplete(loader));
}
});
}
function approvedSelectedPO() {
const loader = $('#overlay, #loader').css('z-index', 1070);
const POList = [];
const selectedCheckboxes = $('.selected-Item-checkbox:checked');
selectedCheckboxes.each(function () {
const $row = $(this).closest('tr');
const rowIndex = forPOTable.row($row).index();
const rowData = forPOTable.row(rowIndex).data();
const itemData = {
PONo: rowData.poNo
};
POList.push(itemData);
});
showConfirmation({
title: 'Purchase Order Approval',
message: 'Are you sure you want to approve this purchase order ? This action cannot be undone.',
type: 'warning',
confirmText: 'Yes, Approve',
cancelText: 'No'
}).then((confirmed) => {
if (confirmed) {
$.ajax({
url: endpoint.ApprovedSelectedPO,
type: 'POST',
data: { POList },
...beforeComplete(loader),
success: function (response) {
if (response.success) {
refreshTable();
showToast('success', 'PO Approved Successfully', 'Success', 4000);
} else {
refreshTable();
showToast('error', response.response, 'Cancellation failed', 4000);
}
},
error: errorHandler
});
}
});
}
function deleteIncShip(data) {
loader = $('#overlay, #loader').css('z-index', 1070);
let POId = data.poId;
showConfirmation({
title: 'Incoming For Shipment',
message: `Are you sure you want to delete PO #${data.poNo} linked to supplier ${data.supplierName}? This action cannot be undone.`,
type: 'warning',
confirmText: 'Yes, Proceed',
cancelText: 'No'
}).then((confirmed) => {
if (confirmed) {
$.ajax($.extend({
url: '/POMgmt/DeleteIncShip',
type: 'POST',
data: { POId },
success: function (response) {
if (response.success) {
$('#ForShipmentTable').modal('hide');
forShipmentTable.ajax.reload();
alert('PO for shipment deleted successfully!');
} else {
forShipmentTable.ajax.reload();
alert('Failed: ' + response.response);
}
},
error: errorHandler
}, beforeComplete(loader)));
}
});
}