Procesar otras cartolas Santander

This commit is contained in:
Juan Pablo Vial
2024-02-28 17:02:04 -03:00
parent 21d1ef653f
commit d9b5bc6507

View File

@ -17,7 +17,12 @@ class Santander extends Banco
'DESCRIPCIÓN MOVIMIENTO' => 'glosa',
'FECHA' => 'fecha',
'N° DOCUMENTO' => 'documento',
'SALDO' => 'saldo'
'SALDO' => 'saldo',
'Fecha' => 'fecha',
'Cargo ($)' => 'cargo',
'Abono ($)' => 'abono',
'Descripcin' => 'glosa',
'Saldo Diario' => 'saldo'
];
}
protected function parseFile(UploadedFileInterface $uploadedFile): array
@ -26,7 +31,11 @@ class Santander extends Banco
$uploadedFile->moveTo($filename);
$reader = PhpSpreadsheet\IOFactory::createReader('Xlsx');
$xlsx = $reader->load($filename);
try {
$xlsx = $reader->load($filename);
} catch (PhpSpreadsheet\Reader\Exception) {
return $this->parseHtml($filename);
}
$sheet = $xlsx->getActiveSheet();
$found = false;
@ -74,4 +83,59 @@ class Santander extends Banco
return $data;
}
protected function parseHtml(string $filename): array
{
$data = [];
$lines = explode("\r\n", file_get_contents($filename));
$columns = [];
$found = false;
for ($rowIndex = 0; $rowIndex < count($lines); $rowIndex ++) {
if (!$found and str_contains($lines[$rowIndex], 'Cuenta Corriente: ')) {
$found = true;
$rowIndex += 2;
$columns = $this->parseHtmlRow($lines, $rowIndex);
continue;
}
if (!$found) {
continue;
}
$row = $this->parseHtmlRow($lines, $rowIndex);
if (str_contains($row[0], 'Saldo Contable')) {
break;
}
$row = array_combine($columns, $row);
$row['Fecha'] = implode('-', array_reverse(explode('-', $row['Fecha'])));
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
{
if (!str_contains($lines[$rowIndex ++], '<tr>')) {
return [];
}
$data = [];
while (!str_contains($lines[$rowIndex], '</tr>')) {
$tags = substr_count($lines[$rowIndex], '<') - substr_count($lines[$rowIndex], '</');
$ini = 0;
for ($t = 0; $t < $tags; $t ++) {
$ini = strpos($lines[$rowIndex], '>', $ini) + 1;
}
$end = strpos($lines[$rowIndex], '<', $ini + 1);
$cell = str_replace(' ', '', substr($lines[$rowIndex], $ini, $end - $ini));
$encoding = mb_detect_encoding($cell, ['Windows-1252', 'UTF-8']);
if ($encoding !== 'UTF-8') {
$cell = mb_convert_encoding($cell, $encoding, 'UTF-8');
$cell = str_replace('?', '', $cell);
}
$data []= $cell;
$rowIndex ++;
}
return $data;
}
}