Files
Back_modulo_comercial/Back_comercial_iko/services/Comercial_service.py

84 lines
3.6 KiB
Python

from Back_comercial_iko.database.Database import Database
from Back_comercial_iko.models.Societe_model import SocieteModel
from Back_comercial_iko.models.Projet_model import ProjetModel
from Back_comercial_iko.models.Projet_extrafields_model import ProjetExtrafieldsModel
from Back_comercial_iko.core.validators.Field_validator import FieldValidator
from Back_comercial_iko.core.MessageException import MessageException
from datetime import datetime
class ComercialService:
def __init__(self):
self.conn = Database()
self.db = self.conn.setConnection()
def new_ref(self):
ALIAS_REF = "PCom"
year = datetime.now().strftime("%y")
result = self.db.query(ProjetModel.REF).filter(
ProjetModel.REF.like(f"%{ALIAS_REF}0%")
).order_by(ProjetModel.REF.desc()).first()
if result and result.REF:
ref_str = result.REF
start = ref_str.find(ALIAS_REF) + len(ALIAS_REF)
numero_str = ref_str[start:start + 4]
new_ref_num = int(numero_str) + 1 if numero_str.isdigit() else 1
else:
new_ref_num = 1
return f"{ALIAS_REF}{str(new_ref_num).zfill(4)}-{year}"
def create_project_commercial(self, data: dict, token_data: dict):
"""Service to create a new commercial project"""
data_session_login = token_data['data']
FieldValidator.required(data.get("ex_societe"), "ex_societe")
FieldValidator.required(data.get("description"), "description")
FieldValidator.required(data.get("date_o"), "date_o")
FieldValidator.required(data.get("date_e"), "date_e")
name_soc = data.get("ex_societe")
date_c = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
result_soc = (self.db.query(SocieteModel.ROWID, SocieteModel.NOM).filter(SocieteModel.NOM == name_soc).first())
fk_soc = result_soc.ROWID if result_soc else None
id_user = data_session_login['id']
description = data.get("description")
dateo = data.get("date_o")
datee = data.get("date_e")
last_contact = data.get("last_contact"),
agency = data.get("agency"),
advertiser = data.get("advertiser"),
status = data.get("status")
if fk_soc is None:
new_societe = SocieteModel(NOM=name_soc, NAME_ALIAS=name_soc, ENTITY=1, CLIENT=1, FOURNISSEUR=0, DATEC=date_c)
self.db.add(new_societe)
self.db.flush()
self.db.commit()
fk_soc = new_societe.ROWID
new_project = ProjetModel(TMS=date_c, DESCRIPTION=description, TITLE=description, DATEO=dateo, DATEE=datee, FK_STATUT=1,
FK_USER_CREAT=id_user, REF=self.new_ref(), DATEC=date_c, FK_SOC=fk_soc)
self.db.add(new_project)
self.db.flush()
self.db.commit()
rowid_project = new_project.ROWID if new_project.ROWID else MessageException.raise_it(extra_msg="No se pudo crea el proyecto")
if rowid_project:
extrafields_project = ProjetExtrafieldsModel(FK_OBJECT=rowid_project, TIPO=2, LAST_CONTACT=last_contact,
AGENCIA=agency, ANUNCIANTE=advertiser, ESTADO=status)
self.db.add(extrafields_project)
self.db.flush()
self.db.commit()
rowid_extrafields = extrafields_project.ROWID if extrafields_project.ROWID else MessageException.raise_it(extra_msg="No se pudo crear el proyecto")
return {
"fk_soc": fk_soc,
"nombre_soc": name_soc,
"rowid_project": rowid_project,
"rowid_extrafields": rowid_extrafields,
}