backend/src/main/webapp/BOMImport.html aktualisiert

This commit is contained in:
2026-10-13 11:02:06 +02:00
parent 3ed6fcff0d
commit dbb1916544

View File

@@ -1,138 +1,380 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="de"> <html lang="de">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover"> <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<title>Excel BOM Import</title> <title>Excel BOM Import</title>
<script src="/backend/xlsx/xlsx.full.min.js"></script>
<script src="/backend/handsontable/handsontable/dist/handsontable.full.min.js"></script> <!-- Excel-Verarbeitung -->
<link rel="stylesheet" href="/backend/handsontable/handsontable/dist/handsontable.full.min.css"> <script src="/backend/xlsx/xlsx.full.min.js"></script>
<!-- Tabulator: freie MIT-lizenzierte Datentabelle -->
<link rel="stylesheet" href="/backend/tabulator/css/tabulator.min.css">
<script src="/backend/tabulator/js/tabulator.min.js"></script>
<link rel="stylesheet" href="/backend/ios.css"> <link rel="stylesheet" href="/backend/ios.css">
<style>
body { <style>
font-family: Arial, sans-serif; * {
text-align: center; box-sizing: border-box;
background-color: #f4f4f4; }
padding: 20px;
} body {
.container { margin: 0;
background: white; padding: 20px;
padding: 20px; font-family: Arial, sans-serif;
border-radius: 10px; text-align: center;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); background-color: #f4f4f4;
display: inline-block; }
}
button { .container {
margin: 10px; display: inline-block;
padding: 10px 20px; width: min(1400px, 100%);
border: none; padding: 20px;
background-color: red; background: white;
color: white; border-radius: 10px;
border-radius: 5px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
cursor: pointer; }
}
#import-table { .button-area {
margin-top: 20px; display: flex;
} flex-wrap: wrap;
</style> justify-content: center;
</head> align-items: center;
<body> gap: 10px;
<div class="container"> }
<h2>Excel Export & Import</h2>
<button id="export-button">BOM Excel Export</button> button {
<input type="file" id="file-input" accept=".xlsx"> margin: 10px 0;
<button id="send-button">Daten Senden</button> padding: 10px 20px;
<div id="import-table"></div> border: none;
</div> color: white;
background-color: red;
<script> border-radius: 5px;
document.addEventListener("DOMContentLoaded", function () { cursor: pointer;
let importedData = []; }
let hot; // Handsontable instance
const url = sessionStorage.getItem("url") ; button:hover:not(:disabled) {
const stationNumber = sessionStorage.getItem("stationNumber"); background-color: #c90000;
}
function exportExcel() {
const headers = ["PRODUCT", "BOM_VERSION_ERP","Part_NO","COMP_NAME","POS_TYPE","ALTERNATIVE","WS_ERP", "LAYER","QTY","UNIT","SETUP","PRODUKT", "PROCESS_GROUP"]; button:disabled {
const worksheet = XLSX.utils.aoa_to_sheet([headers]); background-color: #999;
const workbook = XLSX.utils.book_new(); cursor: not-allowed;
XLSX.utils.book_append_sheet(workbook, worksheet, "BOM"); }
XLSX.writeFile(workbook, "BOMExport.xlsx");
} input[type="file"] {
max-width: 100%;
function importExcelData(event) { }
const fileInput = document.getElementById("file-input");
if (!fileInput.files.length) return; #import-table {
width: 100%;
const reader = new FileReader(); margin-top: 20px;
reader.onload = function(event) { text-align: left;
const workbook = XLSX.read(event.target.result, { type: "binary" }); }
const sheet = workbook.Sheets[workbook.SheetNames[0]];
importedData = XLSX.utils.sheet_to_json(sheet, { header: 1 }).slice(1); #status-message {
min-height: 22px;
if (hot) { margin-top: 12px;
hot.loadData(importedData); // Update existing Handsontable font-size: 14px;
} else { }
const container = document.getElementById("import-table");
hot = new Handsontable(container, { .status-success {
data: importedData, color: #087a20;
colHeaders:["MATERIAL_NO", "BOM_VERSION_ERP", "COMPONENT_NO","COMP_NAME","POS_TYPE","ALTERNATIVE","WS_ERP", "LAYER","QTY","UNIT","SETUP_FLAG","PRODUKT", "PROCESS_GROUP"], }
rowHeaders: true,
width: '100%', .status-error {
height: 300, color: #b00020;
stretchH: 'all', }
autoColumnSize: false,
manualColumnResize: true, .status-info {
colWidths: 110, color: #333;
wordWrap: false, }
licenseKey: 'non-commercial-and-evaluation'
.tabulator {
border: 1px solid #ccc;
font-size: 13px;
}
.tabulator .tabulator-header,
.tabulator .tabulator-header .tabulator-col {
background-color: #ededed;
}
.tabulator-cell.tabulator-editing {
border: 2px solid red !important;
}
@media (max-width: 700px) {
body {
padding: 10px;
}
.container {
padding: 12px;
}
.button-area {
flex-direction: column;
}
button,
input[type="file"] {
width: 100%;
}
}
</style>
</head>
<body>
<div class="container">
<h2>Excel Export &amp; Import</h2>
<div class="button-area">
<button id="export-button" type="button">BOM Excel Export</button>
<input type="file" id="file-input" accept=".xlsx,.xls">
<button id="send-button" type="button" disabled>Daten senden</button>
</div>
<div id="status-message" class="status-info" role="status" aria-live="polite"></div>
<div id="import-table"></div>
</div>
<script>
document.addEventListener("DOMContentLoaded", function () {
let table = null;
const backendUrl = sessionStorage.getItem("url");
const stationNumber = sessionStorage.getItem("stationNumber");
const fileInput = document.getElementById("file-input");
const sendButton = document.getElementById("send-button");
const statusMessage = document.getElementById("status-message");
const columnDefinitions = [
{ title: "MATERIAL_NO", field: "MATERIAL_NO", editor: "input", minWidth: 130 },
{ title: "BOM_VERSION_ERP", field: "BOM_VERSION_ERP", editor: "input", minWidth: 150 },
{ title: "COMPONENT_NO", field: "COMPONENT_NO", editor: "input", minWidth: 140 },
{ title: "COMP_NAME", field: "COMP_NAME", editor: "input", minWidth: 180 },
{ title: "POS_TYPE", field: "POS_TYPE", editor: "input", minWidth: 100 },
{ title: "ALTERNATIVE", field: "ALTERNATIVE", editor: "input", minWidth: 110 },
{ title: "WS_ERP", field: "WS_ERP", editor: "input", minWidth: 100 },
{ title: "LAYER", field: "LAYER", editor: "input", minWidth: 90 },
{ title: "QTY", field: "QTY", editor: "input", minWidth: 90 },
{ title: "UNIT", field: "UNIT", editor: "input", minWidth: 90 },
{ title: "SETUP_FLAG", field: "SETUP_FLAG", editor: "input", minWidth: 110 },
{ title: "PRODUKT", field: "PRODUKT", editor: "input", minWidth: 120 },
{ title: "PROCESS_GROUP", field: "PROCESS_GROUP", editor: "input", minWidth: 140 }
];
function setStatus(message, type = "info") {
statusMessage.textContent = message;
statusMessage.className = "status-" + type;
}
function valueToString(value) {
if (value === undefined || value === null) {
return "";
}
return String(value).trim();
}
function exportExcel() {
const headers = columnDefinitions.map(column => column.field);
const worksheet = XLSX.utils.aoa_to_sheet([headers]);
const workbook = XLSX.utils.book_new();
worksheet["!cols"] = headers.map(header => ({
wch: Math.max(header.length + 2, 15)
}));
XLSX.utils.book_append_sheet(workbook, worksheet, "BOM");
XLSX.writeFile(workbook, "BOMExport.xlsx");
}
function convertRowsToObjects(rows) {
return rows
.filter(row => Array.isArray(row) && row.some(cell => valueToString(cell) !== ""))
.map((row, index) => ({
id: index + 1,
MATERIAL_NO: valueToString(row[0]),
BOM_VERSION_ERP: valueToString(row[1]),
COMPONENT_NO: valueToString(row[2]),
COMP_NAME: valueToString(row[3]),
POS_TYPE: valueToString(row[4]),
ALTERNATIVE: valueToString(row[5]),
WS_ERP: valueToString(row[6]),
LAYER: valueToString(row[7]),
QTY: valueToString(row[8]),
UNIT: valueToString(row[9]),
SETUP_FLAG: valueToString(row[10]),
PRODUKT: valueToString(row[11]),
PROCESS_GROUP: valueToString(row[12])
}));
}
async function createOrUpdateTable(data) {
if (table) {
await table.replaceData(data);
return;
}
table = new Tabulator("#import-table", {
data: data,
index: "id",
height: "300px",
layout: "fitDataStretch",
movableColumns: true,
resizableColumnFit: true,
placeholder: "Keine BOM-Daten importiert",
rowHeader: {
formatter: "rownum",
headerSort: false,
hozAlign: "center",
frozen: true,
width: 50
},
columns: columnDefinitions
});
}
function importExcelData() {
const file = fileInput.files[0];
if (!file) {
setStatus("Bitte zuerst eine Excel-Datei auswählen.", "error");
sendButton.disabled = true;
return;
}
sendButton.disabled = true;
setStatus("Excel-Datei wird eingelesen ...", "info");
const reader = new FileReader();
reader.onload = async function (event) {
try {
const workbook = XLSX.read(event.target.result, {
type: "array",
cellDates: false
});
if (!workbook.SheetNames.length) {
throw new Error("Die Excel-Datei enthält kein Arbeitsblatt.");
}
const sheet = workbook.Sheets[workbook.SheetNames[0]];
const rows = XLSX.utils.sheet_to_json(sheet, {
header: 1,
defval: "",
raw: false
});
if (rows.length <= 1) {
throw new Error("Die Excel-Datei enthält keine BOM-Daten.");
}
const importedData = convertRowsToObjects(rows.slice(1));
if (!importedData.length) {
throw new Error("Es wurden keine gefüllten Datenzeilen gefunden.");
}
await createOrUpdateTable(importedData);
sendButton.disabled = false;
setStatus(importedData.length + " BOM-Zeile(n) erfolgreich importiert.", "success");
} catch (error) {
console.error("Fehler beim Excel-Import:", error);
sendButton.disabled = true;
setStatus("Fehler beim Excel-Import: " + error.message, "error");
}
};
reader.onerror = function () {
sendButton.disabled = true;
setStatus("Die Excel-Datei konnte nicht gelesen werden.", "error");
};
reader.readAsArrayBuffer(file);
}
async function sendExcelData() {
if (!table) {
setStatus("Es wurden noch keine Daten importiert.", "error");
return;
}
if (!backendUrl) {
setStatus("Backend-URL ist in der Session nicht definiert.", "error");
return;
}
if (!stationNumber) {
setStatus("Stationsnummer ist in der Session nicht definiert.", "error");
return;
}
const jsonData = table.getData().map(row => ({
MATERIAL_NO: valueToString(row.MATERIAL_NO),
BOM_VERSION_ERP: valueToString(row.BOM_VERSION_ERP),
COMPONENT_NO: valueToString(row.COMPONENT_NO),
COMP_NAME: valueToString(row.COMP_NAME),
POS_TYPE: valueToString(row.POS_TYPE),
ALTERNATIVE: valueToString(row.ALTERNATIVE),
WS_ERP: valueToString(row.WS_ERP),
LAYER: valueToString(row.LAYER),
QTY: valueToString(row.QTY),
UNIT: valueToString(row.UNIT),
SETUP_FLAG: valueToString(row.SETUP_FLAG),
PRODUKT: valueToString(row.PRODUKT),
PROCESS_GROUP: valueToString(row.PROCESS_GROUP)
}));
if (!jsonData.length) {
setStatus("Es sind keine Daten zum Senden vorhanden.", "error");
return;
}
sendButton.disabled = true;
setStatus("Daten werden gesendet ...", "info");
try {
const response = await fetch(backendUrl + "/backend/api/updateBom", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
station: stationNumber,
data: jsonData
})
}); });
}
}; if (!response.ok) {
reader.readAsBinaryString(fileInput.files[0]); const responseText = await response.text();
} throw new Error(
"HTTP " + response.status +
function sendExcelData() { (responseText ? ": " + responseText : "")
);
const jsonData = importedData.map(row => ({ }
MATERIAL_NO: ""+row[0],
BOM_VERSION_ERP: ""+row[1], const contentType = response.headers.get("content-type") || "";
COMPONENT_NO: ""+row[2], if (contentType.includes("application/json")) {
COMP_NAME: ""+row[3], await response.json();
POS_TYPE: ""+row[4], }
ALTERNATIVE: ""+row[5],
WS_ERP: ""+row[6], setStatus(jsonData.length + " BOM-Zeile(n) erfolgreich gesendet.", "success");
LAYER: ""+row[7], } catch (error) {
QTY: ""+row[8], console.error("Fehler beim Senden:", error);
UNIT: ""+row[9], setStatus("Fehler beim Senden der Daten: " + error.message, "error");
SETUP_FLAG: ""+row[10], } finally {
PRODUKT: ""+row[11], sendButton.disabled = false;
PROCESS_GROUP: ""+row[12] }
})); }
if (!url) {
alert("Backend URL is not defined."); document.getElementById("export-button").addEventListener("click", exportExcel);
return; fileInput.addEventListener("change", importExcelData);
} sendButton.addEventListener("click", sendExcelData);
fetch(url + '/backend/api/updateBom', { });
method: 'POST', </script>
headers: { 'Content-Type': 'application/json' }, </body>
body: JSON.stringify({ station: stationNumber, data: jsonData })
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then(result => alert("Daten erfolgreich gesendet!"))
.catch(error => alert("Fehler beim Senden der Daten: " + error.message));
}
// Event Listeners
document.getElementById("export-button").addEventListener("click", exportExcel);
document.getElementById("file-input").addEventListener("change", importExcelData);
document.getElementById("send-button").addEventListener("click", sendExcelData);
});
</script>
</body>
</html> </html>