diff --git a/app/common/Implement/Exception/HttpResponse.php b/app/common/Implement/Exception/HttpResponse.php new file mode 100644 index 0000000..249c7d8 --- /dev/null +++ b/app/common/Implement/Exception/HttpResponse.php @@ -0,0 +1,13 @@ +group('/nubox/{inmobiliaria_rut}', function($app) { $app->get('/token[/]', [Nubox::class, 'token']); $app->get('/sistemas[/]', [Nubox::class, 'sistemas']); + $app->get('/cuentas[/]', [Nubox::class, 'cuentas']); $app->group('/libro', function($app) { $app->post('/mayor[/]', [Nubox::class, 'libroMayor']); $app->post('/diario[/]', [Nubox::class, 'libroDiario']); }); + $app->post('/cuenta', [Nubox::class, 'cuenta']); }); diff --git a/app/resources/views/ventas/facturacion/show.blade.php b/app/resources/views/ventas/facturacion/show.blade.php index 8eb0763..0d871dc 100644 --- a/app/resources/views/ventas/facturacion/show.blade.php +++ b/app/resources/views/ventas/facturacion/show.blade.php @@ -13,23 +13,18 @@ {{$venta->propiedad()->summary()}} -
- Valor Venta: {{$format->ufs($venta->valor)}} -
-
-
- -
- -
- -
+
+
+
+ Valor Venta: {{$format->ufs($venta->valor)}}
+
+
@foreach ($venta->propiedad()->unidades as $unidad)
- +
UF
@@ -41,7 +36,7 @@ @foreach($venta->propiedad()->unidades as $unidad)
@if ($unidad->prorrateo === 0.0) - +
%
@@ -51,8 +46,8 @@ @endforeach
- @php $lastDic = new DateTimeImmutable((new DateTimeImmutable())->sub(new DateInterval('P1Y'))->format('31-12-Y')); @endphp - @if (!isset($venta->proyecto()->terreno->fecha) or $venta->proyecto()->terreno->fecha < $lastDic) + @php $lastDic = new DateTimeImmutable((new DateTimeImmutable())->sub(new DateInterval('P1Y'))->format('Y-12-31')); @endphp + @if (!isset($terreno->fecha) or $terreno->fecha->getTimestamp() < 0 or $terreno->fecha < $lastDic)
@@ -61,6 +56,46 @@
@endif +
+ + +
+
+
+
+ +
+ +
+ +
+
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ + +
+
+
+
@if ($venta->currentEstado()->fecha->sub(new DateInterval('P1M')) > $venta->proyecto()->terreno->fecha and $IPC->get($venta->proyecto()->terreno->fecha, $venta->currentEstado()->fecha->sub(new DateInterval('P1M'))) === 0.0) @@ -71,8 +106,8 @@
@endif -
-
+
+ {{--
@@ -174,18 +209,213 @@
-
+
--}} @endsection @push('page_scripts') @endpush diff --git a/app/src/Controller/API/Contabilidad/Nubox.php b/app/src/Controller/API/Contabilidad/Nubox.php index b46ffa6..2fce7b1 100644 --- a/app/src/Controller/API/Contabilidad/Nubox.php +++ b/app/src/Controller/API/Contabilidad/Nubox.php @@ -2,6 +2,8 @@ namespace Incoviba\Controller\API\Contabilidad; use DateTimeImmutable; +use Incoviba\Common\Implement\Exception\EmptyRedis; +use Incoviba\Common\Implement\Exception\EmptyResult; use Incoviba\Common\Implement\Exception\HttpResponse; use Incoviba\Controller\API\withJson; use Incoviba\Service; @@ -88,4 +90,34 @@ class Nubox } return $this->withJson($response, $output); } + public function cuentas(ServerRequestInterface $request, ResponseInterface $response, Service\Contabilidad\Nubox $nuboxService, int $inmobiliaria_rut): ResponseInterface + { + $body = $request->getParsedBody(); + $output = [ + 'inmobiliaria_rut' => $inmobiliaria_rut, + 'input' => $body, + 'cuentas' => $nuboxService->getCuentas($inmobiliaria_rut) + ]; + return $this->withJson($response, $output); + } + public function cuenta(ServerRequestInterface $request, ResponseInterface $response, Service\Contabilidad\Nubox $nuboxService, int $inmobiliaria_rut): ResponseInterface + { + $body = $request->getParsedBody(); + $output = [ + 'inmobiliaria_rut' => $inmobiliaria_rut, + 'input' => $body, + 'movimientos' => [] + ]; + try { + $mes = new DateTimeImmutable($body['mes']); + $cuenta = $body['cuenta']; + $output['movimientos'] = $nuboxService->getMesCuenta($inmobiliaria_rut, $cuenta, $mes); + } catch (HttpResponse $exception) { + $output['error'] = [ + 'code' => $exception->getCode(), + 'message' => $exception->getMessage() + ]; + } + return $this->withJson($response, $output); + } } diff --git a/app/src/Service/Contabilidad/Nubox.php b/app/src/Service/Contabilidad/Nubox.php index 321c79f..73dc104 100644 --- a/app/src/Service/Contabilidad/Nubox.php +++ b/app/src/Service/Contabilidad/Nubox.php @@ -66,7 +66,11 @@ class Nubox extends Ideal\Service { if (!isset($this->sistemas[$inmobiliaria_rut])) { $redisKey = "nubox:{$inmobiliaria_rut}"; - $this->sistemas[$inmobiliaria_rut] = json_decode($this->redisService->get($redisKey)); + try { + $this->sistemas[$inmobiliaria_rut] = json_decode($this->redisService->get($redisKey)); + } catch (Exception\EmptyRedis) { + $this->getToken($inmobiliaria_rut); + } } return $this->sistemas[$inmobiliaria_rut]; } @@ -80,6 +84,34 @@ class Nubox extends Ideal\Service return $this; } + public function getCuentas(int $inmobiliaria_rut): array + { + $redisKey = "nubox:cuentas:{$inmobiliaria_rut}"; + try { + return json_decode($this->redisService->get($redisKey)); + } catch (Exception\EmptyRedis) { + $today = new DateTimeImmutable(); + $libro = $this->getLibroMayor($inmobiliaria_rut, new DateTimeImmutable($today->format('Y-m-1')), new DateTimeImmutable($today->format('Y-m-t'))); + $cuentas = array_map(function($cuenta) { + $full = $cuenta['Cuenta']; + $space = strpos($full, ' '); + return ['codigo' => substr($full, 0, $space), 'nombre' => substr($full, $space + 1)]; + }, $libro); + $this->redisService->set($redisKey, json_encode($cuentas)); + return $cuentas; + } + } + public function getCuenta(int $inmobiliaria_rut, string $cuentaNombre): string + { + $cuentas = $this->getCuentas($inmobiliaria_rut); + $validas = array_filter($cuentas, function($cuenta) use ($cuentaNombre) { + return strtolower($cuenta->nombre) === strtolower($cuentaNombre); + }); + if (count($validas) === 0) { + throw new Exception\EmptyResult('Cuenta no encontrada'); + } + return array_values($validas)[0]->codigo; + } public function getLibroMayor(int $inmobiliaria_rut, DateTimeInterface $from, DateTimeInterface $to): array { $inmobiliaria = $this->nuboxRepository->fetchByInmobiliaria($inmobiliaria_rut); @@ -131,7 +163,7 @@ 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 + public function getMesCuenta(int $inmobiliaria_rut, string $cuenta, DateTimeInterface $mes): array { $inmobiliaria = $this->nuboxRepository->fetchByInmobiliaria($inmobiliaria_rut); $from = new DateTimeImmutable($mes->format('Y-m-1')); diff --git a/app/src/Service/Proyecto/Terreno.php b/app/src/Service/Proyecto/Terreno.php index ebb5e04..637fc7d 100644 --- a/app/src/Service/Proyecto/Terreno.php +++ b/app/src/Service/Proyecto/Terreno.php @@ -2,6 +2,7 @@ namespace Incoviba\Service\Proyecto; use DateTimeImmutable; +use DateInterval; use Psr\Log\LoggerInterface; use Incoviba\Common\Ideal; use Incoviba\Common\Implement; @@ -12,7 +13,7 @@ use Incoviba\Model; class Terreno extends Ideal\Service { public function __construct(LoggerInterface $logger, protected Repository\Proyecto $proyectoRepository, - protected Service\Contabilidad\Nubox $nuboxService) + protected Service\Contabilidad\Nubox $nuboxService, protected Service\IPC $ipcService) { parent::__construct($logger); } @@ -23,14 +24,45 @@ class Terreno extends Ideal\Service try { $proyecto = $this->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'); + $lastDecember = (new DateTimeImmutable($today->format('Y-01-01'))); if ($proyecto->terreno->fecha >= $lastDecember) { return $proyecto->terreno; } + try { + return $this->getValorContable($proyecto, $lastDecember); + } catch (Implement\Exception\EmptyResponse) {} + if ($proyecto->terreno->fecha->getTimestamp() > 0) { + return $this->getValorReajustado($proyecto, $lastDecember); + } + $terreno = $proyecto->terreno; } catch (Implement\Exception\EmptyResult) {} return $terreno; } + + protected function getValorContable(Model\Proyecto $proyecto, DateTimeImmutable $lastDecember): Model\Proyecto\Terreno + { + $cuentaNubox = $this->nuboxService->getCuenta($proyecto->inmobiliaria()->rut, 'Terrenos'); + $movimientos = $this->nuboxService->getMesCuenta($proyecto->inmobiliaria()->rut, $cuentaNubox, $lastDecember); + if (count($movimientos) === 0 or $movimientos[0]['Saldo'] === 0.0) { + throw new Implement\Exception\EmptyResponse("No hay movimientos para este proyecto para la fecha {$lastDecember->format('d-m-Y')}"); + } + $data = [ + 'fecha' => (new DateTimeImmutable($movimientos[0]['FechaMovimiento']))->format('Y-m-d'), + 'valor' => $movimientos[0]['Saldo'], + ]; + $proyecto = $this->proyectoRepository->editTerreno($proyecto, $data); + return $proyecto->terreno; + } + + protected function getValorReajustado(Model\Proyecto $proyecto): ?Model\Proyecto\Terreno + { + $novPrevTerreno = new DateTimeImmutable($proyecto->terreno->fecha->format('m') < 12 ? $proyecto->terreno->fecha->sub(new DateInterval('P1Y'))->format('Y-11-1') : $proyecto->terreno->fecha->format('Y-11-1')); + $lastDecember = new DateTimeImmutable((new DateTimeImmutable())->sub(new DateInterval('P1Y'))->format('Y-12-31')); + $novLast = new DateTimeImmutable($lastDecember->format('Y-11-1')); + $ipc = $this->ipcService->get($novPrevTerreno, $novLast); + $terreno = $proyecto->terreno; + $terreno->fecha = $lastDecember; + $terreno->valor *= (1 + $ipc); + return $terreno; + } }