From dc2e2c7f71b73bdf942d577c9a717a6a82525117 Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Mon, 18 Mar 2024 17:54:27 -0300 Subject: [PATCH] Cartola BCI --- app/common/Define/Cartola/Banco.php | 5 ++ app/common/Ideal/Cartola/Banco.php | 41 +++++++++++++++- app/setup/setups/services.php | 3 +- app/src/Service/Cartola.php | 7 ++- app/src/Service/Cartola/BCI.php | 69 +++++++++++++++++++++++++++ app/src/Service/Cartola/Itau.php | 12 ++--- app/src/Service/Cartola/Santander.php | 9 ++-- app/src/Service/Cartola/Security.php | 24 +++++----- 8 files changed, 143 insertions(+), 27 deletions(-) create mode 100644 app/src/Service/Cartola/BCI.php diff --git a/app/common/Define/Cartola/Banco.php b/app/common/Define/Cartola/Banco.php index 71f1323..029a34e 100644 --- a/app/common/Define/Cartola/Banco.php +++ b/app/common/Define/Cartola/Banco.php @@ -5,6 +5,11 @@ use Psr\Http\Message\UploadedFileInterface; interface Banco { + /** + * Process bank movements for database inserts + * @param UploadedFileInterface $file + * @return array + */ public function process(UploadedFileInterface $file): array; } diff --git a/app/common/Ideal/Cartola/Banco.php b/app/common/Ideal/Cartola/Banco.php index 5237b26..eb47c52 100644 --- a/app/common/Ideal/Cartola/Banco.php +++ b/app/common/Ideal/Cartola/Banco.php @@ -9,7 +9,7 @@ abstract class Banco extends Service implements Define\Cartola\Banco { public function process(UploadedFileInterface $file): array { - $data = $this->parseFile($file); + $data = $this->handleFile($file); $temp = []; $columns = $this->columnMap(); foreach ($data as $row) { @@ -24,11 +24,48 @@ abstract class Banco extends Service implements Define\Cartola\Banco } return $temp; } + + /** + * There are banks that need some post-processing + * @param array $movimientos + * @return array + */ public function processMovimientosDiarios(array $movimientos): array { return $movimientos; } + /** + * Move the UploadedFile into a temp file from getFilename and after parseFile remove temp file + * @param UploadedFileInterface $uploadedFile + * @return array + */ + protected function handleFile(UploadedFileInterface $uploadedFile): array + { + $filename = $this->getFilename($uploadedFile); + $uploadedFile->moveTo($filename); + $data = $this->parseFile($filename); + unlink($filename); + return $data; + } + + /** + * Get filename where to move UploadedFile + * @param UploadedFileInterface $uploadedFile + * @return string + */ + abstract protected function getFilename(UploadedFileInterface $uploadedFile): string; + + /** + * Mapping of uploaded file data columns to database columns + * @return array + */ abstract protected function columnMap(): array; - abstract protected function parseFile(UploadedFileInterface $uploadedFile): array; + + /** + * Translate uploaded file data to database data + * @param string $filename + * @return array + */ + abstract protected function parseFile(string $filename): array; } diff --git a/app/setup/setups/services.php b/app/setup/setups/services.php index 01dd83e..c08cb1c 100644 --- a/app/setup/setups/services.php +++ b/app/setup/setups/services.php @@ -45,7 +45,8 @@ return [ )) ->register('security', $container->get(Incoviba\Service\Cartola\Security::class)) ->register('itau', $container->get(Incoviba\Service\Cartola\Itau::class)) - ->register('santander', $container->get(Incoviba\Service\Cartola\Santander::class)); + ->register('santander', $container->get(Incoviba\Service\Cartola\Santander::class)) + ->register('bci', $container->get(Incoviba\Service\Cartola\BCI::class)); }, Incoviba\Common\Define\Contabilidad\Exporter::class => function(ContainerInterface $container) { return $container->get(Incoviba\Service\Contabilidad\Exporter\Nubox::class); diff --git a/app/src/Service/Cartola.php b/app/src/Service/Cartola.php index 488feb8..3432743 100644 --- a/app/src/Service/Cartola.php +++ b/app/src/Service/Cartola.php @@ -121,7 +121,12 @@ class Cartola extends Service } catch (Exception\EmptyResult $exception) { $data['cuenta_id'] = $cuenta->id; $movimiento = $this->movimientoRepository->create($data); - return $this->movimientoRepository->save($movimiento); + try { + return $this->movimientoRepository->save($movimiento); + } catch (\PDOException $exception) { + $this->logger->critical(var_export($data,true)); + throw $exception; + } } } } diff --git a/app/src/Service/Cartola/BCI.php b/app/src/Service/Cartola/BCI.php new file mode 100644 index 0000000..d328c61 --- /dev/null +++ b/app/src/Service/Cartola/BCI.php @@ -0,0 +1,69 @@ + 'fecha', + 'Cargo ($)' => 'cargo', + 'Abono ($)' => 'abono', + 'Descripcin' => 'descripcion', + 'Saldo Diario' => 'saldo' + ]; + } + protected function getFilename(UploadedFileInterface $uploadedFile): string + { + return '/tmp/cartola.html'; + } + protected function parseFile(string $filename): array + { + + + $domDocument = new DOMDocument(); + $domDocument->loadHTML('' . str_replace('?', '', mb_convert_encoding(file_get_contents($filename), 'windows-1252', 'utf-8')) . ''); + + $tables = $domDocument->getElementsByTagName('table'); + $table = $tables->item(1); + $data = []; + $columns = []; + $found = false; + foreach ($table->getElementsByTagName('tr')->getIterator() as $row) { + if (!$found and str_contains($row->textContent, 'Fecha')) { + $found = true; + $columns = $this->getRowContent($row); + continue; + } + if (!$found) { + continue; + } + $rowData = $this->getRowContent($row); + if (str_starts_with($rowData[0], 'Saldo Contable Final ($)')) { + break; + } + $rowData = array_combine($columns, $rowData); + $rowData['Fecha'] = implode('-', array_reverse(explode('-', $rowData['Fecha']))); + $rowData['Cargo ($)'] = (int) str_replace('.', '', $rowData['Cargo ($)']); + $rowData['Abono ($)'] = (int) str_replace('.', '', $rowData['Abono ($)']); + $rowData['Saldo Diario'] = (int) str_replace('.', '', $rowData['Saldo Diario']); + $data []= $rowData; + } + return $data; + } + + protected function getRowContent(DOMElement $row): array + { + $content = []; + $tdsIterator = $row->getElementsByTagName('td')->getIterator(); + foreach ($tdsIterator as $cell) { + $content []= $cell->textContent; + } + return $content; + } +} diff --git a/app/src/Service/Cartola/Itau.php b/app/src/Service/Cartola/Itau.php index 0e3c486..5fb546a 100644 --- a/app/src/Service/Cartola/Itau.php +++ b/app/src/Service/Cartola/Itau.php @@ -29,12 +29,12 @@ class Itau extends Banco 'Saldos' => 'saldo' ]; } - - protected function parseFile(UploadedFileInterface $uploadedFile): array + protected function getFilename(UploadedFileInterface $uploadedFile): string + { + return '/tmp/cartola.xls'; + } + protected function parseFile(string $filename): array { - $filename = '/tmp/cartola.xls'; - $uploadedFile->moveTo($filename); - $reader = PhpSpreadsheet\IOFactory::createReader('Xls'); $xlsx = $reader->load($filename); $sheet = $xlsx->getActiveSheet(); @@ -51,8 +51,6 @@ class Itau extends Banco } } catch (PhpSpreadsheet\Exception $exception) { $this->logger->critical($exception); - } finally { - unlink($filename); } return $data; } diff --git a/app/src/Service/Cartola/Santander.php b/app/src/Service/Cartola/Santander.php index b6af374..4c4248e 100644 --- a/app/src/Service/Cartola/Santander.php +++ b/app/src/Service/Cartola/Santander.php @@ -10,7 +10,6 @@ class Santander extends Banco { protected function columnMap(): array { - // MONTO DESCRIPCIÓN MOVIMIENTO FECHA N° DOCUMENTO SUCURSAL CARGO/ABONO return [ 'cargo' => 'cargo', 'abono' => 'abono', @@ -25,11 +24,13 @@ class Santander extends Banco 'Saldo Diario' => 'saldo' ]; } - protected function parseFile(UploadedFileInterface $uploadedFile): array + protected function getFilename(UploadedFileInterface $uploadedFile): string { - $filename = '/tmp/cartola.xlsx'; - $uploadedFile->moveTo($filename); + return '/tmp/cartola.xlsx'; + } + protected function parseFile(string $filename): array + { $reader = PhpSpreadsheet\IOFactory::createReader('Xlsx'); try { $xlsx = $reader->load($filename); diff --git a/app/src/Service/Cartola/Security.php b/app/src/Service/Cartola/Security.php index fd10992..7f3d338 100644 --- a/app/src/Service/Cartola/Security.php +++ b/app/src/Service/Cartola/Security.php @@ -16,14 +16,21 @@ class Security extends Banco return $movimientos; } - protected function parseFile(UploadedFileInterface $uploadedFile): array + protected function getFilename(UploadedFileInterface $uploadedFile): string { $stream = $uploadedFile->getStream(); $stream->seek(3); if ($stream->read(strlen('table')) === 'table') { - return $this->processHtm($uploadedFile); + return '/tmp/cartola.htm'; } - return $this->processXls($uploadedFile); + return '/tmp/cartola.xls'; + } + protected function parseFile(string $filename): array + { + if (str_ends_with($filename, '.htm')) { + return $this->processHtm($filename); + } + return $this->processXls($filename); } protected function columnMap(): array { @@ -38,10 +45,8 @@ class Security extends Banco ]; } - private function processXls(UploadedFileInterface $file): array + private function processXls(string $filename): array { - $filename = '/tmp/cartola.xls'; - $file->moveTo($filename); $xlsx = @PhpSpreadsheet\IOFactory::load($filename); $worksheet = $xlsx->getActiveSheet(); $rows = $worksheet->getRowIterator(3); @@ -82,14 +87,10 @@ class Security extends Banco $data []= $rowData; } } - unlink($filename); return $data; } - private function processHtm(UploadedFileInterface $file): array + private function processHtm(string $filename): array { - $filename = '/tmp/cartola.htm'; - $file->moveTo($filename); - $domDocument = new DOMDocument(); $domDocument->loadHTML('' . file_get_contents($filename) . ''); @@ -122,7 +123,6 @@ class Security extends Banco } $data []= $rowData; } - return $data; } }