getConnection(); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $data = json_decode(file_get_contents('php://input'), true); $iddatadb = $data['iddatadb'] ?? null; $parts = $data['parts'] ?? []; if (!$iddatadb || empty($parts)) { echo json_encode(['success' => false, 'message' => 'Dati mancanti']); exit; } try { $pdo->beginTransaction(); $results = []; // Custom fields statements (child table) $stmtUpsertCF = $pdo->prepare(" INSERT INTO identification_parts_customfields (part_id, field_id, value_id, value_text) VALUES (:part_id, :field_id, :value_id, :value_text) ON DUPLICATE KEY UPDATE value_id = VALUES(value_id), value_text = VALUES(value_text), updated_at = NOW() "); $stmtDeleteCF = $pdo->prepare(" DELETE FROM identification_parts_customfields WHERE part_id = :part_id AND field_id = :field_id "); foreach ($parts as $part) { $partId = $part['id'] ?? null; $partNumber = $part['part_number'] ?? null; $partDescription = $part['part_description'] ?? ''; $mix = $part['mix'] ?? 'N'; $idmatrice = $part['idmatrice'] ?? null; $note = $part['note'] ?? null; $dateexpiry = $part['dateexpiry'] ?? null; // Extra field (0/1) $extraFieldId = $part['extra_field_id'] ?? null; $extraValueId = $part['extra_value_id'] ?? null; $extraValueText = $part['extra_value_text'] ?? null; // Normalizza vuoti if ($extraFieldId !== null && $extraFieldId !== '') $extraFieldId = (int)$extraFieldId; else $extraFieldId = null; if ($extraValueId !== null && $extraValueId !== '') $extraValueId = (int)$extraValueId; else $extraValueId = null; if ($extraValueText !== null) { $extraValueText = trim((string)$extraValueText); if ($extraValueText === '') $extraValueText = null; } if ($partId) { // UPDATE se la parte esiste (sempre) $stmt = $pdo->prepare("UPDATE identification_parts SET part_number = :part_number, part_description = :part_description, mix = :mix, idmatrice = :idmatrice, note = :note, dateexpiry = :dateexpiry, updated_at = NOW() WHERE id = :id"); $stmt->execute([ ':id' => $partId, ':part_number' => $partNumber, ':part_description' => $partDescription, ':mix' => $mix, ':idmatrice' => $idmatrice, ':note' => $note, ':dateexpiry' => $dateexpiry, ]); // Save extra custom field (if provided) if ($extraFieldId !== null) { if ($extraValueId === null && $extraValueText === null) { $stmtDeleteCF->execute([ ':part_id' => $partId, ':field_id' => $extraFieldId, ]); } else { $stmtUpsertCF->execute([ ':part_id' => $partId, ':field_id' => $extraFieldId, ':value_id' => $extraValueId, ':value_text' => $extraValueText, ]); } } $cf_row = null; if ($extraFieldId !== null) { $chk = $pdo->prepare("SELECT id, value_id, value_text FROM identification_parts_customfields WHERE part_id = ? AND field_id = ?"); $chk->execute([$partId, $extraFieldId]); $cf_row = $chk->fetch(PDO::FETCH_ASSOC); } $results[] = [ 'part_id' => $partId, 'part_number' => $partNumber, 'message' => 'Parte aggiornata con successo', 'cf_row' => $cf_row ]; } else if ($partDescription || $note || $dateexpiry) { // INSERT per nuova parte (solo se ha contenuto) $stmt = $pdo->prepare("INSERT INTO identification_parts (iddatadb, part_number, part_description, mix, idmatrice, note, dateexpiry, created_at, updated_at) VALUES (:iddatadb, :part_number, :part_description, :mix, :idmatrice, :note, :dateexpiry, NOW(), NOW())"); $stmt->execute([ ':iddatadb' => $iddatadb, ':part_number' => $partNumber, ':part_description' => $partDescription, ':mix' => $mix, ':idmatrice' => $idmatrice, ':note' => $note, ':dateexpiry' => $dateexpiry, ]); $newId = (int)$pdo->lastInsertId(); if ($extraFieldId !== null) { if ($extraValueId === null && $extraValueText === null) { $stmtDeleteCF->execute([ ':part_id' => $newId, ':field_id' => $extraFieldId, ]); } else { $stmtUpsertCF->execute([ ':part_id' => $newId, ':field_id' => $extraFieldId, ':value_id' => $extraValueId, ':value_text' => $extraValueText, ]); } } $results[] = [ 'part_id' => $newId, 'part_number' => $partNumber, 'message' => 'Parte salvata con successo' ]; } } $pdo->commit(); echo json_encode([ 'success' => true, 'results' => $results, 'debug_last' => [ 'extra_field_id' => $extraFieldId ?? null, 'extra_value_id' => $extraValueId ?? null, 'extra_value_text' => $extraValueText ?? null, 'part_id' => $partId ?? ($newId ?? null), ] ]); } catch (PDOException $e) { $pdo->rollBack(); echo json_encode(['success' => false, 'message' => 'Errore nel salvataggio: ' . $e->getMessage()]); }