This commit is contained in:
2023-06-26 10:49:25 -04:00
parent 42a97bb074
commit 4d00f10274
20 changed files with 350 additions and 33 deletions

View File

@ -1,13 +1,17 @@
<?php
namespace Incoviba\API\Common\Controller;
use Carbon\Carbon;
use Incoviba\API\Common\Alias\Controller;
use Incoviba\Mapper\EstadoVenta;
use Incoviba\Mapper\ProyectoAgente;
use Incoviba\Mapper\TipoEstadoProyecto;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Incoviba\API\Common\Factory\Model as Factory;
use Incoviba\Proyecto\Proyecto;
use Incoviba\Mapper\Proyecto as ProyectoMapper;
use Incoviba\Mapper\EstadoProyecto;
use Incoviba\Mapper\Venta as VentaMapper;
use Incoviba\Mapper\ProyectoTipoUnidad as TipoMapper;
use Incoviba\Mapper\Unidad as UnidadMapper;
@ -27,28 +31,10 @@ class Proyectos extends Controller {
}
return $this->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);
}
}