98 lines
3.6 KiB
PHP
98 lines
3.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Controller\Admin\AccommodationBooking;
|
|
|
|
use App\Entity\Groups\AccommodationBooking;
|
|
use App\Entity\User;
|
|
use App\Exception\ImplausibleBookingPriceException;
|
|
use App\Form\Admin\Groups\AccommodationBookingCreateType;
|
|
use App\Htmx\HxRedirectResponse;
|
|
use App\Service\AccommodationBookingService;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\Form\FormError;
|
|
use Symfony\Component\Form\FormInterface;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\Routing\Attribute\Route;
|
|
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
|
|
|
#[IsGranted('ROLE_GROUPS_MANAGER')]
|
|
class CreateController extends AbstractController
|
|
{
|
|
public function __construct(
|
|
private readonly EntityManagerInterface $entityManager,
|
|
private readonly LoggerInterface $logger,
|
|
private readonly AccommodationBookingService $bookingService,
|
|
) {
|
|
}
|
|
|
|
#[Route('/admin/accommodation-booking/create', name: 'app_admin_accommodationbooking_create')]
|
|
public function index(Request $request): Response
|
|
{
|
|
$booking = new AccommodationBooking();
|
|
|
|
// Whoever creates a booking in the backoffice looks after it until someone else is
|
|
// assigned in the edit form. The route is behind ROLE_GROUPS_MANAGER, so the creator
|
|
// is always eligible as Betreuer.
|
|
$user = $this->getUser();
|
|
if ($user instanceof User) {
|
|
$booking->setManagedBy($user);
|
|
}
|
|
|
|
$form = $this->createForm(AccommodationBookingCreateType::class, $booking, [
|
|
'hx_post' => $request->getRequestUri(),
|
|
]);
|
|
$form->handleRequest($request);
|
|
|
|
if ($form->isSubmitted() && $form->isValid() && $this->refreshPriceSnapshot($form, $booking)) {
|
|
$this->entityManager->persist($booking);
|
|
$this->entityManager->flush();
|
|
|
|
$this->addFlash('success', 'Die Buchung wurde erstellt');
|
|
|
|
$this->logger->info('Created accommodation booking', [
|
|
'id' => $booking->getId(),
|
|
]);
|
|
|
|
return new HxRedirectResponse(
|
|
$this->generateUrl('app_admin_accommodationbooking_edit', ['id' => $booking->getId()])
|
|
);
|
|
}
|
|
|
|
return $this->render('admin/accommodation_booking/modal_create.html.twig', [
|
|
'form' => $form,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* The price is computed after validation, so an implausible total cannot be a constraint on
|
|
* the form — it is reported as one anyway, because the office's next move is to go and fix
|
|
* the house's prices, not to read an error page.
|
|
*
|
|
* @param FormInterface<AccommodationBooking> $form
|
|
*/
|
|
private function refreshPriceSnapshot(FormInterface $form, AccommodationBooking $booking): bool
|
|
{
|
|
try {
|
|
$this->bookingService->refreshPriceSnapshot($booking);
|
|
|
|
return true;
|
|
} catch (ImplausibleBookingPriceException $e) {
|
|
$this->logger->error('Refused to store an implausible accommodation booking total', [
|
|
'exception' => $e,
|
|
'id' => $booking->getId(),
|
|
'accommodation' => $booking->getAccommodation()?->getCalendarCode(),
|
|
'paxCount' => $booking->getPaxCount(),
|
|
'nights' => $booking->getNights(),
|
|
]);
|
|
$form->addError(new FormError(ImplausibleBookingPriceException::FORM_ERROR_MESSAGE));
|
|
|
|
return false;
|
|
}
|
|
}
|
|
}
|