Corrected method that caused error on modifying a document

This commit is contained in:
fanilo 2026-01-06 11:03:07 +01:00
parent 97a2bc01f0
commit fd0385d417
2 changed files with 41 additions and 18 deletions

View file

@ -7441,7 +7441,7 @@ class SageConnector:
collab = win32com.client.CastTo(persist, iface) collab = win32com.client.CastTo(persist, iface)
logger.info(f"✓ Cast vers {iface}") logger.info(f"✓ Cast vers {iface}")
break break
except: except Exception:
pass pass
if not collab: if not collab:
collab = persist collab = persist
@ -7514,27 +7514,27 @@ class SageConnector:
try: try:
collab.Vendeur = True collab.Vendeur = True
logger.debug(" ✓ Vendeur = True") logger.debug(" ✓ Vendeur = True")
except: except Exception:
pass pass
if data.get("caissier") is True: if data.get("caissier") is True:
try: try:
collab.Caissier = True collab.Caissier = True
except: except Exception:
pass pass
if data.get("acheteur") is True: if data.get("acheteur") is True:
try: try:
collab.Acheteur = True collab.Acheteur = True
except: except Exception:
pass pass
if data.get("sommeil") is True: if data.get("sommeil") is True:
try: try:
collab.Sommeil = True collab.Sommeil = True
except: except Exception:
pass pass
if data.get("chef_ventes") is True: if data.get("chef_ventes") is True:
try: try:
collab.ChefVentes = True collab.ChefVentes = True
except: except Exception:
pass pass
# ===== WRITE ===== # ===== WRITE =====
@ -7558,9 +7558,9 @@ class SageConnector:
if val and isinstance(val, int): if val and isinstance(val, int):
numero_cree = val numero_cree = val
break break
except: except Exception:
pass pass
except: except Exception:
pass pass
# Via SQL si pas trouvé # Via SQL si pas trouvé
@ -7634,7 +7634,7 @@ class SageConnector:
collab = win32com.client.CastTo(persist, iface) collab = win32com.client.CastTo(persist, iface)
logger.info(f"✓ Cast vers {iface}") logger.info(f"✓ Cast vers {iface}")
break break
except: except Exception:
pass pass
if not collab: if not collab:
collab = persist collab = persist

View file

@ -331,7 +331,11 @@ def _recuperer_numero_document(process, doc) -> str:
def _relire_document_final( 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: ) -> Dict:
""" """
Relit le document pour obtenir les totaux calculés par Sage Relit le document pour obtenir les totaux calculés par Sage
@ -339,6 +343,9 @@ def _relire_document_final(
factory_doc = self.cial.FactoryDocumentVente factory_doc = self.cial.FactoryDocumentVente
persist_reread = factory_doc.ReadPiece(config.type_sage, numero_document) persist_reread = factory_doc.ReadPiece(config.type_sage, numero_document)
client_code = None
date_secondaire_value = None
if persist_reread: if persist_reread:
doc_final = win32com.client.CastTo(persist_reread, "IBODocumentVente3") doc_final = win32com.client.CastTo(persist_reread, "IBODocumentVente3")
doc_final.Read() doc_final.Read()
@ -347,8 +354,16 @@ def _relire_document_final(
total_ttc = float(getattr(doc_final, "DO_TotalTTC", 0.0)) total_ttc = float(getattr(doc_final, "DO_TotalTTC", 0.0))
reference_finale = getattr(doc_final, "DO_Ref", "") 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
date_secondaire_value = None
if config.champ_date_secondaire: if config.champ_date_secondaire:
try: try:
date_livr = getattr(doc_final, "DO_DateLivr", None) date_livr = getattr(doc_final, "DO_DateLivr", None)
@ -363,16 +378,22 @@ def _relire_document_final(
reference_finale = doc_data.get("reference", "") reference_finale = doc_data.get("reference", "")
date_secondaire_value = doc_data.get(config.champ_date_secondaire) 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 # Construction du résultat
resultat = { resultat = {
config.champ_numero: numero_document, config.champ_numero: numero_document,
"total_ht": total_ht, "total_ht": total_ht,
"total_ttc": total_ttc, "total_ttc": total_ttc,
"nb_lignes": len(doc_data["lignes"]), "nb_lignes": len(doc_data.get("lignes", [])),
"client_code": doc_data["client"]["code"], "client_code": client_code,
config.champ_date_principale: str( config.champ_date_principale: str(
normaliser_date(doc_data.get(config.champ_date_principale)) normaliser_date(doc_data.get(config.champ_date_principale))
), )
if doc_data.get(config.champ_date_principale)
else None,
"reference": reference_finale, "reference": reference_finale,
} }
@ -412,7 +433,7 @@ def modifier_document_vente(
persist = persist_test persist = persist_test
logger.info(f" ✓ Document trouvé (type={type_test})") logger.info(f" ✓ Document trouvé (type={type_test})")
break break
except: except Exception:
continue continue
if not persist: if not persist:
@ -463,7 +484,7 @@ def modifier_document_vente(
break break
nb_lignes_initial += 1 nb_lignes_initial += 1
index += 1 index += 1
except: except Exception:
break break
logger.info(f" Lignes existantes: {nb_lignes_initial}") logger.info(f" Lignes existantes: {nb_lignes_initial}")
except Exception as e: except Exception as e:
@ -581,7 +602,7 @@ def modifier_document_vente(
try: try:
factory_lignes = doc.FactoryDocumentLigne factory_lignes = doc.FactoryDocumentLigne
except: except Exception:
factory_lignes = doc.FactoryDocumentVenteLigne factory_lignes = doc.FactoryDocumentVenteLigne
factory_article = self.cial.FactoryArticle factory_article = self.cial.FactoryArticle
@ -655,7 +676,9 @@ def modifier_document_vente(
# ========================================== # ==========================================
# 9. RELECTURE FINALE # 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 resultat["champs_modifies"] = champs_modifies
logger.info(f"{config.nom_document.upper()} {numero} MODIFIÉ") logger.info(f"{config.nom_document.upper()} {numero} MODIFIÉ")