production: excel export fix
This commit is contained in:
@@ -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 () {
|
||||
|
||||
@@ -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'
|
||||
));
|
||||
}
|
||||
@@ -277,6 +277,8 @@
|
||||
</div>
|
||||
|
||||
<div id="2" style="display: none">
|
||||
<br>
|
||||
<input class="button" id="rawMaterialsExcelBtn" value="Excel" type="button">
|
||||
<div id="rawMaterialsTableContainer">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user