diff --git a/app/resources/views/proyectos/brokers/contracts/show.blade.php b/app/resources/views/proyectos/brokers/contracts/show.blade.php index 147e371..55a91f2 100644 --- a/app/resources/views/proyectos/brokers/contracts/show.blade.php +++ b/app/resources/views/proyectos/brokers/contracts/show.blade.php @@ -268,7 +268,7 @@ })) }) return Promise.all(promises) - } + }, } }, draw() { diff --git a/app/src/Controller/API/Ventas/PropiedadesUnidades.php b/app/src/Controller/API/Ventas/PropiedadesUnidades.php index 74ba123..524a8df 100644 --- a/app/src/Controller/API/Ventas/PropiedadesUnidades.php +++ b/app/src/Controller/API/Ventas/PropiedadesUnidades.php @@ -4,9 +4,11 @@ namespace Incoviba\Controller\API\Ventas; use PDOException; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ResponseInterface; +use Psr\Log\LoggerInterface; use Incoviba\Controller\API\{withJson, emptyBody}; use Incoviba\Controller\withRedis; use Incoviba\Common\Implement\Exception\{EmptyResult,EmptyRedis}; +use Incoviba\Exception\ServiceAction\{Read, Create, Update}; use Incoviba\Repository; use Incoviba\Service; @@ -14,7 +16,9 @@ class PropiedadesUnidades { use emptyBody, withJson, withRedis; - public function add(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\PropiedadUnidad $propiedadUnidadService): ResponseInterface + public function add(ServerRequestInterface $request, ResponseInterface $response, + LoggerInterface $logger, + Service\Venta\PropiedadUnidad $propiedadUnidadService): ResponseInterface { $body = $request->getParsedBody(); $output = [ @@ -26,8 +30,8 @@ class PropiedadesUnidades $pu = $propiedadUnidadService->add($body); $output['propiedad_unidad'] = $pu; $output['added'] = true; - } catch (EmptyResult $exception) { - error_log($exception); + } catch (Read | Create $exception) { + $logger->notice($exception); } return $this->withJson($response, $output); } @@ -44,7 +48,7 @@ class PropiedadesUnidades $pu = $propiedadUnidadService->getById($pu_id); $propiedadUnidadService->edit($pu, (array) $body); $output['edited'] = true; - } catch (PDOException | EmptyResult) {} + } catch (PDOException | Read | EmptyResult | Update) {} return $this->withJson($response, $output); } public function remove(ServerRequestInterface $request, ResponseInterface $response, @@ -58,7 +62,7 @@ class PropiedadesUnidades $pu = $propiedadUnidadRepository->fetchById($pu_id); $propiedadUnidadRepository->remove($pu); $output['removed'] = true; - } catch (EmptyResult) {} + } catch (PDOException | EmptyResult) {} return $this->withJson($response, $output); } } diff --git a/app/src/Repository/Venta/Propiedad.php b/app/src/Repository/Venta/Propiedad.php index d28dd96..e5ff30b 100644 --- a/app/src/Repository/Venta/Propiedad.php +++ b/app/src/Repository/Venta/Propiedad.php @@ -1,6 +1,8 @@ register('unidad_principal', (new Implement\Repository\Mapper()) - ->setProperty('unidades') - ->setDefault([]) - ->setFunction(function($data) { - return [$this->unidadService->getById($data['unidad_principal'])]; - }) - ) + $map = (new Implement\Repository\MapperParser(['unidad_principal', 'estacionamientos', 'bodegas', 'estado'])) ->register('estado', new Implement\Repository\Mapper\Boolean('estado')); return $this->parseData(new Model\Venta\Propiedad(), $data, $map); } @@ -67,7 +62,10 @@ class Propiedad extends Ideal\Repository */ public function fetchVigenteByUnidad(int $unidad_id): Model\Venta\Propiedad { - $query = "SELECT * FROM `{$this->getTable()}` WHERE `unidad_principal` = ?"; + $query = $this->connection->getQueryBuilder() + ->select() + ->from($this->getTable()) + ->where('`unidad_principal` = ? AND `estado` = 1'); return $this->fetchOne($query, [$unidad_id]); } } diff --git a/app/src/Repository/Venta/PropiedadUnidad.php b/app/src/Repository/Venta/PropiedadUnidad.php index 0febced..3ef43bd 100644 --- a/app/src/Repository/Venta/PropiedadUnidad.php +++ b/app/src/Repository/Venta/PropiedadUnidad.php @@ -1,6 +1,7 @@ update($model, ['propiedad', 'unidad', 'valor'], $new_data); } - public function fetchById(int $id): Define\Model + /** + * @param int $id + * @return Model\Venta\PropiedadUnidad + * @throws Implement\Exception\EmptyResult + */ + public function fetchById(int $id): Model\Venta\PropiedadUnidad { $query = $this->connection->getQueryBuilder() ->select() @@ -66,16 +72,27 @@ class PropiedadUnidad extends Ideal\Repository return $this->fetchOne($query, [$id]); } + /** + * @param int $venta_id + * @return array + * @throws Implement\Exception\EmptyResult + */ public function fetchByVenta(int $venta_id): array { $query = $this->connection->getQueryBuilder() ->select('a.*') ->from("{$this->getTable()} a") - ->joined('JOIN unidad ON a.unidad = unidad.id - JOIN venta ON venta.propiedad = a.propiedad') + ->joined('INNER JOIN unidad ON a.unidad = unidad.id') + ->joined('INNER JOIN venta ON venta.propiedad = a.propiedad') ->where('venta.id = ?'); return $this->fetchMany($query, [$venta_id]); } + + /** + * @param int $propiedad_id + * @return array + * @throws Implement\Exception\EmptyResult + */ public function fetchByPropiedad(int $propiedad_id): array { $query = $this->connection->getQueryBuilder() @@ -86,6 +103,12 @@ class PropiedadUnidad extends Ideal\Repository ->group('`unidad`.`id`'); return $this->fetchMany($query, [$propiedad_id]); } + + /** + * @param Define\Model $model + * @return void + * @throws PDOException + */ public function remove(Define\Model $model): void { $query = $this->connection->getQueryBuilder() @@ -94,7 +117,15 @@ class PropiedadUnidad extends Ideal\Repository $this->connection->execute($query, [$model->pu_id]); } - protected function update(Define\Model $model, array $columns, array $data): Define\Model + /** + * @param Define\Model $model + * @param array $columns + * @param array $data + * @return Model\Venta\PropiedadUnidad + * @throws Implement\Exception\EmptyResult + * @throws PDOException + */ + protected function update(Define\Model $model, array $columns, array $data): Model\Venta\PropiedadUnidad { $changes = []; $values = []; diff --git a/app/src/Service/Venta/PropiedadUnidad.php b/app/src/Service/Venta/PropiedadUnidad.php index d60a677..9cb4f7e 100644 --- a/app/src/Service/Venta/PropiedadUnidad.php +++ b/app/src/Service/Venta/PropiedadUnidad.php @@ -1,7 +1,9 @@ process($this->propiedadUnidadRepository->fetchById($unidad_id)); + try { + return $this->process($this->propiedadUnidadRepository->fetchById($unidad_id)); + } catch (EmptyResult $exception) { + throw new Read(__CLASS__, $exception); + } } public function getByVenta(int $venta_id): array { - return array_map([$this, 'process'], $this->propiedadUnidadRepository->fetchByVenta($venta_id)); + try { + return array_map([$this, 'process'], $this->propiedadUnidadRepository->fetchByVenta($venta_id)); + } catch (EmptyResult) { + return []; + } } public function getByPropiedad(int $propiedad_id): array { - return array_map([$this, 'process'], $this->propiedadUnidadRepository->fetchByPropiedad($propiedad_id)); + try { + return array_map([$this, 'process'], $this->propiedadUnidadRepository->fetchByPropiedad($propiedad_id)); + } catch (EmptyResult) { + return []; + } } + + /** + * @param array $data + * @return Model\Venta\PropiedadUnidad + * @throws Create + * @throws Read + */ public function add(array $data): Model\Venta\PropiedadUnidad { - $unidad = $this->unidadRepository->fetchById($data['unidad']); + try { + $unidad = $this->unidadRepository->fetchById($data['unidad']); + } catch (EmptyResult $exception) { + throw new Read(__CLASS__, $exception); + } $temp = json_decode(json_encode($unidad), JSON_OBJECT_AS_ARRAY); $columnMap = [ 'proyecto_tipo_unidad' => 'pt' @@ -38,11 +68,27 @@ class PropiedadUnidad $temp['propiedad'] = $data['propiedad']; $temp['valor'] = $data['valor']; $pu = $this->propiedadUnidadRepository->create($temp); - return $this->process($this->propiedadUnidadRepository->save($pu)); + try { + return $this->process($this->propiedadUnidadRepository->save($pu)); + } catch (PDOException $exception) { + throw new Create(__CLASS__, $exception); + } } + + /** + * @param Model\Venta\PropiedadUnidad $propiedadUnidad + * @param array $data + * @return Model\Venta\PropiedadUnidad + * @throws EmptyResult + * @throws Update + */ public function edit(Model\Venta\PropiedadUnidad $propiedadUnidad, array $data): Model\Venta\PropiedadUnidad { - return $this->process($this->propiedadUnidadRepository->edit($propiedadUnidad, $data)); + try { + return $this->process($this->propiedadUnidadRepository->edit($propiedadUnidad, $data)); + } catch (PDOException $exception) { + throw new Update(__CLASS__, $exception); + } } protected function process(Model\Venta\PropiedadUnidad $unidad): Model\Venta\PropiedadUnidad