From 4d00f1027449e1a6fef071a4bd17be6da7718653 Mon Sep 17 00:00:00 2001 From: Aldarien Date: Mon, 26 Jun 2023 10:49:25 -0400 Subject: [PATCH] Mappers --- common/Controller/Proyectos.php | 89 +++++++++++++++++------ resources/routes/proyectos.php | 6 +- src/Mapper/Agente.php | 26 +++++++ src/Mapper/AgenteTipo.php | 2 + src/Mapper/Comuna.php | 25 +++++++ src/Mapper/Direccion.php | 4 +- src/Mapper/EstadoProyecto.php | 55 ++++++++++++++ src/Mapper/Etapa.php | 25 +++++++ src/Mapper/ProyectoAgente.php | 15 ++++ src/Mapper/TipoAgente.php | 24 ++++++ src/Mapper/TipoEstadoProyecto.php | 26 +++++++ src/Model/Common/Comuna.php | 4 +- src/Model/Common/Direccion.php | 21 +++++- src/Model/Proyecto/Agente.php | 12 +++ src/Model/Proyecto/AgenteTipo.php | 4 +- src/Model/Proyecto/EstadoProyecto.php | 11 +++ src/Model/Proyecto/Etapa.php | 10 +++ src/Model/Proyecto/Proyecto.php | 4 +- src/Model/Proyecto/TipoAgente.php | 9 +++ src/Model/Proyecto/TipoEstadoProyecto.php | 11 +++ 20 files changed, 350 insertions(+), 33 deletions(-) create mode 100644 src/Mapper/Agente.php create mode 100644 src/Mapper/Comuna.php create mode 100644 src/Mapper/EstadoProyecto.php create mode 100644 src/Mapper/Etapa.php create mode 100644 src/Mapper/TipoAgente.php create mode 100644 src/Mapper/TipoEstadoProyecto.php create mode 100644 src/Model/Proyecto/Agente.php create mode 100644 src/Model/Proyecto/EstadoProyecto.php create mode 100644 src/Model/Proyecto/Etapa.php create mode 100644 src/Model/Proyecto/TipoAgente.php create mode 100644 src/Model/Proyecto/TipoEstadoProyecto.php diff --git a/common/Controller/Proyectos.php b/common/Controller/Proyectos.php index 1eee921..a23db86 100644 --- a/common/Controller/Proyectos.php +++ b/common/Controller/Proyectos.php @@ -1,13 +1,17 @@ withJson($response, compact('proyectos')); } - public function show(Request $request, Response $response, Factory $factory, $proyecto_id): Response { - $proyecto = $factory->find(Proyecto::class)->one($proyecto_id); + public function show(Request $request, Response $response, $proyecto_id): Response { + $proyecto = $this->getMapper(ProyectoMapper::class)->fetchById($proyecto_id); $output = [ - 'input' => $proyecto_id, - 'proyecto' => $proyecto->toArray(), - 'link' => [ - 'rel' => 'proyectos', - 'title' => 'Proyectos', - 'href' => str_replace("/proyecto/{$proyecto_id}", '/proyectos', $request->getUri()) - ] - ]; - $output['links'] = [ - [ - 'rel' => 'inmobiliaria', - 'title' => $proyecto->inmobiliaria()->abreviacion, - 'href' => str_replace("/proyecto/{$proyecto_id}", "/inmobiliaria/{$proyecto->inmobiliaria()->rut}", $request->getUri()) - ], - [ - 'rel' => 'direccion', - 'title' => $proyecto->direccion()->calle, - 'href' => str_replace("/proyecto/{$proyecto_id}", "/direccion/{$proyecto->direccion}", $request->getUri()) - ] + 'proyecto' => json_decode(json_encode($proyecto), JSON_OBJECT_AS_ARRAY) ]; return $this->withJson($response, $output); } @@ -95,7 +81,7 @@ class Proyectos extends Controller { return $this->withJson($response, $output); } - public function ventas(Request $request, Response $response, $proyecto_id): Response + public function ventas(Request $request, Response $response, int $proyecto_id): Response { $proyecto = $this->getMapper(ProyectoMapper::class)->fetchById($proyecto_id); $ventas = $this->getMapper(VentaMapper::class)->fetchActivaByProyecto($proyecto_id); @@ -107,7 +93,7 @@ class Proyectos extends Controller { } return $this->withJson($response, $output); } - public function precios(Request $request, Response $response, Format $format, $proyecto_id): Response { + public function precios(Request $request, Response $response, Format $format, int $proyecto_id): Response { $proyecto = $this->getMapper(ProyectoMapper::class)->fetchById($proyecto_id); $output = json_decode(json_encode(compact('proyecto')), JSON_OBJECT_AS_ARRAY); $tipos = $this->getMapper(TipoMapper::class)->fetchByProyecto($proyecto_id); @@ -138,4 +124,61 @@ class Proyectos extends Controller { } return $this->withJson($response, $output); } + public function estado(Request $request, Response $response, int $proyecto_id): Response + { + $proyecto = $this->getMapper(ProyectoMapper::class)->fetchById($proyecto_id); + $estado = $this->getMapper(EstadoProyecto::class)->fetchLastByProyecto($proyecto_id); + $output = [ + 'proyecto' => $proyecto, + 'estado' => json_decode(json_encode($estado), JSON_OBJECT_AS_ARRAY) + ]; + $output['estado']['fecha']['diff'] = Carbon::createFromTimestamp($estado->fecha->getTimestamp())->locale('es-CL')->diffForHumans(); + return $this->withJson($response, $output); + } + public function inicio(Request $request, Response $response, int $proyecto_id): Response + { + $proyecto = $this->getMapper(ProyectoMapper::class)->fetchById($proyecto_id); + $estado = $this->getMapper(EstadoProyecto::class)->fetchFirstByProyecto($proyecto_id); + $output = [ + 'proyecto' => $proyecto, + 'estado' => json_decode(json_encode($estado), JSON_OBJECT_AS_ARRAY) + ]; + $output['estado']['fecha']['diff'] = Carbon::createFromTimestamp($estado->fecha->getTimestamp())->locale('es-CL')->diffForHumans(); + if ($estado->fecha->getTimestamp() < 0) { + $output['estado']['fecha']['diff'] = '-'; + } + return $this->withJson($response, $output); + } + public function progreso(Request $request, Response $response, int $proyecto_id): Response + { + $proyecto = $this->getMapper(ProyectoMapper::class)->fetchById($proyecto_id); + $inicio = $this->getMapper(EstadoProyecto::class)->fetchFirstByProyecto($proyecto_id); + $estado = $this->getMapper(EstadoProyecto::class)->fetchLastByProyecto($proyecto_id); + $orden_tipo_estados = array_map(function($item) { + return $item->orden; + }, $this->getMapper(TipoEstadoProyecto::class)->fetchAll()); + $estados = $this->getMapper(EstadoProyecto::class)->fetchByProyecto($proyecto_id); + $total = 1; + if ($estado->tipo->orden < max($orden_tipo_estados) - 1 and count($estados) > 1) { + $tf = Carbon::createFromTimestamp($estado->fecha->getTimestamp()); + $t0 = Carbon::createFromTimestamp($inicio->fecha->getTimestamp()); + $df = $tf->diffInSeconds($t0); + $hoy = Carbon::now(); + $dh = $hoy->diffInSeconds($t0); + $total = $df / $dh; + } + $valor = round(($estado->tipo->orden + 1) / max($orden_tipo_estados) * 100 * $total); + $output = [ + 'proyecto' => $proyecto, + 'progreso' => $valor + ]; + return $this->withJson($response, $output); + } + public function operadores(Request $request, Response $response, int $proyecto_id): Response + { + $proyecto = $this->getMapper(ProyectoMapper::class)->fetchById($proyecto_id); + $operadores = $this->getMapper(ProyectoAgente::class)->fetchOperadoresVigenteByProyecto($proyecto_id); + $output = compact('proyecto', 'operadores'); + return $this->withJson($response, $output); + } } diff --git a/resources/routes/proyectos.php b/resources/routes/proyectos.php index 73db1b6..96ceca0 100644 --- a/resources/routes/proyectos.php +++ b/resources/routes/proyectos.php @@ -18,7 +18,11 @@ $app->group('/proyectos', function ($app) { $app->group('/proyecto/{proyecto_id}', function ($app) { $app->get('/ventas', [Proyectos::class, 'ventas']); $app->get('/precios', [Proyectos::class, 'precios']); + $app->get('/inicio', [Proyectos::class, 'inicio']); + $app->get('/estado', [Proyectos::class, 'estado']); + $app->get('/progreso', [Proyectos::class, 'progreso']); + $app->get('/operadores', [Proyectos::class, 'operadores']); $app->put('/edit[/]', [Proyectos::class, 'edit']); - $app->delete('/edit[/]', [Proyectos::class, 'delete']); + $app->delete('/delete[/]', [Proyectos::class, 'delete']); $app->get('[/]', [Proyectos::class, 'show']); }); diff --git a/src/Mapper/Agente.php b/src/Mapper/Agente.php new file mode 100644 index 0000000..e1375d5 --- /dev/null +++ b/src/Mapper/Agente.php @@ -0,0 +1,26 @@ +id = $row['id']; + $model->rut = $row['rut']; + $model->descripcion = $row['descripcion']; + $model->abreviacion = $row['abreviacion']; + return $model; + } + + public function save(Model $model): bool + { + // TODO: Implement save() method. + } +} diff --git a/src/Mapper/AgenteTipo.php b/src/Mapper/AgenteTipo.php index b63b85d..e2246f2 100644 --- a/src/Mapper/AgenteTipo.php +++ b/src/Mapper/AgenteTipo.php @@ -13,6 +13,8 @@ class AgenteTipo extends Mapper { $model = new AgenteModel(); $model->id = $row['id']; + $model->agente = $this->getMapper(Agente::class)->fetchById($row['agente']); + $model->tipo = $this->getMapper(TipoAgente::class)->fetchById($row['tipo']); return $model; } diff --git a/src/Mapper/Comuna.php b/src/Mapper/Comuna.php new file mode 100644 index 0000000..0125281 --- /dev/null +++ b/src/Mapper/Comuna.php @@ -0,0 +1,25 @@ +id = $row['id']; + $model->descripcion = $row['descripcion']; + // $model->provincia = $this->getMapper(Provincia::class)->fetchById($row['provincia']); + return $model; + } + + public function save(Model $model): bool + { + // TODO: Implement save() method. + } +} diff --git a/src/Mapper/Direccion.php b/src/Mapper/Direccion.php index 69282e8..8383b3f 100644 --- a/src/Mapper/Direccion.php +++ b/src/Mapper/Direccion.php @@ -8,9 +8,6 @@ use Incoviba\Model\Common\Direccion as DireccionModel; class Direccion extends Mapper { protected string $table = 'direccion'; - /*protected array $required = [ - Comuna::class - ];*/ protected function load(bool|array $row, bool $lazy = false): DireccionModel|bool { @@ -20,6 +17,7 @@ class Direccion extends Mapper $model->calle = $row['calle']; $model->numero = $row['numero']; $model->extra = $row['extra']; + $model->comuna = $this->getMapper(Comuna::class)->fetchById($row['comuna']); return $model; } diff --git a/src/Mapper/EstadoProyecto.php b/src/Mapper/EstadoProyecto.php new file mode 100644 index 0000000..c8bef77 --- /dev/null +++ b/src/Mapper/EstadoProyecto.php @@ -0,0 +1,55 @@ +id = $row['id']; + $model->proyecto = $this->getMapper(Proyecto::class)->fetchById($row['proyecto']); + $model->tipo = $this->getMapper(TipoEstadoProyecto::class)->fetchById($row['estado']); + $model->fecha = new \DateTimeImmutable($row['fecha']); + return $model; + } + + public function save(Model $model): bool + { + // TODO: Implement save() method. + } + + public function fetchByProyecto(int $proyecto_id): array + { + $qb = $this->connection->createQueryBuilder() + ->select('*') + ->from($this->table) + ->where('proyecto = ?'); + return array_map([$this, 'load'], $this->connection->executeQuery($qb, [$proyecto_id])->fetchAllAssociative()); + } + public function fetchLastByProyecto(int $proyecto_id): BaseModel|bool + { + $qb = $this->connection->createQueryBuilder() + ->select('*') + ->from($this->table) + ->where('proyecto = ?') + ->setMaxResults(1) + ->orderBy('id', 'desc'); + return $this->load($this->connection->executeQuery($qb, [$proyecto_id])->fetchAssociative()); + } + public function fetchFirstByProyecto(int $proyecto_id): BaseModel|bool + { + $qb = $this->connection->createQueryBuilder() + ->select('*') + ->from($this->table) + ->where('proyecto = ?') + ->setMaxResults(1) + ->orderBy('id', 'asc'); + return $this->load($this->connection->executeQuery($qb, [$proyecto_id])->fetchAssociative()); + } +} diff --git a/src/Mapper/Etapa.php b/src/Mapper/Etapa.php new file mode 100644 index 0000000..7decbd9 --- /dev/null +++ b/src/Mapper/Etapa.php @@ -0,0 +1,25 @@ +id = $row['id']; + $model->descripcion = $row['descripcion']; + $model->orden = $row['orden']; + return $model; + } + + public function save(Model $model): bool + { + // TODO: Implement save() method. + } +} diff --git a/src/Mapper/ProyectoAgente.php b/src/Mapper/ProyectoAgente.php index 70a0a11..9ecbcb6 100644 --- a/src/Mapper/ProyectoAgente.php +++ b/src/Mapper/ProyectoAgente.php @@ -23,4 +23,19 @@ class ProyectoAgente extends Mapper { // TODO: Implement save() method. } + + public function fetchOperadoresVigenteByProyecto(int $proyecto_id): array + { + $qb = $this->connection->createQueryBuilder() + ->select('pa.*') + ->from($this->table, 'pa') + ->innerJoin('pa', 'agente_tipo', 'at', 'at.id = pa.agente') + ->innerJoin('at', 'tipo_agente', 'ta', 'ta.id = at.tipo') + ->innerJoin('pa', '(SELECT e1.* FROM estado_proyecto_agente e1 JOIN (SELECT MAX(id) AS id, agente FROM estado_proyecto_agente GROUP BY agente) e0 ON e0.id = e1.id)', 'ep', 'ep.agente = pa.id') + ->innerJoin('ep', 'tipo_estado_proyecto_agente', 'tep', 'tep.id = ep.tipo') + ->where('pa.proyecto = ?') + ->andWhere('ta.descripcion = ?') + ->andWhere('tep.descripcion = ?'); + return array_map([$this, 'load'], $this->connection->executeQuery($qb, [$proyecto_id, 'operador', 'vigente'])->fetchAllAssociative()); + } } diff --git a/src/Mapper/TipoAgente.php b/src/Mapper/TipoAgente.php new file mode 100644 index 0000000..1668ead --- /dev/null +++ b/src/Mapper/TipoAgente.php @@ -0,0 +1,24 @@ +id = $row['id']; + $model->descripcion = $row['descripcion']; + return $model; + } + + public function save(Model $model): bool + { + // TODO: Implement save() method. + } +} diff --git a/src/Mapper/TipoEstadoProyecto.php b/src/Mapper/TipoEstadoProyecto.php new file mode 100644 index 0000000..dae795a --- /dev/null +++ b/src/Mapper/TipoEstadoProyecto.php @@ -0,0 +1,26 @@ +id = $row['id']; + $model->descripcion = $row['descripcion']; + $model->orden = $row['orden']; + $model->etapa = $this->getMapper(Etapa::class)->fetchById($row['etapa']); + return $model; + } + + public function save(Model $model): bool + { + // TODO: Implement save() method. + } +} diff --git a/src/Model/Common/Comuna.php b/src/Model/Common/Comuna.php index 748f307..e195379 100644 --- a/src/Model/Common/Comuna.php +++ b/src/Model/Common/Comuna.php @@ -4,5 +4,7 @@ namespace Incoviba\Model\Common; class Comuna extends \Incoviba\Model\Model { - + public int $id; + public string $descripcion; + // public bool|Provincia $provincia; } diff --git a/src/Model/Common/Direccion.php b/src/Model/Common/Direccion.php index 4a2d8c5..50ae3fa 100644 --- a/src/Model/Common/Direccion.php +++ b/src/Model/Common/Direccion.php @@ -7,5 +7,24 @@ class Direccion extends \Incoviba\Model\Model public string $calle; public int $numero; public string $extra; - public Comuna $comuna; + public bool|Comuna $comuna; + + public function completa(): string + { + $str = implode(' ', [$this->calle, $this->numero]); + if ($this->extra != '') { + $str .= ' ' . $this->extra; + } + if ($this->comuna) { + $str .= ', ' . $this->comuna->descripcion; + } + return $str; + } + + public function jsonSerialize(): mixed + { + $arr = parent::jsonSerialize(); + $arr['completa'] = $this->completa(); + return $arr; + } } diff --git a/src/Model/Proyecto/Agente.php b/src/Model/Proyecto/Agente.php new file mode 100644 index 0000000..79e7c0c --- /dev/null +++ b/src/Model/Proyecto/Agente.php @@ -0,0 +1,12 @@ +