111 lines
5.0 KiB
PHP
111 lines
5.0 KiB
PHP
<?php
|
|
namespace Incoviba\Controller\Ventas;
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
use DateTimeImmutable;
|
|
use Incoviba\Common\Alias\View;
|
|
use Incoviba\Common\Implement\Exception\EmptyResult;
|
|
use Incoviba\Model;
|
|
use Incoviba\Repository;
|
|
use Incoviba\Service;
|
|
|
|
class Cuotas
|
|
{
|
|
public function pendientes(ServerRequestInterface $request, ResponseInterface $response, View $view, Service\Venta\Cuota $cuotaService): ResponseInterface
|
|
{
|
|
$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, $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');
|
|
}
|
|
public function add(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Pie $pieService, Repository\Venta $ventaRepository, Repository\Contabilidad\Banco $bancoRepository, View $view, int $pie_id): ResponseInterface
|
|
{
|
|
$pie = $pieService->getById($pie_id);
|
|
$venta = $ventaRepository->fetchByPie($pie_id);
|
|
$bancos = $bancoRepository->fetchAll();
|
|
usort($bancos, function(Model\Contabilidad\Banco $a, Model\Contabilidad\Banco $b) {
|
|
return strcmp($a->nombre, $b->nombre);
|
|
});
|
|
return $view->render($response, 'ventas.pies.cuotas.add', compact('pie', 'venta', 'bancos'));
|
|
}
|
|
public function doAdd(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Pie $pieService,
|
|
Repository\Venta $ventaRepository, Service\Valor $valorService, int $pie_id): ResponseInterface
|
|
{
|
|
$body = $request->getParsedBody();
|
|
$pie = $pieService->getById($pie_id);
|
|
$venta = $ventaRepository->fetchByPie($pie_id);
|
|
$start = count($pie->cuotas(vigentes: true));
|
|
$total = $pie->cuotas;
|
|
for ($i = $start; $i < $total; $i ++) {
|
|
if ($body["banco{$i}"] === '') {
|
|
continue;
|
|
}
|
|
$data = [
|
|
'pie' => $pie->id,
|
|
'fecha' => $body["fecha{$i}"],
|
|
'banco' => (int) $body["banco{$i}"],
|
|
'identificador' => $body["identificador{$i}"],
|
|
'valor' => $valorService->clean(str_replace('.', '', $body["valor{$i}"])),
|
|
'numero' => $i + 1,
|
|
];
|
|
$pieService->addCuota($data);
|
|
}
|
|
return $response->withHeader('Location', "/venta/{$venta->id}");
|
|
}
|
|
}
|