This commit is contained in:
Juan Pablo Vial
2023-09-07 23:03:21 -03:00
parent 59825259b6
commit fa15da1ee2
40 changed files with 1787 additions and 71 deletions

View File

@ -2,61 +2,72 @@
namespace Incoviba\Controller\Ventas;
use DateTimeImmutable;
use DateInterval;
use Incoviba\Common\Implement\Exception\EmptyResult;
use IntlDateFormatter;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Common\Alias\View;
use Incoviba\Repository;
use Incoviba\Service;
class Cuotas
{
public function pendientes(ServerRequestInterface $request, ResponseInterface $response, View $view, Repository\Venta\Cuota $cuotaRepository): ResponseInterface
public function pendientes(ServerRequestInterface $request, ResponseInterface $response, View $view, Service\Venta\Cuota $cuotaService): ResponseInterface
{
$cuotas = $cuotaRepository->fetchPendientes();
$cuotas_pendientes = [];
$today = new DateTimeImmutable();
$formatter = new IntlDateFormatter('es_ES');
$formatter->setPattern('EEEE dd');
foreach ($cuotas as $cuota) {
$date = new DateTimeImmutable($cuota['fecha']);
$day = clone $date;
$weekday = $date->format('N');
if ($weekday > 5) {
$diff = 7 - $weekday + 1;
$day = $day->add(new DateInterval("P{$diff}D"));
}
$cuotas_pendientes []= [
'id' => $cuota['cuota_id'],
'venta_id' => $cuota['venta_id'],
'Proyecto' => $cuota['Proyecto'],
'Departamento' => $cuota['Departamento'],
'Valor' => $cuota['Valor'],
'Dia' => $formatter->format($day),
'Numero' => $cuota['Numero'],
'Propietario' => $cuota['Propietario'],
'Banco' => $cuota['Banco'],
'Fecha Cheque' => $date->format('d-m-Y'),
'Vencida' => $today->diff($date)->days,
'Fecha ISO' => $date->format('Y-m-d')
];
}
$cuotas_pendientes = $cuotaService->pendientes();
return $view->render($response, 'ventas.cuotas.pendientes', compact('cuotas_pendientes'));
}
public function depositadas(ServerRequestInterface $request, ResponseInterface $response, View $view, Service\Venta\Cuota $cuotaService): ResponseInterface
{
$cuotas_depositadas = $cuotaService->depositadas();
return $view->render($response, 'ventas.cuotas.abonar', compact('cuotas_depositadas'));
}
public function depositar(ServerRequestInterface $request, ResponseInterface $response, Repository\Venta\Cuota $cuotaRepository, Service\Venta\Pago $pagoService): ResponseInterface
{
$body = $request->getBody();
$json = json_decode($body->getContents());
$cuota_id = $json->cuota_id;
$fecha = new DateTimeImmutable($json->fecha);
$output = [
'cuota_id' => $cuota_id,
'depositada' => false
];
try{
$cuota = $cuotaRepository->fetchById($cuota_id);
$output['depositada'] = $pagoService->depositar($cuota->pago);
$output['depositada'] = $pagoService->depositar($cuota->pago, $fecha);
} catch (EmptyResult) {}
$response->getBody()->write(json_encode($output));
return $response->withHeader('Content-Type', 'application/json');
}
public function abonar(ServerRequestInterface $request, ResponseInterface $response, Repository\Venta\Cuota $cuotaRepository, Service\Venta\Pago $pagoService): ResponseInterface
{
$body = $request->getBody();
$json = json_decode($body->getContents());
$cuota_id = $json->cuota_id;
$fecha = new DateTimeImmutable($json->fecha);
$output = [
'cuota_id' => $cuota_id,
'abonada' => false
];
try{
$cuota = $cuotaRepository->fetchById($cuota_id);
$output['abonada'] = $pagoService->abonar($cuota->pago, $fecha);
} catch (EmptyResult) {}
$response->getBody()->write(json_encode($output));
return $response->withHeader('Content-Type', 'application/json');
}
public function devolver(ServerRequestInterface $request, ResponseInterface $response, Repository\Venta\Cuota $cuotaRepository, Service\Venta\Pago $pagoService): ResponseInterface
{
$body = $request->getBody();
$json = json_decode($body->getContents());
$cuota_id = $json->cuota_id;
$fecha = new DateTimeImmutable($json->fecha);
$output = [
'cuota_id' => $cuota_id,
'devuelta' => false
];
try{
$cuota = $cuotaRepository->fetchById($cuota_id);
$output['devuelta'] = $pagoService->devolver($cuota->pago, $fecha);
} catch (EmptyResult) {}
$response->getBody()->write(json_encode($output));
return $response->withHeader('Content-Type', 'application/json');