From a33dd341cd15bb85577534f9cbcf842508dee102 Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Mon, 15 Apr 2024 20:13:15 -0400 Subject: [PATCH] Facturacion --- .../views/ventas/facturacion.blade.php | 2 +- .../views/ventas/facturacion/show.blade.php | 4 +- app/src/Controller/API/CentrosCostos.php | 62 +++++++++++++ app/src/Controller/API/Nubox.php | 90 +++++++++++++++++++ app/src/Controller/CentrosCostos.php | 32 +++++++ app/src/Controller/Ventas/Facturacion.php | 10 ++- app/src/Service/Contabilidad/Nubox.php | 29 ++++++ app/src/Service/Proyecto/Terreno.php | 36 ++++++++ 8 files changed, 259 insertions(+), 6 deletions(-) create mode 100644 app/src/Controller/API/CentrosCostos.php create mode 100644 app/src/Controller/API/Nubox.php create mode 100644 app/src/Controller/CentrosCostos.php create mode 100644 app/src/Service/Proyecto/Terreno.php diff --git a/app/resources/views/ventas/facturacion.blade.php b/app/resources/views/ventas/facturacion.blade.php index d0a09d4..6bc5676 100644 --- a/app/resources/views/ventas/facturacion.blade.php +++ b/app/resources/views/ventas/facturacion.blade.php @@ -177,7 +177,7 @@ const cantidad = this.unidades.length this.unidades.forEach(unidad => { const values = [ - venta, + '' + venta + '', cantidad, unidad.tipo, unidad.descripcion, diff --git a/app/resources/views/ventas/facturacion/show.blade.php b/app/resources/views/ventas/facturacion/show.blade.php index e8f7c03..8eb0763 100644 --- a/app/resources/views/ventas/facturacion/show.blade.php +++ b/app/resources/views/ventas/facturacion/show.blade.php @@ -184,8 +184,8 @@ totales: {}, proporcion: 1, precio: {{$UF->transform($venta->currentEstado()->fecha, $venta->valor)}}, - terreno: {{(isset($venta->proyecto()->terreno->fecha) and $venta->proyecto()->terreno->fecha >= $lastDic) ? - $IPC->readjust($venta->proyecto()->terreno->valor, $venta->proyecto()->terreno->fecha, $venta->currentEstado()->fecha) : 0}}, + terreno: {{(isset($terreno->fecha) and $terreno->fecha >= $lastDic) ? + $IPC->readjust($terreno->valor, $terreno->fecha, $venta->currentEstado()->fecha) : 0}}, uf: {{$UF->get($venta->currentEstado()->fecha)}}, unidades: JSON.parse('{!! json_encode(array_map(function(Incoviba\Model\Venta\PropiedadUnidad $unidad) use ($venta, $UF, $format) { $precio = ($unidad->valor > 0) ? $unidad->valor : ($unidad->precio($venta->currentEstado()->fecha) ? $unidad->precio($venta->currentEstado()->fecha)->valor : 0); diff --git a/app/src/Controller/API/CentrosCostos.php b/app/src/Controller/API/CentrosCostos.php new file mode 100644 index 0000000..f1466b5 --- /dev/null +++ b/app/src/Controller/API/CentrosCostos.php @@ -0,0 +1,62 @@ +getParsedBody(); + $output = [ + 'input' => $body, + 'added' => false + ]; + try { + $centroCosto = $centroCostoRepository->create($body); + $centroCosto->id = $body['id']; + $centroCostoRepository->save($centroCosto); + $output['added'] = true; + } catch (EmptyResult) {} + return $this->withJson($response, $output); + } + public function edit(ServerRequestInterface $request, ResponseInterface $response, + Repository\CentroCosto $centroCostoRepository, int $centro_costo_id): ResponseInterface + { + $body = $request->getParsedBody(); + $output = [ + 'centro_costo_id' => $centro_costo_id, + 'input' => $body, + 'edited' => false + ]; + try { + $centroCosto = $centroCostoRepository->fetchById($centro_costo_id); + if ($body['tipo_cuenta_id'] === '') { + $body['tipo_cuenta_id'] = null; + } + $centroCostoRepository->edit($centroCosto, $body); + $output['edited'] = true; + } catch (EmptyResult) {} + return $this->withJson($response, $output); + } + public function remove(ServerRequestInterface $request, ResponseInterface $response, + Repository\CentroCosto $centroCostoRepository, int $centro_costo_id): ResponseInterface + { + $output = [ + 'centro_costo_id' => $centro_costo_id, + 'removed' => false + ]; + try { + $centroCosto = $centroCostoRepository->fetchById($centro_costo_id); + $centroCostoRepository->remove($centroCosto); + $output['removed'] = true; + } catch (EmptyResult) {} + return $this->withJson($response, $output); + } +} diff --git a/app/src/Controller/API/Nubox.php b/app/src/Controller/API/Nubox.php new file mode 100644 index 0000000..cc1d323 --- /dev/null +++ b/app/src/Controller/API/Nubox.php @@ -0,0 +1,90 @@ + $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); + } +} diff --git a/app/src/Controller/CentrosCostos.php b/app/src/Controller/CentrosCostos.php new file mode 100644 index 0000000..6854b8b --- /dev/null +++ b/app/src/Controller/CentrosCostos.php @@ -0,0 +1,32 @@ +fetchAll(); + $tiposCentros = $tipoCentroRepository->fetchAll(); + $categorias = $categoriaCentroRepository->fetchAll('descripcion'); + $tiposCuentas = $tipoCuentaRepository->fetchAll(); + 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')); + } +} diff --git a/app/src/Controller/Ventas/Facturacion.php b/app/src/Controller/Ventas/Facturacion.php index 90581a9..74ea55e 100644 --- a/app/src/Controller/Ventas/Facturacion.php +++ b/app/src/Controller/Ventas/Facturacion.php @@ -8,14 +8,18 @@ use Incoviba\Service; class Facturacion { - public function __invoke(ServerRequestInterface $request, ResponseInterface $response, View $view, Service\Proyecto $proyectoService): ResponseInterface + public function __invoke(ServerRequestInterface $request, ResponseInterface $response, View $view, + Service\Proyecto $proyectoService): ResponseInterface { $proyectos = $proyectoService->getEscriturando(); return $view->render($response, 'ventas.facturacion', compact('proyectos')); } - public function show(ServerRequestInterface $request, ResponseInterface $response, View $view, Service\Venta $ventaService, int $venta_id): ResponseInterface + public function show(ServerRequestInterface $request, ResponseInterface $response, View $view, + Service\Venta $ventaService, Service\Proyecto\Terreno $terrenoService, + int $venta_id): ResponseInterface { $venta = $ventaService->getById($venta_id); - return $view->render($response, 'ventas.facturacion.show', compact('venta')); + $terreno = $terrenoService->valor($venta->proyecto()->id); + return $view->render($response, 'ventas.facturacion.show', compact('venta', 'terreno')); } } diff --git a/app/src/Service/Contabilidad/Nubox.php b/app/src/Service/Contabilidad/Nubox.php index 62bdf48..321c79f 100644 --- a/app/src/Service/Contabilidad/Nubox.php +++ b/app/src/Service/Contabilidad/Nubox.php @@ -2,6 +2,7 @@ namespace Incoviba\Service\Contabilidad; use DateTimeInterface; +use DateTimeImmutable; use Incoviba\Common\Ideal; use Incoviba\Common\Implement\Exception; use Incoviba\Repository; @@ -130,6 +131,34 @@ class Nubox extends Ideal\Service } return json_decode($response->getBody()->getContents(), JSON_OBJECT_AS_ARRAY); } + public function getSaldoCuenta(int $inmobiliaria_rut, string $cuenta, DateTimeInterface $mes, bool $acumuladoAnual = false): array + { + $inmobiliaria = $this->nuboxRepository->fetchByInmobiliaria($inmobiliaria_rut); + $from = new DateTimeImmutable($mes->format('Y-m-1')); + $to = new DateTimeImmutable($mes->format('Y-m-t')); + $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' => $cuenta, + '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); + } private function send(string $uri, int $inmobiliaria_rut, string $method = 'GET', ?StreamInterface $body = null): ResponseInterface { diff --git a/app/src/Service/Proyecto/Terreno.php b/app/src/Service/Proyecto/Terreno.php new file mode 100644 index 0000000..ebb5e04 --- /dev/null +++ b/app/src/Service/Proyecto/Terreno.php @@ -0,0 +1,36 @@ +proyectoRepository->fetchById($proyecto_id); + $today = new DateTimeImmutable(); + $lastDecember = (new DateTimeImmutable($today->modify('-1 year')->format('Y-12-31'))); + // 1110-02 + $movimientos = $this->nuboxService->getSaldoCuenta($proyecto->inmobiliaria()->rut, '1110-02', $lastDecember, true); + error_log(var_export($movimientos, true).PHP_EOL,3,'/logs/debug'); + if ($proyecto->terreno->fecha >= $lastDecember) { + return $proyecto->terreno; + } + } catch (Implement\Exception\EmptyResult) {} + return $terreno; + } +}