Files
oficial/app/src/Service/Proyecto/Terreno.php

92 lines
3.8 KiB
PHP
Raw Normal View History

2024-04-15 20:13:15 -04:00
<?php
namespace Incoviba\Service\Proyecto;
use Exception;
2024-04-15 20:13:15 -04:00
use DateTimeImmutable;
2024-04-18 20:30:26 -04:00
use DateInterval;
use Psr\Http\Client\ClientExceptionInterface;
2024-04-15 20:13:15 -04:00
use Psr\Log\LoggerInterface;
use Incoviba\Common\Ideal;
use Incoviba\Common\Implement;
use Incoviba\Repository;
use Incoviba\Service;
use Incoviba\Model;
class Terreno extends Ideal\Service
{
public function __construct(LoggerInterface $logger, protected Repository\Proyecto $proyectoRepository,
2024-04-18 20:30:26 -04:00
protected Service\Contabilidad\Nubox $nuboxService, protected Service\IPC $ipcService)
2024-04-15 20:13:15 -04:00
{
parent::__construct($logger);
}
/**
* @param int $proyecto_id
* @return Model\Proyecto\Terreno|null
* @throws Exception
*/
2024-04-15 20:13:15 -04:00
public function valor(int $proyecto_id): ?Model\Proyecto\Terreno
{
$terreno = null;
try {
$proyecto = $this->proyectoRepository->fetchById($proyecto_id);
$today = new DateTimeImmutable();
2025-01-16 19:30:11 -03:00
$lastNovember = (new DateTimeImmutable($today->format('Y-01-30')))->sub(new DateInterval('P2M'));
if ($proyecto->terreno->fecha >= $lastNovember) {
2024-04-15 20:13:15 -04:00
return $proyecto->terreno;
}
2024-04-18 20:30:26 -04:00
try {
2025-03-03 14:57:22 -03:00
// Valor 1o enero
2025-01-16 19:30:11 -03:00
return $this->getValorContable($proyecto, $lastNovember->add(new DateInterval('P1M'))->add(new DateInterval('P1D')));
2024-04-18 20:30:26 -04:00
} catch (Implement\Exception\EmptyResponse) {}
2024-04-19 23:19:35 -04:00
if ($proyecto->terreno->fecha === null) {
return null;
}
2024-04-18 20:30:26 -04:00
if ($proyecto->terreno->fecha->getTimestamp() > 0) {
2024-04-19 23:19:35 -04:00
return $this->getValorReajustado($proyecto);
2024-04-18 20:30:26 -04:00
}
$terreno = $proyecto->terreno;
2024-04-15 20:13:15 -04:00
} catch (Implement\Exception\EmptyResult) {}
return $terreno;
}
2024-04-18 20:30:26 -04:00
/**
* @throws Implement\Exception\EmptyResponse
* @throws Implement\Exception\EmptyResult
* @throws Exception
*/
2024-04-18 20:30:26 -04:00
protected function getValorContable(Model\Proyecto $proyecto, DateTimeImmutable $lastDecember): Model\Proyecto\Terreno
{
try {
$cuentaNubox = $this->nuboxService->getCuenta($proyecto->inmobiliaria()->rut, 'Terrenos');
$movimientos = $this->nuboxService->getMesCuenta($proyecto->inmobiliaria()->rut, $cuentaNubox, $lastDecember);
} catch (Implement\Exception\HttpResponse | ClientExceptionInterface $exception) {
throw new Implement\Exception\EmptyResponse("No existen cuentas para este proyecto para la fecha {$lastDecember->format('d-m-Y')}", $exception);
}
2024-04-18 20:30:26 -04:00
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;
}
/**
* @throws Exception
*/
2024-04-18 20:30:26 -04:00
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;
2025-01-16 19:30:11 -03:00
$terreno->fecha = $novLast;
2024-04-18 20:30:26 -04:00
$terreno->valor *= (1 + $ipc);
return $terreno;
}
2024-04-15 20:13:15 -04:00
}