Class: CustomerLocatorUtility

Inherits:
Object
  • Object
show all
Defined in:
app/models/customer_locator_utility.rb

Overview

Bulk operations on the Dealer/Installer Locator (the public-facing
"find a pro near me" surface). Backfills LocatorRecords for newly
eligible dealers and produces the support-options data set used by
CRM dashboards. trial_run: true performs no writes — useful for
previewing the impact of an eligibility-rule change before applying it.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger = nil, trial_run = false) ⇒ CustomerLocatorUtility

Returns a new instance of CustomerLocatorUtility.



8
9
10
11
# File 'app/models/customer_locator_utility.rb', line 8

def initialize(logger = nil, trial_run = false)
  @logger = logger || Rails.logger
  @trial_run = trial_run
end

Class Method Details

.create_default_locator_record(customer, _save_lr = true) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/models/customer_locator_utility.rb', line 87

def self.create_default_locator_record(customer, _save_lr = true)
  lr = new_locator_record_with_defaults(customer)
  # set default contact points
  lr_cp_ids = lr.contact_point_ids
  lr_phone = customer.guess_best_callable_contact_point
  lr_cp_ids << lr_phone.id if lr_phone
  lr_fax = customer.guess_best_contact_point_by_category('fax')
  lr_cp_ids << lr_fax.id if lr_fax
  lr_website = customer.guess_best_contact_point_by_category('website')
  lr_cp_ids << lr_website.id if lr_website
  lr.contact_point_ids = lr_cp_ids.uniq.compact
  lr_saved = lr.save
  unless lr_saved
    lr.errors.each do |k, msg|
      customer.errors.add k, msg
    end
  end
  lr_saved
end

.customer_has_countertop_display(customer) ⇒ Object



79
80
81
# File 'app/models/customer_locator_utility.rb', line 79

def self.customer_has_countertop_display(customer)
  customer.activities.where(activity_type_id: 22, activity_result_type_id: 2).present? # DISPINF
end

.customer_has_infloor_display(customer) ⇒ Object



75
76
77
# File 'app/models/customer_locator_utility.rb', line 75

def self.customer_has_infloor_display(customer)
  customer.activities.where(activity_type_id: 23, activity_result_type_id: 2).present?
end

.customer_has_marketing_materials(customer) ⇒ Object



83
84
85
# File 'app/models/customer_locator_utility.rb', line 83

def self.customer_has_marketing_materials(customer)
  customer.activities.where(activity_type_id: [19, 22, 23, 24], activity_result_type_id: 2).present? # BINDER, DISPHCTD, DISPINF, DISPTW
end

.new_locator_record_with_defaults(customer) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/models/customer_locator_utility.rb', line 35

def self.new_locator_record_with_defaults(customer)
  lr = LocatorRecord.new(customer_id: customer.id)
  # set as inactive if customer was blacklisted
  lr.is_inactive = true if customer.locator_black_listing
  # set default capabilities
  lr.is_installer = true if customer.customer_record&.has_install_capability
  lr.is_retailer = true if customer.customer_record&.has_sales_capability
  lr.has_in_floor_display = customer_has_infloor_display(customer)
  lr.has_countertop_display = customer_has_countertop_display(customer)
  lr.has_marketing_materials = customer_has_marketing_materials(customer)
  # set support capabilities
  begin
    customer_support_options(lr, customer)
  rescue StandardError
    nil
  end
  # set default address
  lr.address_id = begin
    customer.main_address.id
  rescue StandardError
    nil
  end
  lr_email = customer.guess_best_contact_point_by_category('email')
  lr.contact_point_ids = [lr_email.id] if lr_email
  lr
end

Instance Method Details

#create_new_locator_records_for_qualifying_dealersObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/models/customer_locator_utility.rb', line 13

def create_new_locator_records_for_qualifying_dealers
  @logger.info "### Set Default Dealer Locator Records started #{Time.current}"
  @logger.info "### TRIAL RUN MODE" if @trial_run
  customers_rel = Customer.locator_eligible
  customers_count = customers_rel.size
  @logger.info "$$$ Customers to process: #{customers_count}, finding all customers that meet criteria for dealers without a dealer locator record."
  customer_processed_counter = 0
  customers_rel.select("parties.*").find_each do |c|
    @logger.info "*** Processing Customer ID: #{c.id}, name: #{c.name}"
    unless @trial_run
      if self.class.create_default_locator_record(c)
        customer_processed_counter += 1
      else
        err_msg = "!!! Customer ID: #{c.id}, name: #{c.name}, locator record failed to process, errors: #{c.errors.full_messages}"
        ErrorReporting.error err_msg
        @logger.error err_msg
      end
    end
  end
  @logger.info "*** Processed #{customer_processed_counter} out of #{customers_count}"
end

#customer_support_options(lr, customer) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/models/customer_locator_utility.rb', line 62

def customer_support_options(lr, customer)
  invoices = customer.invoices.where(invoice_type: 'SO').where(Invoice[:gl_date].gt(1.year.ago))
  return if invoices.blank?

  product_line_slug_ltrees = LineItem.collect_product_lines(invoices.map(&:line_items).flatten.uniq).map { |li| li.slug_ltree.to_s }.flatten.uniq
  product_line_slug_ltrees.each do |slt|
    lr.supports_flooring = true if slt.start_with?('floor_heating')
    lr.supports_snowmelting = true if slt.start_with?('snow_melting')
    lr.supports_roof_deicing = true if slt.start_with?('roof_and_gutter_deicing', 'pipe_freeze_protection')
    lr.supports_comfort_products = true if slt.start_with?('towel_warmer', 'led_mirror', 'mirror_defogger', 'countertop_heater', 'infrared_heating_panels')
  end
end