mode règlement avec regroupement par règlement (je crois)

This commit is contained in:
fanilo 2026-01-16 15:19:31 +01:00
parent 5ad54a2ff0
commit 06a5fc8df4

View file

@ -175,6 +175,7 @@ def lire_tous_reglements(
type_reglement: str = None,
limit: int = 500,
) -> Dict:
"""Liste tous les règlements avec TOUTES les colonnes F_CREGLEMENT"""
if not self.cial:
raise RuntimeError("Connexion Sage non établie")
@ -188,40 +189,60 @@ def lire_tous_reglements(
query = """
SELECT
r.RG_No,
r.CT_NumPayeur,
r.RG_Date,
r.RG_Reference,
r.RG_Libelle,
r.RG_Montant,
r.RG_MontantDev,
r.N_Reglement,
r.RG_Impute,
r.RG_Compta,
r.EC_No,
r.RG_Type,
r.RG_Cours,
r.RG_Banque,
r.RG_Impaye,
r.RG_MontantEcart,
r.CT_NumPayeur,
r.N_Devise,
r.JO_Num,
r.RG_Piece,
r.N_Reglement,
r.CG_NumCont,
r.RG_Impaye,
r.CG_Num,
r.RG_TypeReg,
r.RG_Heure,
r.RG_Piece,
r.CA_No,
r.CO_NoCaissier,
r.RG_Banque,
r.RG_Transfere,
r.RG_Cloture,
r.RG_Ticket,
r.RG_Souche,
r.CT_NumPayeurOrig,
r.RG_DateEchCont,
r.CG_NumEcart,
r.JO_NumEcart,
r.RG_MontantEcart,
r.RG_NoBonAchat,
r.RG_Valide,
r.RG_Anterieur,
r.RG_MontantCommission,
r.RG_MontantNet,
r.cbCreation,
r.cbModification,
r.cbCreateur,
c.CT_Intitule,
j.JO_Intitule,
mr.R_Intitule as ModeReglementIntitule,
(SELECT ISNULL(SUM(RE_Montant), 0) FROM F_REGLECH WHERE RG_No = r.RG_No) as MontantImpute
dev.D_Intitule as DeviseIntitule,
(SELECT ISNULL(SUM(RC_Montant), 0) FROM F_REGLECH WHERE RG_No = r.RG_No) as MontantImpute
FROM F_CREGLEMENT r
LEFT JOIN F_COMPTET c ON r.CT_NumPayeur = c.CT_Num
LEFT JOIN F_JOURNAUX j ON r.JO_Num = j.JO_Num
LEFT JOIN P_REGLEMENT mr ON r.N_Reglement = mr.cbIndice
LEFT JOIN P_DEVISE dev ON r.N_Devise = dev.cbIndice
WHERE 1=1
"""
params = []
# Filtrer par type (0 = client, 1 = fournisseur)
if type_reglement:
if type_reglement.lower() == "client":
query += " AND r.RG_Type = 0"
@ -240,7 +261,7 @@ def lire_tous_reglements(
query += " AND r.CT_NumPayeur = ?"
params.append(client_code)
query += f" ORDER BY r.RG_Date DESC, r.RG_No DESC"
query += " ORDER BY r.RG_Date DESC, r.RG_No DESC"
if limit:
query = query.replace("SELECT", f"SELECT TOP {limit}")
@ -249,10 +270,12 @@ def lire_tous_reglements(
rows = cursor.fetchall()
types_reglement = {0: "Client", 1: "Fournisseur", 2: "Salarié"}
statuts_impute = {
0: "Non imputé",
1: "Partiellement imputé",
2: "Totalement imputé",
types_reg = {
0: "Aucun",
1: "Règlement",
2: "Escompte accordé",
3: "Effet encaissé",
4: "Effet impayé",
}
reglements = []
@ -260,12 +283,10 @@ def lire_tous_reglements(
total_impute = 0.0
for row in rows:
rg_no = row[0]
rg_montant = float(row[4] or 0)
montant_impute = float(row[25] or 0)
rg_montant = float(row[5] or 0)
montant_impute = float(row[45] or 0)
reste_a_imputer = rg_montant - montant_impute
# Déterminer le statut d'imputation
if montant_impute == 0:
statut_imputation = "Non imputé"
elif abs(reste_a_imputer) < 0.01:
@ -273,79 +294,141 @@ def lire_tous_reglements(
else:
statut_imputation = "Partiellement imputé"
heure_str = None
if row[19]:
try:
heure_str = (
row[19].strftime("%H:%M:%S")
if hasattr(row[19], "strftime")
else str(row[19])
)
except:
heure_str = str(row[19])
reglement = {
"rg_no": rg_no,
"numero_piece": (row[15] or "").strip(),
"date": row[1].strftime("%Y-%m-%d") if row[1] else None,
"reference": (row[2] or "").strip(),
"libelle": (row[3] or "").strip(),
"rg_no": row[0],
"numero_piece": (row[20] or "").strip(),
"date": row[2].strftime("%Y-%m-%d") if row[2] else None,
"heure": heure_str,
"date_echeance_contrepartie": row[29].strftime("%Y-%m-%d")
if row[29]
else None,
"reference": (row[3] or "").strip(),
"libelle": (row[4] or "").strip(),
"montant": rg_montant,
"montant_devise": float(row[5] or 0),
"montant_devise": float(row[6] or 0),
"montant_impute": montant_impute,
"reste_a_imputer": reste_a_imputer,
"montant_ecart": float(row[32] or 0),
"montant_commission": float(row[36] or 0),
"montant_net": float(row[37] or 0),
"cours": float(row[12] or 1),
"statut_imputation": statut_imputation,
"type_code": row[8],
"type_libelle": types_reglement.get(row[8], f"Type {row[8]}"),
"client_code": (row[13] or "").strip(),
"client_intitule": (row[22] or "").strip(),
"est_impute": row[8] == 1,
"est_comptabilise": row[9] == 1,
"est_impaye": row[16] == 1,
"est_transfere": row[24] == 1,
"est_cloture": row[25] == 1,
"est_valide": row[34] == 1,
"est_anterieur": row[35] == 1,
"type_code": row[11],
"type_libelle": types_reglement.get(row[11], f"Type {row[11]}"),
"type_reg_code": row[18],
"type_reg_libelle": types_reg.get(row[18], f"TypeReg {row[18]}"),
"client_code": (row[1] or "").strip(),
"client_intitule": (row[41] or "").strip(),
"client_origine": (row[28] or "").strip(),
"journal_code": (row[14] or "").strip(),
"journal_intitule": (row[23] or "").strip(),
"mode_reglement_code": row[16],
"journal_intitule": (row[42] or "").strip(),
"compte_general": (row[17] or "").strip(),
"compte_contrepartie": (row[15] or "").strip(),
"compte_ecart": (row[30] or "").strip(),
"journal_ecart": (row[31] or "").strip(),
"mode_reglement_code": row[7],
"mode_reglement_libelle": (
row[24] or ModeReglement.get_libelle(row[16] or 0)
).strip(),
"compte_contrepartie": (row[17] or "").strip(),
"cours": float(row[9] or 1),
"banque": (row[10] or "").strip(),
"impaye": row[11] == 1,
"ecart": float(row[12] or 0),
"cloture": row[18] == 1,
"ticket": (row[19] or "").strip(),
"date_creation": row[20].strftime("%Y-%m-%d %H:%M:%S")
if row[20]
row[43] or ModeReglement.get_libelle(row[7] or 0)
).strip()
if row[43]
else ModeReglement.get_libelle(row[7] or 0),
"devise_code": row[13],
"devise_intitule": (row[44] or "").strip(),
"banque": (row[23] or "").strip(),
"caisse_no": row[21],
"caissier_no": row[22],
"echeancier_no": row[10],
"ticket": (row[26] or "").strip(),
"souche": row[27],
"bon_achat_no": row[33],
"date_creation": row[38].strftime("%Y-%m-%d %H:%M:%S")
if row[38]
else None,
"date_modification": row[21].strftime("%Y-%m-%d %H:%M:%S")
if row[21]
"date_modification": row[39].strftime("%Y-%m-%d %H:%M:%S")
if row[39]
else None,
"createur": (row[40] or "").strip() if row[40] else None,
}
reglements.append(reglement)
total_montant += rg_montant
total_impute += montant_impute
# Récupérer les détails d'imputation pour chaque règlement
for reg in reglements:
cursor.execute(
"""
SELECT
re.RE_Montant,
re.RE_MontantDev,
e.DR_No,
d.DO_Piece,
re.RG_No,
re.RC_Montant,
re.DR_No,
dr.DO_Domaine,
dr.DO_Type,
dr.DO_Piece,
d.DO_Date,
d.DO_TotalTTC
d.DO_TotalTTC,
d.DO_Ref
FROM F_REGLECH re
LEFT JOIN F_DOCREGL e ON re.DR_No = e.DR_No
LEFT JOIN F_DOCENTETE d ON e.DO_Domaine = d.DO_Domaine AND e.DO_Type = d.DO_Type AND e.DO_Piece = d.DO_Piece
LEFT JOIN F_DOCREGL dr ON re.DR_No = dr.DR_No
AND re.DO_Domaine = dr.DO_Domaine
AND re.DO_Type = dr.DO_Type
AND re.DO_Piece = dr.DO_Piece
LEFT JOIN F_DOCENTETE d ON dr.DO_Domaine = d.DO_Domaine
AND dr.DO_Type = d.DO_Type
AND dr.DO_Piece = d.DO_Piece
WHERE re.RG_No = ?
""",
(reg["rg_no"],),
)
imputations = cursor.fetchall()
types_doc = {
0: "Devis",
1: "Bon de commande",
2: "Préparation",
3: "Bon de livraison",
6: "Facture",
7: "Avoir",
}
domaines = {0: "Vente", 1: "Achat", 2: "Stock"}
reg["imputations"] = [
{
"montant": float(imp[0] or 0),
"montant_devise": float(imp[1] or 0),
"document_piece": (imp[3] or "").strip() if imp[3] else None,
"document_date": imp[4].strftime("%Y-%m-%d") if imp[4] else None,
"document_total": float(imp[5] or 0) if imp[5] else None,
"rg_no": imp[0],
"montant": float(imp[1] or 0),
"dr_no": imp[2],
"document_domaine": domaines.get(imp[3], f"Domaine {imp[3]}")
if imp[3] is not None
else None,
"document_type": types_doc.get(imp[4], f"Type {imp[4]}")
if imp[4] is not None
else None,
"document_piece": (imp[5] or "").strip() if imp[5] else None,
"document_date": imp[6].strftime("%Y-%m-%d") if imp[6] else None,
"document_total": float(imp[7] or 0) if imp[7] else None,
"document_reference": (imp[8] or "").strip() if imp[8] else None,
}
for imp in imputations
]
reg["nb_imputations"] = len(imputations)
# Statistiques
nb_total = len(reglements)
nb_imputes = sum(
1 for r in reglements if r["statut_imputation"] == "Totalement imputé"
@ -387,40 +470,92 @@ def lire_reglement_detail(self, rg_no: int) -> Dict:
cursor.execute(
"""
SELECT
r.RG_No, r.RG_Date, r.RG_Reference, r.RG_Libelle,
r.RG_Montant, r.RG_MontantDev, r.RG_Impute, r.RG_Compta,
r.RG_Type, r.RG_Cours, r.RG_Banque, r.RG_Impaye,
r.RG_MontantEcart, r.CT_NumPayeur, r.JO_Num, r.RG_Piece,
r.N_Reglement, r.CG_NumCont, r.RG_Cloture, r.RG_Ticket,
r.cbCreation, r.cbModification,
c.CT_Intitule, j.JO_Intitule, mr.R_Intitule
r.RG_No,
r.CT_NumPayeur,
r.RG_Date,
r.RG_Reference,
r.RG_Libelle,
r.RG_Montant,
r.RG_MontantDev,
r.N_Reglement,
r.RG_Impute,
r.RG_Compta,
r.EC_No,
r.RG_Type,
r.RG_Cours,
r.N_Devise,
r.JO_Num,
r.CG_NumCont,
r.RG_Impaye,
r.CG_Num,
r.RG_TypeReg,
r.RG_Heure,
r.RG_Piece,
r.CA_No,
r.CO_NoCaissier,
r.RG_Banque,
r.RG_Transfere,
r.RG_Cloture,
r.RG_Ticket,
r.RG_Souche,
r.CT_NumPayeurOrig,
r.RG_DateEchCont,
r.CG_NumEcart,
r.JO_NumEcart,
r.RG_MontantEcart,
r.RG_NoBonAchat,
r.RG_Valide,
r.RG_Anterieur,
r.RG_MontantCommission,
r.RG_MontantNet,
r.cbCreation,
r.cbModification,
r.cbCreateur,
c.CT_Intitule,
j.JO_Intitule,
mr.R_Intitule,
dev.D_Intitule
FROM F_CREGLEMENT r
LEFT JOIN F_COMPTET c ON r.CT_NumPayeur = c.CT_Num
LEFT JOIN F_JOURNAUX j ON r.JO_Num = j.JO_Num
LEFT JOIN P_REGLEMENT mr ON r.N_Reglement = mr.cbIndice
LEFT JOIN P_DEVISE dev ON r.N_Devise = dev.cbIndice
WHERE r.RG_No = ?
""",
(rg_no,),
)
row = cursor.fetchone()
row = cursor.fetchone()
if not row:
raise ValueError(f"Règlement {rg_no} introuvable")
# Imputations
cursor.execute(
"""
SELECT
re.RE_No, re.RE_Montant, re.RE_MontantDev, re.DR_No,
e.DO_Domaine, e.DO_Type, e.DO_Piece,
d.DO_Date, d.DO_TotalTTC, d.DO_MontantRegle, d.DO_Ref
re.RG_No,
re.RC_Montant,
re.DR_No,
dr.DO_Domaine,
dr.DO_Type,
dr.DO_Piece,
d.DO_Date,
d.DO_TotalTTC,
d.DO_MontantRegle,
d.DO_Ref,
d.DO_Tiers
FROM F_REGLECH re
LEFT JOIN F_DOCREGL e ON re.DR_No = e.DR_No
LEFT JOIN F_DOCENTETE d ON e.DO_Domaine = d.DO_Domaine AND e.DO_Type = d.DO_Type AND e.DO_Piece = d.DO_Piece
LEFT JOIN F_DOCREGL dr ON re.DR_No = dr.DR_No
AND re.DO_Domaine = dr.DO_Domaine
AND re.DO_Type = dr.DO_Type
AND re.DO_Piece = dr.DO_Piece
LEFT JOIN F_DOCENTETE d ON dr.DO_Domaine = d.DO_Domaine
AND dr.DO_Type = d.DO_Type
AND dr.DO_Piece = d.DO_Piece
WHERE re.RG_No = ?
""",
(rg_no,),
)
imputations_rows = cursor.fetchall()
types_doc = {
@ -432,6 +567,14 @@ def lire_reglement_detail(self, rg_no: int) -> Dict:
7: "Avoir",
}
domaines = {0: "Vente", 1: "Achat", 2: "Stock"}
types_reglement = {0: "Client", 1: "Fournisseur", 2: "Salarié"}
types_reg = {
0: "Aucun",
1: "Règlement",
2: "Escompte accordé",
3: "Effet encaissé",
4: "Effet impayé",
}
imputations = []
total_impute = 0.0
@ -440,65 +583,106 @@ def lire_reglement_detail(self, rg_no: int) -> Dict:
total_impute += montant_imp
imputations.append(
{
"re_no": imp[0],
"rg_no": imp[0],
"montant": montant_imp,
"montant_devise": float(imp[2] or 0),
"dr_no": imp[2],
"document": {
"domaine": domaines.get(imp[4], f"Domaine {imp[4]}")
"domaine": domaines.get(imp[3], f"Domaine {imp[3]}")
if imp[3] is not None
else None,
"type": types_doc.get(imp[4], f"Type {imp[4]}")
if imp[4] is not None
else None,
"type": types_doc.get(imp[5], f"Type {imp[5]}")
if imp[5] is not None
else None,
"numero": (imp[6] or "").strip() if imp[6] else None,
"date": imp[7].strftime("%Y-%m-%d") if imp[7] else None,
"total_ttc": float(imp[8] or 0) if imp[8] else None,
"montant_regle": float(imp[9] or 0) if imp[9] else None,
"reference": (imp[10] or "").strip() if imp[10] else None,
"numero": (imp[5] or "").strip() if imp[5] else None,
"date": imp[6].strftime("%Y-%m-%d") if imp[6] else None,
"total_ttc": float(imp[7] or 0) if imp[7] else None,
"montant_regle": float(imp[8] or 0) if imp[8] else None,
"reference": (imp[9] or "").strip() if imp[9] else None,
"tiers": (imp[10] or "").strip() if imp[10] else None,
},
}
)
rg_montant = float(row[4] or 0)
rg_montant = float(row[5] or 0)
reste = rg_montant - total_impute
types_reglement = {0: "Client", 1: "Fournisseur", 2: "Salarié"}
heure_str = None
if row[19]:
try:
heure_str = (
row[19].strftime("%H:%M:%S")
if hasattr(row[19], "strftime")
else str(row[19])
)
except:
heure_str = str(row[19])
if total_impute == 0:
statut_imputation = "Non imputé"
elif abs(reste) < 0.01:
statut_imputation = "Totalement imputé"
else:
statut_imputation = "Partiellement imputé"
return {
"rg_no": row[0],
"numero_piece": (row[15] or "").strip(),
"date": row[1].strftime("%Y-%m-%d") if row[1] else None,
"reference": (row[2] or "").strip(),
"libelle": (row[3] or "").strip(),
"numero_piece": (row[20] or "").strip(),
"date": row[2].strftime("%Y-%m-%d") if row[2] else None,
"heure": heure_str,
"date_echeance_contrepartie": row[29].strftime("%Y-%m-%d")
if row[29]
else None,
"reference": (row[3] or "").strip(),
"libelle": (row[4] or "").strip(),
"montant": rg_montant,
"montant_devise": float(row[5] or 0),
"montant_devise": float(row[6] or 0),
"montant_impute": total_impute,
"reste_a_imputer": reste,
"statut_imputation": "Totalement imputé"
if abs(reste) < 0.01
else ("Partiellement imputé" if total_impute > 0 else "Non imputé"),
"est_comptabilise": row[7] == 1,
"type_code": row[8],
"type_libelle": types_reglement.get(row[8], f"Type {row[8]}"),
"client_code": (row[13] or "").strip(),
"client_intitule": (row[22] or "").strip(),
"montant_ecart": float(row[32] or 0),
"montant_commission": float(row[36] or 0),
"montant_net": float(row[37] or 0),
"cours": float(row[12] or 1),
"statut_imputation": statut_imputation,
"est_impute": row[8] == 1,
"est_comptabilise": row[9] == 1,
"est_impaye": row[16] == 1,
"est_transfere": row[24] == 1,
"est_cloture": row[25] == 1,
"est_valide": row[34] == 1,
"est_anterieur": row[35] == 1,
"type_code": row[11],
"type_libelle": types_reglement.get(row[11], f"Type {row[11]}"),
"type_reg_code": row[18],
"type_reg_libelle": types_reg.get(row[18], f"TypeReg {row[18]}"),
"client_code": (row[1] or "").strip(),
"client_intitule": (row[41] or "").strip(),
"client_origine": (row[28] or "").strip(),
"journal_code": (row[14] or "").strip(),
"journal_intitule": (row[23] or "").strip(),
"mode_reglement_code": row[16],
"journal_intitule": (row[42] or "").strip(),
"compte_general": (row[17] or "").strip(),
"compte_contrepartie": (row[15] or "").strip(),
"compte_ecart": (row[30] or "").strip(),
"journal_ecart": (row[31] or "").strip(),
"mode_reglement_code": row[7],
"mode_reglement_libelle": (
row[24] or ModeReglement.get_libelle(row[16] or 0)
).strip(),
"compte_contrepartie": (row[17] or "").strip(),
"cours": float(row[9] or 1),
"banque": (row[10] or "").strip(),
"impaye": row[11] == 1,
"ecart": float(row[12] or 0),
"cloture": row[18] == 1,
"ticket": (row[19] or "").strip(),
"date_creation": row[20].strftime("%Y-%m-%d %H:%M:%S") if row[20] else None,
"date_modification": row[21].strftime("%Y-%m-%d %H:%M:%S")
if row[21]
row[43] or ModeReglement.get_libelle(row[7] or 0)
).strip()
if row[43]
else ModeReglement.get_libelle(row[7] or 0),
"devise_code": row[13],
"devise_intitule": (row[44] or "").strip() if row[44] else "",
"banque": (row[23] or "").strip(),
"caisse_no": row[21],
"caissier_no": row[22],
"echeancier_no": row[10],
"ticket": (row[26] or "").strip(),
"souche": row[27],
"bon_achat_no": row[33],
"date_creation": row[38].strftime("%Y-%m-%d %H:%M:%S") if row[38] else None,
"date_modification": row[39].strftime("%Y-%m-%d %H:%M:%S")
if row[39]
else None,
"createur": (row[40] or "").strip() if row[40] else None,
"nb_imputations": len(imputations),
"imputations": imputations,
}