Class: CustomerLocatorUtility
- Inherits:
-
Object
- Object
- CustomerLocatorUtility
- 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
- .create_default_locator_record(customer, _save_lr = true) ⇒ void
- .customer_has_countertop_display(customer) ⇒ void
- .customer_has_infloor_display(customer) ⇒ void
- .customer_has_marketing_materials(customer) ⇒ void
- .new_locator_record_with_defaults(customer) ⇒ void
Instance Method Summary collapse
- #create_new_locator_records_for_qualifying_dealers ⇒ void
- #customer_support_options(lr, customer) ⇒ void
- #initialize(logger = nil, trial_run = false) ⇒ void constructor
Constructor Details
#initialize(logger = nil, trial_run = false) ⇒ void
11 12 13 14 |
# File 'app/models/customer_locator_utility.rb', line 11 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) ⇒ void
This method returns an undefined value.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'app/models/customer_locator_utility.rb', line 105 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) ⇒ void
This method returns an undefined value.
92 93 94 |
# File 'app/models/customer_locator_utility.rb', line 92 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) ⇒ void
This method returns an undefined value.
86 87 88 |
# File 'app/models/customer_locator_utility.rb', line 86 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) ⇒ void
This method returns an undefined value.
98 99 100 |
# File 'app/models/customer_locator_utility.rb', line 98 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) ⇒ void
This method returns an undefined value.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'app/models/customer_locator_utility.rb', line 41 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 (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_dealers ⇒ void
This method returns an undefined value.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'app/models/customer_locator_utility.rb', line 17 def @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.}" 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) ⇒ void
This method returns an undefined value.
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'app/models/customer_locator_utility.rb', line 71 def (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 |