214 lines
8.1 KiB
Python
214 lines
8.1 KiB
Python
"""Router `perfil`: `GET /perfil-regulatorio`, el marco regulatorio de la empresa (Pieza M).
|
|
|
|
Página de consulta, para presentarla en una visita de verificación: régimen con el nombre completo
|
|
de la ley, autoridades, acuerdo vigente y su entrada en vigor, actividad vulnerable, umbrales de
|
|
identificación y de aviso, y la UMA vigente con la fecha en que se capturó. Se llega desde el
|
|
encabezado de Inicio; no es un paso del recorrido. La ven todos los roles.
|
|
|
|
Pieza N: los datos del sujeto obligado (bloque 1) y los días inhábiles por año (bloque 6,
|
|
`/perfil-regulatorio/calendario/{anio}`), que solo edita el oficial.
|
|
"""
|
|
|
|
from typing import Annotated
|
|
|
|
from fastapi import APIRouter, Depends, Form, Request
|
|
from sqlalchemy.orm import Session
|
|
from starlette.responses import HTMLResponse, RedirectResponse, Response
|
|
|
|
from app import fechas as servicio_fechas
|
|
from app.auth import requiere_rol
|
|
from app.db import get_db
|
|
from app.models import Usuario
|
|
from app.models.usuario import Rol
|
|
from app.routers import render
|
|
from app.services import calendario as servicio_calendario
|
|
from app.services import catalogos_sat
|
|
from app.services import perfil as servicio
|
|
from app.services import sujeto_obligado as servicio_sujeto
|
|
from app.services.alertas import tenant_de_sesion
|
|
|
|
router = APIRouter(tags=["perfil"])
|
|
|
|
# Quien ve el aviso de UMA desactualizada: no hay rol de administrador de la empresa; quien
|
|
# administra la configuración regulatoria es el oficial de cumplimiento.
|
|
ROLES_AVISO_UMA = (Rol.OFICIAL,)
|
|
# Quien edita los datos de la empresa como sujeto obligado (Pieza N, bloque 1).
|
|
ROLES_SUJETO_OBLIGADO = (Rol.OFICIAL,)
|
|
RUTA_SUJETO_OBLIGADO = "/perfil-regulatorio/sujeto-obligado"
|
|
# Quien configura los días inhábiles de la empresa (Pieza N, bloque 6).
|
|
ROLES_CALENDARIO = (Rol.OFICIAL,)
|
|
RUTA_CALENDARIO = "/perfil-regulatorio/calendario"
|
|
|
|
|
|
@router.get("/perfil-regulatorio", include_in_schema=False)
|
|
def perfil(request: Request, db: Annotated[Session, Depends(get_db)]) -> HTMLResponse:
|
|
tenant = tenant_de_sesion(db)
|
|
datos = servicio.perfil(db, tenant)
|
|
return render(
|
|
request,
|
|
"perfil/index.html",
|
|
{
|
|
"perfil": datos,
|
|
"marco": datos.marco,
|
|
"ver_aviso_uma": request.state.usuario.rol in ROLES_AVISO_UMA,
|
|
"sujeto": servicio_sujeto.datos(tenant),
|
|
"puede_editar_sujeto": request.state.usuario.rol in ROLES_SUJETO_OBLIGADO,
|
|
"ruta_sujeto": RUTA_SUJETO_OBLIGADO,
|
|
"calendario": servicio_calendario.anios(db, tenant),
|
|
"anio_sin_calendario": _anio_sin_calendario(db, tenant),
|
|
"puede_editar_calendario": request.state.usuario.rol in ROLES_CALENDARIO,
|
|
"ruta_calendario": RUTA_CALENDARIO,
|
|
},
|
|
)
|
|
|
|
|
|
def _anio_sin_calendario(db: Session, tenant) -> int | None:
|
|
"""El año en curso o el siguiente (si ya es diciembre) cuando el calendario no lo trae."""
|
|
hoy = servicio_fechas.hoy()
|
|
cubiertos = servicio_calendario.anios_cubiertos(db, tenant.id)
|
|
for anio in (hoy.year, hoy.year + 1) if hoy.month == 12 else (hoy.year,):
|
|
if anio not in cubiertos:
|
|
return anio
|
|
return None
|
|
|
|
|
|
def _formulario_sujeto(
|
|
request: Request, tenant, valores: dict, error: str | None = None, status_code: int = 200
|
|
) -> HTMLResponse:
|
|
return render(
|
|
request,
|
|
"perfil/sujeto_obligado.html",
|
|
{
|
|
"valores": valores,
|
|
"error": error,
|
|
"actividades": catalogos_sat.catalogo(catalogos_sat.ACTIVIDAD_VULNERABLE),
|
|
"instrumentos": catalogos_sat.catalogo(catalogos_sat.INSTRUMENTO_MONETARIO),
|
|
"monedas": catalogos_sat.catalogo(catalogos_sat.MONEDA),
|
|
"garantias": servicio_sujeto.garantias(),
|
|
"fuente_cp": catalogos_sat.fuente(catalogos_sat.CODIGO_POSTAL),
|
|
"nombre_empresa": tenant.nombre,
|
|
},
|
|
status_code=status_code,
|
|
)
|
|
|
|
|
|
@router.get(RUTA_SUJETO_OBLIGADO, include_in_schema=False)
|
|
def sujeto_obligado(
|
|
request: Request,
|
|
db: Annotated[Session, Depends(get_db)],
|
|
_: Annotated[Usuario, Depends(requiere_rol(*ROLES_SUJETO_OBLIGADO))],
|
|
) -> HTMLResponse:
|
|
tenant = tenant_de_sesion(db)
|
|
valores = {campo: getattr(tenant, campo) for campo in servicio_sujeto.CAMPOS}
|
|
valores["clave_actividad_sat"] = servicio_sujeto.clave_actividad_por_omision(tenant)
|
|
return _formulario_sujeto(request, tenant, valores)
|
|
|
|
|
|
@router.post(RUTA_SUJETO_OBLIGADO, include_in_schema=False)
|
|
def guardar_sujeto_obligado(
|
|
request: Request,
|
|
db: Annotated[Session, Depends(get_db)],
|
|
usuario: Annotated[Usuario, Depends(requiere_rol(*ROLES_SUJETO_OBLIGADO))],
|
|
rfc: Annotated[str, Form()] = "",
|
|
clave_actividad_sat: Annotated[str, Form()] = "",
|
|
clave_entidad_colegiada: Annotated[str, Form()] = "",
|
|
codigo_postal_sucursal: Annotated[str, Form()] = "",
|
|
instrumento_dispersion: Annotated[str, Form()] = "",
|
|
moneda_aviso: Annotated[str, Form()] = "",
|
|
garantia_por_omision: Annotated[str, Form()] = "",
|
|
telefono_contacto: Annotated[str, Form()] = "",
|
|
correo_contacto: Annotated[str, Form()] = "",
|
|
motivo: Annotated[str, Form()] = "",
|
|
) -> Response:
|
|
tenant = tenant_de_sesion(db)
|
|
valores = {
|
|
"rfc": rfc,
|
|
"clave_actividad_sat": clave_actividad_sat,
|
|
"clave_entidad_colegiada": clave_entidad_colegiada,
|
|
"codigo_postal_sucursal": codigo_postal_sucursal,
|
|
"instrumento_dispersion": instrumento_dispersion,
|
|
"moneda_aviso": moneda_aviso,
|
|
"garantia_por_omision": garantia_por_omision,
|
|
"telefono_contacto": telefono_contacto,
|
|
"correo_contacto": correo_contacto,
|
|
}
|
|
try:
|
|
servicio_sujeto.actualizar(db, tenant, valores, usuario=usuario.usuario, motivo=motivo)
|
|
except servicio_sujeto.SujetoObligadoError as e:
|
|
return _formulario_sujeto(request, tenant, {**valores, "motivo": motivo}, str(e), 422)
|
|
db.commit()
|
|
return RedirectResponse("/perfil-regulatorio", status_code=303)
|
|
|
|
|
|
def _formulario_calendario(
|
|
request: Request,
|
|
db: Session,
|
|
tenant,
|
|
anio: int,
|
|
fechas: str | None = None,
|
|
justificacion: str = "",
|
|
error: str | None = None,
|
|
status_code: int = 200,
|
|
) -> HTMLResponse:
|
|
por_anio = {a.anio: a for a in servicio_calendario.anios(db, tenant)}
|
|
actual = por_anio.get(anio)
|
|
if fechas is None:
|
|
fechas = "\n".join(f.isoformat() for f in actual.fechas) if actual else ""
|
|
return render(
|
|
request,
|
|
"perfil/calendario.html",
|
|
{
|
|
"anio": anio,
|
|
"fechas": fechas,
|
|
"justificacion": justificacion,
|
|
"catalogo": [f.isoformat() for f in actual.catalogo] if actual else [],
|
|
"error": error,
|
|
},
|
|
status_code=status_code,
|
|
)
|
|
|
|
|
|
@router.get(RUTA_CALENDARIO, include_in_schema=False)
|
|
def calendario_otro_anio(
|
|
anio: int,
|
|
_: Annotated[Usuario, Depends(requiere_rol(*ROLES_CALENDARIO))],
|
|
) -> Response:
|
|
return RedirectResponse(f"{RUTA_CALENDARIO}/{anio}", status_code=303)
|
|
|
|
|
|
@router.get(RUTA_CALENDARIO + "/{anio}", include_in_schema=False)
|
|
def calendario_anio(
|
|
request: Request,
|
|
anio: int,
|
|
db: Annotated[Session, Depends(get_db)],
|
|
_: Annotated[Usuario, Depends(requiere_rol(*ROLES_CALENDARIO))],
|
|
) -> HTMLResponse:
|
|
tenant = tenant_de_sesion(db)
|
|
return _formulario_calendario(request, db, tenant, anio)
|
|
|
|
|
|
@router.post(RUTA_CALENDARIO + "/{anio}", include_in_schema=False)
|
|
def guardar_calendario_anio(
|
|
request: Request,
|
|
anio: int,
|
|
db: Annotated[Session, Depends(get_db)],
|
|
usuario: Annotated[Usuario, Depends(requiere_rol(*ROLES_CALENDARIO))],
|
|
fechas: Annotated[str, Form()] = "",
|
|
justificacion: Annotated[str, Form()] = "",
|
|
) -> Response:
|
|
tenant = tenant_de_sesion(db)
|
|
try:
|
|
servicio_calendario.configurar_anio(
|
|
db,
|
|
tenant,
|
|
anio,
|
|
servicio_calendario.leer_fechas(fechas),
|
|
justificacion,
|
|
usuario=usuario.usuario,
|
|
)
|
|
except servicio_calendario.CalendarioError as e:
|
|
db.rollback()
|
|
return _formulario_calendario(request, db, tenant, anio, fechas, justificacion, str(e), 422)
|
|
db.commit()
|
|
return RedirectResponse("/perfil-regulatorio", status_code=303)
|