Class: Report::GrossSalesReport::GrossSalesReport

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

Overview

Service object: gross sales report.

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

Data comparison year.

Parameters:

  • start_date (Date)
  • end_date (Date)
  • grouping (Object)
  • report_groupings (Object)
  • sales_rep_ids (Array<Integer>)
  • company_id (Integer)
  • native_currency (Object)

Returns:

  • (Object)


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
430
431
432
433
434
435
436
437
438
# File 'app/services/report/gross_sales_report/gross_sales_report.rb', line 390

def self.data_comparison_year(start_date, end_date, grouping, report_groupings, sales_rep_ids, company_id, native_currency)
  report_groupings_sql = report_groupings.empty? ? '' : " and vsf.report_grouping in (#{report_groupings.map { |rg| ActiveRecord::Base.lease_connection.quote(rg) }.join(',')})"
  sales_rep_ids_sql = sales_rep_ids.blank? ? "" : " and primary_sr_id in (#{sales_rep_ids.map(&:to_i).join(',')})"
  company_id_sql = company_id ? " and vsf.company_id in (#{company_id.join(',')})" : ""
  selects = ''
  group_by = ''

  revenue_sql = native_currency ? 'revenue_total' : 'revenue_total_consolidated'

  case grouping
  when 'year'
    selects = " select 1 as ord_num, #{start_date.year} as date, "
    group_by = " group by to_char(gl_date,'YYYY')::int "
  when '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 "
  when '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 "
  when '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.squish
    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

  ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys)
end

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

Data gen info.

Parameters:

  • start_date (Date)
  • end_date (Date)
  • _grouping (Object)
  • report_groupings (Object)
  • sales_rep_ids (Array<Integer>)
  • company_id (Integer)
  • native_currency (Object)

Returns:

  • (Object)


331
332
333
334
335
336
337
338
339
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
378
379
# File 'app/services/report/gross_sales_report/gross_sales_report.rb', line 331

def self.data_gen_info(start_date, end_date, _grouping, report_groupings, sales_rep_ids, company_id, native_currency)
  report_groupings_sql = report_groupings.empty? ? '' : " and report_grouping in (#{report_groupings.map { |rg| ActiveRecord::Base.lease_connection.quote(rg) }.join(',')})"
  sales_rep_ids_sql = sales_rep_ids.blank? ? '' : " and primary_sr_id in (#{sales_rep_ids.map { |id| Integer(id) }.join(',')})"
  if company_id
    company_id_sql = ""
  else
    (company_id_sql = company_id == 'CAN' ? " and company_id = 2 " : " and company_id = 1 ")
  end

  revenue_sql = native_currency ? 'revenue_total' : 'revenue_total_consolidated'

  sql = <<-SQL.squish
    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
  ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys)
end

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

Data gross sales.

Parameters:

  • start_date (Date)
  • end_date (Date)
  • grouping (Object)
  • report_groupings (Object)
  • sales_rep_ids (Array<Integer>)
  • company_id (Integer)
  • native_currency (Object)

Returns:

  • (Object)


499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
# File 'app/services/report/gross_sales_report/gross_sales_report.rb', line 499

def self.data_gross_sales(start_date, end_date, grouping, report_groupings, sales_rep_ids, company_id, native_currency)
  report_groupings_sql = report_groupings.empty? ? '' : " and vsf.report_grouping in (#{report_groupings.map { |rg| ActiveRecord::Base.lease_connection.quote(rg) }.join(',')})"
  sales_rep_ids_sql = sales_rep_ids.blank? ? "" : " and primary_sr_id in (#{sales_rep_ids.map(&:to_i).join(',')})"
  company_id_sql = company_id ? " and vsf.company_id in (#{company_id.join(',')})" : ""
  selects = ''
  group_by = ''

  revenue_sql = native_currency ? 'revenue_total' : 'revenue_total_consolidated'

  case grouping
  when 'year'
    selects = " select 1 as ord_num, #{start_date.year} as date_1, "
    group_by = " group by vsf.company_id "
  when '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 "
  when '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 "
  when '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.squish
    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

  ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys)
end

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

Data over time.

Parameters:

  • report_groupings (Object)
  • sales_rep_ids (Array<Integer>)
  • company_id (Integer)
  • native_currency (Object)

Returns:

  • (Object)


559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
# File 'app/services/report/gross_sales_report/gross_sales_report.rb', line 559

def self.data_over_time(report_groupings, sales_rep_ids, company_id, native_currency)
  report_groupings_sql = report_groupings.empty? ? '' : " and report_grouping in (#{report_groupings.map { |rg| ActiveRecord::Base.lease_connection.quote(rg) }.join(',')})"
  sales_rep_ids_sql = sales_rep_ids.blank? ? '' : " and primary_sr_id in (#{sales_rep_ids.map { |id| Integer(id) }.join(',')})"
  company_id_sql = company_id ? " and view_sales_facts.company_id in (#{company_id.join(',')})" : ""

  revenue_sql = native_currency ? 'revenue_total' : 'revenue_total_consolidated'

  sql = <<-SQL.squish
    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

  ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys)
end

.data_qvc(native_currency) ⇒ Object

Data qvc.

Parameters:

  • native_currency (Object)

Returns:

  • (Object)


126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'app/services/report/gross_sales_report/gross_sales_report.rb', line 126

def self.data_qvc(native_currency)
  revenue_sql = native_currency ? 'revenue_total' : 'revenue_total_consolidated'
  sql = <<-SQL.squish
    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

  ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys)
end

.data_qvs(native_currency) ⇒ Object

Data qvs.

Parameters:

  • native_currency (Object)

Returns:

  • (Object)


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

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
  # Anchor the week window to the CURRENT week (end_date), not the year start —
  # start_date.beginning_of_week is the week containing Jan 1, which made the
  # "W - NN" row span the whole year (equal to the "Y - YYYY" row).
  w_start_date = end_date.beginning_of_week
  # Same week one year ago, ending on the equivalent day (past_end_date =
  # end_date.years_ago(1)); w_past_end_date also serves as "yesterday last year"
  # for the day-over-day (D) row's variance.
  w_past_end_date = past_end_date
  w_past_start_date = past_end_date.beginning_of_week

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

  revenue_sql = native_currency ? 'revenue_total' : 'revenue_total_consolidated'

  sql = <<-SQL.squish
    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(#{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 '#{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

  ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys)
end

.data_qvt(native_currency) ⇒ Object

Data qvt.

Parameters:

  • native_currency (Object)

Returns:

  • (Object)


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

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.squish
    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 (#{Customer::ReportGrouping.etailer_groupings_sql_list})
        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

  ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys)
end

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

Data sales rep.

Parameters:

  • start_date (Date)
  • end_date (Date)
  • report_groupings (Object)
  • sales_rep_ids (Array<Integer>)
  • company_id (Integer)
  • native_currency (Object)

Returns:

  • (Object)


590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
# File 'app/services/report/gross_sales_report/gross_sales_report.rb', line 590

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

  revenue_sql = native_currency ? 'revenue_total' : 'revenue_total_consolidated'

  sql = <<-SQL.squish
    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

  ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys)
end

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

Data trend.

Parameters:

  • start_date (Date)
  • end_date (Date)
  • grouping (Object)
  • report_groupings (Object)
  • sales_rep_ids (Array<Integer>)
  • company_id (Integer)
  • native_currency (Object)

Returns:

  • (Object)


449
450
451
452
453
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
# File 'app/services/report/gross_sales_report/gross_sales_report.rb', line 449

def self.data_trend(start_date, end_date, grouping, report_groupings, sales_rep_ids, company_id, native_currency)
  report_groupings_sql = report_groupings.empty? ? '' : " and vsf.report_grouping in (#{report_groupings.map { |rg| ActiveRecord::Base.lease_connection.quote(rg) }.join(',')})"
  sales_rep_ids_sql = sales_rep_ids.blank? ? "" : " and primary_sr_id in (#{sales_rep_ids.map(&:to_i).join(',')})"
  company_id_sql = company_id ? " and vsf.company_id in (#{company_id.join(',')})" : ""
  selects = ''
  group_by = ''

  revenue_sql = native_currency ? 'revenue_total' : 'revenue_total_consolidated'

  case grouping
  when 'year'
    selects = " 1 as ord_num, max(dt.year) as date, "
    group_by = " order by 1"
  when '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"
  when '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 "
  when 'week'
    selects = " dt.week as ord_num, dt.week as date, "
    group_by = " group by dt.week order by dt.week "
  end

  sql = <<-SQL.squish
    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

  ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys)
end

.total_report(options = {}) ⇒ Result

Builds the gross sales report result.

Parameters:

  • options (Hash) (defaults to: {})

    configurable options

Options Hash (options):

  • period1_gteq (Date)

    start of the report period

  • period1_lteq (Date)

    end of the report period

  • grouping (String)

    period grouping ('year', 'quarter', 'month', 'week')

  • native_currency (Boolean)

    use native currency instead of consolidated USD

  • report_groupings (Array<String>)

    report groupings to include

  • primary_sales_rep_ids (Array<Integer>)

    sales rep IDs to filter on

  • company_ids (Array<Integer>)

    company IDs to filter on (all sales companies when blank)

Returns:

  • (Result)

    the report result



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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/services/report/gross_sales_report/gross_sales_report.rb', line 49

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].filter_map(&:presence)
  # Coerce IDs through Integer() so any non-numeric input raises before
  # reaching SQL. Downstream interpolation sites assume an int array.
  sales_rep_ids = options[:primary_sales_rep_ids].filter_map(&:presence).map { |id| Integer(id) }
  company_id = options[:company_ids].blank? ? Company.sales_companies.pluck(:id) : options[:company_ids].filter_map(&:presence).map { |id| Integer(id) }
  ["", nil].include?(options[:company_ids]) ? '' : 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.map do |p|
    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)
  if grouping == 'week'
    gs_period = end_date.cweek
  else
    (if grouping == 'month'
       gs_period = end_date.month
     else
       (gs_period = grouping == 'quarter' ? end_date.quarter_index : 1)
     end)
  end

  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