Agregar Bono Pie

This commit is contained in:
Juan Pablo Vial
2024-11-18 23:25:20 -03:00
parent 1e1994264e
commit cb2edb1543
9 changed files with 156 additions and 4 deletions

View File

@ -0,0 +1,42 @@
<?php
namespace Incoviba\Controller\API\Ventas;
use DateTimeImmutable;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Common\Ideal\Controller;
use Incoviba\Controller\API\withJson;
use Incoviba\Service;
use Incoviba\Repository;
class Bonos extends Controller
{
use withJson;
public function add(ServerRequestInterface $request, ResponseInterface $response, Repository\Venta $ventaRepository,
Service\Valor $valorService, Service\UF $ufService,
Service\Venta\BonoPie $bonoPieService, int $venta_id): ResponseInterface
{
$input = $request->getParsedBody();
$output = [
'venta_id' => $venta_id,
'input' => $input,
'bono' => null,
'success' => false
];
try {
$venta = $ventaRepository->fetchById($venta_id);
$input['venta_id'] = $venta->id;
$uf = $ufService->get(new DateTimeImmutable($input['fecha']));
$input['valor'] = $input['valor'] * $uf;
$input['valor'] = $valorService->clean($input['valor']);
$input['uf'] = $uf;
$bono = $bonoPieService->add($input);
$ventaRepository->edit($venta, ['bono_pie' => $bono->id]);
$output['bono'] = $bono;
$output['success'] = true;
} catch (EmptyResult) {}
return $this->withJson($response, $output);
}
}