From e278695137ec391a491ab5288b5abd1f6830dd72 Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Wed, 12 Nov 2025 17:31:55 -0300 Subject: [PATCH] Load api data once --- .../views/ventas/reservations.blade.php | 159 ++++++++++++++++-- .../modal/common/scripts/modal.blade.php | 40 +++-- 2 files changed, 176 insertions(+), 23 deletions(-) diff --git a/app/resources/views/ventas/reservations.blade.php b/app/resources/views/ventas/reservations.blade.php index 3f7819d..7a01dfd 100644 --- a/app/resources/views/ventas/reservations.blade.php +++ b/app/resources/views/ventas/reservations.blade.php @@ -130,7 +130,7 @@ } component_id = '' component = null - current_project = null; + current_project = null title_id = '' title_component = null @@ -165,6 +165,7 @@ reservations.get.reservations(project_id) reservations.components.modals.add.load(project_id) + reservations.components.modals.edit.load(project_id) this.hide() this.title_component.innerHTML = event.currentTarget.innerHTML @@ -311,7 +312,7 @@ oferta: reservation => `${this.formatters.ufs.format(reservation.offer)} UF`, valida: reservation => reservation.valid ? 'Si' : 'No', operador: reservation => reservation.broker?.name ?? '', - }; + } const tooltipVariation = 'very wide multiline' const tooltips = this.draw().tooltip() return this.reservations.map(reservation => { @@ -422,10 +423,10 @@ switch (promotion.type) { case {{ \Incoviba\Model\Venta\Promotion\Type::FIXED }}: value = `${formatter.format(promotion.amount)} UF` - break; + break case {{ \Incoviba\Model\Venta\Promotion\Type::VARIABLE }}: value = `${formatter.format(promotion.amount * 100)} %` - break; + break } table.push(`${promotion.description}:${value}`) }) @@ -515,7 +516,7 @@ } get columnsData() { - const data = super.columnsData; + const data = super.columnsData return data.map(row => { delete (row['valida']) return row @@ -558,8 +559,7 @@ edit: event => { event.preventDefault() const id = event.currentTarget.dataset.id - const project_id = {{ $project_id ?? 'null' }}; - reservations.components.modals.edit.load({project_id, type: 'active', reservation_id: id}) + reservations.components.modals.edit.show({type: 'active', reservation_id: id}) return false }, remove: event => { @@ -608,7 +608,7 @@ reservation.units.forEach(unit => { let type = unit.unit.proyecto_tipo_unidad.tipo_unidad.descripcion type = type.charAt(0).toUpperCase() + type.slice(1) - const base = unit.base ?? (unit.value ?? 0); + const base = unit.base ?? (unit.value ?? 0) const price = unit.unit.current_precio?.valor ?? 0 const diff = (base - price) / price * 100 const rowsData = [ @@ -639,10 +639,10 @@ switch (promotion.type) { case {{ \Incoviba\Model\Venta\Promotion\Type::FIXED }}: value = `${formatter.format(promotion.amount)} UF` - break; + break case {{ \Incoviba\Model\Venta\Promotion\Type::VARIABLE }}: value = `${formatter.format(promotion.amount * 100)} %` - break; + break } table.push(`${promotion.description}:${value}`) }) @@ -660,8 +660,7 @@ edit: event => { event.preventDefault() const id = event.currentTarget.dataset.id - const project_id = {{ $project_id ?? 'null' }}; - reservations.components.modals.edit.load({project_id, type: 'pending', reservation_id: id}) + reservations.components.modals.edit.show({type: 'pending', reservation_id: id}) return false }, approve: event => { @@ -737,6 +736,18 @@ loader: '', results: '', }, + data: { + comunas: {}, + brokers: {}, + promotions: {}, + units: {} + }, + locks: { + comunas: null, + brokers: null, + promotions: null, + units: null + }, get: { send: (project_id, url_segment, component) => { const url = `/api/ventas/reservations/project/${project_id}/${url_segment}` @@ -781,7 +792,129 @@ return current_url.pathname.replace(/project\/\d+/, `project/${project_id}`) } return `${current_url.pathname}/project/${project_id}` - } + }, + comunas: region_id => { + if (region_id in reservations.data.comunas) { + reservations.locks.comunas = null + return new Promise(resolve => { + resolve(reservations.data.comunas[region_id]) + }) + } + if (reservations.locks.comunas !== null) { + return reservations.locks.comunas + } + const uri = `/api/region/${region_id}/comunas` + reservations.locks.comunas = APIClient.fetch(uri).then(response => response.json()).then(json => { + if (json.comunas.length === 0) { + return + } + reservations.data.comunas[region_id] = json.comunas.map(comuna => { + return { + text: comuna.descripcion, + name: comuna.descripcion, + value: comuna.id + } + }) + return reservations.data.comunas[region_id] + }) + return reservations.locks.comunas + }, + brokers: project_id => { + if (project_id in reservations.data.brokers) { + reservations.locks.brokers = null + return new Promise((resolve, reject) => { + resolve(reservations.data.brokers[project_id]) + }) + } + if (reservations.locks.brokers !== null) { + return reservations.locks.brokers + } + const uri = `/api/proyecto/${project_id}/brokers` + reservations.locks.brokers = APIClient.fetch(uri).then(response => response.json()).then(json => { + if (json.contracts.length === 0) { + return + } + const formatter = new Intl.NumberFormat('es-CL', { style: 'percent', minimumFractionDigits: 2 }) + reservations.data.brokers[project_id] = json.contracts.map(contract => { + return { + id: contract.id, + broker_rut: contract.broker_rut, + commission: formatter.format(contract.commission), + name: '', + text: '', + } + }) + const promises = [] + json.contracts.forEach(contract => { + promises.push(reservations.get.broker(contract.broker_rut)) + }) + + return Promise.all(promises).then(data => { + data.forEach(broker => { + if (!('rut' in broker)) { + return + } + const idx = reservations.data.brokers[project_id].findIndex(contract => contract.broker_rut === broker.rut) + reservations.data.brokers[project_id][idx].name = reservations.data.brokers[project_id][idx].text = `${broker.name} - ${reservations.data.brokers[project_id][idx].commission}` + }) + }) + }) + return reservations.locks.brokers + }, + broker: (broker_rut) => { + const uri = `/api/proyectos/broker/${broker_rut}` + return APIClient.fetch(uri).then(response => response.json()).then(json => { + if (!('broker' in json)) { + return [] + } + return json.broker + }) + }, + promotions: project_id => { + if (project_id in reservations.data.promotions) { + reservations.locks.promotions = null + return new Promise((resolve, reject) => { + resolve(this.data.promotions[project_id]) + }) + } + if (reservations.locks.promotions !== null) { + return reservations.locks.promotions + } + const uri = `/api/proyecto/${project_id}/promotions` + reservations.locks.promotions = APIClient.fetch(uri).then(response => response.json()).then(json => { + if (json.promotions.length === 0) { + return reservations.data.promotions[project_id] = [] + } + return reservations.data.promotions[project_id] = json.promotions + }) + return reservations.locks.promotions + }, + units: project_id => { + if (project_id in reservations.data.units) { + return new Promise((resolve, reject) => { + resolve(reservations.data.units[project_id]) + }) + } + const uri = `/api/proyecto/${project_id}/unidades/disponibles` + return APIClient.fetch(uri).then(response => response.json()).then(json => { + if (json.unidades.length === 0) { + reservations.data.units[project_id] = {} + return reservations.data.units[project_id] + } + if (!(project_id in reservations.data.units)) { + reservations.data.units[project_id] = {} + } + json.unidades.forEach(unit => { + const type = unit.proyecto_tipo_unidad.tipo_unidad.descripcion + if (!(type in reservations.data.units[project_id])) { + reservations.data.units[project_id][type] = [] + } + reservations.data.units[project_id][type].push(unit) + }) + + return reservations.data.units[project_id] + }) + }, }, loading: { show: () => { diff --git a/app/resources/views/ventas/reservations/modal/common/scripts/modal.blade.php b/app/resources/views/ventas/reservations/modal/common/scripts/modal.blade.php index 533d348..ae5f4db 100644 --- a/app/resources/views/ventas/reservations/modal/common/scripts/modal.blade.php +++ b/app/resources/views/ventas/reservations/modal/common/scripts/modal.blade.php @@ -89,7 +89,8 @@ this.components.form.querySelector(`input[name="${this.prefix}_project_id"]`).value = project_id this.get.brokers(project_id) - this.get.promotions(project_id).then(promotions => { + this.get.promotions(project_id).then(() => { + const promotions = reservations.data.promotions[project_id] this.components.promotions.data.promotions = promotions.map(promotion => { return { text: promotion.description, @@ -99,7 +100,8 @@ }) this.components.promotions.draw.promotions() }) - this.get.units(project_id).then(unitTypes => { + this.get.units(project_id).then(() => { + const unitTypes = reservations.data.units[project_id] Object.entries(unitTypes).map(([type, units]) => { units = units.map(unit => { return { @@ -132,7 +134,15 @@ } return } - const uri = `/api/region/${region_id}/comunas` + return reservations.get.comunas(region_id).then(comunas => { + this.data.comunas[region_id] = comunas + this.components.$comuna.dropdown('change values', this.data.comunas[region_id]) + + if (this.data.current_user !== null && this.data.current_user?.direccion?.comuna !== null) { + this.components.$comuna.dropdown('set selected', this.data.current_user.direccion.comuna.id) + } + }) + /*const uri = `/api/region/${region_id}/comunas` return APIClient.fetch(uri).then(response => response.json()).then(json => { if (json.comunas.length === 0) { return @@ -149,7 +159,7 @@ if (this.data.current_user !== null && this.data.current_user?.direccion?.comuna !== null) { this.components.$comuna.dropdown('set selected', this.data.current_user.direccion.comuna.id) } - }) + })*/ }, brokers: project_id => { if (project_id in this.data.brokers) { @@ -157,7 +167,11 @@ resolve(this.data.brokers[project_id]) }) } - const uri = `/api/proyecto/${project_id}/brokers` + return reservations.get.brokers(project_id).then(brokers => { + this.data.brokers[project_id] = brokers + this.fill.brokers() + }) + /*const uri = `/api/proyecto/${project_id}/brokers` return APIClient.fetch(uri).then(response => response.json()).then(json => { if (json.contracts.length === 0) { return @@ -187,7 +201,7 @@ }) this.fill.brokers() }) - }) + })*/ }, broker: (broker_rut) => { const uri = `/api/proyectos/broker/${broker_rut}` @@ -204,13 +218,16 @@ resolve(this.data.promotions[project_id]) }) } - const uri = `/api/proyecto/${project_id}/promotions` + return reservations.get.promotions(project_id).then(promotions => { + this.data.promotions[project_id] = promotions + }) + /*const uri = `/api/proyecto/${project_id}/promotions` return APIClient.fetch(uri).then(response => response.json()).then(json => { if (json.promotions.length === 0) { return this.data.promotions[project_id] = [] } return this.data.promotions[project_id] = json.promotions - }) + })*/ }, units: project_id => { if (project_id in this.data.units) { @@ -218,7 +235,10 @@ resolve(this.data.units[project_id]) }) } - const uri = `/api/proyecto/${project_id}/unidades/disponibles` + return reservations.get.units(project_id).then(units => { + this.data.units[project_id] = units + }) + /*const uri = `/api/proyecto/${project_id}/unidades/disponibles` return APIClient.fetch(uri).then(response => response.json()).then(json => { if (json.unidades.length === 0) { this.data.units[project_id] = {} @@ -236,7 +256,7 @@ }) return this.data.units[project_id] - }) + })*/ }, user: rut => { if (this.data.current_user !== null && this.data.current_user?.rut === rut) {