Cartola BCI
This commit is contained in:
@ -5,6 +5,11 @@ use Psr\Http\Message\UploadedFileInterface;
|
|||||||
|
|
||||||
interface Banco
|
interface Banco
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Process bank movements for database inserts
|
||||||
|
* @param UploadedFileInterface $file
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
public function process(UploadedFileInterface $file): array;
|
public function process(UploadedFileInterface $file): array;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ abstract class Banco extends Service implements Define\Cartola\Banco
|
|||||||
{
|
{
|
||||||
public function process(UploadedFileInterface $file): array
|
public function process(UploadedFileInterface $file): array
|
||||||
{
|
{
|
||||||
$data = $this->parseFile($file);
|
$data = $this->handleFile($file);
|
||||||
$temp = [];
|
$temp = [];
|
||||||
$columns = $this->columnMap();
|
$columns = $this->columnMap();
|
||||||
foreach ($data as $row) {
|
foreach ($data as $row) {
|
||||||
@ -24,11 +24,48 @@ abstract class Banco extends Service implements Define\Cartola\Banco
|
|||||||
}
|
}
|
||||||
return $temp;
|
return $temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* There are banks that need some post-processing
|
||||||
|
* @param array $movimientos
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
public function processMovimientosDiarios(array $movimientos): array
|
public function processMovimientosDiarios(array $movimientos): array
|
||||||
{
|
{
|
||||||
return $movimientos;
|
return $movimientos;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Move the UploadedFile into a temp file from getFilename and after parseFile remove temp file
|
||||||
|
* @param UploadedFileInterface $uploadedFile
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function handleFile(UploadedFileInterface $uploadedFile): array
|
||||||
|
{
|
||||||
|
$filename = $this->getFilename($uploadedFile);
|
||||||
|
$uploadedFile->moveTo($filename);
|
||||||
|
$data = $this->parseFile($filename);
|
||||||
|
unlink($filename);
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get filename where to move UploadedFile
|
||||||
|
* @param UploadedFileInterface $uploadedFile
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
abstract protected function getFilename(UploadedFileInterface $uploadedFile): string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mapping of uploaded file data columns to database columns
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
abstract protected function columnMap(): array;
|
abstract protected function columnMap(): array;
|
||||||
abstract protected function parseFile(UploadedFileInterface $uploadedFile): array;
|
|
||||||
|
/**
|
||||||
|
* Translate uploaded file data to database data
|
||||||
|
* @param string $filename
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
abstract protected function parseFile(string $filename): array;
|
||||||
}
|
}
|
||||||
|
@ -45,7 +45,8 @@ return [
|
|||||||
))
|
))
|
||||||
->register('security', $container->get(Incoviba\Service\Cartola\Security::class))
|
->register('security', $container->get(Incoviba\Service\Cartola\Security::class))
|
||||||
->register('itau', $container->get(Incoviba\Service\Cartola\Itau::class))
|
->register('itau', $container->get(Incoviba\Service\Cartola\Itau::class))
|
||||||
->register('santander', $container->get(Incoviba\Service\Cartola\Santander::class));
|
->register('santander', $container->get(Incoviba\Service\Cartola\Santander::class))
|
||||||
|
->register('bci', $container->get(Incoviba\Service\Cartola\BCI::class));
|
||||||
},
|
},
|
||||||
Incoviba\Common\Define\Contabilidad\Exporter::class => function(ContainerInterface $container) {
|
Incoviba\Common\Define\Contabilidad\Exporter::class => function(ContainerInterface $container) {
|
||||||
return $container->get(Incoviba\Service\Contabilidad\Exporter\Nubox::class);
|
return $container->get(Incoviba\Service\Contabilidad\Exporter\Nubox::class);
|
||||||
|
@ -121,7 +121,12 @@ class Cartola extends Service
|
|||||||
} catch (Exception\EmptyResult $exception) {
|
} catch (Exception\EmptyResult $exception) {
|
||||||
$data['cuenta_id'] = $cuenta->id;
|
$data['cuenta_id'] = $cuenta->id;
|
||||||
$movimiento = $this->movimientoRepository->create($data);
|
$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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
69
app/src/Service/Cartola/BCI.php
Normal file
69
app/src/Service/Cartola/BCI.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -29,12 +29,12 @@ class Itau extends Banco
|
|||||||
'Saldos' => 'saldo'
|
'Saldos' => 'saldo'
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
protected function getFilename(UploadedFileInterface $uploadedFile): string
|
||||||
protected function parseFile(UploadedFileInterface $uploadedFile): array
|
{
|
||||||
|
return '/tmp/cartola.xls';
|
||||||
|
}
|
||||||
|
protected function parseFile(string $filename): array
|
||||||
{
|
{
|
||||||
$filename = '/tmp/cartola.xls';
|
|
||||||
$uploadedFile->moveTo($filename);
|
|
||||||
|
|
||||||
$reader = PhpSpreadsheet\IOFactory::createReader('Xls');
|
$reader = PhpSpreadsheet\IOFactory::createReader('Xls');
|
||||||
$xlsx = $reader->load($filename);
|
$xlsx = $reader->load($filename);
|
||||||
$sheet = $xlsx->getActiveSheet();
|
$sheet = $xlsx->getActiveSheet();
|
||||||
@ -51,8 +51,6 @@ class Itau extends Banco
|
|||||||
}
|
}
|
||||||
} catch (PhpSpreadsheet\Exception $exception) {
|
} catch (PhpSpreadsheet\Exception $exception) {
|
||||||
$this->logger->critical($exception);
|
$this->logger->critical($exception);
|
||||||
} finally {
|
|
||||||
unlink($filename);
|
|
||||||
}
|
}
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,6 @@ class Santander extends Banco
|
|||||||
{
|
{
|
||||||
protected function columnMap(): array
|
protected function columnMap(): array
|
||||||
{
|
{
|
||||||
// MONTO DESCRIPCIÓN MOVIMIENTO FECHA N° DOCUMENTO SUCURSAL CARGO/ABONO
|
|
||||||
return [
|
return [
|
||||||
'cargo' => 'cargo',
|
'cargo' => 'cargo',
|
||||||
'abono' => 'abono',
|
'abono' => 'abono',
|
||||||
@ -25,11 +24,13 @@ class Santander extends Banco
|
|||||||
'Saldo Diario' => 'saldo'
|
'Saldo Diario' => 'saldo'
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
protected function parseFile(UploadedFileInterface $uploadedFile): array
|
protected function getFilename(UploadedFileInterface $uploadedFile): string
|
||||||
{
|
{
|
||||||
$filename = '/tmp/cartola.xlsx';
|
return '/tmp/cartola.xlsx';
|
||||||
$uploadedFile->moveTo($filename);
|
}
|
||||||
|
|
||||||
|
protected function parseFile(string $filename): array
|
||||||
|
{
|
||||||
$reader = PhpSpreadsheet\IOFactory::createReader('Xlsx');
|
$reader = PhpSpreadsheet\IOFactory::createReader('Xlsx');
|
||||||
try {
|
try {
|
||||||
$xlsx = $reader->load($filename);
|
$xlsx = $reader->load($filename);
|
||||||
|
@ -16,14 +16,21 @@ class Security extends Banco
|
|||||||
return $movimientos;
|
return $movimientos;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function parseFile(UploadedFileInterface $uploadedFile): array
|
protected function getFilename(UploadedFileInterface $uploadedFile): string
|
||||||
{
|
{
|
||||||
$stream = $uploadedFile->getStream();
|
$stream = $uploadedFile->getStream();
|
||||||
$stream->seek(3);
|
$stream->seek(3);
|
||||||
if ($stream->read(strlen('table')) === 'table') {
|
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
|
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);
|
$xlsx = @PhpSpreadsheet\IOFactory::load($filename);
|
||||||
$worksheet = $xlsx->getActiveSheet();
|
$worksheet = $xlsx->getActiveSheet();
|
||||||
$rows = $worksheet->getRowIterator(3);
|
$rows = $worksheet->getRowIterator(3);
|
||||||
@ -82,14 +87,10 @@ class Security extends Banco
|
|||||||
$data []= $rowData;
|
$data []= $rowData;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
unlink($filename);
|
|
||||||
return $data;
|
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 = new DOMDocument();
|
||||||
$domDocument->loadHTML('<body>' . file_get_contents($filename) . '</body>');
|
$domDocument->loadHTML('<body>' . file_get_contents($filename) . '</body>');
|
||||||
|
|
||||||
@ -122,7 +123,6 @@ class Security extends Banco
|
|||||||
}
|
}
|
||||||
$data []= $rowData;
|
$data []= $rowData;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user