File manager - Edit - /home/autoph/public_html/projects/tickets-autohub/public/js/authentication/report/AnnualSupportSummaryReport.js
Back
$(document).ready(function() { window.jsPDF = window.jspdf.jsPDF; var data_set = []; var date_from = ''; var date_to = ''; var totalRecords = 0; var company_name =''; var dealer_name =''; var department_name =''; var year = $("#year").val(); $("#year").on("change", function() { year = $(this).val(); }); // 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 company = $("#multiple-select-clear-field-company").val(); let dealer = $("#multiple-select-clear-field-dealer").val(); $.ajax({ url: "/reports/get/annual-support-count-summary?page=1", type: "POST", data: { year : year, company : company, dealer : dealer, department_id : $("#department_id").val() }, headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') }, success: function(result) { console.log(result); totalRecords = 0; // totalClosedTicket = 0; // totalOngoingTicket = 0; total_count = 0; data_set.push(...result.data); date_from = result.date_from; date_to = result.date_to; totalRecords = result.total_records; company_name = result.company_name; dealer_name = result.dealer_name; department_name = result.department_name; total_count = result.total_sla_ticket_count; $('#loading-modal-annual').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,from_date,to_date,ticket_status); // } }, 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,from_date,to_date,ticket_status) { return new Promise(function(resolve, reject) { $.ajax({ url: "/reports/get/annual-support-count-summary?page=" + page, type: "POST", data: { from_date: from_date, to_date: to_date, status: ticket_status }, 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,from_date,to_date,ticket_status) { for (var i = 2; i <= totalPages; i++) { console.log('i...'); console.log(i); console.log('totalPages '+totalPages); if (i != 0 && i > 1) { try { const response = await collectData(i,from_date,to_date,ticket_status); 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); console.log('loading percent '+loadingPercentage ) if (incPercent == 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); }); function generatePDFtable(data) { console.log(data); var doc = new jsPDF({ orientation: 'portrait', format: 'a4' }); console.log(data); var margin = 10; var pageSize = doc.internal.pageSize; var pageWidth = pageSize.width; var pageHeight = pageSize.height; var contentWidth = pageWidth - margin * 2; var startY = 33; // Starting Y position var cellHeight = 10; // Height for table cells var imgWidth = 30; var imgHeight = 20; var imgY = 10; doc.addImage(logo, 'PNG', pageWidth - 40, imgY, imgWidth, imgHeight); // Footer function var addFooter = function() { var totalPages = doc.internal.getNumberOfPages(); doc.setFontSize(6); for (var i = 1; i <= totalPages; i++) { doc.setPage(i); var textY = doc.internal.pageSize.getHeight() - 8; doc.text('Page ' + i + ' of ' + totalPages, pageWidth / 2, textY, 'center'); } }; var startXtitle = 10; doc.setFont('sans serif', 'bold'); doc.setFontSize(10); doc.text(company_name, pageWidth /2, startXtitle,'center'); doc.text(dealer_name, pageWidth /2, startXtitle + 5,'center'); doc.text(department_name, pageWidth /2, startXtitle + 10,'center'); doc.text('Support for the Year of '+year, pageWidth /2, startXtitle + 15,'center'); doc.setFont('sans serif', 'normal') doc.setFontSize(7); 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('Period covered', startXtitle, 24); // doc.text(':', 28, 24); // doc.text(DateFrom_formattedDateTime + ' to ' + DateTo_formattedDateTime, 30, 24); doc.setFont('sans serif', 'normal') doc.text('Prepared date', startXtitle, 28); doc.text(':', 28, 28); 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, 30, 28); doc.setFont('sans serif', 'normal') doc.text('Prepared by', startXtitle, 32); doc.text(':', 28, 32); doc.setFont('sans serif', 'italic'); doc.text(employee_name, 30, 32); // Define column widths based on the number of items var numColumns = data.length + 1; // plus 1 for last column total var colWidth = contentWidth / numColumns; // Draw the category headers (Network, System, Hardware, etc.) doc.setFont('helvetica', 'bold'); var currentX = margin; doc.setFillColor(200, 200, 200); // Light gray background doc.rect(currentX , startY+1, contentWidth, cellHeight -1, 'F'); // Fill the rectangle data.forEach(function(item) { var title = item.title || 'N/A'; // Default to 'N/A' if title is undefined var splitTitle = doc.splitTextToSize(title,colWidth - 4); // doc.rect(currentX, startY, colWidth, cellHeight); // Draw cell // doc.text(currentX + colWidth / 2, startY + cellHeight / 2 + 2, title, null, null, 'center'); // Center the text // doc.rect(currentX, startY, colWidth, cellHeight); var lineHeight = 4; var textY = startY + (cellHeight / 2) - ((splitTitle.length - 1) * lineHeight / 2); // Centering adjustment // Loop through each line in splitTitle and place it at a calculated Y position splitTitle.forEach(function(line, index) { doc.text(currentX + colWidth / 2, 2+ textY + (index * 5), line, null, null, 'center'); // Adjust line height with index * 5 }); currentX += colWidth; }); // doc.setFillColor(200, 200, 200); // Light gray background for TOTAL header // doc.rect(currentX, startY, colWidth, cellHeight, 'F'); // doc.rect(currentX, startY, colWidth, cellHeight); doc.text(currentX + colWidth / 2, startY + cellHeight / 2 + 2, 'TOTAL', null, null, 'center'); // Move to the next row for counts startY += cellHeight; // Draw the counts underneath the headers doc.setFont('helvetica', 'normal'); var totalCount = 0; currentX = margin; // Reset X position data.forEach(function(item) { var count = item.total_count !== undefined ? parseInt(item.total_count, 10) : 0; // Check for NaN to prevent invalid values if (isNaN(count)) { count = 0; // Default to 0 if the number is invalid } totalCount += count; // Draw cell with count doc.rect(currentX, startY, colWidth, cellHeight); doc.text(currentX + colWidth / 2, startY + cellHeight / 2 + 2, count.toString(), null, null, 'center'); // Convert count to string currentX += colWidth; }); // TOTAL VALUE // Ensure totalCount is an integer totalCount = Math.round(totalCount); // Safeguard against floating point var formattedTotal = totalCount.toString(); // Convert totalCount to string currentX += colWidth; doc.rect(currentX - colWidth, startY, colWidth, cellHeight); doc.text(currentX - colWidth / 2, startY + cellHeight / 2 + 2, formattedTotal, null, null, 'center'); // Add footer addFooter(); // Generate and open the PDF var pdfData = doc.output('blob'); var blobUrl = URL.createObjectURL(pdfData); window.open(blobUrl); } function generateExcel(data) { const workbook = XLSX.utils.book_new(); const worksheetData = []; // Header and subheader worksheetData.push([company_name]); worksheetData.push([dealer_name]); worksheetData.push([department_name]); worksheetData.push(['Support for the Year of ' + year]); worksheetData.push([]); // Prepared date and prepared by let now = new Date(); const formattedDateTime = now.toLocaleString('en-US', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }); worksheetData.push(['Prepared date:', formattedDateTime]); worksheetData.push(['Prepared by:', employee_name]); worksheetData.push([]); // Headers for each data column and a TOTAL column const headers = data.map(item => item.title || 'N/A'); headers.push('TOTAL'); worksheetData.push(headers); // Add counts row under headers const counts = data.map(item => parseInt(item.total_count, 10) || 0); const totalCount = counts.reduce((acc, count) => acc + count, 0); counts.push(totalCount); // Add total to the last column worksheetData.push(counts); // Create worksheet and set column widths const worksheet = XLSX.utils.aoa_to_sheet(worksheetData); worksheet['!cols'] = headers.map(() => ({ wpx: 70 })); // Set column width to 100px // Add footer (not dynamic like PDF but added as separate rows) const totalPages = 1; // Excel will typically fit everything on one sheet worksheetData.push([]); worksheetData.push([`Page 1 of ${totalPages}`]); // Append worksheet to workbook XLSX.utils.book_append_sheet(workbook, worksheet, 'Support Report'); // Export Excel file XLSX.writeFile(workbook, 'annual support for ther year of'+year+'.xlsx'); } });
| ver. 1.4 |
.
| PHP 8.1.32 | Generation time: 0.01 |
proxy
|
phpinfo
|
Settings