50 lines
1.6 KiB
Python
50 lines
1.6 KiB
Python
from app.modules.clients.schema import ClientCreate
|
|
from app.modules.clients.models import Client
|
|
#
|
|
from sqlalchemy.exc import IntegrityError
|
|
from sqlalchemy.orm import Session
|
|
from app.core.dependencies import fiscal_number_exists, email_exist
|
|
|
|
|
|
|
|
|
|
class ClientService:
|
|
def __init__(self):
|
|
pass
|
|
|
|
|
|
@staticmethod
|
|
def create_client(db:Session, client_data: ClientCreate) -> Client:
|
|
|
|
|
|
if fiscal_number_exists(db, client_data.get("fiscal_number")):
|
|
raise ValueError("el numero fiscal ya existe")
|
|
|
|
if email_exist(db, client_data.get("email")):
|
|
raise ValueError("este email ya existe")
|
|
|
|
new_client = Client(
|
|
rfc = client_data.get("rfc"),
|
|
email = client_data.get("email"),
|
|
short_name = client_data.get("short_name"),
|
|
razon_social = client_data.get("razon_social"),
|
|
fiscal_number = client_data.get("fiscal_number"),
|
|
cellphone = client_data.get("cellphone"),
|
|
medio = client_data.get("medio"),
|
|
third_type = client_data.get("third_type"),
|
|
operation_type = client_data.get("operation_type"),
|
|
is_foreign = client_data.get("is_foreign", False),
|
|
user_id = client_data.get("user_id"),
|
|
location_id = client_data.get("location_id"),
|
|
)
|
|
try:
|
|
db.add(new_client)
|
|
db.commit()
|
|
db.refresh(new_client)
|
|
|
|
except IntegrityError as e:
|
|
db.rollback()
|
|
raise ValueError(f"Error al crear el cliente: {e}")
|
|
return new_client
|
|
|