Cartolas
This commit is contained in:
52
app/src/Controller/API/Contabilidad.php
Normal file
52
app/src/Controller/API/Contabilidad.php
Normal file
@ -0,0 +1,52 @@
|
||||
<?php
|
||||
namespace Incoviba\Controller\API;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Incoviba\Common\Implement\Exception\EmptyResult;
|
||||
use Incoviba\Repository;
|
||||
use Incoviba\Service;
|
||||
|
||||
class Contabilidad
|
||||
{
|
||||
use withJson;
|
||||
|
||||
public function procesarCartola(ServerRequestInterface $request, ResponseInterface $response,
|
||||
Repository\Inmobiliaria $inmobiliariaRepository,
|
||||
Repository\Banco $bancoRepository,
|
||||
Service\Cartola $cartolaService): ResponseInterface
|
||||
{
|
||||
$body = $request->getParsedBody();
|
||||
$output = [
|
||||
'input' => $body,
|
||||
'movimientos' => []
|
||||
];
|
||||
try {
|
||||
$inmobiliaria = $inmobiliariaRepository->fetchById($body['inmobiliaria']);
|
||||
$banco = $bancoRepository->fetchById($body['banco']);
|
||||
$mes = new DateTimeImmutable($body['mes']);
|
||||
$file = $request->getUploadedFiles()['file'];
|
||||
$output['movimientos'] = $cartolaService->process($inmobiliaria, $banco, $mes, $file);
|
||||
} catch (EmptyResult) {}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function exportarCartola(ServerRequestInterface $request, ResponseInterface $response,
|
||||
Repository\Inmobiliaria $inmobiliariaRepository,
|
||||
Repository\Banco $bancoRepository,
|
||||
Service\Cartola $cartolaService): ResponseInterface
|
||||
{
|
||||
$body = $request->getParsedBody();
|
||||
$output = [
|
||||
'input' => $body,
|
||||
'filename' => ''
|
||||
];
|
||||
try {
|
||||
$inmobiliaria = $inmobiliariaRepository->fetchById($body['inmobiliaria']);
|
||||
$banco = $bancoRepository->fetchById($body['banco']);
|
||||
$mes = new DateTimeImmutable($body['mes']);
|
||||
$output['filename'] = $cartolaService->export($inmobiliaria, $banco, $mes, json_decode($body['movimientos']));
|
||||
} catch (EmptyResult) {}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
25
app/src/Controller/API/Informes.php
Normal file
25
app/src/Controller/API/Informes.php
Normal file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
namespace Incoviba\Controller\API;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Incoviba\Service;
|
||||
|
||||
class Informes
|
||||
{
|
||||
use withJson;
|
||||
|
||||
public function diario(ServerRequestInterface $request, ResponseInterface $response, Service\Informes\Diario $diarioService): ResponseInterface
|
||||
{
|
||||
$body = $request->getParsedBody();
|
||||
$output = [
|
||||
'input' => $body,
|
||||
'data' => []
|
||||
];
|
||||
try {
|
||||
$file = $request->getUploadedFiles()['file'];
|
||||
$output['data'] = $diarioService->process($file);
|
||||
} catch (\Error) {}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
48
app/src/Controller/API/Inmobiliarias.php
Normal file
48
app/src/Controller/API/Inmobiliarias.php
Normal file
@ -0,0 +1,48 @@
|
||||
<?php
|
||||
namespace Incoviba\Controller\API;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Incoviba\Common\Implement\Exception\EmptyResult;
|
||||
use Incoviba\Model;
|
||||
use Incoviba\Repository;
|
||||
use Incoviba\Service;
|
||||
|
||||
class Inmobiliarias
|
||||
{
|
||||
use withJson;
|
||||
|
||||
public function cuentas(ServerRequestInterface $request, ResponseInterface $response,
|
||||
Repository\Inmobiliaria $inmobiliariaRepository,
|
||||
Repository\Inmobiliaria\Cuenta $cuentaRepository, int $inmobiliaria_rut): ResponseInterface
|
||||
{
|
||||
$output = [
|
||||
'inmobiliaria_rut' => $inmobiliaria_rut,
|
||||
'cuentas' => []
|
||||
];
|
||||
try {
|
||||
$inmobiliaria = $inmobiliariaRepository->fetchById($inmobiliaria_rut);
|
||||
$output['cuentas'] = $cuentaRepository->fetchByInmobiliaria($inmobiliaria->rut);
|
||||
} catch (EmptyResult) {}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function proyectos(ServerRequestInterface $request, ResponseInterface $response,
|
||||
Repository\Inmobiliaria $inmobiliariaRepository,
|
||||
Service\Proyecto $proyectoService, int $inmobiliaria_rut): ResponseInterface
|
||||
{
|
||||
$output = [
|
||||
'inmobiliaria_rut' => $inmobiliaria_rut,
|
||||
'proyectos' => []
|
||||
];
|
||||
try {
|
||||
$inmobiliaria = $inmobiliariaRepository->fetchById($inmobiliaria_rut);
|
||||
$output['proyectos'] = array_map(function(Model\Proyecto $proyecto) {
|
||||
$p = json_decode(json_encode($proyecto));
|
||||
$p->current_estado = $proyecto->currentEstado();
|
||||
$p->estados = $proyecto->estados();
|
||||
return $p;
|
||||
},$proyectoService->getByInmobiliaria($inmobiliaria->rut));
|
||||
} catch (EmptyResult) {}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
90
app/src/Controller/API/Nubox.php
Normal file
90
app/src/Controller/API/Nubox.php
Normal file
@ -0,0 +1,90 @@
|
||||
<?php
|
||||
namespace Incoviba\Controller\API;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Incoviba\Common\Implement\Exception\HttpResponse;
|
||||
use Incoviba\Service;
|
||||
|
||||
class Nubox
|
||||
{
|
||||
use withJson;
|
||||
|
||||
public function token(ServerRequestInterface $request, ResponseInterface $response,
|
||||
Service\Contabilidad\Nubox $nuboxService, int $inmobiliaria_rut): ResponseInterface
|
||||
{
|
||||
$output = [
|
||||
'inmobiliaria_rut' => $inmobiliaria_rut,
|
||||
'token' => ''
|
||||
];
|
||||
try {
|
||||
$output['token'] = $nuboxService->getToken($inmobiliaria_rut);
|
||||
} catch (HttpResponse $exception) {
|
||||
$output['error'] = [
|
||||
'code' => $exception->getCode(),
|
||||
'message' => $exception->getMessage()
|
||||
];
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function sistemas(ServerRequestInterface $request, ResponseInterface $response,
|
||||
Service\Contabilidad\Nubox $nuboxService, int $inmobiliaria_rut): ResponseInterface
|
||||
{
|
||||
$output = [
|
||||
'inmobiliaria_rut' => $inmobiliaria_rut,
|
||||
'sistemas' => []
|
||||
];
|
||||
try {
|
||||
$output['sistemas'] = $nuboxService->getSistemas($inmobiliaria_rut);
|
||||
} catch (HttpResponse $exception) {
|
||||
$output['error'] = [
|
||||
'code' => $exception->getCode(),
|
||||
'message' => $exception->getMessage()
|
||||
];
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function libroMayor(ServerRequestInterface $request, ResponseInterface $response,
|
||||
Service\Contabilidad\Nubox $nuboxService, int $inmobiliaria_rut): ResponseInterface
|
||||
{
|
||||
$body = $request->getParsedBody();
|
||||
$output = [
|
||||
'inmobiliaria_rut' => $inmobiliaria_rut,
|
||||
'input' => $body,
|
||||
'libro_mayor' => []
|
||||
];
|
||||
try {
|
||||
$from = new DateTimeImmutable($body['inicio']);
|
||||
$to = new DateTimeImmutable($body['termino']);
|
||||
$output['libro_mayor'] = $nuboxService->getLibroMayor($inmobiliaria_rut, $from, $to);
|
||||
} catch (HttpResponse $exception) {
|
||||
$output['error'] = [
|
||||
'code' => $exception->getCode(),
|
||||
'message' => $exception->getMessage()
|
||||
];
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function libroDiario(ServerRequestInterface $request, ResponseInterface $response,
|
||||
Service\Contabilidad\Nubox $nuboxService, int $inmobiliaria_rut): ResponseInterface
|
||||
{
|
||||
$body = $request->getParsedBody();
|
||||
$output = [
|
||||
'inmobiliaria_rut' => $inmobiliaria_rut,
|
||||
'input' => $body,
|
||||
'libro_diario' => []
|
||||
];
|
||||
try {
|
||||
$from = new DateTimeImmutable($body['inicio']);
|
||||
$to = new DateTimeImmutable($body['termino']);
|
||||
$output['libro_diario'] = $nuboxService->getLibroDiario($inmobiliaria_rut, $from, $to);
|
||||
} catch (HttpResponse $exception) {
|
||||
$output['error'] = [
|
||||
'code' => $exception->getCode(),
|
||||
'message' => $exception->getMessage()
|
||||
];
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
@ -155,4 +155,19 @@ class Proyectos
|
||||
} catch (EmptyResult) {}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function tiposUnidades(ServerRequestInterface $request, ResponseInterface $response,
|
||||
Repository\Proyecto $proyectoRepository,
|
||||
Repository\Proyecto\TipoUnidad $tipoUnidadRepository,
|
||||
int $proyecto_id): ResponseInterface
|
||||
{
|
||||
$output = [
|
||||
'proyecto_id' => $proyecto_id,
|
||||
'tipos' => []
|
||||
];
|
||||
try {
|
||||
$proyecto = $proyectoRepository->fetchById($proyecto_id);
|
||||
$output['tipos'] = $tipoUnidadRepository->fetchByProyecto($proyecto->id);
|
||||
} catch (EmptyResult) {}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
||||
|
@ -21,4 +21,12 @@ class CentrosCostos
|
||||
return $view->render($response, 'contabilidad.centros_costos', compact('centrosCostos',
|
||||
'tiposCentros', 'categorias', 'tiposCuentas'));
|
||||
}
|
||||
public function asignar(ServerRequestInterface $request, ResponseInterface $response, View $view,
|
||||
Repository\CentroCosto $centroCostoRepository,
|
||||
Repository\Inmobiliaria $inmobiliariaRepository): ResponseInterface
|
||||
{
|
||||
$centrosCostos = $centroCostoRepository->fetchAll();
|
||||
$inmobiliarias = $inmobiliariaRepository->fetchAllActive('razon');
|
||||
return $view->render($response, 'contabilidad.centros_costos.asignar', compact('centrosCostos', 'inmobiliarias'));
|
||||
}
|
||||
}
|
||||
|
18
app/src/Model/Nubox.php
Normal file
18
app/src/Model/Nubox.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
namespace Incoviba\Model;
|
||||
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Common\Ideal;
|
||||
|
||||
class Nubox extends Ideal\Model
|
||||
{
|
||||
public Inmobiliaria $inmobiliaria;
|
||||
public string $alias;
|
||||
public string $usuario;
|
||||
public string $password;
|
||||
|
||||
public function getLogin(): string
|
||||
{
|
||||
return base64_encode(implode(':', [$this->usuario, $this->password]));
|
||||
}
|
||||
}
|
@ -46,4 +46,20 @@ class Inmobiliaria extends Ideal\Repository
|
||||
{
|
||||
return $this->update($model, ['dv', 'razon', 'abreviacion', 'cuenta', 'banco', 'sociedad'], $new_data);
|
||||
}
|
||||
|
||||
public function fetchAllActive(null|string|array $sorting = null): array
|
||||
{
|
||||
$query = $this->connection->getQueryBuilder()
|
||||
->select('a.*')
|
||||
->from("{$this->getTable()} a")
|
||||
->joined('JOIN proyecto ON a.rut = proyecto.inmobiliaria')
|
||||
->joined('JOIN (SELECT ep1.* FROM estado_proyecto ep1 JOIN (SELECT MAX(id) AS id, proyecto FROM estado_proyecto GROUP BY proyecto) ep0 ON ep0.id = ep1.id) ep ON ep.proyecto = proyecto.id')
|
||||
->joined('JOIN tipo_estado_proyecto tep ON tep.id = ep.estado')
|
||||
->joined('JOIN etapa_proyecto ON etapa_proyecto.id = tep.etapa')
|
||||
->where('etapa_proyecto.orden BETWEEN ? AND ?');
|
||||
if ($sorting !== null) {
|
||||
$query->order($sorting);
|
||||
}
|
||||
return $this->fetchMany($query, [1, 8]);
|
||||
}
|
||||
}
|
||||
|
52
app/src/Repository/Inmobiliaria/Cuenta.php
Normal file
52
app/src/Repository/Inmobiliaria/Cuenta.php
Normal file
@ -0,0 +1,52 @@
|
||||
<?php
|
||||
namespace Incoviba\Repository\Inmobiliaria;
|
||||
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Common\Ideal;
|
||||
use Incoviba\Repository;
|
||||
use Incoviba\Model;
|
||||
use Incoviba\Common\Implement;
|
||||
|
||||
class Cuenta extends Ideal\Repository
|
||||
{
|
||||
public function __construct(Define\Connection $connection,
|
||||
protected Repository\Inmobiliaria $inmobiliariaRepository,
|
||||
protected Repository\Banco $bancoRepository)
|
||||
{
|
||||
parent::__construct($connection);
|
||||
$this->setTable('cuenta');
|
||||
}
|
||||
|
||||
public function create(?array $data = null): Model\Inmobiliaria\Cuenta
|
||||
{
|
||||
$map = (new Implement\Repository\MapperParser(['cuenta']))
|
||||
->register('inmobiliaria', (new Implement\Repository\Mapper())
|
||||
->setFunction(function(array $data) {
|
||||
return $this->inmobiliariaRepository->fetchById($data['inmobiliaria']);
|
||||
}))
|
||||
->register('banco', (new Implement\Repository\Mapper())
|
||||
->setFunction(function(array $data) {
|
||||
return $this->bancoRepository->fetchById($data['banco']);
|
||||
}));
|
||||
return $this->parseData(new Model\Inmobiliaria\Cuenta(), $data, $map);
|
||||
}
|
||||
public function save(Define\Model $model): Model\Inmobiliaria\Cuenta
|
||||
{
|
||||
$model->id = $this->saveNew(['inmobiliaria', 'banco', 'cuenta'],
|
||||
[$model->inmobiliaria->rut, $model->banco->id, $model->cuenta]);
|
||||
return $model;
|
||||
}
|
||||
public function edit(Define\Model $model, array $new_data): Model\Inmobiliaria\Cuenta
|
||||
{
|
||||
return $this->update($model, ['inmobiliaria', 'banco', 'cuenta'], $new_data);
|
||||
}
|
||||
|
||||
public function fetchByInmobiliaria(int $inmobiliaria_rut): array
|
||||
{
|
||||
$query = $this->connection->getQueryBuilder()
|
||||
->select()
|
||||
->from($this->getTable())
|
||||
->where('inmobiliaria = ?');
|
||||
return $this->fetchMany($query, [$inmobiliaria_rut]);
|
||||
}
|
||||
}
|
55
app/src/Repository/Nubox.php
Normal file
55
app/src/Repository/Nubox.php
Normal file
@ -0,0 +1,55 @@
|
||||
<?php
|
||||
namespace Incoviba\Repository;
|
||||
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Common\Ideal;
|
||||
use Incoviba\Common\Implement;
|
||||
use Incoviba\Model;
|
||||
|
||||
class Nubox extends Ideal\Repository
|
||||
{
|
||||
public function __construct(Define\Connection $connection, protected Inmobiliaria $inmobiliariaRepository)
|
||||
{
|
||||
parent::__construct($connection);
|
||||
$this->setTable('inmobiliarias_nubox');
|
||||
}
|
||||
|
||||
public function create(?array $data = null): Model\Nubox
|
||||
{
|
||||
$map = (new Implement\Repository\MapperParser(['usuario', 'alias']))
|
||||
->register('inmobiliaria_rut', (new Implement\Repository\Mapper())
|
||||
->setProperty('inmobiliaria')
|
||||
->setFunction(function(array $data) {
|
||||
return $this->inmobiliariaRepository->fetchById($data['inmobiliaria_rut']);
|
||||
}))
|
||||
->register('contraseña', (new Implement\Repository\Mapper())
|
||||
->setProperty('password'));
|
||||
return $this->parseData(new Model\Nubox(), $data, $map);
|
||||
}
|
||||
public function save(Define\Model $model): Model\Nubox
|
||||
{
|
||||
$this->saveNew(
|
||||
['inmobiliaria_rut', 'alias', 'usuario', 'contraseña'],
|
||||
[$model->inmobiliaria->rut, $model->alias, $model->usuario, $model->password]
|
||||
);
|
||||
return $model;
|
||||
}
|
||||
public function edit(Define\Model $model, array $new_data): Model\Nubox
|
||||
{
|
||||
return $this->update($model, ['inmobiliaria_rut', 'alias', 'usuario', 'contraseña'], $new_data);
|
||||
}
|
||||
|
||||
public function fetchByInmobiliaria(int $inmobiliaria_rut): Model\Nubox
|
||||
{
|
||||
$query = $this->connection->getQueryBuilder()
|
||||
->select()
|
||||
->from($this->getTable())
|
||||
->where('inmobiliaria_rut = ?');
|
||||
return $this->fetchOne($query, [$inmobiliaria_rut]);
|
||||
}
|
||||
|
||||
protected function getKey(): string
|
||||
{
|
||||
return 'inmobiliaria_rut';
|
||||
}
|
||||
}
|
29
app/src/Service/Cartola.php
Normal file
29
app/src/Service/Cartola.php
Normal file
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
namespace Incoviba\Service;
|
||||
|
||||
use DateTimeInterface;
|
||||
use Psr\Http\Message\StreamFactoryInterface;
|
||||
use Psr\Http\Message\UploadedFileInterface;
|
||||
use Incoviba\Common\Define\Cartola\Banco;
|
||||
use Incoviba\Common\Define\Contabilidad\Exporter;
|
||||
use Incoviba\Model;
|
||||
|
||||
class Cartola
|
||||
{
|
||||
public function __construct(protected StreamFactoryInterface $streamFactory, protected Exporter $exporter) {}
|
||||
|
||||
protected array $bancos;
|
||||
public function register(string $name, Banco $banco): Cartola
|
||||
{
|
||||
$this->bancos[$name] = $banco;
|
||||
return $this;
|
||||
}
|
||||
public function process(Model\Inmobiliaria $inmobiliaria, Model\Banco $banco, DateTimeInterface $mes, UploadedFileInterface $file): array
|
||||
{
|
||||
return $this->bancos[strtolower($banco->nombre)]->process($file);
|
||||
}
|
||||
public function export(Model\Inmobiliaria $inmobiliaria, Model\Banco $banco, DateTimeInterface $mes, array $movimientos): string
|
||||
{
|
||||
return $this->exporter->export($inmobiliaria, $banco, $mes, $movimientos);
|
||||
}
|
||||
}
|
54
app/src/Service/Cartola/Security.php
Normal file
54
app/src/Service/Cartola/Security.php
Normal file
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
namespace Incoviba\Service\Cartola;
|
||||
|
||||
use Psr\Http\Message\UploadedFileInterface;
|
||||
use PhpOffice\PhpSpreadsheet;
|
||||
use Incoviba\Common\Define\Cartola\Banco;
|
||||
|
||||
class Security implements Banco
|
||||
{
|
||||
public function process(UploadedFileInterface $file): array
|
||||
{
|
||||
$reader = PhpSpreadsheet\IOFactory::createReader('Xls');
|
||||
$filename = '/tmp/cartola.xls';
|
||||
$file->moveTo($filename);
|
||||
$xlsx = $reader->load($filename);
|
||||
$worksheet = $xlsx->getActiveSheet();
|
||||
$rows = $worksheet->getRowIterator();
|
||||
$dataFound = false;
|
||||
$columns = [];
|
||||
$data = [];
|
||||
foreach ($rows as $row) {
|
||||
$cells = $row->getCellIterator();
|
||||
$rowData = [];
|
||||
foreach ($cells as $cell) {
|
||||
if ($cell->getColumn() === 'A' and $cell->getCalculatedValue() === "fecha ") {
|
||||
$cols = $row->getColumnIterator();
|
||||
foreach ($cols as $col) {
|
||||
$columns[$col->getColumn()] = trim($col->getCalculatedValue());
|
||||
}
|
||||
$dataFound = true;
|
||||
break;
|
||||
}
|
||||
if ($cell->getColumn() === 'A' and $cell->getCalculatedValue() === null) {
|
||||
$dataFound = false;
|
||||
break;
|
||||
}
|
||||
if (!$dataFound) {
|
||||
break;
|
||||
}
|
||||
$col = $columns[$cell->getColumn()];
|
||||
$value = $cell->getCalculatedValue();
|
||||
if ($col === 'fecha') {
|
||||
$value = PhpSpreadsheet\Shared\Date::excelToDateTimeObject($cell->getValue(), 'America/Santiago')->format('Y-m-d');
|
||||
}
|
||||
$rowData[$col] = $value;
|
||||
}
|
||||
if (count($rowData) > 0) {
|
||||
$data []= $rowData;
|
||||
}
|
||||
}
|
||||
unlink($filename);
|
||||
return $data;
|
||||
}
|
||||
}
|
115
app/src/Service/Contabilidad/Exporter/Nubox.php
Normal file
115
app/src/Service/Contabilidad/Exporter/Nubox.php
Normal file
@ -0,0 +1,115 @@
|
||||
<?php
|
||||
namespace Incoviba\Service\Contabilidad\Exporter;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use DateTimeInterface;
|
||||
use Incoviba\Common\Define\Contabilidad\Exporter;
|
||||
use Incoviba\Model;
|
||||
use Incoviba\Repository;
|
||||
use PhpOffice\PhpSpreadsheet;
|
||||
|
||||
class Nubox implements Exporter
|
||||
{
|
||||
public function __construct(protected Repository\CentroCosto $centroCostoRepository, protected string $uploadFolder) {}
|
||||
|
||||
public function export(Model\Inmobiliaria $inmobiliaria, Model\Banco $banco, DateTimeInterface $mes, array $movimientos): string
|
||||
{
|
||||
PhpSpreadsheet\Settings::setLocale('es-CL');
|
||||
$workbook = new PhpSpreadsheet\Spreadsheet();
|
||||
$sheet = $workbook->getActiveSheet();
|
||||
|
||||
$rowIndex = $this->buildHeaders($sheet);
|
||||
|
||||
foreach ($movimientos as $movimiento) {
|
||||
$tipoCentro = '';
|
||||
$cuenta = '';
|
||||
$centro = '';
|
||||
if ($movimiento->centro_costo !== '') {
|
||||
$centroCosto = $this->centroCostoRepository->fetchById($movimiento->centro_costo);
|
||||
$tipoCentro = substr($centroCosto->tipoCentro->descripcion, 0, 1);
|
||||
$cuenta = $centroCosto->cuentaContable;
|
||||
$centro = $centroCosto->id;
|
||||
}
|
||||
$fecha = (new DateTimeImmutable($movimiento->fecha))->format('d/m/Y');
|
||||
$rowIndex = $this->add($sheet, [
|
||||
'Número' => '0',
|
||||
'Tipo' => $tipoCentro,
|
||||
'Fecha' => $fecha,
|
||||
'Glosa' => $movimiento->detalle,
|
||||
'Cuenta Detalle' => $cuenta,
|
||||
'Glosa Detalle' => '',
|
||||
'Centro Costo' => $centro,
|
||||
'Sucursal' => '',
|
||||
'Debe' => $movimiento->abono === 0 ? '' : $movimiento->abono,
|
||||
'Haber' => $movimiento->cargo === 0 ? '' : $movimiento->cargo,
|
||||
'Tipo Auxiliar' => 'B',
|
||||
'A: Rut Cliente-Proveedor/H: Rut Prestador' => '',
|
||||
'A: Razon Social/B: Descripción Movimiento Bancario/ H: Nombre Prestador' => $movimiento->glosa,
|
||||
'A: Tipo De Documento/H: Tipo De Boleta Honorario' => '',
|
||||
'A: Folio /B: Numero Documento/H: Folio Boleta' => $movimiento->documento,
|
||||
'A/B/H: Monto' => ($movimiento->abono === 0) ? $movimiento->cargo : $movimiento->abono,
|
||||
'A: Fecha Vencimiento /B: Fecha /H: Fecha Emisión (DD/MM/AAAA)' => $fecha
|
||||
], $rowIndex);
|
||||
}
|
||||
$sheet->getStyle("I1:J{$rowIndex}")->getNumberFormat()
|
||||
->setFormatCode('#,##0');
|
||||
$sheet->getStyle("O1:O{$rowIndex}")->getNumberFormat()
|
||||
->setFormatCode('##0');
|
||||
$sheet->getStyle("P1:P{$rowIndex}")->getNumberFormat()
|
||||
->setFormatCode('#,##0');
|
||||
foreach (range('A', 'Q') as $col) {
|
||||
$sheet->getColumnDimension($col)->setAutoSize(true);
|
||||
}
|
||||
$sheet->getSheetView()->setZoomScale(90);
|
||||
|
||||
$writer = PhpSpreadsheet\IOFactory::createWriter($workbook, 'Xlsx');
|
||||
$filename = "Cartola {$banco->nombre} - {$inmobiliaria->abreviacion} - {$mes->format('M Y')}.xlsx";
|
||||
$writer->save(implode(DIRECTORY_SEPARATOR, [
|
||||
$this->uploadFolder,
|
||||
$filename
|
||||
]));
|
||||
return $filename;
|
||||
}
|
||||
|
||||
protected function getHeaders(): array
|
||||
{
|
||||
return [
|
||||
'Número',
|
||||
'Tipo',
|
||||
'Fecha',
|
||||
'Glosa',
|
||||
'Cuenta Detalle',
|
||||
'Glosa Detalle',
|
||||
'Centro Costo',
|
||||
'Sucursal',
|
||||
'Debe',
|
||||
'Haber',
|
||||
'Tipo Auxiliar',
|
||||
'A: Rut Cliente-Proveedor/H: Rut Prestador',
|
||||
'A: Razon Social/B: Descripción Movimiento Bancario/ H: Nombre Prestador',
|
||||
'A: Tipo De Documento/H: Tipo De Boleta Honorario',
|
||||
'A: Folio /B: Numero Documento/H: Folio Boleta',
|
||||
'A/B/H: Monto',
|
||||
'A: Fecha Vencimiento /B: Fecha /H: Fecha Emisión (DD/MM/AAAA)'
|
||||
];
|
||||
}
|
||||
protected function buildHeaders(PhpSpreadsheet\Worksheet\Worksheet &$sheet, int $rowIndex = 1): int
|
||||
{
|
||||
$map = $this->getHeaders();
|
||||
foreach ($map as $index => $header) {
|
||||
$columnIndex = $index + 1;
|
||||
$sheet->getCell([$columnIndex, $rowIndex])->setValue($header);
|
||||
}
|
||||
return $rowIndex + 1;
|
||||
}
|
||||
protected function add(PhpSpreadsheet\Worksheet\Worksheet &$sheet, array $row, int $rowIndex): int
|
||||
{
|
||||
$headers = $this->getHeaders();
|
||||
foreach ($headers as $index => $header) {
|
||||
$columnIndex = $index + 1;
|
||||
$sheet->getCell([$columnIndex, $rowIndex])->setValue($row[$header]);
|
||||
$sheet->getCell([$columnIndex, $rowIndex + 1])->setValue(($header === 'Tipo Auxiliar') ? 'A' : '');
|
||||
}
|
||||
return $rowIndex + 2;
|
||||
}
|
||||
}
|
139
app/src/Service/Contabilidad/Nubox.php
Normal file
139
app/src/Service/Contabilidad/Nubox.php
Normal file
@ -0,0 +1,139 @@
|
||||
<?php
|
||||
namespace Incoviba\Service\Contabilidad;
|
||||
|
||||
use DateTimeInterface;
|
||||
use Psr\Http\Client\ClientInterface;
|
||||
use Psr\Http\Message\RequestFactoryInterface;
|
||||
use Incoviba\Common\Implement\Exception;
|
||||
use Incoviba\Repository;
|
||||
use Incoviba\Service;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\StreamInterface;
|
||||
use function Symfony\Component\Translation\t;
|
||||
|
||||
class Nubox
|
||||
{
|
||||
public function __construct(protected Repository\Nubox $nuboxRepository,
|
||||
protected Service\Redis $redisService,
|
||||
protected ClientInterface $client,
|
||||
protected RequestFactoryInterface $requestFactory,
|
||||
protected string $api_url) {}
|
||||
|
||||
protected array $tokens;
|
||||
public function getToken(int $inmobiliaria_rut): string
|
||||
{
|
||||
if (!isset($this->tokens[$inmobiliaria_rut])) {
|
||||
$redisKey = "token_nubox:{$inmobiliaria_rut}";
|
||||
try {
|
||||
$this->tokens[$inmobiliaria_rut] = $this->redisService->get($redisKey);
|
||||
} catch (Exception\EmptyRedis) {
|
||||
$nubox = $this->nuboxRepository->fetchByInmobiliaria($inmobiliaria_rut);
|
||||
$request = $this->requestFactory
|
||||
->createRequest('POST', implode('/', [$this->api_url, 'autenticar']))
|
||||
->withHeader('Authorization', "Basic {$nubox->getLogin()}")
|
||||
->withHeader('Content-Type', 'application/json')
|
||||
->withHeader('Accept', 'application/json');
|
||||
$response = $this->client->sendRequest($request);
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception\HttpResponse($response->getReasonPhrase(), $response->getStatusCode());
|
||||
}
|
||||
$sistemas = json_decode($response->getBody()->getContents(), JSON_OBJECT_AS_ARRAY);
|
||||
|
||||
$this->setToken($inmobiliaria_rut, $response->getHeaderLine('Token'))
|
||||
->setSistemas($inmobiliaria_rut, $sistemas['Sistemas']);
|
||||
}
|
||||
}
|
||||
return $this->tokens[$inmobiliaria_rut];
|
||||
}
|
||||
public function setToken(int $inmobiliaria_rut, string $token): Nubox
|
||||
{
|
||||
$this->tokens[$inmobiliaria_rut] = $token;
|
||||
|
||||
$redisKey = "token_nubox:{$inmobiliaria_rut}";
|
||||
$this->redisService->set($redisKey, $this->tokens[$inmobiliaria_rut], 60 * 15);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected array $sistemas;
|
||||
public function getSistemas(int $inmobiliaria_rut): array
|
||||
{
|
||||
if (!isset($this->sistemas[$inmobiliaria_rut])) {
|
||||
$redisKey = "nubox:{$inmobiliaria_rut}";
|
||||
$this->sistemas[$inmobiliaria_rut] = json_decode($this->redisService->get($redisKey));
|
||||
}
|
||||
return $this->sistemas[$inmobiliaria_rut];
|
||||
}
|
||||
public function setSistemas(int $inmobiliaria_rut, array $sistemas): Nubox
|
||||
{
|
||||
$this->sistemas[$inmobiliaria_rut] = $sistemas;
|
||||
|
||||
$redisKey = "nubox:{$inmobiliaria_rut}";
|
||||
$this->redisService->set($redisKey, json_encode($sistemas));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getLibroMayor(int $inmobiliaria_rut, DateTimeInterface $from, DateTimeInterface $to): array
|
||||
{
|
||||
$inmobiliaria = $this->nuboxRepository->fetchByInmobiliaria($inmobiliaria_rut);
|
||||
$query = [
|
||||
'NumeroSerie' => 1,
|
||||
'CodigoEmpresa' => $inmobiliaria->alias,
|
||||
'DDInicio' => $from->format('j'),
|
||||
'MMInicio' => $from->format('n'),
|
||||
'YYInicio' => $from->format('Y'),
|
||||
'DDTermino' => $to->format('j'),
|
||||
'MMTermino' => $to->format('n'),
|
||||
'YYTermino' => $to->format('Y'),
|
||||
'CodigoCentroDeCosto' => 0,
|
||||
'CodigoSucursal' => 0,
|
||||
'CodigoCuenta' => 0,
|
||||
'ModoIFRS' => 'false',
|
||||
'CuentasConSaldo' => 'false',
|
||||
'IncluirCodigoCuenta' => 'true',
|
||||
];
|
||||
$uri = 'contabilidad/libro-mayor?' . http_build_query($query);
|
||||
$response = $this->send($uri, $inmobiliaria_rut);
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception\HttpResponse($response->getReasonPhrase(), $response->getStatusCode());
|
||||
}
|
||||
return json_decode($response->getBody()->getContents(), JSON_OBJECT_AS_ARRAY);
|
||||
}
|
||||
public function getLibroDiario(int $inmobiliaria_rut, DateTimeInterface $from, DateTimeInterface $to): array
|
||||
{
|
||||
$inmobiliaria = $this->nuboxRepository->fetchByInmobiliaria($inmobiliaria_rut);
|
||||
$query = [
|
||||
'ModoIFRS' => 0,
|
||||
'DDInicio' => $from->format('j'),
|
||||
'MMInicio' => $from->format('n'),
|
||||
'YYInicio' => $from->format('Y'),
|
||||
'DDTermino' => $to->format('j'),
|
||||
'MMTermino' => $to->format('n'),
|
||||
'YYTermino' => $to->format('Y'),
|
||||
'Sucursal' => 0,
|
||||
'codigoEmpresa' => $inmobiliaria->alias,
|
||||
'NumeroSerie' => 1,
|
||||
'CuentasConSaldo' => 0,
|
||||
'incluirCodigoCuenta' => 1
|
||||
];
|
||||
$uri = 'contabilidad/libro-diario?' . http_build_query($query);
|
||||
$response = $this->send($uri, $inmobiliaria_rut);
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
error_log(var_export($uri,true).PHP_EOL,3,'/logs/debug');
|
||||
throw new Exception\HttpResponse($response->getReasonPhrase(), $response->getStatusCode());
|
||||
}
|
||||
return json_decode($response->getBody()->getContents(), JSON_OBJECT_AS_ARRAY);
|
||||
}
|
||||
|
||||
private function send(string $uri, int $inmobiliaria_rut, string $method = 'GET', ?StreamInterface $body = null): ResponseInterface
|
||||
{
|
||||
$request = $this->requestFactory
|
||||
->createRequest($method, implode('/', [$this->api_url, $uri]))
|
||||
->withHeader('token', $this->getToken($inmobiliaria_rut));
|
||||
if ($body !== null) {
|
||||
$request = $request->withBody($body);
|
||||
}
|
||||
return $this->client->sendRequest($request);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user