Improved booking product package code style

This commit is contained in:
Jitendra Singh 2020-02-20 13:43:06 +05:30
parent 96cba32d8b
commit 521fc4b6b6
8 changed files with 76 additions and 53 deletions

View File

@ -2,9 +2,9 @@
return [
'booking' => [
'key' => 'booking',
'name' => 'Booking',
'key' => 'booking',
'name' => 'Booking',
'class' => 'Webkul\BookingProduct\Type\Booking',
'sort' => 7
'sort' => 7
]
];

View File

@ -34,11 +34,11 @@ class Booking
* @return array
*/
protected $typeHelpers = [
'default' => DefaultSlot::class,
'default' => DefaultSlot::class,
'appointment' => AppointmentSlot::class,
'event' => EventSlot::class,
'rental' => RentalSlot::class,
'table' => TableSlot::class,
'event' => EventSlot::class,
'rental' => RentalSlot::class,
'table' => TableSlot::class,
];
/**
@ -107,11 +107,12 @@ class Booking
foreach ($this->daysOfWeek as $index => $isOpen) {
$slots = [];
if ($isOpen)
if ($isOpen) {
$slots = $bookingProductSlot->same_slot_all_days ? $bookingProductSlot->slots : ($bookingProductSlot->slots[$index] ?? []);
}
$slotsByDays[] = [
'name' => trans($this->daysOfWeek[$index]),
'name' => trans($this->daysOfWeek[$index]),
'slots' => isset($availabileDays[$index]) ? $this->conver24To12Hours($slots) : []
];
}
@ -159,8 +160,9 @@ class Booking
*/
public function getAvailableWeekDays($bookingProductSlot)
{
if ($bookingProductSlot->available_every_week)
if ($bookingProductSlot->available_every_week) {
return $this->daysOfWeek;
}
$days = [];
@ -174,8 +176,9 @@ class Booking
$date = clone $currentTime;
$date->addDays($i);
if ($date >= $availableFrom && $date <= $availableTo)
if ($date >= $availableFrom && $date <= $availableTo) {
$days[$i] = $date->format('l');
}
}
return $this->sortDaysOfWeek($days);
@ -212,8 +215,9 @@ class Booking
*/
public function conver24To12Hours($slots)
{
if (! $slots)
if (! $slots) {
return [];
}
foreach ($slots as $index => $slot) {
$slots[$index]['from'] = Carbon::createFromTimeString($slot['from'])->format("h:i a");
@ -234,8 +238,9 @@ class Booking
{
$bookingProductSlot = $this->typeRepositories[$bookingProduct->type]->findOneByField('booking_product_id', $bookingProduct->id);
if (! is_array($bookingProductSlot->slots) || ! count($bookingProductSlot->slots))
if (! is_array($bookingProductSlot->slots) || ! count($bookingProductSlot->slots)) {
return [];
}
$requestedDate = Carbon::createFromTimeString($date . " 00:00:00");
@ -293,10 +298,10 @@ class Booking
if ($qty && $currentTime <= $from) {
$slots[] = [
'from' => $from->format('h:i A'),
'to' => $to->format('h:i A'),
'from' => $from->format('h:i A'),
'to' => $to->format('h:i A'),
'timestamp' => $from->getTimestamp() . '-' . $to->getTimestamp(),
'qty' => $qty,
'qty' => $qty,
];
}
} else {
@ -318,8 +323,9 @@ class Booking
{
$bookingProduct = $this->bookingProductRepository->findOneByField('product_id', $data['product_id']);
if (! $bookingProduct)
if (! $bookingProduct) {
return $data;
}
switch ($bookingProduct->type) {
case 'rental':
@ -338,16 +344,16 @@ class Booking
$data['attributes'] = [
[
'attribute_name' => 'Rent Type',
'option_id' => 0,
'option_label' => trans('bookingproduct::app.shop.cart.' . $rentingType),
'option_id' => 0,
'option_label' => trans('bookingproduct::app.shop.cart.' . $rentingType)
], [
'attribute_name' => 'Rent From',
'option_id' => 0,
'option_label' => $from,
'option_id' => 0,
'option_label' => $from
], [
'attribute_name' => 'Rent Till',
'option_id' => 0,
'option_label' => $to,
'option_id' => 0,
'option_label' => $to
]];
break;
@ -358,12 +364,12 @@ class Booking
$data['attributes'] = [
[
'attribute_name' => 'Booking From',
'option_id' => 0,
'option_label' => Carbon::createFromTimestamp($timestamps[0])->format('d F, Y h:i A'),
'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'),
'option_id' => 0,
'option_label' => Carbon::createFromTimestamp($timestamps[1])->format('d F, Y h:i A'),
]];
break;
@ -393,8 +399,9 @@ class Booking
{
$price = $item->product->getTypeInstance()->getFinalPrice();
if ($price == $item->base_price)
if ($price == $item->base_price) {
return;
}
$item->base_price = $price;
$item->price = core()->convertPrice($price);

View File

@ -28,8 +28,9 @@ class DefaultSlot extends Booking
{
$bookingProductSlot = $this->typeRepositories[$bookingProduct->type]->findOneByField('booking_product_id', $bookingProduct->id);
if (! is_array($bookingProductSlot->slots) || ! count($bookingProductSlot->slots))
if (! is_array($bookingProductSlot->slots) || ! count($bookingProductSlot->slots)) {
return [];
}
$requestedDate = Carbon::createFromTimeString($date . " 00:00:00");
@ -41,8 +42,9 @@ class DefaultSlot extends Booking
if ($requestedDate < $currentTime
|| $requestedDate < $availableFrom
|| $requestedDate > $availableTo)
|| $requestedDate > $availableTo) {
return [];
}
$slots = [];
@ -63,15 +65,16 @@ class DefaultSlot extends Booking
$slots = [];
foreach ($bookingProductSlot->slots as $timeDuration) {
if ($requestedDate->dayOfWeek != $timeDuration['from_day'])
if ($requestedDate->dayOfWeek != $timeDuration['from_day']) {
continue;
}
$startDate = clone $requestedDate->modify('this ' . $this->daysOfWeek[$timeDuration['from_day']]);
$endDate = clone $requestedDate->modify('this ' . $this->daysOfWeek[$timeDuration['to_day']]);
$slots[] = [
'from' => $startDate->format('d F, Y h:i A'),
'to' => $endDate->format('d F, Y h:i A'),
'from' => $startDate->format('d F, Y h:i A'),
'to' => $endDate->format('d F, Y h:i A'),
'timestamp' => $startDate->getTimestamp() . '-' . $endDate->getTimestamp(),
];
}
@ -96,8 +99,9 @@ class DefaultSlot extends Booking
$timeDuration = $bookingProductSlot->slots[$requestedDate->format('w')] ?? [];
if (! count($timeDuration) || ! $timeDuration['status'])
if (! count($timeDuration) || ! $timeDuration['status']) {
return [];
}
$slots = [];
@ -138,10 +142,10 @@ class DefaultSlot extends Booking
if ($qty && $currentTime <= $from) {
$slots[] = [
'from' => $from->format('h:i A'),
'to' => $to->format('h:i A'),
'from' => $from->format('h:i A'),
'to' => $to->format('h:i A'),
'timestamp' => $from->getTimestamp() . '-' . $to->getTimestamp(),
'qty' => $qty,
'qty' => $qty,
];
}
} else {

View File

@ -35,8 +35,9 @@ class EventSlot extends Booking
*/
public function getTickets($bookingProduct)
{
if (! $bookingProduct->event_slot)
if (! $bookingProduct->event_slot) {
return;
}
return $this->formatPrice($bookingProduct->event_slot->slots);
}

View File

@ -23,8 +23,9 @@ class RentalSlot extends Booking
{
$bookingProductSlot = $this->typeRepositories[$bookingProduct->type]->findOneByField('booking_product_id', $bookingProduct->id);
if (! is_array($bookingProductSlot->slots) || ! count($bookingProductSlot->slots))
if (! is_array($bookingProductSlot->slots) || ! count($bookingProductSlot->slots)) {
return [];
}
$requestedDate = Carbon::createFromTimeString($date . " 00:00:00");
@ -72,15 +73,16 @@ class RentalSlot extends Booking
$qty = isset($timeDuration['qty']) ? ( $timeDuration['qty'] - $orderedQty ) : 1;
if ($qty && $currentTime <= $from) {
if (! isset($slots[$index]))
if (! isset($slots[$index])) {
$slots[$index]['time'] = $startDayTime->format('h:i A') . ' - ' . $endDayTime->format('h:i A');
}
$slots[$index]['slots'][] = [
'from' => $from->format('h:i A'),
'to' => $to->format('h:i A'),
'from' => $from->format('h:i A'),
'to' => $to->format('h:i A'),
'from_timestamp' => $from->getTimestamp(),
'to_timestamp' => $to->getTimestamp(),
'qty' => $qty
'to_timestamp' => $to->getTimestamp(),
'qty' => $qty
];
}
} else {
@ -150,8 +152,9 @@ class RentalSlot extends Booking
$price += $bookingProduct->rental_slot->hourly_price * $to->diffInHours($from);
}
if ($price == $item->base_price)
if ($price == $item->base_price) {
return;
}
$item->base_price = $price;
$item->price = core()->convertPrice($price);

View File

@ -37,8 +37,9 @@ class Product
*/
public function afterProductUpdated($product)
{
if ($product->type != 'booking' || ! request('booking'))
if ($product->type != 'booking' || ! request('booking')) {
return;
}
$bookingProduct = $this->bookingProductRepository->findOneByField('product_id', $product->id);

View File

@ -84,16 +84,18 @@ class BookingProductRepository extends Repository
parent::update($data, $id, $attribute);
foreach ($this->typeRepositories as $type => $repository) {
if ($type == $data['type'])
if ($type == $data['type']) {
continue;
}
$repository->deleteWhere(['booking_product_id' => $id]);
}
$bookingProductTypeSlot = $this->typeRepositories[$data['type']]->findOneByField('booking_product_id', $id);
if (isset($data['slots']))
if (isset($data['slots'])) {
$data['slots'] = $this->formatSlots($data);
}
if (! $bookingProductTypeSlot) {
$this->typeRepositories[$data['type']]->create(array_merge($data, ['booking_product_id' => $id]));

View File

@ -93,8 +93,9 @@ class Booking extends Virtual
{
static $bookingProduct;
if ($bookingProduct)
if ($bookingProduct) {
return $bookingProduct;
}
return $bookingProduct = $this->bookingProductRepository->findOneByField('product_id', $productId);
}
@ -117,8 +118,9 @@ class Booking extends Virtual
{
$bookingProduct = $this->getBookingProduct($this->product->id);
if ($bookingProduct->type != 'default')
if ($bookingProduct->type != 'default') {
unset($this->additionalViews[0]);
}
return $this->additionalViews;
}
@ -131,8 +133,9 @@ class Booking extends Virtual
*/
public function prepareForCart($data)
{
if (! isset($data['booking']) || ! count($data['booking']))
if (! isset($data['booking']) || ! count($data['booking'])) {
return trans('shop::app.checkout.cart.integrity.missing_options');
}
$products = parent::prepareForCart($data);
@ -153,8 +156,9 @@ class Booking extends Virtual
*/
public function compareOptions($options1, $options2)
{
if ($this->product->id != $options2['product_id'])
if ($this->product->id != $options2['product_id']) {
return false;
}
return $options1['booking'] == $options2['booking'];
}
@ -180,8 +184,9 @@ class Booking extends Virtual
{
$bookingProduct = $this->getBookingProduct($item->product_id);
if (! $bookingProduct)
if (! $bookingProduct) {
return;
}
app($this->bookingHelper->getTypeHepler($bookingProduct->type))->validateCartItem($item);
}