Subir cartolas procesadas

This commit is contained in:
Juan Pablo Vial
2024-07-17 22:33:33 -04:00
parent 390e79ad6d
commit 8e7f43e487
48 changed files with 1692 additions and 530 deletions

View File

@ -5,10 +5,14 @@ use DateTimeImmutable;
use Incoviba\Common\Ideal\Controller;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Controller\API\withJson;
use Incoviba\Model\Contabilidad\Banco;
use Incoviba\Model\Inmobiliaria;
use Incoviba\Repository;
use Incoviba\Service;
use PhpParser\Node\Stmt\TryCatch;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface;
class Cartolas extends Controller
{
@ -95,4 +99,60 @@ class Cartolas extends Controller
} catch (EmptyResult) {}
return $this->withJson($response, $output);
}
public function importar(ServerRequestInterface $request, ResponseInterface $response,
LoggerInterface $logger,
Repository\Inmobiliaria $inmobiliariaRepository,
Repository\Contabilidad\Banco $bancoRepository,
Repository\Inmobiliaria\Cuenta $cuentaRepository,
Service\Contabilidad\Cartola $cartolaService,
Service\Contabilidad\Movimiento $movimientoService): ResponseInterface
{
$body = $request->getParsedBody();
$files = $request->getUploadedFiles();
$output = [
'input' => $body,
'movimientos' => []
];
if (is_array($files['file'])) {
foreach ($files['file'] as $i => $file) {
if ($file->getError() !== UPLOAD_ERR_OK) {
continue;
}
try {
$inmobiliaria = $inmobiliariaRepository->fetchById($body['sociedad_rut'][$i]);
$banco = $bancoRepository->fetchById($body['banco_id'][$i]);
$movimientos = $cartolaService->process($inmobiliaria, $banco, new DateTimeImmutable($body['mes'][$i]), $file);
$cuenta = $cuentaRepository->fetchByInmobiliariaAndBanco($inmobiliaria->rut, $banco->id);
$this->addMovimientos($movimientoService, $cuenta, $movimientos);
$output['movimientos'] = array_merge($output['movimientos'], $movimientos);
} catch (EmptyResult) {}
}
} else {
try {
$inmobiliaria = $inmobiliariaRepository->fetchById($body['sociedad_rut']);
$banco = $bancoRepository->fetchById($body['banco_id']);
$movimientos = $cartolaService->process($inmobiliaria, $banco, new DateTimeImmutable($body['mes']), $files['file']);
$cuenta = $cuentaRepository->fetchByInmobiliariaAndBanco($inmobiliaria->rut, $banco->id);
$this->addMovimientos($movimientoService, $cuenta, $movimientos);
$output['movimientos'] = $movimientos;
} 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;
$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'] = preg_replace('/\D+/', '', $rut);
$dataMovimiento['digito'] = $digito;
}
$movimientoService->add($dataMovimiento);
}
}
}

View File

@ -23,29 +23,30 @@ class Movimientos extends Ideal\Controller
'movimiento_id' => $movimiento_id,
'input' => $body,
'status' => false,
'movimiento' => null,
'centro' => null,
'detalle' => ''
'movimiento' => null
];
try {
$movimiento = $movimientoService->getById($movimiento_id);
$output['movimiento'] = $this->movimientosToArray([$movimiento])[0];
$data = [];
if (isset($body['centro_id'])) {
$centro = $centroCostoRepository->fetchById($body['centro_id']);
$data['centro_costo_id'] = $centro->id;
}
if (isset($body['detalle'])) {
$data['detalle'] = $body['detalle'];
$fieldMap = [
'centro_costo_id',
'categoria',
'detalle',
'rut',
'digito',
'nombres',
'identificador'
];
foreach ($fieldMap as $field) {
if (key_exists($field, $body)) {
$data[$field] = $body[$field];
}
}
$movimientoService->setDetalles($movimiento, $data);
$output['movimiento'] = $this->movimientosToArray([$movimientoService->getById($movimiento->id)])[0];
if (isset($body['centro_id'])) {
$output['centro'] = $centro;
}
if (isset($body['detalle'])) {
$output['detalle'] = $body['detalle'];
}
$output['status'] = true;
} catch (EmptyResult) {}
return $this->withJson($response, $output);
}

View File

@ -0,0 +1,29 @@
<?php
namespace Incoviba\Controller\API\Contabilidad;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface;
use Incoviba\Controller\API\withJson;
use Incoviba\Service;
class Tesoreria
{
use withJson;
public function import(ServerRequestInterface $request, ResponseInterface $response, LoggerInterface $logger,
Service\Contabilidad\Informe\Tesoreria $tesoreriaService): ResponseInterface
{
$files = $request->getUploadedFiles();
$output = [];
foreach ($files['file'] as $file) {
if ($file->getError() !== UPLOAD_ERR_OK) {
continue;
}
$data = $tesoreriaService->getInput()->loadFromExcel($file);
$tesoreriaService->getInput()->load($data);
$output['informes'] []= $data;
}
return $this->withJson($response, $output);
}
}