Class: Report::OrdersOriginReport::OrdersReport

Inherits:
Object
  • Object
show all
Defined in:
app/services/report/orders_origin_report/orders_report.rb

Defined Under Namespace

Classes: Result

Class Method Summary collapse

Class Method Details

.online_orders_acumulate(start_date, end_date, report_groupings) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/services/report/orders_origin_report/orders_report.rb', line 76

def self.online_orders_acumulate(start_date,end_date,report_groupings)
  report_groupings.empty? ? report_groupings_sql = "" : report_groupings_sql = " and report_grouping in (#{report_groupings.map{|rg| "'#{rg.sub("'","''")}'" }.join(',')})"

  sql = <<-SQL
    select detail,sum(quantity_current) as quantity,
    case when sum(quantity_previous) = 0 then 0 else round((((sum(quantity_current) - sum(quantity_previous)) / sum(quantity_previous))*100),1) end as var_qua,
    sum(amount_current) as amount,
    case when sum(amount_previous) = 0 then 0 else round((((sum(amount_current) - sum(amount_previous)) / sum(amount_previous))*100),1) end as var_amt
    from (
        select code,detail,
        case when to_char(ord_date,'YYYY')::int = #{end_date.years_ago(1).year} then count(distinct order_id)::numeric else 0 end quantity_previous,
        case when to_char(ord_date,'YYYY')::int = #{end_date.year} then count(distinct order_id)::numeric else 0 end quantity_current,
        case when to_char(ord_date,'YYYY')::int = #{end_date.years_ago(1).year} then sum(revenue_consolidated)::numeric else 0 end amount_previous,
        case when to_char(ord_date,'YYYY')::int = #{end_date.year} then sum(revenue_consolidated)::numeric else 0 end amount_current
        from view_orders_origin_facts
        where (ord_date between '#{start_date.years_ago(1)}' and '#{end_date.years_ago(1)}' or ord_date between '#{start_date}' and '#{end_date}')
        #{report_groupings_sql}
        group by code,detail,to_char(ord_date,'YYYY')::int
    )a
    group by code,detail
    order by code
  SQL

  results = ActiveRecord::Base.connection.execute(sql).to_a.map{ |r| r.symbolize_keys }
end

.online_orders_breakdown(start_date, end_date, grouping, report_groupings) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/services/report/orders_origin_report/orders_report.rb', line 102

def self.online_orders_breakdown(start_date,end_date,grouping,report_groupings)
  report_groupings.empty? ? report_groupings_sql = "" : report_groupings_sql = " and report_grouping in (#{report_groupings.map{|rg| "'#{rg.sub("'","''")}'" }.join(',')})"

  sql = <<-SQL
    select breakdown,code,cast(sum(revenue_previous_year) as numeric) as revenue_previous_year,
    cast(sum(num_orders_previous_year) as numeric) as num_orders_previous_year,
    cast(sum(revenue_current_year) as numeric) as revenue_current_year,
    cast(sum(num_orders_current_year) as numeric) as num_orders_current_year
    from (
        select #{grouping} as breakdown,code,
        case when year = #{end_date.years_ago(1).year} then sum(revenue_consolidated) else 0 end as revenue_previous_year,
        case when year = #{end_date.years_ago(1).year} then count(distinct order_id) else 0 end as num_orders_previous_year,
        case when year = #{end_date.year} then sum(revenue_consolidated) else 0 end as revenue_current_year,
        case when year = #{end_date.year} then count(distinct order_id) else 0 end as num_orders_current_year
        from view_orders_origin_facts of
        inner join analytic_date_time_dimensions dt on of.ord_date = dt.date
        where (ord_date between '#{start_date.years_ago(1)}' and '#{end_date.years_ago(1)}' or ord_date between '#{start_date}' and '#{end_date}')
        #{report_groupings_sql}
        group by breakdown,code,year
    )a
    group by breakdown,code
    order by code,breakdown
  SQL

  results = ActiveRecord::Base.connection.execute(sql).to_a.map{ |r| r.symbolize_keys }
end

.online_orders_by_year(start_date, end_date, report_groupings) ⇒ Object



40
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
67
68
69
70
71
72
73
74
# File 'app/services/report/orders_origin_report/orders_report.rb', line 40

def self.online_orders_by_year(start_date,end_date,report_groupings)
  report_groupings.empty? ? report_groupings_sql = "" : report_groupings_sql = " and report_grouping in (#{report_groupings.map{|rg| "'#{rg.sub("'","''")}'" }.join(',')})"

  sql = <<-SQL
    select year as year_c,sum(count_code_one) as count_code_one,cast(sum(sum_code_one) as numeric) as sum_code_one,
    sum(count_code_two) as count_code_two,cast(sum(sum_code_two) as numeric) as sum_code_two,
    sum(count_code_thr) as count_code_thr,cast(sum(sum_code_thr) as numeric) as sum_code_thr,
    sum(count_code_fou) as count_code_fou,cast(sum(sum_code_fou) as numeric) as sum_code_fou,
    sum(count_code_fiv) as count_code_fiv,cast(sum(sum_code_fiv) as numeric) as sum_code_fiv,
    sum(count_code_six) as count_code_six,cast(sum(sum_code_six) as numeric) as sum_code_six
    from (
        select to_char(ord_date,'YYYY') as year,
        case when code = 1 then count(distinct order_id) else 0 end as count_code_one,
        case when code = 1 then sum(revenue_consolidated) else 0 end as sum_code_one,
        case when code = 2 then count(distinct order_id) else 0 end as count_code_two,
        case when code = 2 then sum(revenue_consolidated) else 0 end as sum_code_two,
        case when code = 3 then count(distinct order_id) else 0 end as count_code_thr,
        case when code = 3 then sum(revenue_consolidated) else 0 end as sum_code_thr,
        case when code = 4 then count(distinct order_id) else 0 end as count_code_fou,
        case when code = 4 then sum(revenue_consolidated) else 0 end as sum_code_fou,
        case when code = 5 then count(distinct order_id) else 0 end as count_code_fiv,
        case when code = 5 then sum(revenue_consolidated) else 0 end as sum_code_fiv,
        case when code = 6 then count(distinct order_id) else 0 end as count_code_six,
        case when code = 6 then sum(revenue_consolidated) else 0 end as sum_code_six
        from view_orders_origin_facts
        where ord_date between '#{start_date}' and '#{end_date}'
        #{report_groupings_sql}
        group by to_char(ord_date,'YYYY'),code
        order by 1
    )a
    group by year
  SQL

  results = ActiveRecord::Base.connection.execute(sql).to_a.map{ |r| r.symbolize_keys }
end

.opportunity_report(options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/services/report/orders_origin_report/orders_report.rb', line 8

def self.opportunity_report(options = {})
  start_date = options[:period1_gteq]
  end_date = options[:period1_lteq]
  grouping = options[:grouping]
  report_groupings = options[:report_groupings].map(&:presence).compact
  ask_year = options[:period1_lteq].year.to_i
  ask_previous_year = options[:period1_lteq].year.to_i - 1
  analysis_periods = [6,5,4,3,2,1,0]

  online_orders_by_year = []
  analysis_periods.each do |p|
    online_orders_by_year_data = online_orders_by_year(start_date.years_ago(p),end_date.years_ago(p),report_groupings)
    online_orders_by_year << online_orders_by_year_data.first if online_orders_by_year_data.present?
  end
  online_orders_pie = online_orders_by_year
  online_orders_stk = online_orders_by_year
  online_orders_tb = online_orders_acumulate(start_date,end_date,report_groupings)
  online_orders_comp = online_orders_breakdown(start_date,end_date,grouping,report_groupings)
  breakdown_detail = online_orders_comp.map{ |b| b[:breakdown].to_i }.uniq.sort

  Result.new(success: true, online_orders_pie: online_orders_pie,
                            online_orders_stk: online_orders_stk,
                            online_orders_tb: online_orders_tb,
                            online_orders_comp: online_orders_comp,
                            breakdown: breakdown_detail,
                            grouping: grouping,
                            date_from: start_date.to_s,
                            date_to: end_date.to_s,
                            current_year: ask_year,
                            previous_year: ask_previous_year)
end