-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproducts.php
More file actions
528 lines (495 loc) · 25 KB
/
Copy pathproducts.php
File metadata and controls
528 lines (495 loc) · 25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
<?php
require_once 'includes/auth.php';
require_page_login();
require_once 'includes/db.php';
?>
<!DOCTYPE html>
<html lang="hu">
<head>
<meta charset="UTF-8">
<title>Products | Aegisnet application</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body { background-color: #f8fff8; }
.sidebar { min-height: 100vh; background: #212529; display: flex; flex-direction: column; }
.sidebar .nav-link { color: #fff; }
.sidebar .nav-link.active { background: #28a745; color: #fff; }
.logo-img { max-height: 50px; }
.sidebar ul { flex: 1 1 auto; }
.sidebar .sidebar-footer { margin-top: auto; }
.table-hover tbody tr:hover { background: #e3ffe3; }
.btn-success { background-color: #28a745; }
.modal-header { background: #28a745; color: #fff; }
</style>
</head>
<body>
<div class="container-fluid">
<div class="row">
<!-- Sidebar -->
<nav class="col-md-2 d-none d-md-block sidebar py-4">
<div class="text-center mb-2">
<img src="assets/logo_en.jpg" class="logo-img" alt="company_logo">
</div>
<div class="text-center mb-3">
<span class="text-light small">Logged in as:<br>
<strong><?= htmlspecialchars($_SESSION['user']['username']) ?></strong>
</span>
</div>
<ul class="nav flex-column">
<li class="nav-item mb-2">
<a class="nav-link" href="orders.php">Orders</a>
</li>
<li class="nav-item mb-2">
<a class="nav-link active" href="products.php">Products</a>
</li>
<li class="nav-item mb-2">
<a class="nav-link" href="materials.php">Materials</a>
</li>
</ul>
<div class="sidebar-footer text-center mt-4">
<a href="logout.php" class="btn btn-outline-danger w-75">Logout</a>
</div>
</nav>
<!-- Main Content -->
<main class="col-md-10 ms-sm-auto px-4 py-4">
<div class="d-flex justify-content-between align-items-center mb-3">
<h2 class="fw-bold text-success">Products</h2>
<div>
<button class="btn btn-success me-2" onclick="exportTableToCSV('products.csv', 'productsTable')">CSV Export</button>
<button class="btn btn-success" data-bs-toggle="modal" data-bs-target="#addProductModal">+ New product</button>
</div>
</div>
<!-- Keresés/szűrés -->
<form id="filterForm" class="row g-3 mb-3">
<div class="col-md-3">
<input type="text" class="form-control" name="search" placeholder="Search by article, part number, name...">
</div>
<div class="col-md-2">
<button class="btn btn-outline-success w-100">Filter</button>
</div>
</form>
<div id="filterError" class="text-danger mb-2"></div>
<div class="table-responsive">
<table id="productsTable" class="table table-bordered table-hover align-middle">
<thead class="table-light">
<tr>
<th>Article</th>
<th>Type</th>
<th>Product type</th>
<th>Primary type</th>
<th>Stock</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<!-- JS tölti fel -->
</tbody>
</table>
</div>
</main>
</div>
</div>
<!-- Késztermék részletei modal -->
<div class="modal fade" id="productDetailsModal" tabindex="-1">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header bg-success text-white">
<h5 class="modal-title">Product details</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body" id="productDetailsBody">
<!-- JS tölti fel -->
</div>
</div>
</div>
</div>
<!-- Új termék modal -->
<div class="modal fade" id="addProductModal" tabindex="-1">
<div class="modal-dialog modal-lg">
<form id="addProductForm" class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Create a new product</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body row g-3">
<div class="col-md-3"><label class="form-label"><b>Article</b></label><input type="text" class="form-control" name="product_code" required maxlength="10"></div>
<div class="col-md-3"><label class="form-label"><b>Product name</b></label><input type="text" class="form-control" name="product_name" required></div>
<div class="col-md-3"><label class="form-label"><b>Product type</b></label><input type="text" class="form-control" name="product_type" required></div>
<div class="col-md-3"><label class="form-label"><b>Primary type</b></label><input type="text" class="form-control" name="primary_type"></div>
<div class="col-md-3"><label class="form-label"><b>Shape</b></label><input type="text" class="form-control" name="shape"></div>
<div class="col-md-3"><label class="form-label"><b>Device</b></label><input type="text" class="form-control" name="device"></div>
<div class="col-md-3"><label class="form-label"><b>Accuracy</b></label><input type="text" class="form-control" name="accuracy" required maxlength="10"></div>
<div class="col-md-3"><label class="form-label"><b>Frequency</b></label><input type="text" class="form-control" name="frequency" required></div>
<div class="col-md-3"><label class="form-label"><b>Primary value</b></label><input type="text" class="form-control" name="primary_value"></div>
<div class="col-md-3"><label class="form-label"><b>Secondary value</b></label><input type="text" class="form-control" name="secondary_value"></div>
<div class="col-md-3"><label class="form-label"><b>Extension</b></label><input type="text" class="form-control" name="extension"></div>
<div class="col-md-3"><label class="form-label"><b>Isolation level</b></label><input type="text" class="form-control" name="isolation_level" required maxlength="10"></div>
<div class="col-md-3"><label class="form-label"><b>Cable length</b></label><input type="text" class="form-control" name="cable_length" required></div>
<div class="col-md-3"><label class="form-label"><b>Cable type</b></label><input type="text" class="form-control" name="cable_type"></div>
<div class="col-md-3"><label class="form-label"><b>Connection type</b></label><input type="text" class="form-control" name="connection_type"></div>
<div class="col-md-3"><label class="form-label"><b>Load</b></label><input type="text"class="form-control" name="burden"></div>
<div class="col-md-3"><label class="form-label"><b>Additional info</b></label><input type="text" class="form-control" name="additional_info" required maxlength="10"></div>
<div class="col-md-3"><label class="form-label"><b>Stock</b></label><input type="text" class="form-control" name="stock" required></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-success">Save</button>
</div>
</form>
</div>
</div>
<!-- Edit modal (same fields, with the addition of an id field) -->
<div class="modal fade" id="editProductModal" tabindex="-1">
<div class="modal-dialog modal-lg">
<form id="editProductForm" class="modal-content">
<input type="hidden" name="id">
<div class="modal-header">
<h5 class="modal-title">Edit product</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body row g-3">
<div class="col-md-3"><label class="form-label"><b>Article</b></label><input type="text" class="form-control" name="product_code" required maxlength="10"></div>
<div class="col-md-3"><label class="form-label"><b>Product name</b></label><input type="text" class="form-control" name="product_name" required></div>
<div class="col-md-3"><label class="form-label"><b>Product type</b></label><input type="text" class="form-control" name="product_type" required></div>
<div class="col-md-3"><label class="form-label"><b>Primary type</b></label><input type="text" class="form-control" name="primary_type"></div>
<div class="col-md-3"><label class="form-label"><b>Shape</b></label><input type="text" class="form-control" name="shape"></div>
<div class="col-md-3"><label class="form-label"><b>Device</b></label><input type="text" class="form-control" name="device"></div>
<div class="col-md-3"><label class="form-label"><b>Accuracy</b></label><input type="text" class="form-control" name="accuracy" required maxlength="10"></div>
<div class="col-md-3"><label class="form-label"><b>Frequency</b></label><input type="text" class="form-control" name="frequency" required></div>
<div class="col-md-3"><label class="form-label"><b>Primary value</b></label><input type="text" class="form-control" name="primary_value"></div>
<div class="col-md-3"><label class="form-label"><b>Secondary value</b></label><input type="text" class="form-control" name="secondary_value"></div>
<div class="col-md-3"><label class="form-label"><b>Extension</b></label><input type="text" class="form-control" name="extension"></div>
<div class="col-md-3"><label class="form-label"><b>Isolation level</b></label><input type="text" class="form-control" name="isolation_level" required maxlength="10"></div>
<div class="col-md-3"><label class="form-label"><b>Cable length</b></label><input type="text" class="form-control" name="cable_length" required></div>
<div class="col-md-3"><label class="form-label"><b>Cable type</b></label><input type="text" class="form-control" name="cable_type"></div>
<div class="col-md-3"><label class="form-label"><b>Connection type</b></label><input type="text" class="form-control" name="connection_type"></div>
<div class="col-md-3"><label class="form-label"><b>Load</b></label><input type="text"class="form-control" name="burden"></div>
<div class="col-md-3"><label class="form-label"><b>Additional info</b></label><input type="text" class="form-control" name="additional_info" required maxlength="10"></div>
<div class="col-md-3"><label class="form-label"><b>Stock</b></label><input type="text" class="form-control" name="stock" required></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-success">Save</button>
</div>
</form>
</div>
</div>
<!-- BOM editor modal (Detailed, with version selector, calculation) -->
<div class="modal fade" id="bomModal" tabindex="-1">
<div class="modal-dialog modal-xl">
<form class="modal-content" id="bomForm" onsubmit="event.preventDefault()">
<div class="modal-header bg-success text-white">
<h5 class="modal-title" id="bomModalTitle">Standard BOM list</h5>
<select id="bomVersionSelect" class="form-select form-select-sm w-auto ms-3" onchange="changeBOMVersion()"></select>
<button class="btn btn-outline-primary btn-sm ms-2" type="button" onclick="createBOMVersion()">+ New version</button>
<button type="button" class="btn-close ms-auto" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body" id="bomTableBody"></div>
<div class="modal-footer">
<div class="me-auto fw-bold" id="bomSumPrice"></div>
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
</div>
</form>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
<script>
const CSRF_TOKEN = <?= json_encode(current_csrf_token()) ?>;
function withCsrf(params = {}) { const data = new URLSearchParams(params); data.append('csrf_token', CSRF_TOKEN); return data; }
function appendCsrf(formData) { formData.append('csrf_token', CSRF_TOKEN); return formData; }
function esc(value) { return String(value ?? '').replace(/[&<>"']/g, ch => ({'&':'&','<':'<','>':'>','"':'"',"'":'''}[ch])); }
// CSV export
function exportTableToCSV(filename = 'export.csv', tableId = 'productsTable') {
let table = document.getElementById(tableId);
let rows = Array.from(table.querySelectorAll('tr'));
let csv = rows.map(row => {
return Array.from(row.querySelectorAll('th,td'))
.map(cell => {
let text = cell.innerText.replace(/\n/g,' ').replace(/"/g,'""');
return `"${text}"`;
}).join(',');
}).join('\n');
let blob = new Blob([csv], {type: 'text/csv'});
let link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = filename;
link.click();
}
// Loading products
function loadProducts(filters = {}) {
let params = new URLSearchParams(filters);
fetch('products_list.php?' + params.toString())
.then(res => res.json())
.then(list => {
let html = '';
list.forEach(p => {
html += `<tr>
<td>${esc(p.product_code)}</td>
<td>${esc(p.product_name)}</td>
<td>${esc(p.product_type)}</td>
<td>${esc(p.primary_type)}</td>
<td>${esc(p.stock)}</td>
<td>
<button class="btn btn-outline-info btn-sm me-1" onclick="showProductDetails(${p.id});">Details</button>
<button class="btn btn-outline-primary btn-sm me-1" title="Edit product" onclick="editProduct(${p.id});">✏️</button>
<button class="btn btn-outline-success btn-sm me-1" title="BOM list" onclick="showBOMModal(${p.id},${esc(JSON.stringify(p.product_name||''))});">🧾</button>
<button class="btn btn-outline-danger btn-sm" title="Delete product" onclick="deleteProduct(${p.id});">🗑️</button>
</td>
</tr>`;
});
document.querySelector('#productsTable tbody').innerHTML = html;
});
}
loadProducts();
document.getElementById('filterForm').onsubmit = function(e) {
e.preventDefault();
document.getElementById('filterError').textContent = '';
let data = Object.fromEntries(new FormData(this));
loadProducts(data);
}
// Details modal
function showProductDetails(id) {
fetch('products_details.php?id='+id)
.then(res => res.json())
.then(p => {
let html = `
<div class="row mb-3">
<div class="col-md-4"><b>Article:</b> ${esc(p.product_code)}</div>
<div class="col-md-4"><b>Product name:</b> ${esc(p.product_name)}</div>
<div class="col-md-4"><b>Stock:</b> ${esc(p.stock)}</div>
</div>
<div class="row mb-3">
<div class="col-md-4"><b>Product type:</b> ${esc(p.product_type)}</div>
<div class="col-md-4"><b>Primary type:</b> ${esc(p.primary_type)}</div>
<div class="col-md-4"><b>Shape:</b> ${p.shape||''}</div>
</div>
<hr>
<div class="col-12 d-flex justify-content-center"><b>Technical parameters:</b></div>
<br>
<div class="row mb-3">
<div class="col-6 d-flex justify-content-center"><b>Device:</b></div>
<div class="col-6 d-flex justify-content-center"> ${p.device||''}</div>
</div>
<div class="row mb-3">
<div class="col-6 d-flex justify-content-center"><b>Accuracy:</b></div>
<div class="col-6 d-flex justify-content-center"> ${p.accuracy||''}</div>
</div>
<div class="row mb-3">
<div class="col-6 d-flex justify-content-center"><b>Frequency:</b></div>
<div class="col-6 d-flex justify-content-center"> ${p.frequency||''}</div>
</div>
<div class="row mb-3">
<div class="col-6 d-flex justify-content-center"><b>Primary value:</b></div>
<div class="col-6 d-flex justify-content-center"> ${p.primary_value||''}</div>
</div>
<div class="row mb-3">
<div class="col-6 d-flex justify-content-center"><b>Secondary value:</b></div>
<div class="col-6 d-flex justify-content-center"> ${p.secondary_value||''}</div>
</div>
<div class="row mb-3">
<div class="col-6 d-flex justify-content-center"><b>Extension:</b></div>
<div class="col-6 d-flex justify-content-center"> ${p.extension||''}</div>
</div>
<div class="row mb-3">
<div class="col-6 d-flex justify-content-center"><b>Isolation level:</b></div>
<div class="col-6 d-flex justify-content-center"> ${p.isolation_level||''}</div>
</div>
<div class="row mb-3">
<div class="col-6 d-flex justify-content-center"><b>Cable length:</b></div>
<div class="col-6 d-flex justify-content-center"> ${p.cable_length||''}</div>
</div>
<div class="row mb-3">
<div class="col-6 d-flex justify-content-center"><b>Cable type:</b></div>
<div class="col-6 d-flex justify-content-center"> ${p.cable_type||''}</div>
</div>
<div class="row mb-3">
<div class="col-6 d-flex justify-content-center"><b>Connection type:</b></div>
<div class="col-6 d-flex justify-content-center"> ${p.connection_type||''}</div>
</div>
<div class="row mb-3">
<div class="col-6 d-flex justify-content-center"><b>Load:</b></div>
<div class="col-6 d-flex justify-content-center"> ${p.burden||''}</div>
</div>
<div class="row mb-3">
<div class="col-6 d-flex justify-content-center"><b>Additional info:</b></div>
<div class="col-6 d-flex justify-content-center"> ${p.additional_info||''}</div>
</div>
`;
// Specifications
if(p.specifications && Array.isArray(p.specifications) && p.specifications.length) {
html += `<h5 class="mt-4 mb-2">Specification parameters:</h5>
<div class="table-responsive">
<table class="table table-bordered">
<thead><tr><th>Name of parameter</th><th>Value</th></tr></thead>
<tbody>
`;
for(const spec of p.specifications) {
html += `<tr><td>${spec.name}</td><td>${spec.value}</td></tr>`;
}
html += `</tbody></table></div>`;
} else {
html += `<div class="mt-3 text-muted">There are no specification parameters.</div>`;
}
document.getElementById('productDetailsBody').innerHTML = html;
var modal = new bootstrap.Modal(document.getElementById('productDetailsModal'));
modal.show();
});
}
// New/Edit modal submit - like in materials.php, just with different endpoints!
document.getElementById('addProductForm').onsubmit = function(e) {
e.preventDefault();
const data = appendCsrf(new FormData(this));
fetch('products_save.php', { method: 'POST', body: data })
.then(res => res.json())
.then(json => {
if (json.success) {
alert('Successful save!');
document.getElementById('addProductModal').querySelector('button.btn-close').click();
this.reset();
loadProducts();
} else {
alert('Hiba: ' + (json.message || 'Unknown error'));
}
});
};
function editProduct(id) {
fetch('products_details.php?id='+id)
.then(res => res.json())
.then(m => {
for(const key in m) {
let el = document.querySelector('#editProductForm [name="'+key+'"]');
if(el) el.value = m[key];
}
var modal = new bootstrap.Modal(document.getElementById('editProductModal'));
modal.show();
});
}
document.getElementById('editProductForm').onsubmit = function(e) {
e.preventDefault();
const data = appendCsrf(new FormData(this));
fetch('products_update.php', {method: 'POST', body: data})
.then(res => res.json())
.then(json => {
if(json.success) {
document.getElementById('editProductModal').querySelector('button.btn-close').click();
this.reset();
loadProducts();
} else alert('Error while saving!');
});
}
function deleteProduct(id) {
if (confirm('Are you sure you want to delete this product?')) {
fetch('products_delete.php', {method: 'POST', body: withCsrf({id:id})})
.then(res => res.json()).then(json => {
if(json.success) loadProducts();
else alert('Error while deleting!');
});
}
}
// BOM editor modal
let currentBOMProduct = 0;
let currentBOMVersion = 'standard';
function showBOMModal(product_id, product_name) {
currentBOMProduct = product_id;
loadBOMVersions(product_id, function(versions) {
let select = document.getElementById('bomVersionSelect');
select.innerHTML = versions.map(v => `<option${v==currentBOMVersion?' selected':''}>${v}</option>`).join('');
});
document.getElementById('bomModalTitle').textContent = 'Standard BOM list – ' + product_name;
loadBOM(product_id, currentBOMVersion);
var modal = new bootstrap.Modal(document.getElementById('bomModal'));
modal.show();
}
function loadBOMVersions(product_id, cb) {
fetch('boms_versions.php?product_id='+product_id)
.then(res=>res.json())
.then(versions=>cb(versions));
}
function changeBOMVersion() {
currentBOMVersion = document.getElementById('bomVersionSelect').value;
loadBOM(currentBOMProduct, currentBOMVersion);
}
function loadBOM(product_id, version) {
fetch('boms_list.php?product_id='+product_id+'&version='+encodeURIComponent(version))
.then(res => res.json())
.then(list => {
let html = `<table class="table table-bordered"><thead><tr>
<th>Material</th><th>Quantity</th><th>Unit</th><th>Unit price</th><th>Total</th><th></th></tr></thead><tbody>`;
let sum = 0;
list.forEach(row => {
let ossz = (parseFloat(row.purchase_price||0) * parseFloat(row.quantity||0)).toFixed(2);
sum += parseFloat(ossz);
html += `<tr>
<td>${row.product_name} <span class="text-muted">(${row.product_code})</span></td>
<td><input type="number" step="0.001" value="${row.quantity}" data-id="${row.id}" class="form-control bom-qty"></td>
<td>${row.unit||row.material_unit||''}</td>
<td>${row.purchase_price||'-'}</td>
<td>${ossz}</td>
<td>
<button class="btn btn-outline-danger btn-sm" onclick="deleteBOM(${row.id})">🗑️</button>
</td>
</tr>`;
});
html += '</tbody></table>';
html += `<button class="btn btn-outline-success btn-sm" onclick="addBOMRow(${product_id},'${version}')">+ Add a new row</button>`;
document.getElementById('bomTableBody').innerHTML = html;
document.getElementById('bomSumPrice').innerHTML = "Total BOM raw material: <span class='text-success'>" + sum.toFixed(2) + " EUR</span>";
// automatic update upon change:
document.querySelectorAll('.bom-qty').forEach(el => {
el.onchange = function() {
fetch('boms_update.php', {
method:'POST',
body: withCsrf({id: el.dataset.id, quantity: el.value, unit: el.closest("tr").querySelector(".bom-unit")?.value || ""})
}).then(()=>loadBOM(product_id, version));
};
});
});
}
function addBOMRow(product_id, version) {
fetch('materials_list.php?is_active=1')
.then(res => res.json())
.then(materials => {
let select = `<select class="form-select" id="newBOMMaterial">` +
materials.map(m => `<option value="${m.id}">${m.product_name} (${m.product_code})</option>`).join('') +
`</select>`;
let html = `
<div class="row g-2 mb-2 align-items-center">
<div class="col-md-4">${select}</div>
<div class="col-md-2"><input type="number" step="0.001" class="form-control" id="newBOMQty" placeholder="Quantity"></div>
<div class="col-md-2"><input type="text" class="form-control" id="newBOMUnit" placeholder="Unit"></div>
<div class="col-md-2">
<button class="btn btn-success btn-sm" onclick="saveNewBOMRow(${product_id},'${version}')">Save</button>
</div>
</div>`;
document.getElementById('bomTableBody').insertAdjacentHTML('beforeend', html);
});
}
function saveNewBOMRow(product_id, version) {
let material_id = document.getElementById('newBOMMaterial').value;
let qty = document.getElementById('newBOMQty').value;
let unit = document.getElementById('newBOMUnit').value;
fetch('boms_save.php', {
method:'POST',
body: withCsrf({product_id, material_id, quantity: qty, unit, version})
}).then(()=>loadBOM(product_id, version));
}
function deleteBOM(id) {
if(confirm("Are you sure you want to delete this line?")) {
fetch('boms_delete.php', {method:'POST', body:withCsrf({id})})
.then(()=>loadBOM(currentBOMProduct, currentBOMVersion));
}
}
function createBOMVersion() {
let name = prompt("New BOM version name:");
if(name && name.trim().length > 0) {
currentBOMVersion = name.trim();
loadBOM(currentBOMProduct, currentBOMVersion);
loadBOMVersions(currentBOMProduct, function(versions){
document.getElementById('bomVersionSelect').innerHTML = versions.map(v => `<option${v==currentBOMVersion?' selected':''}>${v}</option>`).join('');
});
}
}
</script>
</body>
</html>