-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsave_product.php
More file actions
43 lines (38 loc) · 2.38 KB
/
Copy pathsave_product.php
File metadata and controls
43 lines (38 loc) · 2.38 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
<?php
// Compatibility endpoint: maps the old article_number/type fields to the current product_code/product_type schema.
require_once 'includes/auth.php';
require_login();
require_csrf();
require_once 'includes/db.php';
$product_code = trim($_POST['product_code'] ?? $_POST['article_number'] ?? '');
$product_name = trim($_POST['product_name'] ?? $_POST['type'] ?? '');
$product_type = trim($_POST['product_type'] ?? $_POST['type'] ?? '');
$stock = (float)($_POST['stock'] ?? 0);
$force = filter_var($_POST['force'] ?? false, FILTER_VALIDATE_BOOLEAN);
$materials = isset($_POST['materials']) ? json_decode($_POST['materials'], true) : [];
if ($product_code === '' || $product_name === '') {
json_response(['success' => false, 'message' => 'The article number and description are mandatory.'], 422);
}
$stmt = $pdo->prepare("SELECT COUNT(*) FROM products WHERE product_code = ?");
$stmt->execute([$product_code]);
if ((int)$stmt->fetchColumn() > 0 && !$force) {
json_response(['success' => false, 'duplicate' => true, 'message' => 'The article number already exists!']);
}
$stmt = $pdo->prepare("INSERT INTO products (product_code, product_name, product_type, primary_type, shape, device, accuracy, frequency, primary_value, secondary_value, extension, isolation_level, cable_length, cable_type, connection_type, burden, additional_info, stock) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->execute([
$product_code, $product_name, $product_type,
$_POST['primary_type'] ?? null, $_POST['shape'] ?? null, $_POST['device'] ?? null, $_POST['accuracy'] ?? null,
$_POST['frequency'] ?? null, $_POST['primary_value'] ?? null, $_POST['secondary_value'] ?? null, $_POST['extension'] ?? null,
$_POST['isolation_level'] ?? null, $_POST['cable_length'] ?? null, $_POST['cable_type'] ?? null,
$_POST['connection_type'] ?? null, $_POST['burden'] ?? null, $_POST['additional_info'] ?? null, $stock
]);
$product_id = $pdo->lastInsertId();
if ($product_id && is_array($materials)) {
$ins = $pdo->prepare("INSERT INTO boms (product_id, material_id, quantity, unit, version) VALUES (?, ?, ?, ?, 'standard')");
foreach ($materials as $row) {
if (!empty($row['material_id']) && (float)($row['quantity'] ?? 0) > 0) {
$ins->execute([$product_id, (int)$row['material_id'], (float)$row['quantity'], $row['unit'] ?? null]);
}
}
}
json_response(['success' => true]);