From d2511901ecbe9b58467f935d7d22122e5ab90b08 Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Fri, 19 Apr 2024 23:19:35 -0400 Subject: [PATCH] Facturacion --- .../layout/body/header/menu/ventas.blade.php | 1 + .../views/ventas/facturacion.blade.php | 141 +++++++++++++----- .../views/ventas/facturacion/show.blade.php | 9 +- app/src/Controller/Ventas/Facturacion.php | 5 +- app/src/Repository/Venta.php | 4 +- app/src/Service/Proyecto/Terreno.php | 5 +- app/src/Service/Venta.php | 4 + 7 files changed, 126 insertions(+), 43 deletions(-) diff --git a/app/resources/views/layout/body/header/menu/ventas.blade.php b/app/resources/views/layout/body/header/menu/ventas.blade.php index 2cea6c4..5df196e 100644 --- a/app/resources/views/layout/body/header/menu/ventas.blade.php +++ b/app/resources/views/layout/body/header/menu/ventas.blade.php @@ -33,6 +33,7 @@ --}} {{--Importar Precios--}} {{--Evaluar Cierre--}} + Facturación Nueva Venta diff --git a/app/resources/views/ventas/facturacion.blade.php b/app/resources/views/ventas/facturacion.blade.php index 6bc5676..e0eac21 100644 --- a/app/resources/views/ventas/facturacion.blade.php +++ b/app/resources/views/ventas/facturacion.blade.php @@ -24,7 +24,7 @@ @endforeach -
+
@endsection @include('layout.head.styles.datatables') @@ -226,49 +226,104 @@ ids: {}, selected: 0, data: JSON.parse('{!! json_encode($proyectos) !!}'), + sent: false, + ufs: {}, + ipcs: {}, table: null, get: function() { return { ventas: () => { + if (this.sent) { + return + } + this.sent = true const url = '{{$urls->api}}/ventas/facturacion/proyecto/' + this.selected + this.draw().loading() return fetchAPI(url).then(response => { - if (response.ok) { - return response.json() + if (!response) { + return } + return response.json() }).then(json => { const idx = this.data.findIndex(proyecto => proyecto.id === json.proyecto_id) - const fecha_terreno = new Date(this.data[idx].terreno.date) + const fecha_terreno = (typeof this.data[idx].terreno.date === 'undefined') ? new Date() : new Date(this.data[idx].terreno.date) this.data[idx]['ventas'] = [] const ventas = [] - json.ventas.forEach(venta => { - const data = { - id: venta.id, - precio: venta.valor, - fecha: new Date(venta.fecha), - escritura: null - } - if (['escriturando'].includes(venta.current_estado.tipo_estado_venta.descripcion)) { - data.escritura = new Date(venta.current_estado.fecha) - } - const v = new Venta(data) - const promises = [] - if (v.escritura !== null) { - promises.push(money.get().uf(v.escritura).then(uf => { - v.uf = uf - })) - promises.push(money.get().ipc(v.escritura, fecha_terreno).then(ipc => { - v.ipc = ipc - })) - } - promises.push(v.get().unidades()) - const promise = Promise.all(promises).then(() => { - this.data[idx].ventas.push(v) + const unidadesQueue = [] + const ufQueue = {} + const ipcQueue = {} + const chunkSize = 100 + const url = '{{$urls->api}}/ventas/get' + for (let i = 0; i < json.ventas.length; i += chunkSize) { + const chunk = json.ventas.slice(i, i + chunkSize).map(venta => venta.id) + const body = new FormData() + body.set('ventas', chunk) + const promise = fetchAPI(url, {method: 'post', body}).then(response => { + if (!response) { + return response + } + return response.json().then(json => { + json.ventas.forEach(venta => { + const data = { + id: venta.id, + precio: venta.valor, + fecha: new Date(venta.fecha), + escritura: new Date(venta.fecha) + } + if (['escriturando'].includes(venta.current_estado.tipo_estado_venta.descripcion)) { + data.escritura = new Date(venta.current_estado.fecha) + } + const v = new Venta(data) + if (v.escritura !== null) { + const dateString = v.escritura.toString() + if (!Object.hasOwn(ufQueue, dateString)) { + ufQueue[dateString] = [] + } + ufQueue[dateString].push(v.id) + if (!Object.hasOwn(ipcQueue, dateString)) { + ipcQueue[dateString] = [] + } + ipcQueue[dateString].push(v.id) + } + unidadesQueue.push(v.id) + this.data[idx].ventas.push(v) + + }) + }); }) ventas.push(promise) - }) + } Promise.all(ventas).then(() => { - this.data[idx].ventas.sort((a, b) => parseInt(a.principal.descripcion) - parseInt(b.principal.descripcion)) - this.draw().ventas(idx) + const promises = [] + Object.entries(ufQueue).forEach(([dateString, ventas]) => { + const date = new Date(dateString) + promises.push(money.get().uf(date).then(uf => { + ventas.forEach(id => { + const vidx = this.data[idx].ventas.findIndex(venta => venta.id === id) + this.data[idx].ventas[vidx].uf = uf + }) + })) + }) + Object.entries(ipcQueue).forEach(([dateString, ventas]) => { + const date = new Date(dateString) + promises.push(money.get().ipc(date, fecha_terreno).then(ipc => { + ventas.forEach(id => { + const vidx = this.data[idx].ventas.findIndex(venta => venta.id === id) + this.data[idx].ventas[vidx].ipc = ipc + }) + })) + }) + for (let i = 0; i < unidadesQueue.length; i += chunkSize) { + const chunk = unidadesQueue.slice(i, i + chunkSize) + chunk.forEach(id => { + const vidx = this.data[idx].ventas.findIndex(venta => venta.id === id) + promises.push(this.data[idx].ventas[vidx].get().unidades()) + }) + } + Promise.all(promises).then(() => { + this.draw().ventas(idx) + this.sent = false + }) }) }) } @@ -277,23 +332,37 @@ draw: function() { return { proyectos: () => { + $(this.ids.title).html('Proyectos') if (this.table !== null) { this.table.clear() + this.table.destroy() + $(this.ids.data).html('') } $(this.ids.data).hide() + $(this.ids.proyectos).find('.item').css('cursor', 'pointer') $(this.ids.proyectos).show() }, ventas: proyecto_idx => { - const table = $(this.ids.data) + $(this.ids.title).html(this.data[proyecto_idx].descripcion) + const parent = $(this.ids.data) if (this.table !== null) { - this.table.clear().destroy() + this.table.clear() + this.table.destroy() + this.table = null + parent.html('') } + const table = $('
').addClass('ui table') table.append(this.draw().thead()) table.append(this.draw().tbody(proyecto_idx)) - table.show() + parent.show() + parent.html(table) $(this.ids.proyectos).hide() if (this.table === null) { - this.table = table.DataTable() + this.table = table.DataTable({ + orders: [ + [0, 'asc'] + ] + }) } }, thead: () => { @@ -331,6 +400,9 @@ venta.draw({tbody, valor_terreno: this.data[proyecto_idx].terreno.valor}) }) return tbody + }, + loading: () => { + $(this.ids.proyectos).find('.item').css('cursor', 'wait') } } }, @@ -369,6 +441,7 @@ } $(document).ready(() => { proyectos.setup({ids: { + title: '#list_title', proyectos: '#proyectos', data: '#data', buttons: { diff --git a/app/resources/views/ventas/facturacion/show.blade.php b/app/resources/views/ventas/facturacion/show.blade.php index 0d871dc..7de3264 100644 --- a/app/resources/views/ventas/facturacion/show.blade.php +++ b/app/resources/views/ventas/facturacion/show.blade.php @@ -97,8 +97,8 @@
- @if ($venta->currentEstado()->fecha->sub(new DateInterval('P1M')) > $venta->proyecto()->terreno->fecha - and $IPC->get($venta->proyecto()->terreno->fecha, $venta->currentEstado()->fecha->sub(new DateInterval('P1M'))) === 0.0) + @if ($venta->currentEstado()->fecha->sub(new DateInterval('P1M')) > $terreno?->fecha + and ($terreno?->valor ?? 0) === 0.0)
@@ -409,8 +409,7 @@ }, proyecto: { direccion: '{{$venta->proyecto()->direccion()->simple()}}', - terreno: {{(isset($terreno->fecha) and $terreno->fecha >= $lastDic) ? - $IPC->readjust($terreno->valor, $terreno->fecha, $venta->currentEstado()->fecha) : 0}}, + terreno: {{$terreno?->valor ?? 0}} }, estadoVenta: { fecha: new Date('{{$venta->currentEstado()->fecha->format('Y-m-d')}}') @@ -758,7 +757,7 @@ this.update().unidades() this.update().propietarios($(count_propietarios_id).val()) this.draw().propietarios() - this.unidades.forEach((unidad, idx) => { + this.propietarios.forEach((unidad, idx) => { this.update().proporciones(idx) }) this.update().facturas() diff --git a/app/src/Controller/Ventas/Facturacion.php b/app/src/Controller/Ventas/Facturacion.php index 74ea55e..b73ddd7 100644 --- a/app/src/Controller/Ventas/Facturacion.php +++ b/app/src/Controller/Ventas/Facturacion.php @@ -9,9 +9,12 @@ use Incoviba\Service; class Facturacion { public function __invoke(ServerRequestInterface $request, ResponseInterface $response, View $view, - Service\Proyecto $proyectoService): ResponseInterface + Service\Proyecto $proyectoService, Service\Proyecto\Terreno $terrenoService): ResponseInterface { $proyectos = $proyectoService->getEscriturando(); + foreach ($proyectos as &$proyecto) { + $proyecto->terreno = $terrenoService->valor($proyecto->id) ?? $proyecto->terreno; + } return $view->render($response, 'ventas.facturacion', compact('proyectos')); } public function show(ServerRequestInterface $request, ResponseInterface $response, View $view, diff --git a/app/src/Repository/Venta.php b/app/src/Repository/Venta.php index ef392f1..9eac6b2 100644 --- a/app/src/Repository/Venta.php +++ b/app/src/Repository/Venta.php @@ -171,7 +171,7 @@ class Venta extends Ideal\Repository ->joined('JOIN proyecto_tipo_unidad ptu ON ptu.id = unidad.pt') ->joined('JOIN (SELECT ev1.* FROM estado_venta ev1 JOIN (SELECT MAX(id) AS id, venta FROM estado_venta GROUP BY venta) ev0 ON ev0.id = ev1.id) ev ON ev.venta = a.id') ->joined('JOIN tipo_estado_venta tev ON tev.id = ev.estado') - ->where('ptu.proyecto = ? AND tev.activa') + ->where('ptu.proyecto = ? AND tev.activa = 1') ->group('a.id'); return $this->fetchIds($query, [$proyecto_id]); } @@ -185,7 +185,7 @@ class Venta extends Ideal\Repository ->joined('JOIN `proyecto_tipo_unidad` ptu ON ptu.`id` = `unidad`.`pt`') ->joined("JOIN (SELECT e1.* FROM `estado_venta` e1 JOIN (SELECT MAX(`id`) AS 'id', `venta` FROM `estado_venta` GROUP BY `venta`) e0 ON e0.`id` = e1.`id`) ev ON ev.`venta` = a.`id`") ->joined('JOIN `tipo_estado_venta` tev ON tev.`id` = ev.`estado`') - ->where('ptu.`proyecto` = ? AND tev.`activa`') + ->where('ptu.`proyecto` = ? AND tev.`activa` = 1') ->group('a.id'); return $this->fetchMany($query, [$proyecto_id]); } diff --git a/app/src/Service/Proyecto/Terreno.php b/app/src/Service/Proyecto/Terreno.php index 637fc7d..6afa8c5 100644 --- a/app/src/Service/Proyecto/Terreno.php +++ b/app/src/Service/Proyecto/Terreno.php @@ -31,8 +31,11 @@ class Terreno extends Ideal\Service try { return $this->getValorContable($proyecto, $lastDecember); } catch (Implement\Exception\EmptyResponse) {} + if ($proyecto->terreno->fecha === null) { + return null; + } if ($proyecto->terreno->fecha->getTimestamp() > 0) { - return $this->getValorReajustado($proyecto, $lastDecember); + return $this->getValorReajustado($proyecto); } $terreno = $proyecto->terreno; } catch (Implement\Exception\EmptyResult) {} diff --git a/app/src/Service/Venta.php b/app/src/Service/Venta.php index f9ebf44..36d9642 100644 --- a/app/src/Service/Venta.php +++ b/app/src/Service/Venta.php @@ -46,6 +46,10 @@ class Venta extends Service $ventas = $this->ventaRepository->fetchActivaByProyecto($proyecto_id); return array_map([$this, 'process'], $ventas); } + public function getIdsByProyecto(int $proyecto_id): array + { + return $this->ventaRepository->fetchIdsByProyecto($proyecto_id); + } public function getByProyectoAndUnidad(string $proyecto_nombre, int $unidad_descripcion): Model\Venta { $venta = $this->ventaRepository->fetchByProyectoAndUnidad($proyecto_nombre, $unidad_descripcion);