Venta->Listado->Cierres
This commit is contained in:
24
app/src/Controller/Ventas/Cierres.php
Normal file
24
app/src/Controller/Ventas/Cierres.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
namespace Incoviba\Controller\Ventas;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Incoviba\Common\Alias\View;
|
||||
use Incoviba\Service;
|
||||
|
||||
class Cierres
|
||||
{
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, View $view): ResponseInterface
|
||||
{
|
||||
return $view->render($response, 'ventas.cierres.list');
|
||||
}
|
||||
public function proyecto(ServerRequestInterface $request, ResponseInterface $response, Service\Ventas\Cierre $service): ResponseInterface
|
||||
{
|
||||
$body = $request->getBody();
|
||||
$json = json_decode($body->getContents());
|
||||
$proyecto_id = $json->proyecto_id;
|
||||
$cierres = $service->getByProyecto($proyecto_id);
|
||||
$response->getBody()->write(json_encode(['cierres' => $cierres, 'total' => count($cierres)]));
|
||||
return $response->withHeader('Content-Type', 'application/json');
|
||||
}
|
||||
}
|
@ -13,14 +13,22 @@ class Cierre extends Ideal\Model
|
||||
public bool $relacionado;
|
||||
public Propietario $propietario;
|
||||
|
||||
public array $estados = [];
|
||||
public ?EstadoCierre $current = null;
|
||||
|
||||
public array $unidades = [];
|
||||
|
||||
public function jsonSerialize(): mixed
|
||||
{
|
||||
return array_merge(parent::jsonSerialize(), [
|
||||
'proyecto_id' => $this->proyecto->id,
|
||||
'proyecto' => $this->proyecto,
|
||||
'precio' => $this->precio,
|
||||
'date_time' => $this->dateTime->format('Y-m-d H:i:s'),
|
||||
'relacionado' => $this->relacionado,
|
||||
'propietario' => $this->propietario->rut
|
||||
'propietario' => $this->propietario->rut,
|
||||
'estados' => $this->estados,
|
||||
'estado_cierre' => $this->current,
|
||||
'unidades' => $this->unidades
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
21
app/src/Model/Venta/EstadoCierre.php
Normal file
21
app/src/Model/Venta/EstadoCierre.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
namespace Incoviba\Model\Venta;
|
||||
|
||||
use DateTimeInterface;
|
||||
use Incoviba\Common\Ideal;
|
||||
|
||||
class EstadoCierre extends Ideal\Model
|
||||
{
|
||||
public Cierre $cierre;
|
||||
public TipoEstadoCierre $tipoEstadoCierre;
|
||||
public DateTimeInterface $fecha;
|
||||
|
||||
public function jsonSerialize(): mixed
|
||||
{
|
||||
return array_merge(parent::jsonSerialize(), [
|
||||
'cierre_id' => $this->cierre->id,
|
||||
'tipo_estado_cierre' => $this->tipoEstadoCierre,
|
||||
'fecha' => $this->fecha->format('Y-m-d')
|
||||
]);
|
||||
}
|
||||
}
|
16
app/src/Model/Venta/TipoEstadoCierre.php
Normal file
16
app/src/Model/Venta/TipoEstadoCierre.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
namespace Incoviba\Model\Venta;
|
||||
|
||||
use Incoviba\Model;
|
||||
|
||||
class TipoEstadoCierre extends Model\Tipo
|
||||
{
|
||||
public bool $vigente;
|
||||
|
||||
public function jsonSerialize(): mixed
|
||||
{
|
||||
return array_merge(parent::jsonSerialize(), [
|
||||
'vigente' => $this->vigente
|
||||
]);
|
||||
}
|
||||
}
|
@ -71,7 +71,7 @@ FROM `{$this->getTable()}` a
|
||||
FROM `estado_cierre` e1
|
||||
JOIN (SELECT MAX(`id`) AS id, `cierre` FROM `estado_cierre` GROUP BY `cierre`) e0 ON e0.`id` = e1.`id`) ec ON ec.`cierre` = a.`id`
|
||||
JOIN `tipo_estado_cierre` tec ON tec.`id` = ec.`tipo`
|
||||
JOIN `proyecto` ON `proyecto`.`id` = a.`proyecto`
|
||||
JOIN `proyecto` ON `proyecto`.`id` = a.`proyecto` AND tec.`descripcion` NOT IN ('revisado')
|
||||
GROUP BY `proyecto`.`descripcion`, tec.`descripcion`";
|
||||
$results = $this->connection->execute($query)->fetchAll(PDO::FETCH_ASSOC);
|
||||
if ($results === false) {
|
||||
@ -79,4 +79,15 @@ GROUP BY `proyecto`.`descripcion`, tec.`descripcion`";
|
||||
}
|
||||
return $results;
|
||||
}
|
||||
public function fetchByProyecto(int $proyecto_id): array
|
||||
{
|
||||
$query = "SELECT a.*
|
||||
FROM `{$this->getTable()}` a
|
||||
JOIN (SELECT e1.*
|
||||
FROM `estado_cierre` e1
|
||||
JOIN (SELECT MAX(`id`) AS id, `cierre` FROM `estado_cierre` GROUP BY `cierre`) e0 ON e0.`id` = e1.`id`) ec ON ec.`cierre` = a.`id`
|
||||
JOIN `tipo_estado_cierre` tec ON tec.`id` = ec.`tipo`
|
||||
WHERE `proyecto` = ? AND tec.`descripcion` NOT IN ('revisado')";
|
||||
return $this->fetchMany($query, [$proyecto_id]);
|
||||
}
|
||||
}
|
||||
|
66
app/src/Repository/Venta/EstadoCierre.php
Normal file
66
app/src/Repository/Venta/EstadoCierre.php
Normal file
@ -0,0 +1,66 @@
|
||||
<?php
|
||||
namespace Incoviba\Repository\Venta;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use Incoviba\Common\Ideal;
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Model;
|
||||
use Incoviba\Repository;
|
||||
|
||||
class EstadoCierre extends Ideal\Repository
|
||||
{
|
||||
public function __construct(Define\Connection $connection, protected Repository\Venta\Cierre $cierreRepository, protected Repository\Venta\TipoEstadoCierre $tipoEstadoCierreRepository)
|
||||
{
|
||||
parent::__construct($connection);
|
||||
$this->setTable('estado_cierre');
|
||||
}
|
||||
|
||||
public function create(?array $data = null): Define\Model
|
||||
{
|
||||
$map = [
|
||||
'cierre' => [
|
||||
'function' => function($data) {
|
||||
return $this->cierreRepository->fetchById($data['cierre']);
|
||||
}
|
||||
],
|
||||
'tipo' => [
|
||||
'property' => 'tipoEstadoCierre',
|
||||
'function' => function($data) {
|
||||
return $this->tipoEstadoCierreRepository->fetchById($data['tipo']);
|
||||
}
|
||||
],
|
||||
'fecha' => [
|
||||
'function' => function($data) {
|
||||
return new DateTimeImmutable($data['fecha']);
|
||||
}
|
||||
]
|
||||
];
|
||||
return $this->parseData(new Model\Venta\EstadoCierre(), $data, $map);
|
||||
}
|
||||
public function save(Define\Model $model): Define\Model
|
||||
{
|
||||
$model->id = $this->saveNew(
|
||||
['cierre', 'tipo', 'fecha'],
|
||||
[$model->cierre->id, $model->tipoEstadoCierre->id, $model->fecha->format('Y-m-d')]
|
||||
);
|
||||
return $model;
|
||||
}
|
||||
public function edit(Define\Model $model, array $new_data): Define\Model
|
||||
{
|
||||
return $this->update($model, ['cierre', 'tipo', 'fecha'], $new_data);
|
||||
}
|
||||
|
||||
public function fetchByCierre(int $cierre_id): array
|
||||
{
|
||||
$query = "SELECT * FROM `{$this->getTable()}` WHERE `cierre` = ?";
|
||||
return $this->fetchMany($query, [$cierre_id]);
|
||||
}
|
||||
public function fetchCurrentByCierre(int $cierre_id): Define\Model
|
||||
{
|
||||
$query = "SELECT e1.*
|
||||
FROM `{$this->getTable()}` e1
|
||||
JOIN (SELECT MAX(`id`) AS 'id', `cierre` FROM `{$this->getTable()}` GROUP BY `cierre`) e0 ON e0.`id` = e1.`id`
|
||||
WHERE e1.`cierre` = ?";
|
||||
return $this->fetchOne($query, [$cierre_id]);
|
||||
}
|
||||
}
|
40
app/src/Repository/Venta/TipoEstadoCierre.php
Normal file
40
app/src/Repository/Venta/TipoEstadoCierre.php
Normal file
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
namespace Incoviba\Repository\Venta;
|
||||
|
||||
use Incoviba\Common\Ideal;
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Model;
|
||||
|
||||
class TipoEstadoCierre extends Ideal\Repository
|
||||
{
|
||||
public function __construct(Define\Connection $connection)
|
||||
{
|
||||
parent::__construct($connection);
|
||||
$this->setTable('tipo_estado_cierre');
|
||||
}
|
||||
|
||||
public function create(?array $data = null): Define\Model
|
||||
{
|
||||
$map = [
|
||||
'descripcion' => [],
|
||||
'vigente' => [
|
||||
'function' => function($data) {
|
||||
return $data['vigente'] != 0;
|
||||
}
|
||||
]
|
||||
];
|
||||
return $this->parseData(new Model\Venta\TipoEstadoCierre(), $data, $map);
|
||||
}
|
||||
public function save(Define\Model $model): Define\Model
|
||||
{
|
||||
$model->id = $this->saveNew(
|
||||
['descripcion', 'vigente'],
|
||||
[$model->descripcion, $model->vigente ? 1 : 0]
|
||||
);
|
||||
return $model;
|
||||
}
|
||||
public function edit(Define\Model $model, array $new_data): Define\Model
|
||||
{
|
||||
return $this->update($model, ['descripcion', 'vigente'], $new_data);
|
||||
}
|
||||
}
|
@ -30,7 +30,6 @@ class Unidad extends Ideal\Repository
|
||||
];
|
||||
return $this->parseData(new Model\Venta\Unidad(), $data, $map);
|
||||
}
|
||||
|
||||
public function save(Define\Model $model): Define\Model
|
||||
{
|
||||
$model->id = $this->saveNew(
|
||||
@ -39,9 +38,20 @@ class Unidad extends Ideal\Repository
|
||||
);
|
||||
return $model;
|
||||
}
|
||||
|
||||
public function edit(Define\Model $model, array $new_data): Define\Model
|
||||
{
|
||||
return $this->update($model, ['subtipo', 'piso', 'descripcion', 'orientacion', 'pt'], $new_data);
|
||||
}
|
||||
|
||||
public function fetchByCierre(int $cierre_id): array
|
||||
{
|
||||
$query = "SELECT a.*
|
||||
FROM `{$this->getTable()}` a
|
||||
JOIN `unidad_cierre` uc ON uc.`unidad` = a.`id`
|
||||
JOIN `proyecto_tipo_unidad` ptu ON ptu.`id` = a.`pt`
|
||||
JOIN `tipo_unidad` tu ON tu.`id` = ptu.`tipo`
|
||||
WHERE uc.`cierre` = ?
|
||||
ORDER BY tu.`orden`, LPAD(a.`descripcion`, 4, '0')";
|
||||
return $this->fetchMany($query, [$cierre_id]);
|
||||
}
|
||||
}
|
||||
|
23
app/src/Service/Ventas/Cierre.php
Normal file
23
app/src/Service/Ventas/Cierre.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
namespace Incoviba\Service\Ventas;
|
||||
|
||||
use Incoviba\Repository;
|
||||
|
||||
class Cierre
|
||||
{
|
||||
public function __construct(
|
||||
protected Repository\Venta\Cierre $cierreRepository,
|
||||
protected Repository\Venta\EstadoCierre $estadoCierreRepository,
|
||||
protected Repository\Venta\Unidad $unidadRepository) {}
|
||||
|
||||
public function getByProyecto(int $proyecto_id): array
|
||||
{
|
||||
$cierres = $this->cierreRepository->fetchByProyecto($proyecto_id);
|
||||
foreach ($cierres as &$cierre) {
|
||||
$cierre->estados = $this->estadoCierreRepository->fetchByCierre($cierre->id);
|
||||
$cierre->current = $this->estadoCierreRepository->fetchCurrentByCierre($cierre->id);
|
||||
$cierre->unidades = $this->unidadRepository->fetchByCierre($cierre->id);
|
||||
}
|
||||
return $cierres;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user