forked from Somnia/SomniaRooms
Booking - Small improvements and fixes
This commit is contained in:
@@ -15,6 +15,7 @@ use App\Request\BookingDetailsRequest;
|
|||||||
use App\Response\BookingCreateResponse;
|
use App\Response\BookingCreateResponse;
|
||||||
use App\Service\ConfigVariableService;
|
use App\Service\ConfigVariableService;
|
||||||
use App\Service\PricesService;
|
use App\Service\PricesService;
|
||||||
|
use Doctrine\DBAL\TransactionIsolationLevel;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Nelmio\ApiDocBundle\Attribute\Model;
|
use Nelmio\ApiDocBundle\Attribute\Model;
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
@@ -90,8 +91,6 @@ class BookingController extends AbstractController
|
|||||||
#[MapRequestPayload()] BookingCreateRequest $createRequest
|
#[MapRequestPayload()] BookingCreateRequest $createRequest
|
||||||
): JsonResponse {
|
): JsonResponse {
|
||||||
|
|
||||||
$booking = new Booking();
|
|
||||||
try {
|
|
||||||
if ($createRequest->arrivalDate >= $createRequest->departureDate) {
|
if ($createRequest->arrivalDate >= $createRequest->departureDate) {
|
||||||
throw new ApiErrorException(
|
throw new ApiErrorException(
|
||||||
new ApiErrorMessage(422, ApiErrorMessage::TYPE_INVALID_DATE),
|
new ApiErrorMessage(422, ApiErrorMessage::TYPE_INVALID_DATE),
|
||||||
@@ -99,27 +98,6 @@ class BookingController extends AbstractController
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
$entityManager->beginTransaction();
|
|
||||||
|
|
||||||
$currentTime = new \DateTime("now");
|
|
||||||
|
|
||||||
// Create booking from request data.
|
|
||||||
$booking->setBookingTime($currentTime);
|
|
||||||
$booking->setArrivalDate($createRequest->arrivalDate);
|
|
||||||
$booking->setDepartureDate($createRequest->departureDate);
|
|
||||||
$booking->setBookingCustomer($createRequest->customerDetails);
|
|
||||||
|
|
||||||
//TODOME: Improve refNumber generation.
|
|
||||||
$highest_id = $entityManager->createQueryBuilder()
|
|
||||||
->select('MAX(e.id)')
|
|
||||||
->from('App\Entity\Booking', 'e')
|
|
||||||
->getQuery()
|
|
||||||
->getSingleScalarResult();
|
|
||||||
|
|
||||||
$booking->setRefNumber($currentTime->format("Y") . $highest_id + 1);
|
|
||||||
|
|
||||||
$entityManager->persist($booking);
|
|
||||||
|
|
||||||
if (\count($createRequest->rooms) == 0) {
|
if (\count($createRequest->rooms) == 0) {
|
||||||
throw new ApiErrorException(
|
throw new ApiErrorException(
|
||||||
new ApiErrorMessage(422, ApiErrorMessage::TYPE_VALIDATION_ERROR),
|
new ApiErrorMessage(422, ApiErrorMessage::TYPE_VALIDATION_ERROR),
|
||||||
@@ -141,6 +119,29 @@ class BookingController extends AbstractController
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$currentTime = new \DateTimeImmutable("now");
|
||||||
|
$booking = new Booking();
|
||||||
|
try {
|
||||||
|
|
||||||
|
$entityManager->beginTransaction(TransactionIsolationLevel::REPEATABLE_READ);
|
||||||
|
|
||||||
|
// Create booking from request data.
|
||||||
|
$booking->setBookingTime($currentTime);
|
||||||
|
$booking->setArrivalDate($createRequest->arrivalDate);
|
||||||
|
$booking->setDepartureDate($createRequest->departureDate);
|
||||||
|
$booking->setBookingCustomer($createRequest->customerDetails);
|
||||||
|
|
||||||
|
//TODOME: Improve refNumber generation.
|
||||||
|
$highest_id = $entityManager->createQueryBuilder()
|
||||||
|
->select('MAX(e.id)')
|
||||||
|
->from('App\Entity\Booking', 'e')
|
||||||
|
->getQuery()
|
||||||
|
->getSingleScalarResult();
|
||||||
|
|
||||||
|
$booking->setRefNumber($currentTime->format("Y") . $highest_id + 1);
|
||||||
|
|
||||||
|
$entityManager->persist($booking);
|
||||||
|
|
||||||
// Create each room.
|
// Create each room.
|
||||||
foreach ($createRequest->rooms as $roomItem) {
|
foreach ($createRequest->rooms as $roomItem) {
|
||||||
$room = new BookingRoom();
|
$room = new BookingRoom();
|
||||||
@@ -174,9 +175,9 @@ class BookingController extends AbstractController
|
|||||||
//Get Price for period.
|
//Get Price for period.
|
||||||
$priceList = $priceSrv->getFinalPricesForSearch($roomCategory, $createRequest->arrivalDate, $createRequest->departureDate);
|
$priceList = $priceSrv->getFinalPricesForSearch($roomCategory, $createRequest->arrivalDate, $createRequest->departureDate);
|
||||||
$priceFound = false;
|
$priceFound = false;
|
||||||
foreach ($priceList as $key => $value) {
|
foreach ($priceList as $priceItem) {
|
||||||
if ($key == $selectedGuestAmount) {
|
if ($priceItem->guestNumber == $selectedGuestAmount) {
|
||||||
$room->setRoomPrice($value);
|
$room->setRoomPrice($priceItem->price);
|
||||||
$priceFound = true;
|
$priceFound = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -195,10 +196,12 @@ class BookingController extends AbstractController
|
|||||||
$repoAvailability->updateAvailabilityForRoomCategoryOnPeriod($roomCategory, $createRequest->arrivalDate, $createRequest->departureDate, $selectedAmount, true); //Update using "diff" mode.
|
$repoAvailability->updateAvailabilityForRoomCategoryOnPeriod($roomCategory, $createRequest->arrivalDate, $createRequest->departureDate, $selectedAmount, true); //Update using "diff" mode.
|
||||||
}
|
}
|
||||||
|
|
||||||
$entityManager->commit();
|
|
||||||
$entityManager->flush();
|
$entityManager->flush();
|
||||||
|
$entityManager->commit();
|
||||||
} catch (\Throwable $th) {
|
} catch (\Throwable $th) {
|
||||||
|
if ($entityManager->getConnection()->isTransactionActive()) {
|
||||||
$entityManager->rollback();
|
$entityManager->rollback();
|
||||||
|
}
|
||||||
throw $th;
|
throw $th;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -106,6 +106,7 @@ class RoomAvailabilityRepository extends ServiceEntityRepository
|
|||||||
$strEndDate = $endDate->format('Y-m-d');
|
$strEndDate = $endDate->format('Y-m-d');
|
||||||
$roomCatId = $roomCategory->getId();
|
$roomCatId = $roomCategory->getId();
|
||||||
|
|
||||||
|
/** @var RoomAvailability[] */
|
||||||
$availabilityItemList = $this->createQueryBuilder('ra')
|
$availabilityItemList = $this->createQueryBuilder('ra')
|
||||||
->andWhere('ra.day >= :startDate')
|
->andWhere('ra.day >= :startDate')
|
||||||
->andWhere('ra.day < :endDate')
|
->andWhere('ra.day < :endDate')
|
||||||
@@ -116,8 +117,6 @@ class RoomAvailabilityRepository extends ServiceEntityRepository
|
|||||||
->getQuery()
|
->getQuery()
|
||||||
->getResult();
|
->getResult();
|
||||||
|
|
||||||
$em = $this->getEntityManager();
|
|
||||||
|
|
||||||
foreach ($availabilityItemList as $item) {
|
foreach ($availabilityItemList as $item) {
|
||||||
if ($isDiff) {
|
if ($isDiff) {
|
||||||
$item->modNumAvailable($amount);
|
$item->modNumAvailable($amount);
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ class PricesService
|
|||||||
*
|
*
|
||||||
* e. g. --> [ 2 => 120€, 3 => 180€ ]
|
* e. g. --> [ 2 => 120€, 3 => 180€ ]
|
||||||
*
|
*
|
||||||
* @return array Contiene los precios en función del número de personas.
|
* @return SearchPricesResponsePriceItem[] Contiene los precios en función del número de personas.
|
||||||
*/
|
*/
|
||||||
public function getFinalPricesForSearch(RoomCategory $category, \DateTimeInterface $startDate, \DateTimeInterface $endDate): array
|
public function getFinalPricesForSearch(RoomCategory $category, \DateTimeInterface $startDate, \DateTimeInterface $endDate): array
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user