289 lines
11 KiB
JavaScript
289 lines
11 KiB
JavaScript
function updateDepartmentId() {
|
|
var chargeToElement = document.getElementById('chargeTo');
|
|
if (!chargeToElement) {
|
|
console.error('chargeTo element not found');
|
|
return;
|
|
}
|
|
var selectedOption = chargeToElement.options[chargeToElement.selectedIndex];
|
|
if (!selectedOption) {
|
|
console.error('No option selected');
|
|
return;
|
|
}
|
|
document.getElementById('departmentId').value = selectedOption.value;
|
|
document.getElementById('chargeTo').addEventListener('change', updateDepartmentId);
|
|
}
|
|
function renderItembtns(data, row) {
|
|
var jsonData = JSON.stringify(row).replace(/"/g, """);
|
|
var buttonsHtml = '';
|
|
// var statusNumber = parseInt(row.status, 10);
|
|
buttonsHtml += '<button onclick="deleteCartItemById(' + jsonData + ')" class="btn btn-default">' +
|
|
'<i class="fa-solid fa-trash-can fa-xl" style="color: #Ff0000;" aria-hidden="true"></i>' +
|
|
'</button > ';
|
|
|
|
return buttonsHtml;
|
|
}
|
|
function showHideLabelButtons() {
|
|
if (RequestTypeId == 1) {
|
|
let labelnormalPR = document.getElementById('normalPR');
|
|
let specialPR = document.getElementById('specialPR');
|
|
let chargeTo = document.getElementById('chargeTo');
|
|
let labelChargeTo = document.getElementById('labelChargeTo');
|
|
|
|
labelnormalPR.style.display = 'none';
|
|
specialPR.style.display = 'none';
|
|
chargeTo.style.display = 'none';
|
|
labelChargeTo.style.display = 'none';
|
|
} else if (RequestTypeId == 2){
|
|
let specialPR = document.getElementById('specialPR');
|
|
let internalRequest = document.getElementById('internalRequest');
|
|
let chargeTo = document.getElementById('chargeTo');
|
|
let labelChargeTo = document.getElementById('labelChargeTo');
|
|
|
|
internalRequest.style.display = 'none';
|
|
specialPR.style.display = 'none';
|
|
chargeTo.style.display = 'none';
|
|
labelChargeTo.style.display = 'none';
|
|
} else {
|
|
let labelnormalPR = document.getElementById('normalPR');
|
|
let internalRequest = document.getElementById('internalRequest');
|
|
|
|
labelnormalPR.style.display = 'none';
|
|
internalRequest.style.display = 'none';
|
|
}
|
|
}
|
|
$(document).ready(function () {
|
|
loader = $('#overlay, #loader');
|
|
var submitButton = $('#btnSubmitItem');
|
|
|
|
tableName = '#ItemCartTable';
|
|
|
|
totalSelectedLabel = $('#totalSelected');
|
|
|
|
clearTableSelection(tableName, selectedProductsMap, () => {
|
|
totalSelectedLabel.text(0);
|
|
}, 'selected-row', '.select-all-item-checkbox');
|
|
|
|
tableElement = $(tableName);
|
|
tableDestroy(tableElement);
|
|
|
|
UserRights = document.getElementById("roleRights").value;
|
|
RequestTypeId = document.getElementById("requestTypeId").value;
|
|
showHideLabelButtons();
|
|
itemTable = $(tableName).DataTable({
|
|
ajax: {
|
|
url: '/ItemMgmt/GetItemCart',
|
|
type: 'GET',
|
|
data: { RequestTypeId },
|
|
beforeSend: function () {
|
|
loader.show();
|
|
},
|
|
complete: function () {
|
|
loader.hide();
|
|
}
|
|
},
|
|
initComplete: function () {
|
|
initializeTableSelection({
|
|
tableName: tableName,
|
|
dataTable: itemTable,
|
|
selectedItemsMap: selectedProductsMap,
|
|
idKey: 'itemCartId',
|
|
idKey2: 'itemNo',
|
|
checkboxClass: '.select-item-checkbox',
|
|
selectAllClass: '.select-all-item-checkbox',
|
|
selectedRowClass: 'selected-row',
|
|
updateCountCallback: function () {
|
|
totalSelectedLabel.text(getSelectedCount(selectedProductsMap));
|
|
}
|
|
});
|
|
var api = this.api();
|
|
var data = api.ajax.json();
|
|
|
|
if (!data || !data.data || data.data === "No Data") {
|
|
// Display the "No record available" message
|
|
$('.dataTables_empty').html("No record available");
|
|
}
|
|
updateSubmitButtonVisibility();
|
|
},
|
|
columns: [
|
|
{
|
|
data: 'itemCartId',
|
|
title: '<input type="checkbox" class="select-all-item-checkbox" />',
|
|
render: function () {
|
|
return '<input type="checkbox" class="select-item-checkbox"/>';
|
|
},
|
|
orderable: false,
|
|
searchable: false
|
|
},
|
|
{ data: 'itemNo' },
|
|
{ data: 'itemName' },
|
|
{ data: 'itemCategoryName' },
|
|
{ data: 'prTypeId' },
|
|
{ data: 'qty' },
|
|
{
|
|
data: null,
|
|
render: function (data, type, row) {
|
|
return renderItembtns(data, row);
|
|
}
|
|
}
|
|
],
|
|
|
|
"columnDefs": [
|
|
{
|
|
"targets": [5], // Index of the 'qty' column (0-based)
|
|
"render": function (data, type, row) {
|
|
// Render the 'qty' column as an input field
|
|
return '<input type="number" class="editable-qty" style="width:60px;" value="' + data + '" />';
|
|
}
|
|
}
|
|
],
|
|
rowCallback: function (row, data) {
|
|
var cartItemCount = data.cartItemCount; // Retrieve the cart item count from the data object
|
|
var itemCount = parseInt(cartItemCount, 10); // Parse the text content as an integer
|
|
|
|
var prTypeIdCell = $('td:eq(4)', row);
|
|
var PRTypeId = prTypeIdCell.text();
|
|
var PRTypeIdNumer = parseInt(PRTypeId, 10);
|
|
// console.log('ItemCartIds', ItemCartId);
|
|
// Check if the parsed integer is a valid number
|
|
if (!isNaN(itemCount)) {
|
|
$('#cartCount').text(itemCount); // Update the cart count in the navbar
|
|
} else {
|
|
$('#cartCount').text('0'); // If the parsed integer is NaN, set the cart count to 0
|
|
}
|
|
if (!isNaN(PRTypeIdNumer)) {
|
|
if (PRTypeIdNumer === 1) {
|
|
prTypeIdCell.text('Goods').addClass('next-facilitator');
|
|
} else {
|
|
prTypeIdCell.text('Service').addClass('next-facilitator');
|
|
}
|
|
}
|
|
},
|
|
//responsive: true,
|
|
order: [[0, 'desc']],
|
|
language: {
|
|
emptyTable: "No record available"
|
|
},
|
|
error: function (xhr, error, thrown) {
|
|
console.log('DataTables error:', error);
|
|
console.log('Status:', Status);
|
|
console.log('Details:', xhr.responseText);
|
|
window.location.href = '/Home/Logout';
|
|
}
|
|
});
|
|
// Function to update the visibility of the submit button
|
|
function updateSubmitButtonVisibility() {
|
|
var isEmpty = itemTable.data().length === 0; // Check if the table is empty
|
|
|
|
// Toggle the visibility of the submit button based on whether the table is empty or not
|
|
submitButton.toggle(!isEmpty);
|
|
}
|
|
})
|
|
function deleteCartItemById(data) {
|
|
loader = $('#overlay, #loader');
|
|
|
|
var ItemCartId = data.itemCartId;
|
|
var ItemNo = data.itemNo;
|
|
|
|
showConfirmation({
|
|
title: 'Item Cart Deletion',
|
|
message: 'Are you sure you want to delete this item? This action cannot be undone.',
|
|
type: 'danger',
|
|
confirmText: 'Yes, Delete',
|
|
cancelText: 'Keep Item'
|
|
}).then((confirmed) => {
|
|
if (confirmed) {
|
|
$.ajax({
|
|
url: '/ItemMgmt/PostPutItemCart',
|
|
type: 'POST',
|
|
data: { ItemNo, ItemCartId },
|
|
success: function (response) {
|
|
if (response.success) {
|
|
itemTable.ajax.reload();
|
|
var totalSelectedLabel = $('#totalSelected');
|
|
totalSelectedLabel.text('');
|
|
showToast('success', 'Selected item deleted!', 'Success', 4000);
|
|
} else {
|
|
loader.hide();
|
|
showToast('error', response.response, title + ' failed', 4000);
|
|
}
|
|
},
|
|
beforeSend: function () {
|
|
loader.show();
|
|
},
|
|
complete: function () {
|
|
loader.hide();
|
|
}
|
|
});
|
|
}
|
|
});
|
|
}
|
|
document.getElementById('fileAttachment').addEventListener('change', function (e) {
|
|
const file = e.target.files[0];
|
|
const filePreview = document.getElementById('filePreview');
|
|
const fileName = document.getElementById('fileName');
|
|
const fileSize = document.getElementById('fileSize');
|
|
|
|
if (file) {
|
|
// Validate file size (5MB max)
|
|
const maxSize = 5 * 1024 * 1024; // 5MB in bytes
|
|
if (file.size > maxSize) {
|
|
showToast('warning', 'File size exceeds 5MB. Please choose a smaller file.', 'File Upload', 4000);
|
|
e.target.value = '';
|
|
return;
|
|
}
|
|
|
|
// Validate file type
|
|
const allowedExtensions = ['csv', 'xlsx', 'xls', 'pdf'];
|
|
const fileExtension = file.name.split('.').pop().toLowerCase();
|
|
if (!allowedExtensions.includes(fileExtension)) {
|
|
showToast('warning', 'Invalid file type. Please upload CSV, Excel, or PDF files only.', 'File Upload', 4000);
|
|
e.target.value = '';
|
|
return;
|
|
}
|
|
|
|
// Show file preview
|
|
fileName.textContent = file.name;
|
|
fileSize.textContent = formatFileSize(file.size);
|
|
filePreview.classList.remove('d-none');
|
|
}
|
|
});
|
|
function clearFormData() {
|
|
document.getElementById('dateNeeded').value = '';
|
|
document.getElementById('projectCode').value = '';
|
|
document.getElementById('requestorRemarks').value = '';
|
|
document.getElementById('fileAttachment').value = '';
|
|
document.getElementById('chargeTo').value = '';
|
|
clearFileAttachment();
|
|
document.getElementById('charCount').textContent = '0';
|
|
}
|
|
function clearFileAttachment() {
|
|
document.getElementById('fileAttachment').value = '';
|
|
document.getElementById('filePreview').classList.add('d-none');
|
|
}
|
|
document.getElementById('requestorRemarks').addEventListener('input', function (e) {
|
|
const charCount = document.getElementById('charCount');
|
|
const currentLength = e.target.value.length;
|
|
const maxLength = 500;
|
|
|
|
charCount.textContent = currentLength;
|
|
|
|
if (currentLength > maxLength) {
|
|
e.target.value = e.target.value.substring(0, maxLength);
|
|
charCount.textContent = maxLength;
|
|
}
|
|
|
|
// Change color when approaching limit
|
|
if (currentLength > maxLength * 0.9) {
|
|
charCount.classList.add('text-danger');
|
|
} else {
|
|
charCount.classList.remove('text-danger');
|
|
}
|
|
});
|
|
function formatFileSize(bytes) {
|
|
if (bytes === 0) return '0 Bytes';
|
|
const k = 1024;
|
|
const sizes = ['Bytes', 'KB', 'MB'];
|
|
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
return Math.round(bytes / Math.pow(k, i) * 100) / 100 + ' ' + sizes[i];
|
|
}
|