Add sub Bancos to BCI and Santander
This commit is contained in:
@ -80,5 +80,17 @@ return [
|
|||||||
$container->get(Incoviba\Service\UF::class),
|
$container->get(Incoviba\Service\UF::class),
|
||||||
$container->get(Incoviba\Service\USD::class)
|
$container->get(Incoviba\Service\USD::class)
|
||||||
);
|
);
|
||||||
|
},
|
||||||
|
Incoviba\Service\Contabilidad\Cartola\Santander::class => function(ContainerInterface $container) {
|
||||||
|
return (new Incoviba\Service\Contabilidad\Cartola\Santander(
|
||||||
|
$container->get(Psr\Log\LoggerInterface::class),
|
||||||
|
))
|
||||||
|
->registerSub($container->get(Incoviba\Service\Contabilidad\Cartola\Santander\OfficeBanking::class));
|
||||||
|
},
|
||||||
|
Incoviba\Service\Contabilidad\Cartola\BCI::class => function(ContainerInterface $container) {
|
||||||
|
return (new Incoviba\Service\Contabilidad\Cartola\BCI(
|
||||||
|
$container->get(Psr\Log\LoggerInterface::class),
|
||||||
|
))
|
||||||
|
->registerSub($container->get(Incoviba\Service\Contabilidad\Cartola\BCI\Mes::class));
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
@ -7,6 +7,8 @@ use Psr\Http\Message\UploadedFileInterface;
|
|||||||
|
|
||||||
class BCI extends Banco
|
class BCI extends Banco
|
||||||
{
|
{
|
||||||
|
use withSubBancos;
|
||||||
|
|
||||||
protected function columnMap(): array
|
protected function columnMap(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
@ -29,7 +31,13 @@ class BCI extends Banco
|
|||||||
}
|
}
|
||||||
protected function parseFile(string $filename): array
|
protected function parseFile(string $filename): array
|
||||||
{
|
{
|
||||||
$xlsx = @PhpSpreadsheet\IOFactory::load($filename);
|
try {
|
||||||
|
$reader = PhpSpreadsheet\IOFactory::createReader('Xlsx');
|
||||||
|
} catch (PhpSpreadsheet\Reader\Exception $exception) {
|
||||||
|
$this->logger->critical($exception, ['filename' => $filename]);
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
$xlsx = $reader->load($filename);
|
||||||
$worksheet = $xlsx->getActiveSheet();
|
$worksheet = $xlsx->getActiveSheet();
|
||||||
$rows = $worksheet->getRowIterator();
|
$rows = $worksheet->getRowIterator();
|
||||||
|
|
||||||
|
100
app/src/Service/Contabilidad/Cartola/BCI/Mes.php
Normal file
100
app/src/Service/Contabilidad/Cartola/BCI/Mes.php
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
<?php
|
||||||
|
namespace Incoviba\Service\Contabilidad\Cartola\BCI;
|
||||||
|
|
||||||
|
use Psr\Http\Message\UploadedFileInterface;
|
||||||
|
use PhpOffice\PhpSpreadsheet;
|
||||||
|
use Incoviba\Common\Ideal\Cartola\Banco;
|
||||||
|
|
||||||
|
class Mes extends Banco
|
||||||
|
{
|
||||||
|
public function is(string $filename): bool
|
||||||
|
{
|
||||||
|
if (!str_ends_with($filename, 'xlsx')) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
$reader = PhpSpreadsheet\IOFactory::createReader('Xlsx');
|
||||||
|
} catch (PhpSpreadsheet\Reader\Exception $exception) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$xlsx = $reader->load($filename);
|
||||||
|
$sheet = $xlsx->getActiveSheet();
|
||||||
|
$subtitle = $sheet->getCell('A2')->getCalculatedValue();
|
||||||
|
return trim($subtitle) === 'Cartola de cuenta corriente';
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function columnMap(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'Fecha' => 'fecha',
|
||||||
|
'Descripción' => 'glosa',
|
||||||
|
'N° Documento' => 'identificador',
|
||||||
|
'Cheques y otros cargos' => 'cargo',
|
||||||
|
'Depósitos y Abono' => 'abono',
|
||||||
|
'Saldo diario' => 'saldo',
|
||||||
|
'Categoría' => 'categoria',
|
||||||
|
'Centro costos' => 'centro_costo',
|
||||||
|
'Detalle' => 'detalle',
|
||||||
|
'Factura Boleta' => 'identificador',
|
||||||
|
'RUT' => 'rut',
|
||||||
|
'Nombres' => 'nombres',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
protected function getFilename(UploadedFileInterface $uploadedFile): string
|
||||||
|
{
|
||||||
|
return '/tmp/cartola.xlsx';
|
||||||
|
}
|
||||||
|
protected function parseFile(string $filename): array
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$reader = PhpSpreadsheet\IOFactory::createReader('Xlsx');
|
||||||
|
} catch (PhpSpreadsheet\Reader\Exception $exception) {
|
||||||
|
$this->logger->critical($exception, ['filename' => $filename]);
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
$xlsx = $reader->load($filename);
|
||||||
|
$sheet = $xlsx->getActiveSheet();
|
||||||
|
|
||||||
|
$valueColumns = [
|
||||||
|
'cargo',
|
||||||
|
'abono',
|
||||||
|
'saldo',
|
||||||
|
];
|
||||||
|
$found = false;
|
||||||
|
$columns = [];
|
||||||
|
$data = [];
|
||||||
|
foreach ($sheet->getRowIterator() as $row) {
|
||||||
|
if (!$found and $sheet->getCell("A{$row->getRowIndex()}")->getCalculatedValue() === 'Fecha') {
|
||||||
|
$found = true;
|
||||||
|
foreach ($row->getColumnIterator() as $column) {
|
||||||
|
if ($column->getValue() === null) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$columns[$column->getColumn()] = trim($column->getValue());
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!$found) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if ($sheet->getCell("A{$row->getRowIndex()}")->getValue() === null) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$rowData = [];
|
||||||
|
foreach ($columns as $columnIndex => $column) {
|
||||||
|
$value = $sheet->getCell("{$columnIndex}{$row->getRowIndex()}")->getCalculatedValue();
|
||||||
|
$mapped = $this->columnMap()[$column] ?? $column;
|
||||||
|
if ($mapped === 'fecha') {
|
||||||
|
$value = PhpSpreadsheet\Shared\Date::excelToDateTimeObject($value)->format('Y-m-d');
|
||||||
|
}
|
||||||
|
if (in_array($mapped, $valueColumns)) {
|
||||||
|
$value = (int) $value;
|
||||||
|
}
|
||||||
|
$rowData[$column] = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
$data []= $rowData;
|
||||||
|
}
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
}
|
@ -1,34 +1,46 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Incoviba\Service\Contabilidad\Cartola;
|
namespace Incoviba\Service\Contabilidad\Cartola;
|
||||||
|
|
||||||
use Incoviba\Common\Ideal\Cartola\Banco;
|
|
||||||
use PhpOffice\PhpSpreadsheet;
|
|
||||||
use Psr\Http\Message\UploadedFileInterface;
|
use Psr\Http\Message\UploadedFileInterface;
|
||||||
|
use PhpOffice\PhpSpreadsheet;
|
||||||
|
use Incoviba\Common\Ideal\Cartola\Banco;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
|
|
||||||
class Santander extends Banco
|
class Santander extends Banco
|
||||||
{
|
{
|
||||||
|
use withSubBancos;
|
||||||
|
|
||||||
|
/*public function process(UploadedFileInterface $file): array
|
||||||
|
{
|
||||||
|
$filename = $this->processUploadedFile($file);
|
||||||
|
try {
|
||||||
|
$subBanco = $this->getSubBanco($filename);
|
||||||
|
$data = $subBanco->processFile($filename);
|
||||||
|
return $subBanco->mapColumns($data);
|
||||||
|
} catch (InvalidArgumentException) {
|
||||||
|
$data = parent::processFile($filename);
|
||||||
|
return $this->mapColumns($data);
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
|
||||||
protected function columnMap(): array
|
protected function columnMap(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'cargo' => 'cargo',
|
|
||||||
'abono' => 'abono',
|
|
||||||
'DESCRIPCIÓN MOVIMIENTO' => 'glosa',
|
|
||||||
'FECHA' => 'fecha',
|
|
||||||
'N° DOCUMENTO' => 'documento',
|
|
||||||
'SALDO' => 'saldo',
|
|
||||||
'Fecha' => 'fecha',
|
'Fecha' => 'fecha',
|
||||||
'Cargo ($)' => 'cargo',
|
'Descripción' => 'glosa',
|
||||||
'Abono ($)' => 'abono',
|
'Nº Docu' => 'documento',
|
||||||
'Descripcin' => 'glosa',
|
'Cheques y Otros Cargos' => 'cargo',
|
||||||
'Saldo Diario' => 'saldo',
|
'Depósitos y Abonos' => 'abono',
|
||||||
|
'Saldo' => 'saldo',
|
||||||
'Categoría' => 'categoria',
|
'Categoría' => 'categoria',
|
||||||
'Centro costos' => 'centro_costo',
|
'Centro costos' => 'centro_costo',
|
||||||
'Detalle' => 'detalle',
|
'Detalle' => 'detalle',
|
||||||
'Factura Boleta' => 'identificador',
|
'Factura o Boleta' => 'identificador',
|
||||||
'RUT' => 'rut',
|
'RUT' => 'rut',
|
||||||
'Nombres' => 'nombres',
|
'Nombre' => 'nombres',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getFilename(UploadedFileInterface $uploadedFile): string
|
protected function getFilename(UploadedFileInterface $uploadedFile): string
|
||||||
{
|
{
|
||||||
$start = $uploadedFile->getStream()->read(10);
|
$start = $uploadedFile->getStream()->read(10);
|
||||||
@ -47,15 +59,25 @@ class Santander extends Banco
|
|||||||
}
|
}
|
||||||
protected function parseXlsx(string $filename): array
|
protected function parseXlsx(string $filename): array
|
||||||
{
|
{
|
||||||
$reader = PhpSpreadsheet\IOFactory::createReader('Xlsx');
|
try {
|
||||||
|
$reader = PhpSpreadsheet\IOFactory::createReader('Xlsx');
|
||||||
|
} catch (PhpSpreadsheet\Reader\Exception $exception) {
|
||||||
|
$this->logger->critical($exception, ['filename' => $filename]);
|
||||||
|
return [];
|
||||||
|
}
|
||||||
$xlsx = $reader->load($filename);
|
$xlsx = $reader->load($filename);
|
||||||
$sheet = $xlsx->getActiveSheet();
|
$sheet = $xlsx->getActiveSheet();
|
||||||
|
|
||||||
|
$valueColumns = [
|
||||||
|
'Cheques y Otros Cargos',
|
||||||
|
'Depósitos y Abonos',
|
||||||
|
'Saldo'
|
||||||
|
];
|
||||||
$found = false;
|
$found = false;
|
||||||
$columns = [];
|
$columns = [];
|
||||||
$data = [];
|
$data = [];
|
||||||
foreach ($sheet->getRowIterator() as $row) {
|
foreach ($sheet->getRowIterator() as $row) {
|
||||||
if (!$found and $sheet->getCell("A{$row->getRowIndex()}")->getCalculatedValue() === 'MONTO') {
|
if (!$found and $sheet->getCell("A{$row->getRowIndex()}")->getCalculatedValue() === 'Fecha') {
|
||||||
$found = true;
|
$found = true;
|
||||||
foreach ($row->getColumnIterator() as $column) {
|
foreach ($row->getColumnIterator() as $column) {
|
||||||
if ($column->getValue() === null) {
|
if ($column->getValue() === null) {
|
||||||
@ -76,21 +98,21 @@ class Santander extends Banco
|
|||||||
$value = $sheet->getCell("{$columnIndex}{$row->getRowIndex()}")->getCalculatedValue();
|
$value = $sheet->getCell("{$columnIndex}{$row->getRowIndex()}")->getCalculatedValue();
|
||||||
$mapped = $this->columnMap()[$column] ?? $column;
|
$mapped = $this->columnMap()[$column] ?? $column;
|
||||||
if ($mapped === 'fecha') {
|
if ($mapped === 'fecha') {
|
||||||
$value = implode('-', array_reverse(explode('/', $value)));
|
$value = PhpSpreadsheet\Shared\Date::excelToDateTimeObject($value)->format('Y-m-d');
|
||||||
}
|
}
|
||||||
if ($column === 'MONTO' or $column === 'SALDO') {
|
if (in_array($column, $valueColumns)) {
|
||||||
$value = (int) $value;
|
$value = (int) $value;
|
||||||
}
|
}
|
||||||
$rowData[$column] = $value;
|
$rowData[$column] = $value;
|
||||||
}
|
}
|
||||||
if ($rowData['CARGO/ABONO'] === 'C') {
|
/*if ($rowData['CARGO/ABONO'] === 'C') {
|
||||||
$rowData['MONTO'] = -$rowData['MONTO'];
|
$rowData['MONTO'] = -$rowData['MONTO'];
|
||||||
$rowData['cargo'] = $rowData['MONTO'];
|
$rowData['cargo'] = $rowData['MONTO'];
|
||||||
$rowData['abono'] = 0;
|
$rowData['abono'] = 0;
|
||||||
} else {
|
} else {
|
||||||
$rowData['cargo'] = 0;
|
$rowData['cargo'] = 0;
|
||||||
$rowData['abono'] = $rowData['MONTO'];
|
$rowData['abono'] = $rowData['MONTO'];
|
||||||
}
|
}*/
|
||||||
$data []= $rowData;
|
$data []= $rowData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
175
app/src/Service/Contabilidad/Cartola/Santander/OfficeBanking.php
Normal file
175
app/src/Service/Contabilidad/Cartola/Santander/OfficeBanking.php
Normal file
@ -0,0 +1,175 @@
|
|||||||
|
<?php
|
||||||
|
namespace Incoviba\Service\Contabilidad\Cartola\Santander;
|
||||||
|
|
||||||
|
use Psr\Http\Message\UploadedFileInterface;
|
||||||
|
use PhpOffice\PhpSpreadsheet;
|
||||||
|
use Incoviba\Common\Ideal\Cartola\Banco;
|
||||||
|
|
||||||
|
class OfficeBanking extends Banco
|
||||||
|
{
|
||||||
|
public function is(string $filename): bool
|
||||||
|
{
|
||||||
|
if (!str_ends_with($filename, 'xlsx')) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
$reader = PhpSpreadsheet\IOFactory::createReader('Xlsx');
|
||||||
|
} catch (PhpSpreadsheet\Reader\Exception $exception) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$xlsx = $reader->load($filename);
|
||||||
|
$sheet = $xlsx->getActiveSheet();
|
||||||
|
$subtitle = $sheet->getCell('A2')->getCalculatedValue();
|
||||||
|
return $subtitle === 'Consulta de movimientos de Cuentas Corrientes';
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function columnMap(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'cargo' => 'cargo',
|
||||||
|
'abono' => 'abono',
|
||||||
|
'DESCRIPCIÓN MOVIMIENTO' => 'glosa',
|
||||||
|
'FECHA' => 'fecha',
|
||||||
|
'N° DOCUMENTO' => 'documento',
|
||||||
|
'SALDO' => 'saldo',
|
||||||
|
'Fecha' => 'fecha',
|
||||||
|
'Cargo ($)' => 'cargo',
|
||||||
|
'Abono ($)' => 'abono',
|
||||||
|
'Descripcin' => 'glosa',
|
||||||
|
'Saldo Diario' => 'saldo',
|
||||||
|
'Categoría' => 'categoria',
|
||||||
|
'Centro costos' => 'centro_costo',
|
||||||
|
'Detalle' => 'detalle',
|
||||||
|
'Factura Boleta' => 'identificador',
|
||||||
|
'RUT' => 'rut',
|
||||||
|
'Nombres' => 'nombres',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
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
|
||||||
|
{
|
||||||
|
if (str_ends_with($filename, 'xlsx')) {
|
||||||
|
return $this->parseXlsx($filename);
|
||||||
|
}
|
||||||
|
return $this->parseHtml($filename);
|
||||||
|
}
|
||||||
|
protected function parseXlsx(string $filename): array
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$reader = PhpSpreadsheet\IOFactory::createReader('Xlsx');
|
||||||
|
} catch (PhpSpreadsheet\Reader\Exception $exception) {
|
||||||
|
$this->logger->critical($exception, ['filename' => $filename]);
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
$xlsx = $reader->load($filename);
|
||||||
|
$sheet = $xlsx->getActiveSheet();
|
||||||
|
|
||||||
|
$found = false;
|
||||||
|
$columns = [];
|
||||||
|
$data = [];
|
||||||
|
foreach ($sheet->getRowIterator() as $row) {
|
||||||
|
if (!$found and $sheet->getCell("A{$row->getRowIndex()}")->getCalculatedValue() === 'MONTO') {
|
||||||
|
$found = true;
|
||||||
|
foreach ($row->getColumnIterator() as $column) {
|
||||||
|
if ($column->getValue() === null) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$columns[$column->getColumn()] = trim($column->getValue());
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!$found) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if ($sheet->getCell("A{$row->getRowIndex()}")->getValue() === 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;
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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['N° DOCUMENTO'] = '';
|
||||||
|
$data []= $row;
|
||||||
|
}
|
||||||
|
|
||||||
|
return array_reverse($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;
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user