Files
zibo-dashboard/public/userarea/manutenzioni/ajax/save_equipment.php
T
2026-08-12 21:29:55 +03:00

96 lines
3.7 KiB
PHP

<?php
require_once __DIR__ . '/auth_check.php';
mnt_require_permission('maintenance.manage');
try {
$pdo = mnt_pdo();
$id = (int)($_POST['id'] ?? 0);
$name = trim((string)($_POST['name'] ?? ''));
$categoryId = (int)($_POST['category_id'] ?? 0);
$lineId = (int)($_POST['line_id'] ?? 0);
$registration = trim((string)($_POST['registration_number'] ?? ''));
$serial = trim((string)($_POST['serial_number'] ?? ''));
$batchLot = trim((string)($_POST['batch_lot'] ?? ''));
$manufacturer = trim((string)($_POST['manufacturer'] ?? ''));
$purchaseDate = trim((string)($_POST['purchase_date'] ?? ''));
$commissioningDate = trim((string)($_POST['commissioning_date'] ?? ''));
$toolType = trim((string)($_POST['tool_type'] ?? ''));
$status = (string)($_POST['status'] ?? 'active');
$departmentId = (int)($_POST['department_id'] ?? 0);
$location = trim((string)($_POST['location'] ?? ''));
$description = trim((string)($_POST['description'] ?? ''));
$notes = trim((string)($_POST['notes'] ?? ''));
if ($name === '') {
mnt_json_fail('Il nome è obbligatorio.');
}
if ($categoryId <= 0) {
mnt_json_fail('La categoria è obbligatoria.');
}
if (!array_key_exists($status, mnt_equipment_statuses())) {
mnt_json_fail('Stato non valido.');
}
// A category flagged requires_line must carry a production line
$stmt = $pdo->prepare("SELECT requires_line FROM inv_categories WHERE id = ?");
$stmt->execute([$categoryId]);
$requiresLine = $stmt->fetchColumn();
if ($requiresLine === false) {
mnt_json_fail('Categoria inesistente.');
}
if ((int)$requiresLine === 1 && $lineId <= 0) {
mnt_json_fail('Per questa categoria la linea di produzione è obbligatoria.');
}
$values = [
'name' => $name,
'category_id' => $categoryId,
'line_id' => $lineId ?: null,
'registration_number' => $registration ?: null,
'serial_number' => $serial ?: null,
'batch_lot' => $batchLot ?: null,
'manufacturer' => $manufacturer ?: null,
'purchase_date' => $purchaseDate ?: null,
'commissioning_date' => $commissioningDate ?: null,
'tool_type' => $toolType ?: null,
'status' => $status,
'department_id' => $departmentId ?: null,
'location' => $location ?: null,
'description' => $description ?: null,
'notes' => $notes ?: null,
];
if ($id > 0) {
$assignments = [];
foreach (array_keys($values) as $column) {
$assignments[] = "$column = :$column";
}
$stmt = $pdo->prepare("UPDATE inv_equipment SET " . implode(', ', $assignments) . " WHERE id = :id");
$stmt->execute($values + ['id' => $id]);
mnt_log($pdo, 'equipment_updated', $id, null, null, $currentUserId, $values);
mnt_json_ok(['id' => $id]);
}
$columns = array_keys($values);
$placeholders = array_map(fn($column) => ':' . $column, $columns);
$stmt = $pdo->prepare(
"INSERT INTO inv_equipment (" . implode(', ', $columns) . ", created_by)
VALUES (" . implode(', ', $placeholders) . ", :created_by)"
);
$stmt->execute($values + ['created_by' => $currentUserId]);
$newId = (int)$pdo->lastInsertId();
mnt_log($pdo, 'equipment_created', $newId, null, null, $currentUserId, $values);
mnt_json_ok(['id' => $newId]);
} catch (Throwable $e) {
mnt_json_fail('Errore: ' . $e->getMessage());
}