Ventas->Listado->Ventas
This commit is contained in:
67
app/src/Repository/Venta/EstadoVenta.php
Normal file
67
app/src/Repository/Venta/EstadoVenta.php
Normal file
@ -0,0 +1,67 @@
|
||||
<?php
|
||||
namespace Incoviba\Repository\Venta;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use Incoviba\Common\Ideal;
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Model;
|
||||
use Incoviba\Repository;
|
||||
|
||||
class EstadoVenta extends Ideal\Repository
|
||||
{
|
||||
public function __construct(Define\Connection $connection, protected Repository\Venta $ventaRepository,
|
||||
protected TipoEstadoVenta $tipoEstadoVentaRepository)
|
||||
{
|
||||
parent::__construct($connection);
|
||||
$this->setTable('estado_venta');
|
||||
}
|
||||
|
||||
public function create(?array $data = null): Define\Model
|
||||
{
|
||||
$map = [
|
||||
'venta' => [
|
||||
'function' => function($data) {
|
||||
return $this->ventaRepository->fetchById($data['venta']);
|
||||
}
|
||||
],
|
||||
'estado' => [
|
||||
'property' => 'tipoEstadoVenta',
|
||||
'function' => function($data) {
|
||||
return $this->tipoEstadoVentaRepository->fetchById($data['estado']);
|
||||
}
|
||||
],
|
||||
'fecha' => [
|
||||
'function' => function($data) {
|
||||
return new DateTimeImmutable($data['fecha']);
|
||||
}
|
||||
]
|
||||
];
|
||||
return $this->parseData(new Model\Venta\EstadoVenta(), $data, $map);
|
||||
}
|
||||
public function save(Define\Model $model): Define\Model
|
||||
{
|
||||
$model->id = $this->saveNew(
|
||||
['venta', 'estado', 'fecha'],
|
||||
[$model->venta->id, $model->tipoEstadoVenta->id, $model->fecha->format('Y-m-d')]
|
||||
);
|
||||
return $model;
|
||||
}
|
||||
public function edit(Define\Model $model, array $new_data): Define\Model
|
||||
{
|
||||
return $this->update($model, ['venta', 'estado', 'fecha'], $new_data);
|
||||
}
|
||||
|
||||
public function fetchByVenta(int $venta_id): array
|
||||
{
|
||||
$query = "SELECT * FROM `{$this->getTable()}` WHERE `venta` = ?";
|
||||
return $this->fetchMany($query, [$venta_id]);
|
||||
}
|
||||
public function fetchCurrentByVenta(int $venta_id): Define\Model
|
||||
{
|
||||
$query = "SELECT a.*
|
||||
FROM `{$this->getTable()}` a
|
||||
JOIN (SELECT MAX(`id`) AS 'id', `venta` FROM `{$this->getTable()}` GROUP BY `venta`) e0 ON e0.`id` = a.`id`
|
||||
WHERE a.`venta` = ?";
|
||||
return $this->fetchOne($query, [$venta_id]);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user