134 lines
5.4 KiB
PHP
134 lines
5.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Controller\Admin\AccommodationBooking;
|
|
|
|
use App\Entity\Groups\AccommodationBooking;
|
|
use App\Entity\Groups\AdditionalService;
|
|
use App\Entity\Groups\BoardService;
|
|
use App\Form\Admin\Groups\AccommodationBookingType;
|
|
use App\Repository\Groups\AdditionalServiceRepository;
|
|
use App\Repository\Groups\BoardServiceRepository;
|
|
use App\Service\AccommodationBookingService;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
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 EditController extends AbstractController
|
|
{
|
|
public function __construct(
|
|
private readonly EntityManagerInterface $entityManager,
|
|
private readonly LoggerInterface $logger,
|
|
private readonly BoardServiceRepository $boardServiceRepo,
|
|
private readonly AdditionalServiceRepository $additionalServiceRepo,
|
|
private readonly AccommodationBookingService $bookingService,
|
|
) {
|
|
}
|
|
|
|
#[Route('/admin/accommodation-booking/{id}/edit', name: 'app_admin_accommodationbooking_edit')]
|
|
public function index(AccommodationBooking $booking, Request $request): Response
|
|
{
|
|
$accommodation = $booking->getAccommodation();
|
|
$boardServices = [];
|
|
$additionalServices = [];
|
|
$currentBoardService = null;
|
|
$currentAdditionalServices = [];
|
|
|
|
if (null !== $accommodation && null !== $booking->getDateFrom() && null !== $booking->getDateTo()) {
|
|
$boardServices = $this->boardServiceRepo->findByAccommodationAndDateRange(
|
|
$accommodation,
|
|
$booking->getDateFrom(),
|
|
$booking->getDateTo(),
|
|
);
|
|
$additionalServices = $this->additionalServiceRepo->findByAccommodationAndDateRange(
|
|
$accommodation,
|
|
$booking->getDateFrom(),
|
|
$booking->getDateTo(),
|
|
);
|
|
|
|
// Pre-select current board service if it is still in the available choices
|
|
foreach ($boardServices as $bs) {
|
|
if ($bs->getId() === $booking->getBoardServiceOriginalId()) {
|
|
$currentBoardService = $bs;
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Pre-select current additional services that are still in the available choices
|
|
$currentIds = array_column($booking->getAdditionalServices(), 'originalServiceId');
|
|
$currentAdditionalServices = array_values(array_filter(
|
|
$additionalServices,
|
|
fn (AdditionalService $s) => in_array($s->getId(), $currentIds, true),
|
|
));
|
|
}
|
|
|
|
$form = $this->createForm(AccommodationBookingType::class, $booking, [
|
|
'max_adolescent_age' => $accommodation?->getMaxAdolescentAge() ?? 0,
|
|
'board_services' => $boardServices,
|
|
'additional_services' => $additionalServices,
|
|
'current_board_service' => $currentBoardService,
|
|
'current_additional_services' => $currentAdditionalServices,
|
|
]);
|
|
$wasInquiry = $booking->isInquiry();
|
|
|
|
$form->handleRequest($request);
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
|
if ($form->has('boardService')) {
|
|
$selectedBoardService = $form->get('boardService')->getData();
|
|
if ($selectedBoardService instanceof BoardService) {
|
|
$booking->setBoardServiceLabel($selectedBoardService->getLabel());
|
|
$booking->setBoardServicePrice($selectedBoardService->getPrice());
|
|
$booking->setBoardServiceOriginalId($selectedBoardService->getId());
|
|
} else {
|
|
$booking->setBoardServiceLabel(null);
|
|
$booking->setBoardServicePrice(null);
|
|
$booking->setBoardServiceOriginalId(null);
|
|
}
|
|
}
|
|
|
|
if ($form->has('selectedAdditionalServices')) {
|
|
$booking->setAdditionalServices([]);
|
|
foreach ($form->get('selectedAdditionalServices')->getData() as $service) {
|
|
$booking->addAdditionalServiceSnapshot(
|
|
$service->getLabel() ?? '',
|
|
$service->getPrice() ?? 0,
|
|
$service->getType(),
|
|
$service->getId(),
|
|
);
|
|
}
|
|
}
|
|
|
|
$this->bookingService->refreshPriceSnapshot($booking);
|
|
|
|
$this->entityManager->persist($booking);
|
|
$this->entityManager->flush();
|
|
|
|
if ($wasInquiry && !$booking->isInquiry()) {
|
|
$this->bookingService->issueAccessLinkForDirectBooking($booking);
|
|
$this->bookingService->sendCustomerConfirmationEmail($booking);
|
|
}
|
|
|
|
$this->addFlash('success', 'Die Buchung wurde aktualisiert');
|
|
|
|
$this->logger->info('Updated accommodation booking', [
|
|
'id' => $booking->getId(),
|
|
'groupName' => $booking->getGroupName(),
|
|
]);
|
|
|
|
return $this->redirectToRoute('app_admin_accommodationbooking_edit', ['id' => $booking->getId()]);
|
|
}
|
|
|
|
return $this->render('admin/accommodation_booking/edit.html.twig', [
|
|
'booking' => $booking,
|
|
'form' => $form,
|
|
]);
|
|
}
|
|
}
|