Facturacion y Terreno
This commit is contained in:
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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'));
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user