Estado cuenta
This commit is contained in:
@ -2,6 +2,7 @@
|
||||
namespace Incoviba\Model\Inmobiliaria;
|
||||
|
||||
use Incoviba\Common\Ideal;
|
||||
use Incoviba\Common\Implement\Exception\EmptyResult;
|
||||
use Incoviba\Model;
|
||||
|
||||
class Cuenta extends Ideal\Model
|
||||
@ -10,6 +11,30 @@ class Cuenta extends Ideal\Model
|
||||
public Model\Banco $banco;
|
||||
public string $cuenta;
|
||||
|
||||
protected array $estados;
|
||||
public function getEstados(): array
|
||||
{
|
||||
if (!isset($this->estados)) {
|
||||
$this->estados = $this->runFactory('estados');
|
||||
}
|
||||
return $this->estados;
|
||||
}
|
||||
protected Model\Inmobiliaria\Cuenta\Estado $currentEstado;
|
||||
public function currentEstado(): Model\Inmobiliaria\Cuenta\Estado
|
||||
{
|
||||
if (!isset($this->currentEstado)) {
|
||||
$estados = $this->getEstados();
|
||||
if (count($estados) === 0) {
|
||||
throw new EmptyResult('Current Estado Cuenta');
|
||||
}
|
||||
usort($estados, function(Model\Inmobiliaria\Cuenta\Estado $a, Model\Inmobiliaria\Cuenta\Estado $b) {
|
||||
return $a->id - $b->id;
|
||||
});
|
||||
$this->currentEstado = end($estados);
|
||||
}
|
||||
return $this->currentEstado;
|
||||
}
|
||||
|
||||
public function jsonSerialize(): mixed
|
||||
{
|
||||
return array_merge(parent::jsonSerialize(), [
|
||||
|
22
app/src/Model/Inmobiliaria/Cuenta/Estado.php
Normal file
22
app/src/Model/Inmobiliaria/Cuenta/Estado.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
namespace Incoviba\Model\Inmobiliaria\Cuenta;
|
||||
|
||||
use DateTimeInterface;
|
||||
use Incoviba\Common\Ideal;
|
||||
use Incoviba\Model;
|
||||
|
||||
class Estado extends Ideal\Model
|
||||
{
|
||||
public Model\Inmobiliaria\Cuenta $cuenta;
|
||||
public DateTimeInterface $fecha;
|
||||
public bool $active;
|
||||
|
||||
public function jsonSerialize(): mixed
|
||||
{
|
||||
return array_merge(parent::jsonSerialize(), [
|
||||
'cuenta_id' => $this->cuenta->id,
|
||||
'fecha' => $this->fecha->format('Y-m-d'),
|
||||
'active' => $this->active
|
||||
]);
|
||||
}
|
||||
}
|
62
app/src/Repository/Inmobiliaria/Cuenta/Estado.php
Normal file
62
app/src/Repository/Inmobiliaria/Cuenta/Estado.php
Normal 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]);
|
||||
}
|
||||
}
|
@ -4,7 +4,6 @@ namespace Incoviba\Service\Contabilidad\Informe;
|
||||
use DateTimeInterface;
|
||||
use DateInterval;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use PhpOffice\PhpSpreadsheet;
|
||||
use Incoviba\Common\Ideal;
|
||||
use Incoviba\Common\Implement;
|
||||
use Incoviba\Model;
|
||||
@ -15,7 +14,7 @@ class Tesoreria extends Ideal\Service
|
||||
{
|
||||
public function __construct(LoggerInterface $logger,
|
||||
protected Repository\Inmobiliaria $inmobiliariaRepository,
|
||||
protected Repository\Inmobiliaria\Cuenta $cuentaRepository,
|
||||
protected Service\Inmobiliaria\Cuenta $cuentaService,
|
||||
protected Repository\Deposito $depositoRepository,
|
||||
protected Repository\Cartola $cartolaRepository,
|
||||
protected Repository\Movimiento $movimientoRepository,
|
||||
@ -170,7 +169,7 @@ class Tesoreria extends Ideal\Service
|
||||
};
|
||||
$dataInmobiliaria->inmobiliaria = $inmobiliaria;
|
||||
try {
|
||||
$cuentas = $this->cuentaRepository->fetchByInmobiliaria($inmobiliaria->rut);
|
||||
$cuentas = $this->cuentaService->getAllActiveByInmobiliaria($inmobiliaria->rut);
|
||||
} catch (Implement\Exception\EmptyResult) {
|
||||
return $dataInmobiliaria;
|
||||
}
|
||||
|
56
app/src/Service/Inmobiliaria/Cuenta.php
Normal file
56
app/src/Service/Inmobiliaria/Cuenta.php
Normal file
@ -0,0 +1,56 @@
|
||||
<?php
|
||||
namespace Incoviba\Service\Inmobiliaria;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Incoviba\Common\Ideal\Service;
|
||||
use Incoviba\Common\Implement;
|
||||
use Incoviba\Model;
|
||||
use Incoviba\Repository;
|
||||
|
||||
class Cuenta extends Service
|
||||
{
|
||||
public function __construct(LoggerInterface $logger, protected Repository\Inmobiliaria\Cuenta $cuentaRepository,
|
||||
protected Repository\Inmobiliaria\Cuenta\Estado $estadoRepository)
|
||||
{
|
||||
parent::__construct($logger);
|
||||
}
|
||||
|
||||
public function getAllActive(): array
|
||||
{
|
||||
$cuentas = array_map([$this, 'process'], $this->cuentaRepository->fetchAll());
|
||||
return array_values(array_filter($cuentas, function(Model\Inmobiliaria\Cuenta $cuenta) {
|
||||
return $cuenta->currentEstado()->active;
|
||||
}));
|
||||
}
|
||||
public function getAllActiveByInmobiliaria(int $inmobiliaria_rut): array
|
||||
{
|
||||
$cuentas = array_map([$this, 'process'], $this->cuentaRepository->fetchByInmobiliaria($inmobiliaria_rut));
|
||||
return array_values(array_filter($cuentas, function(Model\Inmobiliaria\Cuenta $cuenta) {
|
||||
return $cuenta->currentEstado()->active;
|
||||
}));
|
||||
}
|
||||
public function add(array $data): Model\Inmobiliaria\Cuenta
|
||||
{
|
||||
$cuenta = $this->cuentaRepository->create($data);
|
||||
$cuenta = $this->cuentaRepository->save($cuenta);
|
||||
$dataEstado = [
|
||||
'cuenta_id' => $cuenta->id,
|
||||
'fecha' => new DateTimeImmutable(),
|
||||
'active' => true
|
||||
];
|
||||
$estado = $this->estadoRepository->create($dataEstado);
|
||||
$this->estadoRepository->save($estado);
|
||||
return $this->process($cuenta);
|
||||
}
|
||||
|
||||
protected function process(Model\Inmobiliaria\Cuenta $cuenta): Model\Inmobiliaria\Cuenta
|
||||
{
|
||||
$cuenta->addFactory('estados', (new Implement\Repository\Factory())
|
||||
->setCallable(function($cuenta_id) {
|
||||
return $this->estadoRepository->fetchByCuenta($cuenta_id);
|
||||
})
|
||||
->setArgs(['cuenta_id' => $cuenta->id]));
|
||||
return $cuenta;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user