Application : dirCops
Date : 2026-02-09
Objectif : Définir la logique métier et technique pour les paiements et écheanciers, inspirée des bonnes pratiques ERP tout en restant simple.
montant_ht - SUM(paiements.montant).encours <= 0 → Payée, sinon → En attente).montant > 0.facture_id obligatoire.SUM(paiements) > montant_ht, l'encours = 0 (pas de crédit client géré).encours_actuel / nb_echeances.encours_facture = GREATEST(0, montant_ht - COALESCE(SUM(paiements.montant), 0))
Explication :
GREATEST(0, ...) : garantit que l'encours ne soit jamais négatif.COALESCE(..., 0) : si aucun paiement, somme = 0.encours_client = SUM(encours_facture) pour toutes les factures du client
| Cas | Comportement |
|---|---|
| Paiement supérieur au montant HT | Encours = 0 (pas de crédit) |
| Plusieurs paiements partiels | Encours = montant_ht - somme des paiements |
| Aucun paiement | Encours = montant_ht |
| Arrondis décimaux | Utilisation de ROUND() pour éviter les écarts |
facture_id, nb_echeances, date_debut_echeancier, montant_par_echeance (calculé et stocké).Au moment de la création de l'écheancier :
montant_par_echeance = ROUND(encours_actuel / nb_echeances, 2)
Exemple :
Les dates sont calculées à l'affichage :
date_debut_echeancierdate_debut_echeancier + 1 moisdate_debut_echeancier + 2 moisFréquence : mensuelle (toujours).
Un écheancier ne peut être créé que si :
Le montant par échéance est calculé au moment de la création :
echeanciers.montant_par_echeance pour éviter de recalculer.| Table | Contrainte | Détail |
|---|---|---|
paiements |
montant > 0 |
Un paiement doit être positif |
paiements |
facture_id FK |
Référence facture obligatoire |
echeanciers |
facture_id UNIQUE |
Une facture = un écheancier max |
echeanciers |
nb_echeances > 0 |
Au moins une échéance |
echeanciers |
montant_par_echeance > 0 |
Montant positif |
CREATE INDEX idx_paiements_facture_id ON paiements(facture_id);
CREATE INDEX idx_paiements_date ON paiements(date_paiement);
CREATE UNIQUE INDEX idx_echeanciers_facture_id ON echeanciers(facture_id);
Table echeanciers :
montant_par_echeance DECIMAL(12,2) : montant calculé au moment de la création.Avant insertion :
montant > 0.date_paiement est valide.Après insertion :
Avant insertion :
encours > 0.montant_par_echeance = ROUND(encours / nb_echeances, 2).Insertion :
INSERT INTO echeanciers (facture_id, nb_echeances, date_debut_echeancier, montant_par_echeance)
VALUES (?, ?, ?, ?)
En SQL :
SELECT
f.id,
f.montant_ht,
COALESCE(SUM(p.montant), 0) AS total_paye,
GREATEST(0, f.montant_ht - COALESCE(SUM(p.montant), 0)) AS encours_facture,
CASE
WHEN GREATEST(0, f.montant_ht - COALESCE(SUM(p.montant), 0)) <= 0 THEN 'paye'
ELSE 'en_attente'
END AS etat
FROM factures f
LEFT JOIN paiements p ON p.facture_id = f.id
WHERE f.id = ?
GROUP BY f.id, f.montant_ht
En PHP (mock) :
function calculerEncoursFacture($factureId, $factures, $paiements) {
$f = array_filter($factures, fn($x) => $x['id'] == $factureId)[0] ?? null;
if (!$f) return null;
$totalPaye = array_sum(array_map(
fn($p) => (float)$p['montant'],
array_filter($paiements, fn($p) => $p['facture_id'] == $factureId)
));
$encours = max(0, (float)$f['montant_ht'] - $totalPaye);
return [
'montant_ht' => (float)$f['montant_ht'],
'total_paye' => $totalPaye,
'encours_facture' => $encours,
'etat' => $encours <= 0 ? 'paye' : 'en_attente'
];
}
| Vérification | Quand | Action si échec |
|---|---|---|
| Facture existe | Avant paiement/écheancier | Erreur 404 |
| Montant paiement > 0 | Avant insertion paiement | Erreur 400 |
| Encours > 0 | Avant création écheancier | Erreur 400 "Facture déjà soldée" |
| Pas d'écheancier existant | Avant création écheancier | Erreur 400 "Écheancier déjà existant" |
DECIMAL(12,2) (2 décimales).ROUND() pour éviter les écarts.<= 0.01 pour considérer comme nul (tolérance d'arrondi).Les « échéances à venir » affichées sur le tableau de bord sont calculées à partir des écheanciers lorsque la facture en possède un, sinon à partir de la date de facture.
date_debut_echeancier + i moismontant_par_echeance (stocké dans l’écheancier)SELECT
f.*,
COALESCE(SUM(p.montant), 0) AS total_paye,
GREATEST(0, f.montant_ht - COALESCE(SUM(p.montant), 0)) AS encours_facture,
CASE
WHEN GREATEST(0, f.montant_ht - COALESCE(SUM(p.montant), 0)) <= 0 THEN 'paye'
ELSE 'en_attente'
END AS etat
FROM factures f
LEFT JOIN paiements p ON p.facture_id = f.id
WHERE f.client_id = ?
GROUP BY f.id
ORDER BY f.date_facture DESC
SELECT
c.id AS client_id,
c.nom,
COALESCE(SUM(GREATEST(0, f.montant_ht - COALESCE(sp.total_paye, 0))), 0) AS encours
FROM clients c
LEFT JOIN factures f ON f.client_id = c.id
LEFT JOIN (
SELECT facture_id, SUM(montant) AS total_paye
FROM paiements
GROUP BY facture_id
) sp ON sp.facture_id = f.id
GROUP BY c.id, c.nom
HAVING encours > 0
ORDER BY encours ASC
Document de référence pour l'implémentation. À mettre à jour si la logique métier évolue.