backend/src/main/webapp/BOMImport.html aktualisiert
This commit is contained in:
@@ -4,134 +4,376 @@
|
|||||||
<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>
|
||||||
|
|
||||||
|
<!-- Excel-Verarbeitung -->
|
||||||
<script src="/backend/xlsx/xlsx.full.min.js"></script>
|
<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">
|
<link rel="stylesheet" href="/backend/ios.css">
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
|
* {
|
||||||
|
box-sizing: border-box;
|
||||||
|
}
|
||||||
|
|
||||||
body {
|
body {
|
||||||
|
margin: 0;
|
||||||
|
padding: 20px;
|
||||||
font-family: Arial, sans-serif;
|
font-family: Arial, sans-serif;
|
||||||
text-align: center;
|
text-align: center;
|
||||||
background-color: #f4f4f4;
|
background-color: #f4f4f4;
|
||||||
padding: 20px;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.container {
|
.container {
|
||||||
background: white;
|
display: inline-block;
|
||||||
|
width: min(1400px, 100%);
|
||||||
padding: 20px;
|
padding: 20px;
|
||||||
|
background: white;
|
||||||
border-radius: 10px;
|
border-radius: 10px;
|
||||||
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
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 {
|
button {
|
||||||
margin: 10px;
|
margin: 10px 0;
|
||||||
padding: 10px 20px;
|
padding: 10px 20px;
|
||||||
border: none;
|
border: none;
|
||||||
background-color: red;
|
|
||||||
color: white;
|
color: white;
|
||||||
|
background-color: red;
|
||||||
border-radius: 5px;
|
border-radius: 5px;
|
||||||
cursor: pointer;
|
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 {
|
#import-table {
|
||||||
|
width: 100%;
|
||||||
margin-top: 20px;
|
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>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<h2>Excel Export & Import</h2>
|
<h2>Excel Export & Import</h2>
|
||||||
<button id="export-button">BOM Excel Export</button>
|
|
||||||
<input type="file" id="file-input" accept=".xlsx">
|
<div class="button-area">
|
||||||
<button id="send-button">Daten Senden</button>
|
<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 id="import-table"></div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
document.addEventListener("DOMContentLoaded", function () {
|
document.addEventListener("DOMContentLoaded", function () {
|
||||||
let importedData = [];
|
let table = null;
|
||||||
let hot; // Handsontable instance
|
|
||||||
const url = sessionStorage.getItem("url") ;
|
const backendUrl = sessionStorage.getItem("url");
|
||||||
const stationNumber = sessionStorage.getItem("stationNumber");
|
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() {
|
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 worksheet = XLSX.utils.aoa_to_sheet([headers]);
|
||||||
const workbook = XLSX.utils.book_new();
|
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.utils.book_append_sheet(workbook, worksheet, "BOM");
|
||||||
XLSX.writeFile(workbook, "BOMExport.xlsx");
|
XLSX.writeFile(workbook, "BOMExport.xlsx");
|
||||||
}
|
}
|
||||||
|
|
||||||
function importExcelData(event) {
|
function convertRowsToObjects(rows) {
|
||||||
const fileInput = document.getElementById("file-input");
|
return rows
|
||||||
if (!fileInput.files.length) return;
|
.filter(row => Array.isArray(row) && row.some(cell => valueToString(cell) !== ""))
|
||||||
|
.map((row, index) => ({
|
||||||
const reader = new FileReader();
|
id: index + 1,
|
||||||
reader.onload = function(event) {
|
MATERIAL_NO: valueToString(row[0]),
|
||||||
const workbook = XLSX.read(event.target.result, { type: "binary" });
|
BOM_VERSION_ERP: valueToString(row[1]),
|
||||||
const sheet = workbook.Sheets[workbook.SheetNames[0]];
|
COMPONENT_NO: valueToString(row[2]),
|
||||||
importedData = XLSX.utils.sheet_to_json(sheet, { header: 1 }).slice(1);
|
COMP_NAME: valueToString(row[3]),
|
||||||
|
POS_TYPE: valueToString(row[4]),
|
||||||
if (hot) {
|
ALTERNATIVE: valueToString(row[5]),
|
||||||
hot.loadData(importedData); // Update existing Handsontable
|
WS_ERP: valueToString(row[6]),
|
||||||
} else {
|
LAYER: valueToString(row[7]),
|
||||||
const container = document.getElementById("import-table");
|
QTY: valueToString(row[8]),
|
||||||
hot = new Handsontable(container, {
|
UNIT: valueToString(row[9]),
|
||||||
data: importedData,
|
SETUP_FLAG: valueToString(row[10]),
|
||||||
colHeaders:["MATERIAL_NO", "BOM_VERSION_ERP", "COMPONENT_NO","COMP_NAME","POS_TYPE","ALTERNATIVE","WS_ERP", "LAYER","QTY","UNIT","SETUP_FLAG","PRODUKT", "PROCESS_GROUP"],
|
PRODUKT: valueToString(row[11]),
|
||||||
rowHeaders: true,
|
PROCESS_GROUP: valueToString(row[12])
|
||||||
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]
|
|
||||||
}));
|
}));
|
||||||
if (!url) {
|
}
|
||||||
alert("Backend URL is not defined.");
|
|
||||||
|
async function createOrUpdateTable(data) {
|
||||||
|
if (table) {
|
||||||
|
await table.replaceData(data);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
fetch(url + '/backend/api/updateBom', {
|
|
||||||
method: 'POST',
|
table = new Tabulator("#import-table", {
|
||||||
headers: { 'Content-Type': 'application/json' },
|
data: data,
|
||||||
body: JSON.stringify({ station: stationNumber, data: jsonData })
|
index: "id",
|
||||||
})
|
height: "300px",
|
||||||
.then(response => {
|
layout: "fitDataStretch",
|
||||||
if (!response.ok) {
|
movableColumns: true,
|
||||||
throw new Error(`HTTP error! status: ${response.status}`);
|
resizableColumnFit: true,
|
||||||
}
|
placeholder: "Keine BOM-Daten importiert",
|
||||||
return response.json();
|
rowHeader: {
|
||||||
})
|
formatter: "rownum",
|
||||||
.then(result => alert("Daten erfolgreich gesendet!"))
|
headerSort: false,
|
||||||
.catch(error => alert("Fehler beim Senden der Daten: " + error.message));
|
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("export-button").addEventListener("click", exportExcel);
|
||||||
document.getElementById("file-input").addEventListener("change", importExcelData);
|
fileInput.addEventListener("change", importExcelData);
|
||||||
document.getElementById("send-button").addEventListener("click", sendExcelData);
|
sendButton.addEventListener("click", sendExcelData);
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
Reference in New Issue
Block a user