185 lines
9.2 KiB
PHP
185 lines
9.2 KiB
PHP
<?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;
|
|
use Incoviba\Mapper\Precio as PrecioMapper;
|
|
use Incoviba\Mapper\EstadoPrecio as EstadoMapper;
|
|
use Incoviba\API\Common\Service\Format;
|
|
|
|
class Proyectos extends Controller {
|
|
public function __invoke(Request $request, Response $response): Response {
|
|
$proyectos = $this->getMapper(ProyectoMapper::class)->fetchAll();
|
|
usort($proyectos, function($a, $b) {
|
|
return strcmp($a->descripcion, $b->descripcion);
|
|
});
|
|
$proyectos = json_decode(json_encode($proyectos), JSON_OBJECT_AS_ARRAY);
|
|
foreach ($proyectos as &$proyecto) {
|
|
$proyecto['ventas'] = $this->getMapper(VentaMapper::class)->fetchCountByProyecto($proyecto['id']);
|
|
}
|
|
return $this->withJson($response, compact('proyectos'));
|
|
}
|
|
public function show(Request $request, Response $response, $proyecto_id): Response {
|
|
$proyecto = $this->getMapper(ProyectoMapper::class)->fetchById($proyecto_id);
|
|
$output = [
|
|
'proyecto' => json_decode(json_encode($proyecto), JSON_OBJECT_AS_ARRAY)
|
|
];
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function add(Request $request, Response $response, Factory $factory): Response {
|
|
$post = $request->getParsedBody();
|
|
$output = [
|
|
'input' => $post
|
|
];
|
|
if (in_array('proyectos', $post)) {
|
|
$output['proyectos'] = [];
|
|
foreach ($post['proyectos'] as $input) {
|
|
$proyecto = Proyecto::add($factory, $input);
|
|
$proyecto []= [
|
|
'proyecto' => $proyecto->toArray(),
|
|
'created' => $proyecto->is_new() ? $proyecto->save() : false
|
|
];
|
|
}
|
|
} elseif (in_array('proyecto', $post)) {
|
|
$proyecto = Proyecto::add($factory, $post);
|
|
$output['proyecto'] = $proyecto;
|
|
$output['created'] = $proyecto->is_new() ? $proyecto->save() : false;
|
|
}
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function edit(Request $request, Response $response, Factory $factory, $proyecto_id): Response {
|
|
$post = $request->getParsedBody();
|
|
$input = compact('proyecto_id', 'post');
|
|
$proyecto = $factory->find(Proyecto::class)->one($proyecto_id);
|
|
$output = [
|
|
'input' => $input,
|
|
'proyecto' => $proyecto->toArray()
|
|
];
|
|
$output['edited'] = $proyecto->edit($post);
|
|
$output['changes'] = $proyecto->toArray();
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function delete(Request $request, Response $response, Factory $factory, $proyecto_id): Response {
|
|
$proyecto = $factory->find(Proyecto::class)->one($proyecto_id);
|
|
$output = [
|
|
'input' => $proyecto_id,
|
|
'proyecto' => $proyecto->toArray()
|
|
];
|
|
$output['deleted'] = $proyecto->delete();
|
|
return $this->withJson($response, $output);
|
|
}
|
|
|
|
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);
|
|
$output = ['proyecto' => json_decode(json_encode($proyecto), JSON_OBJECT_AS_ARRAY)];
|
|
$output['proyecto']['ventas'] = json_decode(json_encode($ventas), JSON_OBJECT_AS_ARRAY);
|
|
foreach ($ventas as $i => $venta) {
|
|
$estado = $this->getMapper(EstadoVenta::class)->fetchLastByVenta($venta->id);
|
|
$output['proyecto']['ventas'][$i]['estado'] = $estado;
|
|
}
|
|
return $this->withJson($response, $output);
|
|
}
|
|
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);
|
|
usort($tipos, function($a, $b) {
|
|
$t = strcmp($a->tipo->id, $b->tipo->id);
|
|
if ($t === 0) {
|
|
return strcmp($a->nombre, $b->nombre);
|
|
}
|
|
return $t;
|
|
});
|
|
$output['proyecto']['tipos'] = json_decode(json_encode($tipos), JSON_OBJECT_AS_ARRAY);
|
|
foreach ($tipos as $i => $tipo) {
|
|
$unidades = $this->getMapper(UnidadMapper::class)->fetchByTipo($tipo->id);
|
|
$output['proyecto']['tipos'][$i]['unidades'] = json_decode(json_encode($unidades), JSON_OBJECT_AS_ARRAY);
|
|
foreach ($unidades as $k => $unidad) {
|
|
if ($unidad->subtipo === null) {
|
|
$output['proyecto']['tipos'][$i]['unidades'][$k]['subtipo'] = $tipo->nombre;
|
|
}
|
|
$precio = $this->getMapper(PrecioMapper::class)->fetchLastByUnidad($unidad->id);
|
|
if (!$precio) {
|
|
$output['proyecto']['tipos'][$i]['unidades'][$k]['precio'] = ['valor' => 0, 'formateado' => 0, 'estado' => ['estado' => ['descripcion' => '']]];
|
|
continue;
|
|
}
|
|
$output['proyecto']['tipos'][$i]['unidades'][$k]['precio'] = json_decode(json_encode($precio), JSON_OBJECT_AS_ARRAY);
|
|
$output['proyecto']['tipos'][$i]['unidades'][$k]['precio']['estado'] = json_decode(json_encode($this->getMapper(EstadoMapper::class)->fetchLastByPrecio($precio->id)), JSON_OBJECT_AS_ARRAY);
|
|
$output['proyecto']['tipos'][$i]['unidades'][$k]['precio']['formateado'] = $format->uf($precio->valor);
|
|
}
|
|
}
|
|
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);
|
|
}
|
|
}
|