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,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;
}
}