Quantity availability check implemented for rental booking

This commit is contained in:
Jitendra Singh 2020-02-26 19:49:04 +05:30
parent 13c51b32d5
commit e08f09f7d2
3 changed files with 92 additions and 9 deletions

View File

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

View File

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

View File

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