From eabdab23c3bf869fb109c47389b8f7c4e00b1866 Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Tue, 22 Apr 2025 13:08:15 -0400 Subject: [PATCH] Simplificacion de retorno de ventas por unidades --- app/src/Repository/Venta.php | 4 ++-- app/src/Service/Venta.php | 16 +++++++++++++-- app/src/Service/Venta/Propiedad.php | 32 +++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/app/src/Repository/Venta.php b/app/src/Repository/Venta.php index 6af241e..68c6e9b 100644 --- a/app/src/Repository/Venta.php +++ b/app/src/Repository/Venta.php @@ -321,7 +321,7 @@ class Venta extends Ideal\Repository * @return array * @throws Implement\Exception\EmptyResult */ - public function fetchActiveByUnidadIds(array $unidad_ids): array + public function fetchActiveArrayByUnidadIds(array $unidad_ids): array { $subSubQuery = $this->connection->getQueryBuilder() ->select('MAX(`id`) AS `id`, `venta`') @@ -347,7 +347,7 @@ class Venta extends Ideal\Repository return array_map(function($result) { return [ 'unidad_id' => $result['unidad'], - 'venta' => $this->load($result) + 'venta' => $result ]; }, $results); } catch (PDOException $exception) { diff --git a/app/src/Service/Venta.php b/app/src/Service/Venta.php index f7f9985..c7ca268 100644 --- a/app/src/Service/Venta.php +++ b/app/src/Service/Venta.php @@ -108,11 +108,11 @@ class Venta extends Service public function getActiveByUnidadIds(array $unidad_ids): array { try { - $ventas = $this->ventaRepository->fetchActiveByUnidadIds($unidad_ids); + $ventas = $this->ventaRepository->fetchActiveArrayByUnidadIds($unidad_ids); return array_map(function($data) { return [ 'unidad_id' => $data['unidad_id'], - 'venta' => $this->process($data['venta']) + 'venta' => $this->processSimple($data['venta']) ]; }, $ventas); } catch (Implement\Exception\EmptyResult $exception) { @@ -189,6 +189,18 @@ class Venta extends Service return $venta; } + /** + * @param array $venta + * @return array + * @throws Read + */ + protected function processSimple(array $venta): array + { + $output = $venta; + $output['propiedad'] = $this->propiedadService->getArrayById($venta['propiedad']); + return $output; + } + /** * @throws Exception */ diff --git a/app/src/Service/Venta/Propiedad.php b/app/src/Service/Venta/Propiedad.php index 5c0b36c..9c0c984 100644 --- a/app/src/Service/Venta/Propiedad.php +++ b/app/src/Service/Venta/Propiedad.php @@ -7,6 +7,7 @@ use Psr\Log\LoggerInterface; use Incoviba\Common\Define; use Incoviba\Common\Ideal\Service; use Incoviba\Common\Implement\Exception\EmptyResult; +use Incoviba\Exception; use Incoviba\Repository; use Incoviba\Model; @@ -21,6 +22,33 @@ class Propiedad extends Service parent::__construct($logger); } + /** + * @param int $propiedad_id + * @return Model\Venta\Propiedad + * @throws Exception\ServiceAction\Read + */ + public function getById(int $propiedad_id): Model\Venta\Propiedad + { + try { + return $this->process($this->propiedadRepository->fetchById($propiedad_id)); + } catch (EmptyResult $exception) { + throw new Exception\ServiceAction\Read(__CLASS__, $exception); + } + } + + /** + * @param int $propiedad_id + * @return array + * @throws Exception\ServiceAction\Read + */ + public function getArrayById(int $propiedad_id): array + { + try { + return $this->propiedadRepository->fetchArrayById($propiedad_id); + } catch (EmptyResult $exception) { + throw new Exception\ServiceAction\Read(__CLASS__, $exception); + } + } public function addPropiedad(array $ids): Model\Venta\Propiedad { $unidades = []; @@ -90,4 +118,8 @@ class Propiedad extends Service $statement->execute([$propiedad->id, $id]); } } + protected function process(Model\Venta\Propiedad $propiedad): Model\Venta\Propiedad + { + return $propiedad; + } }