fix(api): normalize document types and fix status change validation

This commit is contained in:
Fanilo-Nantenaina 2025-12-30 12:48:45 +03:00
parent e85c6560be
commit 6eba416f72

32
api.py
View file

@ -830,30 +830,38 @@ async def changer_statut_document(
): ):
document_type_sql = None document_type_sql = None
document_type_code = None document_type_code = None
type_doc_normalized = None
try: try:
match type_doc: match type_doc:
case 0: case 0:
document_type_sql = TypeDocumentSQL.DEVIS document_type_sql = TypeDocumentSQL.DEVIS
document_type_code = TypeDocument.DEVIS document_type_code = TypeDocument.DEVIS
type_doc_normalized = 0
case 10 | 1: case 10 | 1:
document_type_sql = TypeDocumentSQL.BON_COMMANDE document_type_sql = TypeDocumentSQL.BON_COMMANDE
document_type_code = TypeDocument.BON_COMMANDE document_type_code = TypeDocument.BON_COMMANDE
type_doc_normalized = 10
case 20 | 2: case 20 | 2:
document_type_sql = TypeDocumentSQL.PREPARATION document_type_sql = TypeDocumentSQL.PREPARATION
document_type_code = TypeDocument.PREPARATION document_type_code = TypeDocument.PREPARATION
type_doc_normalized = 20
case 30 | 3: case 30 | 3:
document_type_sql = TypeDocumentSQL.BON_LIVRAISON document_type_sql = TypeDocumentSQL.BON_LIVRAISON
document_type_code = TypeDocument.BON_LIVRAISON document_type_code = TypeDocument.BON_LIVRAISON
type_doc_normalized = 30
case 40 | 4: case 40 | 4:
document_type_sql = TypeDocumentSQL.BON_RETOUR document_type_sql = TypeDocumentSQL.BON_RETOUR
document_type_code = TypeDocument.BON_RETOUR document_type_code = TypeDocument.BON_RETOUR
type_doc_normalized = 40
case 50 | 5: case 50 | 5:
document_type_sql = TypeDocumentSQL.BON_AVOIR document_type_sql = TypeDocumentSQL.BON_AVOIR
document_type_code = TypeDocument.BON_AVOIR document_type_code = TypeDocument.BON_AVOIR
type_doc_normalized = 50
case 60 | 6: case 60 | 6:
document_type_sql = TypeDocumentSQL.FACTURE document_type_sql = TypeDocumentSQL.FACTURE
document_type_code = TypeDocument.FACTURE document_type_code = TypeDocument.FACTURE
type_doc_normalized = 60
case _: case _:
raise HTTPException( raise HTTPException(
400, 400,
@ -876,26 +884,38 @@ async def changer_statut_document(
f"et ne peut plus changer de statut", f"et ne peut plus changer de statut",
) )
case 10 | 1 | 20 | 2 | 30 | 3 | 40 |4 |50 | 5 | 60 | 6: case 10 | 1 | 20 | 2 | 30 | 3 | 40 | 4 | 50 | 5 | 60 | 6:
if statut_actuel >= 2: if statut_actuel >= 2:
type_names = { type_names = {
10: "la commande", 20: "la préparation", 30: "la livraison", 10: "la commande", 1: "la commande",
40: "le retour", 50: "l'avoir", 60: "la facture" 20: "la préparation", 2: "la préparation",
30: "la livraison", 3: "la livraison",
40: "le retour", 4: "le retour",
50: "l'avoir", 5: "l'avoir",
60: "la facture", 6: "la facture"
} }
raise HTTPException( raise HTTPException(
400, 400,
f"Le document {numero} ({type_names.get(document_type_sql, 'document')}) " f"Le document {numero} ({type_names.get(type_doc, 'document')}) "
f"ne peut plus changer de statut (statut actuel ≥ 2)", f"ne peut plus changer de statut (statut actuel ≥ 2)",
) )
resultat = sage_client.changer_statut_document(numero,document_type_code, nouveau_statut) # CORRECTION ICI : utiliser les bons paramètres dans le bon ordre
# Et convertir l'enum en int avec .value si nécessaire
document_type_int = document_type_code.value if hasattr(document_type_code, 'value') else type_doc_normalized
resultat = sage_client.changer_statut_document(
document_type_code=document_type_int,
numero=numero,
nouveau_statut=nouveau_statut
)
logger.info(f"Statut document {numero} changé: {statut_actuel}{nouveau_statut}") logger.info(f"Statut document {numero} changé: {statut_actuel}{nouveau_statut}")
return { return {
"success": True, "success": True,
"document_id": numero, "document_id": numero,
"type_document_code": document_type_code, "type_document_code": document_type_int,
"type_document_sql": str(document_type_sql), "type_document_sql": str(document_type_sql),
"statut_ancien": resultat.get("statut_ancien", statut_actuel), "statut_ancien": resultat.get("statut_ancien", statut_actuel),
"statut_nouveau": resultat.get("statut_nouveau", nouveau_statut), "statut_nouveau": resultat.get("statut_nouveau", nouveau_statut),