6 Commits

11 changed files with 467 additions and 83 deletions

View File

@ -9,20 +9,9 @@ abstract class Banco extends Service implements Define\Cartola\Banco
{
public function process(UploadedFileInterface $file): array
{
$data = $this->handleFile($file);
$temp = [];
$columns = $this->columnMap();
foreach ($data as $row) {
$r = [];
foreach ($columns as $old => $new) {
if (!isset($row[$old])) {
continue;
}
$r[$new] = $row[$old];
}
$temp []= $r;
}
return $temp;
$filename = $this->processUploadedFile($file);
$data = $this->processFile($filename);
return $this->mapColumns($data);
}
/**
@ -36,19 +25,52 @@ abstract class Banco extends Service implements Define\Cartola\Banco
}
/**
* Move the UploadedFile into a temp file from getFilename and after parseFile remove temp file
* Move the UploadedFile into a temp file from getFilename
* @param UploadedFileInterface $uploadedFile
* @return array
* @return string
*/
protected function handleFile(UploadedFileInterface $uploadedFile): array
protected function processUploadedFile(UploadedFileInterface $uploadedFile): string
{
$filename = $this->getFilename($uploadedFile);
$uploadedFile->moveTo($filename);
return $filename;
}
/**
* Process the temp file from getFilename and remove it
* @param string $filename
* @return array
*/
protected function processFile(string $filename): array
{
$data = $this->parseFile($filename);
unlink($filename);
return $data;
}
/**
* Map columns from uploaded file data to database columns
* @param array $data
* @return array
*/
protected function mapColumns(array $data): array
{
$temp = [];
$columns = $this->columnMap();
foreach ($data as $row) {
$r = [];
foreach ($columns as $old => $new) {
if (!isset($row[$old])) {
continue;
}
$r[$new] = $row[$old];
}
$temp []= $r;
}
return $temp;
}
/**
* Get filename where to move UploadedFile
* @param UploadedFileInterface $uploadedFile

View File

@ -285,6 +285,11 @@
return
}
return response.json().then(data => {
if (data.errors.length > 0) {
data.errors.forEach(errorData => {
console.error(errorData)
})
}
this.data.movimientos = data.movimientos.map(movimiento => new Movimiento(movimiento))
this.draw().movimientos()
})

View File

@ -80,5 +80,17 @@ return [
$container->get(Incoviba\Service\UF::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));
}
];

View File

@ -7,7 +7,6 @@ use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Ideal\Controller;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Controller\API\withJson;
use Incoviba\Model\Inmobiliaria;
use Incoviba\Repository;
use Incoviba\Service;
@ -97,62 +96,47 @@ class Cartolas extends Controller
return $this->withJson($response, $output);
}
public function importar(ServerRequestInterface $request, ResponseInterface $response,
Repository\Inmobiliaria\Cuenta $cuentaRepository,
Service\Contabilidad\Cartola $cartolaService,
Service\Contabilidad\Movimiento $movimientoService): ResponseInterface
Service\Contabilidad\Cartola $cartolaService): ResponseInterface
{
$body = $request->getParsedBody();
$files = $request->getUploadedFiles();
$output = [
'input' => $body,
'movimientos' => []
'movimientos' => [],
'errors' => []
];
$errors = [
UPLOAD_ERR_OK => 'Ok',
UPLOAD_ERR_INI_SIZE => 'El archivo excede el tamaño máximo permitido',
UPLOAD_ERR_FORM_SIZE => 'El archivo excede el tamaño máximo permitido',
UPLOAD_ERR_PARTIAL => 'El archivo fue subido parcialmente',
UPLOAD_ERR_NO_FILE => 'No se subió ningún archivo',
UPLOAD_ERR_NO_TMP_DIR => 'Falta la carpeta temporal',
UPLOAD_ERR_CANT_WRITE => 'No se pudo escribir el archivo',
UPLOAD_ERR_EXTENSION => 'Una extensión de PHP detuvo la subida del archivo'
];
if (is_array($files['file'])) {
foreach ($files['file'] as $i => $file) {
if ($file->getError() !== UPLOAD_ERR_OK) {
$output['errors'] []= ['filename' => $file->getClientFilename(), 'error' => $errors[$file->getError()]];
continue;
}
try {
$cuenta = $cuentaRepository->fetchById($body['cuenta_id'][$i]);
$movimientos = $cartolaService->process($cuenta->banco, $file);
$this->addMovimientos($movimientoService, $cuenta, $movimientos);
$inmobiliaria = $cuenta->inmobiliaria;
$output['movimientos'] = array_merge($output['movimientos'], array_map(function($movimiento) use ($inmobiliaria) {
$movimiento['sociedad'] = $inmobiliaria;
return $movimiento;
}, $movimientos));
$output['movimientos'] = array_merge($output['movimientos'], $cartolaService->import($body['cuenta_id'][$i], $file));
} catch (EmptyResult) {}
}
} else {
try {
$cuenta = $cuentaRepository->fetchById($body['cuenta_id']);
$movimientos = $cartolaService->process($cuenta->banco, $files['file']);
$this->addMovimientos($movimientoService, $cuenta, $movimientos);
$inmobiliaria = $cuenta->inmobiliaria;
$output['movimientos'] = array_map(function($movimiento) use ($inmobiliaria) {
$movimiento['sociedad'] = $inmobiliaria;
return $movimiento;
}, $movimientos);
} catch (EmptyResult) {}
$file = $files['file'];
if ($file->getError() !== UPLOAD_ERR_OK) {
$output['errors'][] = ['filename' => $file->getClientFilename(), 'error' => $errors[$file->getError()]];
} else {
try {
$output['movimientos'] = $cartolaService->import($body['cuenta_id'], $file);
} catch (EmptyResult) {}
}
}
return $this->withJson($response, $output);
}
protected function addMovimientos(Service\Contabilidad\Movimiento $movimientoService, Inmobiliaria\Cuenta $cuenta, array $movimientos): void
{
foreach ($movimientos as $dataMovimiento) {
$dataMovimiento['cuenta_id'] = $cuenta->id;
if (array_key_exists('centro_costo', $dataMovimiento)) {
$dataMovimiento['centro_costo_id'] = $dataMovimiento['centro_costo'];
}
$dataMovimiento['fecha'] = new DateTimeImmutable($dataMovimiento['fecha']);
if (array_key_exists('rut', $dataMovimiento)) {
list($rut, $digito) = explode('-', $dataMovimiento['rut']);
$dataMovimiento['rut'] = trim(preg_replace('/\D+/', '', $rut));
$dataMovimiento['digito'] = trim($digito);
}
$movimientoService->add($dataMovimiento);
}
}
}

View File

@ -91,6 +91,31 @@ class Cartola extends Service
$cartola = $this->buildCartola($cuenta, $fecha, $cartolaData);
return compact('cartola', 'movimientos');
}
public function import(int $cuenta_id, UploadedFileInterface $file): array
{
$cuenta = $this->cuentaRepository->fetchById($cuenta_id);
$movimientos = $this->process($cuenta->banco, $file);
foreach ($movimientos as $dataMovimiento) {
$dataMovimiento['cuenta_id'] = $cuenta->id;
if (array_key_exists('centro_costo', $dataMovimiento)) {
$dataMovimiento['centro_costo_id'] = $dataMovimiento['centro_costo'];
}
$dataMovimiento['fecha'] = new DateTimeImmutable($dataMovimiento['fecha']);
if (array_key_exists('rut', $dataMovimiento)) {
list($rut, $digito) = explode('-', $dataMovimiento['rut']);
$dataMovimiento['rut'] = trim(preg_replace('/\D+/', '', $rut));
$dataMovimiento['digito'] = trim($digito);
}
$this->movimientoService->add($dataMovimiento);
}
$inmobiliaria = $cuenta->inmobiliaria;
return array_map(function($movimiento) use ($inmobiliaria) {
$movimiento['sociedad'] = $inmobiliaria;
return $movimiento;
}, $movimientos);
}
protected function getMovimientosDiarios(Model\Contabilidad\Banco $banco, UploadedFileInterface $file): array
{

View File

@ -7,6 +7,8 @@ use Psr\Http\Message\UploadedFileInterface;
class BCI extends Banco
{
use withSubBancos;
protected function columnMap(): array
{
return [
@ -29,7 +31,13 @@ class BCI extends Banco
}
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();
$rows = $worksheet->getRowIterator();

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

View File

@ -1,34 +1,46 @@
<?php
namespace Incoviba\Service\Contabilidad\Cartola;
use Incoviba\Common\Ideal\Cartola\Banco;
use PhpOffice\PhpSpreadsheet;
use Psr\Http\Message\UploadedFileInterface;
use PhpOffice\PhpSpreadsheet;
use Incoviba\Common\Ideal\Cartola\Banco;
use Psr\Log\LoggerInterface;
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
{
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',
'Descripción' => 'glosa',
'Nº Docu' => 'documento',
'Cheques y Otros Cargos' => 'cargo',
'Depósitos y Abonos' => 'abono',
'Saldo' => 'saldo',
'Categoría' => 'categoria',
'Centro costos' => 'centro_costo',
'Detalle' => 'detalle',
'Factura Boleta' => 'identificador',
'Factura o Boleta' => 'identificador',
'RUT' => 'rut',
'Nombres' => 'nombres',
'Nombre' => 'nombres',
];
}
protected function getFilename(UploadedFileInterface $uploadedFile): string
{
$start = $uploadedFile->getStream()->read(10);
@ -47,15 +59,25 @@ class Santander extends Banco
}
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);
$sheet = $xlsx->getActiveSheet();
$valueColumns = [
'Cheques y Otros Cargos',
'Depósitos y Abonos',
'Saldo'
];
$found = false;
$columns = [];
$data = [];
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;
foreach ($row->getColumnIterator() as $column) {
if ($column->getValue() === null) {
@ -76,21 +98,21 @@ class Santander extends Banco
$value = $sheet->getCell("{$columnIndex}{$row->getRowIndex()}")->getCalculatedValue();
$mapped = $this->columnMap()[$column] ?? $column;
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;
}
$rowData[$column] = $value;
}
if ($rowData['CARGO/ABONO'] === 'C') {
/*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;
}

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

View File

@ -0,0 +1,36 @@
<?php
namespace Incoviba\Service\Contabilidad\Cartola;
use Incoviba\Common\Ideal\Cartola\Banco;
use Psr\Http\Message\UploadedFileInterface;
trait withSubBancos
{
protected array $subBancos;
public function registerSub(Banco $sub): self
{
$this->subBancos []= $sub;
return $this;
}
public function getSubBanco(string $filename): Banco
{
foreach ($this->subBancos as $subBanco) {
if ($subBanco->is($filename)) {
return $subBanco;
}
}
return $this;
}
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);
}
}
}

View File

@ -74,11 +74,6 @@ class Movimiento extends Service
}
})
->setArgs(['movimiento_id' => $movimiento->id]));
/*$movimiento->addFactory('auxiliares', (new Implement\Repository\Factory())
->setCallable(function(int $movimiento_id) {
return $this->auxiliarService->getByMovimiento($movimiento_id);
})
->setArgs(['movimiento_id' => $movimiento->id]));*/
return $movimiento;
}
}