Class: HhPackage::ReservationPackages::SelectedPackagesBuilder

Inherits:
Object
  • Object
show all
Defined in:
app/my_lib/hh_package/reservation_packages/selected_packages_builder.rb

Overview

HhPackage::ReservationPackages::SelectedPackagesBuilder is responsible for building a list of selected packages based on the provided packages and pricing rules.

This builder class calculates base prices, applies mix-and-match quantity rules, handles pricing for adults and kids, and optionally applies promotional pricing like “Come more pay less”.

Examples:

Initialize and build selected packages

builder = HhPackage::ReservationPackages::SelectedPackagesBuilder.new(
  packages: packages,
  restaurant_package_id: 1,
  adult: 2,
  kids: 1,
  price_cents: 1000,
  date: Date.today
  use_dynamic_pricing: true,
  custom_price_cents: 1500,
  group_sections: group_sections
)
builder.build

Returns:

  • (Array<Hash>)

    A list of selected packages with calculated prices

Instance Method Summary collapse

Constructor Details

#initialize(packages:, restaurant_package_id:, adult:, kids:, price_cents:, date:, use_dynamic_pricing: true, custom_price_cents: nil, group_sections: nil) ⇒ SelectedPackagesBuilder

Returns a new instance of SelectedPackagesBuilder.



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/my_lib/hh_package/reservation_packages/selected_packages_builder.rb', line 35

def initialize(packages:, restaurant_package_id:, adult:, kids:, price_cents:, date:, use_dynamic_pricing: true, custom_price_cents: nil, group_sections: nil)
  @packages = packages
  @restaurant_package_id = restaurant_package_id
  @adult = to_integer(adult)
  @kids = to_integer(kids)
  @price_cents = price_cents
  @date = date
  @price_finder = HhPackage::ReservationPackages::PriceFinder.new
  @price_finder.use_dynamic_pricing = use_dynamic_pricing
  @custom_price_cents = custom_price_cents
  @selected_kids_price_package_id = nil
  @group_sections = group_sections
end

Instance Method Details

#buildArray<Hash>

Build the selected packages and calculate prices for adults and kids.

Returns:

  • (Array<Hash>)

    A list of selected packages with calculated prices



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/my_lib/hh_package/reservation_packages/selected_packages_builder.rb', line 52

def build
  selected_package = find_selected_package
  return [] if selected_package.blank?

  package = selected_package[:package]
  base_price = calculate_base_price(package).cents
  selected_qty = package.mix_n_match_qty(@adult, selected_package[:quantity], @packages.count)

  kids_price_data = find_kids_price_if_applicable(package)
  adult_price = calculate_adult_price(base_price, selected_qty)
  kids_price = calculate_kids_price(kids_price_data[:price]).cents

  adult_price = ensure_valid_adult_price(adult_price, package)
  promo_price = calculate_come_more_pay_less_price(package, adult_price, selected_qty)
  spending_tier_discount = calculate_spending_tier_discount(package)

  build_selected_packages_response(package, selected_qty, kids_price, kids_price_data, base_price, adult_price,
                                   promo_price, spending_tier_discount, selected_package)
end

#extract_spending_tierHash?

Extract DIY spending tier information from the selected packages. This method returns the spending tier data that was previously included in each package response. The spending tier provides information about spending thresholds and discount percentages for DIY packages, which is now returned as a separate attribute in the API response. The structure is flattened to include the appropriate tier data directly based on total price.

Examples:

{
  spending_type: 'per_booking',
  discount_type: 'percentage',
  name: 'Spending Incentive Tier 2',  # Selected based on total price
  price: { price: 150, price_cents: 15000, currency: 'THB', format: '฿150' },
  discount_percent: '5.0'
}

Returns:

  • (Hash, nil)

    A hash containing the flattened spending tier details or nil if not applicable



87
88
89
90
91
92
93
94
95
# File 'app/my_lib/hh_package/reservation_packages/selected_packages_builder.rb', line 87

def extract_spending_tier
  selected_package = find_selected_package
  return nil if selected_package.blank?

  package = selected_package[:package]
  return nil unless package.type_short.to_sym == :diy

  diy_spending_tier(package)
end