function putItemDetails() {
loader = $('#overlay, #loader');
const itemPictureImageInput = document.getElementById("itemPictureImageInput");
if (itemPictureImageInput && itemPictureImageInput.files.length > 0) {
const file = itemPictureImageInput.files[0];
const allowedExtensions = ['.jpg', '.jpeg', '.png'];
const fileExtension = '.' + file.name.split('.').pop().toLowerCase();
if (!allowedExtensions.includes(fileExtension)) {
showToast('error', 'Only .jpg, .jpeg, and .png files are allowed.', 'Invalid File Type', 4000);
return;
}
const maxSizeInBytes = 2 * 1024 * 1024;
if (file.size > maxSizeInBytes) {
const fileSizeInMB = (file.size / (1024 * 1024)).toFixed(2);
showToast('error', `File size (${fileSizeInMB} MB) exceeds the 2MB limit. Please choose a smaller image.`, 'File Too Large', 4000);
return;
}
const reader = new FileReader();
reader.onload = function (event) {
sendUpdateRequest({ itemAttachPath: event.target.result }, loader);
};
reader.readAsDataURL(file);
} else {
sendUpdateRequest({ itemAttachPath: null }, loader);
}
}
function sendUpdateRequest(data, loader) {
const formElements = {
itemNo: document.getElementById('itemNo').value,
itemAttachId: document.getElementById('itemAttachId').value,
uomId: document.getElementById('uomId').value,
itemName: document.getElementById('itemName'),
itemDescription: document.getElementById('itemDescription'),
itemLocalName: document.getElementById('itemLocalName'),
uomName: document.getElementById('uomName'),
itemQty: document.getElementById('itemQty'),
itemClassId: document.getElementById('itemClassId').value,
packagingTypeId: document.getElementById('packagingTypeId').value,
prTypeId: document.getElementById('prTypeId').value,
itemColorId: document.getElementById('itemColorId').value,
itemLocalId: document.getElementById('itemLocalId').value,
isCommon: document.getElementById('isCommon').value === "true",
requestTypeId: document.getElementById('requestTypeId')
};
['itemName', 'itemDescription', 'itemLocalName', 'uomName', 'itemQty', 'requestTypeId'].forEach(id => {
formElements[id].addEventListener('input', removeErrorClass);
});
const requiredFields = [
{ element: formElements.itemName, value: formElements.itemName.value },
{ element: formElements.itemDescription, value: formElements.itemDescription.value },
{ element: formElements.itemLocalName, value: formElements.itemLocalName.value },
{ element: formElements.itemQty, value: formElements.itemQty.value, condition: value => value != 0 },
{ element: formElements.uomName, value: formElements.uomName.value },
{ element: formElements.requestTypeId, value: formElements.requestTypeId.value }
];
const isValid = requiredFields.every(field => {
const isFieldValid = field.condition ? field.condition(field.value) : !!field.value;
if (!isFieldValid) {
field.element.classList.add('error-input');
}
return isFieldValid;
});
if (!isValid) {
showToast('warning', 'Please fill the required fields!', 'Update item failed', 4000);
return;
}
const itemCategoryId = $('#ItemCategory2Id').val();
const isMDLD = document.getElementById("mdld").checked;
const submitData = {
ItemNo: formElements.itemNo,
ItemCodeId: ItemCodeId,
ItemName: formElements.itemName.value,
ItemDescription: formElements.itemDescription.value,
ItemCategoryId: itemCategoryId,
ItemLocalId: formElements.itemLocalId,
IsMDLD: isMDLD,
ItemClassId: formElements.itemClassId,
PackagingTypeId: formElements.packagingTypeId,
PRTypeId: formElements.prTypeId,
UOMId: formElements.uomId,
ItemColorId: formElements.itemColorId,
Qty: formElements.itemQty.value,
ItemAttachPath: data.itemAttachPath,
ItemAttachId: formElements.itemAttachId,
IsCommon: formElements.isCommon,
RequestTypeId: formElements.requestTypeId.value
};
showConfirmation({
title: 'Update Item Details',
message: 'Are you sure you want to update this item? This action cannot be undone.',
type: 'warning',
confirmText: 'Yes, Update',
cancelText: 'Keep Item'
}).then((confirmed) => {
if (confirmed) {
$.ajax({
url: '/ItemMgmt/PutItemDetail',
type: 'POST',
data: submitData,
success: function (response) {
if (response.success) {
clearTextModal();
fetchAndRefreshTable();
document.getElementById("itemPictureImageInput").value = '';
document.getElementById('btnAddToCart').style.display = 'block';
showToast('success', 'Updated successfully!', 'Success', 4000);
} else {
loader.hide();
isFullFilled();
showToast('error', response.response, title + ' failed', 4000);
}
},
beforeSend: function () {
loader.show();
},
complete: function () {
loader.hide();
}
});
}
});
}
function postPutPurchase() {
loader = $('#overlay, #loader').css('z-index', 1060);
isValid = true;
var Remarks = document.getElementById('requestorRemarks').value;
var selectedCheckboxes = $('.selectedItem-checkbox:checked');
var requestData = [];
selectedCheckboxes.each(function () {
var $row = $(this).closest('tr');
var rowIndex = itemTable.row($row).index();
var rowData = itemTable.row(rowIndex).data();
var itemCartId = rowData.itemCartId;
var itemNo = rowData.itemNo;
var qty = $row.find('.editable-qty').val() || rowData.qty;
if (parseFloat(qty) === 0 || isNaN(parseFloat(qty))) {
isValid = false;
showToast('warning', "Please input a valid qty for ItemNo# " + itemNo + "!", ' warning', 4000);
$(modalId).modal('hide');
return false;
}
var itemData = {
itemCartId: itemCartId,
ItemNo: itemNo,
qty: qty
};
requestData.push(itemData);
});
if (selectedCheckboxes.length <= 0) {
showToast('warning', 'No selected item!', 'Item cart failed', 4000);
return;
} else {
var DateNeeded = document.getElementById('dateNeeded').value;
if (!DateNeeded) {
showToast('warning', 'Please choose date needed!', 'P.R. submission failed', 4000);
confirmUpdateListener = false;
return;
}
if (RequestTypeId == 3) {
updateDepartmentId();
var ChargeTo = document.getElementById('departmentId').value;
if (!ChargeTo) {
showToast('warning', 'Please choose a department to be in charge of!', 'P.R. submission failed', 4000);
confirmUpdateListener = false;
return;
}
}
}
showConfirmation({
title: 'Purchasing Requisition',
message: 'Are you sure you want to proceed? This action cannot be undone.',
type: 'warning',
confirmText: 'Yes',
cancelText: 'No'
}).then((confirmed) => {
if (confirmed) {
$.ajax({
url: '/ItemMgmt/PostPurchRequest',
type: 'POST',
data: { ItemCartIds: requestData, DateNeeded: DateNeeded, RequestTypeId: RequestTypeId, ChargeTo: ChargeTo, Remarks: Remarks },
success: function (response) {
if (response.success) {
itemTable.ajax.reload();
$('#addDateNeeded').modal('hide');
showToast('success', 'P.R. Successfully Created!', 'Success', 4000);
var totalSelectedLabel = $('#totalSelected');
totalSelectedLabel.text('');
} else {
itemTable.ajax.reload();
showToast('error', response.response, title + ' failed', 4000);
}
},
beforeSend: function () {
loader.show();
},
complete: function () {
loader.hide();
}
});
}
});
}
function AddToCart(isUpdated) {
var loader = $('#overlay, #loader');
var ItemNo = $('#itemNo').val();
var ItemCategoryId = $('#ItemCategory2Id').val();
var ItemColorId = $('#itemColorId').val();
var PackagingTypeId = $('#packagingTypeId').val();
var ItemAttachId = $('#itemAttachId').val();
var UOMId = $('#uomId').val();
var ItemLocalId = $('#itemLocalId').val();
const message = (ItemCategoryId == 29)
? 'This item requires a PNP permit. Please coordinate with our Security Department once the PR has been created.'
: 'Do you want to add this item to your cart? Please note that this action cannot be undone.';
showConfirmation({
title: 'Item Cart',
message: message,
type: 'warning',
confirmText: 'Yes',
cancelText: 'No'
}).then((confirmed) => {
if (confirmed) {
$.ajax({
url: '/ItemMgmt/PostPutItemCart',
type: 'POST',
data: { ItemNo, ItemCategoryId, ItemColorId, PackagingTypeId, ItemAttachId, UOMId, ItemLocalId },
beforeSend: function () {
loader.show();
},
success: function (response) {
if (response.success) {
$('#viewItemDetails').modal('hide');
clearTextModal();
fetchAndRefreshTable();
if (response.cartCount !== undefined) {
const cartCountElement = document.getElementById("cartCount");
if (cartCountElement) {
cartCountElement.textContent = response.cartCount > 0 ? response.cartCount : "0";
}
}
showToast('success', 'The item has been added to your cart successfully.', 'Success', 4000);
} else {
showToast('error', response.response, title + ' failed', 4000);
}
},
error: errorHandler,
complete: function () {
loader.hide();
}
});
}
});
}
function postItem(isUpdated) {
loader = $('#overlay, #loader');
const itemNameInput = document.getElementById('ItemName');
const itemDescriptionInput = document.getElementById('ItemDescription');
const itemCategoryIdInput = document.getElementById('ItemCategoryId');
const itemCategoryInput = document.getElementById('ItemCategory');
itemNameInput.addEventListener('input', removeErrorClass);
itemDescriptionInput.addEventListener('input', removeErrorClass);
itemCategoryIdInput.addEventListener('input', removeErrorClass);
itemCategoryInput.addEventListener('input', removeErrorClass);
const ItemName = itemNameInput.value;
const ItemDescription = itemDescriptionInput.value;
const ItemCategoryId = itemCategoryIdInput.value;
if (!ItemName || !ItemDescription || !ItemCategoryId) {
showToast('warning', 'Please fill the required fields!', 'Item creation failed', 4000);
if (!ItemName) {
itemNameInput.classList.add('error-input');
}
if (!ItemDescription) {
itemDescriptionInput.classList.add('error-input');
}
if (!ItemCategoryId) {
itemCategoryInput.classList.add('error-input');
}
return;
}
showConfirmation({
title: 'Item Creation',
message: 'Are you sure you want to proceed? This action cannot be undone.',
type: 'warning',
confirmText: 'Yes',
cancelText: 'No'
}).then((confirmed) => {
if (confirmed) {
$.ajax({
url: '/ItemMgmt/PostPutItem',
type: 'POST',
data: { ItemName, ItemDescription, ItemCategoryId },
success: function (response) {
if (response.success) {
$('#addNewItem').modal('hide');
if (!isUpdated == 0) {
var data = [];
data.itemCodeId = response.itemCodeId;
viewItem(data);
clearTextModal();
fetchAndRefreshTable();
showToast('success', 'Update successfully!', 'Success', 4000);
} else {
clearTextModal();
fetchAndRefreshTable();
showToast('success', 'Item added successfully!', 'Success', 4000);
}
} else {
loader.hide();
showToast('error', response.response, title + ' failed', 4000);
}
},
beforeSend: function () {
loader.show();
},
complete: function () {
loader.hide();
}
});
}
})
}
function fetchAndRefreshTable() {
$.ajax({
url: '/ItemMgmt/GetItemList',
type: 'GET',
success: function (newResponse) {
if (newResponse && newResponse.data) {
fetchedData = newResponse.data;
itemTable.clear().rows.add(fetchedData).draw();
} else {
console.log('No data returned from server.');
}
},
error: function (xhr, error) {
console.log('Error fetching updated data:', error);
}
});
}