NonInventPurchasingSystem/CPRNIMS.WebApps/wwwroot/JsFunctions/PR/PRViewV5.js
2026-02-12 10:41:44 +08:00

744 lines
25 KiB
JavaScript

function showProjectCode(data = {}, isNew = true) {
const modal = $('#showProjectCode');
if (isNew) {
// Mode
modal.attr('data-mode', 'add');
modal.attr('data-id', '');
// Header & button
$('#headerNew').show();
$('#headerUpdate').hide();
$('#btnAddUpdate')
.text('Add Project')
.removeClass('btn-warning')
.addClass('btn-success');
// Clear fields
$('#itemPostPutForm')[0].reset();
$('#status').val('true');
} else {
modal.attr('data-mode', 'update');
modal.attr('data-id', data.id);
$('#headerNew').hide();
$('#headerUpdate').show();
$('#btnAddUpdate')
.text('Update Project')
.removeClass('btn-success')
.addClass('btn-warning');
// Fill fields
$('#projectCodeId').val(data.projectCodeId);
$('#projectCode').val(data.projectCode);
$('#projectName').val(data.projectName);
$('#deliveryAddress').val(data.deliveryAddress);
$('#maxDays').val(data.maxDays);
$('#statusSwitch').prop('checked', data.isActive);
$('label[for="statusSwitch"]').text(data.isActive ? 'Active' : 'Inactive');
}
$('#maxDays').on('input', function () {
const value = Number(this.value);
$('#btnAddUpdate').prop('disabled', value <= 0);
});
$('#statusSwitch').on('change', function () {
$('label[for="statusSwitch"]').text(
this.checked ? 'Active' : 'Inactive'
);
});
modal.modal('show');
}
function getApproverName(prDetailsId) {
PRDetailsId = prDetailsId;
$.ajax({
url: '/PRMgmt/GetApproverName',
type: 'POST',
data: { PRDetailsId },
success: function (data) {
if (data && data.data && data.data.length > 0) {
var item = data.data[0];
const attestedBy = document.getElementById('label-pr-attestedBy');
const approveBy = document.getElementById('label-pr-approvedBy');
if (attestedBy) {
attestedBy.innerHTML = item.attestedBy;
}
if (approveBy) {
approveBy.innerHTML = item.approvedBy;
}
}
},
error: errorHandler
});
}
function printPRItem() {
const iframe = document.createElement('iframe');
iframe.style.position = 'fixed';
iframe.style.right = '0';
iframe.style.bottom = '0';
iframe.style.width = '0';
iframe.style.height = '0';
iframe.style.border = 'none';
document.body.appendChild(iframe);
const doc = iframe.contentWindow.document;
doc.open();
doc.write('<html><head><title>Print</title>');
const stylesheets = document.querySelectorAll('link[rel="stylesheet"]');
stylesheets.forEach(sheet => {
doc.write(sheet.outerHTML);
});
doc.write(`
<style>
@media print {
.pr-info-section .row {
display: flex !important;
flex-wrap: nowrap !important;
}
.pr-info-section .col-md-4 {
flex: 0 0 33.33% !important;
max-width: 33.33% !important;
}
table {
table-layout: fixed !important;
width: 125% !important;
}
/* Hide the 5th and 6th columns */
/* Hide the 5th and 6th columns */
table th:nth-child(5),
table th:nth-child(6),
table td:nth-child(5),
table td:nth-child(6) {
display: none !important;
}
table th:not(:nth-child(5)):not(:nth-child(6)),
table td:not(:nth-child(5)):not(:nth-child(6)) {
width: auto !important;
}
.page-break {
page-break-before: always;
}
}
body {
font-family: Roman, sans-serif;
font-size: 12px;
margin: 0;
padding: 10px;
}
.header {
font-family: Roman, sans-serif;
font-size: 14px;
font-weight: bold;
text-align: center;
margin-bottom: 5px;
padding: 5px 0;
}
/* Reduce vertical padding inside table rows */
table {
border-collapse: collapse;
font-size: 12px;
font-family: Roman, sans-serif;
}
table th,
table td {
padding: 2px 2px;
line-height: 1.2;
vertical-align: top;
border: .5px solid #808080 !important;
}
.pr-info-section {
font-size: 10px !important;
background: #f8f9fa !important;
border: 1px solid #dee2e6 !important;
padding: 8px !important;
margin-bottom: 10px !important;
}
.pr-info-section label,
.pr-info-section strong,
.pr-info-section .fw-bold {
font-size: 10px !important;
font-weight: bold !important;
}
.pr-info-section .text-muted {
font-size: 9px !important;
color: #666 !important;
}
/* Hide DataTables controls */
.dataTables_length,
.dataTables_filter,
.dataTables_info,
.dataTables_paginate {
display: none !important;
}
</style>
`);
doc.write('</head><body>');
// Add the main header
doc.write('<div class="header">Purchase Requisition Details</div>');
// Get the PR info section HTML
const prInfoSection = document.querySelector('.pr-info-section');
let prInfoHTML = '';
if (prInfoSection) {
prInfoHTML = prInfoSection.outerHTML;
}
// Clone the printable content
const printContent = document.getElementById('printableRelatedItem').cloneNode(true);
// Remove DataTables controls
const dataTableControls = printContent.querySelectorAll('.dataTables_length, .dataTables_filter, .dataTables_info, .dataTables_paginate');
dataTableControls.forEach(control => {
if (control && control.parentNode) {
control.parentNode.removeChild(control);
}
});
// Get the table
const table = printContent.querySelector('table');
if (table) {
const tbody = table.querySelector('tbody');
const rows = Array.from(tbody.querySelectorAll('tr'));
const rowsPerPage = 50; // Reduced to accommodate PR info section
// Create table header HTML
const thead = table.querySelector('thead');
const tableHeaderHTML = thead ? thead.outerHTML : '';
// Split into pages
for (let i = 0; i < rows.length; i += rowsPerPage) {
// Add page break for pages after the first
if (i > 0) {
doc.write('<div class="page-break"></div>');
}
// Add PR info section to every page
doc.write(prInfoHTML);
// Get rows for current page
const currentPageRows = rows.slice(i, i + rowsPerPage);
// Create table for current page
doc.write('<table class="row-border" style="width: 100%;">');
doc.write('<colgroup>');
doc.write('<col style="width:8%" />');
doc.write('<col style="width:30%" />');
doc.write('<col style="width:34%" />');
doc.write('<col style="width:8%" />');
doc.write('<col style="width:8%" />');
doc.write('<col style="width:12%" />');
doc.write('</colgroup>');
// Add table header
doc.write(tableHeaderHTML);
// Add table body with current page rows
doc.write('<tbody>');
currentPageRows.forEach(row => {
doc.write(row.outerHTML);
});
doc.write('</tbody>');
doc.write('</table>');
}
} else {
// Fallback: just add PR info and content
doc.write(prInfoHTML);
doc.write(printContent.innerHTML);
}
// Close the document
doc.write('</body></html>');
doc.close();
// Wait for the iframe to load before printing
iframe.onload = function () {
// Print the iframe content
iframe.contentWindow.print();
// Remove the iframe after printing
setTimeout(function () {
document.body.removeChild(iframe);
}, 1000);
};
}
function viewItemRemovalRemarks(data) {
PRDetailsId = data.prDetailsId;
ItemName = data.itemName;
ItemNo = data.itemNo;
PRNo = document.getElementById('label-pr-prNo').innerHTML;
$('#viewItemRemovalRemarks').modal('show');
$('#viewItemRemovalRemarks').css('z-index', 1060);
}
function rejectItem() {
$('#rejectRemarks').modal('show');
$('#rejectRemarks').css('z-index', 1060);
}
function viewSupplierAlterOfferList(data) {
loader = $('#overlay, #loader');
$('#viewSupplierAlterOfferList').modal('show');
$('#viewSupplierAlterOfferList').css('z-index', 1050);
tableElement = $('#AlterOfferListDataTable');
tableDestroy(tableElement);
const CanvassDetailId = data.canvassDetailId;
$('#canvassDetailId').val(CanvassDetailId);
document.getElementById('label-prNo').innerHTML = data.prNo;
document.getElementById('label-itemNo').innerHTML = data.itemNo;
document.getElementById('label-qty').innerHTML = data.qty;
document.getElementById('label-itemName').innerHTML = data.itemName;
prDataTable = tableElement.DataTable({
ajax: $.extend({
url: endpoint.GetSupplierAlterOfferDetails,
type: 'GET',
data: { CanvassDetailId },
}, beforeComplete(loader)),
language: {
emptyTable: "No record available"
},
initComplete: initCompleteCallback(),
columns: colAlterOfferTable,
responsive: true,
createdRow: function (row, data) {
if (data.isApproved) {
$(row).css({
'color': 'white',
'background-color': 'green'
});
}
},
error: errorHandler
});
}
function viewItemDetail(data) {
var loader = $('#overlay, #loader').css('z-index', 1060);
let itemCategory = document.getElementById('itemCategoryName');
itemCategory.style.display = 'block';
let itemCategorySelect = document.getElementById('itemCategorySelect');
itemCategorySelect.style.display = 'none';
inputPopulation();
ItemCodeId = data.itemCodeId;
PRDetailsId = data.prDetailsId;
$.ajax({
url: '/PRMgmt/GetMyPR',
type: 'POST',
data: { ItemCodeId, PRDetailsId },
beforeSend: function () {
loader.show();
},
complete: function () {
loader.hide();
},
success: function (data) {
if (data && data.data && data.data.length > 0) {
var item = data.data[0];
enableDisableInput(item.queue, item.dynamicBtn);
$('#itemCodeId').val(item.itemCodeId);
$('#itemName').val(item.itemName);
$('#itemDescription').val(item.itemDescription);
$('#department').val(item.department);
$('#itemCategoryName').val(item.itemCategoryName);
$('#ItemCategory2Id').val(item.itemCategoryId);
$('#itemNo').val(item.itemNo);
$('#itemTypeName').val(item.itemTypeName);
$('#itemTypeId').val(item.itemTypeId);
$('#itemClassId').val(item.itemClassId);
$('#uomName').val(item.uomName);
$('#uomId').val(item.uomId);
$('#itemColorName').val(item.itemColorName);
$('#itemColorId').val(item.itemColorId);
$('#createdDate').val(item.createdDate);
$('#itemLocalName').val(item.itemLocalName);
$('#itemLocalId').val(item.itemLocalId);
$('#itemQty').val(item.qty);
$('#itemAttachId').val(item.itemAttachId);
$('#itemRemarks').val(item.remarks);
const statusEl = document.getElementById('statusName');
const status = item.statusName; // 'Approved', 'Denied', 'For Approval'
// set text
statusEl.textContent = status;
// reset classes
statusEl.className = 'status-badge';
// apply based on value
switch (status) {
case 'Approved':
statusEl.classList.add('approved');
hideButtonApproval(true);
break;
case 'Attested':
hideButtonApproval(false);
statusEl.classList.add('approved');
break;
case 'Denied':
hideButtonApproval(false);
statusEl.classList.add('denied');
break;
case 'For Approval':
hideButtonApproval(false);
statusEl.classList.add('pending');
break;
}
var itemPicturePath = item.itemAttachPath;
if (!itemPicturePath || itemPicturePath === 'N/A' || itemPicturePath === 'None') {
$('#itemPictureImage').attr('src', '/Content/Common/empty.jpg');
} else {
var imageUrl = item.url + itemPicturePath;
// Set the image source
$('#itemPictureImage').attr('src', imageUrl);
}
$('#viewPRItemDetails').modal('show');
$('#viewPRItemDetails').css('z-index', 1060);
} else {
window.location.href = '/Home/Logout';
}
},
error: errorHandler
});
}
function hideButtonApproval(isApproved) {
if (['Approver1', 'Approver2', 'CnvssAppver'].includes(UserRights)) {
const btnApprove = document.getElementById('btnApprove');
const btnHold = document.getElementById('btnHold');
const isPurchaser = (UserRights == 'CnvssAppver') ? 'block' : 'none';
if (isApproved) {
btnApprove.style.display = 'none';
btnHold.style.display = isPurchaser;
} else {
btnApprove.style.display = 'block';
btnHold.style.display = 'block';
}
}
}
function viewPRStatusById(data) {
loader = $('#overlay, #loader').css('z-index', 1060);
PRDetailsId = data.prDetailsId;
$.ajax($.extend({
url: '/PRMgmt/GetPRStatusById',
type: 'POST',
data: { PRDetailsId },
success: function (data) {
if (data && data.data && data.data.length > 0) {
var item = data.data[0];
$('#viewPRTracking').on('shown.bs.modal', function () {
updateProgressBar(item);
});
$('#poTypeName').val(item.poTypeName);
$('#poNumber').val(item.poNumber);
$('#prNo').val(item.prNo);
$('#requestedBy').val(item.requestedBy);
$('#quantity').val(item.qty);
$('#t-itemName').val(item.itemName);
$('#t-itemDescription').val(item.itemDescription);
$('#t-itemCategoryName').val(item.itemCategoryName);
$('#t-itemNo').val(item.itemNo);
$('#t-uomName').val(item.uomName);
let formattedDate = formatDate(item.dateNeeded);
$('#t-dateNeeded').val(formattedDate);
var itemPicturePath = item.itemAttachPath;
if (!itemPicturePath || itemPicturePath === 'N/A' || itemPicturePath === 'None') {
$('#t-itemPictureImage').attr('src', '/Content/Common/empty.jpg');
} else {
var imageUrl = item.url + itemPicturePath;
//console.log('imageUrl', imageUrl);
$('#t-itemPictureImage').attr('src', imageUrl);
}
$('#viewPRTracking').modal('show');
$('#viewPRTracking').css('z-index', 1060);
} else {
console.log('Data is null or undefined');
window.location.href = '/Home/Logout';
}
},
error: errorHandler
}, beforeComplete(loader)));
}
function viewRRDetailByPO(data) {
loader = $('#overlay, #loader');
$('#viewRRDetailByPO').modal('show');
$('#viewRRDetailByPO').css('z-index', 1050);
PONo = data.poNo;
POTypeId = data.poTypeId;
let amountUSDLabel = document.getElementById('amountUSDLabel');
let vatRateLabel = document.getElementById('vatRateLabel');
let amountPHPVatLabel = document.getElementById('amountPHPVatLabel');
let amountPHPLabel = document.getElementById('amountPHPLabel');
let vatRate = document.getElementById('vatRate');
let amountPHPVat = document.getElementById('amountPHPVat');
let amountUSD = document.getElementById('amountUSD');
let amountPHP = document.getElementById('amountPHP');
let btnSubmit = document.getElementById('btnSubmit');
let btnReject = document.getElementById('btnReject');
let btnPayment = document.getElementById('btnPayment');
if (POTypeId !== 3) {
amountUSD.style.display = 'none';
amountUSDLabel.style.display = 'none';
}
if (UserRights !== 'LLIFINANCE' && UserRights !== 'CnvssAppver' && UserRights !== 'POApprover') { //dont touch &&
btnSubmit.style.display = 'none';
amountUSDLabel.style.display = 'none';
vatRateLabel.style.display = 'none';
amountPHPVatLabel.style.display = 'none';
amountPHPLabel.style.display = 'none';
amountPHPVat.style.display = 'none';
vatRate.style.display = 'none';
amountPHP.style.display = 'none';
}
let btnComplete = document.getElementById('btnComplete');
let btnIncomplete = document.getElementById('btnIncomplete');
let btnClosePO = document.getElementById('btnClosePO');
btnPayment.style.display = 'none';
if (UserRights == 'POApprover' || UserRights == 'LLIFINANCE' || UserRights == 'CnvssAppver') {
btnComplete.style.display = 'none';
btnIncomplete.style.display = 'none';
btnSubmit.style.display = 'none';
btnReject.style.display = 'none';
dynamicColumn = colRRFinance;
dynamicTable = '#FRRdataTable';
rrTableComponent(1, loader);
} else {
dynamicColumn = colForReceivingDetail;
dynamicTable = '#RRdataTable';
btnClosePO.style.display = 'none';
rrTableComponent(2, loader);
}
}
function inputPopulation() {
// Update hidden input when an item category is selected
$('#ItemCategory').on('change', function () {
var selectedValue = $(this).val();
$('#ItemCategoryId').val(selectedValue);
});
$('#itemCategorySelect').on('change', function () {
var selectedValue2 = $(this).val();
$('#ItemCategory2Id').val(selectedValue2);
});
// Bind the click event to the select element
$('#itemCategoryName').on('click', function () {
let itemCategory = document.getElementById('itemCategoryName');
itemCategory.style.display = 'none';
let itemCategorySelect = document.getElementById('itemCategorySelect');
itemCategorySelect.style.display = 'block';
populateItemCategSelect();
});
$("#itemColorName").on('keyup', function () {
populateItemColor();
});
$("#uomName").on('keyup', function () {
populateItemUOM();
});
$("#itemLocalName").on('keyup', function () {
populateItemLocalization();
});
}
function rrInitializeDatatable(loader) {
tableElement = $(dynamicTable);
tableDestroy(tableElement);
receivingDetailTable = tableElement.DataTable({
ajax: $.extend({
url: '/PRMgmt/GetRRDetailByPO',
type: 'POST',
data: { PONo, POTypeId },
}, beforeComplete(loader)),
searching: false,
responsive: true,
language: {
emptyTable: "No record available"
},
initComplete: initCompleteCallback(),
columns: dynamicColumn,
columnDefs: colDefForReceivingSKU,
rowCallback: rowRRDetailCallback,
error: errorHandler
});
}
function rrTableComponent(id, loader) {
$.ajax({
url: '/PRMgmt/GetRRTable',
type: 'GET',
data: { id: id },
success: function (response) {
$('#PRTableContainer').html(response);
rrInitializeDatatable(loader);
},
error: errorHandler
});
}
function enableDisableInput(isDenied, dynamicBtn) {
let buttonUpdate = document.getElementById('btnUpdateItem');
let btnHold = document.getElementById('btnHold');
let btnApprove = document.getElementById('btnApprove');
if (['Approver1', 'Approver2', 'CnvssAppver'].includes(UserRights)) {
if (UserRights !== 'CnvssAppver') {
btnApprove.style.display = 'block';
} else {
if (dynamicBtn) {
btnHold.style.display = 'none';
}
btnApprove.style.display = 'none';
}
} else {
if (UserRights === 'Requestor') {
btnApprove.style.display = 'none';
btnHold.style.display = 'none';
}
}
if (isDenied != 1 || isDenied != true) {
if (UserRights === 'Requestor') {
buttonUpdate.style.display = 'block';
}
} else {
if (['Approver1', 'Approver2', 'CnvssAppver'].includes(UserRights)) {
buttonUpdate.style.display = 'none';
} else {
buttonUpdate.style.display = 'block';
}
}
// Get all the input and select elements
let inputs = [
document.getElementById('itemName'),
document.getElementById('itemDescription'),
document.getElementById('itemCategoryName'),
document.getElementById('uomName'),
document.getElementById('itemColorName'),
document.getElementById('itemLocalName'),
document.getElementById('itemClassId'),
document.getElementById('prTypeId'),
document.getElementById('itemQty'),//btnUpdateItem
/* document.getElementById('btnUpdateItem'),*/
];
inputs.forEach(function (input) {
if (input) {
input.disabled = (isDenied != 1);
}
});
}
function viewPRDetails(data) {
loader = $('#overlay, #loader').css('z-index', 1060);
$('#viewPRDetails').modal('show');
$('#viewPRDetails').css('z-index', 1050);
tableName = '#PRdataTable';
totalSelectedLabel = $('#totalSelected');
clearTableSelection(tableName, selectedProductsMap, () => {
totalSelectedLabel.text(0);
}, 'selected-row', '.select-all-item-checkbox');
tableElement = $(tableName);
tableDestroy(tableElement);
var PRNo = data.prNo;
document.getElementById('label-pr-prNo').innerHTML = data.prNo;
document.getElementById('label-prby').innerHTML = data.createdBy;
document.getElementById('label-pr-Department').innerHTML = data.department;
document.getElementById('label-pr-remarks').innerHTML = data.remarks;
document.getElementById('label-pr-attestedBy').innerHTML = data.attestedBy;
document.getElementById('label-pr-approvedBy').innerHTML = data.approvedBy;
document.getElementById('label-pr-ProjectCode').innerHTML = data.projectCode;
console.log('data.projectCode', data.projectCode);
document.getElementById('label-pr-dateNeeded').innerHTML = formatDate(data.dateNeeded);
prDataTable = tableElement.DataTable({
ajax: $.extend({
url: '/PRMgmt/GetPRDetailByPRNo',
type: 'POST',
data: { PRNo },
}, beforeComplete(loader)),
language: {
emptyTable: "No record available"
},
initComplete: function () {
initializeTableSelection({
tableName: tableName,
dataTable: prDataTable,
selectedItemsMap: selectedProductsMap,
idKey: 'prDetailsId',
idKey2: 'itemNo',
checkboxClass: '.select-item-checkbox',
selectAllClass: '.select-all-item-checkbox',
selectedRowClass: 'selected-row',
updateCountCallback: function () {
totalSelectedLabel.text(getSelectedCount(selectedProductsMap));
}
});
},
columns: colItemList,
// responsive: true,
rowCallback: rowStatusColorCallback,
error: errorHandler
});
}
function clearTextModal() {
if (UserRights == 'LLISCMAdmin' || UserRights == 'LTReceiver') {
document.getElementById('docTypeId').value = "";
document.getElementById('suppDocNo').value = "";
document.getElementById('remarks').value = "";
}
}
function resetIsApproval() {
isApproval = false;
}