Files
oficial/app/src/Controller/Ventas/Facturacion.php
Juan Pablo Vial 86b8d6b3c7 Limpieza
2025-04-29 13:01:21 -04:00

54 lines
2.4 KiB
PHP

<?php
namespace Incoviba\Controller\Ventas;
use DateInterval;
use DateTimeImmutable;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Alias\View;
use Incoviba\Common\Ideal;
use Incoviba\Repository;
use Incoviba\Service;
class Facturacion extends Ideal\Controller
{
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, View $view,
Service\Proyecto $proyectoService, Service\Proyecto\Terreno $terrenoService): ResponseInterface
{
$proyectos = $proyectoService->getEscriturando();
foreach ($proyectos as &$proyecto) {
$proyecto->terreno = $terrenoService->valor($proyecto->id) ?? $proyecto->terreno;
}
return $view->render($response, 'ventas.facturacion', compact('proyectos'));
}
public function show(ServerRequestInterface $request, ResponseInterface $response, View $view,
Service\Venta $ventaService, Service\Proyecto\Terreno $terrenoService,
Service\IPC $ipcService, Service\UF $ufService,
Service\Venta\Factura $facturasService,
Repository\Comuna $comunaRepository,
int $venta_id): ResponseInterface
{
$venta = $ventaService->getById($venta_id);
$uf = $ufService->get($venta->currentEstado()->fecha);
$terreno = $terrenoService->valor($venta->proyecto()->id);
$lastNov = new DateTimeImmutable((new DateTimeImmutable())->sub(new DateInterval('P1Y'))->format('Y-11-1'));
$prevMonth = $venta->currentEstado()->fecha->sub(new DateInterval('P1M'));
if ($prevMonth->format('m') === $venta->currentEstado()->fecha->format('m')) {
// If sub P1M stays in same month
$prevMonth = $prevMonth->sub(new DateInterval('P10D'));
}
$ipc = $ipcService->get($lastNov, $prevMonth);
if ($terreno !== null) {
$monthTerreno = $terreno->fecha;
if ($monthTerreno->format('Y-m') !== $lastNov->format('Y-m')) {
$ipc = $ipcService->get($monthTerreno, $prevMonth);
}
}
$facturas = $facturasService->getByVenta($venta->id);
$comunas = $comunaRepository->fetchAll('descripcion');
return $view->render($response, 'ventas.facturacion.show', compact('venta', 'terreno', 'uf', 'ipc', 'facturas', 'comunas'));
}
}