diff --git a/sage_connector.py b/sage_connector.py index bd2f2f4..7a6b1b5 100644 --- a/sage_connector.py +++ b/sage_connector.py @@ -7441,7 +7441,7 @@ class SageConnector: collab = win32com.client.CastTo(persist, iface) logger.info(f"✓ Cast vers {iface}") break - except: + except Exception: pass if not collab: collab = persist @@ -7514,27 +7514,27 @@ class SageConnector: try: collab.Vendeur = True logger.debug(" ✓ Vendeur = True") - except: + except Exception: pass if data.get("caissier") is True: try: collab.Caissier = True - except: + except Exception: pass if data.get("acheteur") is True: try: collab.Acheteur = True - except: + except Exception: pass if data.get("sommeil") is True: try: collab.Sommeil = True - except: + except Exception: pass if data.get("chef_ventes") is True: try: collab.ChefVentes = True - except: + except Exception: pass # ===== WRITE ===== @@ -7558,9 +7558,9 @@ class SageConnector: if val and isinstance(val, int): numero_cree = val break - except: + except Exception: pass - except: + except Exception: pass # Via SQL si pas trouvé @@ -7634,7 +7634,7 @@ class SageConnector: collab = win32com.client.CastTo(persist, iface) logger.info(f"✓ Cast vers {iface}") break - except: + except Exception: pass if not collab: collab = persist diff --git a/utils/functions/data/create_doc.py b/utils/functions/data/create_doc.py index 2f1729b..e8f4d6f 100644 --- a/utils/functions/data/create_doc.py +++ b/utils/functions/data/create_doc.py @@ -331,7 +331,11 @@ def _recuperer_numero_document(process, doc) -> str: def _relire_document_final( - self, config: ConfigDocument, numero_document: str, doc_data: dict + self, + config: ConfigDocument, + numero_document: str, + doc_data: dict, + client_code_fallback: str = None, ) -> Dict: """ Relit le document pour obtenir les totaux calculés par Sage @@ -339,6 +343,9 @@ def _relire_document_final( factory_doc = self.cial.FactoryDocumentVente persist_reread = factory_doc.ReadPiece(config.type_sage, numero_document) + client_code = None + date_secondaire_value = None + if persist_reread: doc_final = win32com.client.CastTo(persist_reread, "IBODocumentVente3") doc_final.Read() @@ -347,8 +354,16 @@ def _relire_document_final( total_ttc = float(getattr(doc_final, "DO_TotalTTC", 0.0)) reference_finale = getattr(doc_final, "DO_Ref", "") + # Récupérer le client depuis le document Sage + try: + client_obj = getattr(doc_final, "Client", None) + if client_obj: + client_obj.Read() + client_code = getattr(client_obj, "CT_Num", "").strip() + except Exception: + pass + # Date secondaire - date_secondaire_value = None if config.champ_date_secondaire: try: date_livr = getattr(doc_final, "DO_DateLivr", None) @@ -363,16 +378,22 @@ def _relire_document_final( reference_finale = doc_data.get("reference", "") date_secondaire_value = doc_data.get(config.champ_date_secondaire) + # Fallback pour le code client (priorité: Sage > fallback > doc_data) + if not client_code: + client_code = client_code_fallback or doc_data.get("client", {}).get("code", "") + # Construction du résultat resultat = { config.champ_numero: numero_document, "total_ht": total_ht, "total_ttc": total_ttc, - "nb_lignes": len(doc_data["lignes"]), - "client_code": doc_data["client"]["code"], + "nb_lignes": len(doc_data.get("lignes", [])), + "client_code": client_code, config.champ_date_principale: str( normaliser_date(doc_data.get(config.champ_date_principale)) - ), + ) + if doc_data.get(config.champ_date_principale) + else None, "reference": reference_finale, } @@ -412,7 +433,7 @@ def modifier_document_vente( persist = persist_test logger.info(f" ✓ Document trouvé (type={type_test})") break - except: + except Exception: continue if not persist: @@ -463,7 +484,7 @@ def modifier_document_vente( break nb_lignes_initial += 1 index += 1 - except: + except Exception: break logger.info(f" Lignes existantes: {nb_lignes_initial}") except Exception as e: @@ -581,7 +602,7 @@ def modifier_document_vente( try: factory_lignes = doc.FactoryDocumentLigne - except: + except Exception: factory_lignes = doc.FactoryDocumentVenteLigne factory_article = self.cial.FactoryArticle @@ -655,7 +676,9 @@ def modifier_document_vente( # ========================================== # 9. RELECTURE FINALE # ========================================== - resultat = self._relire_document_final(config, numero, doc_data) + resultat = _relire_document_final( + self, config, numero, doc_data, client_code_fallback=client_code_initial + ) resultat["champs_modifies"] = champs_modifies logger.info(f"✅ {config.nom_document.upper()} {numero} MODIFIÉ")