refactored create client method to align IBOClientFactory3's requirement
This commit is contained in:
parent
e8558e207b
commit
914ea61243
1 changed files with 49 additions and 21 deletions
|
|
@ -2378,7 +2378,7 @@ class SageConnector:
|
||||||
def creer_client(self, client_data: Dict) -> Dict:
|
def creer_client(self, client_data: Dict) -> Dict:
|
||||||
"""
|
"""
|
||||||
Crée un nouveau client dans Sage 100c via l'API COM,
|
Crée un nouveau client dans Sage 100c via l'API COM,
|
||||||
en utilisant CreateNew() pour obtenir l'objet concret.
|
en utilisant Create() pour obtenir l'objet Tiers.
|
||||||
"""
|
"""
|
||||||
if not self.cial:
|
if not self.cial:
|
||||||
raise RuntimeError("Connexion Sage non établie")
|
raise RuntimeError("Connexion Sage non établie")
|
||||||
|
|
@ -2388,22 +2388,28 @@ class SageConnector:
|
||||||
# 1. Accès à la Factory Client (CptaApplication si Tiers)
|
# 1. Accès à la Factory Client (CptaApplication si Tiers)
|
||||||
factory_client = self.cial.CptaApplication.FactoryClient
|
factory_client = self.cial.CptaApplication.FactoryClient
|
||||||
|
|
||||||
# CORRECTION CRITIQUE: Utiliser CreateNew() pour obtenir l'objet Tiers concret
|
# ✅ CORRECTION CRITIQUE: Utiliser Create() au lieu de CreateNew()
|
||||||
client = factory_client.CreateNew()
|
persist_client = factory_client.Create()
|
||||||
|
|
||||||
|
if not persist_client:
|
||||||
|
raise RuntimeError("Factory.Create() a retourné None")
|
||||||
|
|
||||||
|
# Cast en IBOClient3 pour accéder aux propriétés
|
||||||
|
client = win32com.client.CastTo(persist_client, "IBOClient3")
|
||||||
|
|
||||||
# 2. Remplissage des Champs OBLIGATOIRES (Conformes F_COMPTET)
|
# 2. Remplissage des Champs OBLIGATOIRES (Conformes F_COMPTET)
|
||||||
|
|
||||||
# CT_Num (Numéro de compte)
|
# CT_Num (Numéro de compte) - Optionnel si auto-numérotation
|
||||||
num_prop = client_data.get("num", "")
|
num_prop = client_data.get("num", "")
|
||||||
if num_prop:
|
if num_prop:
|
||||||
# S'assurer que le numéro est en majuscule (conforme Alpha Majuscule)
|
# S'assurer que le numéro est en majuscule (conforme Alpha Majuscule)
|
||||||
client.CT_Num = num_prop.upper()
|
client.CT_Num = num_prop.upper()
|
||||||
|
|
||||||
# CT_Intitule - Si cette ligne échoue, l'objet créé n'est pas bon.
|
# CT_Intitule - OBLIGATOIRE
|
||||||
client.CT_Intitule = client_data["intitule"]
|
client.CT_Intitule = client_data["intitule"]
|
||||||
client.CT_Type = 0 # 0 = Client
|
client.CT_Type = 0 # 0 = Client
|
||||||
|
|
||||||
# CORRECTION : Utiliser CT_CompteG (mappé à CG_NumPrinc)
|
# ✅ CORRECTION : Utiliser CT_CompteG (mappé à CG_NumPrinc)
|
||||||
if client_data.get("compte_collectif"):
|
if client_data.get("compte_collectif"):
|
||||||
client.CT_CompteG = client_data["compte_collectif"]
|
client.CT_CompteG = client_data["compte_collectif"]
|
||||||
|
|
||||||
|
|
@ -2415,46 +2421,68 @@ class SageConnector:
|
||||||
client.N_Condition = 1
|
client.N_Condition = 1
|
||||||
client.N_Risque = 1
|
client.N_Risque = 1
|
||||||
|
|
||||||
# CT_NumPayeur doit être défini, souvent égal à CT_Num
|
# CT_NumPayeur doit être défini - Si pas de num, sera auto-généré
|
||||||
client.CT_NumPayeur = client.CT_Num
|
if num_prop:
|
||||||
|
client.CT_NumPayeur = num_prop.upper()
|
||||||
|
|
||||||
# 3. CHAMPS OPTIONNELS
|
# 3. CHAMPS OPTIONNELS
|
||||||
|
|
||||||
# --- Adresse principale (Accès via sous-objet Adresse) ---
|
# --- Adresse principale (Accès via sous-objet Adresse) ---
|
||||||
# Ajout d'une gestion d'erreur sur l'accès aux sous-objets car ils peuvent varier
|
|
||||||
try:
|
try:
|
||||||
if client_data.get("adresse"): client.Adresse.Adresse = client_data["adresse"]
|
if client_data.get("adresse"):
|
||||||
if client_data.get("code_postal"): client.Adresse.CodePostal = client_data["code_postal"]
|
client.Adresse.Adresse = client_data["adresse"]
|
||||||
if client_data.get("ville"): client.Adresse.Ville = client_data["ville"]
|
if client_data.get("code_postal"):
|
||||||
if client_data.get("pays"): client.Adresse.Pays = client_data["pays"]
|
client.Adresse.CodePostal = client_data["code_postal"]
|
||||||
|
if client_data.get("ville"):
|
||||||
|
client.Adresse.Ville = client_data["ville"]
|
||||||
|
if client_data.get("pays"):
|
||||||
|
client.Adresse.Pays = client_data["pays"]
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warning(f"Impossible de définir l'adresse: {e}")
|
logger.warning(f"Impossible de définir l'adresse: {e}")
|
||||||
|
|
||||||
# --- Contact / Télécom (Accès via sous-objet Telecom) ---
|
# --- Contact / Télécom (Accès via sous-objet Telecom) ---
|
||||||
try:
|
try:
|
||||||
if client_data.get("telephone"): client.Telecom.Telephone = client_data["telephone"]
|
if client_data.get("telephone"):
|
||||||
if client_data.get("email"): client.Telecom.EMail = client_data["email"]
|
client.Telecom.Telephone = client_data["telephone"]
|
||||||
|
if client_data.get("email"):
|
||||||
|
client.Telecom.EMail = client_data["email"]
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warning(f"Impossible de définir les télécoms: {e}")
|
logger.warning(f"Impossible de définir les télécoms: {e}")
|
||||||
|
|
||||||
# --- Identifiants ---
|
# --- Identifiants ---
|
||||||
if client_data.get("siret"): client.CT_Siret = client_data["siret"]
|
if client_data.get("siret"):
|
||||||
if client_data.get("tva_intra"): client.CT_Identifiant = client_data["tva_intra"]
|
client.CT_Siret = client_data["siret"]
|
||||||
|
if client_data.get("tva_intra"):
|
||||||
|
client.CT_Identifiant = client_data["tva_intra"]
|
||||||
|
|
||||||
# 4. Écriture en base
|
# 4. Écriture en base
|
||||||
client.Write()
|
client.Write()
|
||||||
|
|
||||||
|
# ✅ IMPORTANT: Relire pour récupérer le numéro auto-généré
|
||||||
|
client.Read()
|
||||||
|
|
||||||
num_final = client.CT_Num
|
num_final = client.CT_Num
|
||||||
logger.info(f"✅ Client créé et conforme: {num_final} - {client.CT_Intitule}")
|
|
||||||
|
if not num_final:
|
||||||
|
raise RuntimeError("CT_Num vide après Write() - création échouée")
|
||||||
|
|
||||||
|
logger.info(f"✅ Client créé: {num_final} - {client.CT_Intitule}")
|
||||||
|
|
||||||
|
# ✅ Forcer le refresh du cache pour que le nouveau client soit visible
|
||||||
|
self._refresh_cache_clients()
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"numero": num_final,
|
"numero": num_final,
|
||||||
"intitule": client.CT_Intitule,
|
"intitule": client.CT_Intitule,
|
||||||
"compte_collectif": getattr(client, "CT_CompteG", "")
|
"compte_collectif": getattr(client, "CT_CompteG", ""),
|
||||||
|
"adresse": client_data.get("adresse"),
|
||||||
|
"ville": client_data.get("ville"),
|
||||||
|
"email": client_data.get("email")
|
||||||
}
|
}
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"❌ Erreur création client: {e}", exc_info=True)
|
logger.error(f"❌ Erreur création client: {e}", exc_info=True)
|
||||||
|
|
||||||
# Gestion d'erreur remontant l'erreur COM détaillée (si disponible)
|
# Gestion d'erreur remontant l'erreur COM détaillée (si disponible)
|
||||||
error_message = str(e)
|
error_message = str(e)
|
||||||
if self.cial:
|
if self.cial:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue