File manager - Edit - /home/autoph/public_html/data03252025consolidation/dist/js/ltms/report_sales_ltms.js
Back
$(document).ready(function(){ let data_set_sales = []; // Global data set let totalDataCount = 0; // Total number of records to fetch // Function to disable buttons function disableButtons() { $('#generate-btn-sales').prop('disabled', true); $('#btn-close-modal-sales').prop('disabled', true); $('#export-sales-excel').prop('disabled', true); $('#export-sales-pdf').prop('disabled', true); } // Function to enable buttons function enableButtons() { $('#generate-btn-sales').prop('disabled', false); $('#btn-close-modal-sales').prop('disabled', false); $('#export-sales-excel').prop('disabled', false); $('#export-sales-pdf').prop('disabled', false); } // Function to fetch data in batches async function fetchData(offset) { try { const response = await $.ajax({ url: "api/ltms_report/ltms_sales_report.php", type: "post", dataType: "json", data: { apiKey: "11113487258290", cid : $("#company_sales_report").val(), did : $("#dealer_sales_report").val(), date_from_sales: $("#date_from_ltms_sales").val(), date_to_sales: $("#date_to_ltms_sales").val(), ltms_status_sales: $("#ltms-filter-sales").val(), offset: offset, limit: 50 // Limit for each request } }); if (offset === 0) { totalDataCount = response.total_data; } data_set_sales = data_set_sales.concat(response.results); const progressPercentage = Math.min((data_set_sales.length / totalDataCount) * 100, 100); $('#data-fetch-progress-sales').css('width', progressPercentage + '%').attr('aria-valuenow', progressPercentage).text(Math.round(progressPercentage) + '%'); if (response.results.length === 50) { await fetchData(offset + 50); } else { enableButtons(); } } catch (error) { console.error("Error fetching data:", error); enableButtons(); } } $("#generate-btn-sales").on('click', function() { data_set_sales = []; disableButtons(); fetchData(0); }); document.getElementById('export-sales-pdf').addEventListener('click', function () { if (data_set_sales.length === 0) { alert("No data available to export."); return; } // var { jsPDF } = window.jspdf; window.jsPDF = window.jspdf.jsPDF; var doc = new jsPDF({ orientation: 'portrait', format: 'a4' }); doc.setFontSize(10); const title = "Sales LTMS "; const titleWidth = doc.getStringUnitWidth(title) * doc.internal.getFontSize() / doc.internal.scaleFactor; const xTitle = (doc.internal.pageSize.getWidth() - titleWidth) / 2; doc.text(title, xTitle, 15); // Add numbering column header const headers = [ "No.", "Dealer", "Name", "Type", "CS Number", "Date Released", "Mobile", "W/ LTMS", ]; let yPos = 25; const xHeader = 10; const columnWidths = [10,25, 34, 20, 27, 30,26,18]; // Adjust column widths // Set fill color for the header background doc.setFillColor(9, 151, 0); // RGB for blue // Draw the headers with background and borders doc.setFont("helvetica", "bold"); // Loop through the headers to draw the background first and then the text doc.rect(xHeader, yPos - 4, doc.internal.pageSize.getWidth() - (xHeader*2), 10, 'FD'); headers.forEach((header, index) => { let xPos = xHeader + columnWidths.slice(0, index).reduce((a, b) => a + b, 0); // Calculate x position based on previous column widths // Draw the background rectangle for each column with its own width // 'FD' means fill and draw (border) // Set text color to white for the header text doc.setTextColor(255, 255, 255); // Add header text (centered vertically within the cell) doc.text(header, xPos + 2, yPos + 2); }); // Reset text color to black for the content rows doc.setTextColor(0, 0, 0); yPos += 10; let counter = 1; doc.setFontSize(8); // Render table rows data_set_sales.forEach((item) => { let dealerName = item.dealer_name || ""; let splitdealerName = doc.splitTextToSize(dealerName, columnWidths[1] - 7); let customerName = item.first_name + ' ' + item.middle_name + ' ' + item.last_name || ""; let corporationName = item.corporation_name || ""; let splitCorporationName = doc.splitTextToSize(corporationName, columnWidths[2] - 7); let splitCustomerName = doc.splitTextToSize(customerName, columnWidths[2] - 7); // Adjust to fit width let csNumber = item.cs_number || ""; let splitCsNumber = doc.splitTextToSize(csNumber, columnWidths[4] - 7); // let salesDate = formatDate(item.activity_date) || ""; // let splitActivityDate = doc.splitTextToSize(salesDate, columnWidths[4] - 5); // Determine which name to use based on type let displayedName = item.type == 1 ? splitCustomerName : splitCorporationName; let maxRowHeight = Math.max(displayedName.length, splitCsNumber.length) * 7; // Draw cell borders let currentX = 10; // Starting X position for columns // Column 1: Row numbering doc.rect(currentX, yPos - 4, columnWidths[0], maxRowHeight); // Draw border doc.text(counter.toString(), currentX + 2, yPos); // Add some padding for text currentX += columnWidths[0]; doc.setFontSize(8); doc.rect(currentX, yPos - 4, columnWidths[1], maxRowHeight); // Draw border doc.text(splitdealerName, currentX + 2, yPos); currentX += columnWidths[1]; // Column 2: Customer or Corporation Name doc.rect(currentX, yPos - 4, columnWidths[2], maxRowHeight); // Draw border doc.text(displayedName, currentX + 2, yPos); currentX += columnWidths[2]; // Column 3: Type doc.rect(currentX, yPos - 4, columnWidths[3], maxRowHeight); // Draw border doc.text(item.type == 1 ? "Individual" : "Corporation", currentX + 2, yPos); currentX += columnWidths[3]; // Column 4: CS Number doc.rect(currentX, yPos - 4, columnWidths[4], maxRowHeight); // Draw border doc.text(splitCsNumber, currentX + 2, yPos); currentX += columnWidths[4]; // Column 5: Date Released doc.setFontSize(8); doc.rect(currentX, yPos - 4, columnWidths[5], maxRowHeight); // Draw border doc.text(formatDate(item.activity_date) || "", currentX + 2, yPos); currentX += columnWidths[5]; // doc.setFontSize(11); // Column 6: Mobile doc.rect(currentX, yPos - 4, columnWidths[6], maxRowHeight); // Draw border doc.text(item.mobile_phone_1, currentX + 2, yPos); currentX += columnWidths[6]; doc.rect(currentX, yPos - 4, columnWidths[7], maxRowHeight); // Draw border doc.text(item.has_tag_ltms == 1 ? "Uploaded" : "No LTMS", currentX + 2, yPos); // Adjust yPos based on the tallest column yPos += maxRowHeight; // Add new page if necessary if (yPos > 270) { doc.addPage(); yPos = 10; } // Increment counter counter++; }); // Add total row at the end yPos += 10; doc.setFont("helvetica", "bold"); doc.text("Total", 10, yPos); doc.text(counter - 1 + " entries", 10 + columnWidths[0] + columnWidths[1], yPos); // Display the total count of rows // Add page numbering to each page const totalPages = doc.internal.getNumberOfPages(); for (let i = 1; i <= totalPages; i++) { doc.setPage(i); doc.text(`Page ${i} of ${totalPages}`, 180, 290); } // Save the PDF doc.save('LTMS_Sales_Report.pdf'); }); document.getElementById('export-sales-excel').addEventListener('click', function () { // Check if data_set has data if (data_set_sales.length === 0) { alert("No data available to export."); return; } // Convert data_set to a format suitable for Excel const formattedData = data_set_sales.map(item => ({ 'COMPANY' : item.company_name, 'DEALER' : item.company_code +' - '+item.dealer_name, 'NAME': (item.type ==1 ? item.first_name + ' ' + item.middle_name + ' ' + item.last_name : item.corporation_name), 'MOBILE' : item.mobile_phone_1, 'TYPE' : (item.type ==1 ? "Individual" : "Corporation"), 'CS NUMBER': item.cs_number, // Ensure the property names are correct 'DATE RELEASED': formatDate(item.activity_date), 'LTMS STATUS' : (item.has_tag_ltms ==1 ? "Uploaded" : "No LTMS") })); // Log the formatted data to verify the structure before export console.log(formattedData); // Create a new workbook const wb = XLSX.utils.book_new(); // Convert the formatted data to a worksheet const ws = XLSX.utils.json_to_sheet(formattedData); // Append the worksheet to the workbook XLSX.utils.book_append_sheet(wb, ws, 'Customer Data'); // Export as Excel file XLSX.writeFile(wb, 'sales_customer_data.xlsx'); }); function formatDate(date) { const parsedDate = new Date(date); if (isNaN(parsedDate)) { return ""; } const formattedDate = parsedDate.toLocaleString('en-US', { year: 'numeric', month: 'long', // Full month name (e.g., "October") day: 'numeric', // hour: 'numeric', // minute: 'numeric', // second: 'numeric', // hour12: true // Enables AM/PM }); return formattedDate; } });
| ver. 1.4 |
.
| PHP 7.3.33 | Generation time: 0.06 |
proxy
|
phpinfo
|
Settings