65 lines
2.2 KiB
PHP
65 lines
2.2 KiB
PHP
![]() |
<?php
|
||
|
namespace Incoviba\Repository;
|
||
|
|
||
|
use DateTimeInterface;
|
||
|
use Incoviba\Common\Define;
|
||
|
use Incoviba\Common\Ideal;
|
||
|
use Incoviba\Common\Implement;
|
||
|
use Incoviba\Model;
|
||
|
|
||
|
class Movimiento extends Ideal\Repository
|
||
|
{
|
||
|
public function __construct(Define\Connection $connection, protected Inmobiliaria\Cuenta $cuentaRepository)
|
||
|
{
|
||
|
parent::__construct($connection);
|
||
|
$this->setTable('movimientos');
|
||
|
}
|
||
|
|
||
|
public function create(?array $data = null): Model\Movimiento
|
||
|
{
|
||
|
$map = (new Implement\Repository\MapperParser(['cargo', 'abono', 'saldo', 'glosa', 'documento']))
|
||
|
->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\Movimiento(), $data, $map);
|
||
|
}
|
||
|
public function save(Define\Model $model): Model\Movimiento
|
||
|
{
|
||
|
$model->id = $this->saveNew([
|
||
|
'cuenta_id',
|
||
|
'fecha',
|
||
|
'glosa',
|
||
|
'documento',
|
||
|
'cargo',
|
||
|
'abono',
|
||
|
'saldo'
|
||
|
], [
|
||
|
$model->cuenta->id,
|
||
|
$model->fecha->format('Y-m-d'),
|
||
|
$model->glosa,
|
||
|
$model->documento,
|
||
|
$model->cargo,
|
||
|
$model->abono,
|
||
|
$model->saldo
|
||
|
]);
|
||
|
return $model;
|
||
|
}
|
||
|
public function edit(Define\Model $model, array $new_data): Model\Movimiento
|
||
|
{
|
||
|
return $this->update($model, ['cuenta_id', 'fecha', 'glosa', 'documento', 'cargo', 'abono', 'saldo'], $new_data);
|
||
|
}
|
||
|
|
||
|
public function fetchByCuentaAndFechaAndMonto(int $cuenta_id, DateTimeInterface $fecha, int $monto): Model\Movimiento
|
||
|
{
|
||
|
$query = $this->connection->getQueryBuilder()
|
||
|
->select()
|
||
|
->from($this->getTable())
|
||
|
->where('cuenta_id = ? AND fecha = ? AND (cargo = ? OR abono = ?)');
|
||
|
return $this->fetchOne($query, [$cuenta_id, $fecha->format('Y-m-d'), $monto, $monto]);
|
||
|
}
|
||
|
}
|