108 lines
4.7 KiB
PHP
108 lines
4.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Controller\Admin\AccommodationBooking;
|
|
|
|
use App\Entity\Groups\Accommodation;
|
|
use App\Entity\Groups\AccommodationBooking;
|
|
use App\Exception\ImplausibleBookingPriceException;
|
|
use App\Form\Admin\Groups\AccommodationBookingChangeAccommodationType;
|
|
use App\Htmx\HxTrait;
|
|
use App\Service\AccommodationBookingService;
|
|
use Psr\Log\LoggerInterface;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\Form\FormError;
|
|
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 ChangeAccommodationController extends AbstractController
|
|
{
|
|
use HxTrait;
|
|
|
|
public function __construct(
|
|
private readonly AccommodationBookingService $bookingService,
|
|
private readonly LoggerInterface $logger,
|
|
) {
|
|
}
|
|
|
|
#[Route('/admin/accommodation-booking/{id}/change-accommodation', name: 'app_admin_accommodationbooking_change_accommodation')]
|
|
public function index(AccommodationBooking $booking, Request $request): Response
|
|
{
|
|
// Correcting the house is for a scratch record only. Once an offer is out, the customer
|
|
// can see it under their access link, and rewriting what they were offered is not
|
|
// something that belongs in an edit — such a booking gets discarded and redone instead.
|
|
if (!$booking->isDraft()) {
|
|
$this->addFlash('error', 'Das Gruppenhaus lässt sich nur bei einem Entwurf ändern.');
|
|
|
|
return $this->htmxRedirect($request, $this->editUrl($booking, $request));
|
|
}
|
|
|
|
$form = $this->createForm(AccommodationBookingChangeAccommodationType::class, $booking, [
|
|
'current_accommodation' => $booking->getAccommodation(),
|
|
'hx_post' => $request->getRequestUri(),
|
|
]);
|
|
$form->handleRequest($request);
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
|
$selected = $form->get('accommodation')->getData();
|
|
|
|
if ($selected instanceof Accommodation && $selected !== $booking->getAccommodation()) {
|
|
try {
|
|
$this->bookingService->changeAccommodation($booking, $selected);
|
|
} catch (ImplausibleBookingPriceException $e) {
|
|
// The house that was picked prices this stay implausibly high, so the change
|
|
// did not happen. Reported in the modal the office is standing in front of —
|
|
// the same treatment the create and edit forms give it.
|
|
$this->logger->error('Refused to change accommodation over an implausible booking total', [
|
|
'exception' => $e,
|
|
'id' => $booking->getId(),
|
|
'accommodation' => $selected->getCalendarCode(),
|
|
]);
|
|
$form->addError(new FormError(ImplausibleBookingPriceException::FORM_ERROR_MESSAGE));
|
|
|
|
return $this->render('admin/accommodation_booking/modal_change_accommodation.html.twig', [
|
|
'booking' => $booking,
|
|
'form' => $form,
|
|
]);
|
|
}
|
|
|
|
$this->addFlash('success', 'Das Gruppenhaus wurde geändert. Bitte wähle die Leistungen neu aus.');
|
|
|
|
$this->logger->info('Changed accommodation of accommodation booking', [
|
|
'id' => $booking->getId(),
|
|
'accommodation' => $selected->getName(),
|
|
]);
|
|
}
|
|
|
|
// Back to the edit form, which is rebuilt from the new house: its board and
|
|
// additional service choices are resolved server-side and cannot be swapped in
|
|
// place, which is the whole reason this is a modal and not a field on that form.
|
|
return $this->htmxRedirect($request, $this->editUrl($booking, $request));
|
|
}
|
|
|
|
return $this->render('admin/accommodation_booking/modal_change_accommodation.html.twig', [
|
|
'booking' => $booking,
|
|
'form' => $form,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* The return url is forwarded still encoded, the way return_url() handed it over, so that
|
|
* the edit page keeps leading back to the list the booking was opened from.
|
|
*/
|
|
private function editUrl(AccommodationBooking $booking, Request $request): string
|
|
{
|
|
$parameters = ['id' => $booking->getId()];
|
|
$returnUrl = $request->query->getString('r');
|
|
if ('' !== $returnUrl) {
|
|
$parameters['r'] = $returnUrl;
|
|
}
|
|
|
|
return $this->generateUrl('app_admin_accommodationbooking_edit', $parameters);
|
|
}
|
|
}
|