fixed quotations parts

This commit is contained in:
2026-08-04 20:02:50 +02:00
parent 54c164478c
commit 601e72ca61
+50 -22
View File
@@ -4,6 +4,7 @@ include('include/headscript.php');
$dbHandler = DBHandlerSelect::getInstance(); $dbHandler = DBHandlerSelect::getInstance();
$pdo = $dbHandler->getConnection(); $pdo = $dbHandler->getConnection();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$data = json_decode(file_get_contents('php://input'), true); $data = json_decode(file_get_contents('php://input'), true);
@@ -15,46 +16,73 @@ if (!$idquotations || empty($parts)) {
exit; exit;
} }
$part = $parts[0]; try {
$partId = $part['id'] ?? null; $pdo->beginTransaction();
$partNumber = $part['part_number'] ?? null; $results = [];
$partDescription = $part['part_description'] ?? '';
$mix = $part['mix'] ?? 'N'; 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) { if ($partId) {
// UPDATE se esiste già la parte // UPDATE se la parte esiste
$stmt = $pdo->prepare("UPDATE identification_parts $stmt = $pdo->prepare("UPDATE identification_parts
SET part_number = :part_number, SET part_number = :part_number,
part_description = :part_description, part_description = :part_description,
mix = :mix, mix = :mix,
idmatrice = :idmatrice,
note = :note,
dateexpiry = :dateexpiry,
updated_at = NOW() updated_at = NOW()
WHERE id = :id"); WHERE id = :id AND idquotations = :idquotations");
$stmt->execute([ $stmt->execute([
':id' => $partId, ':id' => $partId,
':part_number' => $partNumber, ':part_number' => $partNumber,
':part_description' => $partDescription, ':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 { $results[] = [
// INSERT se è nuova '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 $stmt = $pdo->prepare("INSERT INTO identification_parts
(idquotations, part_number, part_description, mix, created_at, updated_at) (idquotations, part_number, part_description, mix, idmatrice, note, dateexpiry, created_at, updated_at)
VALUES (:idquotations, :part_number, :part_description, :mix, NOW(), NOW())"); VALUES (:idquotations, :part_number, :part_description, :mix, :idmatrice, :note, :dateexpiry, NOW(), NOW())");
$stmt->execute([ $stmt->execute([
':idquotations' => $idquotations, ':idquotations' => $idquotations,
':part_number' => $partNumber, ':part_number' => $partNumber,
':part_description' => $partDescription, ':part_description' => $partDescription,
':mix' => $mix ':mix' => $mix,
':idmatrice' => $idmatrice,
':note' => $note,
':dateexpiry' => $dateexpiry,
]); ]);
$newId = $pdo->lastInsertId(); $newId = (int)$pdo->lastInsertId();
echo json_encode(['success' => true, 'part_id' => $newId, 'part_number' => $partNumber, 'message' => 'Parte salvata con successo']);
$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()]);
} }