Auth, Login, Home, Venta->Listados->Precios
This commit is contained in:
82
app/src/Repository/Venta/Cierre.php
Normal file
82
app/src/Repository/Venta/Cierre.php
Normal file
@ -0,0 +1,82 @@
|
||||
<?php
|
||||
namespace Incoviba\Repository\Venta;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Common\Ideal;
|
||||
use Incoviba\Common\Implement\Exception\EmptyResult;
|
||||
use Incoviba\Model;
|
||||
use Incoviba\Repository;
|
||||
use PDO;
|
||||
|
||||
class Cierre extends Ideal\Repository
|
||||
{
|
||||
public function __construct(Define\Connection $connection,
|
||||
protected Repository\Proyecto $proyectoRepository,
|
||||
protected Repository\Venta\Propietario $propietarioRepository)
|
||||
{
|
||||
parent::__construct($connection);
|
||||
$this->setTable('cierre');
|
||||
}
|
||||
|
||||
public function create(?array $data = null): Define\Model
|
||||
{
|
||||
$map = [
|
||||
'proyecto' => [
|
||||
'function' => function($data) {
|
||||
return $this->proyectoRepository->fetchById($data['proyecto']);
|
||||
}
|
||||
],
|
||||
'precio' => [],
|
||||
'fecha' => [
|
||||
'property' => 'dateTime',
|
||||
'function' => function($data) {
|
||||
return new DateTimeImmutable($data['fecha']);
|
||||
}
|
||||
],
|
||||
'relacionado' => [
|
||||
'function' => function($data) {
|
||||
return $data['relacionado'] !== 0;
|
||||
}
|
||||
],
|
||||
'propietario' => [
|
||||
'function' => function($data) {
|
||||
return $this->propietarioRepository->fetchById($data['propietario']);
|
||||
}
|
||||
]
|
||||
];
|
||||
return $this->parseData(new Model\Venta\Cierre(), $data, $map);
|
||||
}
|
||||
|
||||
public function save(Define\Model $model): Define\Model
|
||||
{
|
||||
$model->id = $this->saveNew(
|
||||
['proyecto', 'precio', 'fecha', 'relacionado', 'propietario'],
|
||||
[$model->proyecto->id, $model->precio, $model->fecha->format('Y-m-d H:i:s'), $model->relacionado ? 1 : 0, $model->propietario->rut]
|
||||
);
|
||||
return $model;
|
||||
}
|
||||
|
||||
public function edit(Define\Model $model, array $new_data): Define\Model
|
||||
{
|
||||
return $this->update($model, ['proyecto', 'precio', 'fecha', 'relacionado', 'propietario'], $new_data);
|
||||
}
|
||||
|
||||
public function fetchDatosVigentes(): array
|
||||
{
|
||||
$query = "
|
||||
SELECT `proyecto`.`descripcion` AS 'Proyecto', tec.`descripcion` AS 'Estado', COUNT(a.`id`) AS 'Cantidad'
|
||||
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`
|
||||
JOIN `proyecto` ON `proyecto`.`id` = a.`proyecto`
|
||||
GROUP BY `proyecto`.`descripcion`, tec.`descripcion`";
|
||||
$results = $this->connection->execute($query)->fetchAll(PDO::FETCH_ASSOC);
|
||||
if ($results === false) {
|
||||
throw new EmptyResult($query);
|
||||
}
|
||||
return $results;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user