This commit is contained in:
@@ -0,0 +1,160 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller\Admin;
|
||||
|
||||
use App\Consts\Consts;
|
||||
use App\DTO\SearchFilterByDate;
|
||||
use App\Entity\RoomCategory;
|
||||
use App\Entity\RoomPrice;
|
||||
use App\Enum\AdminErrorEnum;
|
||||
use App\Form\RoomPriceFormType;
|
||||
use App\Form\SearchFilterByDateFormType;
|
||||
use App\Repository\RoomCategoryRepository;
|
||||
use App\Repository\RoomPriceRepository;
|
||||
use App\Service\AdminUtils;
|
||||
use DateInterval;
|
||||
use DateTimeImmutable;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use EasyCorp\Bundle\EasyAdminBundle\Attribute\AdminRoute;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
||||
|
||||
#[IsGranted("ROLE_ADMIN")]
|
||||
class RoomPriceAdminController extends AbstractController
|
||||
{
|
||||
public const string ROUTE_INDEX = 'roomPrice_prices';
|
||||
public const string ROUTE_CREATE = 'roomPrice_prices_create';
|
||||
public const string ROUTE_REMOVE = 'roomPrice_prices_remove';
|
||||
|
||||
private const string SESSION_SEARCHFILTER = 'RoomPrice_SearchFilter';
|
||||
|
||||
public function __construct(
|
||||
private EntityManagerInterface $entityManager,
|
||||
private AdminUtils $adminUtils,
|
||||
private RoomCategoryRepository $catRepo,
|
||||
private RoomPriceRepository $priceRepo
|
||||
) {}
|
||||
|
||||
// List Room Prices from category.
|
||||
#[AdminRoute('/roomPrices/{categoryId}', name: RoomPriceAdminController::ROUTE_INDEX)]
|
||||
public function roomPrices(Request $request, string $categoryId): Response
|
||||
{
|
||||
$session = $request->getSession();
|
||||
|
||||
$roomCategoryItem = $this->catRepo->find($categoryId);
|
||||
if ($roomCategoryItem === null) {
|
||||
return $this->adminUtils->goToAdminErrorType(AdminErrorEnum::ENTITY_NOTFOUND);
|
||||
}
|
||||
|
||||
/** @var SearchFilterByDate */
|
||||
$searchFilter = $session->get(RoomPriceAdminController::SESSION_SEARCHFILTER, null);
|
||||
|
||||
//Set default values to filter.
|
||||
if ($searchFilter == null) {
|
||||
$searchFilter = new SearchFilterByDate();
|
||||
|
||||
$dtFrom = (new DateTimeImmutable())->sub(new DateInterval("P1M"));
|
||||
$searchFilter->setDtFrom($dtFrom);
|
||||
$dtTo = (new DateTimeImmutable())->add(new DateInterval("P365D"));
|
||||
$searchFilter->setDtTo($dtTo);
|
||||
}
|
||||
|
||||
$searchForm = $this->createForm(SearchFilterByDateFormType::class, $searchFilter);
|
||||
$searchForm->handleRequest($request);
|
||||
|
||||
$roomPriceList = $this->priceRepo->getListForPeriod($searchFilter->getDtFrom(), $searchFilter->getDtTo(), $categoryId);
|
||||
|
||||
//Store value in session for following requests.
|
||||
$session->set(RoomPriceAdminController::SESSION_SEARCHFILTER, $searchFilter);
|
||||
|
||||
return $this->render('admin/room_price/roomPriceList.html.twig', [
|
||||
'searchForm' => $searchForm,
|
||||
'categoryData' => $roomCategoryItem,
|
||||
'roomPriceList' => $roomPriceList,
|
||||
]);
|
||||
}
|
||||
|
||||
#[AdminRoute('/roomPrices/{categoryId}/new', name: RoomPriceAdminController::ROUTE_CREATE)]
|
||||
public function create(Request $request, string $categoryId): Response
|
||||
{
|
||||
$roomCategoryItem = $this->catRepo->find($categoryId);
|
||||
if ($roomCategoryItem === null) {
|
||||
return $this->adminUtils->goToAdminErrorType(AdminErrorEnum::ENTITY_NOTFOUND);
|
||||
}
|
||||
|
||||
$roomPriceItem = new RoomPrice();
|
||||
$roomPriceItem->setRoomCategory($roomCategoryItem);
|
||||
|
||||
$form = $this->createForm(RoomPriceFormType::class, $roomPriceItem, [
|
||||
'selectRoomCategory' => false,
|
||||
]);
|
||||
|
||||
$form->handleRequest($request);
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
// $form->getData() holds the submitted values
|
||||
// but, the original `$availabilityItem` variable has also been updated
|
||||
|
||||
if ($this->isRoomPriceCreationValid($roomPriceItem, $roomCategoryItem)) {
|
||||
$this->entityManager->persist($roomPriceItem);
|
||||
$this->entityManager->flush();
|
||||
|
||||
$this->addFlash('success', 'Element created!');
|
||||
return $this->goToIndex($categoryId);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->render('admin/adminEdit.html.twig', [
|
||||
'form' => $form,
|
||||
'ElementType' => "Room Categoy Bed Type",
|
||||
'ElementName' => "Room Categoy - " . $roomCategoryItem
|
||||
]);
|
||||
}
|
||||
|
||||
#[AdminRoute('/roomPrices/{categoryId}/remove/{date}/{guestNumber}', name: RoomPriceAdminController::ROUTE_REMOVE)]
|
||||
public function remove(string $categoryId, string $date, string $guestNumber): Response
|
||||
{
|
||||
$item = $this->priceRepo->find(['roomCategory' => $categoryId, 'date' => $date, 'guestNumber' => $guestNumber]);
|
||||
if ($item === null) {
|
||||
return $this->adminUtils->goToAdminErrorType(AdminErrorEnum::ENTITY_NOTFOUND);
|
||||
}
|
||||
|
||||
$this->entityManager->remove($item);
|
||||
$this->entityManager->flush();
|
||||
|
||||
$this->addFlash('success', 'Element deleted!');
|
||||
return $this->goToIndex($categoryId);
|
||||
}
|
||||
|
||||
|
||||
|
||||
private function goToIndex(int $roomCategory): Response
|
||||
{
|
||||
return $this->redirectToRoute(AdminUtils::GetAdminRoute(RoomPriceAdminController::ROUTE_INDEX), [
|
||||
'categoryId' => $roomCategory,
|
||||
]);
|
||||
}
|
||||
|
||||
private function isRoomPriceCreationValid(RoomPrice $priceItem, RoomCategory $roomCategoryItem): bool
|
||||
{
|
||||
$roomMaxGuestNum = $roomCategoryItem->getMaxGuestNum();
|
||||
if ($priceItem->getGuestNumber() > $roomMaxGuestNum) {
|
||||
$msg = "Guest number is greater than Room Category max Guest number ('$roomMaxGuestNum')";
|
||||
$this->addFlash('error', $msg);
|
||||
return false;
|
||||
}
|
||||
|
||||
$alreadyExistsItem = $this->priceRepo->find([
|
||||
'roomCategory' => $priceItem->getRoomCategory()->getId(),
|
||||
'date' => $priceItem->getDate()->format(Consts::FORMAT_DATE),
|
||||
'guestNumber' => $priceItem->getGuestNumber()
|
||||
]);
|
||||
if ($alreadyExistsItem != null) {
|
||||
$this->addFlash('error', 'Price already exist!');
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user