---
suivi: 1344
date: 2026-08-14
sujet: fix_reprise_retenues_bilan
chantier: marches_reprise
type: feat
statut: poussé
hash: 2216a040
fichiers:
  - app/Services/MarcheRepriseMontantsService.php
  - app/Exceptions/MarcheRepriseHistoriqueBloqueException.php
  - app/Http/Controllers/MarcheRepriseLigneController.php
  - app/Services/SiwiRepriseActionService.php
  - app/Http/Controllers/SiwiImportController.php
  - app/Services/MarcheRetenuesEtatsService.php
  - app/Services/MarcheCouvertureKpiService.php
  - app/Services/MarcheSyntheseFinanciereService.php
  - app/Http/Controllers/MarcheController.php
  - app/Http/Controllers/ProgrammeDashboardController.php
  - app/Services/BilanFinancierService.php
  - resources/js/Pages/Marches/Show.vue
  - tools/diag/diag_reprise_fix_1344.php
  - database/data/journal_mises_a_jour_post_2026_06_12.php
  - public/build/
  - docs/suivi/SUIVI_1344_fix_reprise_retenues_bilan.md
---

## PROMPT ENVOYÉ

SUIVI #1344 PHASE 2 FIX — conclusions #1343 acquises. Option C (interdire
reprise si dépôts historiques) + option A (lecture directe via
MarcheRepriseMontantsService étendu). Ne pas toucher
buildMarchesKpiProgramme:1151. Retenues reprise déjà HT : ne pas
reconvertir. Aucune migration. Script preuve en transaction + rollback.

## FICHES LUES

- `SUIVI_1343` LEÇON : grep BilanFinancierService sur marche_reprise_lignes
  est silencieux et faux ; facture_valide passe par repartitionKpiMarche
  qui inclut déjà montant_paye_ht. Piège : ré-ajouter le payé.
- `SUIVI_1320` LEÇON : plafond = etats.rg.montant_retenu (MRES). Brancher
  la RG reprise dans MRES suffit pour le plafond.
- `SUIVI_1323` / `#1304` : montant_rg_reprise_ht déjà HT ;
  retenues_garantie.montant_retenu stocké TTC, converti à la lecture.
- `SUIVI_1347` / `#1342` : Marches/Index.vue non touché. MarcheController
  computeKpis oui (consommateur listé).
- `SUIVI_1340` / `#1341` : non touchés.

## SYNTHÈSE

`buildMarchesKpiProgramme:1151` n’a PAS été modifié. Le payé repris reste
compté une seule fois via repartitionKpiMarche. Seul
`buildFactureNv1ByMarche` (colonne en attente) additionne
max(0, facture_ht − paye_ht).

### Mapping consommateurs

| Consommateur | AVANT | APRÈS | Unité | Corrigé |
|---|---|---|---|---|
| MRES::pourMarche | NON retenues reprise | OUI rg/cp/cie/finitions HT additionnés APRÈS ttcVersHt | HT | O |
| plafondPourMarche | via MRES | via MRES (inchangé, hérite) | HT | O (indirect) |
| CouvertureKpi::calculer | NON colonnes reprise | OUI rg/cp/cie/finitions HT bruts | HT (reprise) | O |
| MSFS::pourMarche | NON facture/paye | OUI facture + paye ; impayé → en circuit | HT | O |
| computeKpis | NON | OUI facture_ht + paye_ht programme | HT | O |
| ProgrammeDashboardController | NON | OUI facture_ht programme | HT | O |
| buildFactureNv1ByMarche | NON | OUI impayé repris | HT | O |
| buildMarchesKpiProgramme:1151 | OUI paye (indirect) | inchangé | HT | N (volontaire) |

### Décision 1
Garde-fou serveur : `assertCreationRepriseAutorisee` (saisie_historique=1
OU type_source=historique, scope pourMarche). store → 422.
SiwiRepriseActionService avant l’INSERT ~349. Lot : skipped listés,
jamais écrasés. Dépôts historiques non modifiés.

### Décision 2
MarcheRepriseMontantsService somme aussi rg/cp/cie/finitions
(COALESCE, NULL=0). Pas de deleted_at sur reprise.

### Décision 3
Notice Show.vue alignée sur le code réel.

### Dettes signalées
- `marches.total_facture_ht` cache toujours stale (reprise n’appelle
  pas recalculer — conservé).
- Réciproque non posée : on n’interdit pas une saisie historique APRÈS
  une reprise.
- `rattacherMarcheExistant` n’écrit pas de ligne de reprise.

## DÉPLOIEMENT-TEST

```bash
git pull origin main
php artisan optimize:clear && php artisan view:clear
php artisan journal:sync
```

Pas de migrate. Pas de route:clear.

```bash
cd /home/hectare-envol/envol.hectare.fr && php -d display_errors=1 tools/diag/diag_reprise_fix_1344.php
```

## LEÇON

Ajouter la reprise au bilan « réel » recompte montant_paye_ht. Le trou
vrai était les colonnes *_reprise_ht hors MRES / plafond. Brancher MRES
AVANT conversion TTC→HT = ÷1,2. Additionner APRÈS, en HT natif.
