Cartolas
This commit is contained in:
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