Class: Report::OrdersOriginReport::OrdersReport
- Inherits:
-
Object
- Object
- Report::OrdersOriginReport::OrdersReport
- Defined in:
- app/services/report/orders_origin_report/orders_report.rb
Overview
Service object: orders report.
Defined Under Namespace
Classes: Result
Constant Summary collapse
- GROUPING_ALLOWLIST =
Grouping allowlist.
%w[quarter month week].freeze
Class Method Summary collapse
-
.online_orders_acumulate(start_date, end_date, report_groupings) ⇒ Object
Online orders acumulate.
-
.online_orders_breakdown(start_date, end_date, grouping, report_groupings) ⇒ Object
Online orders breakdown.
-
.online_orders_by_year(start_date, end_date, report_groupings) ⇒ Object
Online orders by year.
-
.opportunity_report(options = {}) ⇒ Result
Orders-by-origin data for the current and previous year.
Class Method Details
.online_orders_acumulate(start_date, end_date, report_groupings) ⇒ Object
Online orders acumulate.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'app/services/report/orders_origin_report/orders_report.rb', line 108 def self.online_orders_acumulate(start_date, end_date, report_groupings) report_groupings_sql = report_groupings.empty? ? '' : " and report_grouping in (#{report_groupings.map { |rg| ActiveRecord::Base.lease_connection.quote(rg) }.join(',')})" sql = <<-SQL.squish 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 ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.online_orders_breakdown(start_date, end_date, grouping, report_groupings) ⇒ Object
Online orders breakdown.
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'app/services/report/orders_origin_report/orders_report.rb', line 143 def self.online_orders_breakdown(start_date, end_date, grouping, report_groupings) raise ArgumentError, "Invalid grouping: #{grouping.inspect}" unless GROUPING_ALLOWLIST.include?(grouping) report_groupings_sql = report_groupings.empty? ? '' : " and report_grouping in (#{report_groupings.map { |rg| ActiveRecord::Base.lease_connection.quote(rg) }.join(',')})" sql = <<-SQL.squish 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 ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.online_orders_by_year(start_date, end_date, report_groupings) ⇒ Object
Online orders by year.
67 68 69 70 71 72 73 74 75 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 101 |
# File 'app/services/report/orders_origin_report/orders_report.rb', line 67 def self.online_orders_by_year(start_date, end_date, report_groupings) report_groupings_sql = report_groupings.empty? ? '' : " and report_grouping in (#{report_groupings.map { |rg| ActiveRecord::Base.lease_connection.quote(rg) }.join(',')})" sql = <<-SQL.squish 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 ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.opportunity_report(options = {}) ⇒ Result
Returns orders-by-origin data for the current and previous year.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'app/services/report/orders_origin_report/orders_report.rb', line 30 def self.opportunity_report( = {}) start_date = [:period1_gteq] end_date = [:period1_lteq] grouping = [:grouping] report_groupings = [:report_groupings].filter_map(&:presence) ask_year = [:period1_lteq].year.to_i ask_previous_year = [: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 |