Files
oficial/app/src/Repository/Venta/Comentario.php
2024-07-04 18:09:25 -04:00

70 lines
2.5 KiB
PHP

<?php
namespace Incoviba\Repository\Venta;
use DateTimeImmutable;
use Incoviba\Common\Ideal;
use Incoviba\Common\Define;
use Incoviba\Common\Implement;
use Incoviba\Model;
use Incoviba\Repository;
class Comentario extends Ideal\Repository
{
public function __construct(Define\Connection $connection, protected Repository\Venta $ventaRepsitory)
{
parent::__construct($connection);
$this->setTable('comentario');
}
public function create(?array $data = null): Define\Model
{
$map = (new Implement\Repository\MapperParser(['texto']))
->register('venta', (new Implement\Repository\Mapper())
->setFunction(function($data) {
return $this->ventaRepsitory->fetchById($data['venta']);
}))
->register('fecha', new Implement\Repository\Mapper\DateTime('fecha'))
->register('estado', new Implement\Repository\Mapper\Boolean('estado', 'activo'));
return $this->parseData(new Model\Venta\Comentario(), $data, $map);
}
public function save(Define\Model $model): Define\Model
{
$model->id = $this->saveNew(
['venta', 'fecha', 'texto', 'estado'],
[$model->venta->id, $model->fecha->format('Y-m-d'), $model->texto, $model->activo ? 1 : 0]
);
return $model;
}
public function edit(Define\Model $model, array $new_data): Define\Model
{
return $this->update($model, ['venta', 'fecha', 'texto', 'estado'], $new_data);
}
public function fetchActiveByVenta(int $venta_id): array
{
$query = $this->connection->getQueryBuilder()
->select()
->from($this->getTable())
->where('venta = ? AND estado = 1')
->order('fecha DESC');
return $this->fetchMany($query, [$venta_id]);
}
public function fetchByVenta(int $venta_id): array
{
$query = $this->connection->getQueryBuilder()
->select()
->from($this->getTable())
->where('venta = ?')
->order('fecha DESC');
return $this->fetchMany($query, [$venta_id]);
}
public function fetchByVentaAndFechaAndTexto(int $venta_id, DateTimeImmutable $fecha, string $texto): Model\Venta\Comentario
{
$query = $this->connection->getQueryBuilder()
->select()
->from($this->getTable())
->where('venta = ? AND fecha = ? AND texto = ?');
return $this->fetchOne($query, [$venta_id, $fecha->format('Y-m-d'), $texto]);
}
}