Class: Customer::PricingProgramAssigner

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::API, ActiveModel::Attributes, ActiveModel::Validations, ActiveModel::Validations::Callbacks, Heatwave::AttributeNormalizing, StripAttributes
Defined in:
app/services/customer/pricing_program_assigner.rb

Overview

Questions

  1. What profile best describes your business?
  2. How many offices and showrooms do you have?
  3. Which buying group, distribution center or franchise are you affiliated with?
  4. How many radiant heating projects do you complete per year?
  5. Which Pricing Program would you like?

Constant Summary collapse

GOLD_LEVEL_MIN_THRESHOLD =

Threshold for gold level min.

5000
GOLD_LEVEL_MAX_THRESHOLD =

Threshold for gold level max.

14_999.99
PLATINUM_LEVEL_MIN_THRESHOLD =

Threshold for platinum level min.

15_000
PLATINUM_LEVEL_MAX_THRESHOLD =

Threshold for platinum level max.

34_999.99
TITANIUM_LEVEL_MIN_THRESHOLD =

Threshold for titanium level min.

35_000

Delegated Instance Attributes collapse

Instance Method Summary collapse

Methods included from Heatwave::AttributeNormalizing

normalize

Instance Method Details

#always_msrp?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'app/services/customer/pricing_program_assigner.rb', line 62

def always_msrp?
  homeowner? || unknown? || e_commerce_misc?
end

#basic_discountObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/services/customer/pricing_program_assigner.rb', line 100

def basic_discount
  if always_msrp? || !customer.catalog.is_main_catalog?
    Coupon.find_by(code: 'PP-LEVEL-0')
  elsif q3_answer.present?
    BuyingGroup.find_or_create_by!(company_id: customer.company.id, name: q3_answer).tier2_program_pricing
  elsif ['Home office only', 'One office (without showroom)', 'Multiple offices (without showroom)'].include?(q2_answer)
    q4_answer == '0' ? Coupon.find_by(code: 'PP-LEVEL-1') : determine_discount_level_from_purchases
  elsif q2_answer == 'One office (with showroom)'
    if q4_answer == '0'
      Coupon.find_by(code: 'PP-LEVEL-2')
    elsif %w[1 2].include?(q4_answer)
      Coupon.find_by(code: 'PP-LEVEL-3')
    elsif q4_answer == '3 or more'
      Coupon.find_by(code: 'PP-LEVEL-4')
    end
  elsif q2_answer == 'Multiple offices (with one or more showrooms)'
    if q4_answer == '0'
      Coupon.find_by(code: 'PP-LEVEL-3')
    else
      Coupon.find_by(code: 'PP-LEVEL-4')
    end
  else
    raise 'Cannot determine basic discount'
  end
end

#coupon_boosterObject



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'app/services/customer/pricing_program_assigner.rb', line 230

def coupon_booster
  gold = Coupon::Tier2ProgramPricing.find_by(code: 'PP-GOLD-LEVEL')
  platinum = Coupon::Tier2ProgramPricing.find_by(code: 'PP-PLATINUM-LEVEL')
  titanium = Coupon::Tier2ProgramPricing.find_by(code: 'PP-TITANIUM-LEVEL')
  if customer.customer_record.a12net_rev > TITANIUM_LEVEL_MIN_THRESHOLD && customer.tier2_program_pricing.amount_goods < titanium.amount_goods
    new_level = titanium
  elsif customer.customer_record.a12net_rev > PLATINUM_LEVEL_MIN_THRESHOLD && customer.customer_record.a12net_rev <= PLATINUM_LEVEL_MAX_THRESHOLD && customer.tier2_program_pricing.amount_goods < platinum.amount_goods
    new_level = platinum
  elsif customer.customer_record.a12net_rev > GOLD_LEVEL_MIN_THRESHOLD && customer.customer_record.a12net_rev <= GOLD_LEVEL_MAX_THRESHOLD && customer.tier2_program_pricing.amount_goods < gold.amount_goods
    new_level = gold
  end
  return unless new_level

  customer.update(tier2_program_pricing: new_level)
  create_activity_for_a_higher_level(new_level.code)
end

#create_activity_for_a_higher_level(level = nil) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'app/services/customer/pricing_program_assigner.rb', line 194

def create_activity_for_a_higher_level(level = nil)
  task_type = {
    'PP-GOLD-LEVEL' => 'PRODISCOUNTGOLD',
    'PP-PLATINUM-LEVEL' => 'PRODISCOUNTPLATINUM',
    'PP-TITANIUM-LEVEL' => 'PRODISCOUNTTITANIUM'
  }[level]
  return unless task_type

  customer.activities.create(party: customer,
                             activity_type: ActivityType.find_by(task_type: task_type),
                             target_datetime: Time.current,
                             assigned_resource: customer.primary_sales_rep)
end

#create_multi_office_activityObject



187
188
189
190
191
192
# File 'app/services/customer/pricing_program_assigner.rb', line 187

def create_multi_office_activity
  customer.activities.create(party: customer,
                             activity_type: ActivityType.find_by(task_type: 'CREATE_SUB_ACCOUNTS'),
                             target_datetime: Time.current,
                             assigned_resource: customer.primary_sales_rep)
end

#customerObject

def using_override_pricing?
return false if q5_answer.blank?
Coupon.find(q5_answer).override?
end



56
57
58
59
60
# File 'app/services/customer/pricing_program_assigner.rb', line 56

def customer
  Customer.find(customer_id)
rescue StandardError
  nil
end

#determine_discount_level_from_purchases(number_of_orders = nil) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/services/customer/pricing_program_assigner.rb', line 80

def determine_discount_level_from_purchases(number_of_orders = nil)
  number_of_orders ||= q4_answer
  number_of_orders = '3' if number_of_orders == '3 or more'
  number_of_orders = number_of_orders.to_i
  case number_of_orders.to_i
  when 0
    Coupon.find_by(code: 'PP-LEVEL-1')
  when 1
    Coupon.find_by(code: 'PP-LEVEL-2')
  when 2
    Coupon.find_by(code: 'PP-LEVEL-3')
  else
    Coupon.find_by(code: 'PP-LEVEL-4')
  end
end

#e_commerce_misc?Object

Alias for Profile#e_commerce_misc?

Returns:

  • (Object)

    Profile#e_commerce_misc?

See Also:



72
# File 'app/services/customer/pricing_program_assigner.rb', line 72

delegate :e_commerce_misc?, to: :profile

#existing_discountObject



136
137
138
# File 'app/services/customer/pricing_program_assigner.rb', line 136

def existing_discount
  customer.tier2_program_pricing
end

#existing_discount_better_than_basic?Boolean

Returns:

  • (Boolean)


140
141
142
143
144
# File 'app/services/customer/pricing_program_assigner.rb', line 140

def existing_discount_better_than_basic?
  return false if existing_discount.nil? || basic_discount.nil?

  existing_discount.amount_goods > basic_discount.amount_goods
end

#homeowner?Object

Alias for Profile#homeowner?

Returns:

  • (Object)

    Profile#homeowner?

See Also:



66
# File 'app/services/customer/pricing_program_assigner.rb', line 66

delegate :homeowner?, to: :profile

#options_step?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'app/services/customer/pricing_program_assigner.rb', line 47

def options_step?
  step == :options
end

#orders_eligibleObject



208
209
210
211
212
213
214
215
216
217
# File 'app/services/customer/pricing_program_assigner.rb', line 208

def orders_eligible
  grouped_orders = customer.orders.so_only.invoiced.where(Order[:line_total].gteq(200))
                           .where(Order[:shipped_date].gteq(1.year.ago))
                           .reorder('') # Need this to allow the grouping to work
                           .group(:opportunity_id) # Group by opportunity id (nil opportunity are unassociated orders)
                           .count
  num_orders = grouped_orders[nil] || 0 # unassociated orders are counted one for one
  num_orders += [grouped_orders.keys.size - 1, 0].max # count the number of opportunities with qualifying orders
  num_orders
end

#override_discount_optionsObject



126
127
128
129
130
131
132
133
134
# File 'app/services/customer/pricing_program_assigner.rb', line 126

def override_discount_options
  levels = ['PP-LEVEL-0', 'PP-LEVEL-1-OVERRIDE', 'PP-LEVEL-2-OVERRIDE', 'PP-LEVEL-3-OVERRIDE', 'PP-LEVEL-4-OVERRIDE']
  # show level 5 for Wholesale Distributor, Dealer and Trade Pro (unless affiliated with a buying group)
  levels << 'PP-LEVEL-5-OVERRIDE' if profile.direct_pro? && q3_answer.blank?
  options = Coupon.where(code: levels).order(amount_goods: :asc)

  options = options.where('amount_goods > ?', basic_discount.amount_goods) if basic_discount
  options
end

#override_option_selected?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'app/services/customer/pricing_program_assigner.rb', line 96

def override_option_selected?
  override_discount_options.map { |d| d.id.to_s }.include?(q5_answer)
end

#profileObject



74
75
76
77
78
# File 'app/services/customer/pricing_program_assigner.rb', line 74

def profile
  Profile.find(customer.profile_id)
rescue StandardError
  nil
end

#progressObject



219
220
221
222
223
224
225
226
227
228
# File 'app/services/customer/pricing_program_assigner.rb', line 219

def progress
  return 'not eligible' if customer.is_homeowner? || !customer.catalog.is_main_catalog?

  eligible_program = determine_discount_level_from_purchases(orders_eligible)
  if eligible_program.discount_percentage > customer.pricing_program_discount
    customer.update(tier2_program_pricing: eligible_program)
  else
    'nothing to update'
  end
end

#send_terms_confirmation_emailObject



181
182
183
184
185
# File 'app/services/customer/pricing_program_assigner.rb', line 181

def send_terms_confirmation_email
  CommunicationBuilder.new(template_system_code: 'PRICING_TERMS', recipient_party: customer,
                           emails: email_terms_to,
                           sender_party: customer.primary_sales_rep || customer.sales_manager).create
end

#unknown?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'app/services/customer/pricing_program_assigner.rb', line 68

def unknown?
  profile.name == 'Unknown'
end

#update_customerObject



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'app/services/customer/pricing_program_assigner.rb', line 146

def update_customer
  tier2_coupon_id = if always_msrp? || !customer.catalog.is_main_catalog?
                      Coupon.find_by(code: 'PP-LEVEL-ZERO').id
                    elsif customer.buying_group_id.present?
                      BuyingGroup.find_by(id: customer.buying_group_id).tier2_program_pricing_id
                    else
                      Coupon.find_by(code: 'PP-SILVER-LEVEL').id
                    end
  c = customer
  c.tier2_program_pricing
  coupon = Coupon.find_by(id: tier2_coupon_id)
  # tier2_coupon_id = q5_answer
  # old_offices = c.number_of_offices
  # new_offices = q2_answer

  if homeowner?
    # Name fixes
    np = PersonNameParser.new(c.full_name)
    np.to_party(c)
  end

  # c.profile_id = q1_answer
  # c.number_of_offices = q2_answer
  # c.buying_group = q3_answer.blank? ? nil : BuyingGroup.where(company_id: customer.company.id, name: q3_answer).first
  # c.projects_per_year = q4_answer

  return unless coupon.amount_goods > c.tier2_program_pricing.amount_goods

  c.tier2_program_pricing_id = tier2_coupon_id
  c.save!

  # send_terms_confirmation_email if old_pp != new_pp and email_terms_to.present?
  # create_multi_office_activity if old_offices != new_offices and new_offices == 'Multiple offices (with one or more showrooms)'
end