2021-11-30 18:04:41 -03:00
|
|
|
<?php
|
|
|
|
namespace Incoviba\API\Common\Controller;
|
|
|
|
|
|
|
|
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\Venta\Venta;
|
2022-06-13 21:36:52 -04:00
|
|
|
use Incoviba\Mapper\Venta as VentaMapper;
|
2021-11-30 18:04:41 -03:00
|
|
|
|
|
|
|
class Ventas {
|
2022-06-13 21:36:52 -04:00
|
|
|
use Json;
|
2021-11-30 18:04:41 -03:00
|
|
|
|
2022-06-13 21:36:52 -04:00
|
|
|
public function __invoke(Request $request, Response $response, Factory $factory, VentaMapper $mapper): Response {
|
|
|
|
$ventas = $mapper->fetchAll();
|
|
|
|
error_log(var_export($ventas, true));
|
|
|
|
$ventas = $factory->find(Venta::class)->array();
|
|
|
|
$url = '' . $request->getUri();
|
|
|
|
array_walk($ventas, function (&$item) use ($url) {
|
|
|
|
$item['link'] = [
|
|
|
|
'rel' => 'venta',
|
|
|
|
'title' => 'Venta',
|
|
|
|
'href' => str_replace('/ventas', "/venta/{$item['id']}", $url)
|
|
|
|
];
|
|
|
|
});
|
|
|
|
return $this->withJson($response, compact('ventas'));
|
2021-11-30 22:33:41 -03:00
|
|
|
}
|
2022-06-13 21:36:52 -04:00
|
|
|
public function show(Request $request, Response $response, Factory $factory, $venta_id): Response {
|
|
|
|
$venta = $factory->find(Venta::class)->one($venta_id);
|
|
|
|
$output = [
|
|
|
|
'input' => $venta_id,
|
|
|
|
'venta' => $venta->toArray(),
|
|
|
|
'link' => [
|
|
|
|
'rel' => 'ventas',
|
|
|
|
'title' => 'Ventas',
|
|
|
|
'href' => str_replace("/venta/{$venta_id}", '/ventas', $request->getUri())
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$output['links'] = [
|
|
|
|
[
|
|
|
|
'rel' => 'propietario',
|
|
|
|
'title' => $venta->propietario()->nombreCompleto(),
|
|
|
|
'href' => str_replace("/venta/{$venta_id}", "/propietario/{$venta->propietario}", $request->getUri())
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'rel' => 'propiedad',
|
|
|
|
'title' => 'Propiedad',
|
|
|
|
'href' => str_replace("/venta/{$venta_id}", "/propiedad/{$venta->propiedad}", $request->getUri())
|
|
|
|
]
|
|
|
|
];
|
|
|
|
if ($venta->pie()) {
|
|
|
|
$output['links'] []= [
|
|
|
|
'rel' => 'pie',
|
|
|
|
'title' => 'Pie',
|
|
|
|
'href' => str_replace("/venta/{$venta_id}", "/pie/{$venta->pie}", $request->getUri())
|
|
|
|
];
|
|
|
|
}
|
|
|
|
if ($venta->bono()) {
|
|
|
|
$output['links'] []= [
|
|
|
|
'rel' => 'bono_pie',
|
|
|
|
'title' => 'Bono Pie',
|
|
|
|
'href' => str_replace("/venta/{$venta_id}", "/bono/{$venta->bono_pie}", $request->getUri())
|
|
|
|
];
|
|
|
|
}
|
|
|
|
if ($venta->credito()) {
|
|
|
|
$output['links'] []= [
|
|
|
|
'rel' => 'credito',
|
|
|
|
'title' => 'Credito',
|
|
|
|
'href' => str_replace("/venta/{$venta_id}", "/credito/{$venta->credito}", $request->getUri())
|
|
|
|
];
|
|
|
|
}
|
|
|
|
if ($venta->escritura()) {
|
|
|
|
$output['links'] []= [
|
|
|
|
'rel' => 'escritura',
|
|
|
|
'title' => 'Escritura',
|
|
|
|
'href' => str_replace("/venta/{$venta_id}", "/escritura/{$venta->escritura}", $request->getUri())
|
|
|
|
];
|
|
|
|
}
|
|
|
|
if ($venta->subsidio()) {
|
|
|
|
$output['links'] []= [
|
|
|
|
'rel' => 'subsidio',
|
|
|
|
'title' => 'Subsidio',
|
|
|
|
'href' => str_replace("/venta/{$venta_id}", "/subsidio/{$venta->subsidio}", $request->getUri())
|
|
|
|
];
|
|
|
|
}
|
|
|
|
if ($venta->entrega()) {
|
|
|
|
$output['links'] []= [
|
|
|
|
'rel' => 'entrega',
|
|
|
|
'title' => 'Entrega',
|
|
|
|
'href' => str_replace("/venta/{$venta_id}", "/entrega/{$venta->entrega}", $request->getUri())
|
|
|
|
];
|
|
|
|
}
|
|
|
|
if ($venta->proyectoAgente()) {
|
|
|
|
$output['links'] []= [
|
|
|
|
'rel' => 'proyecto_agente',
|
|
|
|
'title' => 'Proyecto Agente',
|
|
|
|
'href' => str_replace("/venta/{$venta_id}", "/proyecto_agente/{$venta->agente}", $request->getUri())
|
|
|
|
];
|
|
|
|
}
|
|
|
|
if ($venta->promocion()) {
|
|
|
|
$output['links'] []= [
|
|
|
|
'rel' => 'promocion',
|
|
|
|
'title' => 'Promocion',
|
|
|
|
'href' => str_replace("/venta/{$venta_id}", "/promocion/{$venta->promocion}", $request->getUri())
|
|
|
|
];
|
|
|
|
}
|
|
|
|
if ($venta->resciliacion()) {
|
|
|
|
$output['links'] []= [
|
|
|
|
'rel' => 'resciliacion',
|
|
|
|
'title' => 'Resciliacion',
|
|
|
|
'href' => str_replace("/venta/{$venta_id}", "/resciliacion/{$venta->resciliacion}", $request->getUri())
|
|
|
|
];
|
|
|
|
}
|
|
|
|
return $this->withJson($response, $output);
|
2021-11-30 22:33:41 -03:00
|
|
|
}
|
2022-06-13 21:36:52 -04:00
|
|
|
public function add(Request $request, Response $response, Factory $factory): Response {
|
|
|
|
$post = $request->getParsedBody();
|
|
|
|
$output = [
|
|
|
|
'input' => $post
|
|
|
|
];
|
|
|
|
if (in_array('ventas', $post)) {
|
|
|
|
$output['ventas'] = [];
|
|
|
|
foreach ($post['ventas'] as $input) {
|
|
|
|
$venta = Venta::add($factory, $input);
|
|
|
|
$venta []= [
|
|
|
|
'venta' => $venta->toArray(),
|
|
|
|
'created' => $venta->is_new() ? $venta->save() : false
|
|
|
|
];
|
|
|
|
}
|
|
|
|
} elseif (in_array('venta', $post)) {
|
|
|
|
$venta = Venta::add($factory, $post);
|
|
|
|
$output['venta'] = $venta;
|
|
|
|
$output['created'] = $venta->is_new() ? $venta->save() : false;
|
|
|
|
}
|
|
|
|
return $this->withJson($response, $output);
|
2021-11-30 22:33:41 -03:00
|
|
|
}
|
2022-06-13 21:36:52 -04:00
|
|
|
public function edit(Request $request, Response $response, Factory $factory, $venta_id): Response {
|
|
|
|
$post = $request->getParsedBody();
|
|
|
|
$input = compact('venta_id', 'post');
|
|
|
|
$venta = $factory->find(Venta::class)->one($venta_id);
|
|
|
|
$output = [
|
|
|
|
'input' => $input,
|
|
|
|
'venta' => $venta->toArray()
|
|
|
|
];
|
|
|
|
$output['edited'] = $venta->edit($post);
|
|
|
|
$output['changes'] = $venta->toArray();
|
|
|
|
return $this->withJson($response, $output);
|
2021-11-30 22:33:41 -03:00
|
|
|
}
|
2022-06-13 21:36:52 -04:00
|
|
|
public function delete(Request $request, Response $response, Factory $factory, $venta_id): Response {
|
|
|
|
$venta = $factory->find(Venta::class)->one($venta_id);
|
|
|
|
$output = [
|
|
|
|
'input' => $venta_id,
|
|
|
|
'venta' => $venta->toArray()
|
2021-11-30 18:04:41 -03:00
|
|
|
];
|
2022-06-13 21:36:52 -04:00
|
|
|
$output['deleted'] = $venta->delete();
|
|
|
|
return $this->withJson($response, $output);
|
2021-11-30 18:04:41 -03:00
|
|
|
}
|
|
|
|
}
|