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()); }