Files
oficial/app/src/Repository/Cartola.php
2024-03-20 20:22:54 -03:00

79 lines
2.7 KiB
PHP

<?php
namespace Incoviba\Repository;
use DateTimeInterface;
use Incoviba\Common\Define;
use Incoviba\Common\Ideal;
use Incoviba\Common\Implement;
use Incoviba\Model;
use Incoviba\Repository;
class Cartola extends Ideal\Repository
{
public function __construct(Define\Connection $connection, protected Repository\Inmobiliaria\Cuenta $cuentaRepository)
{
parent::__construct($connection);
$this->setTable('cartolas');
}
public function create(?array $data = null): Model\Cartola
{
$map = (new Implement\Repository\MapperParser(['cargos', 'abonos', 'saldo']))
->register('fecha', new Implement\Repository\Mapper\DateTime('fecha'))
->register('cuenta_id', (new Implement\Repository\Mapper())
->setProperty('cuenta')
->setFunction(function($data) {
return $this->cuentaRepository->fetchById($data['cuenta_id']);
}));
return $this->parseData(new Model\Cartola(), $data, $map);
}
public function save(Define\Model $model): Model\Cartola
{
$model->id = $this->saveNew([
'cuenta_id',
'fecha',
'cargos',
'abonos',
'saldo'
], [
$model->cuenta->id,
$model->fecha->format('Y-m-d'),
$model->cargos,
$model->abonos,
$model->saldo
]);
return $model;
}
public function edit(Define\Model $model, array $new_data): Model\Cartola
{
return $this->update($model, ['cuenta_id', 'fecha', 'cargos', 'abonos', 'saldo'], $new_data);
}
public function fetchByFecha(DateTimeInterface $fecha): array
{
$query = $this->connection->getQueryBuilder()
->select()
->from($this->getTable())
->where('fecha = ?');
return $this->fetchMany($query, [$fecha->format('Y-m-d')]);
}
public function fetchByCuentaAndFecha(int $cuenta_id, DateTimeInterface $fecha): Model\Cartola
{
$query = $this->connection->getQueryBuilder()
->select()
->from($this->getTable())
->where('cuenta_id = ? AND fecha = ?');
return $this->fetchOne($query, [$cuenta_id, $fecha->format('Y-m-d')]);
}
public function fetchLastByCuentaAndFecha(int $cuenta_id, DateTimeInterface $fecha): Model\Cartola
{
$query = $this->connection->getQueryBuilder()
->select()
->from($this->getTable())
->where('cuenta_id = ? AND fecha <= ?')
->order('fecha DESC')
->limit(1);
return $this->fetchOne($query, [$cuenta_id, $fecha->format('Y-m-d')]);
}
}