Facturacion, se agrega PropiedadUnidad

This commit is contained in:
2023-11-29 20:09:08 -03:00
parent 094209823a
commit 39048e12b3
24 changed files with 716 additions and 55 deletions

38
app/src/Service/IPC.php Normal file
View File

@ -0,0 +1,38 @@
<?php
namespace Incoviba\Service;
use DateTimeInterface;
use DateTimeImmutable;
use DateInterval;
use Incoviba\Common\Implement\Exception\EmptyRedis;
class IPC
{
protected string $redisKey = 'ipc';
public function __construct(protected Redis $redisService, protected Money $moneyService) {}
public function get(DateTimeInterface $from, DateTimeInterface $to = new DateTimeImmutable()): float
{
$now = new DateTimeImmutable();
if ($to > $now) {
return 0;
}
$dateKey = "{$from->format('Y-m')}-{$to->format('Y-m')}";
$ipcs = [];
try {
$ipcs = json_decode($this->redisService->get($this->redisKey), JSON_OBJECT_AS_ARRAY);
if (!isset($ipcs[$dateKey])) {
throw new EmptyRedis($this->redisKey);
}
} catch (EmptyRedis) {
$ipc = $this->moneyService->getIPC($from, $to);
$ipcs[$dateKey] = $ipc;
$this->redisService->set($this->redisKey, json_encode($ipcs), 60 * 60 * 24 * 30);
}
return $ipcs[$dateKey];
}
public function readjust(float $base, DateTimeInterface $from, DateTimeInterface $to = new DateTimeImmutable()):float
{
return $base * (1 + $this->get($from, $to->sub(new DateInterval('P1M'))));
}
}

View File

@ -41,10 +41,10 @@ class Money
return 0;
}
}
public function getIPC(DateTimeInterface $dateTime): float
public function getIPC(DateTimeInterface $start, DateTimeInterface $end): float
{
try {
return $this->getProvider('ipc')->get(MiIndicador::IPC, $dateTime);
return $this->getProvider('ipc')->getVar($start, $end);
} catch (EmptyResponse) {
return 0;
}

View File

@ -0,0 +1,49 @@
<?php
namespace Incoviba\Service\Money;
use DateTimeInterface;
use DateTimeImmutable;
use DateInterval;
use GuzzleHttp\Exception\GuzzleException;
use Psr\Http\Client\ClientInterface;
use Incoviba\Common\Implement\Exception\EmptyResponse;
use Incoviba\Common\Define\Money\Provider;
class Ine implements Provider
{
protected string $uri = 'https://api-calculadora.ine.cl/ServiciosCalculadoraVariacion';
public function __construct(protected ClientInterface $client) {}
public function get(string $money_symbol, DateTimeInterface $dateTime): float
{
$end = new DateTimeImmutable($dateTime->format('Y-m-1'));
$start = $end->sub(new DateInterval('P1M'));
return $this->getVar($start, $end);
}
public function getVar(DateTimeInterface $start, DateTimeInterface $end): float
{
$base = 1000000000;
$request_query = [
'mesInicio' => $start->format('m'),
'AnioInicio' => $start->format('Y'),
'mesTermino' => $end->format('m'),
'AnioTermino' => $end->format('Y'),
'valor_a_ajustar' => $base
];
$request_uri = implode('?', [
$this->uri,
implode('&', array_map(function($val, $key) {
return "{$key}={$val}";
}, $request_query, array_keys($request_query)))
]);
try {
$response = $this->client->get($request_uri);
} catch (GuzzleException) {
throw new EmptyResponse($request_uri);
}
$body = $response->getBody();
$json = json_decode($body->getContents());
return ((int) str_replace('.', '', $json[0]->valorajustado) - $base) / $base;
}
}

35
app/src/Service/UF.php Normal file
View File

@ -0,0 +1,35 @@
<?php
namespace Incoviba\Service;
use DateTimeInterface;
use Incoviba\Common\Implement\Exception\EmptyRedis;
class UF
{
protected string $redisKey = 'uf';
public function __construct(protected Redis $redisService, protected Money $moneyService) {}
public function get(DateTimeInterface $date): float
{
$ufs = [];
try {
$ufs = json_decode($this->redisService->get($this->redisKey), JSON_OBJECT_AS_ARRAY);
if (!isset($ufs[$date->format('Y-m-d')])) {
throw new EmptyRedis($this->redisKey);
}
$uf = $ufs[$date->format('Y-m-d')];
} catch (EmptyRedis) {
$uf = $this->moneyService->getUF($date);
$ufs[$date->format('Y-m-d')] = $uf;
$this->redisService->set($this->redisKey, json_encode($ufs), 60 * 60 * 24 * 30);
}
return $uf;
}
public function transform(DateTimeInterface $date, float $input, string $from = 'uf'): float
{
$uf = $this->get($date);
return $input * (($from === 'uf') ? $uf : 1/$uf);
}
}

View File

@ -0,0 +1,39 @@
<?php
namespace Incoviba\Service\Venta;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Repository;
use Incoviba\Service;
use Incoviba\Model;
class PropiedadUnidad
{
public function __construct(protected Repository\Venta\PropiedadUnidad $propiedadUnidadRepository, protected Precio $precioService) {}
public function getById(int $unidad_id): Model\Venta\PropiedadUnidad
{
return $this->process($this->propiedadUnidadRepository->fetchById($unidad_id));
}
public function getByVenta(int $venta_id): array
{
return array_map([$this, 'process'], $this->propiedadUnidadRepository->fetchByVenta($venta_id));
}
public function getByPropiedad(int $propiedad_id): array
{
return array_map([$this, 'process'], $this->propiedadUnidadRepository->fetchByPropiedad($propiedad_id));
}
public function edit(Model\Venta\PropiedadUnidad $propiedadUnidad, array $data): Model\Venta\PropiedadUnidad
{
return $this->process($this->propiedadUnidadRepository->edit($propiedadUnidad, $data));
}
protected function process($unidad): Model\Venta\PropiedadUnidad
{
try {
$unidad->precios = $this->precioService->getByUnidad($unidad->id);
$unidad->currentPrecio = $this->precioService->getVigenteByUnidad($unidad->id);
} catch (EmptyResult) {
}
return $unidad;
}
}

View File

@ -10,7 +10,7 @@ class Unidad
{
public function __construct(
protected Repository\Venta\Unidad $unidadRepository,
protected Service\Venta\Precio $precioService
protected Precio $precioService
) {}
public function getById(int $unidad_id): Model\Venta\Unidad