From c2a3192b325ac5d7480d1fc3ce4c66e5a4504add Mon Sep 17 00:00:00 2001 From: Aldarien Date: Fri, 13 Oct 2023 10:45:21 -0300 Subject: [PATCH] Listado proyectos --- app/resources/routes/03_proyectos.php | 1 + app/resources/routes/api/proyectos.php | 5 +- .../routes/api/proyectos/estados.php | 4 + app/resources/views/proyectos/list.blade.php | 194 ++++++++++++++++++ .../views/proyectos/unidades.blade.php | 4 + app/src/Controller/API/Proyectos.php | 46 +++++ .../API/Proyectos/EstadosProyectos.php | 53 +++++ app/src/Controller/API/Ventas/Cierres.php | 8 +- app/src/Controller/API/Ventas/Cuotas.php | 11 +- app/src/Controller/API/emptyBody.php | 12 ++ app/src/Controller/API/withJson.php | 13 ++ app/src/Controller/Proyectos.php | 51 ++--- app/src/Middleware/NotFound.php | 3 + app/src/Model/Inmobiliaria.php | 7 + app/src/Model/Proyecto.php | 1 + app/src/Model/Proyecto/EstadoProyecto.php | 30 +++ app/src/Model/Proyecto/Etapa.php | 18 ++ app/src/Model/Proyecto/TipoEstadoProyecto.php | 18 ++ app/src/Repository/Proyecto.php | 6 +- app/src/Repository/Proyecto/Elemento.php | 8 +- .../Repository/Proyecto/EstadoProyecto.php | 73 +++++++ app/src/Repository/Proyecto/Etapa.php | 31 +++ .../Proyecto/TipoEstadoProyecto.php | 36 ++++ app/src/Repository/Venta/EstadoVenta.php | 8 +- app/src/Service/Proyecto.php | 21 +- app/src/Service/Venta.php | 8 +- 26 files changed, 612 insertions(+), 58 deletions(-) create mode 100644 app/resources/routes/api/proyectos/estados.php create mode 100644 app/resources/views/proyectos/list.blade.php create mode 100644 app/resources/views/proyectos/unidades.blade.php create mode 100644 app/src/Controller/API/Proyectos.php create mode 100644 app/src/Controller/API/Proyectos/EstadosProyectos.php create mode 100644 app/src/Controller/API/emptyBody.php create mode 100644 app/src/Controller/API/withJson.php create mode 100644 app/src/Model/Proyecto/EstadoProyecto.php create mode 100644 app/src/Model/Proyecto/Etapa.php create mode 100644 app/src/Model/Proyecto/TipoEstadoProyecto.php create mode 100644 app/src/Repository/Proyecto/EstadoProyecto.php create mode 100644 app/src/Repository/Proyecto/Etapa.php create mode 100644 app/src/Repository/Proyecto/TipoEstadoProyecto.php diff --git a/app/resources/routes/03_proyectos.php b/app/resources/routes/03_proyectos.php index 02303aa..4f31a20 100644 --- a/app/resources/routes/03_proyectos.php +++ b/app/resources/routes/03_proyectos.php @@ -2,5 +2,6 @@ use Incoviba\Controller\Proyectos; $app->group('/proyectos', function($app) { + $app->get('/unidades[/]', [Proyectos::class, 'unidades']); $app->get('[/]', Proyectos::class); }); diff --git a/app/resources/routes/api/proyectos.php b/app/resources/routes/api/proyectos.php index 6128fd3..eeb96fb 100644 --- a/app/resources/routes/api/proyectos.php +++ b/app/resources/routes/api/proyectos.php @@ -1,9 +1,12 @@ group('/proyectos', function($app) { $app->get('[/]', [Proyectos::class, 'list']); }); $app->group('/proyecto/{proyecto_id}', function($app) { $app->get('/unidades[/]', [Proyectos::class, 'unidades']); + $app->get('/estados[/]', [Proyectos\EstadosProyectos::class, 'byProyecto']); + $app->get('/estado[/]', [Proyectos\EstadosProyectos::class, 'currentByProyecto']); + $app->get('/inicio[/]', [Proyectos\EstadosProyectos::class, 'firstByProyecto']); }); diff --git a/app/resources/routes/api/proyectos/estados.php b/app/resources/routes/api/proyectos/estados.php new file mode 100644 index 0000000..4dfb6a3 --- /dev/null +++ b/app/resources/routes/api/proyectos/estados.php @@ -0,0 +1,4 @@ +group('/estados'); diff --git a/app/resources/views/proyectos/list.blade.php b/app/resources/views/proyectos/list.blade.php new file mode 100644 index 0000000..f0dc90d --- /dev/null +++ b/app/resources/views/proyectos/list.blade.php @@ -0,0 +1,194 @@ +@extends('layout.base') + +@section('page_content') +
+

Proyectos

+ + + + + + + + + + + + @foreach ($proyectos as $proyecto) + + + + + + + + @endforeach + +
ProyectoInmobiliariaEtapaEstadoTiempo Total
{{$proyecto->descripcion}}{{$proyecto->inmobiliaria()->nombreCompleto()}}
+
+@endsection + +@push('page_scripts') + +@endpush diff --git a/app/resources/views/proyectos/unidades.blade.php b/app/resources/views/proyectos/unidades.blade.php new file mode 100644 index 0000000..8db2665 --- /dev/null +++ b/app/resources/views/proyectos/unidades.blade.php @@ -0,0 +1,4 @@ +@extends('layout.base') + +@section('page_content') +@endsection diff --git a/app/src/Controller/API/Proyectos.php b/app/src/Controller/API/Proyectos.php new file mode 100644 index 0000000..d547dae --- /dev/null +++ b/app/src/Controller/API/Proyectos.php @@ -0,0 +1,46 @@ + 0]; + try { + $proyectos = $proyectoRepository->fetchAllActive(); + $output['proyectos'] = $proyectos; + $output['total'] = count($proyectos); + } catch (EmptyResult) {} + return $this->withJson($response, $output); + } + public function unidades(ServerRequestInterface $request, ResponseInterface $response, Repository\Venta\Unidad $unidadRepository, int $proyecto_id): ResponseInterface + { + $output = ['proyecto_id' => $proyecto_id, 'unidades' => [], 'total' => 0]; + try { + $unidades = $unidadRepository->fetchDisponiblesByProyecto($proyecto_id); + $tipos = []; + foreach ($unidades as $unidad) { + if (!isset($tipos[$unidad->proyectoTipoUnidad->tipoUnidad->descripcion])) { + $tipos[$unidad->proyectoTipoUnidad->tipoUnidad->descripcion] = []; + } + $tipos[$unidad->proyectoTipoUnidad->tipoUnidad->descripcion] []= $unidad; + } + foreach ($tipos as &$subtipo) { + usort($subtipo, function(Model\Venta\Unidad $a, Model\Venta\Unidad $b) { + return strcmp(str_pad($a->descripcion, 4, '0', STR_PAD_LEFT), str_pad($b->descripcion, 4, '0', STR_PAD_LEFT)); + }); + } + $output['unidades'] = $tipos; + $output['total'] = count($unidades); + } catch (EmptyResult) {} + return $this->withJson($response, $output); + } +} diff --git a/app/src/Controller/API/Proyectos/EstadosProyectos.php b/app/src/Controller/API/Proyectos/EstadosProyectos.php new file mode 100644 index 0000000..bc03d06 --- /dev/null +++ b/app/src/Controller/API/Proyectos/EstadosProyectos.php @@ -0,0 +1,53 @@ + $proyecto_id, + 'estados' => [] + ]; + try { + $output['estados'] = $estadoProyectoRepository->fetchByProyecto($proyecto_id); + } catch (EmptyResult) { + return $this->emptyBody($response); + } + return $this->withJson($response, $output); + } + public function currentByProyecto(ServerRequestInterface $request, ResponseInterface $response, Repository\Proyecto\EstadoProyecto $estadoProyectoRepository, int $proyecto_id): ResponseInterface + { + $output = [ + 'proyecto_id' => $proyecto_id, + 'estado' => null + ]; + try { + $output['estado'] = $estadoProyectoRepository->fetchCurrentByProyecto($proyecto_id); + } catch (EmptyResult) { + return $this->emptyBody($response); + } + return $this->withJson($response, $output); + } + public function firstByProyecto(ServerRequestInterface $request, ResponseInterface $response, Repository\Proyecto\EstadoProyecto $estadoProyectoRepository, int $proyecto_id): ResponseInterface + { + $output = [ + 'proyecto_id' => $proyecto_id, + 'estado' => null + ]; + try { + $output['estado'] = $estadoProyectoRepository->fetchFirstByProyecto($proyecto_id); + } catch (EmptyResult) { + return $this->emptyBody($response); + } + return $this->withJson($response, $output); + } +} diff --git a/app/src/Controller/API/Ventas/Cierres.php b/app/src/Controller/API/Ventas/Cierres.php index b07f15b..5531e7e 100644 --- a/app/src/Controller/API/Ventas/Cierres.php +++ b/app/src/Controller/API/Ventas/Cierres.php @@ -4,11 +4,13 @@ namespace Incoviba\Controller\API\Ventas; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Incoviba\Common\Implement\Exception\EmptyResult; +use Incoviba\Controller\API\withJson; use Incoviba\Repository; use Incoviba\Service; class Cierres { + use withJson; public function proyecto(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Cierre $service): ResponseInterface { $body = $request->getBody(); @@ -20,8 +22,7 @@ class Cierres $output['cierres'] = $cierres; $output['total'] = count($cierres); } catch (EmptyResult) {} - $response->getBody()->write(json_encode($output)); - return $response->withHeader('Content-Type', 'application/json'); + return $this->withJson($response, $output); } public function vigentes(ServerRequestInterface $request, ResponseInterface $response, Repository\Venta\Cierre $cierreRepository): ResponseInterface { @@ -49,7 +50,6 @@ class Cierres $output[$row['Proyecto']][$estado] += $row['Cantidad']; $output[$row['Proyecto']]['total'] += $row['Cantidad']; } - $response->getBody()->write(json_encode(['cierres' => $output])); - return $response->withHeader('Content-Type', 'application/json'); + return $this->withJson($response, ['cierres' => $output]); } } diff --git a/app/src/Controller/API/Ventas/Cuotas.php b/app/src/Controller/API/Ventas/Cuotas.php index d81a016..05df9df 100644 --- a/app/src/Controller/API/Ventas/Cuotas.php +++ b/app/src/Controller/API/Ventas/Cuotas.php @@ -3,6 +3,7 @@ namespace Incoviba\Controller\API\Ventas; use DateTimeImmutable; use DateInterval; +use Incoviba\Controller\API\withJson; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Incoviba\Repository; @@ -10,21 +11,20 @@ use Incoviba\Service; class Cuotas { + use withJson; public function hoy(ServerRequestInterface $request, ResponseInterface $response, Repository\Venta\Cuota $cuotaRepository): ResponseInterface { $output = [ 'cuotas' => count($cuotaRepository->fetchHoy()) ?? 0 ]; - $response->getBody()->write(json_encode($output)); - return $response->withHeader('Content-Type', 'application/json'); + return $this->withJson($response, $output); } public function pendiente(ServerRequestInterface $request, ResponseInterface $response, Repository\Venta\Cuota $cuotaRepository): ResponseInterface { $output = [ 'cuotas' => count($cuotaRepository->fetchPendientes()) ?? 0 ]; - $response->getBody()->write(json_encode($output)); - return $response->withHeader('Content-Type', 'application/json'); + return $this->withJson($response, $output); } public function porVencer(ServerRequestInterface $request, ResponseInterface $response, Repository\Venta\Cuota $cuotaRepository, Service\Format $formatService): ResponseInterface { @@ -53,7 +53,6 @@ class Cuotas }); $output[$key] = $day; } - $response->getBody()->write(json_encode(['cuotas' => $output])); - return $response->withHeader('Content-Type', 'application/json'); + return $this->withJson($response, ['cuotas' => $output]); } } diff --git a/app/src/Controller/API/emptyBody.php b/app/src/Controller/API/emptyBody.php new file mode 100644 index 0000000..6392565 --- /dev/null +++ b/app/src/Controller/API/emptyBody.php @@ -0,0 +1,12 @@ +withStatus(204); + } +} diff --git a/app/src/Controller/API/withJson.php b/app/src/Controller/API/withJson.php new file mode 100644 index 0000000..78aa191 --- /dev/null +++ b/app/src/Controller/API/withJson.php @@ -0,0 +1,13 @@ +getBody()->write(json_encode($data)); + return $response->withHeader('Content-Type', 'application/json'); + } +} diff --git a/app/src/Controller/Proyectos.php b/app/src/Controller/Proyectos.php index 39cc1e4..81c53b9 100644 --- a/app/src/Controller/Proyectos.php +++ b/app/src/Controller/Proyectos.php @@ -4,48 +4,29 @@ namespace Incoviba\Controller; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Incoviba\Common\Alias\View; -use Incoviba\Common\Implement\Exception\EmptyResult; use Incoviba\Repository; use Incoviba\Model; class Proyectos { - public function __invoke(ServerRequestInterface $request, ResponseInterface $response, View $view): ResponseInterface + public function __invoke(ServerRequestInterface $request, ResponseInterface $response, View $view, Repository\Proyecto $proyectoRepository): ResponseInterface { - return $view->render($response, 'proyectos.list'); + $proyectos = $proyectoRepository->fetchAll(); + usort($proyectos, function(Model\Proyecto $a, Model\Proyecto $b) { + return strcmp($a->descripcion, $b->descripcion); + }); + return $view->render($response, 'proyectos.list', compact('proyectos')); } - public function list(ServerRequestInterface $request, ResponseInterface $response, Repository\Proyecto $proyectoRepository): ResponseInterface + public function unidades(ServerRequestInterface $request, ResponseInterface $response, View $view, Repository\Proyecto $proyectoRepository, Repository\Venta\Unidad $unidadRepository): ResponseInterface { - $output = ['total' => 0]; - try { - $proyectos = $proyectoRepository->fetchAllActive(); - $output['proyectos'] = $proyectos; - $output['total'] = count($proyectos); - } catch (EmptyResult) {} - $response->getBody()->write(json_encode($output)); - return $response->withHeader('Content-Type', 'application/json'); - } - public function unidades(ServerRequestInterface $request, ResponseInterface $response, Repository\Venta\Unidad $unidadRepository, int $proyecto_id): ResponseInterface - { - $output = ['proyecto_id' => $proyecto_id, 'unidades' => [], 'total' => 0]; - try { - $unidades = $unidadRepository->fetchDisponiblesByProyecto($proyecto_id); - $tipos = []; - foreach ($unidades as $unidad) { - if (!isset($tipos[$unidad->proyectoTipoUnidad->tipoUnidad->descripcion])) { - $tipos[$unidad->proyectoTipoUnidad->tipoUnidad->descripcion] = []; - } - $tipos[$unidad->proyectoTipoUnidad->tipoUnidad->descripcion] []= $unidad; - } - foreach ($tipos as &$subtipo) { - usort($subtipo, function(Model\Venta\Unidad $a, Model\Venta\Unidad $b) { - return strcmp(str_pad($a->descripcion, 4, '0', STR_PAD_LEFT), str_pad($b->descripcion, 4, '0', STR_PAD_LEFT)); - }); - } - $output['unidades'] = $tipos; - $output['total'] = count($unidades); - } catch (EmptyResult) {} - $response->getBody()->write(json_encode($output)); - return $response->withHeader('Content-Type', 'application/json'); + $proyectos = $proyectoRepository->fetchAll(); + $proyectos = array_filter($proyectos, function(Model\Proyecto $proyecto) use ($unidadRepository) { + $unidades = $unidadRepository->fetchByProyecto($proyecto->id); + return count($unidades) > 0; + }); + usort($proyectos, function(Model\Proyecto $a, Model\Proyecto $b) { + return strcmp($a->descripcion, $b->descripcion); + }); + return $view->render($response, 'proyectos.unidades', compact('proyectos')); } } diff --git a/app/src/Middleware/NotFound.php b/app/src/Middleware/NotFound.php index 8896854..8971be4 100644 --- a/app/src/Middleware/NotFound.php +++ b/app/src/Middleware/NotFound.php @@ -19,6 +19,9 @@ class NotFound } catch (HttpNotFoundException $exception) { $this->logger->warning($exception); $response = $this->responseFactory->createResponse(404); + if (str_contains($request->getUri()->getPath(), '/api')) { + return $response; + } return $this->view->render($response, 'not_found'); } } diff --git a/app/src/Model/Inmobiliaria.php b/app/src/Model/Inmobiliaria.php index cc7778c..83400ab 100644 --- a/app/src/Model/Inmobiliaria.php +++ b/app/src/Model/Inmobiliaria.php @@ -21,6 +21,13 @@ class Inmobiliaria extends Model $this->dv ]); } + public function nombreCompleto(): string + { + return implode(' ', [ + $this->razon, + $this->tipoSociedad->descripcion + ]); + } public function jsonSerialize(): mixed { diff --git a/app/src/Model/Proyecto.php b/app/src/Model/Proyecto.php index 633e9e7..a0ab993 100644 --- a/app/src/Model/Proyecto.php +++ b/app/src/Model/Proyecto.php @@ -13,6 +13,7 @@ class Proyecto extends Ideal\Model public float $corredor; public int $pisos; public int $subterraneos; + protected Proyecto\Etapa $etapa; public function inmobiliaria(): Inmobiliaria { diff --git a/app/src/Model/Proyecto/EstadoProyecto.php b/app/src/Model/Proyecto/EstadoProyecto.php new file mode 100644 index 0000000..061d0f0 --- /dev/null +++ b/app/src/Model/Proyecto/EstadoProyecto.php @@ -0,0 +1,30 @@ +proyecto)) { + $this->proyecto = $this->runFactory('proyecto'); + } + return $this->proyecto; + } + + public function jsonSerialize(): mixed + { + return array_merge(parent::jsonSerialize(), [ + 'proyecto' => $this->proyecto(), + 'tipo_estado_proyecto' => $this->tipoEstadoProyecto, + 'fecha' => $this->fecha + ]); + } +} diff --git a/app/src/Model/Proyecto/Etapa.php b/app/src/Model/Proyecto/Etapa.php new file mode 100644 index 0000000..10f7d97 --- /dev/null +++ b/app/src/Model/Proyecto/Etapa.php @@ -0,0 +1,18 @@ + $this->descripcion, + 'orden' => $this->orden + ]); + } +} diff --git a/app/src/Model/Proyecto/TipoEstadoProyecto.php b/app/src/Model/Proyecto/TipoEstadoProyecto.php new file mode 100644 index 0000000..a6b0485 --- /dev/null +++ b/app/src/Model/Proyecto/TipoEstadoProyecto.php @@ -0,0 +1,18 @@ + $this->orden, + 'etapa' => $this->etapa + ]); + } +} diff --git a/app/src/Repository/Proyecto.php b/app/src/Repository/Proyecto.php index 03efa2c..2eb9530 100644 --- a/app/src/Repository/Proyecto.php +++ b/app/src/Repository/Proyecto.php @@ -8,7 +8,11 @@ use Incoviba\Model; class Proyecto extends Ideal\Repository { - public function __construct(Define\Connection $connection, protected Inmobiliaria $inmobiliariaRepository, protected Direccion $direccionRepository) + public function __construct( + Define\Connection $connection, + protected Inmobiliaria $inmobiliariaRepository, + protected Direccion $direccionRepository + ) { parent::__construct($connection); $this->setTable('proyecto'); diff --git a/app/src/Repository/Proyecto/Elemento.php b/app/src/Repository/Proyecto/Elemento.php index 119fb6f..5da5e99 100644 --- a/app/src/Repository/Proyecto/Elemento.php +++ b/app/src/Repository/Proyecto/Elemento.php @@ -14,12 +14,12 @@ class Elemento extends Ideal\Repository $this->setTable('tipo_elemento'); } - public function create(?array $data = null): Define\Model + public function create(?array $data = null): Model\Proyecto\Elemento { - $map = (new Implement\Repository\MapperParser(['descripcion', 'abreviacion', 'orden'])); + $map = new Implement\Repository\MapperParser(['descripcion', 'abreviacion', 'orden']); return $this->parseData(new Model\Proyecto\Elemento(), $data, $map); } - public function save(Define\Model $model): Define\Model + public function save(Define\Model $model): Model\Proyecto\Elemento { $model->id = $this->saveNew( ['descripcion', 'abreviacion', 'orden'], @@ -27,7 +27,7 @@ class Elemento extends Ideal\Repository ); return $model; } - public function edit(Define\Model $model, array $new_data): Define\Model + public function edit(Define\Model $model, array $new_data): Model\Proyecto\Elemento { return $this->update($model, ['descripcion', 'abreviacion', 'orden'], $new_data); } diff --git a/app/src/Repository/Proyecto/EstadoProyecto.php b/app/src/Repository/Proyecto/EstadoProyecto.php new file mode 100644 index 0000000..b27a0ca --- /dev/null +++ b/app/src/Repository/Proyecto/EstadoProyecto.php @@ -0,0 +1,73 @@ +setTable('estado_proyecto'); + } + + public function create(?array $data = null): Model\Proyecto\EstadoProyecto + { + $map = (new Implement\Repository\MapperParser()) + ->register('proyecto', (new Implement\Repository\Mapper()) + ->setFactory((new Implement\Repository\Factory()) + ->setCallable([$this->proyectoRepository, 'fetchById']) + ->setArgs([$data['proyecto']]))) + ->register('estado', (new Implement\Repository\Mapper()) + ->setProperty('tipoEstadoProyecto') + ->setFunction(function($data) { + return $this->tipoEstadoProyectoRepository->fetchById($data['estado']); + })) + ->register('fecha', new Implement\Repository\Mapper\DateTime('fecha')) + ; + return $this->parseData(new Model\Proyecto\EstadoProyecto(), $data, $map); + } + public function save(Define\Model $model): Model\Proyecto\EstadoProyecto + { + $model->id = $this->saveNew(['proyecto', 'estado', 'fecha'], [ + $model->proyecto()->id, + $model->tipoEstadoProyecto()->id, + $model->fecha->format('Y-m-d') + ]); + return $model; + } + public function edit(Define\Model $model, array $new_data): Model\Proyecto\EstadoProyecto + { + return $this->update($model, ['proyecto', 'estado', 'fecha'], $new_data); + } + + public function fetchByProyecto(int $proyecto_id): array + { + $query = "SELECT * FROM `{$this->getTable()}` WHERE `proyecto` = ?"; + return $this->fetchMany($query, [$proyecto_id]); + } + public function fetchCurrentByProyecto(int $proyecto_id): Model\Proyecto\EstadoProyecto + { + $query = "SELECT a.* +FROM `{$this->getTable()}` a + JOIN (SELECT MAX(`id`) AS 'id', `proyecto` FROM `{$this->getTable()}` GROUP BY `proyecto`) e0 ON e0.`id` = a.`id` +WHERE a.`proyecto` = ?"; + return $this->fetchOne($query, [$proyecto_id]); + } + public function fetchFirstByProyecto(int $proyecto_id): Model\Proyecto\EstadoProyecto + { + $query = "SELECT a.* +FROM `{$this->getTable()}` a + JOIN (SELECT MIN(`id`) AS 'id', `proyecto` FROM `{$this->getTable()}` GROUP BY `proyecto`) e0 ON e0.`id` = a.`id` +WHERE a.`proyecto` = ?"; + return $this->fetchOne($query, [$proyecto_id]); + } +} diff --git a/app/src/Repository/Proyecto/Etapa.php b/app/src/Repository/Proyecto/Etapa.php new file mode 100644 index 0000000..e81a1a6 --- /dev/null +++ b/app/src/Repository/Proyecto/Etapa.php @@ -0,0 +1,31 @@ +setTable('etapa_proyecto'); + } + + public function create(?array $data = null): Model\Proyecto\Etapa + { + $map = new Implement\Repository\MapperParser(['descripcion', 'orden']); + return $this->parseData(new Model\Proyecto\Etapa(), $data, $map); + } + public function save(Define\Model $model): Model\Proyecto\Etapa + { + $model->id = $this->saveNew(['descripcion', 'orden'], [$model->descripcion, $model->orden]); + return $model; + } + public function edit(Define\Model $model, array $new_data): Model\Proyecto\Etapa + { + return $this->update($model, ['descripcion', 'orden'], $new_data); + } +} diff --git a/app/src/Repository/Proyecto/TipoEstadoProyecto.php b/app/src/Repository/Proyecto/TipoEstadoProyecto.php new file mode 100644 index 0000000..188cb63 --- /dev/null +++ b/app/src/Repository/Proyecto/TipoEstadoProyecto.php @@ -0,0 +1,36 @@ +setTable('tipo_estado_proyecto'); + } + + public function create(?array $data = null): Model\Proyecto\TipoEstadoProyecto + { + $map = (new Implement\Repository\MapperParser(['descripcion', 'orden'])) + ->register('etapa', (new Implement\Repository\Mapper()) + ->setFunction(function($data) { + return $this->etapaRepository->fetchById($data['etapa']); + })); + return $this->parseData(new Model\Proyecto\TipoEstadoProyecto(), $data, $map); + } + public function save(Define\Model $model): Model\Proyecto\TipoEstadoProyecto + { + $model->id = $this->saveNew(['descripcion', 'orden', 'etapa'], [$model->descripcion, $model->orden, $model->etapa->id]); + return $model; + } + public function edit(Define\Model $model, array $new_data): Model\Proyecto\TipoEstadoProyecto + { + return $this->update($model, ['descripcion', 'orden', 'etapa'], $new_data); + } +} diff --git a/app/src/Repository/Venta/EstadoVenta.php b/app/src/Repository/Venta/EstadoVenta.php index d0637fc..caaaba6 100644 --- a/app/src/Repository/Venta/EstadoVenta.php +++ b/app/src/Repository/Venta/EstadoVenta.php @@ -17,7 +17,7 @@ class EstadoVenta extends Ideal\Repository $this->setTable('estado_venta'); } - public function create(?array $data = null): Define\Model + public function create(?array $data = null): Model\Venta\EstadoVenta { $map = (new Implement\Repository\MapperParser()) ->register('venta', (new Implement\Repository\Mapper()) @@ -32,7 +32,7 @@ class EstadoVenta extends Ideal\Repository ->register('fecha', new Implement\Repository\Mapper\DateTime('fecha')); return $this->parseData(new Model\Venta\EstadoVenta(), $data, $map); } - public function save(Define\Model $model): Define\Model + public function save(Define\Model $model): Model\Venta\EstadoVenta { $model->id = $this->saveNew( ['venta', 'estado', 'fecha'], @@ -40,7 +40,7 @@ class EstadoVenta extends Ideal\Repository ); return $model; } - public function edit(Define\Model $model, array $new_data): Define\Model + public function edit(Define\Model $model, array $new_data): Model\Venta\EstadoVenta { return $this->update($model, ['venta', 'estado', 'fecha'], $new_data); } @@ -50,7 +50,7 @@ class EstadoVenta extends Ideal\Repository $query = "SELECT * FROM `{$this->getTable()}` WHERE `venta` = ?"; return $this->fetchMany($query, [$venta_id]); } - public function fetchCurrentByVenta(int $venta_id): Define\Model + public function fetchCurrentByVenta(int $venta_id): Model\Venta\EstadoVenta { $query = "SELECT a.* FROM `{$this->getTable()}` a diff --git a/app/src/Service/Proyecto.php b/app/src/Service/Proyecto.php index eb2f1b2..c6de619 100644 --- a/app/src/Service/Proyecto.php +++ b/app/src/Service/Proyecto.php @@ -1,13 +1,32 @@ proyectoRepository->fetchAllActive(); } + public function getById(int $venta_id): Model\Proyecto + { + return $this->process($this->proyectoRepository->fetchById($venta_id)); + } + protected function process(Model\Proyecto $proyecto): Model\Proyecto + { + $proyecto->addFactory('estados', (new Implement\Repository\Factory()) + ->setCallable([$this->estadoProyecto, 'fetchByProyecto']) + ->setArgs([$proyecto->id])); + $proyecto->addFactory('currentEstado', (new Implement\Repository\Factory()) + ->setCallable([$this->estadoProyecto, 'fetchCurrentByProyecto']) + ->setArgs([$proyecto->id])); + return $proyecto; + } } diff --git a/app/src/Service/Venta.php b/app/src/Service/Venta.php index 086a2cd..93fdd1d 100644 --- a/app/src/Service/Venta.php +++ b/app/src/Service/Venta.php @@ -73,8 +73,12 @@ class Venta protected function process(Model\Venta $venta): Model\Venta { - $venta->addFactory('estados', (new Implement\Repository\Factory())->setCallable([$this->estadoVentaRepository, 'fetchByVenta'])->setArgs([$venta->id])); - $venta->addFactory('currentEstado', (new Implement\Repository\Factory())->setCallable([$this->estadoVentaRepository, 'fetchCurrentByVenta'])->setArgs([$venta->id])); + $venta->addFactory('estados', (new Implement\Repository\Factory()) + ->setCallable([$this->estadoVentaRepository, 'fetchByVenta']) + ->setArgs([$venta->id])); + $venta->addFactory('currentEstado', (new Implement\Repository\Factory()) + ->setCallable([$this->estadoVentaRepository, 'fetchCurrentByVenta']) + ->setArgs([$venta->id])); return $venta; }