diff --git a/packages/Webkul/BookingProduct/src/Helpers/Booking.php b/packages/Webkul/BookingProduct/src/Helpers/Booking.php index 0af6201f3..ddf4be7a9 100644 --- a/packages/Webkul/BookingProduct/src/Helpers/Booking.php +++ b/packages/Webkul/BookingProduct/src/Helpers/Booking.php @@ -3,6 +3,7 @@ namespace Webkul\BookingProduct\Helpers; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Arr; use Carbon\Carbon; use Webkul\BookingProduct\Repositories\BookingProductRepository; use Webkul\BookingProduct\Repositories\BookingProductDefaultSlotRepository; @@ -269,6 +270,13 @@ class Booking ? $bookingProductSlot->slots : ($bookingProductSlot->slots[$requestedDate->format('w')] ?? []); + if ($requestedDate < $currentTime + || $requestedDate < $availableFrom + || $requestedDate > $availableTo + ) { + return []; + } + $slots = []; foreach ($timeDurations as $timeDuration) { @@ -332,7 +340,7 @@ class Booking { $bookingProduct = $this->bookingProductRepository->findOneByField('product_id', $cartItem['product_id']); - if ($bookingProduct->qty - $this->getBookedQuantity($cartItem) < $cartItem['quantity']) { + if ($bookingProduct->qty - $this->getBookedQuantity($cartItem) < $cartItem['quantity'] || $this->isSlotExpired($cartItem)) { return false; } @@ -354,6 +362,25 @@ class Booking return true; } + /** + * @param \Webkul\Ceckout\Contracts\CartItem|array $cartItem + * @return bool + */ + public function isSlotExpired($cartItem) + { + $bookingProduct = $this->bookingProductRepository->findOneByField('product_id', $cartItem['product_id']); + + $typeHelper = app($this->typeHelpers[$bookingProduct->type]); + + $slots = $typeHelper->getSlotsByDate($bookingProduct, $cartItem['additional']['booking']['date']); + + $filtered = Arr::where($slots, function ($slot, $key) use ($cartItem) { + return $slot['timestamp'] == $cartItem['additional']['booking']['slot']; + }); + + return count($filtered) ? false : true; + } + /** * @param array $data * @return int @@ -501,7 +528,7 @@ class Booking * Validate cart item product price * * @param \Webkul\Checkout\Contracts\CartItem $item - * @return float + * @return void */ public function validateCartItem($item) { diff --git a/packages/Webkul/BookingProduct/src/Helpers/RentalSlot.php b/packages/Webkul/BookingProduct/src/Helpers/RentalSlot.php index e876811be..d2a125e79 100644 --- a/packages/Webkul/BookingProduct/src/Helpers/RentalSlot.php +++ b/packages/Webkul/BookingProduct/src/Helpers/RentalSlot.php @@ -38,6 +38,13 @@ class RentalSlot extends Booking ? $bookingProductSlot->slots : $bookingProductSlot->slots[$requestedDate->format('w')]; + if ($requestedDate < $currentTime + || $requestedDate < $availableFrom + || $requestedDate > $availableTo + ) { + return []; + } + $slots = []; foreach ($timeDurations as $index => $timeDuration) { @@ -126,6 +133,33 @@ class RentalSlot extends Booking return ! is_null($result->total_qty_booked) ? $result->total_qty_booked : 0; } + /** + * @param \Webkul\Ceckout\Contracts\CartItem|array $cartItem + * @return bool + */ + public function isSlotExpired($cartItem) + { + $bookingProduct = $this->bookingProductRepository->findOneByField('product_id', $cartItem['product_id']); + + $typeHelper = app($this->typeHelpers[$bookingProduct->type]); + + $timeIntervals = $typeHelper->getSlotsByDate($bookingProduct, $cartItem['additional']['booking']['date']); + + $isExpired = true; + + foreach ($timeIntervals as $timeInterval) { + foreach ($timeInterval['slots'] as $slot) { + if ($slot['from_timestamp'] == $cartItem['additional']['booking']['slot']['from'] + && $slot['to_timestamp'] == $cartItem['additional']['booking']['slot']['to'] + ) { + $isExpired = false; + } + } + } + + return $isExpired; + } + /** * Add booking additional prices to cart item * diff --git a/packages/Webkul/Checkout/src/Cart.php b/packages/Webkul/Checkout/src/Cart.php index 811ef3145..717ee7d4b 100755 --- a/packages/Webkul/Checkout/src/Cart.php +++ b/packages/Webkul/Checkout/src/Cart.php @@ -706,7 +706,7 @@ class Cart return false; } else { foreach ($cart->items as $item) { - $item->product->getTypeInstance()->validateCartItem($item); + $response = $item->product->getTypeInstance()->validateCartItem($item); $price = ! is_null($item->custom_price) ? $item->custom_price : $item->base_price;