diff --git a/public/userarea/save_parts_quotation.php b/public/userarea/save_parts_quotation.php index 2445f70..3408f45 100644 --- a/public/userarea/save_parts_quotation.php +++ b/public/userarea/save_parts_quotation.php @@ -4,6 +4,7 @@ include('include/headscript.php'); $dbHandler = DBHandlerSelect::getInstance(); $pdo = $dbHandler->getConnection(); +$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $data = json_decode(file_get_contents('php://input'), true); @@ -15,46 +16,73 @@ if (!$idquotations || empty($parts)) { exit; } -$part = $parts[0]; -$partId = $part['id'] ?? null; -$partNumber = $part['part_number'] ?? null; -$partDescription = $part['part_description'] ?? ''; -$mix = $part['mix'] ?? 'N'; +try { + $pdo->beginTransaction(); + $results = []; + + 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; -if ($partDescription) { - try { if ($partId) { - // UPDATE se esiste già la parte + // UPDATE se la parte esiste $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"); + WHERE id = :id AND idquotations = :idquotations"); $stmt->execute([ ':id' => $partId, ':part_number' => $partNumber, ':part_description' => $partDescription, - ':mix' => $mix + ':mix' => $mix, + ':idmatrice' => $idmatrice, + ':note' => $note, + ':dateexpiry' => $dateexpiry, + ':idquotations' => $idquotations, ]); - echo json_encode(['success' => true, 'part_id' => $partId, 'part_number' => $partNumber, 'message' => 'Parte aggiornata con successo']); - } else { - // INSERT se è nuova + + $results[] = [ + 'part_id' => $partId, + 'part_number' => $partNumber, + 'message' => 'Parte aggiornata con successo' + ]; + } else if ($partDescription || $note || $dateexpiry) { + // INSERT per nuova parte (solo se ha contenuto) $stmt = $pdo->prepare("INSERT INTO identification_parts - (idquotations, part_number, part_description, mix, created_at, updated_at) - VALUES (:idquotations, :part_number, :part_description, :mix, NOW(), NOW())"); + (idquotations, part_number, part_description, mix, idmatrice, note, dateexpiry, created_at, updated_at) + VALUES (:idquotations, :part_number, :part_description, :mix, :idmatrice, :note, :dateexpiry, NOW(), NOW())"); $stmt->execute([ ':idquotations' => $idquotations, ':part_number' => $partNumber, ':part_description' => $partDescription, - ':mix' => $mix + ':mix' => $mix, + ':idmatrice' => $idmatrice, + ':note' => $note, + ':dateexpiry' => $dateexpiry, ]); - $newId = $pdo->lastInsertId(); - echo json_encode(['success' => true, 'part_id' => $newId, 'part_number' => $partNumber, 'message' => 'Parte salvata con successo']); + $newId = (int)$pdo->lastInsertId(); + + $results[] = [ + 'part_id' => $newId, + 'part_number' => $partNumber, + 'message' => 'Parte salvata con successo' + ]; } - } catch (PDOException $e) { - echo json_encode(['success' => false, 'message' => 'Errore nel salvataggio: ' . $e->getMessage()]); } -} else { - echo json_encode(['success' => false, 'message' => 'Descrizione mancante']); + + $pdo->commit(); + echo json_encode(['success' => true, 'results' => $results]); +} catch (PDOException $e) { + $pdo->rollBack(); + echo json_encode(['success' => false, 'message' => 'Errore nel salvataggio: ' . $e->getMessage()]); }