Class: Customer::PricingProgramAssigner

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::API, ActiveModel::Attributes, ActiveModel::Validations, ActiveModel::Validations::Callbacks, Normalizr::Concern, 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 =
5000
GOLD_LEVEL_MAX_THRESHOLD =
14_999.99
PLATINUM_LEVEL_MIN_THRESHOLD =
15_000
PLATINUM_LEVEL_MAX_THRESHOLD =
34_999.99
TITANIUM_LEVEL_MIN_THRESHOLD =
35_000

Delegated Instance Attributes collapse

Instance Method Summary collapse

Instance Method Details

#always_msrp?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'app/services/customer/pricing_program_assigner.rb', line 55

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

#basic_discountObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/services/customer/pricing_program_assigner.rb', line 93

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 q4_answer == '1' or q4_answer == '2'
      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



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'app/services/customer/pricing_program_assigner.rb', line 223

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



187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'app/services/customer/pricing_program_assigner.rb', line 187

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



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

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



49
50
51
52
53
# File 'app/services/customer/pricing_program_assigner.rb', line 49

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

#determine_discount_level_from_purchases(number_of_orders = nil) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/services/customer/pricing_program_assigner.rb', line 73

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:



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

delegate :e_commerce_misc?, to: :profile

#existing_discountObject



129
130
131
# File 'app/services/customer/pricing_program_assigner.rb', line 129

def existing_discount
  customer.tier2_program_pricing
end

#existing_discount_better_than_basic?Boolean

Returns:

  • (Boolean)


133
134
135
136
137
# File 'app/services/customer/pricing_program_assigner.rb', line 133

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

  existing_discount.amount_goods > basic_discount.amount_goods
end

#homeowner?Object

Alias for Profile#homeowner?

Returns:

  • (Object)

    Profile#homeowner?

See Also:



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

delegate :homeowner?, to: :profile

#options_step?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'app/services/customer/pricing_program_assigner.rb', line 40

def options_step?
  step == :options
end

#orders_eligibleObject



201
202
203
204
205
206
207
208
209
210
# File 'app/services/customer/pricing_program_assigner.rb', line 201

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



119
120
121
122
123
124
125
126
127
# File 'app/services/customer/pricing_program_assigner.rb', line 119

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)


89
90
91
# File 'app/services/customer/pricing_program_assigner.rb', line 89

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

#profileObject



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

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

#progressObject



212
213
214
215
216
217
218
219
220
221
# File 'app/services/customer/pricing_program_assigner.rb', line 212

def progress
  return 'not eligible' if customer.is_homeowner? or !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



174
175
176
177
178
# File 'app/services/customer/pricing_program_assigner.rb', line 174

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)


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

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

#update_customerObject



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
170
171
172
# File 'app/services/customer/pricing_program_assigner.rb', line 139

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

  if coupon.amount_goods > c.tier2_program_pricing.amount_goods
    c.tier2_program_pricing_id = tier2_coupon_id
    c.save!
  end

  # 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