refactor: Use explicit Sage document type constants for enums, document listing, and transformation endpoints.

This commit is contained in:
Fanilo-Nantenaina 2025-11-28 08:28:59 +03:00
parent 33843e031a
commit b468c963c9
2 changed files with 42 additions and 64 deletions

85
api.py
View file

@ -42,12 +42,13 @@ from sage_client import sage_client
# ENUMS # ENUMS
# ===================================================== # =====================================================
class TypeDocument(int, Enum): class TypeDocument(int, Enum):
DEVIS = 0 DEVIS = settings.SAGE_TYPE_DEVIS
BON_LIVRAISON = 1 BON_COMMANDE = settings.SAGE_TYPE_BON_COMMANDE
BON_RETOUR = 2 PREPARATION = settings.SAGE_TYPE_PREPARATION
COMMANDE = 3 BON_LIVRAISON = settings.SAGE_TYPE_BON_LIVRAISON
PREPARATION = 4 BON_RETOUR = settings.SAGE_TYPE_BON_RETOUR
FACTURE = 5 BON_AVOIR = settings.SAGE_TYPE_BON_AVOIR
FACTURE = settings.SAGE_TYPE_FACTURE
class StatutSignature(str, Enum): class StatutSignature(str, Enum):
@ -515,11 +516,14 @@ async def lister_commandes(
limit: int = Query(100, le=1000), statut: Optional[int] = Query(None) limit: int = Query(100, le=1000), statut: Optional[int] = Query(None)
): ):
""" """
📋 Liste toutes les commandes via gateway Windows 📋 Liste toutes les commandes
CORRECTION : Filtre sur le type 10 (BON_COMMANDE)
""" """
try: try:
# ✅ APPEL VIA SAGE_CLIENT (HTTP vers Windows) # Le sage_client doit filtrer sur type=10, pas type=3
commandes = sage_client.lister_commandes(limit=limit, statut=statut) commandes = sage_client.lister_documents_par_type(
type_doc=settings.SAGE_TYPE_BON_COMMANDE, limit=limit, statut=statut # = 10
)
return commandes return commandes
except Exception as e: except Exception as e:
@ -530,25 +534,22 @@ async def lister_commandes(
@app.post("/workflow/devis/{id}/to-commande", tags=["US-A2"]) @app.post("/workflow/devis/{id}/to-commande", tags=["US-A2"])
async def devis_vers_commande(id: str, session: AsyncSession = Depends(get_session)): async def devis_vers_commande(id: str, session: AsyncSession = Depends(get_session)):
""" """
🔧 Transformation Devis Commande via gateway Windows 🔧 Transformation Devis Commande
CORRECTION : Utilise les VRAIS types Sage (0 10)
CORRECTION: Envoie les valeurs numériques des enums, pas les noms
""" """
try: try:
# ✅ CRITIQUE: Utiliser .value pour obtenir l'entier
resultat = sage_client.transformer_document( resultat = sage_client.transformer_document(
numero_source=id, numero_source=id,
type_source=TypeDocument.DEVIS.value, # ← .value = 0 type_source=settings.SAGE_TYPE_DEVIS, # = 0
type_cible=TypeDocument.COMMANDE.value, # ← .value = 3 type_cible=settings.SAGE_TYPE_BON_COMMANDE, # = 10
) )
# Logger en DB
workflow_log = WorkflowLog( workflow_log = WorkflowLog(
id=str(uuid.uuid4()), id=str(uuid.uuid4()),
document_source=id, document_source=id,
type_source=TypeDocument.DEVIS, type_source=TypeDocument.DEVIS,
document_cible=resultat.get("document_cible", ""), document_cible=resultat.get("document_cible", ""),
type_cible=TypeDocument.COMMANDE, type_cible=TypeDocument.BON_COMMANDE,
nb_lignes=resultat.get("nb_lignes", 0), nb_lignes=resultat.get("nb_lignes", 0),
date_transformation=datetime.now(), date_transformation=datetime.now(),
succes=True, succes=True,
@ -576,52 +577,20 @@ async def devis_vers_commande(id: str, session: AsyncSession = Depends(get_sessi
@app.post("/workflow/commande/{id}/to-facture", tags=["US-A2"]) @app.post("/workflow/commande/{id}/to-facture", tags=["US-A2"])
async def commande_vers_facture(id: str, session: AsyncSession = Depends(get_session)): async def commande_vers_facture(id: str, session: AsyncSession = Depends(get_session)):
""" """
🔧 Transformation Commande Facture via gateway Windows 🔧 Transformation Commande Facture
CORRECTION : Utilise les VRAIS types Sage (10 60)
CORRECTION: Envoie les valeurs numériques
""" """
try: try:
resultat = sage_client.transformer_document( resultat = sage_client.transformer_document(
numero_source=id, numero_source=id,
type_source=TypeDocument.COMMANDE.value, # ← 3 type_source=settings.SAGE_TYPE_BON_COMMANDE, # = 10
type_cible=TypeDocument.FACTURE.value, # ← 5 type_cible=settings.SAGE_TYPE_FACTURE, # = 60
) )
workflow_log = WorkflowLog( workflow_log = WorkflowLog(
id=str(uuid.uuid4()), id=str(uuid.uuid4()),
document_source=id, document_source=id,
type_source=TypeDocument.COMMANDE, type_source=TypeDocument.BON_COMMANDE,
document_cible=resultat.get("document_cible", ""),
type_cible=TypeDocument.FACTURE,
nb_lignes=resultat.get("nb_lignes", 0),
date_transformation=datetime.now(),
succes=True,
)
session.add(workflow_log)
await session.commit()
return resultat
except Exception as e:
logger.error(f"Erreur transformation: {e}")
raise HTTPException(500, str(e))
@app.post("/workflow/commande/{id}/to-facture", tags=["US-A2"])
async def commande_vers_facture(id: str, session: AsyncSession = Depends(get_session)):
"""🔧 Transformation Commande → Facture via gateway Windows"""
try:
resultat = sage_client.transformer_document(
numero_source=id,
type_source=TypeDocument.COMMANDE,
type_cible=TypeDocument.FACTURE,
)
workflow_log = WorkflowLog(
id=str(uuid.uuid4()),
document_source=id,
type_source=TypeDocument.COMMANDE,
document_cible=resultat.get("document_cible", ""), document_cible=resultat.get("document_cible", ""),
type_cible=TypeDocument.FACTURE, type_cible=TypeDocument.FACTURE,
nb_lignes=resultat.get("nb_lignes", 0), nb_lignes=resultat.get("nb_lignes", 0),
@ -1132,11 +1101,13 @@ async def lister_factures(
limit: int = Query(100, le=1000), statut: Optional[int] = Query(None) limit: int = Query(100, le=1000), statut: Optional[int] = Query(None)
): ):
""" """
📋 Liste toutes les factures via gateway Windows 📋 Liste toutes les factures
CORRECTION : Filtre sur le type 60 (FACTURE)
""" """
try: try:
# ✅ APPEL VIA SAGE_CLIENT (HTTP vers Windows) factures = sage_client.lister_documents_par_type(
factures = sage_client.lister_factures(limit=limit, statut=statut) type_doc=settings.SAGE_TYPE_FACTURE, limit=limit, statut=statut # = 60
)
return factures return factures
except Exception as e: except Exception as e:

View file

@ -1,14 +1,20 @@
from pydantic_settings import BaseSettings, SettingsConfigDict from pydantic_settings import BaseSettings, SettingsConfigDict
from typing import List from typing import List
class Settings(BaseSettings): class Settings(BaseSettings):
model_config = SettingsConfigDict( model_config = SettingsConfigDict(
env_file=".env", env_file=".env", env_file_encoding="utf-8", case_sensitive=False, extra="ignore"
env_file_encoding="utf-8",
case_sensitive=False,
extra="ignore"
) )
SAGE_TYPE_DEVIS: int = 0
SAGE_TYPE_BON_COMMANDE: int = 10
SAGE_TYPE_PREPARATION: int = 20
SAGE_TYPE_BON_LIVRAISON: int = 30
SAGE_TYPE_BON_RETOUR: int = 40
SAGE_TYPE_BON_AVOIR: int = 50
SAGE_TYPE_FACTURE: int = 60
# === Sage Gateway (Windows) === # === Sage Gateway (Windows) ===
sage_gateway_url: str sage_gateway_url: str
sage_gateway_token: str sage_gateway_token: str
@ -40,4 +46,5 @@ class Settings(BaseSettings):
# === CORS === # === CORS ===
cors_origins: List[str] = ["*"] cors_origins: List[str] = ["*"]
settings = Settings() settings = Settings()