import win32com.client import logging logger = logging.getLogger(__name__) def _cast_client(persist_obj): try: obj = win32com.client.CastTo(persist_obj, "IBOClient3") obj.Read() return obj except Exception as e: logger.debug(f" _cast_client échoue: {e}") return None def _extraire_client(client_obj): try: try: numero = getattr(client_obj, "CT_Num", "").strip() if not numero: logger.debug("Objet sans CT_Num, skip") return None except Exception as e: logger.debug(f" Erreur lecture CT_Num: {e}") return None try: intitule = getattr(client_obj, "CT_Intitule", "").strip() if not intitule: logger.debug(f"{numero} sans CT_Intitule") except Exception as e: logger.debug(f"Erreur CT_Intitule sur {numero}: {e}") intitule = "" data = { "numero": numero, "intitule": intitule, } try: qualite_code = getattr(client_obj, "CT_Type", None) qualite_map = { 0: "CLI", 1: "FOU", 2: "CLIFOU", 3: "SAL", 4: "PRO", } data["qualite"] = qualite_map.get(qualite_code, "CLI") data["est_fournisseur"] = qualite_code in [1, 2] except Exception: data["qualite"] = "CLI" data["est_fournisseur"] = False try: data["est_prospect"] = getattr(client_obj, "CT_Prospect", 0) == 1 except Exception: data["est_prospect"] = False if data["est_prospect"]: data["type_tiers"] = "prospect" elif data["est_fournisseur"] and data["qualite"] != "CLIFOU": data["type_tiers"] = "fournisseur" elif data["qualite"] == "CLIFOU": data["type_tiers"] = "client_fournisseur" else: data["type_tiers"] = "client" try: sommeil = getattr(client_obj, "CT_Sommeil", 0) data["est_actif"] = sommeil == 0 data["est_en_sommeil"] = sommeil == 1 except Exception: data["est_actif"] = True data["est_en_sommeil"] = False try: forme_juridique = getattr(client_obj, "CT_FormeJuridique", "").strip() data["forme_juridique"] = forme_juridique data["est_entreprise"] = bool(forme_juridique) data["est_particulier"] = not bool(forme_juridique) except Exception: data["forme_juridique"] = "" data["est_entreprise"] = False data["est_particulier"] = True try: data["civilite"] = getattr(client_obj, "CT_Civilite", "").strip() except Exception: data["civilite"] = "" try: data["nom"] = getattr(client_obj, "CT_Nom", "").strip() except Exception: data["nom"] = "" try: data["prenom"] = getattr(client_obj, "CT_Prenom", "").strip() except Exception: data["prenom"] = "" if data.get("nom") or data.get("prenom"): parts = [] if data.get("civilite"): parts.append(data["civilite"]) if data.get("prenom"): parts.append(data["prenom"]) if data.get("nom"): parts.append(data["nom"]) data["nom_complet"] = " ".join(parts) else: data["nom_complet"] = "" try: data["contact"] = getattr(client_obj, "CT_Contact", "").strip() except Exception: data["contact"] = "" try: adresse_obj = getattr(client_obj, "Adresse", None) if adresse_obj: try: data["adresse"] = getattr(adresse_obj, "Adresse", "").strip() except Exception: data["adresse"] = "" try: data["complement"] = getattr(adresse_obj, "Complement", "").strip() except Exception: data["complement"] = "" try: data["code_postal"] = getattr(adresse_obj, "CodePostal", "").strip() except Exception: data["code_postal"] = "" try: data["ville"] = getattr(adresse_obj, "Ville", "").strip() except Exception: data["ville"] = "" try: data["region"] = getattr(adresse_obj, "Region", "").strip() except Exception: data["region"] = "" try: data["pays"] = getattr(adresse_obj, "Pays", "").strip() except Exception: data["pays"] = "" else: data["adresse"] = "" data["complement"] = "" data["code_postal"] = "" data["ville"] = "" data["region"] = "" data["pays"] = "" except Exception as e: logger.debug(f"Erreur adresse sur {numero}: {e}") data["adresse"] = "" data["complement"] = "" data["code_postal"] = "" data["ville"] = "" data["region"] = "" data["pays"] = "" try: telecom = getattr(client_obj, "Telecom", None) if telecom: try: data["telephone"] = getattr(telecom, "Telephone", "").strip() except Exception: data["telephone"] = "" try: data["portable"] = getattr(telecom, "Portable", "").strip() except Exception: data["portable"] = "" try: data["telecopie"] = getattr(telecom, "Telecopie", "").strip() except Exception: data["telecopie"] = "" try: data["email"] = getattr(telecom, "EMail", "").strip() except Exception: data["email"] = "" try: site = ( getattr(telecom, "Site", None) or getattr(telecom, "Web", None) or getattr(telecom, "SiteWeb", "") ) data["site_web"] = str(site).strip() if site else "" except Exception: data["site_web"] = "" else: data["telephone"] = "" data["portable"] = "" data["telecopie"] = "" data["email"] = "" data["site_web"] = "" except Exception as e: logger.debug(f"Erreur telecom sur {numero}: {e}") data["telephone"] = "" data["portable"] = "" data["telecopie"] = "" data["email"] = "" data["site_web"] = "" try: data["siret"] = getattr(client_obj, "CT_Siret", "").strip() except Exception: data["siret"] = "" try: data["siren"] = getattr(client_obj, "CT_Siren", "").strip() except Exception: data["siren"] = "" try: data["tva_intra"] = getattr(client_obj, "CT_Identifiant", "").strip() except Exception: data["tva_intra"] = "" try: data["code_naf"] = ( getattr(client_obj, "CT_CodeNAF", "").strip() or getattr(client_obj, "CT_APE", "").strip() ) except Exception: data["code_naf"] = "" try: data["secteur"] = getattr(client_obj, "CT_Secteur", "").strip() except Exception: data["secteur"] = "" try: effectif = getattr(client_obj, "CT_Effectif", None) data["effectif"] = int(effectif) if effectif is not None else None except Exception: data["effectif"] = None try: ca = getattr(client_obj, "CT_ChiffreAffaire", None) data["ca_annuel"] = float(ca) if ca is not None else None except Exception: data["ca_annuel"] = None try: data["commercial_code"] = getattr(client_obj, "CO_No", "").strip() except Exception: try: data["commercial_code"] = getattr( client_obj, "CT_Commercial", "" ).strip() except Exception: data["commercial_code"] = "" if data.get("commercial_code"): try: commercial_obj = getattr(client_obj, "Commercial", None) if commercial_obj: commercial_obj.Read() data["commercial_nom"] = getattr( commercial_obj, "CO_Nom", "" ).strip() else: data["commercial_nom"] = "" except Exception: data["commercial_nom"] = "" else: data["commercial_nom"] = "" try: data["categorie_tarifaire"] = getattr(client_obj, "N_CatTarif", None) except Exception: data["categorie_tarifaire"] = None try: data["categorie_comptable"] = getattr(client_obj, "N_CatCompta", None) except Exception: data["categorie_comptable"] = None try: data["encours_autorise"] = float(getattr(client_obj, "CT_Encours", 0.0)) except Exception: data["encours_autorise"] = 0.0 try: data["assurance_credit"] = float(getattr(client_obj, "CT_Assurance", 0.0)) except Exception: data["assurance_credit"] = 0.0 try: data["compte_general"] = getattr(client_obj, "CG_Num", "").strip() except Exception: data["compte_general"] = "" try: date_creation = getattr(client_obj, "CT_DateCreate", None) data["date_creation"] = str(date_creation) if date_creation else "" except Exception: data["date_creation"] = "" try: date_modif = getattr(client_obj, "CT_DateModif", None) data["date_modification"] = str(date_modif) if date_modif else "" except Exception: data["date_modification"] = "" return data except Exception as e: logger.error(f" ERREUR GLOBALE _extraire_client: {e}", exc_info=True) return None __all__ = ["_extraire_client", "_cast_client"]