Class: Workarea::Insights::MostDiscountGiven

Inherits:
Base
  • Object
show all
Defined in:
app/models/workarea/insights/most_discount_given.rb

Class Method Summary collapse

Methods inherited from Base

beginning_of_last_month, current, end_of_last_month, generate_daily!, generate_weekly!, #include?, #slug

Methods included from ApplicationDocument

#releasable?

Methods included from Sidekiq::Callbacks

add_worker, assert_valid_config!, async, caching_classes?, disable, enable, inline, #run_callbacks, workers, workers_list

Methods included from Mongoid::Document

#embedded_children

Class Method Details

.dashboardsObject



5
6
7
# File 'app/models/workarea/insights/most_discount_given.rb', line 5

def dashboards
  %w(marketing)
end

.find_total_discountObject



43
44
45
46
47
48
# File 'app/models/workarea/insights/most_discount_given.rb', line 43

def find_total_discount
  Metrics::DiscountByDay
    .where(:reporting_on.gte => beginning_of_last_month)
    .where(:reporting_on.lte => end_of_last_month)
    .sum(:discounts) || 0
end

.generate_monthly!Object



9
10
11
12
# File 'app/models/workarea/insights/most_discount_given.rb', line 9

def generate_monthly!
  results = generate_results
  create!(results: results) if results.present?
end

.generate_resultsObject



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

def generate_results
  total_discount = find_total_discount

  report
    .results
    .take(Workarea.config.insights_discounts_list_max_results)
    .map do |result|
      percent_of_total = if total_discount.zero?
        0
      else
        (result['discounts'] / total_discount.to_f) * 100
      end

      result.merge(
        discount_id: result['_id'],
        percent_of_total: percent_of_total
      )
    end
end

.reportObject



34
35
36
37
38
39
40
41
# File 'app/models/workarea/insights/most_discount_given.rb', line 34

def report
  Reports::SalesByDiscount.new(
    starts_at: beginning_of_last_month,
    ends_at: end_of_last_month,
    sort_by: 'discounts',
    sort_direction: 'asc'
  )
end