production: excel export fix

This commit is contained in:
Michał Zieliński
2025-10-02 12:07:25 +02:00
parent fc4268def0
commit aae6334793
3 changed files with 179 additions and 13 deletions

View File

@@ -106,6 +106,7 @@ $(document).ready(function () {
$("#deleteBtn").click(() => { removePositions(); }); $("#deleteBtn").click(() => { removePositions(); });
$("#excelBtn").click(() => { exportExcel(); }); $("#excelBtn").click(() => { exportExcel(); });
$("#pdfBtn").click(() => { exportPDF(); }); $("#pdfBtn").click(() => { exportPDF(); });
$("#rawMaterialsExcelBtn").click(() => { exportRawMaterialsExcel(); });
$("#productionBtn").click(() => { window.alert("In progress."); }); $("#productionBtn").click(() => { window.alert("In progress."); });
$("#createInsideOrder").click(createInsideOrder); $("#createInsideOrder").click(createInsideOrder);
$(document).on('change', '.allCheck', function() { $(document).on('change', '.allCheck', function() {
@@ -348,24 +349,74 @@ function hideLoader() {
$.unblockUI(); $.unblockUI();
} }
function exportExcel() { function exportExcel() {
var ids = []; // Check if any rows are selected
$("input.allCheck:checkbox:checked").each(function () { var checkedRows = $("input.allCheck:checkbox:checked").closest('tr:visible');
ids.push($(this).val()); if (checkedRows.length === 0) {
}); window.alert("Wybierz pozycje do eksportu");
if (ids.length === 0) { return;
$("input.allCheck:checkbox").each(function () {
ids.push($(this).val());
});
} }
// 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..."); showLoader("Generowanie pliku...");
$.ajax({ $.ajax({
method: "get", method: "post",
url: $(location).attr("href") + "&to_pdf=1&ajaxAction=exportExcel&ids=" + ids.join("|"), url: $(location).attr("href") + "&to_pdf=1&ajaxAction=exportExcelWYSIWYG",
success: function (response) data: {
{ tableData: JSON.stringify(tableData)
downloadFile(response.fileContent, response.fileName, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); },
success: function (result) {
downloadFile(result.fileContent, result.fileName, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
hideLoader(); hideLoader();
}, },
error: function() {
window.alert("Błąd podczas generowania pliku");
hideLoader();
}
}); });
} }
function exportPDF() { 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() { function createInsideOrder() {
var ids = []; var ids = [];
$('input.allCheck:checkbox:checked').each(function () { $('input.allCheck:checkbox:checked').each(function () {

View File

@@ -44,6 +44,9 @@ if (!isset($_GET['ajaxAction'])) {
case 'exportExcel': case 'exportExcel':
exportExcel($_GET['ids']); exportExcel($_GET['ids']);
break; break;
case 'exportExcelWYSIWYG':
exportExcelWYSIWYG($_POST['tableData']);
break;
case 'exportPDF': case 'exportPDF':
exportPDF($_GET['ids']); exportPDF($_GET['ids']);
break; break;
@@ -466,4 +469,39 @@ function generateUuidV4()
$data[6] = chr(ord($data[6]) & 0x0f | 0x40); $data[6] = chr(ord($data[6]) & 0x0f | 0x40);
$data[8] = chr(ord($data[8]) & 0x3f | 0x80); $data[8] = chr(ord($data[8]) & 0x3f | 0x80);
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4)); 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'
));
} }

View File

@@ -277,6 +277,8 @@
</div> </div>
<div id="2" style="display: none"> <div id="2" style="display: none">
<br>
<input class="button" id="rawMaterialsExcelBtn" value="Excel" type="button">
<div id="rawMaterialsTableContainer"> <div id="rawMaterialsTableContainer">
</div> </div>
</div> </div>