Commit 5f7e23eb authored by nicola85's avatar nicola85

Allow unlimited events reference in the BEE booking

parent 51cadf68
......@@ -128,3 +128,21 @@ function bee_uninstall() {
bat_event_delete_event_type_schema('availability_daily');
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) {
}
/**
* Add "Availability" field to the node type.
*
* @param $field_name
* @param $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 $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 $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 $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 $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 $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() {
if (ProductVariationType::load('bee') === NULL) {
$variation_type = ProductVariationType::create([
......@@ -976,6 +991,8 @@ function bee_create_bee_product_variation_type() {
}
/**
* Create a "Product" type for each node type in BEE.
*
* @param $node_type
*/
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() {
$id = 'bee';
......@@ -1129,6 +1149,9 @@ function bee_create_bee_order_item_type() {
}
}
/**
* Create "Booking" type for BEE.
*/
function bee_create_bee_booking_type() {
$id = 'bee';
......@@ -1138,9 +1161,66 @@ function bee_create_bee_booking_type() {
'type' => $id,
]);
$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) {
$store_storage = \Drupal::entityTypeManager()->getStorage('commerce_store');
$default_store = $store_storage->loadDefault();
......
......@@ -85,33 +85,24 @@ function bee_webform_form_submit($form, FormStateInterface $form_state) {
if ($bee_settings['bookable_type'] == 'daily') {
$booked_state = bat_event_load_state_by_machine_name('bee_daily_booked');
$event_type = 'availability_daily';
}
else {
$booked_state = bat_event_load_state_by_machine_name('bee_hourly_booked');
$event_type = 'availability_hourly';
}
$start_date = new \DateTime($start_date);
$end_date = new \DateTime($end_date);
foreach ($node_available_units as $unit) {
if ($bee_settings['bookable_type'] == '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 {
$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());
}
$event = bat_event_create(['type' => $event_type]);
$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());
$event->set('field_event_webform_submission', $webform_submission->id());
$event->set('event_bat_unit_reference', $unit);
......@@ -382,8 +373,9 @@ function bee_webform_add_reservation_to_cart(Webform $webform, WebformSubmission
'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->set('booking_start_date', $start_date->format('Y-m-d\TH:i:s'));
$booking->set('booking_end_date', $end_date->format('Y-m-d\TH:i:s'));
$booking->set('booking_capacity', $data[$bee_element_id]['capacity']);
$booking->save();
$order_item->set('field_booking', $booking);
......
......@@ -41,33 +41,35 @@ class OrderEventSubscriber implements EventSubscriberInterface {
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());
$event_type = 'availability_daily';
}
else {
$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 = [
'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);
$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));
$event->save();
$events[] = $event;
}
$booking->set('booking_event_reference', $event->id());
$booking->set('booking_event_reference', $events);
$booking->save();
}
}
......
......@@ -183,8 +183,8 @@ class AddReservationForm extends FormBase {
'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->set('booking_start_date', $start_date->format('Y-m-d\TH:i:s'));
$booking->set('booking_end_date', $end_date->format('Y-m-d\TH:i:s'));
$booking->save();
$product = $node->get('field_product')->entity;
......
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