68 lines
2.2 KiB
PHP
68 lines
2.2 KiB
PHP
![]() |
<?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]);
|
||
|
}
|
||
|
}
|