refactor: Use specialized Sage client methods for listing commands and invoices, and enhance the document transformation response.

This commit is contained in:
Fanilo-Nantenaina 2025-11-28 08:52:55 +03:00
parent b468c963c9
commit 3f8238f674
2 changed files with 31 additions and 19 deletions

34
api.py
View file

@ -516,14 +516,13 @@ async def lister_commandes(
limit: int = Query(100, le=1000), statut: Optional[int] = Query(None)
):
"""
📋 Liste toutes les commandes
CORRECTION : Filtre sur le type 10 (BON_COMMANDE)
📋 Liste toutes les commandes via gateway Windows
CORRECTION : Utilise sage_client.lister_commandes() qui existe déjà
Le filtrage sur type 10 est fait côté Windows dans main.py
"""
try:
# Le sage_client doit filtrer sur type=10, pas type=3
commandes = sage_client.lister_documents_par_type(
type_doc=settings.SAGE_TYPE_BON_COMMANDE, limit=limit, statut=statut # = 10
)
commandes = sage_client.lister_commandes(limit=limit, statut=statut)
return commandes
except Exception as e:
@ -578,7 +577,7 @@ async def devis_vers_commande(id: str, session: AsyncSession = Depends(get_sessi
async def commande_vers_facture(id: str, session: AsyncSession = Depends(get_session)):
"""
🔧 Transformation Commande Facture
CORRECTION : Utilise les VRAIS types Sage (10 60)
Utilise les VRAIS types Sage (10 60)
"""
try:
resultat = sage_client.transformer_document(
@ -601,7 +600,16 @@ async def commande_vers_facture(id: str, session: AsyncSession = Depends(get_ses
session.add(workflow_log)
await session.commit()
return resultat
logger.info(
f"✅ Transformation: Commande {id} → Facture {resultat['document_cible']}"
)
return {
"success": True,
"document_source": id,
"document_cible": resultat["document_cible"],
"nb_lignes": resultat["nb_lignes"],
}
except Exception as e:
logger.error(f"Erreur transformation: {e}")
@ -1101,13 +1109,13 @@ async def lister_factures(
limit: int = Query(100, le=1000), statut: Optional[int] = Query(None)
):
"""
📋 Liste toutes les factures
CORRECTION : Filtre sur le type 60 (FACTURE)
📋 Liste toutes les factures via gateway Windows
CORRECTION : Utilise sage_client.lister_factures() qui existe déjà
Le filtrage sur type 60 est fait côté Windows dans main.py
"""
try:
factures = sage_client.lister_documents_par_type(
type_doc=settings.SAGE_TYPE_FACTURE, limit=limit, statut=statut # = 60
)
factures = sage_client.lister_factures(limit=limit, statut=statut)
return factures
except Exception as e:

View file

@ -9,7 +9,6 @@ logger = logging.getLogger(__name__)
class SageGatewayClient:
"""
Client HTTP pour communiquer avec la gateway Sage Windows
VERSION CORRIGÉE
"""
def __init__(self):
@ -121,7 +120,7 @@ class SageGatewayClient:
params={
"numero": numero,
"nouveau_statut": nouveau_statut,
}, # ← QUERY PARAMS
},
headers=self.headers,
timeout=self.timeout,
)
@ -149,13 +148,13 @@ class SageGatewayClient:
try:
r = requests.post(
f"{self.url}/sage/documents/transform",
params={ # ← QUERY PARAMS
params={
"numero_source": numero_source,
"type_source": type_source,
"type_cible": type_cible,
},
headers=self.headers,
timeout=60, # Timeout plus long pour transformation
timeout=60,
)
r.raise_for_status()
return r.json().get("data", {})
@ -184,7 +183,9 @@ class SageGatewayClient:
def lister_commandes(
self, limit: int = 100, statut: Optional[int] = None
) -> List[Dict]:
"""Liste toutes les commandes"""
"""
Utilise l'endpoint /sage/commandes/list qui filtre déjà sur type 10
"""
payload = {"limit": limit}
if statut is not None:
payload["statut"] = statut
@ -196,7 +197,10 @@ class SageGatewayClient:
def lister_factures(
self, limit: int = 100, statut: Optional[int] = None
) -> List[Dict]:
"""Liste toutes les factures"""
"""
Liste toutes les factures
Utilise l'endpoint /sage/factures/list qui filtre déjà sur type 60
"""
payload = {"limit": limit}
if statut is not None:
payload["statut"] = statut