API Movimientos y cartola diaria

This commit is contained in:
Juan Pablo Vial
2024-02-07 23:48:31 -03:00
parent 3cd699d2e2
commit 38962cb9cf
11 changed files with 307 additions and 9 deletions

View File

@ -0,0 +1,66 @@
<?php
namespace Incoviba\Service\Cartola;
use DateTimeInterface;
use DateTimeImmutable;
use Psr\Http\Message\UploadedFileInterface;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Common\Define;
use Incoviba\Model;
use Incoviba\Repository;
class Diaria
{
public function __construct(protected Repository\Inmobiliaria\Cuenta $cuentaRepository,
protected Repository\Movimiento $movimientoRepository,
protected Repository\Cartola $cartolaRepository) {}
protected array $bancos;
public function register(string $name, Define\Cartola\Banco $banco): Diaria
{
$this->bancos[$name] = $banco;
return $this;
}
public function process(Model\Inmobiliaria $inmobiliaria, Model\Banco $banco, DateTimeInterface $fecha, UploadedFileInterface $file): array
{
$cuenta = $this->cuentaRepository->fetchByInmobiliariaAndBanco($inmobiliaria->rut, $banco->id);
$ms = $this->bancos[strtolower($banco->nombre)]->process($file);
$ms = array_reverse($ms);
$c = array_shift($ms);
$cargos = 0;
$abonos = 0;
$saldo = $c['saldo'];
$movimientos = [];
foreach ($ms as $m) {
$m['cuenta_id'] = $cuenta->id;
try {
$movimiento = $this->movimientoRepository->fetchByCuentaAndFechaAndMonto($cuenta->id, new DateTimeImmutable($m['fecha']), $m['cargo'] ?? $m['abono']);
} catch (EmptyResult) {
$movimiento = $this->movimientoRepository->create($m);
$movimiento = $this->movimientoRepository->save($movimiento);
}
$movimientos []= $movimiento;
if ($movimiento->fecha === $fecha) {
$cargos += $movimiento->cargo;
$abonos += $movimiento->abono;
}
$saldo = $m['saldo'];
}
try {
$cartola = $this->cartolaRepository->fetchByCuentaAndFecha($cuenta->id, $fecha);
} catch (EmptyResult) {
$cartola = $this->cartolaRepository->create([
'cuenta_id' => $cuenta->id,
'fecha' => $fecha->format('Y-m-d'),
'cargos' => $cargos,
'abonos' => $abonos,
'saldo' => $saldo
]);
$cartola = $this->cartolaRepository->save($cartola);
}
return compact('cartola', 'movimientos');
}
}

View File

@ -24,8 +24,10 @@ class Security extends Banco
'fecha' => 'fecha',
'descripción' => 'glosa',
'número de documentos' => 'documento',
'nº documento' => 'documento',
'cargos' => 'cargo',
'abonos' => 'abono'
'abonos' => 'abono',
'saldos' => 'saldo'
];
}
@ -33,10 +35,9 @@ class Security extends Banco
{
$filename = '/tmp/cartola.xls';
$file->moveTo($filename);
$reader = PhpSpreadsheet\IOFactory::createReader('Xls');
$xlsx = $reader->load($filename);
$xlsx = @PhpSpreadsheet\IOFactory::load($filename);
$worksheet = $xlsx->getActiveSheet();
$rows = $worksheet->getRowIterator();
$rows = $worksheet->getRowIterator(3);
$dataFound = false;
$columns = [];
$data = [];
@ -44,10 +45,10 @@ class Security extends Banco
$cells = $row->getCellIterator();
$rowData = [];
foreach ($cells as $cell) {
if ($cell->getColumn() === 'A' and $cell->getCalculatedValue() === "fecha ") {
if ($cell->getColumn() === 'A' and $cell->getCalculatedValue() !== null and strtolower($cell->getCalculatedValue()) === "fecha ") {
$cols = $row->getColumnIterator();
foreach ($cols as $col) {
$columns[$col->getColumn()] = trim($col->getCalculatedValue());
$columns[$col->getColumn()] = trim(strtolower($col->getCalculatedValue()), '  ');
}
$dataFound = true;
break;
@ -62,7 +63,11 @@ class Security extends Banco
$col = $columns[$cell->getColumn()];
$value = $cell->getCalculatedValue();
if ($col === 'fecha') {
$value = PhpSpreadsheet\Shared\Date::excelToDateTimeObject($cell->getValue(), 'America/Santiago')->format('Y-m-d');
if ((int) $cell->getValue() !== $cell->getValue()) {
$value = implode('-', array_reverse(explode('-', $cell->getValue())));
} else {
$value = PhpSpreadsheet\Shared\Date::excelToDateTimeObject($cell->getValue(), 'America/Santiago')->format('Y-m-d');
}
}
$rowData[$col] = $value;
}