File manager - Edit - /home/autoph/public_html/projects/tickets-autohub/public/js/authentication/report/TicketMasterfileReport.js
Back
$(document).ready(function() { window.jsPDF = window.jspdf.jsPDF; $('#multiple-select-clear-field-company').select2({ theme: "bootstrap-5", width: $(this).data('width') ? $(this).data('width') : $(this).hasClass('w-100') ? '100%' : 'style', placeholder: $(this).data('placeholder'), closeOnSelect: false, allowClear: true, }); $('#multiple-select-clear-field-dealer').select2({ theme: "bootstrap-5", width: $(this).data('width') ? $(this).data('width') : $(this).hasClass('w-100') ? '100%' : 'style', placeholder: $(this).data('placeholder'), closeOnSelect: false, allowClear: true, }); $("#multiple-select-clear-field-company").on('change', function() { const comArr = $(this).val(); $.ajax({ url: "/roles/fetchDealers/", type: "POST", data: { com_id: comArr }, headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') }, success: function(result) { $('#multiple-select-clear-field-dealer').empty(); $.each(result[0], function(index, dealer) { let optSelected = (result[1].includes(dealer.id) ? 'selected' : ''); $('#multiple-select-clear-field-dealer').append( '<option value="' + dealer.id + '" ' + optSelected + '>' + dealer.dealer_name + '</option>'); }); }, error: function(error) { console.log(error); } }); }); var data_set = []; var date_from = ''; var date_to = ''; var totalRecords = 0; var totalClosedTicket = 0; var totalOngoingTicket = 0; var total_sla = 0; $('.btn-submit').click(function(e) { e.preventDefault(); $('#progrss-loading-bar').css('width', 0 + '%'); $('#progrss-loading-bar').text(0 + '%'); $('#progrss-loading-bar').attr('aria-valuenow', 0); $("#btn-pdf").hide(); $("#btn-table").hide(); data_set = []; let comp_code = $("#multiple-select-clear-field-company").val(); let dealer_code = $("#multiple-select-clear-field-dealer").val(); let from_date = $("#from_date").val(); let to_date = $("#to_date").val(); $.ajax({ url: "/reports/get/tickets_masterfile2?page=1", type: "POST", data: { comp_code: comp_code, dealer_code: dealer_code, from_date: from_date, to_date: to_date }, headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') }, success: function(result) { console.log(result); totalRecords = 0; totalClosedTicket = 0; totalOngoingTicket = 0; total_sla = 0; data_set.push(...result.data); date_from = result.date_from; date_to = result.date_to; totalRecords = result.total_records; totalClosedTicket = result.total_closed_ticket; totalOngoingTicket = result.total_ongoing_ticket; total_sla = result.total_sla_ticket; $('#loading-modal').modal('show'); if (result.total_records == 0 || result.total_page == 1) { $('#progrss-loading-bar').css('width', 100 + '%'); $('#progrss-loading-bar').text(100 + '%'); $("#btn-pdf").show(); $("#btn-table").show(); } if(result.total_page >1){ fetchData(result.total_page,comp_code,dealer_code,from_date,to_date); } // var incPercent = 0; // for (var i = 1; i <= result.total_page; i++) { // if (i != 0 && i != 1) { // $.ajax({ // url: "/reports/get/tickets_masterfile2?page=" + i, // type: "POST", // data: { // comp_code: comp_code, // dealer_code: dealer_code, // from_date: from_date, // to_date: to_date // }, // headers: { // 'X-CSRF-TOKEN': $('meta[name="csrf-token"]') // .attr('content') // }, // success: function(response) { // var loadingPercentage = // computeLoadingPercentage(response // .currentPage, response // .total_records, response.total_page, // response.perPage); // // console.log(loadingPercentage); // incPercent = loadingPercentage; // $('#progrss-loading-bar').attr( // 'aria-valuenow', loadingPercentage // .toFixed(2)); // $('#progrss-loading-bar').css('width', // loadingPercentage.toFixed(2) + '%'); // $('#progrss-loading-bar').text( // loadingPercentage + '%'); // data_set.push(...response.data); // if (loadingPercentage == 100) { // isDone = true; // $("#btn-pdf").show(); // $("#btn-table").show(); // // $("#btn-pdf").prop("disabled", false); // // $("#btn-table").attr('disabled',false); // } // } // }); // } // } }, error: function(xhr, status, error) { // Hide loading indicator // Handle error response console.error(xhr.responseText); alert('Error processing data. Please try again.'); } }); $("#close-moda-btn").on('click', function() { data_set = []; }); }); async function collectData(page,comp_code,dealer_code,from_date,to_date) { return new Promise(function(resolve, reject) { $.ajax({ url: "/reports/get/tickets_masterfile2?page=" + page, type: "POST", data: { comp_code: comp_code, dealer_code: dealer_code, from_date: from_date, to_date: to_date }, headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') }, success: function(response) { resolve(response); }, error: function(xhr, status, error) { reject(error); } }); }); } async function fetchData(totalPages,comp_code,dealer_code,from_date,to_date) { // $("#txt-center-lable").show(); for (var i = 2; i <= totalPages; i++) { if (i != 0 && i > 1) { try { const response = await collectData(i,comp_code,dealer_code,from_date,to_date); var loadingPercentage = computeLoadingPercentage(response.currentPage, response .total_records, response.total_page, response.perPage); incPercent = Math.floor(loadingPercentage); console.log(incPercent); $('#progrss-loading-bar').attr('aria-valuenow', incPercent); $('#progrss-loading-bar').css('width', incPercent+ '%'); $('#progrss-loading-bar').text(incPercent + '%'); data_set.push(...response.data); if (loadingPercentage == 100) { isDone = true; $("#btn-pdf").show(); $("#btn-table").show(); // $("#txt-center-lable").hide(); } } catch (error) { console.error(error); } } } } function computeLoadingPercentage(currentPage, totalRows, totalPageCount, rowsPerPage) { totalPageCount = Math.ceil(totalRows / rowsPerPage); const loadedPageCount = Math.min(currentPage, totalPageCount); return (loadedPageCount / totalPageCount) * 100; } $("#btn-pdf").on('click', function() { generatePDFtable(data_set); }); $("#btn-table").on('click', function() { generateExcel(data_set); }); // //WORKING IN LOCAL // function generatePDFtable(data) { // var doc = new jsPDF({ // orientation: 'landscape', // Set orientation to landscape // // unit: 'mm', // Set unit to millimeters // format: 'a4' // Set format to A4 // }); // var imgWidth = 40; // var imgHeight = 30; // // Add the image to the PDF // doc.addImage("{{ asset('files/img/ahg_logo.png') }}", 'PNG', 240, 1, imgWidth, imgHeight); // var addFooter = function() { // var totalPages = doc.internal.getNumberOfPages(); // doc.setFontSize(10); // for (var i = 1; i <= totalPages; i++) { // doc.setPage(i); // if (i === totalPages) { // doc.text('Page ' + i + ' of ' + totalPages, doc.internal.pageSize.getWidth() - 160, doc.internal.pageSize.getHeight() - 8); // } // else // { // doc.text('Page ' + i + ' of ' + totalPages, doc.internal.pageSize.getWidth() - 160, doc.internal.pageSize.getHeight() - 3); // } // } // }; // var startXtitle = 10; // doc.setFont('sans serif', 'bold'); // doc.setFontSize(20); // doc.text('Ticket Masterfile Report',startXtitle, 10); // doc.setFont('sans serif', 'normal') // doc.setFontSize(10); // const setDateFrom = new Date(date_from); // const DateFrom_formattedDateTime = setDateFrom.toLocaleString('en-US', { // // weekday: 'long', // year: 'numeric', // month: 'long', // day: 'numeric', // // hour: 'numeric', // // minute: 'numeric', // // second: 'numeric', // // hour12: true // Include AM/PM indicator // }); // const setDateTo = new Date(date_to); // const DateTo_formattedDateTime = setDateTo.toLocaleString('en-US', { // // weekday: 'long', // year: 'numeric', // month: 'long', // day: 'numeric', // }); // doc.text('Date from '+DateFrom_formattedDateTime +' to '+DateTo_formattedDateTime,startXtitle, 16); // doc.text('Prepared by :',startXtitle, 19); // doc.setFont('sans serif', 'italic'); // doc.text(employee_name,30, 19); // doc.setFont('sans serif', 'normal') // doc.text('Prepared date :',startXtitle, 22); // doc.setFont('sans serif', 'italic'); // let now = new Date(); // const formattedDateTime = now.toLocaleString('en-US', { // weekday: 'long', // year: 'numeric', // month: 'long', // day: 'numeric', // hour: 'numeric', // minute: 'numeric', // second: 'numeric', // hour12: true // Include AM/PM indicator // }); // doc.text(formattedDateTime,32, 22); // doc.setFont('sans serif', 'normal'); // doc.text('Total Records :',startXtitle, 26); // doc.setFont('sans serif', 'italic'); // doc.text(totalRecords.toString(),32, 26); // var startY = 28; // var startX = 10; // var lineHeight = 8; // var cellWidth = 20; // var cellHeight =10; // var headers = [ // 'No.', // 'Ticket No', // 'Sender', // 'Company', // 'Dealer', // 'Category', // 'Sub Category', // 'Date created', // 'Date started', // 'Date ended', // 'Support Personel', // 'Position', // 'SLA', // 'Status' // ]; // // Define the widths of each column // var cellWidths = [12, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20]; // Adjust the width of the first column // doc.setFont('helvetica', 'bold'); // doc.setFillColor(200, 200, 200); // Light gray background color // // Calculate the total width of the headers // var totalHeaderWidth = cellWidths.reduce((acc, curr) => acc + curr, 0); // // Draw background rectangle for the header row // doc.rect(startX, startY, totalHeaderWidth, cellHeight, 'F'); // doc.setFontSize(8); // // Draw headers with adjusted column widths // var currentX = startX; // Track the current X position // headers.forEach(function(header, i) { // var splitValue = doc.splitTextToSize(header, cellWidths[i] - 7); // Adjust width as needed // doc.text(currentX + cellWidths[i] / 2, startY + cellHeight / 2, splitValue, null, null, 'center'); // currentX += cellWidths[i]; // Move to the next column // }); // // Draw data rows with adjusted column widths // var startY = 23; // doc.setFont('helvetica', 'normal'); // var lineHeight = 15; // Adjusted line height // doc.setFillColor(255, 255, 255); // Reset background color // cellHeight = 10; // doc.setFontSize(7); // data.forEach(function(item, rowIndex) { // startY += lineHeight; // if (startY > doc.internal.pageSize.height - 20) { // doc.addPage(); // Add a new page if startY exceeds the height // startY = 20; // Reset startY for the new page // } // var currentX = startX; // Track the current X position // Object.values(item).forEach(function(value, colIndex) { // var splitValue = doc.splitTextToSize(value, cellWidths[colIndex] - 5); // Adjust width as needed // splitValue = (splitValue == 'null' || splitValue == 'null' || splitValue == 'NULL' ? '' : splitValue); // doc.rect(currentX, startY, cellWidths[colIndex], 15); // doc.text(currentX + cellWidths[colIndex] / 2, startY + cellHeight / 2, splitValue, null, null, 'center'); // currentX += cellWidths[colIndex]; // Move to the next column // }); // }); // addFooter(); // var pdfData = doc.output('datauristring'); // window.open(pdfData); // } function generatePDFtable(data) { console.log(data); var doc = new jsPDF({ orientation: 'landscape', format: 'a4' }); var imgWidth = 40; var imgHeight = 30; // doc.addImage("{{ asset('files/img/ahg_logo.png') }}", 'PNG', 240, 1, imgWidth, imgHeight); doc.addImage(logo, 'PNG', 240, 1, imgWidth, imgHeight); var addFooter = function() { var totalPages = doc.internal.getNumberOfPages(); doc.setFontSize(10); for (var i = 1; i <= totalPages; i++) { doc.setPage(i); if (i === totalPages) { doc.text('Page ' + i + ' of ' + totalPages, doc.internal.pageSize.getWidth() - 160, doc.internal.pageSize.getHeight() - 8); } else { doc.text('Page ' + i + ' of ' + totalPages, doc.internal.pageSize.getWidth() - 160, doc.internal.pageSize.getHeight() - 3); } } }; var startXtitle = 10; doc.setFont('sans serif', 'bold'); doc.setFontSize(20); doc.text('Ticket Masterfile Report', startXtitle, 10); doc.setFont('sans serif', 'normal') doc.setFontSize(8); const setDateFrom = new Date(date_from); const DateFrom_formattedDateTime = setDateFrom.toLocaleString('en-US', { // weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', // hour: 'numeric', // minute: 'numeric', // second: 'numeric', // hour12: true // Include AM/PM indicator }); const setDateTo = new Date(date_to); const DateTo_formattedDateTime = setDateTo.toLocaleString('en-US', { // weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', }); doc.text('Date range:', startXtitle, 15); doc.text(DateFrom_formattedDateTime + ' to ' + DateTo_formattedDateTime, 28, 15); doc.setFont('sans serif', 'normal') doc.text('Closed :', 90, 15); doc.setFont('sans serif', 'italic'); doc.text(totalClosedTicket.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","), 107, 15); // doc.setFont('sans serif', 'normal') // doc.text('Prepared by :',startXtitle, 19); // doc.setFont('sans serif', 'italic'); // doc.text(employee_name,28, 19); doc.setFont('sans serif', 'normal') doc.text('Prepared date :', startXtitle, 19); doc.setFont('sans serif', 'italic'); let now = new Date(); const formattedDateTime = now.toLocaleString('en-US', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric', hour12: true // Include AM/PM indicator }); doc.text(formattedDateTime, 28, 19); doc.setFont('sans serif', 'normal') doc.text('Ongoing :', 90, 19); doc.setFont('sans serif', 'italic'); doc.text(totalOngoingTicket.toString(), 107, 19); doc.setFont('sans serif', 'normal') doc.text('Total SLA :', 90, 23); doc.setFont('sans serif', 'italic'); // doc.setFont(undefined, 'bold'); set italic to bold doc.text((total_sla / 60).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + ' hrs.', 107, 23); doc.setFont('sans serif', 'normal') doc.text('Prepared by :', startXtitle, 23); doc.setFont('sans serif', 'italic'); doc.text(employee_name, 28, 23); doc.setFont('sans serif', 'normal'); doc.text('Total Records :', startXtitle, 27); doc.setFont('sans serif', 'italic'); doc.text(totalRecords.toString(), 28, 27); doc.setFontSize(10); var startY = 28; var startX = 10; var lineHeight = 8; var cellWidth = 20; var cellHeight = 10; var headers = [ 'No.', 'Ticket No', 'Sender', 'Company', 'Dealer', 'Category', 'Sub Category', 'Date created', 'Date started', 'Date ended', 'Support Personel', 'Position', 'SLA', 'Status' ]; // Define the widths of each column var cellWidths = [12, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20 ]; // Adjust the width of the first column doc.setFont('helvetica', 'bold'); doc.setFillColor(200, 200, 200); // Light gray background color // Calculate the total width of the headers var totalHeaderWidth = cellWidths.reduce((acc, curr) => acc + curr, 0); // Draw background rectangle for the header row doc.rect(startX, startY, totalHeaderWidth, cellHeight, 'F'); doc.setFontSize(8); // Draw headers with adjusted column widths var currentX = startX; // Track the current X position headers.forEach(function(header, i) { var splitValue = doc.splitTextToSize(header, cellWidths[i] - 7); // Adjust width as needed doc.text(currentX + cellWidths[i] / 2, startY + cellHeight / 2, splitValue, null, null, 'center'); currentX += cellWidths[i]; // Move to the next column }); // Draw data rows with adjusted column widths var startY = 23; doc.setFont('helvetica', 'normal'); var lineHeight = 15; // Adjusted line height doc.setFillColor(255, 255, 255); // Reset background color cellHeight = 10; doc.setFontSize(7); var counter = 1; // Counter variable for the first column data.forEach(function(item, rowIndex) { startY += lineHeight; if (startY > doc.internal.pageSize.height - 20) { doc.addPage(); // Add a new page if startY exceeds the height startY = 20; // Reset startY for the new page } var currentX = startX; // Track the current X position doc.text(14, startY + 11 / 2, counter.toString(), null, null, 'center'); counter++; // Increment the counter Object.values(item).forEach(function(value, colIndex) { var splitValue = doc.splitTextToSize(value, cellWidths[colIndex] - 5); // Adjust width as needed splitValue = (splitValue == 'null' || splitValue == 'null' || splitValue == 'NULL' ? '' : splitValue); doc.rect(currentX, startY, cellWidths[colIndex], 15); doc.text(currentX + cellWidths[colIndex] / 2, startY + cellHeight / 2, splitValue, null, null, 'center'); currentX += cellWidths[colIndex]; // Move to the next column }); }); addFooter(); var pdfData = doc.output('datauristring'); // window.open(pdfData); // var link = document.createElement('a'); // link.href = pdfData; // link.target = '_blank'; // Open link in new tab // link.innerHTML = 'Open PDF'; // Link text // document.body.appendChild(link); // Append link to the body // link.click(); // Simulate click event to open the PDF var pdfData = doc.output('blob'); var blobUrl = URL.createObjectURL(pdfData); window.open(blobUrl); } function generateExcel(data) { var headers = [ 'Ticket No', 'Sender', 'Company', 'Dealer', 'Category', 'Sub Category', 'Date created', 'Date started', 'Date ended', 'Support Personel', 'Position', 'SLA', 'Status' ]; var dataArray = []; // Convert data to array of arrays var dataArray = [headers]; data.forEach(function(item) { var row = []; headers.forEach(function(header) { var key = header.toLowerCase().replace(/\s/g, '_'); // var value = String(item[key]); // Convert value to string var value = item[key] == null ? '' : String(item[key]); row.push(value); // row.push(item[header.toLowerCase().replace(/\s/g, '_')]); // Adjusted to match keys in data }); dataArray.push(row); }); // Create a worksheet var worksheet = XLSX.utils.aoa_to_sheet(dataArray); // Create a workbook var workbook = XLSX.utils.book_new(); XLSX.utils.book_append_sheet(workbook, worksheet, 'Sheet1'); // Convert the workbook to a binary Excel file var excelFile = XLSX.write(workbook, { bookType: 'xlsx', type: 'binary' }); // Convert the binary Excel data to a Blob var blob = new Blob([s2ab(excelFile)], { type: 'application/octet-stream' }); // Create a download link var url = window.URL.createObjectURL(blob); var a = document.createElement('a'); a.href = url; const dateTime = new Date().toLocaleString(); a.download = 'Ticket Masterfile Report ' + dateTime + '.xlsx'; // Trigger a click event to download the file a.click(); window.URL.revokeObjectURL(url); } // Helper function to convert string to array buffer function s2ab(s) { var buf = new ArrayBuffer(s.length); var view = new Uint8Array(buf); for (var i = 0; i < s.length; i++) view[i] = s.charCodeAt(i) & 0xFF; return buf; } }); const form = document.getElementById('tcktsForm'); const rbt = document.getElementsByName('type'); let radioButtonsChecked = false; form.addEventListener('submit', function(e) { e.preventDefault(); for (let i = 0; i < rbt.length; i++) { if (rbt[i].checked) { radioButtonsChecked = true; break; } } if (!radioButtonsChecked) { swal.fire({ icon: 'error', title: 'Oops...', text: 'Kindly choose which type of report you want to generate', }); } else { form.submit(); } });
| ver. 1.4 |
.
| PHP 8.1.32 | Generation time: 0.01 |
proxy
|
phpinfo
|
Settings