feat: edit selected accommodation of bookings in draft
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controller\Admin\AccommodationBooking;
|
||||
|
||||
use App\Entity\Groups\Accommodation;
|
||||
use App\Entity\Groups\AccommodationBooking;
|
||||
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\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()) {
|
||||
$this->bookingService->changeAccommodation($booking, $selected);
|
||||
|
||||
$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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Form\Admin\Groups;
|
||||
|
||||
use App\Entity\Groups\Accommodation;
|
||||
use App\Entity\Groups\AccommodationBooking;
|
||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
/**
|
||||
* Corrects the Gruppenhaus of a draft that was created against the wrong one. It is its own
|
||||
* form rather than a field on AccommodationBookingType because the service choices there are
|
||||
* resolved server-side from the current accommodation — changing it has to rebuild the whole
|
||||
* edit form, which is why this runs as a modal that redirects back to it.
|
||||
*
|
||||
* The field is unmapped, the way the service fields on the edit form are: swapping the house
|
||||
* drops the booked services along with it, so the change goes through
|
||||
* AccommodationBookingService::changeAccommodation() as one transition instead of being
|
||||
* written onto the entity piecemeal by the form.
|
||||
*
|
||||
* @extends AbstractType<AccommodationBooking>
|
||||
*/
|
||||
class AccommodationBookingChangeAccommodationType extends AbstractType
|
||||
{
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||
{
|
||||
$builder
|
||||
->add('accommodation', EntityType::class, [
|
||||
'class' => Accommodation::class,
|
||||
'mapped' => false,
|
||||
'choice_label' => 'name',
|
||||
'label' => 'Gruppenhaus neu',
|
||||
'data' => $options['current_accommodation'],
|
||||
])
|
||||
;
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver): void
|
||||
{
|
||||
$resolver->setDefaults([
|
||||
'data_class' => AccommodationBooking::class,
|
||||
// Only ever reached for an Entwurf, which is a scratch record the office fills in
|
||||
// over time — the `edit` group would reject it over contact data this form does
|
||||
// not even show.
|
||||
'validation_groups' => ['Default'],
|
||||
'current_accommodation' => null,
|
||||
]);
|
||||
$resolver->setAllowedTypes('current_accommodation', ['null', Accommodation::class]);
|
||||
}
|
||||
}
|
||||
@@ -530,6 +530,35 @@ class AccommodationBookingService
|
||||
$this->sendCustomerConfirmationEmail($booking);
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves a draft to another Gruppenhaus, for the case where the wrong one was picked at
|
||||
* creation. The chosen Verpflegung and Zusatzleistungen belong to the old house's catalog —
|
||||
* they carry its prices and its ids — so they are dropped rather than guessed at; the office
|
||||
* picks them again from the new catalog on the edit page.
|
||||
* Restricted to Entwurf: anything further along may already be in front of the customer via
|
||||
* their access link, and swapping the house underneath them would rewrite what they were
|
||||
* offered.
|
||||
* Idempotent — a no-op for a booking that is not a draft or already sits at $accommodation.
|
||||
*/
|
||||
public function changeAccommodation(AccommodationBooking $booking, Accommodation $accommodation): void
|
||||
{
|
||||
if (!$booking->isDraft() || $booking->getAccommodation() === $accommodation) {
|
||||
return;
|
||||
}
|
||||
|
||||
$booking->setAccommodation($accommodation);
|
||||
|
||||
$booking->setBoardServiceLabel(null);
|
||||
$booking->setBoardServicePrice(null);
|
||||
$booking->setBoardServiceOriginalId(null);
|
||||
$booking->setAdditionalServices([]);
|
||||
|
||||
// Clears the snapshot by itself when the new house has no price for the booked range.
|
||||
$this->refreshPriceSnapshot($booking);
|
||||
|
||||
$this->entityManager->flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes a booking that is not going to happen. Deliberately silent: the office tells
|
||||
* the customer itself, so this only records the outcome.
|
||||
|
||||
Reference in New Issue
Block a user