Cartola BCI

This commit is contained in:
Juan Pablo Vial
2024-03-18 17:54:27 -03:00
parent 936fd2d1e1
commit dc2e2c7f71
8 changed files with 143 additions and 27 deletions

View File

@ -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;
}
}
}
}

View File

@ -0,0 +1,69 @@
<?php
namespace Incoviba\Service\Cartola;
use DOMDocument;
use DOMElement;
use Psr\Http\Message\UploadedFileInterface;
use Incoviba\Common\Ideal\Cartola\Banco;
class BCI extends Banco
{
protected function columnMap(): array
{
return [
'Fecha' => '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('<body>' . str_replace('?', '', mb_convert_encoding(file_get_contents($filename), 'windows-1252', 'utf-8')) . '</body>');
$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;
}
}

View File

@ -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;
}

View File

@ -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);

View File

@ -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('<body>' . file_get_contents($filename) . '</body>');
@ -122,7 +123,6 @@ class Security extends Banco
}
$data []= $rowData;
}
return $data;
}
}