From e08f09f7d25fca87342e9adfcbf28359078e2e6e Mon Sep 17 00:00:00 2001 From: Jitendra Singh Date: Wed, 26 Feb 2020 19:49:04 +0530 Subject: [PATCH] Quantity availability check implemented for rental booking --- .../BookingProduct/src/Helpers/RentalSlot.php | 70 +++++++++++++++++++ .../BookingProduct/src/Models/Booking.php | 2 +- .../src/Repositories/BookingRepository.php | 29 +++++--- 3 files changed, 92 insertions(+), 9 deletions(-) diff --git a/packages/Webkul/BookingProduct/src/Helpers/RentalSlot.php b/packages/Webkul/BookingProduct/src/Helpers/RentalSlot.php index 69a3228a4..f80770373 100644 --- a/packages/Webkul/BookingProduct/src/Helpers/RentalSlot.php +++ b/packages/Webkul/BookingProduct/src/Helpers/RentalSlot.php @@ -2,6 +2,7 @@ namespace Webkul\BookingProduct\Helpers; +use Illuminate\Support\Facades\DB; use Carbon\Carbon; /** @@ -94,6 +95,75 @@ class RentalSlot extends Booking return $slots; } + /** + * @param CartItem $cartItem + * @return bool + */ + public function isItemHaveQuantity($cartItem) + { + $bookingProduct = $this->bookingProductRepository->findOneByField('product_id', $cartItem->product_id); + + if ($bookingProduct->qty - $this->getBookedQuantity($cartItem) < $cartItem->quantity) { + return false; + } + + return true; + } + + /** + * @param array $cartProducts + * @return bool + */ + public function isSlotAvailable($cartProducts) + { + foreach ($cartProducts as $cartProduct) { + $bookingProduct = $this->bookingProductRepository->findOneByField('product_id', $cartProduct['product_id']); + + if ($bookingProduct->qty - $this->getBookedQuantity($cartProduct) < $cartProduct['quantity']) { + return false; + } + } + + return true; + } + + /** + * @param array $data + * @return integer + */ + public function getBookedQuantity($data) + { + $bookingProduct = $this->bookingProductRepository->findOneByField('product_id', $data['product_id']); + + $rentingType = $products[0]['additional']['booking']['renting_type'] ?? $bookingProduct->rental_slot->renting_type; + + if ($rentingType == 'daily') { + $from = Carbon::createFromTimeString($data['additional']['booking']['date_from'] . ' 00:00:01')->getTimestamp(); + + $to = Carbon::createFromTimeString($data['additional']['booking']['date_to'] . ' 23:59:59')->getTimestamp(); + } else { + $from = Carbon::createFromTimestamp($data['additional']['booking']['slot']['from'])->getTimestamp(); + + $to = Carbon::createFromTimestamp($data['additional']['booking']['slot']['to'])->getTimestamp(); + } + + $result = $this->bookingRepository->getModel() + ->leftJoin('order_items', 'bookings.order_item_id', '=', 'order_items.id') + ->addSelect(DB::raw('SUM(qty_ordered - qty_canceled - qty_refunded) as total_qty_booked')) + ->where('bookings.product_id', $data['product_id']) + ->where(function ($query) use($from, $to) { + $query->where(function ($query) use($from) { + $query->where('bookings.from', '<=', $from)->where('bookings.to', '>=', $from); + }) + ->orWhere(function($query) use($to) { + $query->where('bookings.from', '<=', $to)->where('bookings.to', '>=', $to); + }); + }) + ->first(); + + return ! is_null($result->total_qty_booked) ? $result->total_qty_booked : 0; + } + /** * Add booking additional prices to cart item * diff --git a/packages/Webkul/BookingProduct/src/Models/Booking.php b/packages/Webkul/BookingProduct/src/Models/Booking.php index 78f2b23dc..364965886 100644 --- a/packages/Webkul/BookingProduct/src/Models/Booking.php +++ b/packages/Webkul/BookingProduct/src/Models/Booking.php @@ -11,7 +11,7 @@ class Booking extends Model implements BookingContract { public $timestamps = false; - protected $fillable = ['from', 'to', 'order_item_id', 'booking_product_event_ticket_id', 'product_id', 'order_id']; + protected $fillable = ['qty', 'from', 'to', 'order_item_id', 'booking_product_event_ticket_id', 'product_id', 'order_id']; /** * Get the order record associated with the order item. diff --git a/packages/Webkul/BookingProduct/src/Repositories/BookingRepository.php b/packages/Webkul/BookingProduct/src/Repositories/BookingRepository.php index 00836aa46..173cde59d 100644 --- a/packages/Webkul/BookingProduct/src/Repositories/BookingRepository.php +++ b/packages/Webkul/BookingProduct/src/Repositories/BookingRepository.php @@ -4,6 +4,7 @@ namespace Webkul\BookingProduct\Repositories; use Illuminate\Container\Container as App; use Illuminate\Support\Facades\Event; +use Carbon\Carbon; use Webkul\Core\Eloquent\Repository; /** @@ -42,19 +43,31 @@ class BookingRepository extends Repository $from = $to = null; if (isset($item->additional['booking']['slot'])) { - $timestamps = explode('-', $item->additional['booking']['slot']); + if (isset($item->additional['booking']['slot']['from']) && isset($item->additional['booking']['slot']['to'])) { + $from = $item->additional['booking']['slot']['from']; - $from = current($timestamps); + $to = $item->additional['booking']['slot']['to']; + } else { + $timestamps = explode('-', $item->additional['booking']['slot']); - $to = end($timestamps); + $from = current($timestamps); + + $to = end($timestamps); + } + } elseif (isset($item->additional['booking']['date_from']) && isset($item->additional['booking']['date_to'])) { + $from = Carbon::createFromTimeString($item->additional['booking']['date_from'] . ' 00:00:00')->getTimestamp(); + + $to = Carbon::createFromTimeString($item->additional['booking']['date_to'] . ' 23:59:59')->getTimestamp(); } $booking = parent::create([ - 'qty' => $item->qty_ordered, - 'from' => $from, - 'to' => $to, - 'order_id' => $order->id, - 'order_item_id' => $item->id + 'qty' => $item->qty_ordered, + 'from' => $from, + 'to' => $to, + 'order_id' => $order->id, + 'order_item_id' => $item->id, + 'product_id' => $item->product_id, + 'booking_product_event_ticket_id' => $item->additional['booking']['ticket_id'] ?? null, ]); Event::dispatch('marketplace.booking.save.after', $booking);