Sage100-ws/utils/articles/articles_data_com.py

241 lines
7.8 KiB
Python

import logging
logger = logging.getLogger(__name__)
def _extraire_article(article_obj):
try:
data = {
"reference": getattr(article_obj, "AR_Ref", "").strip(),
"designation": getattr(article_obj, "AR_Design", "").strip(),
}
data["code_ean"] = ""
data["code_barre"] = ""
try:
code_barre = getattr(article_obj, "AR_CodeBarre", "").strip()
if code_barre:
data["code_ean"] = code_barre
data["code_barre"] = code_barre
if not data["code_ean"]:
code_barre1 = getattr(article_obj, "AR_CodeBarre1", "").strip()
if code_barre1:
data["code_ean"] = code_barre1
data["code_barre"] = code_barre1
except Exception:
pass
try:
data["prix_vente"] = float(getattr(article_obj, "AR_PrixVen", 0.0))
except Exception:
data["prix_vente"] = 0.0
try:
data["prix_achat"] = float(getattr(article_obj, "AR_PrixAch", 0.0))
except Exception:
data["prix_achat"] = 0.0
try:
data["prix_revient"] = float(getattr(article_obj, "AR_PrixRevient", 0.0))
except Exception:
data["prix_revient"] = 0.0
try:
data["stock_reel"] = float(getattr(article_obj, "AR_Stock", 0.0))
except Exception:
data["stock_reel"] = 0.0
try:
data["stock_mini"] = float(getattr(article_obj, "AR_StockMini", 0.0))
except Exception:
data["stock_mini"] = 0.0
try:
data["stock_maxi"] = float(getattr(article_obj, "AR_StockMaxi", 0.0))
except Exception:
data["stock_maxi"] = 0.0
try:
data["stock_reserve"] = float(getattr(article_obj, "AR_QteCom", 0.0))
except Exception:
data["stock_reserve"] = 0.0
try:
data["stock_commande"] = float(getattr(article_obj, "AR_QteComFou", 0.0))
except Exception:
data["stock_commande"] = 0.0
try:
data["stock_disponible"] = data["stock_reel"] - data["stock_reserve"]
except Exception:
data["stock_disponible"] = data["stock_reel"]
try:
commentaire = getattr(article_obj, "AR_Commentaire", "").strip()
data["description"] = commentaire
except Exception:
data["description"] = ""
try:
design2 = getattr(article_obj, "AR_Design2", "").strip()
data["designation_complementaire"] = design2
except Exception:
data["designation_complementaire"] = ""
try:
type_art = getattr(article_obj, "AR_Type", 0)
data["type_article"] = type_art
data["type_article_libelle"] = {
0: "Article",
1: "Prestation",
2: "Divers",
}.get(type_art, "Inconnu")
except Exception:
data["type_article"] = 0
data["type_article_libelle"] = "Article"
try:
famille_code = getattr(article_obj, "FA_CodeFamille", "").strip()
data["famille_code"] = famille_code
if famille_code:
try:
famille_obj = getattr(article_obj, "Famille", None)
if famille_obj:
famille_obj.Read()
data["famille_libelle"] = getattr(
famille_obj, "FA_Intitule", ""
).strip()
else:
data["famille_libelle"] = ""
except Exception:
data["famille_libelle"] = ""
else:
data["famille_libelle"] = ""
except Exception:
data["famille_code"] = ""
data["famille_libelle"] = ""
try:
fournisseur_code = getattr(article_obj, "CT_Num", "").strip()
data["fournisseur_principal"] = fournisseur_code
if fournisseur_code:
try:
fourn_obj = getattr(article_obj, "Fournisseur", None)
if fourn_obj:
fourn_obj.Read()
data["fournisseur_nom"] = getattr(
fourn_obj, "CT_Intitule", ""
).strip()
else:
data["fournisseur_nom"] = ""
except Exception:
data["fournisseur_nom"] = ""
else:
data["fournisseur_nom"] = ""
except Exception:
data["fournisseur_principal"] = ""
data["fournisseur_nom"] = ""
try:
data["unite_vente"] = getattr(article_obj, "AR_UniteVen", "").strip()
except Exception:
data["unite_vente"] = ""
try:
data["unite_achat"] = getattr(article_obj, "AR_UniteAch", "").strip()
except Exception:
data["unite_achat"] = ""
try:
data["poids"] = float(getattr(article_obj, "AR_Poids", 0.0))
except Exception:
data["poids"] = 0.0
try:
data["volume"] = float(getattr(article_obj, "AR_Volume", 0.0))
except Exception:
data["volume"] = 0.0
try:
sommeil = getattr(article_obj, "AR_Sommeil", 0)
data["est_actif"] = sommeil == 0
data["en_sommeil"] = sommeil == 1
except Exception:
data["est_actif"] = True
data["en_sommeil"] = False
try:
tva_code = getattr(article_obj, "TA_Code", "").strip()
data["tva_code"] = tva_code
try:
tva_obj = getattr(article_obj, "Taxe1", None)
if tva_obj:
tva_obj.Read()
data["tva_taux"] = float(getattr(tva_obj, "TA_Taux", 20.0))
else:
data["tva_taux"] = 20.0
except Exception:
data["tva_taux"] = 20.0
except Exception:
data["tva_code"] = ""
data["tva_taux"] = 20.0
try:
date_creation = getattr(article_obj, "AR_DateCreate", None)
data["date_creation"] = str(date_creation) if date_creation else ""
except Exception:
data["date_creation"] = ""
try:
date_modif = getattr(article_obj, "AR_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 extraction article: {e}", exc_info=True)
return {
"reference": getattr(article_obj, "AR_Ref", "").strip(),
"designation": getattr(article_obj, "AR_Design", "").strip(),
"prix_vente": 0.0,
"stock_reel": 0.0,
"code_ean": "",
"description": "",
"designation_complementaire": "",
"prix_achat": 0.0,
"prix_revient": 0.0,
"stock_mini": 0.0,
"stock_maxi": 0.0,
"stock_reserve": 0.0,
"stock_commande": 0.0,
"stock_disponible": 0.0,
"code_barre": "",
"type_article": 0,
"type_article_libelle": "Article",
"famille_code": "",
"famille_libelle": "",
"fournisseur_principal": "",
"fournisseur_nom": "",
"unite_vente": "",
"unite_achat": "",
"poids": 0.0,
"volume": 0.0,
"est_actif": True,
"en_sommeil": False,
"tva_code": "",
"tva_taux": 20.0,
"date_creation": "",
"date_modification": "",
}
__all__ = [
"_extraire_article",
]