Facturacion

This commit is contained in:
2023-11-22 19:08:19 -03:00
parent b4742a501e
commit 9ab0515954
45 changed files with 1846 additions and 71 deletions

View File

@ -0,0 +1,123 @@
<?php
namespace Incoviba\Controller\API;
use DateTimeInterface;
use DateTimeImmutable;
use DateInterval;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Implement\Exception\EmptyRedis;
use Incoviba\Service;
class Money
{
use withJson, withRedis;
private int $time = 60 * 60 * 24 * 30;
public function get(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService, Service\Money $moneyService): ResponseInterface
{
$data = $request->getParsedBody();
$output = [
'input' => $data
];
$output[$data['provider']] = 0;
$redisKey = $data['provider'];
$date = new DateTimeImmutable($data['fecha']);
if (isset($data['start'])) {
$start = new DateTimeImmutable($data['start']);
}
$value = $this->getValue($redisService, $redisKey, $moneyService, $date, $data['provider']);
if (isset($start)) {
$months = $date->diff($start)->m;
$current = clone $start;
$value = $this->getValue($redisService, $redisKey, $moneyService, $current, $data['provider']);
for ($i = 1; $i <= $months; $i ++) {
$current = $current->add(new DateInterval("P{$i}M"));
$value += $this->getValue($redisService, $redisKey, $moneyService, $current, $data['provider']);
}
}
$output[$data['provider']] = $value;
return $this->withJson($response, $output);
}
protected array $data;
protected function getValue(Service\Redis $redisService, string $redisKey, Service\Money $moneyService, DateTimeInterface $date, string $provider): float
{
if (isset($this->data[$date->format('Y-m-d')])) {
return $this->data[$date->format('Y-m-d')];
}
try {
$this->data = (array) $this->fetchRedis($redisService, $redisKey);
if (!isset($this->data[$date->format('Y-m-d')])) {
throw new EmptyRedis($redisKey);
}
} catch (EmptyRedis) {
$result = $moneyService->get($provider, $date);
$this->data[$date->format('Y-m-d')] = $result;
$this->saveRedis($redisService, $redisKey, $this->data, $this->time);
}
return $this->data[$date->format('Y-m-d')];
}
/*public function uf(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService, Service\Money $moneyService): ResponseInterface
{
$body = $request->getParsedBody();
$output = [
'input' => $body,
'uf' => 0
];
$redisKey = 'uf';
$date = new DateTimeImmutable($body['fecha']);
try {
$ufs = $this->fetchRedis($redisService, $redisKey);
if (!isset($ufs[$date->format('Y-m-d')])) {
throw new EmptyRedis($redisKey);
}
} catch (EmptyRedis) {
error_log(var_export($ufs,true));
if (!isset($ufs)) {
$ufs = [];
}
$uf = $moneyService->getUF($date);
$ufs[$date->format('Y-m-d')] = $uf;
$this->saveRedis($redisService, $redisKey, $ufs, 60 * 60 * 24 * 30);
}
$output['uf'] = $ufs[$date->format('Y-m-d')];
return $this->withJson($response, $output);
}*/
public function ipc(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService, Service\Money $moneyService): ResponseInterface
{
$data = $request->getParsedBody();
$output = [
'input' => $data,
'date_string' => '',
'ipc' => 0
];
$redisKey = 'ipc';
$start = new DateTimeImmutable($data['start']);
$end = new DateTimeImmutable($data['end']);
$now = new DateTimeImmutable();
if ($end > $now) {
return $this->withJson($response, $output);
}
$dateKey = "{$start->format('Y-m')}-{$end->format('Y-m')}";
$months = $start->diff($end)->m;
$current = new DateTimeImmutable((($start <= $end) ? $start : $end)->format('Y-m-15'));
$value = 0;
$ipcs = [];
try {
$ipcs = (array) $this->fetchRedis($redisService, $redisKey);
} catch (EmptyRedis) {}
for ($i = 1; $i < $months; $i ++) {
$current = $current->add(new DateInterval("P1M"));
if (!isset($ipcs[$current->format('Y-m')])) {
$ipcs[$current->format('Y-m')] = $moneyService->getIPC($current);
$this->saveRedis($redisService, $redisKey, $ipcs, $this->time);
}
$value += $ipcs[$current->format('Y-m')];
}
$output['date_string'] = $dateKey;
$output['ipc'] = $value;
return $this->withJson($response, $output);
}
}

View File

@ -2,9 +2,9 @@
namespace Incoviba\Controller\API;
use DateTimeImmutable;
use Incoviba\Common\Implement\Exception\EmptyRedis;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Implement\Exception\EmptyRedis;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Model;
use Incoviba\Repository;
@ -153,4 +153,21 @@ class Ventas
}
return $this->withJson($response, $output);
}
public function unidades(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Unidad $unidadService, int $venta_id): ResponseInterface
{
$output = [
'venta_id' => $venta_id,
'unidades' => []
];
try {
$unidades = $unidadService->getByVenta($venta_id);
$output['unidades'] = json_decode(json_encode($unidades));
array_walk($output['unidades'], function($unidad, $index, $unidades) {
$unidad->prorrateo = $unidades[$index]->prorrateo;
$unidad->precios = $unidades[$index]->precios;
$unidad->current_precio = $unidades[$index]->currentPrecio;
}, $unidades);
} catch (EmptyResult) {}
return $this->withJson($response, $output);
}
}

View File

@ -0,0 +1,36 @@
<?php
namespace Incoviba\Controller\API\Ventas;
use DateTimeImmutable;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Service;
use Incoviba\Controller\API\withJson;
use Incoviba\Controller\API\withRedis;
use Incoviba\Controller\API\emptyBody;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Common\Implement\Exception\EmptyRedis;
class Facturacion
{
use withJson, withRedis, emptyBody;
public function proyecto(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService, Service\Venta $ventaService, int $proyecto_id): ResponseInterface
{
$output = [
'proyecto_id' => $proyecto_id,
'ventas' => []
];
$today = new DateTimeImmutable();
$redisKey = "ventas_facturacion-proyecto-{$proyecto_id}-{$today->format('Y-m-d')}";
try {
$output['ventas'] = $this->fetchRedis($redisService, $redisKey);
} catch (EmptyRedis) {
try {
$output['ventas'] = $ventaService->getActivaByProyecto($proyecto_id);
$this->saveRedis($redisService, $redisKey, $output['ventas']);
} catch (EmptyResult) {}
}
return $this->withJson($response, $output);
}
}

View File

@ -14,11 +14,15 @@ trait withRedis
}
return json_decode($jsonString);
}
public function saveRedis(Service\Redis $redisService, string $redisKey, mixed $value): void
public function saveRedis(Service\Redis $redisService, string $redisKey, mixed $value, ?int $expiration = null): void
{
if (is_array($value) or is_object($value)) {
$value = json_encode($value);
}
if ($expiration !== null) {
$redisService->set($redisKey, $value, $expiration);
return;
}
$redisService->set($redisKey, $value);
}
}

View File

@ -0,0 +1,21 @@
<?php
namespace Incoviba\Controller\Ventas;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Alias\View;
use Incoviba\Service;
class Facturacion
{
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
{
$venta = $ventaService->getById($venta_id);
return $view->render($response, 'ventas.facturacion.show', compact('venta'));
}
}