Files
oficial/app/src/Controller/API/Ventas/Cuotas.php

68 lines
2.5 KiB
PHP
Raw Normal View History

2023-10-11 09:03:44 -03:00
<?php
namespace Incoviba\Controller\API\Ventas;
use DateTimeImmutable;
use DateInterval;
use Incoviba\Common\Implement\Exception\EmptyResult;
2023-10-13 10:45:21 -03:00
use Incoviba\Controller\API\withJson;
2023-10-11 09:03:44 -03:00
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Repository;
use Incoviba\Service;
class Cuotas
{
2023-10-13 10:45:21 -03:00
use withJson;
2023-10-11 09:03:44 -03:00
public function hoy(ServerRequestInterface $request, ResponseInterface $response, Repository\Venta\Cuota $cuotaRepository): ResponseInterface
{
$output = [
'cuotas' => 0
2023-10-11 09:03:44 -03:00
];
try {
$output['cuotas'] = count($cuotaRepository->fetchHoy());
} catch (EmptyResult) {}
2023-10-13 10:45:21 -03:00
return $this->withJson($response, $output);
2023-10-11 09:03:44 -03:00
}
public function pendiente(ServerRequestInterface $request, ResponseInterface $response, Repository\Venta\Cuota $cuotaRepository): ResponseInterface
{
$output = [
'cuotas' => 0
2023-10-11 09:03:44 -03:00
];
try {
$output['cuotas'] = count($cuotaRepository->fetchPendientes());
} catch (EmptyResult) {}
2023-10-13 10:45:21 -03:00
return $this->withJson($response, $output);
2023-10-11 09:03:44 -03:00
}
public function porVencer(ServerRequestInterface $request, ResponseInterface $response, Repository\Venta\Cuota $cuotaRepository, Service\Format $formatService): ResponseInterface
{
$output = [];
try {
$cuotas = $cuotaRepository->fetchDatosPorVencer();
foreach ($cuotas as $row) {
$fecha = $row['Fecha'];
$date = new DateTimeImmutable($fecha);
if (($weekday = $date->format('N')) > 5) {
$day_diff = 7 - $weekday + 1;
$date = $date->add(new DateInterval("P{$day_diff}D"));
$fecha = $date->format('Y-m-d');
}
$key = $formatService->localDate($fecha, "EEE. dd 'de' MMMM 'de' yyyy", true);
if (!isset($output[$key])) {
$output[$key] = [];
}
if (!isset($output[$key][$row['Proyecto']])) {
$output[$key][$row['Proyecto']] = 0;
}
$output[$key][$row['Proyecto']] += $row['Cantidad'];
2023-10-11 09:03:44 -03:00
}
foreach ($output as $key => $day) {
uksort($day, function($a, $b) {
return strcmp($a, $b);
});
$output[$key] = $day;
2023-10-11 09:03:44 -03:00
}
} catch (EmptyResult) {}
2023-10-13 10:45:21 -03:00
return $this->withJson($response, ['cuotas' => $output]);
2023-10-11 09:03:44 -03:00
}
}