Quantity availability check implemented for event booking

This commit is contained in:
Jitendra Singh 2020-02-26 16:53:17 +05:30
parent 53da36dd2c
commit 13c51b32d5
7 changed files with 81 additions and 22 deletions

View File

@ -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');

View File

@ -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;

View File

@ -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')] ?? [];

View File

@ -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
*

View File

@ -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

View File

@ -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.

View File

@ -61,9 +61,9 @@
<div class="control-group" :class="[errors.has('booking[available_from]') ? 'has-error' : '']">
<label class="required">{{ __('bookingproduct::app.admin.catalog.products.available-from') }}</label>
<date>
<datetime>
<input type="text" v-validate="'required'" name="booking[available_from]" v-model="booking.available_from" class="control" data-vv-as="&quot;{{ __('bookingproduct::app.admin.catalog.products.available-from') }}&quot;"/>
</date>
</datetime>
<span class="control-error" v-if="errors.has('booking[available_from]')">@{{ errors.first('booking[available_from]') }}</span>
</div>
@ -71,9 +71,9 @@
<div class="control-group" :class="[errors.has('booking[available_to]') ? 'has-error' : '']">
<label class="required">{{ __('bookingproduct::app.admin.catalog.products.available-to') }}</label>
<date>
<datetime>
<input type="text" v-validate="'required'" name="booking[available_to]" v-model="booking.available_to" class="control" data-vv-as="&quot;{{ __('bookingproduct::app.admin.catalog.products.available-to') }}&quot;"/>
</date>
</datetime>
<span class="control-error" v-if="errors.has('booking[available_to]')">@{{ errors.first('booking[available_to]') }}</span>
</div>