0.1.0
This commit is contained in:
@ -1,93 +1,141 @@
|
||||
<?php
|
||||
namespace Incoviba\API\Common\Controller;
|
||||
|
||||
use Incoviba\API\Common\Alias\Controller;
|
||||
use Incoviba\Mapper\EstadoVenta;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Incoviba\API\Common\Define\Controller\Json;
|
||||
use Incoviba\API\Common\Factory\Model as Factory;
|
||||
use Incoviba\Proyecto\Proyecto;
|
||||
use Incoviba\Mapper\Proyecto as ProyectoMapper;
|
||||
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 {
|
||||
use Json;
|
||||
|
||||
public function __invoke(Request $request, Response $response, Factory $factory): Response {
|
||||
$proyectos = $factory->find(Proyecto::class)->array();
|
||||
$base_url = str_replace('/proyectos', '{URL}', $request->getUri());
|
||||
array_walk($proyectos, function (&$item) use ($base_url) {
|
||||
$link = [
|
||||
'rel' => 'proyecto',
|
||||
'title' => $item['descripcion'],
|
||||
'href' => str_replace('{URL}', "/proyecto/{$item['id']}", $base_url)
|
||||
];
|
||||
$item['link'] = $link;
|
||||
});
|
||||
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);
|
||||
$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())
|
||||
]
|
||||
];
|
||||
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;
|
||||
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, Factory $factory, $proyecto_id): Response {
|
||||
$proyecto = $factory->find(Proyecto::class)->one($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())
|
||||
]
|
||||
];
|
||||
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, $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, $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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user