Class: CustomerLocatorUtility

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of CustomerLocatorUtility.



3
4
5
6
# File 'app/models/customer_locator_utility.rb', line 3

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



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/models/customer_locator_utility.rb', line 73

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
   return lr_saved
end

.customer_has_countertop_display(customer) ⇒ Object



65
66
67
# File 'app/models/customer_locator_utility.rb', line 65

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



61
62
63
# File 'app/models/customer_locator_utility.rb', line 61

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



69
70
71
# File 'app/models/customer_locator_utility.rb', line 69

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



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/models/customer_locator_utility.rb', line 30

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
		customer_support_options(lr, customer) rescue nil
    # set default address
    lr.address_id = customer.main_address.id rescue nil
    lr_email = customer.guess_best_contact_point_by_category('email')
    lr.contact_point_ids = [lr_email.id] if lr_email
    return lr
end

Instance Method Details

#create_new_locator_records_for_qualifying_dealersObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/models/customer_locator_utility.rb', line 8

def create_new_locator_records_for_qualifying_dealers
 	@logger.info "### Set Default Dealer Locator Records started #{Time.current.to_s}"
   @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



49
50
51
52
53
54
55
56
57
58
59
# File 'app/models/customer_locator_utility.rb', line 49

def customer_support_options(lr, customer)
	invoices = customer.invoices.where(invoice_type: 'SO').where(Invoice[:gl_date].gt(1.year.ago))
	return unless invoices.present?
	product_line_slug_ltrees = LineItem.collect_product_lines(invoices.map { |i| i.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_gutter_deicing', 'pipe_freeze_protection')
		lr.supports_comfort_products = true if slt.start_with?('towel_warmer', 'led_mirror', 'mirror_defogger', 'countertop_heater', 'radiant_panel')
	end
end