From 2274c9bb4af2c6d8a9a9f2c3dd807e9e49959fe4 Mon Sep 17 00:00:00 2001 From: Michael SCHAL Date: Fri, 9 Oct 2026 09:17:59 +0200 Subject: [PATCH] fix(reconciliation): auto-reconcile deliveries from automatic suppliers on validation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit POST /api/deliveries/:id/validate never set reconciled=true for suppliers in automatic reconciliation mode, so their deliveries showed in neither the Manuels tab (auto suppliers excluded) nor Validées (reconciled=false). - validate endpoint now marks reconciled + validatedAt for auto suppliers - Validées tab includes all deliveries from auto suppliers - migration backfills reconciled for existing delivered deliveries with BL Co-Authored-By: Claude Opus 5.5 --- client/src/pages/BLReconciliation.tsx | 5 ++++- .../20261009_backfill_auto_reconciliation.sql | 22 +++++++++++++++++++ server/routes.ts | 15 +++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 migrations/20261009_backfill_auto_reconciliation.sql diff --git a/client/src/pages/BLReconciliation.tsx b/client/src/pages/BLReconciliation.tsx index 7a876ac..f3f526d 100644 --- a/client/src/pages/BLReconciliation.tsx +++ b/client/src/pages/BLReconciliation.tsx @@ -497,8 +497,11 @@ export default function BLReconciliation() { return isManual && isNotValidated; }), [deliveriesWithBL]); + // Fournisseurs en rapprochement automatique : toujours considérés validés, + // même si reconciled n'a pas été posé (livraisons antérieures au passage en mode auto) const allValidatedDeliveries = useMemo(() => deliveriesWithBL.filter((delivery: any) => { - return delivery.reconciled === true || delivery.reconciled === 1; + const isAutomatic = delivery.supplier?.automaticReconciliation === true; + return isAutomatic || delivery.reconciled === true || delivery.reconciled === 1; }), [deliveriesWithBL]); // Fonctions de gestion diff --git a/migrations/20261009_backfill_auto_reconciliation.sql b/migrations/20261009_backfill_auto_reconciliation.sql new file mode 100644 index 0000000..2fac7db --- /dev/null +++ b/migrations/20261009_backfill_auto_reconciliation.sql @@ -0,0 +1,22 @@ +-- Backfill rapprochement automatique +-- +-- Contexte : l'endpoint POST /api/deliveries/:id/validate ne posait pas +-- reconciled = true pour les fournisseurs en rapprochement automatique +-- (automatic_reconciliation = true). Ces livraisons n'apparaissaient ni dans +-- l'onglet "Manuels" (exclues car fournisseur auto) ni dans "Validées" +-- (reconciled = false). Même cas pour les livraisons antérieures au passage +-- d'un fournisseur en mode automatique. +-- +-- Applique le même critère que l'auto-validation côté serveur : +-- livraison livrée + numéro de BL renseigné. + +UPDATE deliveries d +SET reconciled = true, + validated_at = COALESCE(d.validated_at, NOW()) +FROM suppliers s +WHERE d.supplier_id = s.id + AND s.automatic_reconciliation = true + AND d.status = 'delivered' + AND d.bl_number IS NOT NULL + AND TRIM(d.bl_number) <> '' + AND (d.reconciled IS NULL OR d.reconciled = false); diff --git a/server/routes.ts b/server/routes.ts index 6bf224f..dd69123 100644 --- a/server/routes.ts +++ b/server/routes.ts @@ -2556,6 +2556,21 @@ export async function registerRoutes(app: Express): Promise { await storage.validateDelivery(id, blData); + // AUTO-VALIDATION RAPPROCHEMENT : fournisseur en mode automatique → rapprochement validé directement + try { + // Fournisseur déjà joint à la livraison + const supplier = delivery.supplier; + if (supplier?.automaticReconciliation) { + await storage.updateDelivery(id, { + reconciled: true, + validatedAt: new Date() + }); + console.log(`✅ Auto-reconciliation: Delivery #${id} automatically validated for supplier ${supplier.name}`); + } + } catch (error) { + console.error(`❌ Auto-reconciliation failed for delivery #${id}:`, error); + } + // MISE À JOUR DU CACHE : Marquer le cache comme permanent pour cette livraison validée try { if (delivery.invoiceReference && delivery.invoiceReference.trim()) {