From 019974614c932f5e933284735d6912b05ec2bfb5 Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Thu, 21 Mar 2024 21:18:40 -0300 Subject: [PATCH] FIX: Saldos cartola diaria --- app/composer.json | 2 ++ app/src/Service/Cartola.php | 12 ++++++++---- app/src/Service/Cartola/BCI.php | 4 ++-- app/src/Service/Cartola/Santander.php | 21 ++++++++++++++------- 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/app/composer.json b/app/composer.json index 65db656..63194b6 100644 --- a/app/composer.json +++ b/app/composer.json @@ -4,6 +4,8 @@ "type": "project", "require": { "berrnd/slim-blade-view": "^1.0", + "ext-openssl": "*", + "ext-pdo": "*", "guzzlehttp/guzzle": "^7.8", "monolog/monolog": "^3.4", "nyholm/psr7": "^1.8", diff --git a/app/src/Service/Cartola.php b/app/src/Service/Cartola.php index 3432743..556d8c5 100644 --- a/app/src/Service/Cartola.php +++ b/app/src/Service/Cartola.php @@ -50,20 +50,24 @@ class Cartola extends Service 'saldo' => 0 ]; $movimientos = []; + $ultimo = null; foreach ($ms as $m) { $movimiento = $this->buildMovimiento($cuenta, $m); $movimiento = $this->movimientoService->process($movimiento); + if ($ultimo === null) { + $ultimo = $movimiento; + } + if ($ultimo->fecha < $movimiento->fecha and $movimiento->fecha <= $fecha) { + $ultimo = $movimiento; + } if ($movimiento->fecha->getTimestamp() === $fecha->getTimestamp()) { $movimientos []= $movimiento; $cartolaData['cargos'] += $movimiento->cargo; $cartolaData['abonos'] += $movimiento->abono; } - if ($movimiento->fecha->getTimestamp() > $fecha->getTimestamp()) { - continue; - } - $cartolaData['saldo'] = $movimiento->saldo; } + $cartolaData['saldo'] = $ultimo->saldo; $cartola = $this->buildCartola($cuenta, $fecha, $cartolaData); return compact('cartola', 'movimientos'); } diff --git a/app/src/Service/Cartola/BCI.php b/app/src/Service/Cartola/BCI.php index 08be716..a211c81 100644 --- a/app/src/Service/Cartola/BCI.php +++ b/app/src/Service/Cartola/BCI.php @@ -13,7 +13,7 @@ class BCI extends Banco 'Fecha Transacción' => 'fecha', 'Cargo $ (-)' => 'cargo', 'Abono $ (+)' => 'abono', - 'Descripción' => 'descripcion', + 'Descripción' => 'glosa', 'Saldo' => 'saldo' ]; } @@ -65,8 +65,8 @@ class BCI extends Banco $rowData['Fecha Transacción'] = implode('-', array_reverse(explode('/', $rowData['Fecha Transacción']))); $rowData['Cargo $ (-)'] = (int) str_replace('.', '', $rowData['Cargo $ (-)'] ?? 0); $rowData['Abono $ (+)'] = (int) str_replace('.', '', $rowData['Abono $ (+)'] ?? 0); - $saldo = $saldo + $rowData['Cargo $ (-)'] - $rowData['Abono $ (+)']; $rowData['Saldo'] = $saldo; + $saldo = $saldo + $rowData['Cargo $ (-)'] - $rowData['Abono $ (+)']; unset($rowData['']); $data []= $rowData; diff --git a/app/src/Service/Cartola/Santander.php b/app/src/Service/Cartola/Santander.php index 4c4248e..b37a999 100644 --- a/app/src/Service/Cartola/Santander.php +++ b/app/src/Service/Cartola/Santander.php @@ -26,17 +26,24 @@ class Santander extends Banco } protected function getFilename(UploadedFileInterface $uploadedFile): string { + $start = $uploadedFile->getStream()->read(10); + if (str_starts_with($start, '<')) { + return '/tmp/cartola.html'; + } return '/tmp/cartola.xlsx'; } protected function parseFile(string $filename): array { - $reader = PhpSpreadsheet\IOFactory::createReader('Xlsx'); - try { - $xlsx = $reader->load($filename); - } catch (PhpSpreadsheet\Reader\Exception) { - return $this->parseHtml($filename); + if (str_ends_with($filename, 'xlsx')) { + return $this->parseXlsx($filename); } + return $this->parseHtml($filename); + } + protected function parseXlsx(string $filename): array + { + $reader = PhpSpreadsheet\IOFactory::createReader('Xlsx'); + $xlsx = $reader->load($filename); $sheet = $xlsx->getActiveSheet(); $found = false; @@ -66,7 +73,7 @@ class Santander extends Banco if ($mapped === 'fecha') { $value = implode('-', array_reverse(explode('/', $value))); } - if ($column === 'MONTO') { + if ($column === 'MONTO' or $column === 'SALDO') { $value = (int) $value; } $rowData[$column] = $value; @@ -109,10 +116,10 @@ class Santander extends Banco foreach (['Cargo ($)', 'Abono ($)', 'Saldo Diario'] as $column) { $row[$column] = (int) str_replace('.', '', $row[$column]); } - $row['Saldo Diario'] -= ($row['Abono ($)'] - $row['Cargo ($)']); $row['N° DOCUMENTO'] = ''; $data []= $row; } + return $data; } protected function parseHtmlRow(array $lines, int &$rowIndex): array