feat(contratos): gestión de códigos de pago + asignación por contrato

- Nueva tabla SQLite codigos_pago (codigo, nombre) con seed CIAC/CR/MU
- Columna cod_forma_pago en contratos (override por contrato)
- Página contratos: tarjeta para agregar/eliminar códigos, select inline por fila
- generar_factura_venta acepta forma_pago_override; si asignado lo usa, sino auto CIAC/CR
- Rutas: /codigos-pago/create, /codigos-pago/delete, /set-forma-pago

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lizandro Guarnizo
2026-07-30 16:43:13 -05:00
co-authored by Claude Sonnet 4.6
parent 551dec9c11
commit 91dbcc8135
5 changed files with 157 additions and 14 deletions
+12 -6
View File
@@ -9,7 +9,7 @@ from app.services.firebird_service import get_firebird_from_config
from app.services.json_generator import generar_factura_venta
from app.services.api_client import get_tns_token, TNS_BASE
from app.utils.activity import log_activity, get_ip
from app.routes.contratos import load_excluded_ventas_set
from app.routes.contratos import load_excluded_ventas_set, load_forma_pago_map
router = APIRouter(prefix="/ventas", tags=["ventas"])
@@ -103,11 +103,14 @@ def _agrupar(rows):
return dict(grupos)
def _build_venta(grupo_rows, cfg):
def _build_venta(grupo_rows, cfg, forma_pago_map=None):
prefijo_def = cfg.get("prefijo_tns_default", "00")
num_fac = str(grupo_rows[0].get("NUM_FACTURA") or "").strip()
contrato = str(grupo_rows[0].get("CODCONTRATO") or "").strip()
fp_override = (forma_pago_map or {}).get(contrato, "")
return generar_factura_venta(grupo_rows, default_vendedor="00",
default_prefijo=prefijo_def, numero_override=num_fac)
default_prefijo=prefijo_def, numero_override=num_fac,
forma_pago_override=fp_override)
def _is_sent(factura_key):
@@ -177,6 +180,7 @@ async def preview_ventas(
return JSONResponse({"success": False, "message": "Sin datos para ese rango de fechas"})
excluded_ventas = load_excluded_ventas_set()
fp_map = load_forma_pago_map()
grupos_all = _agrupar(rows)
grupos = {k: v for k, v in grupos_all.items()
if str(v[0].get("CODCONTRATO") or "").strip() not in excluded_ventas}
@@ -184,7 +188,7 @@ async def preview_ventas(
items = []
for factura_key, grupo_rows in grupos.items():
enviado = _is_sent(factura_key)
venta_json = _build_venta(grupo_rows, cfg)
venta_json = _build_venta(grupo_rows, cfg, fp_map)
items.append({
"factura_key": factura_key,
"factura": factura_key,
@@ -242,7 +246,8 @@ async def send_one(
headers = {"Content-Type": "application/json", "Authorization": f"Bearer {token}"}
api_sucursal = cfg.get("api_sucursal", "") or "00"
endpoint = f"{TNS_BASE}/v2/facturacion/Ventas/Crear?codigosucursal={api_sucursal}"
venta_json = _build_venta(grupo_rows, cfg)
fp_map = load_forma_pago_map()
venta_json = _build_venta(grupo_rows, cfg, fp_map)
contrato = str(grupo_rows[0].get("CODCONTRATO") or "").strip()
raw_resp = ""
@@ -279,6 +284,7 @@ async def send_ventas(
return JSONResponse({"success": False, "message": "Sin datos"})
excluded_ventas = load_excluded_ventas_set()
fp_map = load_forma_pago_map()
grupos_all = _agrupar(rows)
grupos = {k: v for k, v in grupos_all.items()
if str(v[0].get("CODCONTRATO") or "").strip() not in excluded_ventas}
@@ -298,7 +304,7 @@ async def send_ventas(
resultados = []
async with httpx.AsyncClient(timeout=int(cfg.get("api_timeout", 30))) as client:
for factura_key, grupo_rows in grupos.items():
venta_json = _build_venta(grupo_rows, cfg)
venta_json = _build_venta(grupo_rows, cfg, fp_map)
contrato = str(grupo_rows[0].get("CODCONTRATO") or "").strip()
raw_resp = ""
ok_v = False