Informe Tesoreria

This commit is contained in:
Juan Pablo Vial
2024-02-13 01:16:17 -03:00
parent 19333bc338
commit e44ab30665
23 changed files with 896 additions and 40 deletions

View File

@ -0,0 +1,277 @@
<?php
namespace Incoviba\Service\Contabilidad\Informe;
use DateTimeInterface;
use DateInterval;
use Psr\Log\LoggerInterface;
use Incoviba\Common\Ideal;
use Incoviba\Common\Implement;
use Incoviba\Repository;
use Incoviba\Model;
class Tesoreria extends Ideal\Service
{
public function __construct(LoggerInterface $logger,
protected Repository\Inmobiliaria $inmobiliariaRepository,
protected Repository\Inmobiliaria\Cuenta $cuentaRepository,
protected Repository\Deposito $depositoRepository,
protected Repository\Cartola $cartolaRepository,
protected Repository\Movimiento $movimientoRepository)
{
parent::__construct($logger);
$this->movimientos = new class() {
public function __construct()
{
$this->dap = new class()
{
public array $ingresos = [];
public array $egresos = [];
};
}
public object $dap;
public array $ingresos = [];
public array $egresos = [];
const INGRESOS = 'ingresos';
const EGRESOS = 'egresos';
public function addDap(string $tipo, array $movimientos)
{
foreach ($movimientos as $movimiento) {
$this->dap->{$tipo} []= $movimiento;
}
return $this;
}
public function build(): array
{
return [
'capital dap' => [
'ingresos' => $this->dap->ingresos,
'egresos' => $this->dap->egresos
],
'ingresos' => $this->ingresos,
'egresos' => $this->egresos
];
}
};
$this->totales = new class() {
public int $anterior = 0;
public int $actual = 0;
public int $ffmm = 0;
public int $deposito = 0;
public function diferencia(): int
{
return $this->actual - $this->anterior;
}
public function saldo(): int
{
return $this->diferencia() + $this->ffmm + $this->deposito;
}
public function cuentas(): int
{
return $this->actual;
}
public function ffmms(): int
{
return $this->ffmm;
}
public function depositos(): int
{
return $this->deposito;
}
public function caja(): int
{
return $this->cuentas() + $this->ffmms() + $this->depositos();
}
};
}
const DAP_INGRESOS = 'dap->ingresos';
const DAP_EGRESOS = 'dap->egresos';
const INGRESOS = 'ingresos';
const EGRESOS = 'egresos';
const TOTAL_ANTERIOR = 'anterior';
const TOTAL_ACTUAL = 'actual';
const TOTAL_FFMM = 'ffmm';
const TOTAL_DAP = 'deposito';
protected DateTimeInterface $anterior;
protected object $totales;
protected object $movimientos;
public function getAnterior(DateTimeInterface $fecha): DateTimeInterface
{
if (!isset($this->anterior)) {
$this->anterior = $fecha->sub(new DateInterval('P1D'));
if ($this->anterior->format('N') === '7') {
$this->anterior = $fecha->sub(new DateInterval('P3D'));
}
}
return $this->anterior;
}
public function build(DateTimeInterface $fecha): array
{
try {
$inmobiliarias = $this->inmobiliariaRepository->fetchAllActive();
} catch (Implement\Exception\EmptyResult) {
return [];
}
$informe = ['inmobiliarias' => []];
foreach ($inmobiliarias as $inmobiliaria) {
$informe['inmobiliarias'][$inmobiliaria->rut] = $this->buildInmobiliaria($inmobiliaria, $fecha);
}
$informe['movimientos'] = $this->buildMovimientos();
$informe['totales'] = $this->buildTotales();
return $informe;
}
protected function buildInmobiliaria(Model\Inmobiliaria $inmobiliaria, DateTimeInterface $fecha): object
{
$dataInmobiliaria = new class() {
public Model\Inmobiliaria $inmobiliaria;
public array $cuentas = [];
public function total(): int
{
return array_reduce($this->cuentas, function(int $sum, $cuenta) {
return $sum + $cuenta->actual;
}, 0);
}
public function ffmm(): int
{
return array_reduce($this->cuentas, function(int $sum, $cuenta) {
return $sum + $cuenta->ffmm;
}, 0);
}
public function deposito(): int
{
return array_reduce($this->cuentas, function(int $sum, $cuenta) {
return $sum + $cuenta->deposito;
}, 0);
}
public function caja(): int
{
return array_reduce($this->cuentas, function(int $sum, $cuenta) {
return $sum + $cuenta->saldo();
}, 0);
}
};
$dataInmobiliaria->inmobiliaria = $inmobiliaria;
try {
$cuentas = $this->cuentaRepository->fetchByInmobiliaria($inmobiliaria->rut);
} catch (Implement\Exception\EmptyResult) {
return $dataInmobiliaria;
}
foreach ($cuentas as $cuenta) {
$data = new class() {
public string $banco;
public string $numero;
public int $anterior = 0;
public int $actual = 0;
public int $ffmm = 0;
public int $deposito = 0;
public function diferencia(): int
{
return $this->actual - $this->anterior;
}
public function saldo(): int
{
return $this->diferencia() + $this->ffmm + $this->deposito;
}
};
$data->banco = $cuenta->banco->nombre;
$data->numero = $cuenta->cuenta;
try {
$depositos = $this->depositoRepository->fetchByCuenta($cuenta->id);
foreach ($depositos as $deposito) {
$data->deposito += $deposito->capital;
$this->addTotal(self::TOTAL_DAP, $deposito->capital);
if ($deposito->inicio === $fecha) {
$this->addMovimientos(self::DAP_EGRESOS, [(object) [
'cuenta' => $deposito->cuenta,
'fecha' => $deposito->inicio,
'cargo' => - $deposito->capital,
'abono' => 0,
'saldo' => - $deposito->capital,
'glosa' => 'INVERSION DAP'
]]);
}
if ($deposito->termino === $fecha) {
$data->deposito -= $deposito->capital;
$this->addTotal(self::TOTAL_DAP, -$deposito->capital);
$this->addMovimientos(self::DAP_INGRESOS, [(object) [
'cuenta' => $deposito->cuenta,
'fecha' => $deposito->termino,
'cargo' => 0,
'abono' => $deposito->futuro,
'saldo' => $deposito->futuro,
'glosa' => 'RESCATE DAP'
]]);
}
}
} catch (Implement\Exception\EmptyResult) {}
try {
$cartola = $this->cartolaRepository->fetchByCuentaAndFecha($cuenta->id, $fecha);
$data->actual = $cartola->saldo;
} catch (Implement\Exception\EmptyResult) {}
try {
$cartola = $this->cartolaRepository->fetchByCuentaAndFecha($cuenta->id, $this->getAnterior($fecha));
$data->anterior = $cartola->saldo;
} catch (Implement\Exception\EmptyResult) {}
if ($data->diferencia() !== 0) {
try {
$movimientos = $this->movimientoRepository->fetchByCuentaAndFecha($cuenta->id, $fecha);
$this->addMovimientos(self::INGRESOS,
array_filter($movimientos, function(Model\Movimiento $movimiento) {
return $movimiento->abono > 0;
}));
$this->addMovimientos(self::EGRESOS,
array_filter($movimientos, function(Model\Movimiento $movimiento) {
return $movimiento->cargo > 0;
}));
} catch (Implement\Exception\EmptyResult) {}
}
$dataInmobiliaria->cuentas []= $data;
$this->addTotal(
[self::TOTAL_ANTERIOR, self::TOTAL_ACTUAL],
[$data->anterior, $data->actual]
);
}
return $dataInmobiliaria;
}
protected function buildMovimientos(): array
{
return $this->movimientos->build();
}
protected function buildTotales(): object
{
return $this->totales;
}
protected function addMovimientos(string $tipo, array $movimientos): Tesoreria
{
if (str_contains($tipo, 'dap')) {
list($d, $t) = explode('->', $tipo);
$this->movimientos->addDap($t, $movimientos);
return $this;
}
foreach ($movimientos as $movimiento) {
$this->movimientos->{$tipo} []= $movimiento;
}
return $this;
}
protected function addTotal(string|array $tipo, int|array $total): Tesoreria
{
if (is_array($tipo)) {
foreach ($tipo as $i => $t) {
$this->addTotal($t, $total[$i]);
}
return $this;
}
$this->totales->{$tipo} += $total;
return $this;
}
}

View File

@ -4,20 +4,25 @@ namespace Incoviba\Service\Contabilidad;
use DateTimeInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Log\LoggerInterface;
use Incoviba\Common\Implement\Exception;
use Incoviba\Repository;
use Incoviba\Service;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use function Symfony\Component\Translation\t;
use Incoviba\Common\Ideal;
class Nubox
class Nubox extends Ideal\Service
{
public function __construct(protected Repository\Nubox $nuboxRepository,
public function __construct(protected LoggerInterface $logger,
protected Repository\Nubox $nuboxRepository,
protected Service\Redis $redisService,
protected ClientInterface $client,
protected RequestFactoryInterface $requestFactory,
protected string $api_url) {}
protected string $api_url)
{
parent::__construct($logger);
}
protected array $tokens;
public function getToken(int $inmobiliaria_rut): string
@ -120,7 +125,7 @@ class Nubox
$uri = 'contabilidad/libro-diario?' . http_build_query($query);
$response = $this->send($uri, $inmobiliaria_rut);
if ($response->getStatusCode() !== 200) {
error_log(var_export($uri,true).PHP_EOL,3,'/logs/debug');
$this->logger->debug($uri);
throw new Exception\HttpResponse($response->getReasonPhrase(), $response->getStatusCode());
}
return json_decode($response->getBody()->getContents(), JSON_OBJECT_AS_ARRAY);