From fa11f5b24046ce7ca3729b4de1c7ece1ce8d9daf Mon Sep 17 00:00:00 2001 From: Aldarien Date: Fri, 19 Jan 2024 23:10:20 -0300 Subject: [PATCH] Cleanup logs, fixed add Venta, fixed search --- app/resources/routes/api/search.php | 2 +- app/resources/routes/api/ventas/unidades.php | 1 + app/resources/views/search.blade.php | 17 +- app/resources/views/ventas/add.blade.php | 31 ++-- app/setup/setups/logs.php | 10 +- app/src/Controller/API/Search.php | 20 +++ app/src/Controller/API/Ventas.php | 28 ++-- app/src/Controller/Search.php | 7 - app/src/Middleware/NotFound.php | 2 +- app/src/Model/Venta.php | 6 +- app/src/Repository/Venta.php | 163 +++++++++++++++---- app/src/Service/Search.php | 13 +- app/src/Service/Venta.php | 25 +-- app/src/Service/Venta/Propiedad.php | 2 +- php-errors.ini | 2 +- 15 files changed, 238 insertions(+), 91 deletions(-) create mode 100644 app/src/Controller/API/Search.php diff --git a/app/resources/routes/api/search.php b/app/resources/routes/api/search.php index 1339a42..e81221f 100644 --- a/app/resources/routes/api/search.php +++ b/app/resources/routes/api/search.php @@ -1,4 +1,4 @@ post('/search', [Search::class, 'query']); diff --git a/app/resources/routes/api/ventas/unidades.php b/app/resources/routes/api/ventas/unidades.php index 693f963..79ae108 100644 --- a/app/resources/routes/api/ventas/unidades.php +++ b/app/resources/routes/api/ventas/unidades.php @@ -8,4 +8,5 @@ $app->group('/unidad/{unidad_id}', function($app) { $app->group('/prorrateo', function($app) { $app->post('[/]', [Unidades::class, 'prorrateo']); }); + $app->get('[/]', [Unidades::class, 'get']); }); diff --git a/app/resources/views/search.blade.php b/app/resources/views/search.blade.php index 8a7b901..0827590 100644 --- a/app/resources/views/search.blade.php +++ b/app/resources/views/search.blade.php @@ -107,9 +107,10 @@ const uri = '{{$urls->api}}/search' this.data = [] return fetchAPI(uri, {method: 'post', body: data}).then(response => { - if (response.ok) { - return response.json() + if (!response) { + return } + return response.json() }).catch(error => { this.draw().clear() this.draw().error(error) @@ -123,7 +124,11 @@ const promises = [] data.results.forEach(row => { if (row.tipo === 'venta') { - promises.push(this.get().venta(row.id).then(json => { + return promises.push(this.get().venta(row.id).then(json => { + if (json.venta === null) { + console.debug(json) + return + } const venta = json.venta progress.progress('increment') const r = new Row({unidad: venta.propiedad.unidades[0], proyecto: venta.proyecto}) @@ -134,7 +139,6 @@ console.error(row) console.error(error) })) - return } promises.push(this.get().unidad(row.id).then(json => { const unidad = json.unidad @@ -164,9 +168,10 @@ venta: id => { const url = '{{$urls->api}}/venta/' + id return fetchAPI(url).then(response => { - if (response.ok) { - return response.json() + if (!response) { + return } + return response.json() }) } } diff --git a/app/resources/views/ventas/add.blade.php b/app/resources/views/ventas/add.blade.php index 00d7c02..dbbee48 100644 --- a/app/resources/views/ventas/add.blade.php +++ b/app/resources/views/ventas/add.blade.php @@ -3,7 +3,7 @@ @section('page_content')

Nueva Venta

-
+
@@ -686,7 +686,7 @@ $('
').addClass('content').append(tipo.charAt(0).toUpperCase() + tipo.slice(1) + ' ').append( unidad.draw(this.unidades[tipo]) ).append( - $('').addClass('ui icon button').attr('type', 'button').attr('data-number', number).append( + $('').addClass('ui basic red icon button').attr('type', 'button').attr('data-number', number).append( $('').addClass('remove icon') ).click(event => { const number = $(event.currentTarget).attr('data-number') @@ -766,7 +766,7 @@ } function showErrors(errors) { - console.debug(errors) + console.error(errors) } $(document).ready(() => { @@ -789,20 +789,21 @@ $('#add_form').submit(event => { event.preventDefault() - const data = new FormData(event.currentTarget) - const uri = $(event.currentTarget).attr('action') - fetch(uri, {method: 'post', body: data}).then(response => { - if (response.ok) { - return response.json() + const body = new FormData(event.currentTarget) + const uri = '{{$urls->api}}/ventas/add' + return fetchAPI(uri, {method: 'post', body}).then(response => { + if (!response) { + return false } - }).then(data => { - if (data.status) { - window.location = '{{$urls->base}}' - return true - } - showErrors(data.errors) + return response.json().then(data => { + if (data.status) { + window.location = '{{$urls->base}}/venta/' + data.venta_id + return true + } + showErrors(data.errors) + return false + }) }) - return false }) }) diff --git a/app/setup/setups/logs.php b/app/setup/setups/logs.php index 046300a..cb17296 100644 --- a/app/setup/setups/logs.php +++ b/app/setup/setups/logs.php @@ -8,12 +8,18 @@ return [ (new Monolog\Handler\RotatingFileHandler('/logs/debug.log', 10)) ->setFormatter(new Monolog\Formatter\LineFormatter(null, null, false, false, true)), Monolog\Level::Debug, - Monolog\Level::Notice + Monolog\Level::Debug + ), + new Monolog\Handler\FilterHandler( + (new Monolog\Handler\RotatingFileHandler('/logs/info.log', 10)) + ->setFormatter(new Monolog\Formatter\LineFormatter(null, null, false, false, true)), + Monolog\Level::Info, + Monolog\Level::Warning, ), new Monolog\Handler\FilterHandler( (new Monolog\Handler\RotatingFileHandler('/logs/error.log', 10)) ->setFormatter(new Monolog\Formatter\LineFormatter(null, null, false, false, true)), - Monolog\Level::Warning, + Monolog\Level::Error, Monolog\Level::Error ), new Monolog\Handler\FilterHandler( diff --git a/app/src/Controller/API/Search.php b/app/src/Controller/API/Search.php new file mode 100644 index 0000000..7770215 --- /dev/null +++ b/app/src/Controller/API/Search.php @@ -0,0 +1,20 @@ +getParsedBody(); + $results = $service->query($data['query'], $data['tipo']); + $output = compact('results'); + return $this->withJson($response, $output); + } +} diff --git a/app/src/Controller/API/Ventas.php b/app/src/Controller/API/Ventas.php index 684ec99..507f405 100644 --- a/app/src/Controller/API/Ventas.php +++ b/app/src/Controller/API/Ventas.php @@ -1,17 +1,19 @@ null + ]; try { $venta = $this->fetchRedis($redisService, $redisKey); $output = compact('venta'); @@ -64,12 +69,7 @@ class Ventas $output = compact('venta'); $this->saveRedis($redisService, $redisKey, $venta); } catch (EmptyResult $exception) { - $output = [ - 'error' => [ - 'code' => $exception->getCode(), - 'message' => str_replace([PHP_EOL, "\r"], [' ', ''], $exception->getMessage()) - ] - ]; + $this->logger->notice($exception); } } return $this->withJson($response, $output); @@ -178,14 +178,20 @@ class Ventas $data = $request->getParsedBody(); $output = [ 'status' => false, + 'venta_id' => null, 'errors' => [] ]; try { $venta = $ventaService->add($data); $this->saveRedis($redisService, "venta:{$venta->id}", $venta); + $output['venta_id'] = $venta->id; $output['status'] = true; - } catch (\Exception $exception) { - $output['errors'] = $exception; + } catch (EmptyResult | PDOException $exception) { + $output['errors'] = [ + 'code' => $exception->getCode(), + 'message' => $exception->getMessage(), + 'stack' => $exception->getTraceAsString() + ]; } return $this->withJson($response, $output); } diff --git a/app/src/Controller/Search.php b/app/src/Controller/Search.php index 94a6a6f..082d713 100644 --- a/app/src/Controller/Search.php +++ b/app/src/Controller/Search.php @@ -13,11 +13,4 @@ class Search $post = $request->getParsedBody() ?? ''; return $view->render($response, 'search', compact('post', 'query', 'tipo')); } - public function query(ServerRequestInterface $request, ResponseInterface $response, Service\Search $service): ResponseInterface - { - $data = $request->getParsedBody(); - $results = $service->query($data['query'], $data['tipo']); - $response->getBody()->write(json_encode(compact('results'))); - return $response->withHeader('Content-Type', 'application/json'); - } } diff --git a/app/src/Middleware/NotFound.php b/app/src/Middleware/NotFound.php index 8971be4..abccf49 100644 --- a/app/src/Middleware/NotFound.php +++ b/app/src/Middleware/NotFound.php @@ -17,7 +17,7 @@ class NotFound try { return $handler->handle($request); } catch (HttpNotFoundException $exception) { - $this->logger->warning($exception); + $this->logger->notice($exception); $response = $this->responseFactory->createResponse(404); if (str_contains($request->getUri()->getPath(), '/api')) { return $response; diff --git a/app/src/Model/Venta.php b/app/src/Model/Venta.php index 50e9bde..4eca125 100644 --- a/app/src/Model/Venta.php +++ b/app/src/Model/Venta.php @@ -19,8 +19,8 @@ class Venta extends Ideal\Model public float $uf; protected ?Pago $resciliacion; - public array $estados; - public Venta\EstadoVenta $currentEstado; + public ?array $estados; + public ?Venta\EstadoVenta $currentEstado; public function propietario(): Venta\Propietario { @@ -63,7 +63,7 @@ class Venta extends Ideal\Model if (!isset($this->estados)) { $this->estados = $this->runFactory('estados'); } - return $this->estados; + return $this->estados ?? []; } public function currentEstado(): ?Venta\EstadoVenta { diff --git a/app/src/Repository/Venta.php b/app/src/Repository/Venta.php index 0476532..34c4fd2 100644 --- a/app/src/Repository/Venta.php +++ b/app/src/Repository/Venta.php @@ -115,13 +115,15 @@ class Venta extends Ideal\Repository ->register('fecha_ingreso', new Implement\Repository\Mapper\DateTime('fecha_ingreso', 'fechaIngreso')) //->register('avalchile') //->register('agente') - ->register('resciliacion', (new Implement\Repository\Mapper()) - ->setFactory((new Implement\Repository\Factory()) - ->setCallable([$this->pagoService, 'getById']) - ->setArgs(['pago_id' => $data['resciliacion']]))) ->register('relacionado', new Implement\Repository\Mapper\Boolean('relacionado')); //->register('promocion') //->register('devolucion'); + if (array_key_exists('resciliacion', $data)) { + $map = $map->register('resciliacion', (new Implement\Repository\Mapper()) + ->setFactory((new Implement\Repository\Factory()) + ->setCallable([$this->pagoService, 'getById']) + ->setArgs(['pago_id' => $data['resciliacion']]))); + } return $this->parseData(new Model\Venta(), $data, $map); } public function save(Define\Model $model): Model\Venta @@ -147,7 +149,17 @@ class Venta extends Ideal\Repository public function fetchByProyecto(int $proyecto_id): array { - $query = "SELECT a.* + $query = $this->connection->getQueryBuilder() + ->select('a.*') + ->from("{$this->getTable()} a") + ->joined('JOIN propiedad_unidad pu ON pu.propiedad = a.propiedad') + ->joined('JOIN unidad ON unidad.id = pu.unidad AND pu.principal = 1') + ->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') + ->group('a.id'); + /*$query = "SELECT a.* FROM `{$this->getTable()}` a JOIN `propiedad_unidad` pu ON pu.`propiedad` = a.`propiedad` JOIN `unidad` ON `unidad`.`id` = pu.`unidad` AND pu.`principal` = 1 @@ -155,12 +167,22 @@ FROM `{$this->getTable()}` a 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` JOIN `tipo_estado_venta` tev ON tev.`id` = ev.`estado` WHERE ptu.`proyecto` = ? AND tev.`activa` -GROUP BY a.`id`"; +GROUP BY a.`id`";*/ return $this->fetchMany($query, [$proyecto_id]); } public function fetchIdsByProyecto(int $proyecto_id): array { - $query = "SELECT a.`id` + $query = $this->connection->getQueryBuilder() + ->select('a.id') + ->from("{$this->getTable()} a") + ->joined('JOIN propiedad_unidad pu ON pu.propiedad = a.propiedad') + ->joined('JOIN unidad ON unidad.id = pu.unidad AND unidad.pt') + ->joined('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') + ->group('a.id'); + /*$query = "SELECT a.`id` FROM `{$this->getTable()}` a JOIN `propiedad_unidad` pu ON pu.`propiedad` = a.`propiedad` JOIN `unidad` ON `unidad`.`id` = pu.`unidad` AND pu.`principal` = 1 @@ -168,12 +190,22 @@ FROM `{$this->getTable()}` a 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` JOIN `tipo_estado_venta` tev ON tev.`id` = ev.`estado` WHERE ptu.`proyecto` = ? AND tev.`activa` -GROUP BY a.`id`"; +GROUP BY a.`id`";*/ return $this->connection->execute($query, [$proyecto_id])->fetchAll(PDO::FETCH_ASSOC); } public function fetchActivaByProyecto(int $proyecto_id): array { - $query = "SELECT a.* + $query = $this->connection->getQueryBuilder() + ->select('a.*') + ->from("{$this->getTable()} a") + ->joined('JOIN `propiedad_unidad` pu ON pu.`propiedad` = a.`propiedad`') + ->joined('JOIN `unidad` ON `unidad`.`id` = pu.`unidad` AND pu.`principal` = 1') + ->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('WHERE ptu.`proyecto` = ? AND tev.`activa`') + ->group('a.id'); + /*$query = "SELECT a.* FROM `{$this->getTable()}` a JOIN `propiedad_unidad` pu ON pu.`propiedad` = a.`propiedad` JOIN `unidad` ON `unidad`.`id` = pu.`unidad` AND pu.`principal` = 1 @@ -181,12 +213,22 @@ FROM `{$this->getTable()}` a 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` JOIN `tipo_estado_venta` tev ON tev.`id` = ev.`estado` WHERE ptu.`proyecto` = ? AND tev.`activa` -GROUP BY a.`id`"; +GROUP BY a.`id`";*/ return $this->fetchMany($query, [$proyecto_id]); } public function fetchByProyectoAndUnidad(string $proyecto_nombre, int $unidad_descripcion): Model\Venta { - $query = "SELECT a.* + $query = $this->connection->getQueryBuilder() + ->select("a.*") + ->from("`{$this->getTable()}` a") + ->joined('JOIN `propiedad_unidad` pu ON pu.`propiedad` = a.`propiedad`') + ->joined('JOIN `unidad` ON `unidad`.`id` = pu.`unidad` AND pu.`principal` = 1') + ->joined('JOIN `proyecto_tipo_unidad` ptu ON ptu.`id` = `unidad`.`pt`') + ->joined('JOIN `proyecto` ON `proyecto`.`id` = ptu.`proyecto`') + ->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('WHERE `proyecto`.`descripcion` = ? AND `unidad`.`descripcion` = ? AND tev.`activa`'); + /*$query = "SELECT a.* FROM `{$this->getTable()}` a JOIN `propiedad_unidad` pu ON pu.`propiedad` = a.`propiedad` JOIN `unidad` ON `unidad`.`id` = pu.`unidad` AND pu.`principal` = 1 @@ -194,82 +236,147 @@ FROM `{$this->getTable()}` a JOIN `proyecto` ON `proyecto`.`id` = ptu.`proyecto` 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` JOIN `tipo_estado_venta` tev ON tev.`id` = ev.`estado` -WHERE `proyecto`.`descripcion` = ? AND `unidad`.`descripcion` = ? AND tev.`activa`"; +WHERE `proyecto`.`descripcion` = ? AND `unidad`.`descripcion` = ? AND tev.`activa`";*/ return $this->fetchOne($query, [$proyecto_nombre, $unidad_descripcion]); } public function fetchByPie(int $pie_id): Model\Venta { - $query = "SELECT * FROM `{$this->getTable()}` WHERE `pie` = ?"; + $query = $this->connection->getQueryBuilder() + ->select() + ->from($this->getTable()) + ->where('pie = ?'); return $this->fetchOne($query, [$pie_id]); } public function fetchIdByPie(int $pie_id): array { - $query = "SELECT `id` FROM `{$this->getTable()}` WHERE `pie` = ?"; + $query = $this->connection->getQueryBuilder() + ->select('id') + ->from($this->getTable()) + ->where('pie = ?'); return $this->connection->execute($query, [$pie_id])->fetch(PDO::FETCH_ASSOC); } public function fetchByUnidad(string $unidad, string $tipo): array { - $query = "SELECT a.* + $query = $this->connection->getQueryBuilder() + ->select('a.*') + ->from("{$this->getTable()} a") + ->joined('JOIN `propiedad_unidad` pu ON pu.`propiedad` = a.`propiedad`') + ->joined('JOIN `unidad` ON `unidad`.`id` = pu.`unidad`') + ->joined('JOIN `proyecto_tipo_unidad` ptu ON ptu.`id` = `unidad`.`pt`') + ->joined('JOIN `tipo_unidad` tu ON tu.`id` = ptu.`tipo`') + ->where('`unidad`.`descripcion` LIKE ? AND tu.`descripcion` = ?'); + /*$query = "SELECT a.* FROM `{$this->getTable()}` a JOIN `propiedad_unidad` pu ON pu.`propiedad` = a.`propiedad` JOIN `unidad` ON `unidad`.`id` = pu.`unidad` JOIN `proyecto_tipo_unidad` ptu ON ptu.`id` = `unidad`.`pt` JOIN `tipo_unidad` tu ON tu.`id` = ptu.`tipo` -WHERE `unidad`.`descripcion` LIKE ? AND tu.`descripcion` = ?"; +WHERE `unidad`.`descripcion` LIKE ? AND tu.`descripcion` = ?";*/ return $this->fetchMany($query, [$unidad, $tipo]); } public function fetchIdsByUnidad(string $unidad, string $tipo): array { - $query = "SELECT a.id + $query = $this->connection->getQueryBuilder() + ->select('a.id') + ->from("{$this->getTable()} a") + ->joined('JOIN `propiedad_unidad` pu ON pu.`propiedad` = a.`propiedad`') + ->joined('JOIN `unidad` ON `unidad`.`id` = pu.`unidad`') + ->joined('JOIN `proyecto_tipo_unidad` ptu ON ptu.`id` = `unidad`.`pt`') + ->joined('JOIN `tipo_unidad` tu ON tu.`id` = ptu.`tipo`') + ->where('`unidad`.`descripcion` LIKE ? AND tu.`descripcion` = ?'); + /*$query = "SELECT a.id FROM `{$this->getTable()}` a JOIN `propiedad_unidad` pu ON pu.`propiedad` = a.`propiedad` JOIN `unidad` ON `unidad`.`id` = pu.`unidad` JOIN `proyecto_tipo_unidad` ptu ON ptu.`id` = `unidad`.`pt` JOIN `tipo_unidad` tu ON tu.`id` = ptu.`tipo` -WHERE `unidad`.`descripcion` LIKE ? AND tu.`descripcion` = ?"; +WHERE `unidad`.`descripcion` LIKE ? AND tu.`descripcion` = ?";*/ return $this->connection->execute($query, [$unidad, $tipo])->fetchAll(PDO::FETCH_ASSOC); } public function fetchByPrecio(string $precio): array { - $query = "SELECT * FROM `{$this->getTable()}` WHERE `valor_uf` = ?"; + $query = $this->connection->getQueryBuilder() + ->select() + ->from($this->getTable()) + ->where('valor_uf = ?'); return $this->fetchMany($query, [$precio]); } public function fetchIdsByPrecio(string $precio): array { - $query = "SELECT `id` FROM `{$this->getTable()}` WHERE `valor_uf` = ?"; + $query = $this->connection->getQueryBuilder() + ->select('id') + ->from($this->getTable()) + ->where('valor_uf = ?'); return $this->connection->execute($query, [$precio])->fetchAll(PDO::FETCH_ASSOC); } + public function fetchByPropietarioAndPropiedad(int $propietario_rut, int $propiedad_id): Model\Venta + { + $query = $this->connection->getQueryBuilder() + ->select() + ->from($this->getTable()) + ->where('propietario = ? AND propiedad = ?'); + return $this->fetchOne($query, [$propietario_rut, $propiedad_id]); + } public function fetchByPropietario(string $propietario): array { - $query = "SELECT a.* + $query = $this->connection->getQueryBuilder() + ->select('a.*') + ->from("{$this->getTable()} a") + ->joined('JOIN `propietario` ON `propietario`.`rut` = a.`propietario`') + ->where("CONCAT_WS('-', `propietario`.`rut`, `propietario`.`dv`) LIKE :propietario OR `propietario`.`nombres` LIKE :propietario + OR `propietario`.`apellido_paterno` LIKE :propietario OR `propietario`.`apellido_materno` LIKE :propietario + OR CONCAT_WS(' ', `propietario`.`nombres`, `propietario`.`apellido_paterno`, `propietario`.`apellido_materno`) LIKE :propietario"); + /*$query = "SELECT a.* FROM `{$this->getTable()}` a JOIN `propietario` ON `propietario`.`rut` = a.`propietario` WHERE CONCAT_WS('-', `propietario`.`rut`, `propietario`.`dv`) LIKE :propietario OR `propietario`.`nombres` LIKE :propietario OR `propietario`.`apellido_paterno` LIKE :propietario OR `propietario`.`apellido_materno` LIKE :propietario - OR CONCAT_WS(' ', `propietario`.`nombres`, `propietario`.`apellido_paterno`, `propietario`.`apellido_materno`) LIKE :propietario"; + OR CONCAT_WS(' ', `propietario`.`nombres`, `propietario`.`apellido_paterno`, `propietario`.`apellido_materno`) LIKE :propietario";*/ return $this->fetchMany($query, [':propietario' => "%{$propietario}%"]); } public function fetchIdsByPropietario(string $propietario): array { - $query = "SELECT a.id + $query = $this->connection->getQueryBuilder() + ->select('a.id') + ->from("{$this->getTable()} a") + ->joined('JOIN `propietario` ON `propietario`.`rut` = a.`propietario`') + ->where("CONCAT_WS('-', `propietario`.`rut`, `propietario`.`dv`) LIKE :propietario OR `propietario`.`nombres` LIKE :propietario + OR `propietario`.`apellido_paterno` LIKE :propietario OR `propietario`.`apellido_materno` LIKE :propietario + OR CONCAT_WS(' ', `propietario`.`nombres`, `propietario`.`apellido_paterno`, `propietario`.`apellido_materno`) LIKE :propietario"); + /*$query = "SELECT a.id FROM `{$this->getTable()}` a JOIN `propietario` ON `propietario`.`rut` = a.`propietario` WHERE CONCAT_WS('-', `propietario`.`rut`, `propietario`.`dv`) LIKE :propietario OR `propietario`.`nombres` LIKE :propietario OR `propietario`.`apellido_paterno` LIKE :propietario OR `propietario`.`apellido_materno` LIKE :propietario - OR CONCAT_WS(' ', `propietario`.`nombres`, `propietario`.`apellido_paterno`, `propietario`.`apellido_materno`) LIKE :propietario"; + OR CONCAT_WS(' ', `propietario`.`nombres`, `propietario`.`apellido_paterno`, `propietario`.`apellido_materno`) LIKE :propietario";*/ return $this->connection->execute($query, [':propietario' => "%{$propietario}%"])->fetchAll(PDO::FETCH_ASSOC); } public function fetchByPropietarioNombreCompleto(string $propietario): array { - $query = "SELECT a.* + $query = $this->connection->getQueryBuilder() + ->select('a.*') + ->from("{$this->getTable()} a") + ->joined('JOIN `propietario` ON `propietario`.`rut` = a.`propietario`') + ->where("CONCAT_WS(' ', `propietario`.`nombres`, `propietario`.`apellido_paterno`, `propietario`.`apellido_materno`) LIKE ?"); + /*$query = "SELECT a.* FROM `{$this->getTable()}` a JOIN `propietario` ON `propietario`.`rut` = a.`propietario` -WHERE CONCAT_WS(' ', `propietario`.`nombres`, `propietario`.`apellido_paterno`, `propietario`.`apellido_materno`) LIKE ?"; +WHERE CONCAT_WS(' ', `propietario`.`nombres`, `propietario`.`apellido_paterno`, `propietario`.`apellido_materno`) LIKE ?";*/ return $this->fetchMany($query, [$propietario]); } public function fetchEscriturasByProyecto(int $proyecto_id): array { - $query = "SELECT DISTINCT a.* + $query = $this->connection->getQueryBuilder() + ->select('DISTINCT a.*') + ->from("{$this->getTable()} a") + ->joined('JOIN `propiedad_unidad` pu ON pu.`propiedad` = a.`propiedad`') + ->joined('JOIN `unidad` ON `unidad`.`id` = pu.`unidad`') + ->joined('JOIN `proyecto_tipo_unidad` ptu ON ptu.`id` = `unidad`.`id`') + ->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.`descripcion` IN ('firmado por inmobiliaria', 'escriturando')") + ->group('a.id'); + /*$query = "SELECT DISTINCT a.* FROM `{$this->getTable()}` a JOIN `propiedad_unidad` pu ON pu.`propiedad` = a.`propiedad` JOIN `unidad` ON `unidad`.`id` = pu.`unidad` @@ -277,7 +384,7 @@ FROM `{$this->getTable()}` a 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` JOIN `tipo_estado_venta` tev ON tev.`id` = ev.`estado` WHERE ptu.`proyecto` = ? AND tev.`descripcion` IN ('firmado por inmobiliaria', 'escriturando') -GROUP BY a.`id`"; +GROUP BY a.`id`";*/ return $this->fetchMany($query, [$proyecto_id]); } public function fetchIdByEscritura(int $escritura_id): array diff --git a/app/src/Service/Search.php b/app/src/Service/Search.php index 0132c28..9d58d4b 100644 --- a/app/src/Service/Search.php +++ b/app/src/Service/Search.php @@ -65,7 +65,11 @@ class Search foreach ($queries as $q) { $this->add($results, $this->findVentas($q, $tipo)); if (in_array($tipo, $tiposUnidades)) { - $this->add($results, $this->findUnidadesDisponibles($q, $tipo), false); + $disponibles = $this->findUnidadesDisponibles($q, $tipo); + if (count($disponibles) === 0) { + continue; + } + $this->add($results, $disponibles, false); } } return $results; @@ -281,11 +285,10 @@ class Search protected function add(array &$results, array $found, bool $is_venta = true): void { foreach ($found as $item) { + if (!isset($item['tipo'])) { + $item['tipo'] = ($is_venta) ? 'venta' : 'unidad'; + } if (!$this->inResults($item, $results)) { - $item['tipo'] = 'venta'; - if (!$is_venta) { - $item['tipo'] = 'unidad'; - } $results []= $item; } } diff --git a/app/src/Service/Venta.php b/app/src/Service/Venta.php index be79d78..9697d0d 100644 --- a/app/src/Service/Venta.php +++ b/app/src/Service/Venta.php @@ -100,17 +100,22 @@ class Venta $venta_data[$field] = $forma_pago->{$name}->id; } } - $venta = $this->ventaRepository->create($venta_data); - $venta = $this->ventaRepository->save($venta); - $tipoEstado = $this->tipoEstadoVentaRepository->fetchByDescripcion('vigente'); - $estado = $this->estadoVentaRepository->create([ - 'venta' => $venta->id, - 'estado' => $tipoEstado->id, - 'fecha' => $venta->fecha->format('Y-m-d') - ]); - $this->estadoVentaRepository->save($estado); + try { + return $this->ventaRepository->fetchByPropietarioAndPropiedad($propietario->rut, $propiedad->id); + } catch (Implement\Exception\EmptyResult) { + $venta = $this->ventaRepository->create($venta_data); + $venta = $this->ventaRepository->save($venta); - return $venta; + $tipoEstado = $this->tipoEstadoVentaRepository->fetchByDescripcion('vigente'); + $estado = $this->estadoVentaRepository->create([ + 'venta' => $venta->id, + 'estado' => $tipoEstado->id, + 'fecha' => $venta->fecha->format('Y-m-d') + ]); + $this->estadoVentaRepository->save($estado); + + return $venta; + } } protected function addPropietario(array $data): Model\Venta\Propietario { diff --git a/app/src/Service/Venta/Propiedad.php b/app/src/Service/Venta/Propiedad.php index 7de58ba..58788c3 100644 --- a/app/src/Service/Venta/Propiedad.php +++ b/app/src/Service/Venta/Propiedad.php @@ -78,7 +78,7 @@ class Propiedad if (count($diff) === 0) { return; } - $query = "DELECT FROM `propiedad_unidad` WHERE `propiedad` = ? AND `unidad` = ?"; + $query = "DELETE FROM `propiedad_unidad` WHERE `propiedad` = ? AND `unidad` = ?"; $statement = $this->connection->prepare($query); foreach ($diff as $id) { $statement->execute([$propiedad->id, $id]); diff --git a/php-errors.ini b/php-errors.ini index aed1ffb..511b6ac 100644 --- a/php-errors.ini +++ b/php-errors.ini @@ -1,3 +1,3 @@ display_errors=no log_errors=yes -error_log=/logs/errors.log +#error_log=/logs/errors.log