feat: API Gateway (Express 5 + Prisma + Monday) y conexion de apps al API
- api-gateway: servicio Monday (retry 429/complejidad, paginacion cursor, column IDs por titulo), endpoints unidades/cotizaciones/clientes/planes, webhook con handshake challenge, Prisma SQLite dev, 55 tests - cotizador: inventario desde /api/unidades con fallback a seed - alta-clientes: POST /api/clientes + upload multipart de documentos - cobranza: persistencia via API con respaldo offline en localStorage - .env.example con board IDs del workspace Frasa-CAS
This commit is contained in:
245
api-gateway/src/services/cotizacion.service.ts
Normal file
245
api-gateway/src/services/cotizacion.service.ts
Normal file
@@ -0,0 +1,245 @@
|
||||
/**
|
||||
* Cálculos replicados fielmente del Excel de producción "COTIZADOR" de
|
||||
* Frasa Desarrollos (port de apps/cotizador/src/lib/calculations.ts).
|
||||
* NO "corregir" las fórmulas: algunas usan precio de lista donde
|
||||
* intuitivamente iría precio final; así está en el Excel y así debe quedar.
|
||||
*/
|
||||
|
||||
export interface UnidadCotizable {
|
||||
codigo: string
|
||||
modelo: string
|
||||
fechaEntrega: string // ISO 'YYYY-MM-DD' (terminación de obra)
|
||||
supEstacionamiento: number
|
||||
supConstruccion: number
|
||||
supExcedente: number
|
||||
precioSinAcabados: number
|
||||
precioConAcabados: number
|
||||
status: 'DISPONIBLE' | 'APARTADA' | 'VENDIDA'
|
||||
desarrollo: string
|
||||
}
|
||||
|
||||
export type TipoPropuesta =
|
||||
| 'CREDITO_BANCARIO'
|
||||
| 'CREDITO_INFONAVIT_COFINAVIT'
|
||||
| 'CONTADO'
|
||||
| 'SEMI_CONTADO'
|
||||
|
||||
export interface DescuentosPropuestas {
|
||||
creditoBancario: number
|
||||
creditoInfonavit: number
|
||||
contado: number
|
||||
semiContado: number
|
||||
}
|
||||
|
||||
export const APARTADO_DEFAULT = 20000
|
||||
|
||||
// Descuentos por defecto de cada propuesta (fracción, no porcentaje)
|
||||
export const DESCUENTOS_DEFAULT: DescuentosPropuestas = {
|
||||
creditoBancario: 0,
|
||||
creditoInfonavit: 0,
|
||||
contado: 0.02,
|
||||
semiContado: 0.01,
|
||||
}
|
||||
|
||||
export interface PropuestaResultado {
|
||||
tipo: TipoPropuesta
|
||||
descuento: number // fracción (0.02 = 2%)
|
||||
montoDescuento: number
|
||||
precioFinal: number // precio lista - descuento
|
||||
enganche: number // NETO del apartado (el apartado ya se pagó)
|
||||
numMensualidades: number
|
||||
mensualidad: number | null // null => N/A (Contado)
|
||||
mensualidadesTotal: number | null // null => N/A (Contado)
|
||||
pagoFinal: number
|
||||
}
|
||||
|
||||
export interface CotizacionInput {
|
||||
unidad: UnidadCotizable
|
||||
acabados: boolean // true => incluye cocina y closet
|
||||
apartado: number
|
||||
descuentos: DescuentosPropuestas
|
||||
/** Permite forzar N en tests; por defecto se calcula desde fechaEntrega */
|
||||
numMeses?: number
|
||||
}
|
||||
|
||||
const MS_POR_MES = 30.4 * 86400000
|
||||
|
||||
/** Meses (redondeados, mínimo 1) desde hoy hasta la terminación de obra */
|
||||
export function calcularNumMensualidades(fechaEntrega: string, hoy: Date = new Date()): number {
|
||||
const entrega = new Date(`${fechaEntrega}T00:00:00`)
|
||||
return Math.max(1, Math.round((entrega.getTime() - hoy.getTime()) / MS_POR_MES))
|
||||
}
|
||||
|
||||
function baseComun(precioLista: number, descuento: number) {
|
||||
// Excel COTIZADOR: MONTO DE DESCUENTO y PRECIO FINAL C/DESC
|
||||
const montoDescuento = precioLista * descuento
|
||||
const precioFinal = precioLista - montoDescuento
|
||||
return { montoDescuento, precioFinal }
|
||||
}
|
||||
|
||||
/** PROPUESTA 1 — CRÉDITO BANCARIO */
|
||||
function calcularBancario(precioLista: number, apartado: number, descuento: number, n: number): PropuestaResultado {
|
||||
const { montoDescuento, precioFinal } = baseComun(precioLista, descuento)
|
||||
// Excel COTIZADOR!M17 — enganche 2.5% sobre precio LISTA, neto del apartado
|
||||
const enganche = 0.025 * precioLista - apartado
|
||||
// Excel COTIZADOR!M19 — pago final 90% del precio final c/desc
|
||||
const pagoFinal = 0.9 * precioFinal
|
||||
// Excel COTIZADOR!M18 — remanente a mensualidades
|
||||
const mensualidadesTotal = precioFinal - apartado - enganche - pagoFinal
|
||||
return {
|
||||
tipo: 'CREDITO_BANCARIO',
|
||||
descuento,
|
||||
montoDescuento,
|
||||
precioFinal,
|
||||
enganche,
|
||||
numMensualidades: n,
|
||||
mensualidad: mensualidadesTotal / n,
|
||||
mensualidadesTotal,
|
||||
pagoFinal,
|
||||
}
|
||||
}
|
||||
|
||||
/** PROPUESTA 2 — CRÉDITO INFONAVIT-COFINAVIT */
|
||||
function calcularInfonavit(precioLista: number, apartado: number, descuento: number, n: number): PropuestaResultado {
|
||||
const { montoDescuento, precioFinal } = baseComun(precioLista, descuento)
|
||||
// Excel COTIZADOR!M26 — enganche 2.5% sobre precio LISTA, neto del apartado
|
||||
const enganche = 0.025 * precioLista - apartado
|
||||
// Excel COTIZADOR!M28 — pago final 95% del precio final c/desc
|
||||
const pagoFinal = 0.95 * precioFinal
|
||||
// Excel COTIZADOR!M27 — OJO: el Excel usa precioLista aquí, NO precioFinal. Replicar tal cual.
|
||||
const mensualidadesTotal = precioLista - pagoFinal - apartado - enganche
|
||||
return {
|
||||
tipo: 'CREDITO_INFONAVIT_COFINAVIT',
|
||||
descuento,
|
||||
montoDescuento,
|
||||
precioFinal,
|
||||
enganche,
|
||||
numMensualidades: n,
|
||||
mensualidad: mensualidadesTotal / n,
|
||||
mensualidadesTotal,
|
||||
pagoFinal,
|
||||
}
|
||||
}
|
||||
|
||||
/** PROPUESTA 3 — CONTADO (sin mensualidades) */
|
||||
function calcularContado(precioLista: number, apartado: number, descuento: number, n: number): PropuestaResultado {
|
||||
const { montoDescuento, precioFinal } = baseComun(precioLista, descuento)
|
||||
// Excel COTIZADOR!M36 — enganche 90% del precio final c/desc, neto del apartado
|
||||
const enganche = 0.9 * precioFinal - apartado
|
||||
// Excel COTIZADOR!M37 — pago final 10% del precio final c/desc
|
||||
const pagoFinal = 0.1 * precioFinal
|
||||
return {
|
||||
tipo: 'CONTADO',
|
||||
descuento,
|
||||
montoDescuento,
|
||||
precioFinal,
|
||||
enganche,
|
||||
numMensualidades: n,
|
||||
mensualidad: null, // N/A
|
||||
mensualidadesTotal: null, // N/A
|
||||
pagoFinal,
|
||||
}
|
||||
}
|
||||
|
||||
/** PROPUESTA 4 — SEMI CONTADO */
|
||||
function calcularSemiContado(precioLista: number, apartado: number, descuento: number, n: number): PropuestaResultado {
|
||||
const { montoDescuento, precioFinal } = baseComun(precioLista, descuento)
|
||||
// Excel COTIZADOR!M45 — enganche 50% del precio final c/desc, neto del apartado
|
||||
const enganche = 0.5 * precioFinal - apartado
|
||||
// Excel COTIZADOR!M46 — 40% del precio final a mensualidades (= 1 - 0.5 - 0.1)
|
||||
const mensualidadesTotal = precioFinal * 0.4
|
||||
// Excel COTIZADOR!M47 — pago final 10% del precio final c/desc
|
||||
const pagoFinal = 0.1 * precioFinal
|
||||
return {
|
||||
tipo: 'SEMI_CONTADO',
|
||||
descuento,
|
||||
montoDescuento,
|
||||
precioFinal,
|
||||
enganche,
|
||||
numMensualidades: n,
|
||||
mensualidad: mensualidadesTotal / n,
|
||||
mensualidadesTotal,
|
||||
pagoFinal,
|
||||
}
|
||||
}
|
||||
|
||||
/** Precio de lista según si incluye cocina y closet (acabados) */
|
||||
export function obtenerPrecioLista(unidad: UnidadCotizable, acabados: boolean): number {
|
||||
return acabados ? unidad.precioConAcabados : unidad.precioSinAcabados
|
||||
}
|
||||
|
||||
/**
|
||||
* Total pagado por el cliente en una propuesta:
|
||||
* apartado + enganche (neto del apartado) + mensualidades + pago final.
|
||||
*/
|
||||
export function totalPagado(p: PropuestaResultado, apartado: number): number {
|
||||
return apartado + p.enganche + (p.mensualidadesTotal ?? 0) + p.pagoFinal
|
||||
}
|
||||
|
||||
/** Índice del menor total pagado; en empate gana el primero. -1 si la lista está vacía */
|
||||
export function indiceMenorTotal(totales: number[]): number {
|
||||
if (totales.length === 0) return -1
|
||||
let indice = 0
|
||||
for (let i = 1; i < totales.length; i++) {
|
||||
const actual = totales[i]
|
||||
const mejor = totales[indice]
|
||||
if (actual !== undefined && mejor !== undefined && actual < mejor) indice = i
|
||||
}
|
||||
return indice
|
||||
}
|
||||
|
||||
/** Calcula las 4 propuestas comerciales */
|
||||
export function calcularPropuestas(input: CotizacionInput): PropuestaResultado[] {
|
||||
const { unidad, acabados, apartado, descuentos } = input
|
||||
const precioLista = obtenerPrecioLista(unidad, acabados)
|
||||
const n = input.numMeses ?? calcularNumMensualidades(unidad.fechaEntrega)
|
||||
return [
|
||||
calcularBancario(precioLista, apartado, descuentos.creditoBancario, n),
|
||||
calcularInfonavit(precioLista, apartado, descuentos.creditoInfonavit, n),
|
||||
calcularContado(precioLista, apartado, descuentos.contado, n),
|
||||
calcularSemiContado(precioLista, apartado, descuentos.semiContado, n),
|
||||
]
|
||||
}
|
||||
|
||||
/** Mapa entre el nombre del API (body) y el nombre interno de la propuesta */
|
||||
export const TIPO_PROPUESTA_API = {
|
||||
BANCARIO: 'CREDITO_BANCARIO',
|
||||
INFONAVIT: 'CREDITO_INFONAVIT_COFINAVIT',
|
||||
CONTADO: 'CONTADO',
|
||||
SEMI_CONTADO: 'SEMI_CONTADO',
|
||||
} as const
|
||||
|
||||
export type TipoPropuestaApi = keyof typeof TIPO_PROPUESTA_API
|
||||
|
||||
const DESCUENTO_DEFAULT_POR_TIPO: Record<TipoPropuestaApi, number> = {
|
||||
BANCARIO: DESCUENTOS_DEFAULT.creditoBancario,
|
||||
INFONAVIT: DESCUENTOS_DEFAULT.creditoInfonavit,
|
||||
CONTADO: DESCUENTOS_DEFAULT.contado,
|
||||
SEMI_CONTADO: DESCUENTOS_DEFAULT.semiContado,
|
||||
}
|
||||
|
||||
/**
|
||||
* Calcula UNA propuesta (la elegida por el cliente) para persistir/cotizar.
|
||||
* `descuento` opcional (fracción); si no viene, usa el default del tipo.
|
||||
*/
|
||||
export function calcularPropuesta(
|
||||
unidad: UnidadCotizable,
|
||||
tipo: TipoPropuestaApi,
|
||||
acabados: boolean,
|
||||
apartado: number = APARTADO_DEFAULT,
|
||||
descuento?: number,
|
||||
): PropuestaResultado {
|
||||
const precioLista = obtenerPrecioLista(unidad, acabados)
|
||||
const n = calcularNumMensualidades(unidad.fechaEntrega)
|
||||
const d = descuento ?? DESCUENTO_DEFAULT_POR_TIPO[tipo]
|
||||
switch (tipo) {
|
||||
case 'BANCARIO':
|
||||
return calcularBancario(precioLista, apartado, d, n)
|
||||
case 'INFONAVIT':
|
||||
return calcularInfonavit(precioLista, apartado, d, n)
|
||||
case 'CONTADO':
|
||||
return calcularContado(precioLista, apartado, d, n)
|
||||
case 'SEMI_CONTADO':
|
||||
return calcularSemiContado(precioLista, apartado, d, n)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user