108 lines
4.5 KiB
PHP
108 lines
4.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Controller\Admin\AccommodationBooking;
|
|
|
|
use App\Entity\Groups\AccommodationBooking;
|
|
use App\Enum\Groups\AccommodationBookingPeriodPosition;
|
|
use App\Exception\ImplausibleBookingPriceException;
|
|
use App\Form\Admin\Groups\AccommodationBookingPeriodType;
|
|
use App\Form\Model\AccommodationBookingPeriodDto;
|
|
use App\Form\Model\AccommodationBookingPeriodLineDto;
|
|
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\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;
|
|
|
|
/**
|
|
* Splits nights off the start or the end of the stay, for a part of the group that arrives
|
|
* early or leaves late and is quoted as free-form lines instead of the booking's parameters.
|
|
* Each side can be split once; the period can be resized afterwards through its edit modal.
|
|
*/
|
|
#[IsGranted('ROLE_GROUPS_MANAGER')]
|
|
class SplitPeriodController extends AbstractController
|
|
{
|
|
use HxTrait;
|
|
use RedirectsToEditFormTrait;
|
|
|
|
public function __construct(
|
|
private readonly AccommodationBookingService $bookingService,
|
|
private readonly LoggerInterface $logger,
|
|
) {
|
|
}
|
|
|
|
#[Route(
|
|
'/admin/accommodation-booking/{id}/period/split/{position}',
|
|
name: 'app_admin_accommodationbooking_period_split',
|
|
requirements: ['position' => 'leading|trailing'],
|
|
)]
|
|
public function index(AccommodationBooking $booking, AccommodationBookingPeriodPosition $position, Request $request): Response
|
|
{
|
|
// One period per side. The edit page only offers the free sides, so this catches a page
|
|
// that was open while somebody else split the same side.
|
|
if (!$booking->canSplitOffPeriod($position)) {
|
|
$this->addFlash('error', AccommodationBookingPeriodPosition::Leading === $position
|
|
? 'Am Anfang wurde bereits ein Zeitraum abgeteilt.'
|
|
: 'Am Ende wurde bereits ein Zeitraum abgeteilt.');
|
|
|
|
return $this->htmxRedirect($request, $this->editUrl($booking, $request));
|
|
}
|
|
|
|
$dto = new AccommodationBookingPeriodDto(1, [new AccommodationBookingPeriodLineDto()]);
|
|
$form = $this->createForm(AccommodationBookingPeriodType::class, $dto, [
|
|
'currency' => $booking->getPricingCurrency() ?? $booking->getAccommodation()?->getCurrency() ?? 'EUR',
|
|
'hx_post' => $request->getRequestUri(),
|
|
]);
|
|
$form->handleRequest($request);
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
|
try {
|
|
$violations = $this->bookingService->splitOffPeriod($booking, $position, $dto);
|
|
|
|
foreach ($violations as $violation) {
|
|
$form->addError(new FormError((string) $violation->getMessage()));
|
|
}
|
|
|
|
if (0 === count($violations)) {
|
|
$this->addFlash('success', 'Der Zeitraum wurde abgeteilt.');
|
|
|
|
$this->logger->info('Split off a period of accommodation booking', [
|
|
'id' => $booking->getId(),
|
|
'position' => $position->value,
|
|
'nights' => $dto->nights,
|
|
]);
|
|
|
|
return $this->htmxRedirect($request, $this->editUrl($booking, $request));
|
|
}
|
|
} catch (ImplausibleBookingPriceException $e) {
|
|
$this->logger->error('Refused to split off a period over an implausible accommodation booking total', [
|
|
'exception' => $e,
|
|
'id' => $booking->getId(),
|
|
]);
|
|
$form->addError(new FormError(ImplausibleBookingPriceException::FORM_ERROR_MESSAGE));
|
|
}
|
|
}
|
|
|
|
return $this->renderModal($booking, $position, $form);
|
|
}
|
|
|
|
/** @param FormInterface<AccommodationBookingPeriodDto> $form */
|
|
private function renderModal(AccommodationBooking $booking, AccommodationBookingPeriodPosition $position, FormInterface $form): Response
|
|
{
|
|
return $this->render('admin/accommodation_booking/modal_period.html.twig', [
|
|
'booking' => $booking,
|
|
'form' => $form,
|
|
'title' => AccommodationBookingPeriodPosition::Leading === $position
|
|
? 'Zeitraum am Anfang abteilen'
|
|
: 'Zeitraum am Ende abteilen',
|
|
]);
|
|
}
|
|
}
|