import win32com.client from typing import Optional import logging logger = logging.getLogger(__name__) def _afficher_etat_document(doc, titre: str): """Affiche l'état complet d'un document.""" logger.info("-" * 80) logger.info(titre) logger.info("-" * 80) try: logger.info(f" DO_Piece: {getattr(doc, 'DO_Piece', 'N/A')}") logger.info(f" DO_Ref: '{getattr(doc, 'DO_Ref', 'N/A')}'") logger.info(f" DO_Statut: {getattr(doc, 'DO_Statut', 'N/A')}") date_doc = getattr(doc, 'DO_Date', None) date_str = date_doc.strftime('%Y-%m-%d') if date_doc else 'None' logger.info(f" DO_Date: {date_str}") date_livr = getattr(doc, 'DO_DateLivr', None) date_livr_str = date_livr.strftime('%Y-%m-%d') if date_livr else 'None' logger.info(f" DO_DateLivr: {date_livr_str}") logger.info(f" DO_TotalHT: {getattr(doc, 'DO_TotalHT', 0)}€") logger.info(f" DO_TotalTTC: {getattr(doc, 'DO_TotalTTC', 0)}€") except Exception as e: logger.error(f" Erreur affichage état: {e}") logger.info("-" * 80) def _compter_lignes_document(doc) -> int: """Compte les lignes d'un document.""" try: try: factory_lignes = doc.FactoryDocumentLigne except: factory_lignes = doc.FactoryDocumentVenteLigne count = 0 index = 1 while index <= 100: try: ligne_p = factory_lignes.List(index) if ligne_p is None: break count += 1 index += 1 except: break return count except Exception as e: logger.warning(f" Erreur comptage lignes: {e}") return 0 def _rechercher_devis_par_numero(numero: str, factory): """Recherche un devis par numéro dans la liste.""" logger.info(f" Recherche de {numero} dans la liste...") index = 1 while index < 10000: try: persist_test = factory.List(index) if persist_test is None: break doc_test = win32com.client.CastTo(persist_test, "IBODocumentVente3") doc_test.Read() if ( getattr(doc_test, "DO_Type", -1) == 0 and getattr(doc_test, "DO_Piece", "") == numero ): logger.info(f" Trouvé à l'index {index}") return persist_test index += 1 except: index += 1 logger.error(f" Devis {numero} non trouvé dans la liste") return None __all__ = [ "_afficher_etat_document", "_compter_lignes_document", "_rechercher_devis_par_numero" ]