NonInventPurchasingSystem/CPRNIMS.WebApps/wwwroot/JsFunctions/Inventory/InventoryV2.js
2026-01-20 07:44:30 +08:00

323 lines
12 KiB
JavaScript

function renderItembtns(data, row) {
var jsonData = JSON.stringify(row).replace(/"/g, """);
var buttonsHtml = '';
// var statusNumber = parseInt(row.status, 10);
buttonsHtml += '<button onclick="viewItemDetail(' + jsonData + ')" class="btn btn-default">' +
'<i class="fa-solid fa-eye fa-xl" style="color: #008080;" aria-hidden="true"></i>' +
'</button > ';
return buttonsHtml;
}
function holdItem(isApprove) {
$('#addRemarksUpdate').modal('show');
$('#addRemarksUpdate').css('z-index', 1060);
// Ensure the event listener is added only once
if (!confirmUpdateListener) {
document.getElementById('btnAddRemarks').addEventListener('click', function () {
var Remarks = document.getElementById('remarks').value;
if (isApprove === 3 && !Remarks) {
alert('Please put remarks!');
return;
}
// Show a confirmation dialog
const confirmation = confirm('Are you sure you want to proceed?');
if (confirmation) {
// If all validation is passed then proceed to confirmation to backend
// confirmPRApproveReject(isApprove, Remarks);
var loader = $('#overlay, #loader').css('z-index', 1060);
var ItemNo = document.getElementById("itemNo").value;
var Status = isApprove;
$.ajax({
url: '/PRMgmt/PostPRApproveReject',
type: 'POST',
data: { ItemNo: ItemNo, Status: Status, PRDetailsId: PRDetailsId, Remarks: Remarks }, // Pass requestData array to the backend
success: function (response) {
if (response.success) {
prTable.ajax.reload();
prDataTable.ajax.reload();
$('#viewPRItemDetails').modal('hide');
$('#addRemarksUpdate').modal('hide');
if (isApprove == 1) {
alert('Item Hold!');
}
} else {
itemTable.ajax.reload();
alert('Failed: ' + response.response);
}
},
beforeSend: function () {
// Show the loader before making the AJAX request
loader.show();
},
complete: function () {
// Hide the loader after the AJAX request is complete (success or error)
loader.hide();
}
});
}
});
// Set the flag to true to indicate that the listener is attached
confirmUpdateListener = true;
}
}
function postPutLotBin(isSave) {
var loader = $('#overlay, #loader').css('z-index', 1060);
const lotIdInput = document.getElementById('lotId');
const lotNameInput = document.getElementById('lotNo');
const LotId = lotIdInput.value;
const LotNo = lotNameInput.value;
if (!LotId || LotId==0) {
alert('Please fill the required fields !');
if (!LotId || LotId == 0) {
lotNameInput.classList.add('error-input');
}
return;
}
const confirmation = confirm('Are you sure you want to proceed?');
if (confirmation) {
$.ajax({
url: '/InventoryMgmt/PostPutLotBin',
type: 'POST',
data: { InventoryId, LotId },
success: function (response) {
if (response.success) {
inventTable.ajax.reload();
$('#viewItemDetails').modal('hide');
if (isSave == 1) {
alert('Lot Save Successfully');
} else {
alert('Lot Updated Successfully');
}
} else {
itemTable.ajax.reload();
alert('Failed: ' + response.response);
}
},
beforeSend: function () {
// Show the loader before making the AJAX request
loader.show();
},
complete: function () {
// Hide the loader after the AJAX request is complete (success or error)
loader.hide();
}
});
}
}
function populateLotNo() {
$.ajax({
url: "/InventoryMgmt/GetLotNo",
success: function (response) {
if (response.success && Array.isArray(response.data)) {
var lots = response.data;
$('#lotNo').empty();
// Add the default option
$('#lotNo').append($('<option>', {
value: 'select',
text: 'Select Location',
disabled: true,
selected: true
}));
lots.forEach(function (lot) {
$('#lotNo').append($('<option>', {
value: lot.lotId,
text: lot.lotName
}));
});
$('#lotNo').on('change', function () {
var selectedValue = $(this).val();
if (selectedValue === 'select') {
$('#lotId').val('');
} else {
$('#lotId').val(selectedValue);
}
});
} else {
console.error('Invalid response format or data is not an array');
}
},
error: function (error) {
console.error('Error fetching lot numbers:', error);
}
});
}
function showHideLabelButtons(isNewOrUpdate) {
//console.log('isNewOrUpdate: ' + isNewOrUpdate);
if (isNewOrUpdate == 1) {
let labelHeaderUpdate = document.getElementById('headerUpdate');
let btnUpdateLayoutType = document.getElementById('btnUpdateItem');
labelHeaderUpdate.style.display = 'none';
btnUpdateLayoutType.style.display = 'none';
let labelHeaderAddNew = document.getElementById('headerNew');
let btnAddNewLayoutType = document.getElementById('btnaddNewItem');
labelHeaderAddNew.style.display = 'block';
btnAddNewLayoutType.style.display = 'block';
} else {
let labelHeaderAddNew = document.getElementById('headerNew');
let btnAddNewLayoutType = document.getElementById('btnaddNewItem');
labelHeaderAddNew.style.display = 'none';
btnAddNewLayoutType.style.display = 'none';
let labelHeaderUpdate = document.getElementById('headerUpdate');
let btnUpdateLayoutType = document.getElementById('btnUpdateItem');
labelHeaderUpdate.style.display = 'block';
btnUpdateLayoutType.style.display = 'block';
}
}
function showModalNewItem() {
let update = 1;
// showHideLabelButtons(update);
$('#addNewItem').modal('show');
}
function viewItemDetail(data) {
var loader = $('#overlay, #loader').css('z-index', 1060);
populateLotNo();
InventoryId = data.inventoryId;
$.ajax({
url: '/InventoryMgmt/GetInventoryById',
type: 'POST',
data: { InventoryId },
beforeSend: function () {
loader.show();
},
complete: function () {
loader.hide();
},
success: function (data) {
if (data && data.data && data.data.length > 0) {
var item = data.data[0];
$('#itemName').val(item.itemName);
$('#itemDescription').val(item.itemDescription);
$('#itemCategoryName').val(item.itemCategoryName);
$('#statusName').val(item.statusName);
$('#itemNo').val(item.itemNo);
$('#uomName').val(item.uomName);
$('#itemColorName').val(item.itemColorName);
$('#createdDate').val(item.createdDate);
$('#itemLocalName').val(item.itemLocalName);
$('#qtyIn').val(item.qtyIn);
$('#qtyOut').val(item.qtyOut);
$('#qtyOnHand').val(item.qtyOnHand);
$('#lotId').val(item.lotId);
$('#lotNo').val(item.lotNo);
$('#lotTypeId').val(item.lotTypeId);
$('#lotTypeName').val(item.lotTypeName);
var itemPicturePath = item.itemAttachPath;
if (!itemPicturePath || itemPicturePath === 'N/A' || itemPicturePath === 'None') {
$('#itemPictureImage').attr('src', '/Content/Common/empty.jpg'); // Default image path
} else {
var imageUrl = item.url + itemPicturePath; // Use HTTPS protocol
// Set the image source
$('#itemPictureImage').attr('src', imageUrl);
}
$('#viewItemDetails').modal('show');
$('#viewItemDetails').css('z-index', 1060);
} else {
console.log('Data is null or undefined');
// window.location.href = '/Home/Logout';
}
},
error: function (xhr, error, thrown) {
console.log('Authentication error:', error);
console.log('Details:', thrown);
window.location.href = '/Home/Logout';
}
});
}
function sortByDate() {
// Custom sorting function for MM/DD/YYYY format
$.fn.dataTable.ext.type.order['date-custom-pre'] = function (d) {
// Parse date string in format MM/DD/YYYY
console.log('Sorting by custom date');
var dateParts = d.split('/');
return new Date(dateParts[2], dateParts[0] - 1, dateParts[1]).getTime();
};
isSorting = true;
// Trigger DataTable reload to apply the new sorting and date range filtering
$('#InventoryTable').DataTable().ajax.reload();
}
$(document).ready(function () {
loader = $('#overlay, #loader');
UserRights = document.getElementById("roleRights").value;
inventTable = $('#InventoryTable').DataTable({
ajax: $.extend({
url: '/InventoryMgmt/GetInventoryByUserId',
type: 'GET',
data: function (d) {
d.dateFrom = $('#dateFrom').val();
d.dateTo = $('#dateTo').val();
d.isSorting = isSorting;
}
}, beforeComplete(loader)),
initComplete: initCompleteCallback(),
scrollX:true,
dom: 'Bfrtip',
buttons: [
'csv', 'excel', 'pdf',
],
columns: [
{ data: 'inventoryId'},
{ data: 'itemNo' },
{ data: 'itemName' },
{ data: 'itemDescription' },
{ data: 'itemCategoryName' },
{ data: 'department' },
{ data: 'qtyIn' },
{ data: 'qtyOut' },
{ data: 'qtyOnHand' },
{ data: 'remainingQty' },
{ data: 'lotNo' },
{
data: 'createdDate',
type: 'date-custom',
render: function (data, type, row) {
if (type === 'display' && data) {
return formatDate(data);
}
return data;
}
},
{
data: null,
render: function (data, type, row) {
return renderItembtns(data, row);
}
},
],
responsive: false,
order: [[0, 'asc']],
language: {
emptyTable: "No record available"
},
error: errorHandler
});
});
function removeErrorClass() {
if (this.value.trim() !== '') {
this.classList.remove('error-input');
}
}