diff --git a/api/index.php b/api/index.php index 770f3d1..d651c39 100644 --- a/api/index.php +++ b/api/index.php @@ -9059,11 +9059,14 @@ function importMergeBackup(PDO $db): void { $stats['products']++; } - // --- 3. inventory : toujours inséré, product_id remappé --- +// --- 3. inventory : skip si même (product_id, location, expiry_date, quantity) existe déjà --- $rows = $src->query("SELECT * FROM inventory")->fetchAll(); foreach ($rows as $row) { $newPid = $productMap[(int)$row['product_id']] ?? null; if (!$newPid) continue; + $exists = $db->prepare("SELECT 1 FROM inventory WHERE product_id = ? AND location = ? AND COALESCE(expiry_date,'') = COALESCE(?,'') AND ABS(quantity - ?) < 0.001"); + $exists->execute([$newPid, $row['location'], $row['expiry_date'] ?? null, $row['quantity']]); + if ($exists->fetch()) continue; unset($row['id']); $row['product_id'] = $newPid; $cols = array_keys($row); @@ -9072,11 +9075,14 @@ function importMergeBackup(PDO $db): void { $stats['inventory']++; } - // --- 4. transactions : toujours insérées, product_id remappé --- + // --- 4. transactions : skip si même (product_id, type, quantity, created_at) existe déjà --- $rows = $src->query("SELECT * FROM transactions")->fetchAll(); foreach ($rows as $row) { $newPid = $productMap[(int)$row['product_id']] ?? null; if (!$newPid) continue; + $exists = $db->prepare("SELECT 1 FROM transactions WHERE product_id = ? AND type = ? AND ABS(quantity - ?) < 0.001 AND created_at = ?"); + $exists->execute([$newPid, $row['type'], $row['quantity'], $row['created_at']]); + if ($exists->fetch()) continue; unset($row['id']); $row['product_id'] = $newPid; $cols = array_keys($row); @@ -9111,9 +9117,12 @@ function importMergeBackup(PDO $db): void { $stats['recipe_library']++; } - // --- 7. chat_messages : toujours insérés --- + // --- 7. chat_messages : skip si même (role, text, created_at) existe déjà --- $rows = $src->query("SELECT * FROM chat_messages")->fetchAll(); foreach ($rows as $row) { + $exists = $db->prepare("SELECT 1 FROM chat_messages WHERE role = ? AND text = ? AND created_at = ?"); + $exists->execute([$row['role'], $row['text'], $row['created_at']]); + if ($exists->fetch()) continue; unset($row['id']); $cols = array_keys($row); $stmt = $db->prepare("INSERT INTO chat_messages (" . implode(',', $cols) . ") VALUES (" . implode(',', array_fill(0, count($cols), '?')) . ")");