Files
oficial/app/src/Controller/API/Ventas/Reservations.php
aldarien 61c813fc08 feature/cierres (#30)
Reservas agregar y aprobar

Co-authored-by: Juan Pablo Vial <jpvialb@incoviba.cl>
Reviewed-on: #30
2025-09-11 15:16:12 -03:00

214 lines
7.9 KiB
PHP

<?php
namespace Incoviba\Controller\API\Ventas;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Controller\API\withJson;
use Incoviba\Exception\ServiceAction;
use Incoviba\Service;
class Reservations
{
use withJson;
public function __invoke(ServerRequestInterface $request, ResponseInterface $response,
Service\Venta\Reservation $reservationService): ResponseInterface
{
$reservations = [];
try {
$reservations = $reservationService->getAll();
} catch (ServiceAction\Read $exception) {
return $this->withError($response, $exception);
}
return $this->withJson($response, compact('reservations'));
}
public function getByProject(ServerRequestInterface $request, ResponseInterface $response,
Service\Venta\Reservation $reservationService, int $project_id): ResponseInterface
{
$reservations = [];
try {
$reservations = $reservationService->getByProject($project_id);
} catch (ServiceAction\Read $exception) {
return $this->withError($response, $exception);
}
return $this->withJson($response, compact('reservations'));
}
public function get(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Reservation $reservationService, int $reservation_id): ResponseInterface
{
$output = [
'reservation_id' => $reservation_id,
'reservation' => null,
];
try {
$output['reservation'] = $reservationService->get($reservation_id);
} catch (ServiceAction\Read $exception) {
return $this->withError($response, $exception);
}
return $this->withJson($response, $output);
}
public function add(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Reservation $reservationService): ResponseInterface
{
$input = $request->getParsedBody();
$output = [
'input' => $input,
'reservations' => [],
'success' => false,
'partial' => false,
'errors' => [],
];
var_dump($input);
/*try {
$output['reservations'] []= [
'reservation' => $reservationService->add($input),
'success' => true
];
$output['partial'] = true;
} catch (ServiceAction\Create $exception) {
$output['errors'] []= $this->parseError($exception);
}*/
if (count($input['reservations']) === count($output['reservations'])) {
$output['success'] = true;
}
return $this->withJson($response, $output);
}
public function addOne(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Reservation $reservationService): ResponseInterface
{
$input = $request->getParsedBody();
$output = [
'input' => $input,
'reservation' => null,
'success' => false,
'errors' => [],
];
$data = [];
foreach ($input as $key => $value) {
if (!str_starts_with($key, 'add_')) {
continue;
}
$data[substr($key, 4)] = $value;
}
try {
$output['reservation'] = $reservationService->add($data);
$output['success'] = true;
} catch (ServiceAction\Create $exception) {
$output['errors'] []= $this->parseError($exception);
}
return $this->withJson($response, $output);
}
public function edit(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Reservation $reservationService, int $reservation_id): ResponseInterface
{
$input = $request->getParsedBody();
$output = [
'input' => $input,
'reservation_id' => $reservation_id,
'reservations' => null,
'success' => false,
];
try {
$reservation = $reservationService->get($reservation_id);
$output['reservations'] = $reservationService->edit($reservation, $input);
$output['success'] = true;
} catch (ServiceAction\Read | ServiceAction\Update $exception) {
return $this->withError($response, $exception);
}
return $this->withJson($response, $output);
}
public function delete(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Reservation $reservationService, int $reservation_id): ResponseInterface
{
$output = [
'reservation_id' => $reservation_id,
'success' => false,
];
try {
$reservationService->delete($reservation_id);
$output['success'] = true;
} catch (ServiceAction\Delete $exception) {
return $this->withError($response, $exception);
}
return $this->withJson($response, $output);
}
public function active(ServerRequestInterface $request, ResponseInterface $response,
Service\Venta\Reservation $reservationService, int $project_id): ResponseInterface
{
$output = [
'project_id' => $project_id,
'reservations' => [],
'success' => false,
];
try {
$output['reservations'] = $reservationService->getActive($project_id);
$output['success'] = true;
} catch (ServiceAction\Read) {}
return $this->withJson($response, $output);
}
public function pending(ServerRequestInterface $request, ResponseInterface $response,
Service\Venta\Reservation $reservationService, int $project_id): ResponseInterface
{
$output = [
'project_id' => $project_id,
'reservations' => [],
'success' => false,
];
try {
$output['reservations'] = $reservationService->getPending($project_id);
$output['success'] = true;
} catch (ServiceAction\Read) {}
return $this->withJson($response, $output);
}
public function rejected(ServerRequestInterface $request, ResponseInterface $response,
Service\Venta\Reservation $reservationService, int $project_id): ResponseInterface
{
$output = [
'project_id' => $project_id,
'reservations' => [],
'success' => false,
];
try {
$output['reservations'] = $reservationService->getRejected($project_id);
$output['success'] = true;
} catch (ServiceAction\Read) {}
return $this->withJson($response, $output);
}
public function approve(ServerRequestInterface $request, ResponseInterface $response,
Service\Venta\Reservation $reservationService, int $reservation_id): ResponseInterface
{
$output = [
'reservation_id' => $reservation_id,
'success' => false,
];
try {
$reservation = $reservationService->get($reservation_id);
$reservationService->approve($reservation);
$output['success'] = true;
} catch (ServiceAction\Read | ServiceAction\Update) {}
return $this->withJson($response, $output);
}
public function reject(ServerRequestInterface $request, ResponseInterface $response,
Service\Venta\Reservation $reservationService, int $reservation_id): ResponseInterface
{
$output = [
'reservation_id' => $reservation_id,
'success' => false,
];
try {
$reservation = $reservationService->get($reservation_id);
$reservationService->reject($reservation);
$output['success'] = true;
} catch (ServiceAction\Read | ServiceAction\Update) {}
return $this->withJson($response, $output);
}
}