From 52cc463ff1df4233692acb03865ba2ed70d2c165 Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Thu, 28 Aug 2025 12:11:33 -0400 Subject: [PATCH] Unidades disponibles y nombre de proyecto --- .../views/ventas/reservations.blade.php | 143 ++++++++++-------- .../add.blade.php} | 36 +++-- 2 files changed, 102 insertions(+), 77 deletions(-) rename app/resources/views/ventas/reservations/{add_modal.blade.php => modal/add.blade.php} (95%) diff --git a/app/resources/views/ventas/reservations.blade.php b/app/resources/views/ventas/reservations.blade.php index 2a0a4a4..3319bfe 100644 --- a/app/resources/views/ventas/reservations.blade.php +++ b/app/resources/views/ventas/reservations.blade.php @@ -10,19 +10,19 @@
Proyectos
- @if (count($projects) == 0) -
- No hay proyectos en venta. -
- @else -
@@ -101,7 +101,7 @@
- @include('ventas.reservations.add_modal') + @include('ventas.reservations.modal.add') @endsection @push('page_styles') @@ -160,20 +160,24 @@ this.title_component.innerHTML = event.currentTarget.innerHTML } + watch() { this.component.querySelectorAll('.item.link').forEach(item => { item.addEventListener('click', this.select.bind(this)) }) } + show() { this.component.style.display = this.display.projects this.title_component.style.display = 'none' } + hide() { this.component.style.display = 'none' this.title_component.style.display = this.display.project } } + class Controls { display = { up: '', @@ -208,10 +212,12 @@ value.addEventListener('click', this.action[name].bind(this)) }) } + hide() { this.buttons.up.style.display = this.display.up this.buttons.reset.style.display = this.display.reset } + show() { this.buttons.up.style.display = 'none' this.buttons.reset.style.display = 'none' @@ -235,6 +241,7 @@ } } } + class Reservations { display = { reservations: '', @@ -256,34 +263,33 @@ this.formatters = formatters - this.set().columnNames() + this.set.columnNames() this.hide() } - set() { - return { - reservations: reservations => { - this.reservations = reservations - return this - }, - columnNames: () => { - const tds = this.component.querySelector('thead tr').querySelectorAll('th') - this.columnNames = [] - tds.forEach(td => { - let name = td.innerHTML.toLowerCase() - if (name === '') { - return - } - if (name.includes('?')) { - name = name.replaceAll(/[¿?]/g, '') - } - this.columnNames.push(name) - }) - return this - } + set = { + reservations: reservations => { + this.reservations = reservations + return this + }, + columnNames: () => { + const tds = this.component.querySelector('thead tr').querySelectorAll('th') + this.columnNames = [] + tds.forEach(td => { + let name = td.innerHTML.toLowerCase() + if (name === '') { + return + } + if (name.includes('?')) { + name = name.replaceAll(/[¿?]/g, '') + } + this.columnNames.push(name) + }) + return this } } + columnsData() { return this.reservations.map(reservation => { const date = new Date(Date.parse(reservation.fecha) + 24 * 60 * 60 * 1000) @@ -298,7 +304,12 @@ } }) } + draw() { + if (this.reservations.length === 0) { + this.empty() + return + } const tbody = this.component.querySelector('tbody') tbody.innerHTML = '' this.columnsData().forEach(column => { @@ -317,6 +328,7 @@ }) this.show() } + drawActions(id) { return ` @@ -328,6 +340,7 @@ ` } + empty() { const tbody = this.component.querySelector('tbody') tbody.innerHTML = '' @@ -342,21 +355,25 @@ show() { this.component.style.display = this.display.reservations } + hide() { this.component.style.display = 'none' } } + class ActiveReservations extends Reservations { constructor({component_id, formatters = {date, ufs}}) { super({component_id, formatters}) } + columnsData() { const data = super.columnsData(); return data.map(row => { - delete(row['valida']) + delete (row['valida']) return row }) } + drawActions(id) { return ` @@ -369,11 +386,13 @@ ` } } + class PendingReservations extends Reservations { constructor({component_id, formatters = {date, ufs}}) { super({component_id, formatters}) } } + class RejectedReservations extends Reservations { constructor({component_id, formatters = {date, ufs}}) { super({component_id, formatters}) @@ -392,6 +411,7 @@ return '' } } + const reservations = { components: { projects: null, @@ -415,42 +435,20 @@ send: (project_id, url_segment, component) => { const url = `/api/ventas/reservations/project/${project_id}/${url_segment}` return APIClient.fetch(url).then(response => response.json()).then(json => { - if (json.reservations.length === 0) { - component.empty() - return + if (json.reservations.length > 0) { + component.set.reservations(json.reservations) } - component.set().reservations(json.reservations).draw() + component.draw() }) }, active: project_id => { return reservations.get.send(project_id, 'active', reservations.components.reservations.active) - /*const url = `/ventas/reservations/project/${project_id}/active` - return APIClient.fetch(url).then(json => { - if (json.reservations.length === 0) { - return - } - this.components.reservations.active.set().reservations(json.reservations).draw() - })*/ }, pending: project_id => { return reservations.get.send(project_id, 'pending', reservations.components.reservations.pending) - /*const url = `/ventas/reservations/project/${project_id}/pending` - return APIClient.fetch(url).then(json => { - if (json.reservations.length === 0) { - return - } - this.components.reservations.pending.set().reservations(json.reservations).draw() - })*/ }, rejected: project_id => { return reservations.get.send(project_id, 'rejected', reservations.components.reservations.rejected) - /*const url = `/ventas/reservations/project/${project_id}/rejected` - return APIClient.fetch(url).then(json => { - if (json.reservations.length === 0) { - return - } - this.components.reservations.rejected.set().reservations(json.reservations).draw() - })*/ }, reservations: project_id => { reservations.loading.show() @@ -510,6 +508,7 @@ results: () => { reservations.components.projects.hide() reservations.components.results.style.display = reservations.display.results + Object.values(reservations.components.reservations).forEach(reservations => reservations.draw()) } }, setup(configuration) { @@ -518,11 +517,23 @@ ufs: new Intl.NumberFormat('es-CL', {minimumFractionDigits: 2, maximumFractionDigits: 2}) } this.components.loader = document.getElementById(configuration.ids.loader) - this.components.projects = new Projects({component_id: configuration.ids.projects, title_id: configuration.ids.project}) + this.components.projects = new Projects({ + component_id: configuration.ids.projects, + title_id: configuration.ids.project + }) this.components.controls = new Controls({component_id: configuration.ids.controls}) - this.components.reservations.active = new ActiveReservations({component_id: configuration.ids.active, formatters}) - this.components.reservations.pending = new PendingReservations({component_id: configuration.ids.pending, formatters}) - this.components.reservations.rejected = new RejectedReservations({component_id: configuration.ids.rejected, formatters}) + this.components.reservations.active = new ActiveReservations({ + component_id: configuration.ids.active, + formatters + }) + this.components.reservations.pending = new PendingReservations({ + component_id: configuration.ids.pending, + formatters + }) + this.components.reservations.rejected = new RejectedReservations({ + component_id: configuration.ids.rejected, + formatters + }) this.display.loader = this.components.loader.style.display this.loading.hide() @@ -532,7 +543,7 @@ this.display.results = this.components.results.style.display this.show.projects() - this.components.modals.add = new AddReservationModal() + this.components.modals.add = new AddReservationModal(configuration.ids.projects) } } $(document).ready(() => { diff --git a/app/resources/views/ventas/reservations/add_modal.blade.php b/app/resources/views/ventas/reservations/modal/add.blade.php similarity index 95% rename from app/resources/views/ventas/reservations/add_modal.blade.php rename to app/resources/views/ventas/reservations/modal/add.blade.php index 90a0251..c36d383 100644 --- a/app/resources/views/ventas/reservations/add_modal.blade.php +++ b/app/resources/views/ventas/reservations/modal/add.blade.php @@ -128,7 +128,7 @@
-

Unidades

+

Unidades

@@ -365,6 +365,8 @@ broker: '', promotion_button: '', promotions: '', + projects: '', + project_name: '', unit_buttons: '', units: '' } @@ -381,6 +383,8 @@ $broker: null, promotion_button: null, promotions: null, + projects: null, + project_name: null, unit_buttons: null, units: null, $loader: null @@ -403,7 +407,7 @@ terraza: 'tree', } } - constructor() { + constructor(projects_id) { this.ids = { modal: 'add_reservation_modal', form: 'add_reservation_form', @@ -416,6 +420,8 @@ broker: 'add_broker', promotion_button: 'add_promotion', promotions: 'add_promotions', + projects: projects_id, + project_name: 'add_project_name', unit_buttons: 'add_unit_buttons', units: 'add_units', loader: 'add_rut_loader' @@ -425,6 +431,7 @@ load(project_id) { this.reset() this.data.current_project = project_id + this.components.project_name.textContent = this.components.projects.querySelector(`.item[data-id="${project_id}"]`).textContent this.components.form.querySelector('input[name="add_project_id"]').value = project_id this.get.brokers(project_id) @@ -572,27 +579,32 @@ resolve(this.data.units[project_id]) }) } - const uri = `/api/proyecto/${project_id}/unidades` + const uri = `/api/proyecto/${project_id}/unidades/disponibles` return APIClient.fetch(uri).then(response => response.json()).then(json => { - if (json.total === 0) { + if (json.unidades.length === 0) { this.data.units[project_id] = {} return this.data.units[project_id] } if (!(project_id in this.data.units)) { this.data.units[project_id] = {} } - Object.entries(json.unidades).forEach(([type, units]) => { + json.unidades.forEach(unit => { + const type = unit.proyecto_tipo_unidad.tipo_unidad.descripcion if (!(type in this.data.units[project_id])) { this.data.units[project_id][type] = [] } - units.forEach(unit => { - this.data.units[project_id][type].push({ - text: unit.descripcion, - name: unit.descripcion, - value: unit.id - }) + this.data.units[project_id][type].push({ + text: unit.descripcion, + name: unit.descripcion, + value: unit.id }) }) + Object.entries(this.data.units[project_id]).forEach(([type, units]) => { + units.sort((a, b) => { + return parseInt(a.text) - parseInt(b.text) + }) + this.data.units[project_id][type] = units + }) return this.data.units[project_id] }) }, @@ -692,6 +704,8 @@ this.components.$region = $(`#${this.ids.region}`) this.components.$broker = $(`#${this.ids.broker}`) this.components.promotions = new AddModalPromotions() + this.components.projects = document.getElementById(this.ids.projects) + this.components.project_name = document.getElementById(this.ids.project_name) this.components.units = new AddModalUnits() this.components.$modal.modal({