Class: Report::GrossSalesReport::GrossSalesReport

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

Defined Under Namespace

Classes: Result

Class Method Summary collapse

Class Method Details

.data_comparison_year(start_date, end_date, grouping, report_groupings, sales_rep_ids, company_id, native_currency) ⇒ Object



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'app/services/report/gross_sales_report/gross_sales_report.rb', line 292

def self.data_comparison_year(start_date,end_date,grouping,report_groupings,sales_rep_ids,company_id,native_currency)
  report_groupings.empty? ? report_groupings_sql = "" : report_groupings_sql = " and vsf.report_grouping in (#{report_groupings.map{|rg| "'#{rg.sub("'","''")}'" }.join(',')})"
  (sales_rep_ids == nil || sales_rep_ids.empty?) ? sales_rep_ids_sql = "" : sales_rep_ids_sql = " and primary_sr_id in (#{sales_rep_ids.map{ |e| e.to_i}.join(",")})"
  company_id ? company_id_sql = " and vsf.company_id in (#{company_id.join(",")})" : company_id_sql = ""
  selects = ''; group_by = ''

  revenue_sql = native_currency ? 'revenue_total' : 'revenue_total_consolidated'

  if grouping == 'year'
    selects = " select 1 as ord_num, #{start_date.year} as date, "
    group_by = " group by to_char(gl_date,'YYYY')::int "
  elsif grouping == 'quarter'
    selects = " select dt.quarter as ord_num, case when dt.quarter = 1 then 'First' when dt.quarter = 2 then 'Second' when dt.quarter = 3 then 'Third' when dt.quarter = 4 then 'Fourth' else '' end as date, "
    group_by = " group by dt.quarter,to_char(gl_date,'YYYY')::int "
  elsif grouping == 'month'
    selects = " select dt.month as ord_num,case when dt.month = 1 then 'Jan' when dt.month = 2 then 'Feb' when dt.month = 3 then 'Mar' when dt.month = 4 then 'Apr' when dt.month = 5 then 'May' when dt.month = 6 then 'Jun' when dt.month = 7 then 'Jul' when dt.month = 8 then 'Aug' when dt.month = 9 then 'Sep' when dt.month = 10 then 'Oct' when dt.month = 11 then 'Nov' when dt.month = 12 then 'Dec' else '' end as date, "
    group_by = " group by dt.month,to_char(gl_date,'YYYY')::int "
  elsif grouping == 'week'
    selects = " select dt.week as ord_num, dt.week::text as date, "
    group_by = " group by dt.week,to_char(gl_date,'YYYY')::int "
  end

  sql = <<-SQL
    select
    ord_num,
    date,
    sum(year2) as sales2,
    sum(year1) as sales1
    from (
        #{selects}
        case when to_char(gl_date,'YYYY')::int = #{start_date.years_ago(1).year}  then sum(#{revenue_sql}) else 0 end as year2,
        case when to_char(gl_date,'YYYY')::int = #{end_date.year} then sum(#{revenue_sql}) else 0 end as year1
        from view_sales_facts vsf
        inner join analytic_date_time_dimensions dt on vsf.gl_date = dt.date
        left join parties e on vsf.primary_sr_id = e.id
        where (gl_date between '#{start_date.years_ago(1)}' and '#{end_date.years_ago(1)}' or gl_date between '#{start_date}' and '#{end_date}')
        #{report_groupings_sql}
        #{sales_rep_ids_sql}
        #{company_id_sql}
        #{group_by}
    )a
    group by ord_num,date
    order by ord_num;
  SQL

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

.data_gen_info(start_date, end_date, grouping, report_groupings, sales_rep_ids, company_id, native_currency) ⇒ Object



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'app/services/report/gross_sales_report/gross_sales_report.rb', line 246

def self.data_gen_info(start_date,end_date,grouping,report_groupings,sales_rep_ids,company_id,native_currency)
  report_groupings.empty? ? report_groupings_sql = "" : report_groupings_sql = " and report_grouping in (#{report_groupings.map{|rg| "'#{rg.sub("'","''")}'" }.join(',')})"
  (sales_rep_ids == nil || sales_rep_ids.empty?) ? sales_rep_ids_sql = "" : sales_rep_ids_sql = " and primary_sr_id in (#{sales_rep_ids.map{ |e| e.to_i}.join(",")})"
  company_id ? company_id_sql = "" : (company_id == 'CAN' ? company_id_sql = " and company_id = 2 " : company_id_sql = " and company_id = 1 ")

  revenue_sql = native_currency ? 'revenue_total' : 'revenue_total_consolidated'

  sql = <<-SQL
    with sales as (
        select
        max(to_char(gl_date,'YYYY')::int) as year,
        sum(case when to_char(gl_date,'YYYY')::int = #{end_date.year} then (#{revenue_sql}) else 0 end) as c_sales,
        sum(case when to_char(gl_date,'YYYY')::int = #{end_date.years_ago(1).year} then (#{revenue_sql}) else 0 end) as p_sales,
        sum(case when to_char(gl_date,'YYYY')::int = #{end_date.year} and company_id = 1 then (#{revenue_sql}) else 0 end) as c_usa_sales,
        sum(case when to_char(gl_date,'YYYY')::int = #{end_date.years_ago(1).year} and company_id = 1 then (#{revenue_sql}) else 0 end) as p_usa_sales,
        sum(case when to_char(gl_date,'YYYY')::int = #{end_date.year} and company_id = 2 then (#{revenue_sql}) else 0 end) as c_can_sales,
        sum(case when to_char(gl_date,'YYYY')::int = #{end_date.years_ago(1).year} and company_id = 2 then (#{revenue_sql}) else 0 end) as p_can_sales,
        sum(case when to_char(gl_date,'YYYY')::int = #{end_date.year} and company_id = 4 then (#{revenue_sql}) else 0 end) as c_nld_sales,
        sum(case when to_char(gl_date,'YYYY')::int = #{end_date.years_ago(1).year} and company_id = 4 then (#{revenue_sql}) else 0 end) as p_nld_sales
        from view_sales_facts
        where (gl_date between '#{start_date.years_ago(1)}' and '#{end_date.years_ago(1)}' or gl_date between '#{start_date}' and '#{end_date}')
        #{report_groupings_sql}
        #{sales_rep_ids_sql}
        #{company_id_sql}

    ), business_days as (
        select to_char(dt.date, 'YYYY')::int as year,count(distinct dt.date) as cal_bd,
        (count(distinct dt.date) - sum(case when hd.company_id = 1 then 1 else 0 end))::numeric as usa_bd,
        (count(distinct dt.date) - sum(case when hd.company_id = 2 then 1 else 0 end))::numeric as can_bd,
        (count(distinct dt.date) - sum(case when hd.company_id = 4 then 1 else 0 end))::numeric as nld_bd
        from analytic_date_time_dimensions dt
        left join company_holidays hd on dt.date = hd.holiday_date and hd.company_id <> 3
        where dt.date between '#{start_date}' and '#{end_date}'
        group by to_char(dt.date, 'YYYY')
    )
    select s.year,
          c_sales as all_sales,(c_sales - p_sales) as var_all_sales,case when p_sales = 0 then 0 else round((((c_sales - p_sales) / p_sales) * 100)::numeric,2) end as all_var_per,
          c_usa_sales as usa_sales,(c_usa_sales - p_usa_sales) as var_usa_sales,case when p_usa_sales = 0 then 0 else round((((c_usa_sales - p_usa_sales) / p_usa_sales) * 100)::numeric,2) end as usa_var_per,usa_bd,
          c_can_sales as can_sales,(c_can_sales - p_can_sales) as var_can_sales,case when p_can_sales = 0 then 0 else round((((c_can_sales - p_can_sales) / p_can_sales) * 100)::numeric,2) end as can_var_per,can_bd,
          c_nld_sales as nld_sales,(c_nld_sales - p_nld_sales) as var_nld_sales,case when p_nld_sales = 0 then 0 else round((((c_nld_sales - p_nld_sales) / p_nld_sales) * 100)::numeric,2) end as nld_var_per,nld_bd
    from sales s
    inner join business_days bd on s.year = bd.year
  SQL
  results = ActiveRecord::Base.connection.execute(sql).to_a.map{ |r| r.symbolize_keys }
end

.data_gross_sales(start_date, end_date, grouping, report_groupings, sales_rep_ids, company_id, native_currency) ⇒ Object



379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'app/services/report/gross_sales_report/gross_sales_report.rb', line 379

def self.data_gross_sales(start_date,end_date,grouping,report_groupings,sales_rep_ids,company_id,native_currency)
  report_groupings.empty? ? report_groupings_sql = "" : report_groupings_sql = " and vsf.report_grouping in (#{report_groupings.map{|rg| "'#{rg.sub("'","''")}'" }.join(',')})"
  (sales_rep_ids == nil || sales_rep_ids.empty?) ? sales_rep_ids_sql = "" : sales_rep_ids_sql = " and primary_sr_id in (#{sales_rep_ids.map{ |e| e.to_i}.join(",")})"
  company_id ? company_id_sql = " and vsf.company_id in (#{company_id.join(",")})" : company_id_sql = ""
  selects = ''; group_by = ''

  revenue_sql = native_currency ? 'revenue_total' : 'revenue_total_consolidated'

  if grouping == 'year'
    selects = " select 1 as ord_num, #{start_date.year} as date_1, "
    group_by = " group by vsf.company_id "
  elsif grouping == 'quarter'
    selects = " select dt.quarter as ord_num, case when dt.quarter = 1 then 'First' when dt.quarter = 2 then 'Second' when dt.quarter = 3 then 'Third' when dt.quarter = 4 then 'Fourth' else '' end as date_1, "
    group_by = " group by dt.quarter,vsf.company_id "
  elsif grouping == 'month'
    selects = " select dt.month as ord_num,case when dt.month = 1 then 'Jan' when dt.month = 2 then 'Feb' when dt.month = 3 then 'Mar' when dt.month = 4 then 'Apr' when dt.month = 5 then 'May' when dt.month = 6 then 'Jun' when dt.month = 7 then 'Jul' when dt.month = 8 then 'Aug' when dt.month = 9 then 'Sep' when dt.month = 10 then 'Oct' when dt.month = 11 then 'Nov' when dt.month = 12 then 'Dec' else '' end as date_1, "
    group_by = " group by dt.month,vsf.company_id "
  elsif grouping == 'week'
    selects = " select dt.week as ord_num, dt.week::text as date_1, "
    group_by = " group by dt.week,vsf.company_id "
  end

  sql = <<-SQL
    select
    ord_num,
    date_1 as date,
    sum(sales_1) as all_sales,
    sum(sales_usa_1) as usa_sales,
    sum(sales_can_1) as can_sales,
    sum(sales_nld_1) as nld_sales
    from (
        #{selects}
        sum(#{revenue_sql}) as sales_1,
        case when vsf.company_id = 1  then sum(#{revenue_sql}) else 0 end as sales_usa_1,
        case when vsf.company_id = 2 then sum(#{revenue_sql}) else 0 end as sales_can_1,
        case when vsf.company_id = 4 then sum(#{revenue_sql}) else 0 end as sales_nld_1
        from view_sales_facts vsf
        inner join analytic_date_time_dimensions dt on vsf.gl_date = dt.date
        left join parties e on vsf.primary_sr_id = e.id
        where gl_date between '#{start_date}' and '#{end_date}'
        #{report_groupings_sql}
        #{sales_rep_ids_sql}
        #{company_id_sql}
        #{group_by}
    )a
    group by ord_num,date_1
    order by ord_num;
  SQL

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

.data_over_time(report_groupings, sales_rep_ids, company_id, native_currency) ⇒ Object



431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# File 'app/services/report/gross_sales_report/gross_sales_report.rb', line 431

def self.data_over_time(report_groupings,sales_rep_ids,company_id,native_currency)
  report_groupings.empty? ? report_groupings_sql = "" : report_groupings_sql = " and report_grouping in (#{report_groupings.map{|rg| "'#{rg.sub("'","''")}'" }.join(',')})"
  (sales_rep_ids == nil || sales_rep_ids.empty?) ? sales_rep_ids_sql = "" : sales_rep_ids_sql = " and primary_sr_id in (#{sales_rep_ids.map{ |e| e.to_i}.join(",")})"
  company_id ? company_id_sql = " and view_sales_facts.company_id in (#{company_id.join(",")})" : company_id_sql = ""

  revenue_sql = native_currency ? 'revenue_total' : 'revenue_total_consolidated'

  sql = <<-SQL
    select
    ((extract ('epoch' from gl_date)) * 1000) as date,
    sum(#{revenue_sql}) as all_sales
    from view_sales_facts
    where to_char(gl_date,'YYYY')::int >= 2012
    #{report_groupings_sql}
    #{sales_rep_ids_sql}
    #{company_id_sql}
    group by gl_date
    order by 1
  SQL

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

.data_qvc(native_currency) ⇒ Object



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
# File 'app/services/report/gross_sales_report/gross_sales_report.rb', line 70

def self.data_qvc(native_currency)
  revenue_sql = native_currency ? 'revenue_total' : 'revenue_total_consolidated'
  sql = <<-SQL
    select
      c.country_iso3 as detail,
      round((sum(#{revenue_sql}) /(select sum(#{revenue_sql}) 
      from view_sales_facts v1 
      where to_char(v1.gl_date,'YYYY')::int = to_char(current_date,'YYYY')::int) * 100)::numeric, 1) as sales
      from view_sales_facts v2
      inner join companies c on c.id = v2.company_id
      where to_char(v2.gl_date,'YYYY')::int = to_char(current_date,'YYYY')::int
      group by c.country_iso3
      union all
      select 'Total', round((sum(#{revenue_sql}) / 1000000)::numeric, 2) as sales
      from view_sales_facts
      where to_char(gl_date,'YYYY')::int = to_char(current_date,'YYYY')::int
      union all
      select 'Budget', round(coalesce((abs(sum(case when year = to_char(current_date,'YYYY')::int then consolidated_amount else 0 end) * -1) / 1000000),(abs(sum(case when year = (to_char(current_date,'YYYY')::int - 1) then consolidated_amount else 0 end) * -1) / 1000000))::numeric, 2) as sales
      from budgets
      where description = 'Product Sales'
      and year in (to_char(current_date,'YYYY')::int, (to_char(current_date,'YYYY')::int - 1));
  SQL

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

.data_qvs(native_currency) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'app/services/report/gross_sales_report/gross_sales_report.rb', line 128

def self.data_qvs(native_currency)
  end_date = Date.yesterday
  start_date = end_date.beginning_of_year
  past_end_date = end_date.years_ago(1)
  past_start_date = start_date.years_ago(1)
  q_start_date = end_date.beginning_of_quarter
  q_past_start_date = q_start_date.years_ago(1)
  m_start_date = end_date.beginning_of_month
  m_past_start_date = m_start_date.years_ago(1)
  curr_year = end_date.year.to_i
  past_year = curr_year - 1
  w = end_date.cweek
  w_start_date = start_date.beginning_of_week
  w_past_start_date = past_start_date.beginning_of_week
  w_past_end_date = w_past_start_date + end_date.wday

  end_date.quarter_index == 1 ? q = '1st' : (end_date.quarter_index == 2 ? q = '2nd' : (end_date.quarter_index == 3 ? q = '3rd' : q = '4th'))
  (end_date.wday.zero? || end_date.wday == 1) ? d = 'Last Friday' : d = 'Yesterday'

  revenue_sql = native_currency ? 'revenue_total' : 'revenue_total_consolidated'

  sql = <<-SQL
    select date_1 as dates,sum(sales_1) as sales,
    case when sum(sales_2) = 0 then 0 else round(((((sum(sales_1) - sum(sales_2)) / sum(sales_2))) * 100)::numeric, 2) end as var,
    sum(sales_usa_1) as sales_usa,
    case when sum(sales_usa_2) = 0 then 0 else round(((((sum(sales_usa_1) - sum(sales_usa_2)) / sum(sales_usa_2))) * 100)::numeric, 2) end as var_usa,
    sum(sales_can_1) as sales_can,
    case when sum(sales_can_2) = 0 then 0 else round(((((sum(sales_can_1) - sum(sales_can_2)) / sum(sales_can_2))) * 100)::numeric, 2) end as var_can,
    sum(sales_nld_1) as sales_nld,
    case when sum(sales_nld_2) = 0 then 0 else round(((((sum(sales_nld_1) - sum(sales_nld_2)) / sum(sales_nld_2))) * 100)::numeric, 2) end as var_nld
    from(
        select
        1 as ord_num,
        '#{d}' as date_1,
        case when to_char(gl_date,'YYYY')::int = #{curr_year} then sum(#{revenue_sql}) else 0 end as  sales_1,
        case when to_char(gl_date,'YYYY')::int = #{curr_year} and company_id = 1 then sum(#{revenue_sql}) else 0 end as  sales_usa_1,
        case when to_char(gl_date,'YYYY')::int = #{curr_year} and company_id = 2 then sum(#{revenue_sql}) else 0 end as  sales_can_1,
        case when to_char(gl_date,'YYYY')::int = #{curr_year} and company_id = 4 then sum(#{revenue_sql}) else 0 end as  sales_nld_1,
        case when to_char(gl_date,'YYYY')::int = #{past_year} then 'Yesterday_last_year' else 'Yesterday_last_year' end as date_2,
        case when to_char(gl_date,'YYYY')::int = #{past_year} then sum(#{revenue_sql}) else 0 end as  sales_2,
        case when to_char(gl_date,'YYYY')::int = #{past_year} and company_id = 1 then sum(#{revenue_sql}) else 0 end as  sales_usa_2,
        case when to_char(gl_date,'YYYY')::int = #{past_year} and company_id = 2 then sum(#{revenue_sql}) else 0 end as  sales_can_2,
        case when to_char(gl_date,'YYYY')::int = #{past_year} and company_id = 4 then sum(#{revenue_sql}) else 0 end as  sales_nld_2
        from view_sales_facts
        where (gl_date = '#{w_past_end_date}' or gl_date = '#{end_date}')
        group by to_char(gl_date,'YYYY')::int,company_id
        union all
        select
        2 as order_c,
        'W - #{w}' as date_1,
        case when to_char(gl_date,'YYYY')::int = #{curr_year} then sum(#{revenue_sql}) else 0 end as  sales_1,
        case when to_char(gl_date,'YYYY')::int = #{curr_year} and company_id = 1 then sum(#{revenue_sql}) else 0 end as  sales_usa_1,
        case when to_char(gl_date,'YYYY')::int = #{curr_year} and company_id = 2 then sum(#{revenue_sql}) else 0 end as  sales_can_1,
        case when to_char(gl_date,'YYYY')::int = #{curr_year} and company_id = 4 then sum(#{revenue_sql}) else 0 end as  sales_nld_1,
        case when to_char(gl_date,'YYYY')::int = #{past_year} then 'week_last_year' else 'week_last_year' end as date_2,
        case when to_char(gl_date,'YYYY')::int = #{past_year} then sum(#{revenue_sql}) else 0 end as  sales_2,
        case when to_char(gl_date,'YYYY')::int = #{past_year} and company_id = 1 then sum(#{revenue_sql}) else 0 end as  sales_usa_2,
        case when to_char(gl_date,'YYYY')::int = #{past_year} and company_id = 2 then sum(#{revenue_sql}) else 0 end as  sales_can_2,
        case when to_char(gl_date,'YYYY')::int = #{past_year} and company_id = 4 then sum(#{revenue_sql}) else 0 end as  sales_nld_2
        from view_sales_facts
        where (gl_date between '#{w_past_start_date}' and '#{w_past_end_date}' or gl_date between '#{w_start_date}' and '#{end_date}')
        group by to_char(gl_date,'YYYY')::int,company_id
        union all
        select
        3 as ord_num,
        'M - #{end_date.strftime('%b')}' as date_1,
        case when to_char(gl_date,'YYYY')::int = #{curr_year} then sum(#{revenue_sql}) else 0 end as  sales_1,
        case when to_char(gl_date,'YYYY')::int = #{curr_year} and company_id = 1 then sum(#{revenue_sql}) else 0 end as  sales_usa_1,
        case when to_char(gl_date,'YYYY')::int = #{curr_year} and company_id = 2 then sum(#{revenue_sql}) else 0 end as  sales_can_1,
        case when to_char(gl_date,'YYYY')::int = #{curr_year} and company_id = 4 then sum(#{revenue_sql}) else 0 end as  sales_nld_1,
        case when to_char(gl_date,'YYYY')::int = #{past_year} then 'month_last_year' else 'month_last_year' end as date_2,
        case when to_char(gl_date,'YYYY')::int = #{past_year} then sum(quantity * consolidated_exchange_rate * discounted_price) else 0 end as  sales_2,
        case when to_char(gl_date,'YYYY')::int = #{past_year} and company_id = 1 then sum(#{revenue_sql}) else 0 end as  sales_usa_2,
        case when to_char(gl_date,'YYYY')::int = #{past_year} and company_id = 2 then sum(#{revenue_sql}) else 0 end as  sales_can_2,
        case when to_char(gl_date,'YYYY')::int = #{past_year} and company_id = 4 then sum(#{revenue_sql}) else 0 end as  sales_nld_2
        from view_sales_facts
        where (gl_date between '#{m_past_start_date}' and '#{past_end_date}' or gl_date between '#{m_start_date}' and '#{end_date}')
        group by to_char(gl_date,'YYYY')::int,company_id
        union all
        select
        4 as order_c,
        'Q - #{q}' as date_1,
        case when to_char(gl_date,'YYYY')::int = #{curr_year} then sum(#{revenue_sql}) else 0 end as  sales_1,
        case when to_char(gl_date,'YYYY')::int = #{curr_year} and company_id = 1 then sum(#{revenue_sql}) else 0 end as  sales_usa_1,
        case when to_char(gl_date,'YYYY')::int = #{curr_year} and company_id = 2 then sum(#{revenue_sql}) else 0 end as  sales_can_1,
        case when to_char(gl_date,'YYYY')::int = #{curr_year} and company_id = 4 then sum(#{revenue_sql}) else 0 end as  sales_nld_1,
        case when to_char(gl_date,'YYYY')::int = #{past_year} then 'quarter_last_year' else 'quarter_last_year' end as date_2,
        case when to_char(gl_date,'YYYY')::int = #{past_year} then sum(#{revenue_sql}) else 0 end as  sales_2,
        case when to_char(gl_date,'YYYY')::int = #{past_year} and company_id = 1 then sum(#{revenue_sql}) else 0 end as  sales_usa_2,
        case when to_char(gl_date,'YYYY')::int = #{past_year} and company_id = 2 then sum(#{revenue_sql}) else 0 end as  sales_can_2,
        case when to_char(gl_date,'YYYY')::int = #{past_year} and company_id = 4 then sum(#{revenue_sql}) else 0 end as  sales_nld_2
        from view_sales_facts
        where (gl_date between '#{q_past_start_date}' and '#{past_end_date}' or gl_date between '#{q_start_date}' and '#{end_date}')
        group by to_char(gl_date,'YYYY')::int,company_id
        union all
        select
        5 as ord_num,
        'Y - #{curr_year}' as date_1,
        case when to_char(gl_date,'YYYY')::int = #{curr_year} then sum(#{revenue_sql}) else 0 end as  sales_1,
        case when to_char(gl_date,'YYYY')::int = #{curr_year} and company_id = 1 then sum(#{revenue_sql}) else 0 end as  sales_usa_1,
        case when to_char(gl_date,'YYYY')::int = #{curr_year} and company_id = 2 then sum(#{revenue_sql}) else 0 end as  sales_can_1,
        case when to_char(gl_date,'YYYY')::int = #{curr_year} and company_id = 4 then sum(#{revenue_sql}) else 0 end as  sales_nld_1,
        case when to_char(gl_date,'YYYY')::int = #{past_year} then 'last_year' else 'last_year' end as date_2,
        case when to_char(gl_date,'YYYY')::int = #{past_year} then sum(#{revenue_sql}) else 0 end as  sales_2,
        case when to_char(gl_date,'YYYY')::int = #{past_year} and company_id = 1 then sum(#{revenue_sql}) else 0 end as  sales_usa_2,
        case when to_char(gl_date,'YYYY')::int = #{past_year} and company_id = 2 then sum(#{revenue_sql}) else 0 end as  sales_can_2,
        case when to_char(gl_date,'YYYY')::int = #{past_year} and company_id = 4 then sum(#{revenue_sql}) else 0 end as  sales_nld_2
        from view_sales_facts
        where (gl_date between '#{past_start_date}' and '#{past_end_date}' or gl_date between '#{start_date}' and '#{end_date}')
        group by to_char(gl_date,'YYYY')::int,company_id
    )
    group by date_1,ord_num
    order by ord_num;
  SQL

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

.data_qvt(native_currency) ⇒ Object



96
97
98
99
100
101
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
# File 'app/services/report/gross_sales_report/gross_sales_report.rb', line 96

def self.data_qvt(native_currency)
  end_date = Date.current.days_ago(1)
  start_date = end_date.beginning_of_year
  curr_year = end_date.year.to_i
  past_year = curr_year - 1
  revenue_sql = native_currency ? 'revenue_total' : 'revenue_total_consolidated'
  sql = <<-SQL
    select
    sales_rep,
    sum(sales1) as sales,
    case when sum(sales2) = 0 then 0 else round((((sum(sales1) - sum(sales2)) / sum(sales2)) * 100)::numeric,1) end as var
    from(
        select
        case when full_name is null then 'Unassigned' else full_name end as sales_rep,
        case when to_char(gl_date,'YYYY')::int = #{curr_year} then sum(#{revenue_sql}) else 0 end as sales1,
        case when to_char(gl_date,'YYYY')::int = #{past_year} then sum(#{revenue_sql}) else 0 end as sales2
        from view_sales_facts vsf
        left join parties e on vsf.primary_sr_id = e.id
        where ((gl_date between '#{start_date.years_ago(1)}' and '#{end_date.years_ago(1)}') or (gl_date between '#{start_date}' and '#{end_date}'))
        and primary_sr_id not in (155,85,92)
        and vsf.report_grouping not in ('Amazon','Costco','Direct Buy','E-Tailers','Home Depot','Lowes','Lowes Canada','Wal-Mart')
        group by full_name,to_char(gl_date,'YYYY')::int
    )a
    where sales_rep <> 'Unassigned'
    group by sales_rep
    order by 2 desc
    limit 5;
  SQL

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

.data_sales_rep(start_date, end_date, report_groupings, sales_rep_ids, company_id, native_currency) ⇒ Object



454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
# File 'app/services/report/gross_sales_report/gross_sales_report.rb', line 454

def self.data_sales_rep(start_date,end_date,report_groupings,sales_rep_ids,company_id,native_currency)
  report_groupings.empty? ? report_groupings_sql = "" : report_groupings_sql = " and vsf.report_grouping in (#{report_groupings.map{|rg| "'#{rg.sub("'","''")}'" }.join(',')})"
  (sales_rep_ids == nil || sales_rep_ids.empty?) ? sales_rep_ids_sql = " and primary_sr_id not in (85,155,92)" : sales_rep_ids_sql = " and primary_sr_id in (#{sales_rep_ids.map{ |e| e.to_i}.join(",")})"
  company_id ? company_id_sql = " and vsf.company_id in (#{company_id.join(",")})" : company_id_sql = ""

  revenue_sql = native_currency ? 'revenue_total' : 'revenue_total_consolidated'

  sql = <<-SQL
    select
    e.full_name as sales_rep,
    sum(case when to_char(gl_date,'YYYY')::int = #{end_date.year} and to_char(gl_date,'MM')::int = 1 then (#{revenue_sql}) else 0 end) as january,
    sum(case when to_char(gl_date,'YYYY')::int = #{end_date.year} and to_char(gl_date,'MM')::int = 2 then (#{revenue_sql}) else 0 end) as february,
    sum(case when to_char(gl_date,'YYYY')::int = #{end_date.year} and to_char(gl_date,'MM')::int = 3 then (#{revenue_sql}) else 0 end) as march,
    sum(case when to_char(gl_date,'YYYY')::int = #{end_date.year} and to_char(gl_date,'MM')::int = 4 then (#{revenue_sql}) else 0 end) as april,
    sum(case when to_char(gl_date,'YYYY')::int = #{end_date.year} and to_char(gl_date,'MM')::int = 5 then (#{revenue_sql}) else 0 end) as may,
    sum(case when to_char(gl_date,'YYYY')::int = #{end_date.year} and to_char(gl_date,'MM')::int = 6 then (#{revenue_sql}) else 0 end) as june,
    sum(case when to_char(gl_date,'YYYY')::int = #{end_date.year} and to_char(gl_date,'MM')::int = 7 then (#{revenue_sql}) else 0 end) as july,
    sum(case when to_char(gl_date,'YYYY')::int = #{end_date.year} and to_char(gl_date,'MM')::int = 8 then (#{revenue_sql}) else 0 end) as august,
    sum(case when to_char(gl_date,'YYYY')::int = #{end_date.year} and to_char(gl_date,'MM')::int = 9 then (#{revenue_sql}) else 0 end) as september,
    sum(case when to_char(gl_date,'YYYY')::int = #{end_date.year} and to_char(gl_date,'MM')::int = 10 then (#{revenue_sql}) else 0 end) as october,
    sum(case when to_char(gl_date,'YYYY')::int = #{end_date.year} and to_char(gl_date,'MM')::int = 11 then (#{revenue_sql}) else 0 end) as november,
    sum(case when to_char(gl_date,'YYYY')::int = #{end_date.year} and to_char(gl_date,'MM')::int = 12 then (#{revenue_sql}) else 0 end) as december,
    sum(case when to_char(gl_date,'YYYY')::int = #{end_date.year} then (#{revenue_sql}) else 0 end) as total_year,
    sum(case when to_char(gl_date,'YYYY')::int = #{end_date.year - 1} then (#{revenue_sql}) else 0 end) as last_year
    from view_sales_facts vsf
    inner join parties e on vsf.primary_sr_id = e.id and e.type = 'Employee' and inactive = false
    inner join employee_records er on vsf.primary_sr_id = er.party_id and er.department in ('Customer Service', 'Sales')
    where (gl_date between '#{start_date.years_ago(1)}' and '#{end_date.years_ago(1)}' or gl_date between '#{start_date}' and '#{end_date}')
    #{report_groupings_sql}
    #{sales_rep_ids_sql}
    #{company_id_sql}
    group by e.full_name
    order by 14 desc;
  SQL

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

.data_trend(start_date, end_date, grouping, report_groupings, sales_rep_ids, company_id, native_currency) ⇒ Object



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'app/services/report/gross_sales_report/gross_sales_report.rb', line 340

def self.data_trend(start_date,end_date,grouping,report_groupings,sales_rep_ids,company_id,native_currency)
  report_groupings.empty? ? report_groupings_sql = "" : report_groupings_sql = " and vsf.report_grouping in (#{report_groupings.map{|rg| "'#{rg.sub("'","''")}'" }.join(',')})"
  (sales_rep_ids == nil || sales_rep_ids.empty?) ? sales_rep_ids_sql = "" : sales_rep_ids_sql = " and primary_sr_id in (#{sales_rep_ids.map{ |e| e.to_i}.join(",")})"
  company_id ? company_id_sql = " and vsf.company_id in (#{company_id.join(",")})" : company_id_sql = ""
  selects = ''; group_by = ''

  revenue_sql = native_currency ? 'revenue_total' : 'revenue_total_consolidated'

  if grouping == 'year'
    selects = " 1 as ord_num, max(dt.year) as date, "
    group_by = " order by 1"
  elsif grouping == 'quarter'
    selects = " dt.quarter as ord_num, case when dt.quarter = 1 then 'First' when dt.quarter = 2 then 'Second' when dt.quarter = 3 then 'Third' when dt.quarter = 4 then 'Fourth' else '' end as date, "
    group_by = " group by dt.quarter order by dt.quarter"
  elsif grouping == 'month'
    selects = " dt.month as ord_num,case when dt.month = 1 then 'Jan' when dt.month = 2 then 'Feb' when dt.month = 3 then 'Mar' when dt.month = 4 then 'Apr' when dt.month = 5 then 'May' when dt.month = 6 then 'Jun' when dt.month = 7 then 'Jul' when dt.month = 8 then 'Aug' when dt.month = 9 then 'Sep' when dt.month = 10 then 'Oct' when dt.month = 11 then 'Nov' when dt.month = 12 then 'Dec' else '' end as date, "
    group_by = " group by dt.month order by dt.month "
  elsif grouping == 'week'
    selects = " dt.week as ord_num, dt.week as date, "
    group_by = " group by dt.week order by dt.week "
  end

  sql = <<-SQL
    select
    #{selects}
    sum(case when to_char(gl_date,'YYYY')::int = #{start_date.years_ago(1).year} then (#{revenue_sql}) else 0 end) as year1,
    sum(case when to_char(gl_date,'YYYY')::int = #{end_date.year} then (#{revenue_sql}) else 0 end) as year2
    from view_sales_facts vsf
    inner join analytic_date_time_dimensions dt on vsf.gl_date = dt.date
    where (gl_date between '#{start_date.years_ago(1)}' and '#{start_date.years_ago(1).end_of_year}' or gl_date between '#{start_date}' and '#{end_date}')
    #{report_groupings_sql}
    #{sales_rep_ids_sql}
    #{company_id_sql}
    #{group_by}
  SQL

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

.total_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
39
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
# File 'app/services/report/gross_sales_report/gross_sales_report.rb', line 8

def self.total_report(options = {})
  start_date = options[:period1_gteq]
  end_date = options[:period1_lteq]
  grouping = options[:grouping]
  native_currency = options[:native_currency]
  report_groupings = options[:report_groupings].map(&:presence).compact
  sales_rep_ids = options[:primary_sales_rep_ids].map(&:presence).compact
  company_id = options[:company_ids].blank? ? Company.sales_companies.all.map{|c|c.id} : options[:company_ids].map(&:presence)
  (options[:company_ids] == "" || options[:company_ids] == nil) ? company = '' : company = options[:company_ids]
  company = company_id.map{|id| Company.find(id).company_label[0,3] }
  company_string = company.join('')
  currency = native_currency ? Company.find(options[:company_ids]).map(&:currency).first : "USD"
  currency_symbol = Money::Currency.new(currency).symbol
  ask_year = options[:period1_lteq].year.to_i
  past_year = options[:period1_lteq].year.to_i - 1
  analysis_periods = [6,5,4,3,2,1,0]

  qv_chart = data_qvc(native_currency) || []
  qv_top_5 = data_qvt(native_currency) || []
  qv_sales = data_qvs(native_currency) || []
  gs_gen_info = []
  analysis_periods.each do |p|
    gs_gen_info << data_gen_info(start_date.years_ago(p),end_date.years_ago(p),grouping,report_groupings,sales_rep_ids,company_id,native_currency)[0]
  end
  gs_gen_info.compact!
  gs_comparison_year = data_comparison_year(start_date,end_date,grouping,report_groupings,sales_rep_ids,company_id,native_currency)
  gs_normal_behavior = data_trend(start_date,end_date,grouping,report_groupings,sales_rep_ids,company_id,native_currency)
  row_num = []; gs_accumulated_behavior = []
  gs_normal_behavior.map{ |r| row_num << r[:ord_num].to_i }
  row_num.each do |n|
    date = ''; year1 = 0; year2 = 0
    gs_normal_behavior.each do |r|
      date = r[:date] if r[:ord_num].to_i == n
      year1 += r[:year1].to_f if r[:ord_num].to_i <= n
      year2 += r[:year2].to_f if r[:ord_num].to_i <= n
    end
    gs_accumulated_behavior << { ord_num: n, date: date, year1: year1, year2: year2 }
  end
  gs_data_usa_can = data_gross_sales(start_date,end_date,grouping,report_groupings,sales_rep_ids,company_id,native_currency)
  gs_over_time = data_over_time(report_groupings,sales_rep_ids,company_id,native_currency)
  gs_data_sales_rep = data_sales_rep(start_date,end_date,report_groupings,sales_rep_ids,company_id,native_currency)
  grouping == 'week' ? gs_period = end_date.cweek : (grouping == 'month' ? gs_period = end_date.month : (grouping == 'quarter' ? gs_period = end_date.quarter_index : gs_period = 1))

  Result.new(success: true, qvc_data: qv_chart,
                            qvt_data: qv_top_5,
                            qvs_data: qv_sales,
                            data: gs_gen_info,
                            data_comparison: gs_comparison_year,
                            data_nb: gs_normal_behavior,
                            data_ab: gs_accumulated_behavior,
                            data_usa_can: gs_data_usa_can,
                            data_over_time: gs_over_time,
                            data_wh: gs_data_sales_rep,
                            year: ask_year, past_year: past_year,
                            grouping: grouping, period: gs_period,
                            company: company,
                            currency: currency,
                            currency_symbol: currency_symbol,
                            company_string: company_string,
                            native_currency: native_currency)
end