Estado cuenta

This commit is contained in:
Juan Pablo Vial
2024-03-21 23:10:18 -03:00
parent 529f9e32a1
commit 4b3397dd63
5 changed files with 173 additions and 9 deletions

View File

@ -0,0 +1,62 @@
<?php
namespace Incoviba\Repository\Inmobiliaria\Cuenta;
use Incoviba\Common\Define;
use Incoviba\Common\Ideal;
use Incoviba\Common\Implement;
use Incoviba\Repository;
use Incoviba\Model;
class Estado extends Ideal\Repository
{
public function __construct(Define\Connection $connection, protected Repository\Inmobiliaria\Cuenta $cuentaRepository)
{
parent::__construct($connection);
$this->setTable('estados_cuentas');
}
public function create(?array $data = null): Model\Inmobiliaria\Cuenta\Estado
{
$map = (new Implement\Repository\MapperParser())
->register('cuenta_id', (new Implement\Repository\Mapper())
->setProperty('cuenta')
->setFunction(function($data) {
return $this->cuentaRepository->fetchById($data['cuenta_id']);
}))
->register('fecha', new Implement\Repository\Mapper\DateTime('fecha'))
->register('active', new Implement\Repository\Mapper\Boolean('active'));
return $this->parseData(new Model\Inmobiliaria\Cuenta\Estado(), $data, $map);
}
public function save(Define\Model $model): Model\Inmobiliaria\Cuenta\Estado
{
$model->id = $this->saveNew([
'cuenta_id', 'fecha', 'active'
], [
$model->cuenta->id, $model->fecha->format('Y-m-d'), $model->active ? 1 : 0
]);
return $model;
}
public function edit(Define\Model $model, array $new_data): Model\Inmobiliaria\Cuenta\Estado
{
return $this->update($model, ['cuenta_id', 'fecha', 'active'], $new_data);
}
public function fetchByCuenta(int $cuenta_id): array
{
$query = $this->connection->getQueryBuilder()
->select()
->from($this->getTable())
->where('cuenta_id = ?');
return $this->fetchMany($query, [$cuenta_id]);
}
public function fetchLastByCuenta(int $cuenta_id): Model\Inmobiliaria\Cuenta\Estado
{
$query = $this->connection->getQueryBuilder()
->select()
->from($this->getTable())
->where('cuenta_id = ?')
->order('id DESC')
->limit(1);
return $this->fetchMany($query, [$cuenta_id]);
}
}