WIP jordan
This commit is contained in:
88
app/composables/usePolicyRegistrationModel.ts
Normal file
88
app/composables/usePolicyRegistrationModel.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
import type { Ref } from 'vue'
|
||||
import type { PolicyInstallmentRow, PolicyRegistration } from '~/types/brokerage-registration'
|
||||
import { POLICY_DRAFT_STORAGE_KEY } from '~/types/brokerage-registration'
|
||||
|
||||
export function createEmptyPolicyRegistration(): PolicyRegistration {
|
||||
return {
|
||||
mintPolicyNumber: '',
|
||||
contratanteId: '',
|
||||
ramo: '',
|
||||
subRamo: '',
|
||||
aseguradora: '',
|
||||
producto: '',
|
||||
agencia: '',
|
||||
numeroPolizaProveedor: '',
|
||||
acreedor: '',
|
||||
fechaEmision: '',
|
||||
inicioVigencia: '',
|
||||
finVigencia: '',
|
||||
comisiones: [
|
||||
{ idx: 1, agenteId: '', porcentaje: '' },
|
||||
{ idx: 2, agenteId: '', porcentaje: '' },
|
||||
{ idx: 3, agenteId: '', porcentaje: '' }
|
||||
],
|
||||
formaPago: '',
|
||||
valorAsegurado: '',
|
||||
primaBruta: '',
|
||||
impuestoPct: '6',
|
||||
primaNeta: '',
|
||||
numCuotas: 10,
|
||||
cuotas: [],
|
||||
cotizacionMintId: '',
|
||||
pdfCotizacionNombre: '',
|
||||
pdfPolizaNombre: '',
|
||||
notas: ''
|
||||
}
|
||||
}
|
||||
|
||||
export function rebuildInstallmentSchedule(p: PolicyRegistration): PolicyInstallmentRow[] {
|
||||
const n = Math.max(1, Math.min(60, Math.floor(p.numCuotas) || 1))
|
||||
const start = p.inicioVigencia ? new Date(p.inicioVigencia) : new Date()
|
||||
const base = Number.isNaN(start.getTime()) ? new Date() : start
|
||||
const per = p.primaBruta
|
||||
? (Number.parseFloat(String(p.primaBruta).replace(/[^0-9.-]/g, '')) || 0) / n
|
||||
: 0
|
||||
const rows: PolicyInstallmentRow[] = []
|
||||
for (let i = 0; i < n; i++) {
|
||||
const d = new Date(base)
|
||||
d.setMonth(d.getMonth() + i)
|
||||
rows.push({
|
||||
n: i + 1,
|
||||
fechaVencimiento: d.toISOString().slice(0, 16),
|
||||
prima: per > 0 ? per.toFixed(2) : ''
|
||||
})
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
export function setFinOneYearAfterInicio(p: PolicyRegistration) {
|
||||
if (!p.inicioVigencia) return
|
||||
const d = new Date(p.inicioVigencia)
|
||||
if (Number.isNaN(d.getTime())) return
|
||||
d.setFullYear(d.getFullYear() + 1)
|
||||
p.finVigencia = d.toISOString().slice(0, 16)
|
||||
}
|
||||
|
||||
export function usePolicyDraftPersistence(form: Ref<PolicyRegistration>) {
|
||||
if (import.meta.server) return
|
||||
try {
|
||||
const raw = localStorage.getItem(POLICY_DRAFT_STORAGE_KEY)
|
||||
if (raw) {
|
||||
const parsed = JSON.parse(raw) as PolicyRegistration
|
||||
form.value = { ...createEmptyPolicyRegistration(), ...parsed, cuotas: parsed.cuotas ?? [] }
|
||||
}
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
watch(
|
||||
form,
|
||||
(v) => {
|
||||
try {
|
||||
localStorage.setItem(POLICY_DRAFT_STORAGE_KEY, JSON.stringify(v))
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
},
|
||||
{ deep: true }
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user