Merge pull request 'FIX: Cartolas not adding Movimientos or adding cargos and abonos' (#46) from fix/carga-cartolas into develop
Reviewed-on: #46
This commit is contained in:
@ -117,25 +117,18 @@ class Cartolas extends Controller
|
||||
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 {
|
||||
$output['movimientos'] = array_merge($output['movimientos'], $cartolaService->import($body['cuenta_id'][$i], $file));
|
||||
} catch (EmptyResult) {}
|
||||
}
|
||||
} else {
|
||||
$file = $files['file'];
|
||||
if (!is_array($files['file'])) {
|
||||
$files['file'] = [$files['file']];
|
||||
$body['cuenta_id'] = [$body['cuenta_id']];
|
||||
}
|
||||
foreach ($files['file'] as $i => $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) {}
|
||||
$output['errors'] []= ['filename' => $file->getClientFilename(), 'error' => $errors[$file->getError()]];
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
$output['movimientos'] = array_merge($output['movimientos'], $cartolaService->import($body['cuenta_id'][$i], $file));
|
||||
} catch (EmptyResult) {}
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
|
@ -1,18 +1,20 @@
|
||||
<?php
|
||||
namespace Incoviba\Service\Contabilidad;
|
||||
|
||||
use DateMalformedStringException;
|
||||
use Incoviba\Exception\ServiceAction\Read;
|
||||
use PDOException;
|
||||
use DateTimeImmutable;
|
||||
use DateTimeInterface;
|
||||
use Psr\Http\Message\StreamFactoryInterface;
|
||||
use Psr\Http\Message\UploadedFileInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Incoviba\Common\Define\Cartola\Banco;
|
||||
use Incoviba\Common\Define\Contabilidad\Exporter;
|
||||
use Incoviba\Common\Ideal\Service;
|
||||
use Incoviba\Common\Implement\Exception;
|
||||
use Incoviba\Model;
|
||||
use Incoviba\Repository;
|
||||
use Psr\Http\Message\StreamFactoryInterface;
|
||||
use Psr\Http\Message\UploadedFileInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class Cartola extends Service
|
||||
{
|
||||
@ -93,9 +95,19 @@ class Cartola extends Service
|
||||
return compact('cartola', 'movimientos');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $cuenta_id
|
||||
* @param UploadedFileInterface $file
|
||||
* @return array
|
||||
* @throws Read
|
||||
*/
|
||||
public function import(int $cuenta_id, UploadedFileInterface $file): array
|
||||
{
|
||||
$cuenta = $this->cuentaRepository->fetchById($cuenta_id);
|
||||
try {
|
||||
$cuenta = $this->cuentaRepository->fetchById($cuenta_id);
|
||||
} catch (Exception\EmptyResult $exception) {
|
||||
throw new Read(__CLASS__, $exception);
|
||||
}
|
||||
$movimientos = $this->process($cuenta->banco, $file);
|
||||
|
||||
$inmobiliaria = $cuenta->inmobiliaria;
|
||||
@ -106,15 +118,23 @@ class Cartola extends Service
|
||||
if (array_key_exists('centro_costo', $dataMovimiento) and $dataMovimiento['centro_costo'] !== 0) {
|
||||
$dataMovimiento['centro_costo_id'] = $dataMovimiento['centro_costo'];
|
||||
}
|
||||
$dataMovimiento['fecha'] = new DateTimeImmutable($dataMovimiento['fecha']);
|
||||
try {
|
||||
$dataMovimiento['fecha'] = new DateTimeImmutable($dataMovimiento['fecha']);
|
||||
} catch (DateMalformedStringException) {
|
||||
continue;
|
||||
}
|
||||
if (array_key_exists('rut', $dataMovimiento)) {
|
||||
$ruts = $this->parseRut($dataMovimiento['rut']);
|
||||
if (key_exists('rut', $ruts)) {
|
||||
$dataMovimiento['rut'] = $ruts['rut'];
|
||||
$dataMovimiento['digito'] = $ruts['digito'];
|
||||
if ($dataMovimiento['rut'] === '') {
|
||||
unset($dataMovimiento['rut']);
|
||||
} else {
|
||||
$dataMovimiento['rut'] = $ruts[0]['rut'];
|
||||
$dataMovimiento['digito'] = $ruts[0]['digito'];
|
||||
$ruts = $this->parseRut($dataMovimiento['rut']);
|
||||
if (key_exists('rut', $ruts)) {
|
||||
$dataMovimiento['rut'] = $ruts['rut'];
|
||||
$dataMovimiento['digito'] = $ruts['digito'];
|
||||
} else {
|
||||
$dataMovimiento['rut'] = $ruts[0]['rut'];
|
||||
$dataMovimiento['digito'] = $ruts[0]['digito'];
|
||||
}
|
||||
}
|
||||
}
|
||||
try {
|
||||
@ -135,12 +155,22 @@ class Cartola extends Service
|
||||
}, $movimientos));
|
||||
foreach ($fechas as $dia) {
|
||||
try {
|
||||
$this->cartolaRepository->fetchByCuentaAndFecha($cuenta->id, new DateTimeImmutable($dia));
|
||||
$dayDate = new DateTimeImmutable($dia);
|
||||
} catch (DateMalformedStringException) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
$this->cartolaRepository->fetchByCuentaAndFecha($cuenta->id, $dayDate);
|
||||
continue;
|
||||
} catch (Exception\EmptyResult) {}
|
||||
|
||||
$movs = array_filter($movimientos, function($movimiento) use ($dia) {
|
||||
return $movimiento['fecha'] === $dia;
|
||||
return $movimiento['fecha']->format('Y-m-d') === $dia;
|
||||
});
|
||||
if (count($movs) === 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$cargos = array_sum(array_map(function($movimiento) {
|
||||
return $movimiento['cargo'];
|
||||
}, $movs));
|
||||
@ -153,11 +183,19 @@ class Cartola extends Service
|
||||
'abonos' => $abonos,
|
||||
'saldo' => $saldo
|
||||
];
|
||||
$this->buildCartola($cuenta, new DateTimeImmutable($dia), $cartolaData);
|
||||
$this->buildCartola($cuenta, $dayDate, $cartolaData);
|
||||
}
|
||||
|
||||
$startDate = new DateTimeImmutable(min($fechas));
|
||||
$endDate = new DateTimeImmutable(max($fechas));
|
||||
try {
|
||||
$startDate = new DateTimeImmutable(min($fechas));
|
||||
} catch (DateMalformedStringException $exception) {
|
||||
throw new Read(__CLASS__, $exception);
|
||||
}
|
||||
try {
|
||||
$endDate = new DateTimeImmutable(max($fechas));
|
||||
} catch (DateMalformedStringException $exception) {
|
||||
throw new Read(__CLASS__, $exception);
|
||||
}
|
||||
$movimientosFaltantes = $this->movimientoService->findMissing($cuenta, $addedMovimientos, $startDate, $endDate);
|
||||
$movimientosObsoletos = [];
|
||||
if (count($movimientosFaltantes) > 0) {
|
||||
|
@ -10,8 +10,8 @@ class Itau extends Banco
|
||||
{
|
||||
use isExcel;
|
||||
|
||||
const CUENTA_CORRIENTE = 0;
|
||||
const ULTIMOS_MOVIMIENTOS = 1;
|
||||
const int CUENTA_CORRIENTE = 0;
|
||||
const int ULTIMOS_MOVIMIENTOS = 1;
|
||||
|
||||
public function processMovimientosDiarios(array $movimientos): array
|
||||
{
|
||||
|
@ -12,6 +12,9 @@ class MiIndicador implements Provider
|
||||
public function __construct(protected ClientInterface $client) {}
|
||||
|
||||
/**
|
||||
* @param string $money_symbol
|
||||
* @param DateTimeInterface|null $dateTime
|
||||
* @return float
|
||||
* @throws EmptyResponse
|
||||
*/
|
||||
public function get(string $money_symbol, ?DateTimeInterface $dateTime = null): float
|
||||
@ -33,7 +36,7 @@ class MiIndicador implements Provider
|
||||
$body = $response->getBody();
|
||||
$json = json_decode($body->getContents());
|
||||
|
||||
if (empty($json) or $json->codigo !== $money_symbol or count($json->serie) === 0) {
|
||||
if (empty($json) or !isset($json->codigo) or !isset($json->serie) or $json->codigo !== $money_symbol or count($json->serie) === 0) {
|
||||
throw new EmptyResponse($request_uri);
|
||||
}
|
||||
return $json->serie[0]->valor;
|
||||
|
Reference in New Issue
Block a user