From aae6334793b5f37af93c182bb585c6fbfbec643b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Zieli=C5=84ski?= Date: Thu, 2 Oct 2025 12:07:25 +0200 Subject: [PATCH] production: excel export fix --- .../productionSchedule/productionSchedule.js | 152 ++++++++++++++++-- .../productionSchedule/productionSchedule.php | 38 +++++ .../productionSchedule/productionSchedule.tpl | 2 + 3 files changed, 179 insertions(+), 13 deletions(-) diff --git a/modules/EcmReports/BimIT-Reports/productionSchedule/productionSchedule.js b/modules/EcmReports/BimIT-Reports/productionSchedule/productionSchedule.js index 114a8b04..f737f7d2 100644 --- a/modules/EcmReports/BimIT-Reports/productionSchedule/productionSchedule.js +++ b/modules/EcmReports/BimIT-Reports/productionSchedule/productionSchedule.js @@ -106,6 +106,7 @@ $(document).ready(function () { $("#deleteBtn").click(() => { removePositions(); }); $("#excelBtn").click(() => { exportExcel(); }); $("#pdfBtn").click(() => { exportPDF(); }); + $("#rawMaterialsExcelBtn").click(() => { exportRawMaterialsExcel(); }); $("#productionBtn").click(() => { window.alert("In progress."); }); $("#createInsideOrder").click(createInsideOrder); $(document).on('change', '.allCheck', function() { @@ -348,24 +349,74 @@ function hideLoader() { $.unblockUI(); } function exportExcel() { - var ids = []; - $("input.allCheck:checkbox:checked").each(function () { - ids.push($(this).val()); - }); - if (ids.length === 0) { - $("input.allCheck:checkbox").each(function () { - ids.push($(this).val()); - }); + // Check if any rows are selected + var checkedRows = $("input.allCheck:checkbox:checked").closest('tr:visible'); + if (checkedRows.length === 0) { + window.alert("Wybierz pozycje do eksportu"); + return; } + + // Get table data from selected rows only + var tableData = []; + + // Get headers (skip first checkbox column) + var headers = []; + $("#allTable thead tr th").each(function(index) { + if (index > 0) { + headers.push($(this).text().trim()); + } + }); + tableData.push(headers); + + // Get data from checked and visible rows + checkedRows.each(function() { + var rowData = []; + var $row = $(this); + + $row.find('td').each(function(index) { + if (index > 0) { // Skip first checkbox column + var $cell = $(this); + var cellValue = ''; + + // Check for input fields (like production date) + var $input = $cell.find('input[type="text"], input[type="number"]'); + if ($input.length > 0) { + cellValue = $input.val() || ''; + } else { + // Check for editable divs (like qty, description) + var $editableDiv = $cell.find('div[id^="qty-"], div[id^="description-"]'); + if ($editableDiv.length > 0) { + cellValue = $editableDiv.text().trim(); + } else { + // Regular cell text + cellValue = $cell.text().trim(); + } + } + + rowData.push(cellValue); + } + }); + + if (rowData.length > 0) { + tableData.push(rowData); + } + }); + showLoader("Generowanie pliku..."); $.ajax({ - method: "get", - url: $(location).attr("href") + "&to_pdf=1&ajaxAction=exportExcel&ids=" + ids.join("|"), - success: function (response) - { - downloadFile(response.fileContent, response.fileName, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); + method: "post", + url: $(location).attr("href") + "&to_pdf=1&ajaxAction=exportExcelWYSIWYG", + data: { + tableData: JSON.stringify(tableData) + }, + success: function (result) { + downloadFile(result.fileContent, result.fileName, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); hideLoader(); }, + error: function() { + window.alert("Błąd podczas generowania pliku"); + hideLoader(); + } }); } function exportPDF() { @@ -389,6 +440,81 @@ function exportPDF() { }, }); } +function exportRawMaterialsExcel() { + // Check if raw materials table exists + if ($("#rawMaterialsTable").length === 0) { + window.alert("Brak tabeli surowców do eksportu"); + return; + } + + // Check if any rows are selected + var checkedRows = $("input.rawMaterialCheck:checkbox:checked").closest('tr:visible'); + if (checkedRows.length === 0) { + // If no rows selected, export all visible rows + checkedRows = $("#rawMaterialsTable tbody tr:visible"); + if (checkedRows.length === 0) { + window.alert("Brak danych do eksportu"); + return; + } + } + + // Get table data from selected rows + var tableData = []; + + // Get headers (skip first checkbox column) + var headers = []; + $("#rawMaterialsTable thead tr th").each(function(index) { + if (index > 0) { + headers.push($(this).text().trim()); + } + }); + tableData.push(headers); + + // Get data from selected/visible rows + checkedRows.each(function() { + var rowData = []; + var $row = $(this); + + $row.find('td').each(function(index) { + if (index > 0) { // Skip first checkbox column + var $cell = $(this); + var cellValue = ''; + + // Check for links (product codes) + var $link = $cell.find('a'); + if ($link.length > 0) { + cellValue = $link.text().trim(); + } else { + // Regular cell text + cellValue = $cell.text().trim(); + } + + rowData.push(cellValue); + } + }); + + if (rowData.length > 0) { + tableData.push(rowData); + } + }); + + showLoader("Generowanie pliku..."); + $.ajax({ + method: "post", + url: $(location).attr("href") + "&to_pdf=1&ajaxAction=exportExcelWYSIWYG", + data: { + tableData: JSON.stringify(tableData) + }, + success: function (result) { + downloadFile(result.fileContent, result.fileName, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); + hideLoader(); + }, + error: function() { + window.alert("Błąd podczas generowania pliku"); + hideLoader(); + } + }); +} function createInsideOrder() { var ids = []; $('input.allCheck:checkbox:checked').each(function () { diff --git a/modules/EcmReports/BimIT-Reports/productionSchedule/productionSchedule.php b/modules/EcmReports/BimIT-Reports/productionSchedule/productionSchedule.php index e3fd60d0..347097b7 100644 --- a/modules/EcmReports/BimIT-Reports/productionSchedule/productionSchedule.php +++ b/modules/EcmReports/BimIT-Reports/productionSchedule/productionSchedule.php @@ -44,6 +44,9 @@ if (!isset($_GET['ajaxAction'])) { case 'exportExcel': exportExcel($_GET['ids']); break; + case 'exportExcelWYSIWYG': + exportExcelWYSIWYG($_POST['tableData']); + break; case 'exportPDF': exportPDF($_GET['ids']); break; @@ -466,4 +469,39 @@ function generateUuidV4() $data[6] = chr(ord($data[6]) & 0x0f | 0x40); $data[8] = chr(ord($data[8]) & 0x3f | 0x80); return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4)); +} +function exportExcelWYSIWYG($tableDataJson) +{ + if (function_exists('from_html')) { + $tableDataJson = from_html($tableDataJson); + } + $json = getJSONobj(); + $rawData = $json->decode($tableDataJson); + + if (isset($rawData['jsonObject']) && is_array($rawData['jsonObject'])) { + $tableData = $rawData['jsonObject']; + } else { + $tableData = $rawData; + } + + if (empty($tableData) || !is_array($tableData)) { + header('Content-Type: application/json'); + echo json_encode(array( + 'success' => false, + 'error' => 'Failed to decode table data. Type: ' . gettype($tableData) + )); + return; + } + + require_once 'modules/EcmReports/BimIT-Reports/lib/xlsxGenerator.php'; + + $xlsx = Shuchkin\SimpleXLSXGen::fromArray($tableData); + $xlsx_content = (string)$xlsx; + + header('Content-Type: application/json'); + echo json_encode(array( + 'success' => true, + 'fileContent' => base64_encode($xlsx_content), + 'fileName' => 'harmonogram_produkcji.xlsx' + )); } \ No newline at end of file diff --git a/modules/EcmReports/BimIT-Reports/productionSchedule/productionSchedule.tpl b/modules/EcmReports/BimIT-Reports/productionSchedule/productionSchedule.tpl index 04238824..d9a3c552 100644 --- a/modules/EcmReports/BimIT-Reports/productionSchedule/productionSchedule.tpl +++ b/modules/EcmReports/BimIT-Reports/productionSchedule/productionSchedule.tpl @@ -277,6 +277,8 @@