fixed renumerate parts
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
header('Content-Type: application/json');
|
||||
include('include/headscript.php');
|
||||
|
||||
$dbHandler = DBHandlerSelect::getInstance();
|
||||
$pdo = $dbHandler->getConnection();
|
||||
|
||||
$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();
|
||||
|
||||
// Elimina tutte le parti esistenti per l'iddatadb per evitare conflitti di unicità
|
||||
$stmt = $pdo->prepare("DELETE FROM identification_parts WHERE iddatadb = :iddatadb");
|
||||
$stmt->execute([':iddatadb' => $iddatadb]);
|
||||
|
||||
// Prepara l'inserimento delle nuove parti
|
||||
$stmt = $pdo->prepare("
|
||||
INSERT INTO identification_parts
|
||||
(iddatadb, part_number, part_description, mix, created_at, updated_at)
|
||||
VALUES (:iddatadb, :part_number, :part_description, :mix, NOW(), NOW())
|
||||
");
|
||||
|
||||
$part_ids = [];
|
||||
foreach ($parts as $part) {
|
||||
$partNumber = $part['part_number'] ?? null;
|
||||
$partDescription = $part['part_description'] ?? '';
|
||||
$mix = $part['mix'] ?? 'N';
|
||||
|
||||
if (!$partNumber || !$partDescription) {
|
||||
throw new PDOException("Numero parte o descrizione mancante per parte: " . json_encode($part));
|
||||
}
|
||||
|
||||
$stmt->execute([
|
||||
':iddatadb' => $iddatadb,
|
||||
':part_number' => $partNumber,
|
||||
':part_description' => $partDescription,
|
||||
':mix' => $mix
|
||||
]);
|
||||
$part_ids[] = $pdo->lastInsertId();
|
||||
}
|
||||
|
||||
$pdo->commit();
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'part_ids' => $part_ids,
|
||||
'message' => 'Parti rinumerate con successo'
|
||||
]);
|
||||
} catch (PDOException $e) {
|
||||
$pdo->rollBack();
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'message' => 'Errore nel salvataggio: ' . $e->getMessage()
|
||||
]);
|
||||
}
|
||||
Reference in New Issue
Block a user