diff --git a/bee.install b/bee.install index d60df84..3ff19da 100644 --- a/bee.install +++ b/bee.install @@ -97,6 +97,10 @@ function bee_uninstall() { Drupal::configFactory()->getEditable('field.field.node.' . $node_type->id() . '.field_open_hours')->delete(); Drupal::configFactory()->getEditable('field.field.node.' . $node_type->id() . '.field_use_open_hours')->delete(); + + Drupal::configFactory()->getEditable('field.field.node.' . $node_type->id() . '.field_product')->delete(); + Drupal::configFactory()->getEditable('field.field.node.' . $node_type->id() . '.field_price')->delete(); + Drupal::configFactory()->getEditable('field.field.node.' . $node_type->id() . '.field_price_frequency')->delete(); } Drupal::configFactory()->getEditable('field.storage.node.field_availability_daily')->delete(); @@ -105,6 +109,10 @@ function bee_uninstall() { Drupal::configFactory()->getEditable('field.storage.node.field_open_hours')->delete(); Drupal::configFactory()->getEditable('field.storage.node.field_use_open_hours')->delete(); + Drupal::configFactory()->getEditable('field.storage.node.field_product')->delete(); + Drupal::configFactory()->getEditable('field.storage.node.field_price')->delete(); + Drupal::configFactory()->getEditable('field.storage.node.field_price_frequency')->delete(); + $states = [ 'bee_daily_available', 'bee_daily_not_available', diff --git a/bee.module b/bee.module index b80e4e6..8d42238 100644 --- a/bee.module +++ b/bee.module @@ -3,9 +3,17 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\Display\EntityViewDisplayInterface; +use Drupal\Core\Entity\Entity\EntityViewDisplay; use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldStorageConfig; use Drupal\office_hours\OfficeHoursDateHelper; +use Drupal\commerce_product\Entity\ProductType; +use Drupal\commerce_product\Entity\ProductVariationType; +use Drupal\commerce_product\Entity\Product; +use Drupal\commerce_product\Entity\ProductVariation; +use Drupal\commerce_order\Entity\OrderItemType; +use Drupal\commerce_order\Entity\OrderItemInterface; +use Drupal\commerce_store\Entity\Store; /** * Implements hook_form_alter(). @@ -64,6 +72,28 @@ function bee_form_alter(&$form, FormStateInterface $form_state, $form_id) { ], ], ]; + $form['bee']['payment'] = [ + '#type' => 'checkbox', + '#title' => t('Enable payment for bookings'), + '#default_value' => isset($bee_settings['payment']) ? $bee_settings['payment'] : 0, + '#states' => [ + 'visible' => [ + ':input[name="bee[bookable]"]' => ['checked' => TRUE], + ], + ], + ]; + + if (!(\Drupal::moduleHandler()->moduleExists('commerce_checkout') && + \Drupal::moduleHandler()->moduleExists('commerce_payment'))) { + $form['bee']['payment']['#disabled'] = TRUE; + $form['bee']['payment']['#description'] = t('To enable payments for bookings, install and enable the Drupal Commerce checkout and payment modules and configure a Commerce Store.'); + } + elseif (\Drupal::moduleHandler()->moduleExists('commerce')) { + if (empty(Store::loadMultiple())) { + $form['bee']['payment']['#disabled'] = TRUE; + $form['bee']['payment']['#description'] = t('To enable payments for bookings, install and enable the Drupal Commerce checkout and payment modules and configure a Commerce Store.'); + } + } if (isset($bee_settings['type_id'])) { $form['bee']['type_id'] = [ @@ -126,6 +156,42 @@ function bee_form_alter(&$form, FormStateInterface $form_state, $form_id) { '#weight' => $form['field_open_hours']['#weight'] + 1, ]; + if ($bee_settings['payment']) { + $form['payments'] = [ + '#type' => 'details', + '#title' => t('Payments'), + '#open' => TRUE, + '#group' => 'bee', + '#weight' => $form['field_open_hours']['#weight'] + 1, + ]; + + if ($bee_settings['bookable_type'] == 'daily') { + $form['field_price']['#group'] = 'payments'; + $form['field_price']['widget'][0]['#title'] = t('Price per day'); + + $form['field_price_frequency']['#access'] = FALSE; + + $form['field_price']['#prefix'] = '
'; + $form['field_price']['#suffix'] = '
'; + } + else { + $form['field_price']['#group'] = 'payments'; + $form['field_price']['widget'][0]['#title'] = t('Price'); + + $form['field_price_frequency']['#group'] = 'payments'; + $form['field_price_frequency']['widget']['#default_value'] = 'hour'; + $form['field_price_frequency']['widget']['#title'] = t('per'); + unset($form['field_price_frequency']['widget']['#options']['_none']); + + $form['field_price']['#prefix'] = '
'; + $form['field_price_frequency']['#suffix'] = '
'; + } + } + else { + $form['field_price']['#access'] = FALSE; + $form['field_price_frequency']['#access'] = FALSE; + } + if ($bee_settings['bookable_type'] == 'hourly') { $form['field_use_open_hours']['#group'] = 'bee'; $form['field_use_open_hours']['#weight'] = $form['field_open_hours']['#weight'] - 1; @@ -153,6 +219,14 @@ function bee_form_alter(&$form, FormStateInterface $form_state, $form_id) { } } elseif (preg_match('/^node_.*_form$/', $form_id)) { + if (isset($form['field_price'])) { + $form['field_price']['#access'] = FALSE; + } + + if (isset($form['field_price_frequency'])) { + $form['field_price_frequency']['#access'] = FALSE; + } + if (isset($form['field_use_open_hours'])) { $form['field_use_open_hours']['#access'] = FALSE; } @@ -268,6 +342,35 @@ function bee_node_type_edit_form_submit(array &$form, FormStateInterface $form_s $bee_settings['type_id'] = $bat_type->id(); } + if ($bee_settings['payment']) { + bee_create_bee_product_variation_type(); + bee_create_bee_order_item_type(); + bee_create_bee_booking_type(); + + bee_create_node_product_type($node_type); + + bee_add_product_reference_field('field_product', $node_type); + bee_add_price_field('field_price', $node_type); + bee_add_price_frequency_field('field_price_frequency', $node_type); + + if (!$form['bee']['payment']['#default_value']) { + $node_storage = \Drupal::entityManager()->getStorage('node'); + + $query = \Drupal::entityQuery('node') + ->condition('type', $node_type->id()); + + $nids = $query->execute(); + foreach ($node_storage->loadMultiple($nids) as $node) { + if (!$node->get('field_product')->entity) { + $product = bee_create_node_product($node); + + $node->set('field_product', $product->id()); + $node->save(); + } + } + } + } + \Drupal::configFactory()->getEditable('node.type.' . $node_type->id())->set('bee', $bee_settings)->save(); } else { @@ -310,6 +413,12 @@ function bee_node_insert(EntityInterface $node) { $bee_settings = \Drupal::config('node.type.' . $node->bundle())->get('bee'); if (isset($bee_settings['bookable']) && $bee_settings['bookable']) { + if ($bee_settings['payment']) { + $product = bee_create_node_product($node); + + $node->set('field_product', $product->id()); + } + $bat_unit = bat_unit_create( [ 'name' => $node->label() . ' 1', @@ -335,6 +444,21 @@ function bee_node_delete(EntityInterface $node) { if ($bat_unit = $node->get('field_availability_' . $bee_settings['bookable_type'])->entity) { bat_unit_delete($bat_unit); } + + if ($product = $node->get('field_product')->entity) { + $product->delete(); + } + } +} + +/** + * Implements hook_ENTITY_TYPE_delete(). + */ +function bee_commerce_order_item_delete(OrderItemInterface $order_item) { + if ($order_item->bundle() == 'bee') { + if ($booking = $order_item->get('field_booking')->entity) { + $booking->delete(); + } } } @@ -662,3 +786,341 @@ function bee_add_use_open_hours_field($field_name, $node_type) { $form_display->save(); } } + +/** + * @param $field_name + * @param $node_type + */ +function bee_add_product_reference_field($field_name, $node_type) { + $field_storage = FieldStorageConfig::loadByName('node', $field_name); + $field = FieldConfig::loadByName('node', $node_type->id(), $field_name); + + if (empty($field_storage)) { + $field_storage = FieldStorageConfig::create([ + 'field_name' => $field_name, + 'entity_type' => 'node', + 'type' => 'entity_reference', + 'cardinality' => 1, + 'locked' => 1, + 'settings' => [ + 'target_type' => 'commerce_product', + ], + ]); + $field_storage->save(); + } + + if (empty($field)) { + $field = FieldConfig::create([ + 'field_storage' => $field_storage, + 'entity_type' => 'node', + 'label' => 'Product', + 'bundle' => $node_type->id(), + 'required' => FALSE, + 'settings' => [ + 'handler' => 'default:commerce_product', + 'handler_settings' => [ + 'target_bundles' => [ + 'bee_' . $node_type->id() => 'bee_' . $node_type->id(), + ], + ], + ], + ]); + $field->save(); + } +} + +/** + * @param $field_name + * @param $node_type + */ +function bee_add_price_field($field_name, $node_type) { + $field_storage = FieldStorageConfig::loadByName('node', $field_name); + $field = FieldConfig::loadByName('node', $node_type->id(), $field_name); + + if (empty($field_storage)) { + $field_storage = FieldStorageConfig::create([ + 'field_name' => $field_name, + 'entity_type' => 'node', + 'type' => 'commerce_price', + 'cardinality' => 1, + 'locked' => 1, + 'settings' => [], + ]); + $field_storage->save(); + } + + if (empty($field)) { + $field = FieldConfig::create([ + 'field_storage' => $field_storage, + 'entity_type' => 'node', + 'label' => 'Price', + 'bundle' => $node_type->id(), + 'required' => FALSE, + 'settings' => [], + ]); + $field->save(); + + $form_display = \Drupal::entityTypeManager()->getStorage('entity_form_display')->load('node.' . $node_type->id() . '.default'); + $form_display->setComponent($field_name, [ + 'type' => 'commerce_price_default', + 'weight' => 3, + ]); + $form_display->save(); + } +} + +/** + * @param $field_name + * @param $node_type + */ +function bee_add_price_frequency_field($field_name, $node_type) { + $field_storage = FieldStorageConfig::loadByName('node', $field_name); + $field = FieldConfig::loadByName('node', $node_type->id(), $field_name); + + if (empty($field_storage)) { + $field_storage = FieldStorageConfig::create([ + 'field_name' => $field_name, + 'entity_type' => 'node', + 'type' => 'list_string', + 'cardinality' => 1, + 'locked' => 1, + 'settings' => [ + 'allowed_values' => [ + 'hour' => 'Hour', + 'minute' => 'Minute', + ], + ], + ]); + $field_storage->save(); + } + + if (empty($field)) { + $field = FieldConfig::create([ + 'field_storage' => $field_storage, + 'entity_type' => 'node', + 'label' => 'Price frequency', + 'bundle' => $node_type->id(), + 'required' => FALSE, + 'settings' => [], + ]); + $field->save(); + + $form_display = \Drupal::entityTypeManager()->getStorage('entity_form_display')->load('node.' . $node_type->id() . '.default'); + $form_display->setComponent($field_name, [ + 'type' => 'options_select', + 'weight' => 4, + ]); + $form_display->save(); + } +} + +function bee_create_bee_product_variation_type() { + if (ProductVariationType::load('bee') === NULL) { + $variation_type = ProductVariationType::create([ + 'id' => 'bee', + 'label' => 'BEE', + 'orderItemType' => 'default', + 'generateTitle' => TRUE, + ]); + $variation_type->save(); + + $view_display = \Drupal::entityTypeManager()->getStorage('entity_view_display')->load('commerce_product_variation.bee.default'); + if (!$view_display) { + $view_display = EntityViewDisplay::create([ + 'targetEntityType' => 'commerce_product_variation', + 'bundle' => 'bee', + 'mode' => 'default', + 'status' => TRUE, + ]); + } + $view_display->setComponent('price', ['type' => 'commerce_price_calculated']); + $view_display->save(); + } +} + +/** + * @param $node_type + */ +function bee_create_node_product_type($node_type) { + $id = 'bee_' . $node_type->id(); + + if (ProductType::load($id) === NULL) { + $product_type = ProductType::create([ + 'id' => $id, + 'label' => $node_type->label(), + 'variationType' => 'bee', + ]); + $product_type->save(); + commerce_product_add_stores_field($product_type); + commerce_product_add_variations_field($product_type); + } +} + +function bee_create_bee_order_item_type() { + $id = 'bee'; + + if (OrderItemType::load($id) === NULL) { + OrderItemType::create([ + 'id' => $id, + 'label' => 'BEE', + 'purchasableEntityType' => 'commerce_product_variation', + 'orderType' => 'default', + ])->save(); + + $view_display = \Drupal::entityTypeManager()->getStorage('entity_view_display')->load('commerce_order_item.bee.default'); + if (!$view_display) { + $view_display = EntityViewDisplay::create([ + 'targetEntityType' => 'commerce_order_item', + 'bundle' => 'bee', + 'mode' => 'default', + 'status' => TRUE, + ]); + $view_display->setComponent('total_price', [ + 'type' => 'commerce_price_default', + 'region' => 'content', + 'label' => 'above', + 'settings' => [ + 'strip_trailing_zeroes' => FALSE, + 'display_currency_code' => FALSE, + ], + ]); + $view_display->setComponent('purchased_entity', [ + 'type' => 'entity_reference_entity_view', + 'region' => 'content', + 'label' => 'above', + 'settings' => [ + 'view_mode' => 'default', + 'link' => FALSE, + ], + ]); + $view_display->setComponent('quantity', [ + 'type' => 'number_decimal', + 'region' => 'content', + 'label' => 'above', + 'settings' => [ + 'thousand_separator' => '', + 'decimal_separator' => '.', + 'scale' => 2, + 'prefix_suffix' => TRUE, + ], + ]); + $view_display->setComponent('unit_price', [ + 'type' => 'commerce_price_default', + 'region' => 'content', + 'label' => 'above', + 'settings' => [ + 'strip_trailing_zeroes' => FALSE, + 'display_currency_code' => FALSE, + ], + ]); + $view_display->save(); + } + } + + $field_name = 'field_booking'; + + $field_storage = FieldStorageConfig::loadByName('commerce_order_item', $field_name); + $field = FieldConfig::loadByName('commerce_order_item', 'bee', $field_name); + + if (empty($field_storage)) { + $field_storage = FieldStorageConfig::create([ + 'field_name' => $field_name, + 'entity_type' => 'commerce_order_item', + 'type' => 'entity_reference', + 'cardinality' => 1, + 'locked' => 1, + 'settings' => [ + 'target_type' => 'bat_booking', + ], + ]); + $field_storage->save(); + } + + if (empty($field)) { + $field = FieldConfig::create([ + 'field_storage' => $field_storage, + 'entity_type' => 'commerce_order_item', + 'label' => 'Booking', + 'bundle' => 'bee', + 'required' => FALSE, + 'settings' => [ + 'handler' => 'default:bat_booking', + 'handler_settings' => [ + 'target_bundles' => [ + 'bee' => 'bee', + ], + ], + ], + ]); + $field->save(); + } + + $field_name = 'field_node'; + + $field_storage = FieldStorageConfig::loadByName('commerce_order_item', $field_name); + $field = FieldConfig::loadByName('commerce_order_item', 'bee', $field_name); + + if (empty($field_storage)) { + $field_storage = FieldStorageConfig::create([ + 'field_name' => $field_name, + 'entity_type' => 'commerce_order_item', + 'type' => 'entity_reference', + 'cardinality' => 1, + 'locked' => 1, + 'settings' => [ + 'target_type' => 'node', + ], + ]); + $field_storage->save(); + } + + if (empty($field)) { + $field = FieldConfig::create([ + 'field_storage' => $field_storage, + 'entity_type' => 'commerce_order_item', + 'label' => 'Node', + 'bundle' => 'bee', + 'required' => FALSE, + 'settings' => [ + 'handler' => 'default:node', + 'handler_settings' => [ + 'target_bundles' => [], + ], + ], + ]); + $field->save(); + } +} + +function bee_create_bee_booking_type() { + $id = 'bee'; + + if (bat_booking_type_load($id) === NULL) { + $booking_type = bat_booking_type_create([ + 'name' => 'BEE', + 'type' => $id, + ]); + $booking_type->save(); + } +} + +function bee_create_node_product($node) { + $variation = ProductVariation::create([ + 'type' => 'bee', + 'sku' => 'bee_' . $node->bundle() . '_' . $node->id(), + 'price' => [ + 'number' => 0, + 'currency_code' => 'USD', + ], + ]); + $variation->save(); + $product = Product::create([ + 'type' => 'bee_' . $node->bundle(), + 'title' => $node->label(), + 'variations' => [$variation], + 'stores' => Store::loadMultiple(), + ]); + $product->save(); + + return $product; +} diff --git a/bee.services.yml b/bee.services.yml index 4a1a43b..c6ad313 100644 --- a/bee.services.yml +++ b/bee.services.yml @@ -9,3 +9,12 @@ services: arguments: ['@entity.manager'] tags: - { name: access_check, applies_to: _bee_add_reservation_access } + bee.saleprice_resolver: + class: Drupal\bee\Resolvers\SalepriceResolver + arguments: ['@request_stack'] + tags: + - { name: commerce_price.price_resolver, priority: 600 } + bee.order_subscriber: + class: Drupal\bee\EventSubscriber\OrderEventSubscriber + tags: + - { name: event_subscriber } diff --git a/src/EventSubscriber/OrderEventSubscriber.php b/src/EventSubscriber/OrderEventSubscriber.php new file mode 100644 index 0000000..21281a2 --- /dev/null +++ b/src/EventSubscriber/OrderEventSubscriber.php @@ -0,0 +1,106 @@ + 'finalizeCart']; + return $events; + } + + /** + * Finalizes the cart when the order is placed. + * + * @param \Drupal\state_machine\Event\WorkflowTransitionEvent $event + * The workflow transition event. + */ + public function finalizeCart(WorkflowTransitionEvent $event) { + $order = $event->getEntity(); + + foreach ($order->getItems() as $item) { + if ($booking = $item->get('field_booking')->entity) { + $node = $item->get('field_node')->entity; + + $start_date = new \DateTime($booking->get('booking_start_date')->value); + $end_date = new \DateTime($booking->get('booking_end_date')->value); + + $bee_settings = \Drupal::config('node.type.' . $node->bundle())->get('bee'); + + if ($bee_settings['bookable_type'] == 'daily') { + $booked_state = bat_event_load_state_by_machine_name('bee_daily_booked'); + + $event = bat_event_create(['type' => 'availability_daily']); + $event_dates = [ + 'value' => $start_date->format('Y-m-d\TH:i:00'), + 'end_value' => $end_date->format('Y-m-d\TH:i:00'), + ]; + $event->set('event_dates', $event_dates); + $event->set('event_state_reference', $booked_state->id()); + } + else { + $booked_state = bat_event_load_state_by_machine_name('bee_hourly_booked'); + + $event = bat_event_create(['type' => 'availability_hourly']); + $event_dates = [ + 'value' => $start_date->format('Y-m-d\TH:i:00'), + 'end_value' => $end_date->format('Y-m-d\TH:i:00'), + ]; + $event->set('event_dates', $event_dates); + $event->set('event_state_reference', $booked_state->id()); + } + + $available_units = $this->getAvailableUnits($node, $start_date, $end_date); + + $event->set('event_bat_unit_reference', reset($available_units)); + $event->save(); + + $booking->set('booking_event_reference', $event->id()); + $booking->save(); + } + } + } + + /** + * Get available Units. + * + * @param $node + * @param $start_date + * @param $end_date + * + * return array + */ + protected function getAvailableUnits($node, $start_date, $end_date) { + $bee_settings = \Drupal::config('node.type.' . $node->bundle())->get('bee'); + + $units_ids = []; + foreach ($node->get('field_availability_' . $bee_settings['bookable_type']) as $unit) { + $units_ids[] = $unit->entity->id(); + } + + $temp_end_date = clone($end_date); + $temp_end_date->sub(new \DateInterval('PT1M')); + + if ($bee_settings['bookable_type'] == 'daily') { + $available_units_ids = bat_event_get_matching_units($start_date, $temp_end_date, ['bee_daily_available'], $bee_settings['type_id'], 'availability_daily'); + } + else { + $available_units_ids = bat_event_get_matching_units($start_date, $temp_end_date, ['bee_hourly_available'], $bee_settings['type_id'], 'availability_hourly'); + } + + return array_intersect($units_ids, $available_units_ids); + } + +} diff --git a/src/Form/AddReservationForm.php b/src/Form/AddReservationForm.php index 2ed91e3..d36da44 100644 --- a/src/Form/AddReservationForm.php +++ b/src/Form/AddReservationForm.php @@ -164,48 +164,91 @@ public function submitForm(array &$form, FormStateInterface $form_state) { $bee_settings = \Drupal::config('node.type.' . $node->bundle())->get('bee'); - $units_ids = []; - foreach ($node->get('field_availability_' . $bee_settings['bookable_type']) as $unit) { - $units_ids[] = $unit->entity->id(); - } - if ($bee_settings['bookable_type'] == 'daily') { $start_date = new \DateTime($start_date); $end_date = new \DateTime($end_date); - - $booked_state = bat_event_load_state_by_machine_name('bee_daily_booked'); - - $event = bat_event_create(['type' => 'availability_daily']); - $event_dates = [ - 'value' => $start_date->format('Y-m-d\TH:i:00'), - 'end_value' => $end_date->format('Y-m-d\TH:i:00'), - ]; - $event->set('event_dates', $event_dates); - $event->set('event_state_reference', $booked_state->id()); } else { $start_date = new \DateTime($start_date->format('Y-m-d H:i')); $end_date = new \DateTime($end_date->format('Y-m-d H:i')); + } + + if ($bee_settings['payment']) { + $booking = bat_booking_create([ + 'type' => 'bee', + 'label' => $node->label(), + ]); + $booking->set('booking_start_date', $start_date->format('Y-m-d H:i:s')); + $booking->set('booking_end_date', $end_date->format('Y-m-d H:i:s')); + $booking->save(); + + $product = $node->get('field_product')->entity; + + $stores = $product->getStores(); + $store = reset($stores); - $booked_state = bat_event_load_state_by_machine_name('bee_hourly_booked'); + $variations = $product->getVariations(); + $product_variation = reset($variations); - $event = bat_event_create(['type' => 'availability_hourly']); - $event_dates = [ - 'value' => $start_date->format('Y-m-d\TH:i:00'), - 'end_value' => $end_date->format('Y-m-d\TH:i:00'), - ]; - $event->set('event_dates', $event_dates); - $event->set('event_state_reference', $booked_state->id()); + $cart_manager = \Drupal::service('commerce_cart.cart_manager'); + $cart_provider = \Drupal::service('commerce_cart.cart_provider'); + + $cart = $cart_provider->getCart('default', $store); + if (!$cart) { + $cart = $cart_provider->createCart('default', $store); + } + else { + $cart_manager->emptyCart($cart); + } + + $order_item = \Drupal::entityManager()->getStorage('commerce_order_item')->create([ + 'title' => $node->label(), + 'type' => 'bee', + 'purchased_entity' => $product_variation->id(), + 'quantity' => 1, + 'unit_price' => $product_variation->getPrice(), + ]); + $order_item->set('field_booking', $booking); + $order_item->set('field_node', $node); + $order_item->save(); + + $cart_manager->addOrderItem($cart, $order_item); + + $form_state->setRedirect('commerce_checkout.form', ['commerce_order' => $cart->id()]); } + else { + if ($bee_settings['bookable_type'] == 'daily') { + $booked_state = bat_event_load_state_by_machine_name('bee_daily_booked'); + + $event = bat_event_create(['type' => 'availability_daily']); + $event_dates = [ + 'value' => $start_date->format('Y-m-d\TH:i:00'), + 'end_value' => $end_date->format('Y-m-d\TH:i:00'), + ]; + $event->set('event_dates', $event_dates); + $event->set('event_state_reference', $booked_state->id()); + } + else { + $booked_state = bat_event_load_state_by_machine_name('bee_hourly_booked'); + + $event = bat_event_create(['type' => 'availability_hourly']); + $event_dates = [ + 'value' => $start_date->format('Y-m-d\TH:i:00'), + 'end_value' => $end_date->format('Y-m-d\TH:i:00'), + ]; + $event->set('event_dates', $event_dates); + $event->set('event_state_reference', $booked_state->id()); + } - $available_units = $this->getAvailableUnits($values); + $available_units = $this->getAvailableUnits($values); - $event->set('event_bat_unit_reference', reset($available_units)); - $event->save(); + $event->set('event_bat_unit_reference', reset($available_units)); + $event->save(); - drupal_set_message(t('Reservation created!')); + drupal_set_message(t('Reservation created!')); - $form_state->setRedirect('entity.node.canonical', ['node' => $node->id()]); + $form_state->setRedirect('entity.node.canonical', ['node' => $node->id()]); + } } /** diff --git a/src/Resolvers/SalepriceResolver.php b/src/Resolvers/SalepriceResolver.php new file mode 100644 index 0000000..0a435e7 --- /dev/null +++ b/src/Resolvers/SalepriceResolver.php @@ -0,0 +1,88 @@ +bundle() != 'bee') { + return; + } + + $cart_provider = \Drupal::service('commerce_cart.cart_provider'); + + $stores = $entity->getProduct()->getStores(); + $store = reset($stores); + + if ($cart = $cart_provider->getCart('default', $store)) { + $order_items = $cart->getItems(); + foreach ($order_items as $order_item) { + if ($order_item->bundle() == 'bee') { + if ($order_item->getPurchasedEntityId() == $entity->id()) { + $query = \Drupal::entityQuery('node') + ->condition('field_product', $entity->getProductId()); + + $nids = $query->execute(); + $node = Node::load(reset($nids)); + + $bee_settings = \Drupal::config('node.type.' . $node->bundle())->get('bee'); + + $booking = $order_item->get('field_booking')->entity; + + $start_date = new \DateTime($booking->get('booking_start_date')->value); + $end_date = new \DateTime($booking->get('booking_end_date')->value); + + $interval = $start_date->diff($end_date); + + $reservation_context = [ + 'order_item' => $order_item, + 'booking' => $booking, + 'node' => $node, + ]; + + $base_price = $node->get('field_price')->number; + $currency_code = $node->get('field_price')->currency_code; + + if ($bee_settings['bookable_type'] == 'daily') { + $days = $interval->days; + $amount = number_format($base_price * $days, 2, '.', ''); + } + else { + $field_price_frequency = $node->get('field_price_frequency')->value; + + if ($field_price_frequency == 'hour') { + $hours = ($interval->days * 24) + $interval->h; + $amount = number_format($base_price * $hours, 2, '.', ''); + } + else { + $minutes = ($interval->days * 24 * 60) + ($interval->h * 60) + $interval->i; + $amount = number_format($base_price * $minutes, 2, '.', ''); + } + } + + $price = new Price($amount, $currency_code); + + \Drupal::moduleHandler()->alter('bee_reservation_price', $price, $reservation_context); + + return $price; + } + } + } + } + } + +}