wip: booking process
This commit is contained in:
@@ -2,49 +2,117 @@
|
||||
|
||||
namespace App\Controller\Booking;
|
||||
|
||||
use App\BusProNet\XmlLoader\TravelLoader;
|
||||
use App\Service\BookingService;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use App\Form\BookingCreateStep1Type;
|
||||
use App\Form\BookingCreateStep2Type;
|
||||
|
||||
class CreateController extends AbstractController
|
||||
{
|
||||
public function __construct(private readonly TravelLoader $travelDataLoader)
|
||||
{}
|
||||
public function __construct(
|
||||
private readonly BookingService $bookingCreateService,
|
||||
) {
|
||||
}
|
||||
|
||||
#[Route('/bookings/create', name: 'app_booking_create', methods: ['POST'])]
|
||||
#[Route('/bookings/create', name: 'app_booking_create_step_1')]
|
||||
public function index(Request $request): Response
|
||||
{
|
||||
$travelId = $request->query->getInt('travel_id');
|
||||
$hotelId = $request->query->getInt('hotel_id');
|
||||
$bookingCreateDto = $this->bookingCreateService->getOrCreateBookingCreateDto($request);
|
||||
|
||||
$roomsIdsAndQuantities = $this->getRoomsIdsAndQuantities($request);
|
||||
// Validate step access - allow step 1 or redirect to current step
|
||||
$this->validateStepAccess($bookingCreateDto, 1);
|
||||
|
||||
$travelData = $this->travelDataLoader->loadById($travelId, $hotelId);
|
||||
$form = $this->createForm(BookingCreateStep1Type::class, $bookingCreateDto);
|
||||
|
||||
if (null === $travelData) {
|
||||
throw $this->createNotFoundException('Travel data not found');
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$bookingCreateDto->currentStep = 2;
|
||||
$this->bookingCreateService->saveBookingCreateDto($request, $bookingCreateDto);
|
||||
|
||||
return $this->redirectToRoute('app_booking_create_step_2');
|
||||
}
|
||||
|
||||
$participantsCount = 0;
|
||||
|
||||
$rooms = $travelData->getRoomsByIds(array_keys($roomsIdsAndQuantities));
|
||||
|
||||
foreach ($rooms as $room) {
|
||||
$roomCount = $roomsIdsAndQuantities[$room->id] ?? 0;
|
||||
$participantsCount += $room->minPax * $roomCount;
|
||||
}
|
||||
|
||||
return $this->render('booking/create.html.twig', [
|
||||
'travelData' => $travelData,
|
||||
return $this->render('booking/create_step_1.html.twig', [
|
||||
'bookingCreateDto' => $bookingCreateDto,
|
||||
'form' => $form->createView(),
|
||||
]);
|
||||
}
|
||||
|
||||
private function getRoomsIdsAndQuantities(Request $request): array
|
||||
#[Route('/bookings/create/participants', name: 'app_booking_create_step_2')]
|
||||
public function participants(Request $request): Response
|
||||
{
|
||||
$rooms = $request->request->all('rooms');
|
||||
$bookingCreateDto = $this->bookingCreateService->getOrCreateBookingCreateDto($request);
|
||||
|
||||
return array_map('intval', array_filter($rooms, 'strlen'));
|
||||
// Validate step access
|
||||
$this->validateStepAccess($bookingCreateDto, 2);
|
||||
|
||||
$form = $this->createForm(BookingCreateStep2Type::class, $bookingCreateDto);
|
||||
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$bookingCreateDto->currentStep = 3;
|
||||
$this->bookingCreateService->saveBookingCreateDto($request, $bookingCreateDto);
|
||||
|
||||
return $this->redirectToRoute('app_booking_create_step_3');
|
||||
}
|
||||
|
||||
return $this->render('booking/create_step_2.html.twig', [
|
||||
'bookingCreateDto' => $bookingCreateDto,
|
||||
'form' => $form->createView(),
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/bookings/create/confirm', name: 'app_booking_create_step_3')]
|
||||
public function confirm(Request $request): Response
|
||||
{
|
||||
$bookingCreateDto = $this->bookingCreateService->getOrCreateBookingCreateDto($request);
|
||||
|
||||
// Validate step access
|
||||
$this->validateStepAccess($bookingCreateDto, 3);
|
||||
|
||||
return $this->render('booking/confirm.html.twig', [
|
||||
'bookingCreateDto' => $bookingCreateDto,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates step access and redirects if necessary.
|
||||
*
|
||||
* @param \App\Form\Model\BookingCreateDto $bookingCreateDto
|
||||
* @param int $expectedStep
|
||||
*/
|
||||
private function validateStepAccess($bookingCreateDto, int $expectedStep): void
|
||||
{
|
||||
// Allow access to current step or any previous step
|
||||
if ($expectedStep > $bookingCreateDto->currentStep) {
|
||||
$this->addFlash('error', 'Bitte erst die vorherigen Schritte abschließen.');
|
||||
|
||||
$this->redirectToCurrentStep($bookingCreateDto);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Redirects to the current step based on the DTO's currentStep.
|
||||
*/
|
||||
private function redirectToCurrentStep($bookingCreateDto): void
|
||||
{
|
||||
$routeParams = [
|
||||
'travel_id' => $bookingCreateDto->travelData->id,
|
||||
'hotel_id' => $bookingCreateDto->travelData->hotelId,
|
||||
];
|
||||
|
||||
$route = match ($bookingCreateDto->currentStep) {
|
||||
1 => 'app_booking_create_step_1',
|
||||
2 => 'app_booking_create_step_2',
|
||||
3 => 'app_booking_create_step_3',
|
||||
default => 'app_booking_create_step_1',
|
||||
};
|
||||
|
||||
$this->redirectToRoute($route, $routeParams);
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@ use App\BusProNet\XmlLoader\PickupLoader;
|
||||
use App\BusProNet\XmlLoader\TravelLoader;
|
||||
use App\Controller\Traits\BookingDataTrait;
|
||||
use App\Entity\User;
|
||||
use App\Form\BookingType;
|
||||
use App\Form\BookingEditType;
|
||||
use App\Form\Model\BookingEditDto;
|
||||
use App\Security\Crypt;
|
||||
use Psr\Cache\InvalidArgumentException;
|
||||
@@ -92,7 +92,7 @@ class EditController extends AbstractController
|
||||
// Create DTO for form
|
||||
$formData = BookingEditDto::fromBooking($bookingData, $travelData);
|
||||
|
||||
$form = $this->createForm(BookingType::class, $formData, [
|
||||
$form = $this->createForm(BookingEditType::class, $formData, [
|
||||
'attr' => ['novalidate' => 'novalidate'],
|
||||
]);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user