Class: Report::RmasReport::RmasReport

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

Defined Under Namespace

Classes: Result

Class Method Summary collapse

Class Method Details

.email_report_channel_returns(start_date, end_date) ⇒ Object



887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
# File 'app/services/report/rmas_report/rmas_report.rb', line 887

def self.email_report_channel_returns(start_date,end_date)
  sql = <<-SQL
    with sales as (
        select report_grouping,count(distinct order_id) as num_orders,sum(quantity * (discounted_price - amazon_vc) * exchange_rate) as sales_amount
        from view_sales vsf
        left join product_lines pl on vsf.primary_product_line_id = pl.id
        where invoice_type in ('SO','CI')
        and trim(split_part(pl.lineage_expanded,'>',1)) <> 'Rental Tools'
        and gl_date between '#{start_date}' and '#{end_date}'
        group by report_grouping
    ), returns as (
        select report_grouping,count(distinct rma_id) as num_rmas,sum(quantity * discounted_price * consolidated_exchange_rate) as returned_amount
        from view_rmas_facts vrf
        left join product_lines pl on vrf.primary_product_line_id = pl.id
        where return_date between '#{start_date}' and '#{end_date}'
        and trim(split_part(pl.lineage_expanded,'>',1)) <> 'Rental Tools'
        group by report_grouping
    )
    select r.report_grouping,num_rmas,returned_amount,
          case when sales_amount = 0 then 0 else (returned_amount / sales_amount) * 100 end as ptg_in_sales,
          coalesce(num_orders,0) as num_orders,coalesce(sales_amount,0) as sales_amount
    from returns r
    left join sales s on r.report_grouping = s.report_grouping
    order by 3 desc
  SQL

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

.email_report_rma_codes_used(start_date, end_date) ⇒ Object



916
917
918
919
920
921
922
923
924
925
926
927
928
# File 'app/services/report/rmas_report/rmas_report.rb', line 916

def self.email_report_rma_codes_used(start_date,end_date)
  sql = <<-SQL
    select returned_reason,returned_description,count(distinct rma_id) as num_rmas,sum(quantity * discounted_price * consolidated_exchange_rate) as returned_amount
    from view_rmas_facts vrf
    left join product_lines pl on vrf.primary_product_line_id = pl.id
    where return_date between '#{start_date}' and '#{end_date}'
    and trim(split_part(pl.lineage_expanded,'>',1)) <> 'Rental Tools'
    group by returned_reason,returned_description
    order by 4 desc limit 10
  SQL

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

.email_report_rmas_by_state(start_date, end_date) ⇒ Object



873
874
875
876
877
878
879
880
881
882
883
884
885
# File 'app/services/report/rmas_report/rmas_report.rb', line 873

def self.email_report_rmas_by_state(start_date,end_date)

  sql = <<-SQL
    select rma_state,count(distinct rma_id) as num_rmas,sum(quantity * discounted_price * consolidated_exchange_rate) as returned_amount
    from view_rmas_facts vrf
    left join product_lines pl on vrf.primary_product_line_id = pl.id
    where (return_date between '#{start_date}' and '#{end_date}' or rma_created_date between '#{start_date}' and '#{end_date}')
    and trim(split_part(pl.lineage_expanded,'>',1)) <> 'Rental Tools'
    group by rma_state
  SQL

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

.email_report_sales_vs_returns(start_date, end_date) ⇒ Object



817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
# File 'app/services/report/rmas_report/rmas_report.rb', line 817

def self.email_report_sales_vs_returns(start_date,end_date)
  sql = <<-SQL
    with sales as (
        select count(distinct order_id) as num_orders,sum(quantity * (discounted_price - amazon_vc) * exchange_rate) as sales_amount
        from view_sales vsf
        left join product_lines pl on vsf.primary_product_line_id = pl.id
        where invoice_type in ('SO','CI')
        and trim(split_part(pl.lineage_expanded,'>',1)) <> 'Rental Tools'
        and gl_date between '#{start_date}' and '#{end_date}'
    ), returns as (
        select count(distinct rma_id) as num_rmas,sum(quantity * discounted_price * consolidated_exchange_rate) as returned_amount
        from view_rmas_facts vrf
        left join product_lines pl on vrf.primary_product_line_id = pl.id
        where return_date between '#{start_date}' and '#{end_date}'
        and trim(split_part(pl.lineage_expanded,'>',1)) <> 'Rental Tools'
    )
    select num_rmas,returned_amount,
          case when sales_amount = 0 then 0 else (returned_amount / sales_amount) * 100 end as ptg_in_sales,
          num_orders,sales_amount
    from returns,sales
  SQL

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

.email_report_sales_vs_returns_by_rep(start_date, end_date) ⇒ Object



842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
# File 'app/services/report/rmas_report/rmas_report.rb', line 842

def self.email_report_sales_vs_returns_by_rep(start_date,end_date)
  sql = <<-SQL
    with sales as (
        select coalesce(primary_sr_id,local_sr_id) as primary_or_local_rep_id,count(distinct order_id) as num_orders,sum(quantity * (discounted_price - amazon_vc) * exchange_rate) as sales_amount
        from view_sales vsf
        left join product_lines pl on vsf.primary_product_line_id = pl.id
        where invoice_type in ('SO','CI')
        and trim(split_part(pl.lineage_expanded,'>',1)) <> 'Rental Tools'
        and gl_date between '#{start_date}' and '#{end_date}'
        group by coalesce(primary_sr_id,local_sr_id)
    ), returns as (
        select primary_or_local_rep_id,e.full_name,count(distinct rma_id) as num_rmas,sum(quantity * discounted_price * consolidated_exchange_rate) as returned_amount
        from view_rmas_facts vrf
        left join parties e on e.id = vrf.primary_or_local_rep_id
        left join product_lines pl on vrf.primary_product_line_id = pl.id
        where return_date between '#{start_date}' and '#{end_date}'
        and trim(split_part(pl.lineage_expanded,'>',1)) <> 'Rental Tools'
        group by primary_or_local_rep_id,e.full_name
    )
    select r.primary_or_local_rep_id,r.full_name,num_rmas,returned_amount,
          case when sales_amount = 0 then 0 else (returned_amount / sales_amount) * 100 end as ptg_in_sales,
          num_orders,sales_amount
    from returns r
    inner join sales s on r.primary_or_local_rep_id = s.primary_or_local_rep_id
    where r.primary_or_local_rep_id not in (85,155)
    order by 4 desc
  SQL

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

.result(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
69
70
71
72
73
74
75
# File 'app/services/report/rmas_report/rmas_report.rb', line 8

def self.result(options = {})
  start_date = options[:period1_gteq]
  end_date = options[:period1_lteq]
  is_email = options[:is_email]
  company_id = options[:company_id].present? ? options[:company_id].first.to_i : ""
  customer_ids = options[:customer_ids]
  product_lines = options[:product_lines].map(&:presence).compact
  product_line_ids = []
  if product_lines.present?
    product_lines.each do |product_line|
      ProductLine.all.each { |pl| product_line_ids << pl.id if pl.lineage_expanded&.split('>')&.first&.strip == product_line }
    end
  end
  report_groupings = options[:report_groupings]

  rmas_review_chart = rmas_review_chart(start_date,end_date,company_id,customer_ids,product_line_ids,report_groupings)
  rmas_by_state = rmas_by_state(start_date,end_date,company_id,customer_ids,product_line_ids,report_groupings)
  rmas_review_table = rmas_review_table(start_date,end_date,company_id,customer_ids,product_line_ids,report_groupings)
  rmas_review_table_bd = []
  product_lines = product_lines.present? ? product_lines : ProductLine.where.not(lineage_expanded: [nil, ""]).map { |pl| pl.lineage_expanded.split('>').first.strip }.uniq.sort
  product_lines.each do |product_line|
    pl_data = rmas_review_table_bd(start_date,end_date,company_id,customer_ids,product_line,report_groupings)
    pl_data.map{ |d| rmas_review_table_bd << d } if pl_data.present?
  end

  rmas_returned_resons = rmas_returned_resons(start_date,end_date,company_id,customer_ids,product_line_ids,report_groupings)
  rmas_restocking_resons = rmas_restocking_resons(start_date,end_date,company_id,customer_ids,product_line_ids,report_groupings)
  rmas_pl_chart = rmas_by_product_line_chart(start_date,end_date,company_id,customer_ids,product_line_ids,report_groupings)
  rmas_pl_table = rmas_by_product_line_table(start_date,end_date,company_id,customer_ids,product_line_ids,report_groupings)
  rmas_pl_table_bd = rmas_by_product_line_table_bd(start_date,end_date,company_id,customer_ids,product_line_ids,report_groupings)
  rmas_pl_table_bd2 = rmas_by_product_line_table_bd2(start_date,end_date,company_id,customer_ids,product_line_ids,report_groupings)
  rmas_channel_table = rmas_by_channel_table(start_date,end_date,company_id,customer_ids,product_line_ids,report_groupings)
  rmas_channel_table_bd = rmas_by_channel_table_bd(start_date,end_date,company_id,customer_ids,product_line_ids,report_groupings)
  rmas_by_rep = rmas_by_rep(start_date,end_date,company_id,customer_ids,product_line_ids,report_groupings)

  if is_email.present?
    email_report_sales_vs_returns = email_report_sales_vs_returns(start_date,end_date)
    email_report_sales_vs_returns_by_rep = email_report_sales_vs_returns_by_rep(start_date,end_date)
    email_report_rmas_by_state = email_report_rmas_by_state(start_date,end_date)
    email_report_channel_returns = email_report_channel_returns(start_date,end_date)
    email_report_rma_codes_used = email_report_rma_codes_used(start_date,end_date)
  end

  Result.new(success: true,
             start_date: start_date,
             end_date: end_date,
             company_id: company_id,
             rmas_review_chart: rmas_review_chart,
             rmas_by_state: rmas_by_state,
             rmas_review_table: rmas_review_table.compact,
             rmas_review_table_bd: rmas_review_table_bd.uniq,
             rmas_returned_resons: rmas_returned_resons,
             rmas_restocking_resons: rmas_restocking_resons,
             rmas_pl_chart: rmas_pl_chart,
             rmas_pl_table: rmas_pl_table,
             rmas_pl_table_bd: rmas_pl_table_bd,
             rmas_pl_table_bd2: rmas_pl_table_bd2,
             rmas_channel_table: rmas_channel_table,
             rmas_channel_table_bd: rmas_channel_table_bd,
             rmas_by_rep: rmas_by_rep,
             email_report_sales_vs_returns: email_report_sales_vs_returns,
             email_report_sales_vs_returns_by_rep: email_report_sales_vs_returns_by_rep,
             email_report_rmas_by_state: email_report_rmas_by_state,
             email_report_channel_returns: email_report_channel_returns,
             email_report_rma_codes_used: email_report_rma_codes_used
  )

end

.rmas_by_channel_table(start_date, end_date, company_id, customer_ids, product_line_ids, report_groupings) ⇒ Object



661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
# File 'app/services/report/rmas_report/rmas_report.rb', line 661

def self.rmas_by_channel_table(start_date,end_date,company_id,customer_ids,product_line_ids,report_groupings)
  company_id_sql = company_id.present? ? " and company_id = #{company_id} " : " "
  customer_ids_sql = customer_ids.present? ? " and customer_id in (#{customer_ids.map{|c| c.to_i }.join(',')})" : " "
  product_line_ids_sql = product_line_ids.present? ? " and pl.id in (#{product_line_ids.map{|pg| pg.to_i }.join(',')}) " : " "
  report_grouping_sql = report_groupings.present? ? " and report_grouping in (#{report_groupings.map{|rg| "'#{rg.sub("'","''")}'" }.join(',')})" : " "

  sql = <<-SQL
    with sales as (
        select report_grouping,count(distinct order_id) as num_orders,sum(quantity * (discounted_price - amazon_vc) * exchange_rate) as sales_amount
        from view_sales vsf
        left join product_lines pl on vsf.primary_product_line_id = pl.id
        where invoice_type in ('SO','CI')
        and trim(split_part(pl.lineage_expanded,'>',1)) <> 'Rental Tools'
        and gl_date between '#{start_date}' and '#{end_date}'
        #{company_id_sql}
        #{customer_ids_sql}
        #{product_line_ids_sql}
        #{report_grouping_sql}
        group by report_grouping
    ), returns as (
        select report_grouping,count(distinct rma_id) as num_rmas,sum(quantity * discounted_price * consolidated_exchange_rate) as returned_amount
        from view_rmas_facts vrf
        inner join companies c on vrf.company_id = c.id
        left join product_lines pl on vrf.primary_product_line_id = pl.id
        where return_date between '#{start_date}' and '#{end_date}'
        and trim(split_part(pl.lineage_expanded,'>',1)) <> 'Rental Tools'
        #{company_id_sql}
        #{customer_ids_sql}
        #{product_line_ids_sql}
        #{report_grouping_sql}
        group by report_grouping
    ), avg_return_window as (
        select report_grouping,round(avg(return_time),0) as avg_return_time
        from (
            select distinct rma_id,rma_number,report_grouping,shipped_date,rma_created_date,(rma_created_date - shipped_date) as return_time
            from view_rmas_facts
            where (rma_created_date - shipped_date) > 0
            #{company_id_sql}
            #{customer_ids_sql}
        )a
        group by report_grouping
    ), uniq_rmas as (
        select distinct rma_id
        from view_rmas_facts vrf
        inner join companies c on vrf.company_id = c.id
        left join product_lines pl on vrf.primary_product_line_id = pl.id
        where return_date between '#{start_date}' and '#{end_date}'
        and trim(split_part(pl.lineage_expanded,'>',1)) <> 'Rental Tools'
        #{company_id_sql}
        #{customer_ids_sql}
        #{product_line_ids_sql}
        #{report_grouping_sql}
    ), rmas_exchange as (
        select report_grouping,count(o.rma_id) as num_rmas_with_exchange,sum(iv.line_total * iv.consolidated_exchange_rate) as exchange_amount
        from orders o
        inner join invoices iv on o.id = iv.order_id
        inner join uniq_rmas ur on o.rma_id = ur.rma_id
        where order_type = 'SO'
        and o.rma_id is not null
        and o.state = 'invoiced'
        group by report_grouping
    )
    select r.report_grouping,num_rmas,returned_amount,num_rmas_with_exchange,exchange_amount,
          case when sales_amount = 0 then 0 else round(((returned_amount / sales_amount) * 100)::decimal,1) end as ptg_in_sales,
          avg_return_time,coalesce(num_orders,0) as num_orders,coalesce(sales_amount,0) as sales_amount
    from returns r
    left join avg_return_window arw on r.report_grouping = arw.report_grouping
    left join rmas_exchange re on r.report_grouping = re.report_grouping
    left join sales s on r.report_grouping = s.report_grouping
    order by 3 desc
  SQL

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

.rmas_by_channel_table_bd(start_date, end_date, company_id, customer_ids, product_line_ids, report_groupings) ⇒ Object



736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
# File 'app/services/report/rmas_report/rmas_report.rb', line 736

def self.rmas_by_channel_table_bd(start_date,end_date,company_id,customer_ids,product_line_ids,report_groupings)
  company_id_sql = company_id.present? ? " and company_id = #{company_id} " : " "
  customer_ids_sql = customer_ids.present? ? " and customer_id in (#{customer_ids.map{|c| c.to_i }.join(',')})" : " "
  product_line_ids_sql = product_line_ids.present? ? " and pl.id in (#{product_line_ids.map{|pg| pg.to_i }.join(',')}) " : " "
  report_grouping_sql = report_groupings.present? ? " and report_grouping in (#{report_groupings.map{|rg| "'#{rg.sub("'","''")}'" }.join(',')})" : " "

  sql = <<-SQL
    select report_grouping,returned_reason,returned_description,count(distinct rma_id) as num_rmas,sum(quantity * discounted_price * consolidated_exchange_rate) as returned_amount
    from view_rmas_facts vrf
    left join product_lines pl on vrf.primary_product_line_id = pl.id
    where return_date between '#{start_date}' and '#{end_date}'
    and trim(split_part(pl.lineage_expanded,'>',1)) <> 'Rental Tools'
    #{company_id_sql}
    #{customer_ids_sql}
    #{product_line_ids_sql}
    #{report_grouping_sql}
    group by report_grouping,returned_reason,returned_description
    order by 1,4,5 desc
  SQL

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

.rmas_by_product_line_chart(start_date, end_date, company_id, customer_ids, product_line_ids, report_groupings) ⇒ Object



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/rmas_report/rmas_report.rb', line 396

def self.rmas_by_product_line_chart(start_date,end_date,company_id,customer_ids,product_line_ids,report_groupings)
  company_id_sql = company_id.present? ? " and company_id = #{company_id} " : " "
  customer_ids_sql = customer_ids.present? ? " and customer_id in (#{customer_ids.map{|c| c.to_i }.join(',')})" : " "
  product_line_ids_sql = product_line_ids.present? ? " and pl.id in (#{product_line_ids.map{|pg| pg.to_i }.join(',')}) " : " "
  report_grouping_sql = report_groupings.present? ? " and report_grouping in (#{report_groupings.map{|rg| "'#{rg.sub("'","''")}'" }.join(',')})" : " "

  sql = <<-SQL
    with weeks as (
        select distinct week,week_start_date
        from analytic_date_time_dimensions
        where date between '#{start_date}' and '#{end_date}'
        order by week_start_date

    ), rmas_data as (
        select week,week_start_date,
            sum(case when trim(split_part(lineage_expanded,'>',1)) = 'Floor Heating' then (quantity * discounted_price * consolidated_exchange_rate) else 0 end) as floorheating,
            sum(case when trim(split_part(lineage_expanded,'>',1)) = 'Snow Melting' then (quantity * discounted_price * consolidated_exchange_rate) else 0 end) as snowdeicing,
            sum(case when trim(split_part(lineage_expanded,'>',1)) = 'Towel Warmer' then (quantity * discounted_price * consolidated_exchange_rate) else 0 end) as towelwarmer,
            sum(case when trim(split_part(lineage_expanded,'>',1)) = 'Radiant Panel' then (quantity * discounted_price * consolidated_exchange_rate) else 0 end) as radiantpanels,
            sum(case when trim(split_part(lineage_expanded,'>',1)) not in ('Floor Heating','Snow Melting','Towel Warmer','Radiant Panel') then (quantity * discounted_price * consolidated_exchange_rate) else 0 end) as otherproduct,
            count(distinct rma_id) as ttl_rmas
        from view_rmas_facts vrf
        inner join analytic_date_time_dimensions dt on return_date = dt.date
        left join product_lines pl on vrf.primary_product_line_id = pl.id
        where return_date between '#{start_date}' and '#{end_date}'
        and trim(split_part(pl.lineage_expanded,'>',1)) <> 'Rental Tools'
        #{company_id_sql}
        #{customer_ids_sql}
        #{product_line_ids_sql}
        #{report_grouping_sql}
        group by week,week_start_date
        order by week_start_date

    )
    select w.week,w.week_start_date,coalesce(rd.floorheating,0) as floorheating,coalesce(rd.snowdeicing,0) as snowdeicing,coalesce(rd.towelwarmer,0) as towelwarmer,
          coalesce(rd.radiantpanels,0) as radiantpanels,coalesce(rd.otherproduct,0) as otherproduct,coalesce(rd.ttl_rmas,0) as ttl_rmas
    from weeks w
    left join rmas_data rd on rd.week = w.week

  SQL

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

.rmas_by_product_line_table(start_date, end_date, company_id, customer_ids, product_line_ids, report_groupings) ⇒ Object



440
441
442
443
444
445
446
447
448
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
489
490
491
492
493
494
495
496
497
498
499
# File 'app/services/report/rmas_report/rmas_report.rb', line 440

def self.rmas_by_product_line_table(start_date,end_date,company_id,customer_ids,product_line_ids,report_groupings)
  prev_start_date = start_date.years_ago(1)
  prev_end_date = end_date.years_ago(1)
  company_id_sql = company_id.present? ? " and company_id = #{company_id} " : " "
  customer_ids_sql = customer_ids.present? ? " and customer_id in (#{customer_ids.map{|c| c.to_i }.join(',')})" : " "
  product_line_ids_sql = product_line_ids.present? ? " and pl.id in (#{product_line_ids.map{|pg| pg.to_i }.join(',')}) " : " "
  report_grouping_sql = report_groupings.present? ? " and report_grouping in (#{report_groupings.map{|rg| "'#{rg.sub("'","''")}'" }.join(',')})" : " "

  sql = <<-SQL
    with pg_list as (
        select distinct trim(split_part(lineage_expanded,'>',1)) as group1
        from product_lines pl
        where trim(split_part(lineage_expanded,'>',1)) not in ('Shipping','Rental Tools','Services')
        #{product_line_ids_sql}

    ), returns as (
        select trim(split_part(lineage_expanded,'>',1)) as group1,
              sum(case when year = #{prev_start_date.year} then quantity else 0 end) as qty_returned_prev,
              sum(case when year = #{prev_start_date.year} then (quantity * discounted_price * consolidated_exchange_rate) else 0 end) as amount_returned_prev,
              sum(case when year = #{start_date.year} then quantity else 0 end) as qty_returned,
              sum(case when year = #{start_date.year} then (quantity * discounted_price * consolidated_exchange_rate) else 0 end) as amount_returned
        from view_rmas_facts vrf
        inner join analytic_date_time_dimensions dt on return_date = dt.date
        left join product_lines pl on vrf.primary_product_line_id = pl.id
        where (return_date between '#{prev_start_date}' and '#{prev_end_date}' or return_date between '#{start_date}' and '#{end_date}')
        and trim(split_part(pl.lineage_expanded,'>',1)) <> 'Rental Tools'
        #{company_id_sql}
        #{customer_ids_sql}
        #{product_line_ids_sql}
        #{report_grouping_sql}
        group by trim(split_part(lineage_expanded,'>',1))

    ), sales as (
      select trim(split_part(lineage_expanded,'>',1)) as group1,
              sum(case when year = #{prev_start_date.year} then quantity else 0 end) as qty_sold_prev,
              sum(case when year = #{prev_start_date.year} then (quantity * (discounted_price - amazon_vc) * exchange_rate) else 0 end) as sales_amount_prev,
              sum(case when year = #{start_date.year} then quantity else 0 end) as qty_sold,
              sum(case when year = #{start_date.year} then (quantity * (discounted_price - amazon_vc) * exchange_rate) else 0 end) as sales_amount
        from view_sales vsf
        inner join analytic_date_time_dimensions dt on vsf.gl_date = dt.date
        left join product_lines pl on vsf.primary_product_line_id = pl.id
        where invoice_type in ('SO','CI')
        and (date between '#{prev_start_date}' and '#{prev_end_date}' or date between '#{start_date}' and '#{end_date}')
        and trim(split_part(pl.lineage_expanded,'>',1)) <> 'Rental Tools'
        #{company_id_sql}
        #{customer_ids_sql}
        #{product_line_ids_sql}
        #{report_grouping_sql}
        group by trim(split_part(lineage_expanded,'>',1))
    )
    select pl.group1,s.qty_sold_prev,s.sales_amount_prev,r.qty_returned_prev,r.amount_returned_prev,s.qty_sold,s.sales_amount,r.qty_returned,r.amount_returned
    from pg_list pl
    left join returns r on pl.group1 = r.group1
    left join sales s on pl.group1 = s.group1
    where s.qty_sold_prev is not null or s.qty_sold is not null or r.qty_returned_prev is not null or r.qty_returned is not null
    order by pl.group1
  SQL

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

.rmas_by_product_line_table_bd(start_date, end_date, company_id, customer_ids, product_line_ids, report_groupings) ⇒ Object



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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
# File 'app/services/report/rmas_report/rmas_report.rb', line 501

def self.rmas_by_product_line_table_bd(start_date,end_date,company_id,customer_ids,product_line_ids,report_groupings)
  prev_start_date = start_date.years_ago(1)
  prev_end_date = end_date.years_ago(1)
  company_id_sql = company_id.present? ? " and company_id = #{company_id} " : " "
  customer_ids_sql = customer_ids.present? ? " and customer_id in (#{customer_ids.map{|c| c.to_i }.join(',')})" : " "
  product_line_ids_sql = product_line_ids.present? ? " and pl.id in (#{product_line_ids.map{|pg| pg.to_i }.join(',')}) " : " "
  product_line_ids_sql2 = product_line_ids.present? ? " " : " and group1 <> ('Shipping') "
  report_grouping_sql = report_groupings.present? ? " and report_grouping in (#{report_groupings.map{|rg| "'#{rg.sub("'","''")}'" }.join(',')})" : " "

  sql = <<-SQL
    with uniq_items as (
        select distinct trim(split_part(lineage_expanded,'>',1)) as group1,item_id
        from view_rmas_facts vrf
        left join product_lines pl on vrf.primary_product_line_id = pl.id
        where (return_date between '#{prev_start_date}' and '#{prev_end_date}' or return_date between '#{start_date}' and '#{end_date}')
        and trim(split_part(pl.lineage_expanded,'>',1)) <> 'Rental Tools'
        and item_id is not null
        #{company_id_sql}
        #{customer_ids_sql}
        #{product_line_ids_sql}
        #{report_grouping_sql}
        union all
        select distinct trim(split_part(lineage_expanded,'>',1)) as group1,item_id
        from view_sales vsf
        left join product_lines pl on vsf.primary_product_line_id = pl.id
        where invoice_type in ('SO','CI')
        and (gl_date between '#{prev_start_date}' and '#{prev_end_date}' or gl_date between '#{start_date}' and '#{end_date}')
        and trim(split_part(pl.lineage_expanded,'>',1)) <> 'Rental Tools'
        and item_id is not null
        #{company_id_sql}
        #{customer_ids_sql}
        #{product_line_ids_sql}
        #{report_grouping_sql}

    ), pg_list as (
        select distinct group1,item_id
        from uniq_items
        where group1 is not null
        #{product_line_ids_sql2}

    ), returns as (
        select trim(split_part(lineage_expanded,'>',1)) as group1,item_id,
            sum(case when year = #{prev_start_date.year} then quantity else 0 end) as qty_returned_prev,
            sum(case when year = #{prev_start_date.year} then (quantity * discounted_price * consolidated_exchange_rate) else 0 end) as amount_returned_prev,
            sum(case when year = #{start_date.year} then quantity else 0 end) as qty_returned,
            sum(case when year = #{start_date.year} then (quantity * discounted_price * consolidated_exchange_rate) else 0 end) as amount_returned
        from view_rmas_facts vrf
        inner join analytic_date_time_dimensions dt on return_date = dt.date
        left join product_lines pl on vrf.primary_product_line_id = pl.id
        where (return_date between '#{prev_start_date}' and '#{prev_end_date}' or return_date between '#{start_date}' and '#{end_date}')
        and trim(split_part(pl.lineage_expanded,'>',1)) <> 'Rental Tools'
        #{company_id_sql}
        #{customer_ids_sql}
        #{product_line_ids_sql}
        #{report_grouping_sql}
        group by trim(split_part(lineage_expanded,'>',1)),item_id

    ), sales as (
        select trim(split_part(lineage_expanded,'>',1)) as group1,item_id,
            sum(case when year = #{prev_start_date.year} then quantity else 0 end) as qty_sold_prev,
            sum(case when year = #{prev_start_date.year} then (quantity * (discounted_price - amazon_vc) * exchange_rate) else 0 end) as sales_amount_prev,
            sum(case when year = #{start_date.year} then quantity else 0 end) as qty_sold,
            sum(case when year = #{start_date.year} then (quantity * (discounted_price - amazon_vc) * exchange_rate) else 0 end) as sales_amount
        from view_sales vsf
        inner join analytic_date_time_dimensions dt on vsf.gl_date = dt.date
        left join product_lines pl on vsf.primary_product_line_id = pl.id
        where invoice_type in ('SO','CI')
        and (date between '#{prev_start_date}' and '#{prev_end_date}' or date between '#{start_date}' and '#{end_date}')
        and trim(split_part(pl.lineage_expanded,'>',1)) <> 'Rental Tools'
        #{company_id_sql}
        #{customer_ids_sql}
        #{product_line_ids_sql}
        #{report_grouping_sql}
        group by trim(split_part(lineage_expanded,'>',1)),item_id

    ), item_supplier as (
        select i.id as item_id,array_agg(replace(full_name,',','')) as suppliers
        from items i
        inner join supplier_items si on i.id = si.item_id
        inner join parties s on si.supplier_id = s.id
        where si.active = true
        group by i.id
    )
    select pl.group1,trim(split_part(pl2.lineage_expanded,'>',2)) as group2,trim(split_part(pl2.lineage_expanded,'>',3)) as group3,trim(split_part(pl2.lineage_expanded,'>',2)) as group4,pl.item_id,sku,i.name,
          coalesce(s.qty_sold_prev,0) as qty_sold_prev,coalesce(s.sales_amount_prev,0) as sales_amount_prev,coalesce(r.qty_returned_prev,0) as qty_returned_prev,coalesce(r.amount_returned_prev,0) as amount_returned_prev,
          coalesce(s.qty_sold,0) as qty_sold,coalesce(s.sales_amount,0) as sales_amount,coalesce(r.qty_returned,0) as qty_returned,coalesce(r.amount_returned,0) as amount_returned,suppliers
    from pg_list pl
    inner join items i on pl.item_id = i.id
    inner join product_lines pl2 on i.primary_product_line_id = pl2.id
    left join returns r on pl.group1 = r.group1 and pl.item_id = r.item_id
    left join sales s on pl.group1 = s.group1 and pl.item_id = s.item_id
    left join item_supplier ts on ts.item_id = pl.item_id
    order by 1,15 desc

  SQL

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

.rmas_by_product_line_table_bd2(start_date, end_date, company_id, customer_ids, product_line_ids, report_groupings) ⇒ Object



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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
# File 'app/services/report/rmas_report/rmas_report.rb', line 600

def self.rmas_by_product_line_table_bd2(start_date,end_date,company_id,customer_ids,product_line_ids,report_groupings)
  prev_start_date = start_date.years_ago(1)
  prev_end_date = end_date.years_ago(1)
  company_id_sql = company_id.present? ? " and company_id = #{company_id} " : " "
  customer_ids_sql = customer_ids.present? ? " and customer_id in (#{customer_ids.map{|c| c.to_i }.join(',')})" : " "
  product_line_ids_sql = product_line_ids.present? ? " and pl.id in (#{product_line_ids.map{|pg| pg.to_i }.join(',')}) " : " "
  report_grouping_sql = report_groupings.present? ? " and report_grouping in (#{report_groupings.map{|rg| "'#{rg.sub("'","''")}'" }.join(',')})" : " "

  sql = <<-SQL
    with pg_list as (
        select distinct trim(split_part(lineage_expanded,'>',1)) as group1,trim(split_part(lineage_expanded,'>',2)) as group2
        from product_lines pl
        where trim(split_part(lineage_expanded,'>',1)) not in ('Shipping','Rental Tools','Services','Mirror Defogger','Shower Kits')
        #{product_line_ids_sql}

    ), returns as (
        select trim(split_part(lineage_expanded,'>',1)) as group1,trim(split_part(lineage_expanded,'>',2)) as group2,
            sum(case when year = #{prev_start_date.year} then quantity else 0 end) as qty_returned_prev,
            sum(case when year = #{prev_start_date.year} then (quantity * discounted_price * consolidated_exchange_rate) else 0 end) as amount_returned_prev,
            sum(case when year = #{start_date.year} then quantity else 0 end) as qty_returned,
            sum(case when year = #{start_date.year} then (quantity * discounted_price * consolidated_exchange_rate) else 0 end) as amount_returned
        from view_rmas_facts vrf
        inner join analytic_date_time_dimensions dt on return_date = dt.date
        left join product_lines pl on vrf.primary_product_line_id = pl.id
        where (return_date between '#{prev_start_date}' and '#{prev_end_date}' or return_date between '#{start_date}' and '#{end_date}')
        and trim(split_part(pl.lineage_expanded,'>',1)) <> 'Rental Tools'
        #{company_id_sql}
        #{customer_ids_sql}
        #{product_line_ids_sql}
        #{report_grouping_sql}
        group by trim(split_part(lineage_expanded,'>',1)),trim(split_part(lineage_expanded,'>',2))

    ), sales as (
        select trim(split_part(lineage_expanded,'>',1)) as group1,trim(split_part(lineage_expanded,'>',2)) as group2,
            sum(case when year = #{prev_start_date.year} then quantity else 0 end) as qty_sold_prev,
            sum(case when year = #{prev_start_date.year} then (quantity * (discounted_price - amazon_vc) * exchange_rate) else 0 end) as sales_amount_prev,
            sum(case when year = #{start_date.year} then quantity else 0 end) as qty_sold,
            sum(case when year = #{start_date.year} then (quantity * (discounted_price - amazon_vc) * exchange_rate) else 0 end) as sales_amount
        from view_sales vsf
        inner join analytic_date_time_dimensions dt on vsf.gl_date = dt.date
        left join product_lines pl on vsf.primary_product_line_id = pl.id
        where invoice_type in ('SO','CI')
        and (date between '#{prev_start_date}' and '#{prev_end_date}' or date between '#{start_date}' and '#{end_date}')
        and trim(split_part(pl.lineage_expanded,'>',1)) <> 'Rental Tools'
        #{company_id_sql}
        #{customer_ids_sql}
        #{product_line_ids_sql}
        #{report_grouping_sql}
        group by trim(split_part(lineage_expanded,'>',1)),trim(split_part(lineage_expanded,'>',2))
    )
    select pl.group1,pl.group2,s.qty_sold_prev,s.sales_amount_prev,r.qty_returned_prev,r.amount_returned_prev,s.qty_sold,s.sales_amount,r.qty_returned,r.amount_returned
    from pg_list pl
    left join returns r on pl.group1 = r.group1 and pl.group2 = r.group2
    left join sales s on pl.group1 = s.group1 and pl.group2 = s.group2
    where s.qty_sold_prev is not null or s.qty_sold is not null or r.qty_returned_prev is not null or r.qty_returned is not null
    order by pl.group1,4 desc
  SQL

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

.rmas_by_rep(start_date, end_date, company_id, customer_ids, product_line_ids, report_groupings) ⇒ Object



759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
# File 'app/services/report/rmas_report/rmas_report.rb', line 759

def self.rmas_by_rep(start_date,end_date,company_id,customer_ids,product_line_ids,report_groupings)
  prev_start_date = start_date.years_ago(1)
  prev_end_date = end_date.years_ago(1)
  company_id_sql = company_id.present? ? " and vf.company_id = #{company_id} " : " "
  customer_ids_sql = customer_ids.present? ? " and vf.customer_id in (#{customer_ids.map{|c| c.to_i }.join(',')})" : " "
  product_line_ids_sql = product_line_ids.present? ? " and pl.id in (#{product_line_ids.map{|pg| pg.to_i }.join(',')}) " : " "
  report_grouping_sql = report_groupings.present? ? " and vf.report_grouping in (#{report_groupings.map{|rg| "'#{rg.sub("'","''")}'" }.join(',')})" : " "

  sql = <<-SQL
    with sales as (
      select coalesce(primary_sr_id,local_sr_id) as primary_or_local_rep_id,
            count(distinct case when year = #{prev_start_date.year} then order_id end) as num_orders_prev,
            sum(case when year = #{prev_start_date.year} then (quantity * (discounted_price - amazon_vc) * exchange_rate) else 0 end) as sales_amount_prev,
            count(distinct case when year = #{start_date.year} then order_id end) as num_orders,
            sum(case when year = #{start_date.year} then (quantity * (discounted_price - amazon_vc) * exchange_rate) else 0 end) as sales_amount
      from view_sales vf
      inner join analytic_date_time_dimensions dt on vf.gl_date = dt.date
      left join product_lines pl on vf.primary_product_line_id = pl.id
      where invoice_type in ('SO','CI')
      and (gl_date between '#{prev_start_date}' and '#{prev_end_date}' or gl_date between '#{start_date}' and '#{end_date}')
      and trim(split_part(pl.lineage_expanded,'>',1)) <> 'Rental Tools'
      #{company_id_sql}
      #{customer_ids_sql}
      #{product_line_ids_sql}
      #{report_grouping_sql}
      group by coalesce(primary_sr_id,local_sr_id)

    ), returns as (
      select primary_or_local_rep_id,full_name,
              count(distinct case when year = #{prev_start_date.year} then rma_id end) as num_rmas_prev,
              sum(case when year = #{prev_start_date.year} then (quantity * discounted_price * consolidated_exchange_rate) else 0 end) as amount_returned_prev,
              count(distinct case when year = #{start_date.year} then rma_id end) as num_rmas,
              sum(case when year = #{start_date.year} then (quantity * discounted_price * consolidated_exchange_rate) else 0 end) as amount_returned
      from view_rmas_facts vf
      inner join analytic_date_time_dimensions dt on return_date = dt.date
      left join parties e on e.id = vf.primary_or_local_rep_id
      left join product_lines pl on vf.primary_product_line_id = pl.id
      where (return_date between '#{prev_start_date}' and '#{prev_end_date}' or return_date between '#{start_date}' and '#{end_date}')
      and trim(split_part(pl.lineage_expanded,'>',1)) <> 'Rental Tools'
      #{company_id_sql}
      #{customer_ids_sql}
      #{product_line_ids_sql}
      #{report_grouping_sql}
      group by primary_or_local_rep_id,full_name
    )
    select r.primary_or_local_rep_id,full_name,num_rmas_prev,amount_returned_prev,
          coalesce(num_orders_prev,0) as num_orders_prev,coalesce(sales_amount_prev,0) as sales_amount_prev,
          num_rmas,amount_returned,
          coalesce(num_orders,0) as num_orders,coalesce(sales_amount,0) as sales_amount
    from returns r
    left join sales s on r.primary_or_local_rep_id = s.primary_or_local_rep_id
    where r.primary_or_local_rep_id not in (85,155)
    order by 7 desc
  SQL

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

.rmas_by_state(start_date, end_date, company_id, customer_ids, product_line_ids, report_groupings) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'app/services/report/rmas_report/rmas_report.rb', line 136

def self.rmas_by_state(start_date,end_date,company_id,customer_ids,product_line_ids,report_groupings)
  company_id_sql = company_id.present? ? " and company_id = #{company_id} " : " "
  customer_ids_sql = customer_ids.present? ? " and customer_id in (#{customer_ids.map{|c| c.to_i }.join(',')})" : " "
  product_line_ids_sql = product_line_ids.present? ? " and pl.id in (#{product_line_ids.map{|pg| pg.to_i }.join(',')}) " : " and pl.id is not null "
  report_grouping_sql = report_groupings.present? ? " and report_grouping in (#{report_groupings.map{|rg| "'#{rg.sub("'","''")}'" }.join(',')})" : " "

  sql = <<-SQL
    select rma_state,sum(quantity * discounted_price * consolidated_exchange_rate) as rmas_amount,count(distinct rma_id) as num_rmas
    from view_rmas_facts vrf
    left join product_lines pl on vrf.primary_product_line_id = pl.id
    where (vrf.rma_created_date between '#{start_date}' and '#{end_date}' or vrf.return_date between '#{start_date}' and '#{end_date}')
    and trim(split_part(pl.lineage_expanded,'>',1)) <> 'Rental Tools'
    #{company_id_sql}
    #{customer_ids_sql}
    #{product_line_ids_sql}
    #{report_grouping_sql}
    group by rma_state
  SQL

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

.rmas_restocking_resons(start_date, end_date, company_id, customer_ids, product_line_ids, report_groupings) ⇒ Object



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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'app/services/report/rmas_report/rmas_report.rb', line 353

def self.rmas_restocking_resons(start_date,end_date,company_id,customer_ids,product_line_ids,report_groupings)
  company_id_sql = company_id.present? ? " and company_id = #{company_id} " : " "
  customer_ids_sql = customer_ids.present? ? " and customer_id in (#{customer_ids.map{|c| c.to_i }.join(',')})" : " "
  product_line_ids_sql = product_line_ids.present? ? " and pl.id in (#{product_line_ids.map{|pg| pg.to_i }.join(',')}) " : " "
  report_grouping_sql = report_groupings.present? ? " and report_grouping in (#{report_groupings.map{|rg| "'#{rg.sub("'","''")}'" }.join(',')})" : " "

  sql = <<-SQL
    with top_reasons as (
        select restocking_reason,restocking_description,count(distinct rma_id) as num_rmas
        from view_rmas_facts vrf
        left join product_lines pl on vrf.primary_product_line_id = pl.id
        where return_date between '#{start_date}' and '#{end_date}'
        and trim(split_part(pl.lineage_expanded,'>',1)) <> 'Rental Tools'
        #{company_id_sql}
        #{customer_ids_sql}
        #{product_line_ids_sql}
        #{report_grouping_sql}
        group by restocking_reason,restocking_description
        order by 3 desc limit 10

    ), others as (
        select 'Others' as restocking_description,count(distinct rma_id) as num_rmas
        from view_rmas_facts vrf
        left join product_lines pl on vrf.primary_product_line_id = pl.id
        where return_date between '#{start_date}' and '#{end_date}'
        and restocking_reason not in (select restocking_reason from top_reasons)
        and trim(split_part(pl.lineage_expanded,'>',1)) <> 'Rental Tools'
        #{company_id_sql}
        #{customer_ids_sql}
        #{product_line_ids_sql}
        #{report_grouping_sql}
    )
    select restocking_reason,(restocking_reason || ' - ' || restocking_description) as restocking_description,num_rmas
    from top_reasons
    where restocking_reason <> 'No code'
    union all
    select restocking_description,restocking_description,num_rmas
    from others
  SQL

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

.rmas_returned_resons(start_date, end_date, company_id, customer_ids, product_line_ids, report_groupings) ⇒ Object



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
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'app/services/report/rmas_report/rmas_report.rb', line 311

def self.rmas_returned_resons(start_date,end_date,company_id,customer_ids,product_line_ids,report_groupings)
  company_id_sql = company_id.present? ? " and company_id = #{company_id} " : " "
  customer_ids_sql = customer_ids.present? ? " and customer_id in (#{customer_ids.map{|c| c.to_i }.join(',')})" : " "
  product_line_ids_sql = product_line_ids.present? ? " and pl.id in (#{product_line_ids.map{|pg| pg.to_i }.join(',')}) " : " "
  report_grouping_sql = report_groupings.present? ? " and report_grouping in (#{report_groupings.map{|rg| "'#{rg.sub("'","''")}'" }.join(',')})" : " "

  sql = <<-SQL
    with top_reasons as (
        select returned_reason,returned_description,count(distinct rma_id) as num_rmas
        from view_rmas_facts vrf
        left join product_lines pl on vrf.primary_product_line_id = pl.id
        where return_date between '#{start_date}' and '#{end_date}'
        and trim(split_part(pl.lineage_expanded,'>',1)) <> 'Rental Tools'
        #{company_id_sql}
        #{customer_ids_sql}
        #{product_line_ids_sql}
        #{report_grouping_sql}
        group by returned_reason,returned_description
        order by 3 desc limit 10

    ), others as (
        select 'Others' as returned_description,count(distinct rma_id) as num_rmas
        from view_rmas_facts vrf
        left join product_lines pl on vrf.primary_product_line_id = pl.id
        where return_date between '#{start_date}' and '#{end_date}'
        and returned_reason not in (select returned_reason from top_reasons)
        and trim(split_part(pl.lineage_expanded,'>',1)) <> 'Rental Tools'
        #{company_id_sql}
        #{customer_ids_sql}
        #{product_line_ids_sql}
        #{report_grouping_sql}
    )
    select returned_reason,(returned_reason || ' - ' || returned_description) as returned_description,num_rmas
    from top_reasons
    union all
    select returned_description,returned_description,num_rmas
    from others
  SQL

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

.rmas_review_chart(start_date, end_date, company_id, customer_ids, product_line_ids, report_groupings) ⇒ Object



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
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/services/report/rmas_report/rmas_report.rb', line 77

def self.rmas_review_chart(start_date,end_date,company_id,customer_ids,product_line_ids,report_groupings)
  start_year = start_date.years_ago(7).year
  end_year = end_date.year
  start_day = (start_date.month * 100) + start_date.day
  end_day = (end_date.month * 100) +  end_date.day
  # prev_start_date = start_date.years_ago(1)
  # prev_end_date = end_date.years_ago(1)
  company_id_sql = company_id.present? ? " and company_id = #{company_id} " : " "
  customer_ids_sql = customer_ids.present? ? " and customer_id in (#{customer_ids.map{|c| c.to_i }.join(',')})" : " "
  product_line_ids_sql = product_line_ids.present? ? " and pl.id in (#{product_line_ids.map{|pg| pg.to_i }.join(',')}) " : " and pl.id is not null "
  report_grouping_sql = report_groupings.present? ? " and report_grouping in (#{report_groupings.map{|rg| "'#{rg.sub("'","''")}'" }.join(',')})" : " "

  sql = <<-SQL
    with sales as (
        select year,sum(quantity * (discounted_price - amazon_vc) * exchange_rate) as sales
        from view_sales vsf
        inner join analytic_date_time_dimensions dt on vsf.gl_date = dt.date
        left join product_lines pl on vsf.primary_product_line_id = pl.id
        where invoice_type in ('SO','CI')
        and year between #{start_year} and #{end_year}
        and ((month * 100) + day) between #{start_day} and #{end_day}
        and trim(split_part(pl.lineage_expanded,'>',1)) <> 'Rental Tools'
        #{company_id_sql}
        #{customer_ids_sql}
        #{product_line_ids_sql}
        #{report_grouping_sql}
        group by year

    ), returns as (
        select year,sum(quantity * discounted_price * consolidated_exchange_rate) as returns,count(distinct rma_id) as num_rmas
        from view_rmas_facts vrf
        inner join analytic_date_time_dimensions dt on vrf.return_date = dt.date
        left join product_lines pl on vrf.primary_product_line_id = pl.id
        where year between #{start_year} and #{end_year}
        and ((month * 100) + day) between #{start_day} and #{end_day}
        and trim(split_part(pl.lineage_expanded,'>',1)) <> 'Rental Tools'
        #{company_id_sql}
        #{customer_ids_sql}
        #{product_line_ids_sql}
        #{report_grouping_sql}
        group by year

    ), calculations as (
      select s.year,s.sales,r.returns,(lag(r.returns) OVER (ORDER BY s.year)) as returns_prev,num_rmas,(lag(r.num_rmas) OVER (ORDER BY s.year)) as num_rmas_prev
      from sales s
      inner join returns r on s.year = r.year

    )
    select year,sales,returns,
      coalesce(case when returns_prev = 0 then 0 else round((((returns - returns_prev) / returns_prev) * 100)::decimal,0) end,0) as returned_amount_variation,
      coalesce(case when num_rmas_prev = 0 then 0 else round((((num_rmas - num_rmas_prev)::decimal / num_rmas_prev) * 100),0) end,0) as num_rmas_variation
    from calculations
    where year >= #{start_year + 1}
    order by year
  SQL

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

.rmas_review_table(start_date, end_date, company_id, customer_ids, product_line_ids, report_groupings) ⇒ Object



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

def self.rmas_review_table(start_date,end_date,company_id,customer_ids,product_line_ids,report_groupings)
  start_year = start_date.years_ago(7).year
  end_year = end_date.year
  start_day = (start_date.month * 100) + start_date.day
  end_day = (end_date.month * 100) +  end_date.day
  # prev_start_date = start_date.years_ago(1)
  # prev_end_date = end_date.years_ago(1)
  company_id_sql = company_id.present? ? " and company_id = #{company_id} " : " "
  customer_ids_sql = customer_ids.present? ? " and customer_id in (#{customer_ids.map{|c| c.to_i }.join(',')})" : " "
  product_line_ids_sql = product_line_ids.present? ? " and pl.id in (#{product_line_ids.map{|pg| pg.to_i }.join(',')}) " : " and pl.id is not null "
  report_grouping_sql = report_groupings.present? ? " and report_grouping in (#{report_groupings.map{|rg| "'#{rg.sub("'","''")}'" }.join(',')})" : " "

  sql = <<-SQL
    with returns as (
        select year,
        count(distinct rma_id) as ttl_rmas,
        sum(quantity * discounted_price * consolidated_exchange_rate) as ttl_amount_returned,
        count(distinct case when company_id = 1 then rma_id end) as us_rmas,
        sum(case when company_id = 1 then (quantity * discounted_price * consolidated_exchange_rate) end) as us_amount_returned,
        count(distinct case when company_id = 2 then rma_id end) as ca_rmas,
        sum(case when company_id = 2 then (quantity * discounted_price * consolidated_exchange_rate) end) as ca_amount_returned
        from view_rmas_facts vrf
        inner join analytic_date_time_dimensions dt on vrf.return_date = dt.date
        left join product_lines pl on vrf.primary_product_line_id = pl.id
        where year between #{start_year} and #{end_year}
        and ((month * 100) + day) between #{start_day} and #{end_day}
        and trim(split_part(pl.lineage_expanded,'>',1)) <> 'Rental Tools'
        #{company_id_sql}
        #{customer_ids_sql}
        #{product_line_ids_sql}
        #{report_grouping_sql}
        group by year
        order by 1

    ), calculations as (
        select year,
        ttl_rmas,lag(ttl_rmas) OVER (ORDER BY year) as ttl_rmas_prev,
        ttl_amount_returned,lag(ttl_amount_returned) OVER (ORDER BY year) as ttl_amount_returned_prev,
        us_rmas,lag(us_rmas) OVER (ORDER BY year) as us_rmas_prev,
        us_amount_returned,lag(us_amount_returned) OVER (ORDER BY year) as us_amount_returned_prev,
        ca_rmas,lag(ca_rmas) OVER (ORDER BY year) as ca_rmas_prev,
        ca_amount_returned,lag(ca_amount_returned) OVER (ORDER BY year) as ca_amount_returned_prev
        from returns r

    ), sales as (
        select year,
        sum(quantity * (discounted_price - amazon_vc) * exchange_rate) as ttl_sales,
        sum(case when company_id = 1 then (quantity * (discounted_price - amazon_vc) * exchange_rate) else 0 end) as us_sales,
        sum(case when company_id = 2 then (quantity * (discounted_price - amazon_vc) * exchange_rate) else 0 end) as ca_sales
        from view_sales vsf
        inner join analytic_date_time_dimensions dt on vsf.gl_date = dt.date
        left join product_lines pl on vsf.primary_product_line_id = pl.id
        where invoice_type in ('SO','CI')
        and year between #{start_year} and #{end_year}
        and ((month * 100) + day) between #{start_day} and #{end_day}
        and trim(split_part(pl.lineage_expanded,'>',1)) <> 'Rental Tools'
        #{company_id_sql}
        #{customer_ids_sql}
        #{product_line_ids_sql}
        #{report_grouping_sql}
        group by year
    )
    select c.year,
        ttl_rmas,case when ttl_rmas_prev = 0 then 0 else round((((ttl_rmas - ttl_rmas_prev)::decimal / ttl_rmas_prev) * 100),0) end as ttl_rmas_variation,
        ttl_amount_returned,case when ttl_sales = 0 then 0 else round(((ttl_amount_returned / ttl_sales) * 100)::decimal,1) end ttl_ptg_of_sales,ttl_sales,
        case when ttl_amount_returned_prev = 0 then 0 else round((((ttl_amount_returned - ttl_amount_returned_prev) / ttl_amount_returned_prev) * 100)::decimal,0) end as ttl_amount_variation,
        us_rmas,case when us_rmas_prev = 0 then 0 else round((((us_rmas - us_rmas_prev)::decimal / us_rmas_prev) * 100),0) end as us_rmas_variation,
        us_amount_returned,case when us_sales = 0 then 0 else round(((us_amount_returned / us_sales) * 100)::decimal,1) end us_ptg_of_sales,us_sales,
        case when us_amount_returned_prev = 0 then 0 else round((((us_amount_returned - us_amount_returned_prev) / us_amount_returned_prev) * 100)::decimal,0) end as us_amount_variation,
        ca_rmas,case when ca_rmas_prev = 0 then 0 else round((((ca_rmas - ca_rmas_prev)::decimal / ca_rmas_prev) * 100),0) end as ca_rmas_variation,
        ca_amount_returned,case when ca_sales = 0 then 0 else round(((ca_amount_returned / ca_sales) * 100)::decimal,1) end ca_ptg_of_sales,ca_sales,
        case when ca_amount_returned_prev = 0 then 0 else round((((ca_amount_returned - ca_amount_returned_prev) / ca_amount_returned_prev) * 100)::decimal,0) end as ca_amount_variation
    from calculations c
    inner join sales s on s.year = c.year
    where c.year >= #{start_year + 1}
    order by c.year
  SQL

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

.rmas_review_table_bd(start_date, end_date, company_id, customer_ids, product_line, report_groupings) ⇒ Object



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

def self.rmas_review_table_bd(start_date,end_date,company_id,customer_ids,product_line,report_groupings)
  start_year = start_date.years_ago(7).year
  end_year = end_date.year
  start_day = (start_date.month * 100) + start_date.day
  end_day = (end_date.month * 100) +  end_date.day
  # prev_start_date = start_date.years_ago(1)
  # prev_end_date = end_date.years_ago(1)
  company_id_sql = company_id.present? ? " and company_id = #{company_id} " : " "
  customer_ids_sql = customer_ids.present? ? " and customer_id in (#{customer_ids.map{|c| c.to_i }.join(',')})" : " "
  report_grouping_sql = report_groupings.present? ? " and report_grouping in (#{report_groupings.map{|rg| "'#{rg.sub("'","''")}'" }.join(',')})" : " "

  sql = <<-SQL
    with returns as (
        select year,trim(split_part(pl.lineage_expanded,'>',1)) as group1,
        coalesce(sum(quantity * discounted_price * consolidated_exchange_rate), 0) as ttl_amount_returned,
        coalesce(sum(case when company_id = 1 then (quantity * discounted_price * consolidated_exchange_rate) end),0) as us_amount_returned,
        coalesce(sum(case when company_id = 2 then (quantity * discounted_price * consolidated_exchange_rate) end),0) as ca_amount_returned
        from view_rmas_facts vrf
        inner join analytic_date_time_dimensions dt on vrf.return_date = dt.date
        left join product_lines pl on vrf.primary_product_line_id = pl.id
        where year between #{start_year} and #{end_year}
        and ((month * 100) + day) between #{start_day} and #{end_day}
        and trim(split_part(pl.lineage_expanded,'>',1)) <> 'Rental Tools'
        and trim(split_part(pl.lineage_expanded,'>',1)) = '#{product_line}'
        #{company_id_sql}
        #{customer_ids_sql}
        #{report_grouping_sql}
        group by year,trim(split_part(pl.lineage_expanded,'>',1))
        order by 1

    ), calculations as (
        select year,group1,
        ttl_amount_returned,coalesce(lag(ttl_amount_returned) OVER (ORDER BY year),0) as ttl_amount_returned_prev,
        us_amount_returned,coalesce(lag(us_amount_returned) OVER (ORDER BY year),0) as us_amount_returned_prev,
        ca_amount_returned,coalesce(lag(ca_amount_returned) OVER (ORDER BY year),0) as ca_amount_returned_prev
        from returns r

    ), sales as (
        select year,trim(split_part(pl.lineage_expanded,'>',1)) as group1,
        sum(quantity * (discounted_price - amazon_vc) * exchange_rate) as ttl_sales,
        sum(case when company_id = 1 then (quantity * (discounted_price - amazon_vc) * exchange_rate) else 0 end) as us_sales,
        sum(case when company_id = 2 then (quantity * (discounted_price - amazon_vc) * exchange_rate) else 0 end) as ca_sales
        from view_sales vsf
        inner join analytic_date_time_dimensions dt on vsf.gl_date = dt.date
        left join product_lines pl on vsf.primary_product_line_id = pl.id
        where invoice_type in ('SO','CI')
        and year between #{start_year} and #{end_year}
        and ((month * 100) + day) between #{start_day} and #{end_day}
        and trim(split_part(pl.lineage_expanded,'>',1)) <> 'Rental Tools'
        and trim(split_part(pl.lineage_expanded,'>',1)) = '#{product_line}'
        #{company_id_sql}
        #{customer_ids_sql}
        #{report_grouping_sql}
        group by year,trim(split_part(pl.lineage_expanded,'>',1))
    )
    select c.year,c.group1,
        ttl_amount_returned,case when ttl_sales = 0 then 0 else round(((ttl_amount_returned / ttl_sales) * 100)::decimal,1) end ttl_ptg_of_sales,ttl_sales,
        case when ttl_amount_returned_prev = 0 then 0 else round((((ttl_amount_returned - ttl_amount_returned_prev) / ttl_amount_returned_prev) * 100)::decimal,0) end as ttl_amount_variation,
        us_amount_returned,case when us_sales = 0 then 0 else round(((us_amount_returned / us_sales) * 100)::decimal,1) end us_ptg_of_sales,us_sales,
        case when us_amount_returned_prev = 0 then 0 else round((((us_amount_returned - us_amount_returned_prev) / us_amount_returned_prev) * 100)::decimal,0) end as us_amount_variation,
        ca_amount_returned,case when ca_sales = 0 then 0 else round(((ca_amount_returned / ca_sales) * 100)::decimal,1) end ca_ptg_of_sales,ca_sales,
        case when ca_amount_returned_prev = 0 then 0 else round((((ca_amount_returned - ca_amount_returned_prev) / ca_amount_returned_prev) * 100)::decimal,0) end as ca_amount_variation
    from calculations c
    left join sales s on s.year = c.year and s.group1 = c.group1
    where c.year >= #{start_year + 1}
    and ttl_amount_returned_prev is not null
    order by c.year,c.group1
  SQL

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