122 lines
4.6 KiB
PHP
122 lines
4.6 KiB
PHP
<?php
|
|
|
|
require_once __DIR__ . '/auth_check.php';
|
|
|
|
mnt_require_permission('maintenance.manage');
|
|
|
|
try {
|
|
$pdo = mnt_pdo();
|
|
|
|
$id = (int)($_POST['id'] ?? 0);
|
|
$equipmentId = (int)($_POST['equipment_id'] ?? 0);
|
|
$code = trim((string)($_POST['code'] ?? ''));
|
|
$title = trim((string)($_POST['title'] ?? ''));
|
|
$description = trim((string)($_POST['description'] ?? ''));
|
|
$interventionType = (string)($_POST['intervention_type'] ?? 'scheduled');
|
|
$executionType = (string)($_POST['execution_type'] ?? 'internal');
|
|
$isCritical = !empty($_POST['is_critical']) ? 1 : 0;
|
|
$frequencyUnit = (string)($_POST['frequency_unit'] ?? 'month');
|
|
$frequencyValue = (int)($_POST['frequency_value'] ?? 0);
|
|
$frequencyNote = trim((string)($_POST['frequency_note'] ?? ''));
|
|
$alertDays = (int)($_POST['alert_days'] ?? 7);
|
|
$assigneeId = (int)($_POST['assignee_employee_id'] ?? 0);
|
|
$supervisorId = (int)($_POST['supervisor_employee_id'] ?? 0);
|
|
$supplierId = (int)($_POST['supplier_id'] ?? 0);
|
|
$isActive = isset($_POST['is_active']) ? (int)!empty($_POST['is_active']) : 1;
|
|
$notes = trim((string)($_POST['notes'] ?? ''));
|
|
|
|
if ($title === '') {
|
|
mnt_json_fail('Il titolo è obbligatorio.');
|
|
}
|
|
if (!array_key_exists($interventionType, mnt_intervention_types())) {
|
|
mnt_json_fail('Tipo di intervento non valido.');
|
|
}
|
|
if (!array_key_exists($executionType, mnt_execution_types())) {
|
|
mnt_json_fail('Tipo di esecuzione non valido.');
|
|
}
|
|
if (!array_key_exists($frequencyUnit, mnt_frequency_units())) {
|
|
mnt_json_fail('Unità di frequenza non valida.');
|
|
}
|
|
if ($frequencyUnit !== 'on_demand' && $frequencyValue <= 0) {
|
|
mnt_json_fail('Indica ogni quanto va eseguita la manutenzione, oppure scegli "Al bisogno".');
|
|
}
|
|
if ($executionType === 'external' && $supplierId <= 0) {
|
|
mnt_json_fail('Per una manutenzione esterna indica il fornitore.');
|
|
}
|
|
|
|
if ($frequencyUnit === 'on_demand') {
|
|
$frequencyValue = null;
|
|
}
|
|
|
|
$values = [
|
|
'code' => $code ?: null,
|
|
'title' => $title,
|
|
'description' => $description ?: null,
|
|
'intervention_type' => $interventionType,
|
|
'execution_type' => $executionType,
|
|
'is_critical' => $isCritical,
|
|
'frequency_value' => $frequencyValue,
|
|
'frequency_unit' => $frequencyUnit,
|
|
'frequency_note' => $frequencyNote ?: null,
|
|
'alert_days' => max(0, $alertDays),
|
|
'assignee_employee_id' => $assigneeId ?: null,
|
|
'supervisor_employee_id' => $supervisorId ?: null,
|
|
'supplier_id' => $supplierId ?: null,
|
|
'is_active' => $isActive,
|
|
'notes' => $notes ?: null,
|
|
];
|
|
|
|
if ($id > 0) {
|
|
$stmt = $pdo->prepare("SELECT equipment_id FROM maint_maintenances WHERE id = ?");
|
|
$stmt->execute([$id]);
|
|
$equipmentId = (int)$stmt->fetchColumn();
|
|
|
|
if (!$equipmentId) {
|
|
mnt_json_fail('Manutenzione non trovata.');
|
|
}
|
|
|
|
$assignments = [];
|
|
foreach (array_keys($values) as $column) {
|
|
$assignments[] = "$column = :$column";
|
|
}
|
|
|
|
$stmt = $pdo->prepare("UPDATE maint_maintenances SET " . implode(', ', $assignments) . " WHERE id = :id");
|
|
$stmt->execute($values + ['id' => $id]);
|
|
|
|
// frequency may have changed — the due date must follow
|
|
mnt_recalc_maintenance($pdo, $id);
|
|
mnt_log($pdo, 'maintenance_updated', $equipmentId, $id, null, $currentUserId, $values);
|
|
|
|
mnt_json_ok(['id' => $id]);
|
|
}
|
|
|
|
if ($equipmentId <= 0) {
|
|
mnt_json_fail('Attrezzatura non valida.');
|
|
}
|
|
|
|
$stmt = $pdo->prepare("SELECT id FROM inv_equipment WHERE id = ?");
|
|
$stmt->execute([$equipmentId]);
|
|
if (!$stmt->fetchColumn()) {
|
|
mnt_json_fail('Attrezzatura non trovata.');
|
|
}
|
|
|
|
$values['equipment_id'] = $equipmentId;
|
|
$values['created_by'] = $currentUserId;
|
|
|
|
$columns = array_keys($values);
|
|
$placeholders = array_map(fn($column) => ':' . $column, $columns);
|
|
|
|
$stmt = $pdo->prepare(
|
|
"INSERT INTO maint_maintenances (" . implode(', ', $columns) . ")
|
|
VALUES (" . implode(', ', $placeholders) . ")"
|
|
);
|
|
$stmt->execute($values);
|
|
|
|
$newId = (int)$pdo->lastInsertId();
|
|
mnt_log($pdo, 'maintenance_created', $equipmentId, $newId, null, $currentUserId, $values);
|
|
|
|
mnt_json_ok(['id' => $newId]);
|
|
} catch (Throwable $e) {
|
|
mnt_json_fail('Errore: ' . $e->getMessage());
|
|
}
|