Class: HhPackage::ReservationPackages::Metadata

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

Overview

}]

Defined Under Namespace

Classes: CustomPkgPricing

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(package_params: [], adult: 0, kids: 0, user_id: nil, date: nil, vouchers: [], corporate_event_id: nil, use_dynamic_pricing: false) ⇒ Metadata

Returns a new instance of Metadata.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/my_lib/hh_package/reservation_packages/metadata.rb', line 99

def initialize(package_params: [], adult: 0, kids: 0, user_id: nil,
               date: nil, vouchers: [], corporate_event_id: nil,
               use_dynamic_pricing: false)
  self.package_params = package_params.map(&:with_indifferent_access)
  self.adult = adult
  self.kids = kids
  self.user_id = user_id
  self.date = date
  self.vouchers = vouchers
  self.covered_by_hh = false
  self.corporate_event_id = corporate_event_id
  self.use_dynamic_pricing = use_dynamic_pricing
  @price_finder = HhPackage::ReservationPackages::PriceFinder.new use_dynamic_pricing: use_dynamic_pricing
end

Instance Attribute Details

#adultObject

Returns the value of attribute adult.



52
53
54
# File 'app/my_lib/hh_package/reservation_packages/metadata.rb', line 52

def adult
  @adult
end

#corporate_event_idObject

Returns the value of attribute corporate_event_id.



52
53
54
# File 'app/my_lib/hh_package/reservation_packages/metadata.rb', line 52

def corporate_event_id
  @corporate_event_id
end

#covered_by_hhObject

Returns the value of attribute covered_by_hh.



52
53
54
# File 'app/my_lib/hh_package/reservation_packages/metadata.rb', line 52

def covered_by_hh
  @covered_by_hh
end

#dateObject

Returns the value of attribute date.



52
53
54
# File 'app/my_lib/hh_package/reservation_packages/metadata.rb', line 52

def date
  @date
end

#kidsObject

Returns the value of attribute kids.



52
53
54
# File 'app/my_lib/hh_package/reservation_packages/metadata.rb', line 52

def kids
  @kids
end

#package_paramsObject

Returns the value of attribute package_params.



52
53
54
# File 'app/my_lib/hh_package/reservation_packages/metadata.rb', line 52

def package_params
  @package_params
end

#use_dynamic_pricingObject

Returns the value of attribute use_dynamic_pricing.



52
53
54
# File 'app/my_lib/hh_package/reservation_packages/metadata.rb', line 52

def use_dynamic_pricing
  @use_dynamic_pricing
end

#user_idObject

Returns the value of attribute user_id.



52
53
54
# File 'app/my_lib/hh_package/reservation_packages/metadata.rb', line 52

def user_id
  @user_id
end

#vouchersObject

Returns the value of attribute vouchers.



52
53
54
# File 'app/my_lib/hh_package/reservation_packages/metadata.rb', line 52

def vouchers
  @vouchers
end

Class Method Details

.fix_package_quantity(package_params, adult, _kids) ⇒ Object

Parameters:

  • package_params (Array)
    { id: 34, quantity: 1 }

    id is a HhPackage::RestaurantPackage's id

  • adult (Integer)
  • kids (Integer)


177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'app/my_lib/hh_package/reservation_packages/metadata.rb', line 177

def self.fix_package_quantity(package_params, adult, _kids)
  package_params.map do |package_param|
    package_param = package_param.to_h.with_indifferent_access
    restaurant_package = HhPackage::RestaurantPackage.fetch package_param['id']
    package = restaurant_package.package

    quantity = case package.dynamic_price_pricing_model.to_sym
               when :per_person
                 package.mix_n_match_qty(adult, package_param['quantity'], package_params.count)
               when :per_pack
                 pricing = package.dynamic_pricings.first
                 per_pack = if pricing.nil?
                              APMErrorHandler.report "missing pricing for package #{package.id}",
                                                     dynamic_pricings: package.dynamic_pricings.as_json
                              HhPackage::Pricing::PER_PACK_DEFAULT_WHEN_MISSING
                            else
                              pricing.per_pack
                            end
                 (adult / per_pack.to_d).ceil
               when :per_set
                 package_param['quantity']
               when :per_item
                 # For DIY packages with per_item pricing, quantity remains as specified
                 # since each item is individually priced and selected by the customer
                 package_param['quantity']
               else
                 raise NotImplementedError
               end

    package_param.merge('quantity' => quantity)
  end
end

.from_reservation(reservation, reservation_custom_commission: nil) ⇒ Object

Returns create instance of this class using data from Reservation instance.

Returns:

  • create instance of this class using data from Reservation instance



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/my_lib/hh_package/reservation_packages/metadata.rb', line 65

def self.from_reservation(reservation, reservation_custom_commission: nil)
  package_params = reservation.package_bought.map do |old_pack|
    pack = old_pack.dup
    id = pack.delete :restaurant_package_id
    pack[:id] = id if id.present?
    if reservation_custom_commission.present?
      pack[:use_custom_commision] = true
      pack[:custom_commision_percent] = reservation_custom_commission
    end

    # For DIY packages, rebuild group_sections from reservation.group_sections
    # This ensures pricing calculations include the selected menu items
    if reservation.group_sections.present?
      pack[:group_sections] = ::SectionTrees.build_group_sections_from_reservation(reservation)
    end

    pack
  end

   = HhPackage::ReservationPackages::Metadata.new(
    package_params: package_params,
    adult: reservation.adult,
    kids: reservation.kids,
    date: reservation.date,
    user_id: reservation.user_id,
    corporate_event_id: reservation.corporate_event_id,
    vouchers: Array.wrap(reservation.vouchers),
    use_dynamic_pricing: reservation.booking_channel&.support_dynamic_pricing,
  )
  property = reservation&.property.presence || reservation.build_property
  .covered_by_hh = property.covered_by_hh
  
end

Instance Method Details

#generateObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'app/my_lib/hh_package/reservation_packages/metadata.rb', line 133

def generate
  data = if ayce?
           package_param = package_params.first
           pack = HhPackage::RestaurantPackage.fetch(package_param[:id]).package
           mixed_package = pack.use_mix_and_match?(package_params.count)
           mixed_package ? generate_pp_like(:ayce) : generate_ayce_multiple(:ayce)
         elsif bfp?
           generate_ayce_like(:bfp)
         elsif pp?
           generate_pp_like(:pp)
         elsif hs?
           generate_pp_like(:hs)
         elsif sm?
           generate_ayce_like(:sm)
         elsif hah?
           generate_pp_like(:hah)
         elsif xp?
           generate_pp_like(:xp)
         elsif diy?
           generate_pp_like(:diy)
         elsif package_params.blank?
           {}
         else
           raise InvalidPackageData, 'this feature is not implemented yet'
         end

  if use_custom_package_pricing? || use_custom_package_commision?
    data.merge!(custom_package_data: store_custom_pricing)
    data[:custom_package_data].first[:custom_price_currency] = data[:price_currency]
  end

  if data.present?
    data[:use_dynamic_pricing] = use_dynamic_pricing
  end

  data
end