Unverified Commit 2e76c3ad authored by acrollet's avatar acrollet Committed by GitHub

Merge pull request #9 from Roomify/commerce_webform_order

Commerce webform order compatibility
parents 49d16108 5b357109
...@@ -128,3 +128,21 @@ function bee_uninstall() { ...@@ -128,3 +128,21 @@ function bee_uninstall() {
bat_event_delete_event_type_schema('availability_daily'); bat_event_delete_event_type_schema('availability_daily');
bat_event_delete_event_type_schema('availability_hourly'); bat_event_delete_event_type_schema('availability_hourly');
} }
/**
* Add booking "Capacity" field.
*/
function bee_update_8001() {
if (bat_booking_type_load('bee') !== NULL) {
bee_create_booking_capacity_field();
}
}
/**
* Set cardinality as unlimited for the "booking_event_reference" field.
*/
function bee_update_8002() {
if (bat_booking_type_load('bee') !== NULL) {
bee_set_booking_event_reference_field_cardinality();
}
}
...@@ -676,6 +676,8 @@ function bee_views_pre_render(ViewExecutable $view) { ...@@ -676,6 +676,8 @@ function bee_views_pre_render(ViewExecutable $view) {
} }
/** /**
* Add "Availability" field to the node type.
*
* @param $field_name * @param $field_name
* @param $node_type * @param $node_type
*/ */
...@@ -714,6 +716,8 @@ function bee_add_availability_field($field_name, $node_type) { ...@@ -714,6 +716,8 @@ function bee_add_availability_field($field_name, $node_type) {
} }
/** /**
* Add "Open Hours" field to the node type.
*
* @param $field_name * @param $field_name
* @param $node_type * @param $node_type
*/ */
...@@ -775,6 +779,8 @@ function bee_add_open_hours_field($field_name, $node_type) { ...@@ -775,6 +779,8 @@ function bee_add_open_hours_field($field_name, $node_type) {
} }
/** /**
* Add "Set Open Hours" field to the node type.
*
* @param $field_name * @param $field_name
* @param $node_type * @param $node_type
*/ */
...@@ -818,6 +824,8 @@ function bee_add_use_open_hours_field($field_name, $node_type) { ...@@ -818,6 +824,8 @@ function bee_add_use_open_hours_field($field_name, $node_type) {
} }
/** /**
* Add "Product" field to the node type.
*
* @param $field_name * @param $field_name
* @param $node_type * @param $node_type
*/ */
...@@ -860,6 +868,8 @@ function bee_add_product_reference_field($field_name, $node_type) { ...@@ -860,6 +868,8 @@ function bee_add_product_reference_field($field_name, $node_type) {
} }
/** /**
* Add "Price" field to the node type.
*
* @param $field_name * @param $field_name
* @param $node_type * @param $node_type
*/ */
...@@ -907,6 +917,8 @@ function bee_add_price_field($field_name, $node_type) { ...@@ -907,6 +917,8 @@ function bee_add_price_field($field_name, $node_type) {
} }
/** /**
* Add "Price frequency" field to the node type.
*
* @param $field_name * @param $field_name
* @param $node_type * @param $node_type
*/ */
...@@ -951,6 +963,9 @@ function bee_add_price_frequency_field($field_name, $node_type) { ...@@ -951,6 +963,9 @@ function bee_add_price_frequency_field($field_name, $node_type) {
} }
} }
/**
* Create "Product variation" type for BEE.
*/
function bee_create_bee_product_variation_type() { function bee_create_bee_product_variation_type() {
if (ProductVariationType::load('bee') === NULL) { if (ProductVariationType::load('bee') === NULL) {
$variation_type = ProductVariationType::create([ $variation_type = ProductVariationType::create([
...@@ -976,6 +991,8 @@ function bee_create_bee_product_variation_type() { ...@@ -976,6 +991,8 @@ function bee_create_bee_product_variation_type() {
} }
/** /**
* Create a "Product" type for each node type in BEE.
*
* @param $node_type * @param $node_type
*/ */
function bee_create_node_product_type($node_type) { function bee_create_node_product_type($node_type) {
...@@ -993,6 +1010,9 @@ function bee_create_node_product_type($node_type) { ...@@ -993,6 +1010,9 @@ function bee_create_node_product_type($node_type) {
} }
} }
/**
* Create "Order item" type for BEE.
*/
function bee_create_bee_order_item_type() { function bee_create_bee_order_item_type() {
$id = 'bee'; $id = 'bee';
...@@ -1129,6 +1149,9 @@ function bee_create_bee_order_item_type() { ...@@ -1129,6 +1149,9 @@ function bee_create_bee_order_item_type() {
} }
} }
/**
* Create "Booking" type for BEE.
*/
function bee_create_bee_booking_type() { function bee_create_bee_booking_type() {
$id = 'bee'; $id = 'bee';
...@@ -1138,9 +1161,66 @@ function bee_create_bee_booking_type() { ...@@ -1138,9 +1161,66 @@ function bee_create_bee_booking_type() {
'type' => $id, 'type' => $id,
]); ]);
$booking_type->save(); $booking_type->save();
bee_set_booking_event_reference_field_cardinality();
bee_create_booking_capacity_field();
}
}
/**
* Set cardinality as unlimited for the "booking_event_reference" field.
*/
function bee_set_booking_event_reference_field_cardinality() {
$field_storage = FieldStorageConfig::loadByName('bat_booking', 'booking_event_reference');
$field_storage->setCardinality(-1);
$field_storage->save();
}
/**
* Add booking "Capacity" field.
*/
function bee_create_booking_capacity_field() {
$field_name = 'booking_capacity';
$field_storage = FieldStorageConfig::loadByName('bat_booking', $field_name);
$field = FieldConfig::loadByName('bat_booking', 'bee', $field_name);
if (empty($field_storage)) {
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'bat_booking',
'type' => 'integer',
'cardinality' => 1,
'locked' => 1,
'settings' => [
'unsigned' => true,
'size' => 'normal',
],
]);
$field_storage->save();
}
if (empty($field)) {
$field = FieldConfig::create([
'field_storage' => $field_storage,
'entity_type' => 'bat_booking',
'label' => 'Capacity',
'bundle' => 'bee',
'required' => FALSE,
'settings' => [
'min' => null,
'max' => null,
'prefix' => '',
'suffix' => '',
],
]);
$field->save();
} }
} }
/**
* Create a Product instance for this node.
*/
function bee_create_node_product($node) { function bee_create_node_product($node) {
$store_storage = \Drupal::entityTypeManager()->getStorage('commerce_store'); $store_storage = \Drupal::entityTypeManager()->getStorage('commerce_store');
$default_store = $store_storage->loadDefault(); $default_store = $store_storage->loadDefault();
......
This diff is collapsed.
...@@ -27,6 +27,7 @@ class WebformBeeReservationForm extends WebformCompositeBase { ...@@ -27,6 +27,7 @@ class WebformBeeReservationForm extends WebformCompositeBase {
public function getDefaultProperties() { public function getDefaultProperties() {
return [ return [
'content_types' => [], 'content_types' => [],
'only_check_availability' => FALSE,
] + parent::getDefaultProperties(); ] + parent::getDefaultProperties();
} }
...@@ -55,6 +56,11 @@ class WebformBeeReservationForm extends WebformCompositeBase { ...@@ -55,6 +56,11 @@ class WebformBeeReservationForm extends WebformCompositeBase {
'#element_validate' => [[get_class($this), 'validateContentTypes']], '#element_validate' => [[get_class($this), 'validateContentTypes']],
]; ];
$form['composite']['only_check_availability'] = [
'#type' => 'checkbox',
'#title' => $this->t('Only check availability'),
];
return $form; return $form;
} }
......
...@@ -41,33 +41,35 @@ class OrderEventSubscriber implements EventSubscriberInterface { ...@@ -41,33 +41,35 @@ class OrderEventSubscriber implements EventSubscriberInterface {
if ($bee_settings['bookable_type'] == 'daily') { if ($bee_settings['bookable_type'] == 'daily') {
$booked_state = bat_event_load_state_by_machine_name('bee_daily_booked'); $booked_state = bat_event_load_state_by_machine_name('bee_daily_booked');
$event_type = 'availability_daily';
$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 { else {
$booked_state = bat_event_load_state_by_machine_name('bee_hourly_booked'); $booked_state = bat_event_load_state_by_machine_name('bee_hourly_booked');
$event_type = 'availability_hourly';
}
$capacity = ($booking->get('booking_capacity')->value) ? ($booking->get('booking_capacity')->value) : 1;
$events = [];
$event = bat_event_create(['type' => 'availability_hourly']); for ($i = 0; $i < $capacity; $i++) {
$event = bat_event_create(['type' => $event_type]);
$event_dates = [ $event_dates = [
'value' => $start_date->format('Y-m-d\TH:i:00'), 'value' => $start_date->format('Y-m-d\TH:i:00'),
'end_value' => $end_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_dates', $event_dates);
$event->set('event_state_reference', $booked_state->id()); $event->set('event_state_reference', $booked_state->id());
}
$available_units = $this->getAvailableUnits($node, $start_date, $end_date); $available_units = $this->getAvailableUnits($node, $start_date, $end_date);
$event->set('event_bat_unit_reference', reset($available_units));
$event->save();
$event->set('event_bat_unit_reference', reset($available_units)); $events[] = $event;
$event->save(); }
$booking->set('booking_event_reference', $event->id()); $booking->set('booking_event_reference', $events);
$booking->save(); $booking->save();
} }
} }
......
...@@ -183,8 +183,8 @@ class AddReservationForm extends FormBase { ...@@ -183,8 +183,8 @@ class AddReservationForm extends FormBase {
'type' => 'bee', 'type' => 'bee',
'label' => $node->label(), 'label' => $node->label(),
]); ]);
$booking->set('booking_start_date', $start_date->format('Y-m-d H:i:s')); $booking->set('booking_start_date', $start_date->format('Y-m-d\TH:i:s'));
$booking->set('booking_end_date', $end_date->format('Y-m-d H:i:s')); $booking->set('booking_end_date', $end_date->format('Y-m-d\TH:i:s'));
$booking->save(); $booking->save();
$product = $node->get('field_product')->entity; $product = $node->get('field_product')->entity;
......
...@@ -41,44 +41,48 @@ class SalepriceResolver implements PriceResolverInterface { ...@@ -41,44 +41,48 @@ class SalepriceResolver implements PriceResolverInterface {
$bee_settings = \Drupal::config('node.type.' . $node->bundle())->get('bee'); $bee_settings = \Drupal::config('node.type.' . $node->bundle())->get('bee');
$booking = $order_item->get('field_booking')->entity; if ($booking = $order_item->get('field_booking')->entity) {
$start_date = new \DateTime($booking->get('booking_start_date')->value);
$start_date = new \DateTime($booking->get('booking_start_date')->value); $end_date = new \DateTime($booking->get('booking_end_date')->value);
$end_date = new \DateTime($booking->get('booking_end_date')->value); $capacity = 1;
if ($booking_capacity = $booking->get('booking_capacity')->value) {
$interval = $start_date->diff($end_date); $capacity = $booking_capacity;
}
$reservation_context = [ $interval = $start_date->diff($end_date);
'order_item' => $order_item,
'booking' => $booking,
'node' => $node,
];
$base_price = $node->get('field_price')->number; $reservation_context = [
$currency_code = $node->get('field_price')->currency_code; 'order_item' => $order_item,
'booking' => $booking,
'node' => $node,
];
if ($bee_settings['bookable_type'] == 'daily') { $base_price = $node->get('field_price')->number;
$days = $interval->days; $currency_code = $node->get('field_price')->currency_code;
$amount = number_format($base_price * $days, 2, '.', '');
}
else {
$field_price_frequency = $node->get('field_price_frequency')->value;
if ($field_price_frequency == 'hour') { if ($bee_settings['bookable_type'] == 'daily') {
$hours = ($interval->days * 24) + $interval->h; $days = $interval->days;
$amount = number_format($base_price * $hours, 2, '.', ''); $amount = number_format($base_price * $days, 2, '.', '');
} }
else { else {
$minutes = ($interval->days * 24 * 60) + ($interval->h * 60) + $interval->i; $field_price_frequency = $node->get('field_price_frequency')->value;
$amount = number_format($base_price * $minutes, 2, '.', '');
if ($field_price_frequency == 'hour') {
$hours = ($interval->days * 24) + $interval->h;
$amount = number_format($base_price * $hours * $capacity, 2, '.', '');
}
else {
$minutes = ($interval->days * 24 * 60) + ($interval->h * 60) + $interval->i;
$amount = number_format($base_price * $minutes * $capacity, 2, '.', '');
}
} }
}
$price = new Price($amount, $currency_code); $price = new Price($amount, $currency_code);
\Drupal::moduleHandler()->alter('bee_reservation_price', $price, $reservation_context); \Drupal::moduleHandler()->alter('bee_reservation_price', $price, $reservation_context);
return $price; return $price;
}
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment