Class: Customer::ReportGrouping

Inherits:
Object
  • Object
show all
Defined in:
app/services/customer/report_grouping.rb

Overview

Service object: report grouping.

Constant Summary collapse

ETAILER_GROUPINGS =

Marketplace / e-tailer sales channels (report_grouping values) — the
complement of direct_sales_report_grouping.

['Amazon', 'Costco', 'Direct Buy', 'E-Tailers', 'Home Depot',
'Lowes', 'RONA Canada', 'Wal-Mart'].freeze

Class Method Summary collapse

Class Method Details

.direct_sales_report_groupingObject



26
27
28
# File 'app/services/customer/report_grouping.rb', line 26

def self.direct_sales_report_grouping
  select_options - ETAILER_GROUPINGS
end

.etailer_groupings_sql_listObject

ETAILER_GROUPINGS as a SQL literal list, so callers that filter in SQL
can't drift from the constant the way the gross-sales report and the Daily
Focus prompt both had. Safe to interpolate: every value is a frozen literal
above, none contains a quote.



14
15
16
# File 'app/services/customer/report_grouping.rb', line 14

def self.etailer_groupings_sql_list
  ETAILER_GROUPINGS.map { |grouping| "'#{grouping}'" }.join(',')
end

.get_report_grouping_for_customer(customer) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'app/services/customer/report_grouping.rb', line 30

def self.get_report_grouping_for_customer(customer)
  billing_entity = customer.try(:billing_entity) || customer
  billing_entity.report_grouping.presence ||
    billing_entity.try(:buying_group).try(:report_grouping).presence ||
    billing_entity.try(:buying_group).try(:name).presence ||
    billing_entity.try(:profile).try(:report_grouping).presence ||
    billing_entity.try(:profile).try(:name).presence ||
    'Homeowners'
end

.select_optionsObject



18
19
20
21
22
23
24
# File 'app/services/customer/report_grouping.rb', line 18

def self.select_options
  channels = [] +
             BuyingGroup.where.not(report_grouping: nil).pluck(:report_grouping) +
             Profile.where.not(report_grouping: nil).pluck(:report_grouping) +
             Customer.where.not(report_grouping: nil).pluck(:report_grouping)
  channels.sort.uniq
end

.synchronize_historical_data(customer) ⇒ Object



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

def self.synchronize_historical_data(customer)
  report_grouping = get_report_grouping_for_customer(customer)
  customer.invoices.where.not(report_grouping: report_grouping).update_all(report_grouping: report_grouping)
end