diff --git a/packages/Webkul/BookingProduct/src/Helpers/Booking.php b/packages/Webkul/BookingProduct/src/Helpers/Booking.php index 0e52a2d9f..cdba3f794 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 Carbon\Carbon; +use Webkul\BookingProduct\Repositories\BookingProductRepository; use Webkul\BookingProduct\Repositories\BookingProductDefaultSlotRepository; use Webkul\BookingProduct\Repositories\BookingProductAppointmentSlotRepository; use Webkul\BookingProduct\Repositories\BookingProductEventSlotRepository; @@ -17,11 +18,29 @@ use Webkul\BookingProduct\Repositories\BookingProductTableSlotRepository; */ class Booking { + /** + * BookingProductRepository + * + * @return object + */ + protected $bookingProductRepository; + /** * @return array */ protected $typeRepositories = []; + /** + * @return array + */ + protected $typeHelpers = [ + 'default' => DefaultSlot::class, + 'appointment' => AppointmentSlot::class, + 'event' => EventSlot::class, + 'rental' => RentalSlot::class, + 'table' => TableSlot::class, + ]; + /** * @return array */ @@ -30,6 +49,7 @@ class Booking /** * Create a new helper instance. * + * @param Webkul\BookingProduct\Repositories\BookingProductRepository $bookingProductRepository * @param Webkul\BookingProduct\Repositories\BookingProductDefaultSlotRepository $bookingProductDefaultSlotRepository * @param Webkul\BookingProduct\Repositories\BookingProductAppointmentSlotRepository $bookingProductAppointmentSlotRepository * @param Webkul\BookingProduct\Repositories\BookingProductEventSlotRepository $bookingProductEventSlotRepository @@ -38,6 +58,7 @@ class Booking * @return void */ public function __construct( + BookingProductRepository $bookingProductRepository, BookingProductDefaultSlotRepository $bookingProductDefaultSlotRepository, BookingProductAppointmentSlotRepository $bookingProductAppointmentSlotRepository, BookingProductEventSlotRepository $bookingProductEventSlotRepository, @@ -45,6 +66,8 @@ class Booking BookingProductTableSlotRepository $bookingProductTableSlotRepository ) { + $this->bookingProductRepository = $bookingProductRepository; + $this->typeRepositories['default'] = $bookingProductDefaultSlotRepository; $this->typeRepositories['appointment'] = $bookingProductAppointmentSlotRepository; @@ -56,6 +79,17 @@ class Booking $this->typeRepositories['table'] = $bookingProductTableSlotRepository; } + /** + * Returns the booking type hepler instance + * + * @param string $type + * @return array + */ + public function getTypeHepler($type) + { + return $this->typeHelpers[$type]; + } + /** * Returns the booking information * @@ -273,4 +307,101 @@ class Booking return $slots; } + + /** + * Returns additional cart item information + * + * @param array $data + * @return array + */ + public function getCartItemOptions($data) + { + $bookingProduct = $this->bookingProductRepository->findOneByField('product_id', $data['product_id']); + + if (! $bookingProduct) + return $data; + + switch ($bookingProduct->type) { + case 'rental': + $rentingType = $data['booking']['renting_type'] ?? $bookingProduct->rental_slot->renting_type; + + if ($rentingType == 'daily') { + $from = Carbon::createFromTimeString($data['booking']['date_from'] . " 00:00:01")->format('d F, Y'); + + $to = Carbon::createFromTimeString($data['booking']['date_to'] . " 23:59:59")->format('d F, Y'); + } else { + $from = Carbon::createFromTimestamp($data['booking']['slot']['from'])->format('d F, Y h:i A'); + + $to = Carbon::createFromTimestamp($data['booking']['slot']['to'])->format('d F, Y h:i A'); + } + + $data['attributes'] = [ + [ + 'attribute_name' => 'Rent Type', + 'option_id' => 0, + 'option_label' => trans('bookingproduct::app.shop.cart.' . $rentingType), + ], [ + 'attribute_name' => 'Rent From', + 'option_id' => 0, + 'option_label' => $from, + ], [ + 'attribute_name' => 'Rent Till', + 'option_id' => 0, + 'option_label' => $to, + ]]; + + break; + + default: + $timestamps = explode('-', $data['booking']['slot']); + + $data['attributes'] = [ + [ + 'attribute_name' => 'Booking From', + 'option_id' => 0, + 'option_label' => Carbon::createFromTimestamp($timestamps[0])->format('d F, Y h:i A'), + ], [ + 'attribute_name' => 'Booking Till', + 'option_id' => 0, + 'option_label' => Carbon::createFromTimestamp($timestamps[1])->format('d F, Y h:i A'), + ]]; + + break; + } + + return $data; + } + + /** + * Add booking additional prices to cart item + * + * @param array $products + * @return array + */ + public function addAdditionalPrices($products) + { + return $products; + } + + /** + * Validate cart item product price + * + * @param CartItem $item + * @return float + */ + public function validateCartItem($item) + { + $price = $item->product->getTypeInstance()->getFinalPrice(); + + if ($price == $item->base_price) + return; + + $item->base_price = $price; + $item->price = core()->convertPrice($price); + + $item->base_total = $price * $item->quantity; + $item->total = core()->convertPrice($price * $item->quantity); + + $item->save(); + } } \ No newline at end of file diff --git a/packages/Webkul/BookingProduct/src/Helpers/RentalSlot.php b/packages/Webkul/BookingProduct/src/Helpers/RentalSlot.php index 2e04cfb90..4744b49c7 100644 --- a/packages/Webkul/BookingProduct/src/Helpers/RentalSlot.php +++ b/packages/Webkul/BookingProduct/src/Helpers/RentalSlot.php @@ -91,4 +91,74 @@ class RentalSlot extends Booking return $slots; } + + /** + * Add booking additional prices to cart item + * + * @param array $products + * @return array + */ + public function addAdditionalPrices($products) + { + $bookingProduct = $this->bookingProductRepository->findOneByField('product_id', $products[0]['product_id']); + + $rentingType = $products[0]['additional']['booking']['renting_type'] ?? $bookingProduct->rental_slot->renting_type; + + if ($rentingType == 'daily') { + $from = Carbon::createFromTimeString($products[0]['additional']['booking']['date_from'] . " 00:00:00"); + $to = Carbon::createFromTimeString($products[0]['additional']['booking']['date_to'] . " 24:00:00"); + + $price = $bookingProduct->rental_slot->daily_price * $to->diffInDays($from); + } else { + $from = Carbon::createFromTimestamp($products[0]['additional']['booking']['slot']['from']); + $to = Carbon::createFromTimestamp($products[0]['additional']['booking']['slot']['to']); + + $price = $bookingProduct->rental_slot->hourly_price * $to->diffInHours($from); + } + + $products[0]['price'] += core()->convertPrice($price); + $products[0]['base_price'] += $price; + $products[0]['total'] += (core()->convertPrice($price) * $products[0]['quantity']); + $products[0]['base_total'] += ($price * $products[0]['quantity']); + + return $products; + } + + /** + * Validate cart item product price + * + * @param CartItem $item + * @return float + */ + public function validateCartItem($item) + { + $price = $item->product->getTypeInstance()->getFinalPrice(); + + $bookingProduct = $this->bookingProductRepository->findOneByField('product_id', $item->product_id); + + $rentingType = $item->additional['booking']['renting_type'] ?? $bookingProduct->rental_slot->renting_type; + + if ($rentingType == 'daily') { + $from = Carbon::createFromTimeString($item->additional['booking']['date_from'] . " 00:00:00"); + $to = Carbon::createFromTimeString($item->additional['booking']['date_to'] . " 24:00:00"); + + $price += $item->product->getTypeInstance()->getFinalPrice() + $bookingProduct->rental_slot->daily_price * $to->diffInDays($from); + } else { + $from = Carbon::createFromTimestamp($item->additional['booking']['slot']['from']); + $to = Carbon::createFromTimestamp($item->additional['booking']['slot']['to']); + + $price += $bookingProduct->rental_slot->hourly_price * $to->diffInHours($from); + } + + if ($price == $item->base_price) + return; + + $item->base_price = $price; + $item->price = core()->convertPrice($price); + + $item->base_total = $price * $item->quantity; + $item->total = core()->convertPrice($price * $item->quantity); + + $item->save(); + } } \ No newline at end of file diff --git a/packages/Webkul/BookingProduct/src/Resources/lang/en/app.php b/packages/Webkul/BookingProduct/src/Resources/lang/en/app.php index 1d6d63b7d..e417b35b4 100644 --- a/packages/Webkul/BookingProduct/src/Resources/lang/en/app.php +++ b/packages/Webkul/BookingProduct/src/Resources/lang/en/app.php @@ -104,6 +104,12 @@ return [ 'base-price' => 'Base Price', 'total-price' => 'Total Price', 'base-price-info' => '(This will be apply to each type of ticket for each quantity)' + ], + + 'cart' => [ + 'renting_type' => 'Rent Type', + 'daily' => 'Daily', + 'hourly' => 'Hourly', ] ] ]; \ No newline at end of file diff --git a/packages/Webkul/BookingProduct/src/Type/Booking.php b/packages/Webkul/BookingProduct/src/Type/Booking.php index 1140c5a4a..17de2eecb 100644 --- a/packages/Webkul/BookingProduct/src/Type/Booking.php +++ b/packages/Webkul/BookingProduct/src/Type/Booking.php @@ -8,6 +8,7 @@ use Webkul\Product\Repositories\ProductAttributeValueRepository; use Webkul\Product\Repositories\ProductInventoryRepository; use Webkul\Product\Repositories\ProductImageRepository; use Webkul\Product\Helpers\ProductImage; +use Webkul\BookingProduct\Repositories\BookingProductRepository; use Webkul\BookingProduct\Helpers\Booking as BookingHelper; use Webkul\Product\Type\AbstractType; @@ -19,6 +20,13 @@ use Webkul\Product\Type\AbstractType; */ class Booking extends AbstractType { + /** + * BookingProductRepository instance + * + * @var BookingProductRepository + */ + protected $bookingProductRepository; + /** * Booking helper instance * @@ -58,8 +66,9 @@ class Booking extends AbstractType * @param Webkul\Product\Repositories\ProductAttributeValueRepository $attributeValueRepository * @param Webkul\Product\Repositories\ProductInventoryRepository $productInventoryRepository * @param Webkul\Product\Repositories\ProductImageRepository $productImageRepository - * @param Webkul\BookingProduct\Helpers\BookingHelper $bookingHelper * @param Webkul\Product\Helpers\ProductImage $productImageHelper + * @param Webkul\BookingProduct\Repositories\BookingProductRepository $bookingProductRepository + * @param Webkul\BookingProduct\Helpers\BookingHelper $bookingHelper * @return void */ public function __construct( @@ -68,8 +77,9 @@ class Booking extends AbstractType ProductAttributeValueRepository $attributeValueRepository, ProductInventoryRepository $productInventoryRepository, ProductImageRepository $productImageRepository, - BookingHelper $bookingHelper, - ProductImage $productImageHelper + ProductImage $productImageHelper, + BookingProductRepository $bookingProductRepository, + BookingHelper $bookingHelper ) { parent::__construct( @@ -81,6 +91,8 @@ class Booking extends AbstractType $productImageHelper ); + $this->bookingProductRepository = $bookingProductRepository; + $this->bookingHelper = $bookingHelper; } @@ -121,14 +133,22 @@ class Booking extends AbstractType /** * Add product. Returns error message if can't prepare product. * - * @param array $data + * @param array $data * @return array */ public function prepareForCart($data) { - $products = parent::prepareForCart($data); + if (! isset($data['booking']) || ! count($data['booking'])) + return trans('shop::app.checkout.cart.integrity.missing_options'); - dd($products); + $products = parent::prepareForCart($data); + + $bookingProduct = $this->bookingProductRepository->findOneByField('product_id', $data['product_id']); + + if ($bookingProduct) + $products = app($this->bookingHelper->getTypeHepler($bookingProduct->type))->addAdditionalPrices($products); + + return $products; } /** @@ -153,17 +173,22 @@ class Booking extends AbstractType */ public function getAdditionalOptions($data) { - $data['attributes'] = [ - [ - 'attribute_name' => 'From', - 'option_id' => 0, - 'option_label' => "", - ], [ - 'attribute_name' => 'To', - 'option_id' => 0, - 'option_label' => "", - ]]; + return $this->bookingHelper->getCartItemOptions($data); + } - return $data; + /** + * Validate cart item product price + * + * @param CartItem $item + * @return float + */ + public function validateCartItem($item) + { + $bookingProduct = $this->bookingProductRepository->findOneByField('product_id', $item->product_id); + + if (! $bookingProduct) + return; + + app($this->bookingHelper->getTypeHepler($bookingProduct->type))->validateCartItem($item); } } \ No newline at end of file diff --git a/packages/Webkul/Product/src/Type/Bundle.php b/packages/Webkul/Product/src/Type/Bundle.php index ea8d8ffad..bc4e7bc87 100644 --- a/packages/Webkul/Product/src/Type/Bundle.php +++ b/packages/Webkul/Product/src/Type/Bundle.php @@ -405,7 +405,6 @@ class Bundle extends AbstractType */ public function prepareForCart($data) { - if (isset($data['bundle_options'])) $data['bundle_options'] = array_filter($this->validateBundleOptionForCart($data['bundle_options']));