Skip to content

Commit

Permalink
Make Daily Booking be a Pricing By option
Browse files Browse the repository at this point in the history
  • Loading branch information
LeticiaErrandonea committed Oct 18, 2024
1 parent 30e687b commit 66d912d
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 34 deletions.
22 changes: 15 additions & 7 deletions app/assets/javascripts/app/manage_instruments.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,26 @@ $(function() {


$(function() {
const dailyBookingField = document.querySelector(".js--daily-booking");
const pricingModeElement = document.querySelector(".js--pricing-mode");

if (!dailyBookingField) {
if (!pricingModeElement) {
return;
}

dailyBookingField.addEventListener("change", function() {
const dailyBooking = dailyBookingField.checked;
pricingModeElement.addEventListener("change", function () {
const dailyBookingRadioButton = document.querySelector(
"#instrument_pricing_mode_schedule_rule_daily_booking_only"
);

if (!dailyBookingRadioButton) {
return;
}

const pricingModeElement = document.querySelector(".js--pricing-mode");
const reserveIntervalElement = document.querySelector(".js--reserve-interval");
const dailyBooking = dailyBookingRadioButton.checked;

const reserveIntervalElement = document.querySelector(
".js--reserve-interval"
);
const minReserveMinsElement = document.querySelector(
".js--min-reserve-mins"
);
Expand All @@ -65,7 +74,6 @@ $(function() {
});

[
pricingModeElement,
reserveIntervalElement,
minReserveMinsElement,
maxReserveMinsElement,
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/products_common_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def default_permitted_params
:auto_cancel_mins, :lock_window, :cutoff_hours,
:problems_resolvable_by_user, :restrict_holiday_access, :billing_mode,
:pricing_mode, :cross_core_ordering_available,
:daily_booking, :min_reserve_days, :max_reserve_days
:min_reserve_days, :max_reserve_days
]
end

Expand Down
25 changes: 23 additions & 2 deletions app/models/instrument.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ class Instrument < Product
include EmailListAttribute

RESERVE_INTERVALS = [1, 5, 10, 15, 30, 60].freeze
PRICING_MODES = ["Schedule Rule", "Duration"].freeze
PRICING_MODES = ["Schedule Rule", "Duration"].tap do |pricing_modes|
if SettingsHelper.feature_on?(:show_daily_rate_option)
pricing_modes.insert(1, "Schedule Rule (Daily Booking only)")
end
end.freeze

with_options foreign_key: "product_id" do |instrument|
instrument.has_many :admin_reservations
Expand Down Expand Up @@ -36,14 +40,16 @@ class Instrument < Product
:maximum_reservation_is_multiple_of_interval,
:max_reservation_not_less_than_min

validates :pricing_mode, presence: true, inclusion: { in: PRICING_MODES }, if: -> { !daily_booking? }
validates :pricing_mode, presence: true, inclusion: { in: PRICING_MODES }

# Callbacks
# --------

# Triggered by Product
# after_create :create_default_price_group_products

before_create :clean_up_reservation_rules

# Scopes
# --------

Expand Down Expand Up @@ -103,6 +109,10 @@ def duration_pricing_mode?
pricing_mode == "Duration"
end

def daily_booking?
pricing_mode == "Schedule Rule (Daily Booking only)"
end

private

def minimum_reservation_is_multiple_of_interval
Expand All @@ -129,4 +139,15 @@ def max_reservation_not_less_than_min
end
end

def clean_up_reservation_rules
if daily_booking?
min_reserve_mins = nil
max_reserve_mins = nil
reserve_interval = nil
else
min_reserve_days = nil
max_reserve_days = nil
end
end

end
4 changes: 4 additions & 0 deletions app/models/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,10 @@ def duration_pricing_mode?
false
end

def daily_booking?
false
end

protected

def translation_scope
Expand Down
19 changes: 8 additions & 11 deletions app/views/instruments/_instrument_fields.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@
- else
= f.input :schedule, as: :readonly, value_method: :display_name, hint: false

- if SettingsHelper.feature_on?(:show_daily_rate_option) && current_user.administrator?
%h2 Pricing
= f.input :daily_booking, as: :boolean, label: false, inline_label: true, input_html: { class: "js--daily-booking" },
hint: text("instruments.instrument_fields.reservation.instruct.daily_booking")

- if f.object.new_record?
= f.input :pricing_mode, as: :radio_buttons, collection: Instrument::PRICING_MODES, label: text("instruments.instrument_fields.schedule.pricing_mode"),
item_wrapper_class: :radio, item_wrapper_tag: :div, wrapper_html: { class: "js--pricing-mode", hidden: f.object.daily_booking }
item_wrapper_class: :radio, item_wrapper_tag: :div, wrapper_html: { class: "js--pricing-mode" }

- daily_booking = f.object.daily_booking?

.well
%h3= text("instruments.instrument_fields.reservation.label.restrict")
Expand All @@ -28,27 +25,27 @@
collection: Instrument::RESERVE_INTERVALS,
label: text("instruments.instrument_fields.reservation.label.reserve_interval"),
hint: text("instruments.instrument_fields.reservation.instruct.reserve_interval"),
wrapper_html: { class: "js--reserve-interval", hidden: f.object.daily_booking }
wrapper_html: { class: "js--reserve-interval", hidden: daily_booking }

= f.input :min_reserve_mins,
label: text("instruments.instrument_fields.reservation.label.min_reserve_mins"),
hint: text("instruments.instrument_fields.reservation.instruct.min_reserve"),
wrapper_html: { class: "js--min-reserve-mins", hidden: f.object.daily_booking }
wrapper_html: { class: "js--min-reserve-mins", hidden: daily_booking }

= f.input :max_reserve_mins,
label: text("instruments.instrument_fields.reservation.label.max_reserve_mins"),
hint: text("instruments.instrument_fields.reservation.instruct.max_reserve"),
wrapper_html: { class: "js--max-reserve-mins", hidden: f.object.daily_booking }
wrapper_html: { class: "js--max-reserve-mins", hidden: daily_booking }

= f.input :min_reserve_days,
label: text("instruments.instrument_fields.reservation.label.min_reserve_days"),
hint: text("instruments.instrument_fields.reservation.instruct.min_reserve"),
wrapper_html: { class: "js--min-reserve-days", hidden: !f.object.daily_booking }
wrapper_html: { class: "js--min-reserve-days", hidden: !daily_booking }

= f.input :max_reserve_days,
label: text("instruments.instrument_fields.reservation.label.max_reserve_days"),
hint: text("instruments.instrument_fields.reservation.instruct.max_reserve"),
wrapper_html: { class: "js--max-reserve-days", hidden: !f.object.daily_booking }
wrapper_html: { class: "js--max-reserve-days", hidden: !daily_booking }

= f.input :min_cancel_hours,
label: text("instruments.instrument_fields.reservation.label.cancel_hours"),
Expand Down
9 changes: 5 additions & 4 deletions app/views/instruments/_instrument_manage_fields.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

= f.input :problems_resolvable_by_user, as: :boolean, hint: false

- if SettingsHelper.feature_on?(:show_daily_rate_option)
= f.input :daily_booking, as: :boolean, hint: false
- daily_booking = @product.daily_booking?

- if @product.daily_booking
- if SettingsHelper.feature_on?(:show_daily_rate_option) || !daily_booking
= f.input :pricing_mode

- if daily_booking
= f.input :min_reserve_days, value_method: :to_i
= f.input :max_reserve_days, value_method: :to_i
- else
= f.input :pricing_mode
= f.input :reserve_interval, value_method: :to_i, label: "Reservation Interval"
= f.input :min_reserve_mins, value_method: :to_i
= f.input :max_reserve_mins, default_value: "None"
Expand Down
1 change: 0 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1222,7 +1222,6 @@ en:
If you enter a value in this field, you must toggle the relay to activate.
reservation:
instruct:
daily_booking: "Only allow daily bookings. Only Schedule Rule pricing mode is supported. Reservation Interval is 1 day and not customizable. Minimum and Maximum Reserve is expressed in days."
reserve_interval: "The minutes of an hour on which a reservation is allowed to begin (e.g. if 5 reservations can be scheduled every 5 minutes)"
min_reserve: "Minimum amount of time for a reservation (optional; must be a multiple of the reservation interval)"
max_reserve: "Maximum amount of time for a reservation (optional)"
Expand Down
7 changes: 0 additions & 7 deletions db/migrate/20241008223047_add_daily_booking_to_products.rb

This file was deleted.

1 change: 0 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,6 @@
t.string "billing_mode", default: "Default", null: false
t.string "pricing_mode", default: "Schedule Rule", null: false
t.boolean "cross_core_ordering_available", default: false, null: false
t.boolean "daily_booking", default: false, null: false
t.integer "min_reserve_days"
t.integer "max_reserve_days"
t.index ["dashboard_token"], name: "index_products_on_dashboard_token"
Expand Down

0 comments on commit 66d912d

Please sign in to comment.