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

509 lines
17 KiB
JavaScript

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 !== '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 == 'CnvssAppver') {
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: '/Receiving/GetRRDetailByPO',
type: 'POST',
data: { PONo, POTypeId },
}, beforeComplete(loader)),
searching: false,
language: {
emptyTable: "No record available"
},
initComplete: initCompleteCallback(),
columns: dynamicColumn,
columnDefs: colDefForReceivingSKU,
rowCallback: rowRRDetailCallback,
error: errorHandler
});
}
function rrTableComponent(id, loader) {
$.ajax({
url: '/Receiving/GetRRTable',
type: 'GET',
data: { id: id },
success: function (response) {
$('#PRTableContainer').html(response);
rrInitializeDatatable(loader);
},
error: errorHandler
});
}
function viewRRNo() {
populateRRNo();
$('#viewRRNo').modal('show');
$('#viewRRNo').css('z-index', 1070);
}
//Start viewRRDetail
function viewRRDetail(data) {
$('#viewRRDetail').modal('show');
$('#viewRRDetail').css('z-index', 1070);
const RRNo = data.rrNo;
const POTypeId = data.poTypeId;
// First load the component, then initialize the table
rrComponent(POTypeId, function () {
// This callback will be called after the component is loaded
initializeRRDetailTable(RRNo, POTypeId);
});
}
function rrComponent(id, callback) {
$.ajax({
url: '/Receiving/GetRRBody',
type: 'GET',
data: { id: id },
success: function (response) {
$('#RRContainer').html(response);
if (typeof callback === 'function') {
callback();
}
const btnPrintSIRR = document.getElementById('btnPrintSIRR');
const btnPrintDRRR = document.getElementById('btnPrintDRRR');
if (id == 1) {
btnPrintSIRR.style.display = 'block';
btnPrintDRRR.style.display = 'none';
} else {
btnPrintSIRR.style.display = 'none';
btnPrintDRRR.style.display = 'block';
}
},
error: errorHandler
});
}
function initializeRRDetailTable(RRNo, POTypeId) {
var tableName = '';
if (POTypeId == 1) {
tableName=$('#RRDataTable');
} else {
tableName = $('#DRRRDataTable');
}
tableElement = tableName;
tableDestroy(tableElement);
rrDetailTable = tableElement.DataTable({
ajax: $.extend({
url: '/Receiving/GetRRDetail',
type: 'POST',
data: { RRNo, POTypeId },
}, beforeComplete(loader)),
dom: '<"top"i>rt<"bottom"f><"clear">',
searching: false,
lengthChange: false,
info: false,
language: {
emptyTable: "No record available"
},
initComplete: initCompleteCallback(),
columns: colRRDetail,
rowCallback: rowRRCallback,
error: errorHandler
});
return rrDetailTable;
}
//End viewRRDetail
function populateRRNo() {
loader = $('#overlay, #loader').css('z-index', 1060);
$.ajax($.extend({
url: '/Receiving/GetLatestRRNo',
type: 'GET',
success: function (data) {
if (data && data.data && data.data.length > 0) {
var item = data.data[0];
$('#rrNoSeries').val(item.rrNo);
} else {
console.log('Data is null or undefined');
window.location.href = '/Home/Logout';
}
},
error: errorHandler
}, beforeComplete(loader)));
}
function clearTextRec() {
if (UserRights == 'LLISCMAdmin' || UserRights == 'LTReceiver') {
document.getElementById('docTypeId').value = "";
document.getElementById('suppDocNo').value = "";
document.getElementById('remarks').value = "";
}
}
function printRRSI() {
try {
// Step 1: Prepare the content for printing
// Clone the content from printableSIDR which contains the full document
const contentDiv = document.getElementById('printableSIDR').cloneNode(true);
// Step 2: Clean up any DataTable controls that might affect layout
const dataTableControls = contentDiv.querySelectorAll('.dataTables_length, .dataTables_filter, .dataTables_info, .dataTables_paginate');
dataTableControls.forEach(control => control.parentNode?.removeChild(control));
// Step 3: Create a container with optimized print styles
const container = document.createElement('div');
container.id = 'print-container';
container.innerHTML = contentDiv.innerHTML;
// Step 4: Create style element with precise layout rules
const styleElement = document.createElement('style');
styleElement.textContent = `
/* Base document styles */
html, body {
margin: 0;
padding: 0;
font-family: Roman, sans-serif;
font-size: 11pt;
-webkit-print-color-adjust: exact;
print-color-adjust: exact;
}
/* Main container */
#print-container {
width: 100%;
padding: 10px;
box-sizing: border-box;
page-break-after: always;
}
/* Supplier section */
.rowSupplier {
display: block;
padding-bottom: 5px;
}
/* Reference section */
.rr-reference {
float: right;
margin-right: 30px;
margin-top: 0;
margin-bottom: 70px;
}
th, td {
font-family: Roman, sans-serif;
line-height: normal;
padding: 3px 5px;
height: 20px; /* Ensure minimum row height */
box-sizing: border-box; /* Ensure padding does not overflow */
}
/* Table styles */
#RRDataTable {
width: 100%;
border-collapse: collapse;
margin-top: 180px;
table-layout: fixed;
font-size: 10pt;
clear: both;
}
#RRDataTable th, #RRDataTable td {
padding: 4px;
vertical-align: top;
}
/* Column sizing */
.itemNo { width: 10%; text-align: right; }
.itemDescription { width: 55%; text-align: left; }
.qty { width: 10%; text-align: right; }
.uomName { width: 10%; text-align: center; }
/* Footer section */
.footer-row {
position: fixed !important;
bottom: 3.5in !important;
left: 0;
display: flex !important;
justify-content: center !important;
width: 100%;
z-index: 100 !important;
}
.footer-column {
flex: 0 0 auto;
text-align: center;
min-width: 150px;
}
.footer-span {
display: block;
padding-top: 3px;
text-align: center;
}
/* Hide datatables cruft */
.dataTables_length, .dataTables_filter,
.dataTables_info, .dataTables_paginate {
display: none !important;
}
`;
// Step 5: Create document wrapper
const wrapper = document.createElement('div');
wrapper.appendChild(styleElement);
wrapper.appendChild(container);
// Step 6: Set pdf generation options
const options = {
filename: `RRSI_Document_${new Date().toISOString().slice(0, 19).replace(/[-:T]/g, '')}.pdf`,
image: { type: 'jpeg', quality: 0.98 },
html2canvas: {
scale: 2, // High quality
useCORS: true, // Allow images
scrollY: 0, // No scroll
allowTaint: true, // Allow modified canvas
logging: false, // Reduce console noise
removeContainer: true
},
jsPDF: {
unit: 'mm',
format: 'letter',
orientation: 'portrait'
},
// Modest margin to prevent content being cut off
margin: [10, 10, 10, 10] // [top, right, bottom, left] in mm
};
// Step 7: Generate PDF and display in new window
html2pdf()
.from(wrapper)
.set(options)
.outputPdf('dataurlnewwindow')
.catch(error => {
console.error('PDF generation failed:', error);
alert('Error generating PDF. Please try again.');
});
} catch (error) {
console.error('Error in printRRSI function:', error);
alert('An unexpected error occurred. Please try again.');
}
}
function printRRDR() {
try {
// Step 1: Prepare the content for printing
// Clone the content from printableSIDR which contains the full document
const contentDiv = document.getElementById('printableSIDR').cloneNode(true);
// Step 2: Clean up any DataTable controls that might affect layout
const dataTableControls = contentDiv.querySelectorAll('.dataTables_length, .dataTables_filter, .dataTables_info, .dataTables_paginate');
dataTableControls.forEach(control => control.parentNode?.removeChild(control));
// Step 3: Create a container with optimized print styles
const container = document.createElement('div');
container.id = 'print-container';
container.innerHTML = contentDiv.innerHTML;
// Step 4: Create style element with precise layout rules
const styleElement = document.createElement('style');
styleElement.textContent = `
/* Base document styles */
html, body {
margin: 0;
padding: 0;
font-family: Roman, sans-serif;
font-size: 11pt;
-webkit-print-color-adjust: exact;
print-color-adjust: exact;
}
/* Main container */
#print-container {
width: 100%;
padding: 10px;
box-sizing: border-box;
page-break-after: always;
}
/* Supplier section */
.clientSupplier {
display: flex;
position: fixed;
top: 2.1in;
}
/* Reference section */
.rr-reference {
float: right;
margin-right: 30px;
margin-top: 10px;
margin-bottom: 55px;
}
/* Table styles */
#DRRRDataTable {
width: 100%;
border-collapse: collapse;
table-layout: fixed;
font-size: 10pt;
clear: both;
}
#DRRRDataTable th, #DRRRDataTable td {
padding: 4px;
vertical-align: top;
}
th, td {
font-family: Roman, sans-serif;
line-height: normal !important;
padding: 3px 5px !important;
height: 20px !important;
box-sizing: border-box !important;
}
/* Column sizing */
.itemNo { width: 10%; text-align: right; }
.itemDescription { width: 55%; text-align: left; }
.qty { width: 10%; text-align: right; }
.uomName { width: 10%; text-align: center; }
/* Footer section */
.footer-row {
position: fixed !important;
bottom: 3in !important;
left: 0;
right: 0;
display: flex !important;
justify-content: center !important;
width: 100% !important;
z-index: 100 !important;
}
.footer-column {
flex: 0 0 auto;
text-align: center;
min-width: 150px;
}
.footer-span {
display: block;
padding-top: 3px;
text-align: center;
}
/* Hide datatables cruft */
.dataTables_length, .dataTables_filter,
.dataTables_info, .dataTables_paginate {
display: none !important;
}
`;
// Step 5: Create document wrapper
const wrapper = document.createElement('div');
wrapper.appendChild(styleElement);
wrapper.appendChild(container);
// Step 6: Set pdf generation options
const options = {
filename: `RRSI_Document_${new Date().toISOString().slice(0, 19).replace(/[-:T]/g, '')}.pdf`,
image: { type: 'jpeg', quality: 0.98 },
html2canvas: {
scale: 2, // High quality
useCORS: true, // Allow images
scrollY: 0, // No scroll
allowTaint: true, // Allow modified canvas
logging: false, // Reduce console noise
removeContainer: true
},
jsPDF: {
unit: 'mm',
format: 'letter',
orientation: 'portrait'
},
// Modest margin to prevent content being cut off
margin: [10, 10, 10, 10] // [top, right, bottom, left] in mm
};
// Step 7: Generate PDF and display in new window
html2pdf()
.from(wrapper)
.set(options)
.outputPdf('dataurlnewwindow')
.catch(error => {
console.error('PDF generation failed:', error);
alert('Error generating PDF. Please try again.');
});
} catch (error) {
console.error('Error in printRRSI function:', error);
alert('An unexpected error occurred. Please try again.');
}
}