511 lines
19 KiB
Python
511 lines
19 KiB
Python
import requests
|
|
from typing import Dict, List, Optional, Union
|
|
from config import settings
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class SageGatewayClient:
|
|
def __init__(self):
|
|
self.url = settings.sage_gateway_url.rstrip("/")
|
|
self.headers = {
|
|
"X-Sage-Token": settings.sage_gateway_token,
|
|
"Content-Type": "application/json",
|
|
}
|
|
self.timeout = 30
|
|
|
|
def _post(self, endpoint: str, data: dict = None, retries: int = 3) -> dict:
|
|
"""POST avec retry automatique"""
|
|
import time
|
|
|
|
for attempt in range(retries):
|
|
try:
|
|
r = requests.post(
|
|
f"{self.url}{endpoint}",
|
|
json=data or {},
|
|
headers=self.headers,
|
|
timeout=self.timeout,
|
|
)
|
|
r.raise_for_status()
|
|
return r.json()
|
|
except requests.exceptions.RequestException as e:
|
|
if attempt == retries - 1:
|
|
logger.error(
|
|
f"❌ Échec après {retries} tentatives sur {endpoint}: {e}"
|
|
)
|
|
raise
|
|
time.sleep(2**attempt)
|
|
|
|
def _get(self, endpoint: str, params: dict = None, retries: int = 3) -> dict:
|
|
"""GET avec retry automatique"""
|
|
import time
|
|
|
|
for attempt in range(retries):
|
|
try:
|
|
r = requests.get(
|
|
f"{self.url}{endpoint}",
|
|
params=params or {},
|
|
headers=self.headers,
|
|
timeout=self.timeout,
|
|
)
|
|
r.raise_for_status()
|
|
return r.json()
|
|
except requests.exceptions.RequestException as e:
|
|
if attempt == retries - 1:
|
|
logger.error(
|
|
f"❌ Échec GET après {retries} tentatives sur {endpoint}: {e}"
|
|
)
|
|
raise
|
|
time.sleep(2**attempt)
|
|
|
|
def lister_clients(self, filtre: str = "") -> List[Dict]:
|
|
"""Liste tous les clients avec filtre optionnel"""
|
|
return self._post("/sage/clients/list", {"filtre": filtre}).get("data", [])
|
|
|
|
def lire_client(self, code: str) -> Optional[Dict]:
|
|
"""Lecture d'un client par code"""
|
|
return self._post("/sage/clients/get", {"code": code}).get("data")
|
|
|
|
def lister_articles(self, filtre: str = "") -> List[Dict]:
|
|
"""Liste tous les articles avec filtre optionnel"""
|
|
return self._post("/sage/articles/list", {"filtre": filtre}).get("data", [])
|
|
|
|
def lire_article(self, ref: str) -> Optional[Dict]:
|
|
"""Lecture d'un article par référence"""
|
|
return self._post("/sage/articles/get", {"code": ref}).get("data")
|
|
|
|
def creer_devis(self, devis_data: Dict) -> Dict:
|
|
"""Création d'un devis"""
|
|
return self._post("/sage/devis/create", devis_data).get("data", {})
|
|
|
|
def lire_devis(self, numero: str) -> Optional[Dict]:
|
|
"""Lecture d'un devis"""
|
|
return self._post("/sage/devis/get", {"code": numero}).get("data")
|
|
|
|
def lister_devis(
|
|
self,
|
|
limit: int = 100,
|
|
statut: Optional[int] = None,
|
|
inclure_lignes: bool = True,
|
|
) -> List[Dict]:
|
|
payload = {"limit": limit, "inclure_lignes": inclure_lignes}
|
|
if statut is not None:
|
|
payload["statut"] = statut
|
|
return self._post("/sage/devis/list", payload).get("data", [])
|
|
|
|
def changer_statut_devis(self, numero: str, nouveau_statut: int) -> Dict:
|
|
try:
|
|
r = requests.post(
|
|
f"{self.url}/sage/devis/statut",
|
|
params={
|
|
"numero": numero,
|
|
"nouveau_statut": nouveau_statut,
|
|
},
|
|
headers=self.headers,
|
|
timeout=self.timeout,
|
|
)
|
|
r.raise_for_status()
|
|
return r.json().get("data", {})
|
|
except requests.exceptions.RequestException as e:
|
|
logger.error(f"❌ Erreur changement statut: {e}")
|
|
raise
|
|
|
|
def lire_document(self, numero: str, type_doc: int) -> Optional[Dict]:
|
|
"""Lecture d'un document générique"""
|
|
return self._post(
|
|
"/sage/documents/get", {"numero": numero, "type_doc": type_doc}
|
|
).get("data")
|
|
|
|
def transformer_document(
|
|
self, numero_source: str, type_source: int, type_cible: int
|
|
) -> Dict:
|
|
try:
|
|
r = requests.post(
|
|
f"{self.url}/sage/documents/transform",
|
|
params={
|
|
"numero_source": numero_source,
|
|
"type_source": type_source,
|
|
"type_cible": type_cible,
|
|
},
|
|
headers=self.headers,
|
|
timeout=60,
|
|
)
|
|
r.raise_for_status()
|
|
return r.json().get("data", {})
|
|
except requests.exceptions.RequestException as e:
|
|
logger.error(f"❌ Erreur transformation: {e}")
|
|
raise
|
|
|
|
def mettre_a_jour_champ_libre(
|
|
self, doc_id: str, type_doc: int, nom_champ: str, valeur: str
|
|
) -> bool:
|
|
"""Mise à jour d'un champ libre"""
|
|
resp = self._post(
|
|
"/sage/documents/champ-libre",
|
|
{
|
|
"doc_id": doc_id,
|
|
"type_doc": type_doc,
|
|
"nom_champ": nom_champ,
|
|
"valeur": valeur,
|
|
},
|
|
)
|
|
return resp.get("success", False)
|
|
|
|
def lister_commandes(
|
|
self, limit: int = 100, statut: Optional[int] = None
|
|
) -> List[Dict]:
|
|
payload = {"limit": limit}
|
|
if statut is not None:
|
|
payload["statut"] = statut
|
|
return self._post("/sage/commandes/list", payload).get("data", [])
|
|
|
|
def lister_factures(
|
|
self, limit: int = 100, statut: Optional[int] = None
|
|
) -> List[Dict]:
|
|
payload = {"limit": limit}
|
|
if statut is not None:
|
|
payload["statut"] = statut
|
|
return self._post("/sage/factures/list", payload).get("data", [])
|
|
|
|
def mettre_a_jour_derniere_relance(self, doc_id: str, type_doc: int) -> bool:
|
|
"""Met à jour le champ 'Dernière relance' d'une facture"""
|
|
resp = self._post(
|
|
"/sage/documents/derniere-relance", {"doc_id": doc_id, "type_doc": type_doc}
|
|
)
|
|
return resp.get("success", False)
|
|
|
|
def lire_contact_client(self, code_client: str) -> Optional[Dict]:
|
|
"""Lecture du contact principal d'un client"""
|
|
return self._post("/sage/contact/read", {"code": code_client}).get("data")
|
|
|
|
def lire_remise_max_client(self, code_client: str) -> float:
|
|
"""Récupère la remise max autorisée pour un client"""
|
|
result = self._post("/sage/client/remise-max", {"code": code_client})
|
|
return result.get("data", {}).get("remise_max", 10.0)
|
|
|
|
def generer_pdf_document(self, doc_id: str, type_doc: int) -> bytes:
|
|
"""Génère le PDF d'un document via la gateway Windows"""
|
|
try:
|
|
r = requests.post(
|
|
f"{self.url}/sage/documents/generate-pdf",
|
|
json={"doc_id": doc_id, "type_doc": type_doc},
|
|
headers=self.headers,
|
|
timeout=60,
|
|
)
|
|
r.raise_for_status()
|
|
|
|
import base64
|
|
|
|
response_data = r.json()
|
|
pdf_base64 = response_data.get("data", {}).get("pdf_base64", "")
|
|
|
|
if not pdf_base64:
|
|
raise ValueError("PDF vide retourné par la gateway")
|
|
|
|
return base64.b64decode(pdf_base64)
|
|
|
|
except Exception as e:
|
|
logger.error(f"Erreur génération PDF: {e}")
|
|
raise
|
|
|
|
def lister_prospects(self, filtre: str = "") -> List[Dict]:
|
|
"""Liste tous les prospects avec filtre optionnel"""
|
|
return self._post("/sage/prospects/list", {"filtre": filtre}).get("data", [])
|
|
|
|
def lire_prospect(self, code: str) -> Optional[Dict]:
|
|
"""Lecture d'un prospect par code"""
|
|
return self._post("/sage/prospects/get", {"code": code}).get("data")
|
|
|
|
def lister_fournisseurs(self, filtre: str = "") -> List[Dict]:
|
|
"""Liste tous les fournisseurs avec filtre optionnel"""
|
|
return self._post("/sage/fournisseurs/list", {"filtre": filtre}).get("data", [])
|
|
|
|
def lire_fournisseur(self, code: str) -> Optional[Dict]:
|
|
"""Lecture d'un fournisseur par code"""
|
|
return self._post("/sage/fournisseurs/get", {"code": code}).get("data")
|
|
|
|
def creer_fournisseur(self, fournisseur_data: Dict) -> Dict:
|
|
return self._post("/sage/fournisseurs/create", fournisseur_data).get("data", {})
|
|
|
|
def modifier_fournisseur(self, code: str, fournisseur_data: Dict) -> Dict:
|
|
return self._post(
|
|
"/sage/fournisseurs/update",
|
|
{"code": code, "fournisseur_data": fournisseur_data},
|
|
).get("data", {})
|
|
|
|
def lister_avoirs(
|
|
self, limit: int = 100, statut: Optional[int] = None
|
|
) -> List[Dict]:
|
|
"""Liste tous les avoirs"""
|
|
payload = {"limit": limit}
|
|
if statut is not None:
|
|
payload["statut"] = statut
|
|
return self._post("/sage/avoirs/list", payload).get("data", [])
|
|
|
|
def lire_avoir(self, numero: str) -> Optional[Dict]:
|
|
"""Lecture d'un avoir avec ses lignes"""
|
|
return self._post("/sage/avoirs/get", {"code": numero}).get("data")
|
|
|
|
def lister_livraisons(
|
|
self, limit: int = 100, statut: Optional[int] = None
|
|
) -> List[Dict]:
|
|
"""Liste tous les bons de livraison"""
|
|
payload = {"limit": limit}
|
|
if statut is not None:
|
|
payload["statut"] = statut
|
|
return self._post("/sage/livraisons/list", payload).get("data", [])
|
|
|
|
def lire_livraison(self, numero: str) -> Optional[Dict]:
|
|
"""Lecture d'une livraison avec ses lignes"""
|
|
return self._post("/sage/livraisons/get", {"code": numero}).get("data")
|
|
|
|
def refresh_cache(self) -> Dict:
|
|
"""Force le rafraîchissement du cache Windows"""
|
|
return self._post("/sage/cache/refresh")
|
|
|
|
def get_cache_info(self) -> Dict:
|
|
"""Récupère les infos du cache Windows"""
|
|
return self._get("/sage/cache/info").get("data", {})
|
|
|
|
def health(self) -> dict:
|
|
"""Health check de la gateway Windows"""
|
|
try:
|
|
r = requests.get(f"{self.url}/health", timeout=5)
|
|
return r.json()
|
|
except:
|
|
return {"status": "down"}
|
|
|
|
def creer_client(self, client_data: Dict) -> Dict:
|
|
return self._post("/sage/clients/create", client_data).get("data", {})
|
|
|
|
def modifier_client(self, code: str, client_data: Dict) -> Dict:
|
|
return self._post(
|
|
"/sage/clients/update", {"code": code, "client_data": client_data}
|
|
).get("data", {})
|
|
|
|
def modifier_devis(self, numero: str, devis_data: Dict) -> Dict:
|
|
return self._post(
|
|
"/sage/devis/update", {"numero": numero, "devis_data": devis_data}
|
|
).get("data", {})
|
|
|
|
def creer_commande(self, commande_data: Dict) -> Dict:
|
|
return self._post("/sage/commandes/create", commande_data).get("data", {})
|
|
|
|
def modifier_commande(self, numero: str, commande_data: Dict) -> Dict:
|
|
return self._post(
|
|
"/sage/commandes/update", {"numero": numero, "commande_data": commande_data}
|
|
).get("data", {})
|
|
|
|
def creer_livraison(self, livraison_data: Dict) -> Dict:
|
|
return self._post("/sage/livraisons/create", livraison_data).get("data", {})
|
|
|
|
def modifier_livraison(self, numero: str, livraison_data: Dict) -> Dict:
|
|
return self._post(
|
|
"/sage/livraisons/update",
|
|
{"numero": numero, "livraison_data": livraison_data},
|
|
).get("data", {})
|
|
|
|
def creer_avoir(self, avoir_data: Dict) -> Dict:
|
|
return self._post("/sage/avoirs/create", avoir_data).get("data", {})
|
|
|
|
def modifier_avoir(self, numero: str, avoir_data: Dict) -> Dict:
|
|
return self._post(
|
|
"/sage/avoirs/update", {"numero": numero, "avoir_data": avoir_data}
|
|
).get("data", {})
|
|
|
|
def creer_facture(self, facture_data: Dict) -> Dict:
|
|
return self._post("/sage/factures/create", facture_data).get("data", {})
|
|
|
|
def modifier_facture(self, numero: str, facture_data: Dict) -> Dict:
|
|
return self._post(
|
|
"/sage/factures/update", {"numero": numero, "facture_data": facture_data}
|
|
).get("data", {})
|
|
|
|
def generer_pdf_document(self, doc_id: str, type_doc: int) -> bytes:
|
|
try:
|
|
logger.info(f"📄 Demande génération PDF: doc_id={doc_id}, type={type_doc}")
|
|
|
|
r = requests.post(
|
|
f"{self.url}/sage/documents/generate-pdf",
|
|
json={"doc_id": doc_id, "type_doc": type_doc},
|
|
headers=self.headers,
|
|
timeout=60,
|
|
)
|
|
|
|
r.raise_for_status()
|
|
|
|
import base64
|
|
|
|
response_data = r.json()
|
|
|
|
if not response_data.get("success"):
|
|
error_msg = response_data.get("error", "Erreur inconnue")
|
|
raise RuntimeError(f"Gateway a retourné une erreur: {error_msg}")
|
|
|
|
pdf_base64 = response_data.get("data", {}).get("pdf_base64", "")
|
|
|
|
if not pdf_base64:
|
|
raise ValueError(
|
|
f"PDF vide retourné par la gateway pour {doc_id} (type {type_doc})"
|
|
)
|
|
|
|
pdf_bytes = base64.b64decode(pdf_base64)
|
|
|
|
logger.info(f"✅ PDF décodé: {len(pdf_bytes)} octets")
|
|
|
|
return pdf_bytes
|
|
|
|
except requests.exceptions.Timeout:
|
|
logger.error(f"⏱️ Timeout génération PDF pour {doc_id}")
|
|
raise RuntimeError(
|
|
f"Timeout lors de la génération du PDF (>60s). "
|
|
f"Le document {doc_id} est peut-être trop volumineux."
|
|
)
|
|
|
|
except requests.exceptions.RequestException as e:
|
|
logger.error(f"❌ Erreur HTTP génération PDF: {e}")
|
|
raise RuntimeError(f"Erreur de communication avec la gateway: {str(e)}")
|
|
|
|
except Exception as e:
|
|
logger.error(f"❌ Erreur génération PDF: {e}", exc_info=True)
|
|
raise
|
|
|
|
def creer_article(self, article_data: Dict) -> Dict:
|
|
return self._post("/sage/articles/create", article_data).get("data", {})
|
|
|
|
def modifier_article(self, reference: str, article_data: Dict) -> Dict:
|
|
return self._post(
|
|
"/sage/articles/update",
|
|
{"reference": reference, "article_data": article_data},
|
|
).get("data", {})
|
|
|
|
def lister_familles(self, filtre: str = "") -> List[Dict]:
|
|
return self._get("/sage/familles", params={"filtre": filtre}).get("data", [])
|
|
|
|
def lire_famille(self, code: str) -> Optional[Dict]:
|
|
try:
|
|
response = self._get(f"/sage/familles/{code}")
|
|
return response.get("data")
|
|
except Exception as e:
|
|
logger.error(f"Erreur lecture famille {code}: {e}")
|
|
return None
|
|
|
|
def creer_famille(self, famille_data: Dict) -> Dict:
|
|
return self._post("/sage/familles/create", famille_data).get("data", {})
|
|
|
|
def get_stats_familles(self) -> Dict:
|
|
return self._get("/sage/familles/stats").get("data", {})
|
|
|
|
def creer_entree_stock(self, entree_data: Dict) -> Dict:
|
|
return self._post("/sage/stock/entree", entree_data).get("data", {})
|
|
|
|
def creer_sortie_stock(self, sortie_data: Dict) -> Dict:
|
|
return self._post("/sage/stock/sortie", sortie_data).get("data", {})
|
|
|
|
def lire_mouvement_stock(self, numero: str) -> Optional[Dict]:
|
|
try:
|
|
response = self._get(f"/sage/stock/mouvement/{numero}")
|
|
return response.get("data")
|
|
except Exception as e:
|
|
logger.error(f"Erreur lecture mouvement {numero}: {e}")
|
|
return None
|
|
|
|
def lister_modeles_disponibles(self) -> Dict:
|
|
"""Liste les modèles Crystal Reports disponibles"""
|
|
try:
|
|
r = requests.get(
|
|
f"{self.url}/sage/modeles/list", headers=self.headers, timeout=30
|
|
)
|
|
r.raise_for_status()
|
|
return r.json().get("data", {})
|
|
except requests.exceptions.RequestException as e:
|
|
logger.error(f"❌ Erreur listage modèles: {e}")
|
|
raise
|
|
|
|
def generer_pdf_document(
|
|
self, numero: str, type_doc: int, modele: str = None, base64_encode: bool = True
|
|
) -> Union[bytes, str, Dict]:
|
|
try:
|
|
params = {"type_doc": type_doc, "base64_encode": base64_encode}
|
|
|
|
if modele:
|
|
params["modele"] = modele
|
|
|
|
r = requests.get(
|
|
f"{self.url}/sage/documents/{numero}/pdf",
|
|
params=params,
|
|
headers=self.headers,
|
|
timeout=60,
|
|
)
|
|
r.raise_for_status()
|
|
|
|
if base64_encode:
|
|
return r.json().get("data", {})
|
|
else:
|
|
return r.content
|
|
|
|
except requests.exceptions.RequestException as e:
|
|
logger.error(f"❌ Erreur génération PDF: {e}")
|
|
raise
|
|
|
|
|
|
|
|
def creer_contact(self, contact_data: Dict) -> Dict:
|
|
return self._post("/sage/contacts/create", contact_data)
|
|
|
|
|
|
def lister_contacts(self, numero: str) -> List[Dict]:
|
|
return self._post("/sage/contacts/list", {"numero": numero}).get("data", [])
|
|
|
|
|
|
def obtenir_contact(self, numero: str, contact_numero: int) -> Dict:
|
|
result = self._post("/sage/contacts/get", {
|
|
"numero": numero,
|
|
"contact_numero": contact_numero
|
|
})
|
|
return result.get("data") if result.get("success") else None
|
|
|
|
|
|
def modifier_contact(self, numero: str, contact_numero: int, updates: Dict) -> Dict:
|
|
return self._post("/sage/contacts/update", {
|
|
"numero": numero,
|
|
"contact_numero": contact_numero,
|
|
"updates": updates
|
|
})
|
|
|
|
|
|
def supprimer_contact(self, numero: str, contact_numero: int) -> Dict:
|
|
"""
|
|
Supprime un contact
|
|
|
|
Args:
|
|
numero: Code du client
|
|
contact_numero: Numéro unique du contact
|
|
|
|
Returns:
|
|
Dictionnaire avec le statut de la suppression
|
|
"""
|
|
return self._post("/sage/contacts/delete", {
|
|
"numero": numero,
|
|
"contact_numero": contact_numero
|
|
})
|
|
|
|
|
|
def definir_contact_defaut(self, numero: str, contact_numero: int) -> Dict:
|
|
"""
|
|
Définit un contact comme contact par défaut du client
|
|
|
|
Args:
|
|
numero: Code du client
|
|
contact_numero: Numéro unique du contact à définir comme par défaut
|
|
|
|
Returns:
|
|
Dictionnaire avec les données du client mis à jour
|
|
"""
|
|
return self._post("/sage/contacts/set-default", {
|
|
"numero": numero,
|
|
"contact_numero": contact_numero
|
|
})
|
|
|
|
sage_client = SageGatewayClient()
|