From 47c5ad34fab1ec9888818a13035d363f4b37a9fe Mon Sep 17 00:00:00 2001 From: Olivier Date: Sun, 12 Jul 2026 19:29:52 +0200 Subject: [PATCH] Fixe bug --- backend/src/utils/schedule.js | 57 ++++++++++++++++++++++++++++++----- 1 file changed, 49 insertions(+), 8 deletions(-) diff --git a/backend/src/utils/schedule.js b/backend/src/utils/schedule.js index 60bf5a2..5909e39 100644 --- a/backend/src/utils/schedule.js +++ b/backend/src/utils/schedule.js @@ -460,14 +460,55 @@ export function generateSimulWithReinvestissements(db, investissementId) { } db.transaction(() => { - db.prepare('DELETE FROM simul_remboursements WHERE investissement_id=?').run(investissementId); - const stmt = db.prepare(` - INSERT INTO simul_remboursements - (investissement_id, numero_echeance, date_prevue, capital_prevu, interets_prevus, total_prevu) - VALUES (?,?,?,?,?,?) - `); - for (const e of schedule) { - stmt.run(investissementId, e.n, e.date, e.capital, e.interets, e.total); + if (inv.date_debut_simul) { + // ── Mode restructuration ────────────────────────────────────────────── + // Même logique que generateSimul() : conserver les échéances déjà payées avant + // la date de restructuration, supprimer le reste, et renuméroter à partir du + // nombre de mois réellement écoulés (pas de repartir à 1, qui ferait disparaître + // les échéances passées — déjà payées — de la table des projections). + const keptEntries = db.prepare(` + SELECT sr.id, sr.numero_echeance FROM simul_remboursements sr + WHERE sr.investissement_id = ? + AND sr.date_prevue < ? + AND EXISTS ( + SELECT 1 FROM remboursements r + WHERE r.investissement_id = sr.investissement_id + AND substr(r.date_remb, 1, 7) = substr(sr.date_prevue, 1, 7) + ) + ORDER BY sr.numero_echeance + `).all(investissementId, inv.date_debut_simul); + + if (keptEntries.length > 0) { + db.prepare( + `DELETE FROM simul_remboursements WHERE investissement_id = ? AND id NOT IN (${keptEntries.map(() => '?').join(',')})` + ).run(investissementId, ...keptEntries.map(e => e.id)); + } else { + db.prepare('DELETE FROM simul_remboursements WHERE investissement_id=?').run(investissementId); + } + + const elapsedMonths = (inv.date_premiere_echeance && inv.date_debut_simul > inv.date_premiere_echeance) + ? monthsDiff(inv.date_premiere_echeance, inv.date_debut_simul) + : keptEntries.length; + + const stmt = db.prepare(` + INSERT INTO simul_remboursements + (investissement_id, numero_echeance, date_prevue, capital_prevu, interets_prevus, total_prevu) + VALUES (?,?,?,?,?,?) + `); + for (const e of schedule) { + stmt.run(investissementId, elapsedMonths + e.n, e.date, e.capital, e.interets, e.total); + } + } else { + // ── Mode standard : régénération complète ──────────────────────────── + db.prepare('DELETE FROM simul_remboursements WHERE investissement_id=?').run(investissementId); + const stmt = db.prepare(` + INSERT INTO simul_remboursements + (investissement_id, numero_echeance, date_prevue, capital_prevu, interets_prevus, total_prevu) + VALUES (?,?,?,?,?,?) + `); + for (const e of schedule) { + stmt.run(investissementId, e.n, e.date, e.capital, e.interets, e.total); + } } })(); }