From eb38236926cbd8510ae48dd166b286b7c0948760 Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Wed, 20 Mar 2024 17:52:34 -0300 Subject: [PATCH] FIX: cartola BCI --- app/src/Model/Movimiento.php | 7 ++- app/src/Service/Cartola/BCI.php | 90 ++++++++++++++++++++------------- 2 files changed, 60 insertions(+), 37 deletions(-) diff --git a/app/src/Model/Movimiento.php b/app/src/Model/Movimiento.php index e89eb39..1fcc911 100644 --- a/app/src/Model/Movimiento.php +++ b/app/src/Model/Movimiento.php @@ -3,6 +3,7 @@ namespace Incoviba\Model; use DateTimeInterface; use Incoviba\Common\Ideal; +use Incoviba\Common\Implement\Exception\EmptyResult; use Incoviba\Model\Movimiento\Detalle; class Movimiento extends Ideal\Model @@ -19,7 +20,11 @@ class Movimiento extends Ideal\Model public function getDetalles(): ?Detalle { if (!isset($this->detalles)) { - $this->detalles = $this->runFactory('detalles'); + try { + $this->detalles = $this->runFactory('detalles'); + } catch (EmptyResult) { + $this->detalles = null; + } } return $this->detalles; } diff --git a/app/src/Service/Cartola/BCI.php b/app/src/Service/Cartola/BCI.php index d328c61..08be716 100644 --- a/app/src/Service/Cartola/BCI.php +++ b/app/src/Service/Cartola/BCI.php @@ -1,9 +1,8 @@ 'fecha', - 'Cargo ($)' => 'cargo', - 'Abono ($)' => 'abono', - 'Descripcin' => 'descripcion', - 'Saldo Diario' => 'saldo' + 'Fecha Transacción' => 'fecha', + 'Cargo $ (-)' => 'cargo', + 'Abono $ (+)' => 'abono', + 'Descripción' => 'descripcion', + 'Saldo' => 'saldo' ]; } protected function getFilename(UploadedFileInterface $uploadedFile): string { - return '/tmp/cartola.html'; + return '/tmp/cartola.xlsx'; } protected function parseFile(string $filename): array { + $xlsx = @PhpSpreadsheet\IOFactory::load($filename); + $worksheet = $xlsx->getActiveSheet(); + $rows = $worksheet->getRowIterator(); - - $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); + $rows->seek(3); + $row = $rows->current(); + $columnIterator = $row->getColumnIterator(); + $saldoFinal = 0; + foreach ($columnIterator as $column) { + if ($column->getValue() === null) { continue; } - if (!$found) { - continue; - } - $rowData = $this->getRowContent($row); - if (str_starts_with($rowData[0], 'Saldo Contable Final ($)')) { + if ($column->getCalculatedValue() === 'Saldo Contable') { + $columnIterator->next(); + $column = $columnIterator->current(); + $saldoFinal = (int) str_replace('.', '', $column->getCalculatedValue()); break; } + } + $saldo = $saldoFinal; + + $data = []; + $columns = []; + $dataFound = false; + foreach ($rows as $row) { + if (!$dataFound and $worksheet->getCell([1, $row->getRowIndex()])->getCalculatedValue() !== null + and trim($worksheet->getCell([1, $row->getRowIndex()])->getCalculatedValue()) === 'Fecha Contable') { + $dataFound = true; + $columns = $this->getRowData($row); + continue; + } + if (!$dataFound) { + continue; + } + if ($worksheet->getCell([1, $row->getRowIndex()])->getValue() === null) { + break; + } + $rowData = $this->getRowData($row); $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']); + $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; + unset($rowData['']); + $data []= $rowData; } + return $data; } - protected function getRowContent(DOMElement $row): array + protected function getRowData(PhpSpreadsheet\Worksheet\Row $row): array { - $content = []; - $tdsIterator = $row->getElementsByTagName('td')->getIterator(); - foreach ($tdsIterator as $cell) { - $content []= $cell->textContent; + $data = []; + $cells = $row->getColumnIterator(); + foreach ($cells as $cell) { + $data []= $cell->getCalculatedValue(); } - return $content; + return $data; } }