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

@@ -4,134 +4,376 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<title>Excel BOM Import</title>
<!-- Excel-Verarbeitung -->
<script src="/backend/xlsx/xlsx.full.min.js"></script>
<script src="/backend/handsontable/handsontable/dist/handsontable.full.min.js"></script>
<link rel="stylesheet" href="/backend/handsontable/handsontable/dist/handsontable.full.min.css">
<!-- 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">
<style>
* {
box-sizing: border-box;
}
body {
margin: 0;
padding: 20px;
font-family: Arial, sans-serif;
text-align: center;
background-color: #f4f4f4;
padding: 20px;
}
.container {
background: white;
display: inline-block;
width: min(1400px, 100%);
padding: 20px;
background: white;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
display: inline-block;
}
.button-area {
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
gap: 10px;
}
button {
margin: 10px;
margin: 10px 0;
padding: 10px 20px;
border: none;
background-color: red;
color: white;
background-color: red;
border-radius: 5px;
cursor: pointer;
}
button:hover:not(:disabled) {
background-color: #c90000;
}
button:disabled {
background-color: #999;
cursor: not-allowed;
}
input[type="file"] {
max-width: 100%;
}
#import-table {
width: 100%;
margin-top: 20px;
text-align: left;
}
#status-message {
min-height: 22px;
margin-top: 12px;
font-size: 14px;
}
.status-success {
color: #087a20;
}
.status-error {
color: #b00020;
}
.status-info {
color: #333;
}
.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 & Import</h2>
<button id="export-button">BOM Excel Export</button>
<input type="file" id="file-input" accept=".xlsx">
<button id="send-button">Daten Senden</button>
<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 importedData = [];
let hot; // Handsontable instance
const url = sessionStorage.getItem("url") ;
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 = ["PRODUCT", "BOM_VERSION_ERP","Part_NO","COMP_NAME","POS_TYPE","ALTERNATIVE","WS_ERP", "LAYER","QTY","UNIT","SETUP","PRODUKT", "PROCESS_GROUP"];
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 importExcelData(event) {
const fileInput = document.getElementById("file-input");
if (!fileInput.files.length) return;
const reader = new FileReader();
reader.onload = function(event) {
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);
if (hot) {
hot.loadData(importedData); // Update existing Handsontable
} else {
const container = document.getElementById("import-table");
hot = new Handsontable(container, {
data: importedData,
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%',
height: 300,
stretchH: 'all',
autoColumnSize: false,
manualColumnResize: true,
colWidths: 110,
wordWrap: false,
licenseKey: 'non-commercial-and-evaluation'
});
}
};
reader.readAsBinaryString(fileInput.files[0]);
}
function sendExcelData() {
const jsonData = importedData.map(row => ({
MATERIAL_NO: ""+row[0],
BOM_VERSION_ERP: ""+row[1],
COMPONENT_NO: ""+row[2],
COMP_NAME: ""+row[3],
POS_TYPE: ""+row[4],
ALTERNATIVE: ""+row[5],
WS_ERP: ""+row[6],
LAYER: ""+row[7],
QTY: ""+row[8],
UNIT: ""+row[9],
SETUP_FLAG: ""+row[10],
PRODUKT: ""+row[11],
PROCESS_GROUP: ""+row[12]
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])
}));
if (!url) {
alert("Backend URL is not defined.");
}
async function createOrUpdateTable(data) {
if (table) {
await table.replaceData(data);
return;
}
fetch(url + '/backend/api/updateBom', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
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));
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) {
const responseText = await response.text();
throw new Error(
"HTTP " + response.status +
(responseText ? ": " + responseText : "")
);
}
const contentType = response.headers.get("content-type") || "";
if (contentType.includes("application/json")) {
await response.json();
}
setStatus(jsonData.length + " BOM-Zeile(n) erfolgreich gesendet.", "success");
} catch (error) {
console.error("Fehler beim Senden:", error);
setStatus("Fehler beim Senden der Daten: " + error.message, "error");
} finally {
sendButton.disabled = false;
}
}
// Event Listeners
document.getElementById("export-button").addEventListener("click", exportExcel);
document.getElementById("file-input").addEventListener("change", importExcelData);
document.getElementById("send-button").addEventListener("click", sendExcelData);
fileInput.addEventListener("change", importExcelData);
sendButton.addEventListener("click", sendExcelData);
});
</script>
</body>