132 lines
4 KiB
Python
132 lines
4 KiB
Python
from typing import Optional
|
|
from pathlib import Path
|
|
import base64
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def get_societe_row(cursor):
|
|
"""Récupère la ligne P_DOSSIER"""
|
|
query = """
|
|
SELECT
|
|
D_RaisonSoc, D_NumDoss, D_Siret, D_Ape, D_Identifiant,
|
|
D_Adresse, D_Complement, D_CodePostal, D_Ville,
|
|
D_CodeRegion, D_Pays,
|
|
D_Telephone, D_Telecopie, D_EMail, D_EMailSoc, D_Site,
|
|
D_Capital, D_FormeJuridique,
|
|
D_DebutExo01, D_FinExo01,
|
|
D_DebutExo02, D_FinExo02,
|
|
D_DebutExo03, D_FinExo03,
|
|
D_DebutExo04, D_FinExo04,
|
|
D_DebutExo05, D_FinExo05,
|
|
N_DeviseCompte, N_DeviseEquival,
|
|
D_LgCg, D_LgAn,
|
|
D_RegimeFEC,
|
|
BM_Intitule,
|
|
cbMarq,
|
|
D_Logo
|
|
FROM P_DOSSIER
|
|
"""
|
|
|
|
cursor.execute(query)
|
|
return cursor.fetchone()
|
|
|
|
|
|
def build_exercices(row) -> list:
|
|
"""Construit la liste des exercices"""
|
|
exercices_data = [
|
|
(1, row.D_DebutExo01, row.D_FinExo01),
|
|
(2, row.D_DebutExo02, row.D_FinExo02),
|
|
(3, row.D_DebutExo03, row.D_FinExo03),
|
|
(4, row.D_DebutExo04, row.D_FinExo04),
|
|
(5, row.D_DebutExo05, row.D_FinExo05),
|
|
]
|
|
|
|
exercices = []
|
|
for numero, debut, fin in exercices_data:
|
|
if debut and debut.year > 1753:
|
|
exercices.append(
|
|
{
|
|
"numero": numero,
|
|
"debut": debut.isoformat(),
|
|
"fin": fin.isoformat() if fin and fin.year > 1753 else None,
|
|
}
|
|
)
|
|
|
|
return exercices
|
|
|
|
|
|
def add_logo(societe_dict: dict) -> None:
|
|
"""Ajoute le logo en base64 au dict"""
|
|
logo_path = societe_dict.pop("_logo_path", None)
|
|
|
|
if logo_path and Path(logo_path).exists():
|
|
try:
|
|
ext = Path(logo_path).suffix.lower()
|
|
content_type = {
|
|
".png": "image/png",
|
|
".jpg": "image/jpeg",
|
|
".jpeg": "image/jpeg",
|
|
".bmp": "image/bmp",
|
|
".gif": "image/gif",
|
|
}.get(ext, "image/png")
|
|
|
|
with open(logo_path, "rb") as f:
|
|
societe_dict["logo_base64"] = base64.b64encode(f.read()).decode("utf-8")
|
|
societe_dict["logo_content_type"] = content_type
|
|
return
|
|
except Exception as e:
|
|
logger.warning(f"Erreur conversion logo: {e}")
|
|
|
|
societe_dict["logo_base64"] = None
|
|
societe_dict["logo_content_type"] = None
|
|
|
|
|
|
def recuperer_logo_com(sage_instance) -> dict:
|
|
"""Cherche le logo dans les répertoires standards"""
|
|
return _chercher_logo_standards()
|
|
|
|
|
|
def _chercher_logo_standards() -> dict:
|
|
"""Cherche dans les répertoires standards Sage"""
|
|
chemins = [
|
|
Path("C:/ProgramData/Sage/Logo"),
|
|
Path("C:/ProgramData/Sage/Sage 100/Logo"),
|
|
Path("C:/Users/Public/Documents/Sage"),
|
|
Path(r"C:\Program Files\Sage\Sage 100\Bitmap"),
|
|
Path(r"C:\Program Files (x86)\Sage\Sage 100\Bitmap"),
|
|
]
|
|
|
|
for repertoire in chemins:
|
|
if not repertoire.exists():
|
|
continue
|
|
|
|
for ext in [".bmp", ".jpg", ".jpeg", ".png", ".gif"]:
|
|
for fichier in repertoire.glob(f"*{ext}"):
|
|
logger.info(f"Logo trouvé: {fichier}")
|
|
return _convertir_fichier_logo(str(fichier))
|
|
|
|
logger.info("Aucun logo trouvé")
|
|
return {"logo_base64": None, "logo_content_type": None}
|
|
|
|
|
|
def _convertir_fichier_logo(chemin: str) -> dict:
|
|
"""Convertit image en base64"""
|
|
ext = Path(chemin).suffix.lower()
|
|
content_type = {
|
|
".png": "image/png",
|
|
".jpg": "image/jpeg",
|
|
".jpeg": "image/jpeg",
|
|
".bmp": "image/bmp",
|
|
".gif": "image/gif",
|
|
}.get(ext, "image/png")
|
|
|
|
with open(chemin, "rb") as f:
|
|
return {
|
|
"logo_base64": base64.b64encode(f.read()).decode("utf-8"),
|
|
"logo_content_type": content_type,
|
|
}
|
|
|
|
|
|
__all__ = ["get_societe_row", "build_exercices", "add_logo", "recuperer_logo_com"]
|