From 13c51b32d5d2afb787adac8f714f488a3d54bc82 Mon Sep 17 00:00:00 2001 From: Jitendra Singh Date: Wed, 26 Feb 2020 16:53:17 +0530 Subject: [PATCH] Quantity availability check implemented for event booking --- ...020_02_18_165639_create_bookings_table.php | 2 + .../BookingProduct/src/Helpers/Booking.php | 20 ++++--- .../src/Helpers/DefaultSlot.php | 8 +-- .../src/Helpers/EventTicket.php | 57 ++++++++++++++++++- .../BookingProduct/src/Helpers/RentalSlot.php | 6 +- .../BookingProduct/src/Models/Booking.php | 2 +- .../products/accordians/booking.blade.php | 8 +-- 7 files changed, 81 insertions(+), 22 deletions(-) diff --git a/packages/Webkul/BookingProduct/src/Database/Migrations/2020_02_18_165639_create_bookings_table.php b/packages/Webkul/BookingProduct/src/Database/Migrations/2020_02_18_165639_create_bookings_table.php index 7165d38f5..3cb5d3fc0 100644 --- a/packages/Webkul/BookingProduct/src/Database/Migrations/2020_02_18_165639_create_bookings_table.php +++ b/packages/Webkul/BookingProduct/src/Database/Migrations/2020_02_18_165639_create_bookings_table.php @@ -21,6 +21,8 @@ class CreateBookingsTable extends Migration $table->integer('order_item_id')->unsigned()->nullable(); + $table->integer('booking_product_event_ticket_id')->unsigned()->nullable(); + $table->integer('order_id')->unsigned()->nullable(); $table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade'); diff --git a/packages/Webkul/BookingProduct/src/Helpers/Booking.php b/packages/Webkul/BookingProduct/src/Helpers/Booking.php index 781ece211..d52b35170 100644 --- a/packages/Webkul/BookingProduct/src/Helpers/Booking.php +++ b/packages/Webkul/BookingProduct/src/Helpers/Booking.php @@ -181,9 +181,13 @@ class Booking $currentTime = Carbon::now(); - $availableFrom = Carbon::createFromTimeString($bookingProduct->available_from->format('Y-m-d') . " 00:00:01"); + $availableFrom = ! $bookingProduct->available_from && $bookingProduct->available_from + ? Carbon::createFromTimeString($bookingProduct->available_from) + : clone $currentTime; - $availableTo = Carbon::createFromTimeString($bookingProduct->available_to->format('Y-m-d') . " 23:59:59"); + $availableTo = ! $bookingProduct->available_from && $bookingProduct->available_to + ? Carbon::createFromTimeString($bookingProduct->available_to) + : Carbon::createFromTimeString('2080-01-01 00:00:00'); for ($i = 0; $i < 7; $i++) { $date = clone $currentTime; @@ -260,11 +264,11 @@ class Booking $currentTime = Carbon::now(); $availableFrom = ! $bookingProduct->available_every_week && $bookingProduct->available_from - ? Carbon::createFromTimeString($bookingProduct->available_from->format('Y-m-d') . " 00:00:00") - : Carbon::createFromTimeString($currentTime->format('Y-m-d') . ' 00:00:00'); + ? Carbon::createFromTimeString($bookingProduct->available_from) + : Carbon::createFromTimeString($currentTime); $availableTo = ! $bookingProduct->available_every_week && $bookingProduct->available_from - ? Carbon::createFromTimeString($bookingProduct->available_to->format('Y-m-d') . ' 23:59:59') + ? Carbon::createFromTimeString($bookingProduct->available_to) : Carbon::createFromTimeString('2080-01-01 00:00:00'); $timeDurations = $bookingProductSlot->same_slot_all_days @@ -374,7 +378,7 @@ class Booking ->where('bookings.to', $timestamps[1]) ->first(); - return $result->total_qty_booked; + return ! is_null($result->total_qty_booked) ? $result->total_qty_booked : 0; } /** @@ -403,11 +407,11 @@ class Booking ], [ 'attribute_name' => 'Rent From', 'option_id' => 0, - 'option_label' => Carbon::createFromTimeString($bookingProduct->available_from->format('Y-m-d'))->format('d F, Y'), + 'option_label' => Carbon::createFromTimeString($bookingProduct->available_from)->format('d F, Y'), ], [ 'attribute_name' => 'Rent Till', 'option_id' => 0, - 'option_label' => Carbon::createFromTimeString($bookingProduct->available_to->format('Y-m-d'))->format('d F, Y'), + 'option_label' => Carbon::createFromTimeString($bookingProduct->available_to)->format('d F, Y'), ]]; break; diff --git a/packages/Webkul/BookingProduct/src/Helpers/DefaultSlot.php b/packages/Webkul/BookingProduct/src/Helpers/DefaultSlot.php index 3dba01805..81d2bc095 100644 --- a/packages/Webkul/BookingProduct/src/Helpers/DefaultSlot.php +++ b/packages/Webkul/BookingProduct/src/Helpers/DefaultSlot.php @@ -37,11 +37,11 @@ class DefaultSlot extends Booking $currentTime = Carbon::now(); $availableFrom = ! $bookingProduct->available_from && $bookingProduct->available_from - ? Carbon::createFromTimeString($bookingProduct->available_from->format('Y-m-d') . ' 00:00:00') + ? Carbon::createFromTimeString($bookingProduct->available_from) : clone $currentTime; $availableTo = ! $bookingProduct->available_from && $bookingProduct->available_to - ? Carbon::createFromTimeString($bookingProduct->available_to->format('Y-m-d') . ' 23:59:59') + ? Carbon::createFromTimeString($bookingProduct->available_to) : Carbon::createFromTimeString('2080-01-01 00:00:00'); if ($requestedDate < $currentTime @@ -101,11 +101,11 @@ class DefaultSlot extends Booking $currentTime = Carbon::now(); $availableFrom = ! $bookingProduct->available_from && $bookingProduct->available_from - ? Carbon::createFromTimeString($bookingProduct->available_from->format('Y-m-d') . ' 00:00:00') + ? Carbon::createFromTimeString($bookingProduct->available_from) : clone $currentTime; $availableTo = ! $bookingProduct->available_from && $bookingProduct->available_to - ? Carbon::createFromTimeString($bookingProduct->available_to->format('Y-m-d') . ' 23:59:59') + ? Carbon::createFromTimeString($bookingProduct->available_to) : Carbon::createFromTimeString('2080-01-01 00:00:00'); $timeDuration = $bookingProductSlot->slots[$requestedDate->format('w')] ?? []; diff --git a/packages/Webkul/BookingProduct/src/Helpers/EventTicket.php b/packages/Webkul/BookingProduct/src/Helpers/EventTicket.php index e6a073b44..41080bdf0 100644 --- a/packages/Webkul/BookingProduct/src/Helpers/EventTicket.php +++ b/packages/Webkul/BookingProduct/src/Helpers/EventTicket.php @@ -2,6 +2,7 @@ namespace Webkul\BookingProduct\Helpers; +use Illuminate\Support\Facades\DB; use Carbon\Carbon; /** @@ -20,9 +21,9 @@ class EventTicket extends Booking */ public function getEventDate($bookingProduct) { - $from = Carbon::createFromTimeString($bookingProduct->available_from->format('Y-m-d'))->format('d F, Y h:i A'); + $from = Carbon::createFromTimeString($bookingProduct->available_from)->format('d F, Y h:i A'); - $to = Carbon::createFromTimeString($bookingProduct->available_to->format('Y-m-d'))->format('d F, Y h:i A'); + $to = Carbon::createFromTimeString($bookingProduct->available_to)->format('d F, Y h:i A'); return $from . ' - ' . $to; } @@ -60,6 +61,58 @@ class EventTicket extends Booking return $tickets; } + /** + * @param CartItem $cartItem + * @return bool + */ + public function isItemHaveQuantity($cartItem) + { + $bookingProduct = $this->bookingProductRepository->findOneByField('product_id', $cartItem->product_id); + + $ticket = $bookingProduct->event_tickets()->find($cartItem['additional']['booking']['ticket_id']); + + if ($ticket->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']); + + $ticket = $bookingProduct->event_tickets()->find($cartProduct['additional']['booking']['ticket_id']); + + if ($ticket->qty - $this->getBookedQuantity($cartProduct) < $cartProduct['quantity']) { + return false; + } + } + + return true; + } + + /** + * @param array $data + * @return integer + */ + public function getBookedQuantity($data) + { + $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('bookings.booking_product_event_ticket_id', $data['additional']['booking']['ticket_id']) + ->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/Helpers/RentalSlot.php b/packages/Webkul/BookingProduct/src/Helpers/RentalSlot.php index dc88ffc34..69a3228a4 100644 --- a/packages/Webkul/BookingProduct/src/Helpers/RentalSlot.php +++ b/packages/Webkul/BookingProduct/src/Helpers/RentalSlot.php @@ -32,11 +32,11 @@ class RentalSlot extends Booking $currentTime = Carbon::now(); $availableFrom = ! $bookingProduct->available_every_week && $bookingProduct->available_from - ? Carbon::createFromTimeString($bookingProduct->available_from->format('Y-m-d') . ' 00:00:00') - : Carbon::createFromTimeString($currentTime->format('Y-m-d') . ' 00:00:00'); + ? Carbon::createFromTimeString($bookingProduct->available_from) + : Carbon::createFromTimeString($currentTime); $availableTo = ! $bookingProduct->available_every_week && $bookingProduct->available_from - ? Carbon::createFromTimeString($bookingProduct->available_to->format('Y-m-d') . ' 23:59:59') + ? Carbon::createFromTimeString($bookingProduct->available_to) : Carbon::createFromTimeString('2080-01-01 00:00:00'); $timeDurations = $bookingProductSlot->same_slot_all_days diff --git a/packages/Webkul/BookingProduct/src/Models/Booking.php b/packages/Webkul/BookingProduct/src/Models/Booking.php index a5e4f5a1c..78f2b23dc 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', 'order_id']; + protected $fillable = ['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/Resources/views/admin/catalog/products/accordians/booking.blade.php b/packages/Webkul/BookingProduct/src/Resources/views/admin/catalog/products/accordians/booking.blade.php index 71df0d525..1e45713f9 100644 --- a/packages/Webkul/BookingProduct/src/Resources/views/admin/catalog/products/accordians/booking.blade.php +++ b/packages/Webkul/BookingProduct/src/Resources/views/admin/catalog/products/accordians/booking.blade.php @@ -61,9 +61,9 @@
- + - + @{{ errors.first('booking[available_from]') }}
@@ -71,9 +71,9 @@
- + - + @{{ errors.first('booking[available_to]') }}