diff --git a/sage_connector.py b/sage_connector.py index 7f3c735..ea3ea2c 100644 --- a/sage_connector.py +++ b/sage_connector.py @@ -1027,14 +1027,14 @@ class SageConnector: def transformer_document(self, numero_source, type_source, type_cible): """ Transformation avec transaction - ✅ CORRIGÉ: Utilise CreateProcess_Document au lieu de CreateProcess_DocumentVente + ✅ CORRIGÉ: Gestion du statut avant transformation """ if not self.cial: raise RuntimeError("Connexion Sage non établie") try: with self._com_context(), self._lock_com: - # Lecture source + # ===== LECTURE SOURCE ===== factory = self.cial.FactoryDocumentVente persist_source = factory.ReadPiece(type_source, numero_source) @@ -1044,6 +1044,31 @@ class SageConnector: doc_source = win32com.client.CastTo(persist_source, "IBODocumentVente3") doc_source.Read() + # ✅ VÉRIFICATION STATUT + statut_actuel = getattr(doc_source, "DO_Statut", 0) + logger.info( + f"📊 Statut actuel du document {numero_source}: {statut_actuel}" + ) + + # ✅ BLOQUER SI DÉJÀ TRANSFORMÉ + if statut_actuel == 5: + raise ValueError( + f"❌ Le document {numero_source} a déjà été transformé (statut=5)" + ) + + # ✅ FORCER STATUT "ACCEPTÉ" (2) SI BROUILLON (0) + if type_source == 0 and statut_actuel == 0: # Devis brouillon + logger.warning( + f"⚠️ Devis {numero_source} en brouillon (statut=0), " + f"passage à 'Accepté' (statut=2)" + ) + try: + doc_source.DO_Statut = 2 # Accepté + doc_source.Write() + logger.info(f"✅ Statut changé: 0 → 2") + except Exception as e: + logger.warning(f"Impossible de changer le statut: {e}") + # Récupérer le client client_code = "" try: @@ -1059,7 +1084,7 @@ class SageConnector: f"Impossible de récupérer le client du document {numero_source}" ) - # Transaction + # ===== TRANSACTION ===== transaction_active = False try: self.cial.CptaApplication.BeginTrans() @@ -1069,7 +1094,7 @@ class SageConnector: logger.warning(f"⚠️ BeginTrans échoué: {e}") try: - # ✅ CORRECTION: CreateProcess_Document (sans Vente) + # ✅ CRÉATION DOCUMENT CIBLE process = self.cial.CreateProcess_Document(type_cible) doc_cible = process.Document @@ -1110,7 +1135,7 @@ class SageConnector: except: pass - # Copie lignes + # ===== COPIE LIGNES ===== try: factory_lignes_source = doc_source.FactoryDocumentLigne factory_lignes_cible = doc_cible.FactoryDocumentLigne @@ -1216,7 +1241,7 @@ class SageConnector: if index > 1000: break - # Validation + # ===== VALIDATION ===== doc_cible.Write() process.Process() @@ -1230,17 +1255,19 @@ class SageConnector: self.cial.CptaApplication.CommitTrans() logger.info("✅ Transaction committée") - # MAJ statut source si transformation devis → commande + # ✅ MAJ STATUT SOURCE → TRANSFORMÉ (5) try: - if type_source == 0 and type_cible == 3: - doc_source.DO_Statut = 5 # Transformé - doc_source.Write() - logger.info(f"✅ Statut source mis à jour: TRANSFORMÉ (5)") + doc_source.DO_Statut = 5 # Transformé + doc_source.Write() + logger.info( + f"✅ Statut source mis à jour: {statut_actuel} → 5 (TRANSFORMÉ)" + ) except Exception as e: - logger.debug(f"Impossible de MAJ statut source: {e}") + logger.warning(f"⚠️ Impossible de MAJ statut source: {e}") logger.info( - f"✅ Transformation: {numero_source} ({type_source}) → {numero_cible} ({type_cible})" + f"✅ Transformation: {numero_source} ({type_source}) → " + f"{numero_cible} ({type_cible}) - {nb_lignes} lignes" ) return {