From ff06e708697eb0756e41aea76b3f0d2cf5666331 Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Mon, 16 Sep 2024 22:13:38 -0300 Subject: [PATCH] OfficeBanking usando isExcel y correccion en procesamiento de valores. --- .../Cartola/Santander/OfficeBanking.php | 68 ++++++++++--------- 1 file changed, 36 insertions(+), 32 deletions(-) diff --git a/app/src/Service/Contabilidad/Cartola/Santander/OfficeBanking.php b/app/src/Service/Contabilidad/Cartola/Santander/OfficeBanking.php index 6e8c92e..3d8ba21 100644 --- a/app/src/Service/Contabilidad/Cartola/Santander/OfficeBanking.php +++ b/app/src/Service/Contabilidad/Cartola/Santander/OfficeBanking.php @@ -4,9 +4,12 @@ namespace Incoviba\Service\Contabilidad\Cartola\Santander; use Psr\Http\Message\UploadedFileInterface; use PhpOffice\PhpSpreadsheet; use Incoviba\Common\Ideal\Cartola\Banco; +use Incoviba\Service\Contabilidad\Cartola\isExcel; class OfficeBanking extends Banco { + use isExcel; + public function is(string $filename): bool { if (!str_ends_with($filename, 'xlsx')) { @@ -14,17 +17,17 @@ class OfficeBanking extends Banco } try { $reader = PhpSpreadsheet\IOFactory::createReader('Xlsx'); - } catch (PhpSpreadsheet\Reader\Exception $exception) { + } catch (PhpSpreadsheet\Reader\Exception) { return false; } $xlsx = $reader->load($filename); $sheet = $xlsx->getActiveSheet(); $subtitle = $sheet->getCell('A1')->getCalculatedValue(); - if ($subtitle === 'Consulta de movimientos de Cuentas Corrientes') { + if ($subtitle !== null and trim($subtitle) === 'Cartolas históricas de Cuentas Corrientes') { return true; } $subtitle = $sheet->getCell('A2')->getCalculatedValue(); - return $subtitle === 'Consulta de movimientos de Cuentas Corrientes'; + return $subtitle !== null and trim($subtitle) === 'Cartolas históricas de Cuentas Corrientes'; } protected function columnMap(): array @@ -47,6 +50,7 @@ class OfficeBanking extends Banco 'Factura Boleta' => 'identificador', 'RUT' => 'rut', 'Nombres' => 'nombres', + 'Depto' => 'identificador' ]; } protected function getFilename(UploadedFileInterface $uploadedFile): string @@ -79,44 +83,44 @@ class OfficeBanking extends Banco $found = false; $columns = []; $data = []; - foreach ($sheet->getRowIterator() as $row) { - if (!$found and $sheet->getCell("A{$row->getRowIndex()}")->getCalculatedValue() === 'MONTO') { + $titleValueMap = [ + 'MONTO' => 'int' + ]; + $rowIterator = $sheet->getRowIterator(); + while ($rowIterator->valid()) { + $row = $rowIterator->current(); + $first = $sheet->getCell("A{$row->getRowIndex()}")->getCalculatedValue(); + if ($first !== null) { + $first = trim($first); + } + if (!$found and $first === 'MONTO') { $found = true; - foreach ($row->getColumnIterator() as $column) { - if ($column->getValue() === null) { - break; - } - $columns[$column->getColumn()] = trim($column->getValue()); - } + $columns = $this->grabTitlesRow($row); + $rowIterator->next(); continue; } if (!$found) { + $rowIterator->next(); continue; } - if ($sheet->getCell("A{$row->getRowIndex()}")->getValue() === null) { + if ($first === 'Resumen comisiones' or $first === null) { break; } - $rowData = []; - foreach ($columns as $columnIndex => $column) { - $value = $sheet->getCell("{$columnIndex}{$row->getRowIndex()}")->getCalculatedValue(); - $mapped = $this->columnMap()[$column] ?? $column; - if ($mapped === 'fecha') { - $value = implode('-', array_reverse(explode('/', $value))); - } - if ($column === 'MONTO' or $column === 'SALDO') { - $value = (int) $value; - } - $rowData[$column] = $value; + $rowData = $this->grabRow($row, $columns, process: true, titleValueMap: $titleValueMap); + $fecha = explode('/', $rowData['FECHA']); + $rowData['FECHA'] = $fecha[2] . '-' . $fecha[1] . '-' . $fecha[0]; + switch ($rowData['CARGO/ABONO']) { + case 'C': + $rowData['abono'] = 0; + $rowData['cargo'] = -$rowData['MONTO']; + break; + default: + $rowData['abono'] = $rowData['MONTO']; + $rowData['cargo'] = 0; + break; } - if ($rowData['CARGO/ABONO'] === 'C') { - $rowData['MONTO'] = -$rowData['MONTO']; - $rowData['cargo'] = $rowData['MONTO']; - $rowData['abono'] = 0; - } else { - $rowData['cargo'] = 0; - $rowData['abono'] = $rowData['MONTO']; - } - $data []= $rowData; + $data[] = $rowData; + $rowIterator->next(); } return $data;