Class: Report::ConversionReport::ConversionReport
- Inherits:
-
Object
- Object
- Report::ConversionReport::ConversionReport
- Defined in:
- app/services/report/conversion_report/conversion_report.rb
Overview
Service object: conversion report.
Defined Under Namespace
Classes: Result
Class Method Summary collapse
-
.converison_per_master_grouping(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) ⇒ Object
Converison per master grouping.
-
.converison_per_product_line_group1(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) ⇒ Object
Converison per product line group1.
-
.converison_per_product_line_group2(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) ⇒ Object
Converison per product line group2.
-
.converison_per_product_line_group3(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) ⇒ Object
Converison per product line group3.
-
.converison_per_product_line_total(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) ⇒ Object
Converison per product line total.
-
.converison_per_rep(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) ⇒ Object
Converison per rep.
-
.converison_per_rep_and_product_line(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) ⇒ Object
Converison per rep and product line.
-
.converison_per_rep_and_report_grouping(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) ⇒ Object
Converison per rep and report grouping.
-
.converison_per_rep_total(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) ⇒ Object
Converison per rep total.
-
.converison_per_report_grouping(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) ⇒ Object
Converison per report grouping.
-
.converison_per_report_grouping_and_product_line(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) ⇒ Object
Converison per report grouping and product line.
-
.conversion_per_discount(start_date, end_date, country, report_groupings) ⇒ Object
Conversion per discount.
-
.conversion_per_discount_report_grouping(start_date, end_date, country, report_groupings) ⇒ Object
Conversion per discount report grouping.
-
.opportunity_report(options = {}) ⇒ Result
Runs the opportunity conversion report for a period and the same period one year earlier.
-
.report_grouping_clause(column, values) ⇒ Object
connection.quote escapes every SQL grammar quirk (not just single quotes like the old
sub("'", "''")pattern) and returns the SQL literal form.
Class Method Details
.converison_per_master_grouping(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) ⇒ Object
Converison per master grouping.
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 439 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 |
# File 'app/services/report/conversion_report/conversion_report.rb', line 404 def self.converison_per_master_grouping(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) cur_year = start_date.year prev_year = prev_start_date.year company_id = if country == 'combined' nil else (country == 'us' ? 1 : 2) end company_sql = country == 'combined' ? ' ' : (' and voc.company_id = ' << company_id.to_s << ' ') report_groupings_sql = report_grouping_clause('voc.report_grouping', report_groupings) sql = <<-SQL with table1 as ( select mg.master_grouping, sum(case when year = #{prev_year} then 1 else 0 end) as opps_created_prev, sum(case when year = #{cur_year} then 1 else 0 end) as opps_created_cur, sum(case when year = #{prev_year} and voc.state = 'won' then 1 else 0 end) as opps_won_prev, sum(case when year = #{cur_year} and voc.state = 'won' then 1 else 0 end) as opps_won_cur, count(distinct voc.report_grouping) as subgroups from view_opportunity_conversions voc inner join parties e on voc.primary_sales_rep_id = e.id and e.type = 'Employee' and e.inactive = false inner join analytic_date_time_dimensions dt on voc.created_at = dt.date left join report_grouping_master_groups mg on voc.report_grouping = mg.report_grouping where (voc.created_at between '#{prev_start_date}' and '#{prev_end_date}' or voc.created_at between '#{start_date}' and '#{end_date}') --and voc.days_conversion > 0 #{company_sql} #{report_groupings_sql} group by mg.master_grouping ), table2 as ( select mg.master_grouping,round(avg(days_conversion)::decimal,0) as avg_closing_time_prev from view_opportunity_conversions voc left join report_grouping_master_groups mg on voc.report_grouping = mg.report_grouping where created_at between '#{prev_start_date}' and '#{prev_end_date}' and state = 'won' --and days_conversion > 0 #{company_sql} #{report_groupings_sql} group by mg.master_grouping ), table3 as ( select mg.master_grouping,round(avg(days_conversion)::decimal,0) as avg_closing_time_cur from view_opportunity_conversions voc left join report_grouping_master_groups mg on voc.report_grouping = mg.report_grouping where created_at between '#{start_date}' and '#{end_date}' and state = 'won' --and days_conversion > 0 #{company_sql} #{report_groupings_sql} group by mg.master_grouping ) select t1.master_grouping,subgroups,opps_created_prev,opps_created_cur, case when opps_created_prev = 0 then 0 else round(((opps_created_cur::decimal - opps_created_prev::decimal) / opps_created_prev::decimal) * 100,0) end as opps_growth, opps_won_prev,opps_won_cur,(opps_won_cur - opps_won_prev) as opp_won_var, case when opps_created_prev = 0 then 0 else round((opps_won_prev::decimal / opps_created_prev::decimal) * 100,0) end as conversion_rate_prev, case when opps_created_cur = 0 then 0 else round((opps_won_cur::decimal / opps_created_cur::decimal) * 100,0) end as conversion_rate_cur, round(((case when opps_created_cur = 0 then 0 else (opps_won_cur::decimal / opps_created_cur::decimal) * 100 end) - (case when opps_created_prev = 0 then 0 else (opps_won_prev::decimal / opps_created_prev::decimal) * 100 end)),0) as conversion_rate_var, coalesce(avg_closing_time_prev,0) as avg_closing_time_prev,coalesce(avg_closing_time_cur,0) as avg_closing_time_cur, (coalesce(avg_closing_time_cur,0) - coalesce(avg_closing_time_prev,0)) as avg_closing_time_var from table1 t1 left join table2 t2 on t1.master_grouping = t2.master_grouping left join table3 t3 on t1.master_grouping = t3.master_grouping order by t1.opps_created_cur desc SQL ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.converison_per_product_line_group1(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) ⇒ Object
Converison per product line group1.
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 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 |
# File 'app/services/report/conversion_report/conversion_report.rb', line 705 def self.converison_per_product_line_group1(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) cur_year = start_date.year prev_year = prev_start_date.year company_id = if country == 'combined' nil else (country == 'us' ? 1 : 2) end company_sql = country == 'combined' ? ' ' : (' and voc.company_id = ' << company_id.to_s << ' ') report_groupings_sql = report_grouping_clause('voc.report_grouping', report_groupings) sql = <<-SQL with table1 as ( select voc.group1, sum(case when year = #{prev_year} then 1 else 0 end) as opps_created_prev, sum(case when year = #{cur_year} then 1 else 0 end) as opps_created_cur, sum(case when year = #{prev_year} and voc.state = 'won' then 1 else 0 end) as opps_won_prev, sum(case when year = #{cur_year} and voc.state = 'won' then 1 else 0 end) as opps_won_cur, count(distinct group2) as subgroups from view_opportunity_conversions voc inner join parties e on voc.primary_sales_rep_id = e.id and e.type = 'Employee' and e.inactive = false inner join analytic_date_time_dimensions dt on voc.created_at = dt.date where (voc.created_at between '#{prev_start_date}' and '#{prev_end_date}' or voc.created_at between '#{start_date}' and '#{end_date}') --and voc.days_conversion > 0 #{company_sql} #{report_groupings_sql} group by voc.group1 ), table2 as ( select group1,round(avg(days_conversion)::decimal,0) as avg_closing_time_prev from view_opportunity_conversions voc where created_at between '#{prev_start_date}' and '#{prev_end_date}' and state = 'won' --and days_conversion > 0 #{company_sql} #{report_groupings_sql} group by group1 ), table3 as ( select group1,round(avg(days_conversion)::decimal,0) as avg_closing_time_cur from view_opportunity_conversions voc where created_at between '#{start_date}' and '#{end_date}' and state = 'won' --and days_conversion > 0 #{company_sql} #{report_groupings_sql} group by group1 ) select t1.group1,subgroups, opps_created_prev,opps_created_cur, case when opps_created_prev = 0 then 0 else round(((opps_created_cur::decimal - opps_created_prev::decimal) / opps_created_prev::decimal) * 100,0) end as opps_growth, opps_won_prev,opps_won_cur, (opps_won_cur - opps_won_prev) as opp_won_var, case when opps_created_prev = 0 then 0 else round((opps_won_prev::decimal / opps_created_prev::decimal) * 100,0) end as conversion_rate_prev, case when opps_created_cur = 0 then 0 else round((opps_won_cur::decimal / opps_created_cur::decimal) * 100,0) end as conversion_rate_cur, round(((case when opps_created_cur = 0 then 0 else (opps_won_cur::decimal / opps_created_cur::decimal) * 100 end) - (case when opps_created_prev = 0 then 0 else (opps_won_prev::decimal / opps_created_prev::decimal) * 100 end)),0) as conversion_rate_var, coalesce(avg_closing_time_prev,0) as avg_closing_time_prev,coalesce(avg_closing_time_cur,0) as avg_closing_time_cur, (coalesce(avg_closing_time_cur,0) - coalesce(avg_closing_time_prev,0)) as avg_closing_time_var from table1 t1 left join table2 t2 on t1.group1 = t2.group1 left join table3 t3 on t1.group1 = t3.group1 order by t1.opps_created_cur desc SQL ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.converison_per_product_line_group2(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) ⇒ Object
Converison per product line group2.
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 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 |
# File 'app/services/report/conversion_report/conversion_report.rb', line 782 def self.converison_per_product_line_group2(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) cur_year = start_date.year prev_year = prev_start_date.year company_id = if country == 'combined' nil else (country == 'us' ? 1 : 2) end company_sql = country == 'combined' ? ' ' : (' and voc.company_id = ' << company_id.to_s << ' ') report_groupings_sql = report_grouping_clause('voc.report_grouping', report_groupings) sql = <<-SQL with table1 as ( select voc.group1,voc.group2, sum(case when year = #{prev_year} then 1 else 0 end) as opps_created_prev, sum(case when year = #{cur_year} then 1 else 0 end) as opps_created_cur, sum(case when year = #{prev_year} and voc.state = 'won' then 1 else 0 end) as opps_won_prev, sum(case when year = #{cur_year} and voc.state = 'won' then 1 else 0 end) as opps_won_cur, count(distinct group3) as subgroups from view_opportunity_conversions voc inner join parties e on voc.primary_sales_rep_id = e.id and e.type = 'Employee' and e.inactive = false inner join analytic_date_time_dimensions dt on voc.created_at = dt.date where (voc.created_at between '#{prev_start_date}' and '#{prev_end_date}' or voc.created_at between '#{start_date}' and '#{end_date}') --and voc.days_conversion > 0 #{company_sql} #{report_groupings_sql} group by voc.group1,voc.group2 ), table2 as ( select group1,group2,round(avg(days_conversion)::decimal,0) as avg_closing_time_prev from view_opportunity_conversions voc where created_at between '#{prev_start_date}' and '#{prev_end_date}' and state = 'won' --and days_conversion > 0 #{company_sql} #{report_groupings_sql} group by group1,group2 ), table3 as ( select group1,group2,round(avg(days_conversion)::decimal,0) as avg_closing_time_cur from view_opportunity_conversions voc where created_at between '#{start_date}' and '#{end_date}' and state = 'won' --and days_conversion > 0 #{company_sql} #{report_groupings_sql} group by group1,group2 ) select t1.group1,t1.group2,subgroups, opps_created_prev,opps_created_cur, case when opps_created_prev = 0 then 0 else round(((opps_created_cur::decimal - opps_created_prev::decimal) / opps_created_prev::decimal) * 100,0) end as opps_growth, opps_won_prev,opps_won_cur, (opps_won_cur - opps_won_prev) as opp_won_var, case when opps_created_prev = 0 then 0 else round((opps_won_prev::decimal / opps_created_prev::decimal) * 100,0) end as conversion_rate_prev, case when opps_created_cur = 0 then 0 else round((opps_won_cur::decimal / opps_created_cur::decimal) * 100,0) end as conversion_rate_cur, round(((case when opps_created_cur = 0 then 0 else (opps_won_cur::decimal / opps_created_cur::decimal) * 100 end) - (case when opps_created_prev = 0 then 0 else (opps_won_prev::decimal / opps_created_prev::decimal) * 100 end)),0) as conversion_rate_var, coalesce(avg_closing_time_prev,0) as avg_closing_time_prev,coalesce(avg_closing_time_cur,0) as avg_closing_time_cur, (coalesce(avg_closing_time_cur,0) - coalesce(avg_closing_time_prev,0)) as avg_closing_time_var from table1 t1 left join table2 t2 on t1.group1 = t2.group1 and t1.group2 = t2.group2 left join table3 t3 on t1.group1 = t3.group1 and t1.group2 = t3.group2 order by t1.opps_created_cur desc SQL ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.converison_per_product_line_group3(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) ⇒ Object
Converison per product line group3.
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 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 915 916 917 918 919 920 921 922 923 924 925 |
# File 'app/services/report/conversion_report/conversion_report.rb', line 859 def self.converison_per_product_line_group3(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) cur_year = start_date.year prev_year = prev_start_date.year company_id = if country == 'combined' nil else (country == 'us' ? 1 : 2) end company_sql = country == 'combined' ? ' ' : (' and voc.company_id = ' << company_id.to_s << ' ') report_groupings_sql = report_grouping_clause('voc.report_grouping', report_groupings) sql = <<-SQL with table1 as ( select voc.group1,voc.group2,voc.group3, sum(case when year = #{prev_year} then 1 else 0 end) as opps_created_prev, sum(case when year = #{cur_year} then 1 else 0 end) as opps_created_cur, sum(case when year = #{prev_year} and voc.state = 'won' then 1 else 0 end) as opps_won_prev, sum(case when year = #{cur_year} and voc.state = 'won' then 1 else 0 end) as opps_won_cur from view_opportunity_conversions voc inner join parties e on voc.primary_sales_rep_id = e.id and e.type = 'Employee' and e.inactive = false inner join analytic_date_time_dimensions dt on voc.created_at = dt.date where (voc.created_at between '#{prev_start_date}' and '#{prev_end_date}' or voc.created_at between '#{start_date}' and '#{end_date}') --and voc.days_conversion > 0 #{company_sql} #{report_groupings_sql} group by voc.group1,voc.group2,voc.group3 ), table2 as ( select group1,group2,group3,round(avg(days_conversion)::decimal,0) as avg_closing_time_prev from view_opportunity_conversions voc where created_at between '#{prev_start_date}' and '#{prev_end_date}' and state = 'won' --and days_conversion > 0 #{company_sql} #{report_groupings_sql} group by group1,group2,group3 ), table3 as ( select group1,group2,group3,round(avg(days_conversion)::decimal,0) as avg_closing_time_cur from view_opportunity_conversions voc where created_at between '#{start_date}' and '#{end_date}' and state = 'won' --and days_conversion > 0 #{company_sql} #{report_groupings_sql} group by group1,group2,group3 ) select t1.group1,t1.group2,t1.group3, opps_created_prev,opps_created_cur, case when opps_created_prev = 0 then 0 else round(((opps_created_cur::decimal - opps_created_prev::decimal) / opps_created_prev::decimal) * 100,0) end as opps_growth, opps_won_prev,opps_won_cur, (opps_won_cur - opps_won_prev) as opp_won_var, case when opps_created_prev = 0 then 0 else round((opps_won_prev::decimal / opps_created_prev::decimal) * 100,0) end as conversion_rate_prev, case when opps_created_cur = 0 then 0 else round((opps_won_cur::decimal / opps_created_cur::decimal) * 100,0) end as conversion_rate_cur, round(((case when opps_created_cur = 0 then 0 else (opps_won_cur::decimal / opps_created_cur::decimal) * 100 end) - (case when opps_created_prev = 0 then 0 else (opps_won_prev::decimal / opps_created_prev::decimal) * 100 end)),0) as conversion_rate_var, coalesce(avg_closing_time_prev,0) as avg_closing_time_prev,coalesce(avg_closing_time_cur,0) as avg_closing_time_cur, (coalesce(avg_closing_time_cur,0) - coalesce(avg_closing_time_prev,0)) as avg_closing_time_var from table1 t1 left join table2 t2 on t1.group1 = t2.group1 and t1.group2 = t2.group2 and t1.group3 = t2.group3 left join table3 t3 on t1.group1 = t3.group1 and t1.group2 = t3.group2 and t1.group3 = t3.group3 order by t1.opps_created_cur desc SQL ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.converison_per_product_line_total(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) ⇒ Object
Converison per product line total.
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 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 |
# File 'app/services/report/conversion_report/conversion_report.rb', line 636 def self.converison_per_product_line_total(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) cur_year = start_date.year prev_year = prev_start_date.year company_id = if country == 'combined' nil else (country == 'us' ? 1 : 2) end company_sql = country == 'combined' ? ' ' : (' and voc.company_id = ' << company_id.to_s << ' ') report_groupings_sql = report_grouping_clause('voc.report_grouping', report_groupings) sql = <<-SQL with table1 as ( select sum(case when year = #{prev_year} then 1 else 0 end) as opps_created_prev, sum(case when year = #{cur_year} then 1 else 0 end) as opps_created_cur, sum(case when year = #{prev_year} and voc.state = 'won' then 1 else 0 end) as opps_won_prev, sum(case when year = #{cur_year} and voc.state = 'won' then 1 else 0 end) as opps_won_cur from view_opportunity_conversions voc inner join parties e on voc.primary_sales_rep_id = e.id and e.type = 'Employee' and e.inactive = false inner join analytic_date_time_dimensions dt on voc.created_at = dt.date where (voc.created_at between '#{prev_start_date}' and '#{prev_end_date}' or voc.created_at between '#{start_date}' and '#{end_date}') --and voc.days_conversion > 0 #{company_sql} #{report_groupings_sql} ), table2 as ( select round(avg(days_conversion)::decimal,0) as avg_closing_time_prev from view_opportunity_conversions voc where created_at between '#{prev_start_date}' and '#{prev_end_date}' and state = 'won' --and days_conversion > 0 #{company_sql} #{report_groupings_sql} ), table3 as ( select round(avg(days_conversion)::decimal,0) as avg_closing_time_cur from view_opportunity_conversions voc where created_at between '#{start_date}' and '#{end_date}' and state = 'won' --and days_conversion > 0 #{company_sql} #{report_groupings_sql} ) select opps_created_prev,opps_created_cur, case when opps_created_prev = 0 then 0 else round(((opps_created_cur::decimal - opps_created_prev::decimal) / opps_created_prev::decimal) * 100,0) end as opps_growth, opps_won_prev,opps_won_cur, (opps_won_cur - opps_won_prev) as opp_won_var, case when opps_created_prev = 0 then 0 else round((opps_won_prev::decimal / opps_created_prev::decimal) * 100,0) end as conversion_rate_prev, case when opps_created_cur = 0 then 0 else round((opps_won_cur::decimal / opps_created_cur::decimal) * 100,0) end as conversion_rate_cur, round(((case when opps_created_cur = 0 then 0 else (opps_won_cur::decimal / opps_created_cur::decimal) * 100 end) - (case when opps_created_prev = 0 then 0 else (opps_won_prev::decimal / opps_created_prev::decimal) * 100 end)),0) as conversion_rate_var, coalesce(avg_closing_time_prev,0) as avg_closing_time_prev,coalesce(avg_closing_time_cur,0) as avg_closing_time_cur, (coalesce(avg_closing_time_cur,0) - coalesce(avg_closing_time_prev,0)) as avg_closing_time_var from table1 t1, table2 t2, table3 t3 order by t1.opps_created_cur desc SQL ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.converison_per_rep(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) ⇒ Object
Converison per rep.
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'app/services/report/conversion_report/conversion_report.rb', line 173 def self.converison_per_rep(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) cur_year = start_date.year prev_year = prev_start_date.year company_id = if country == 'combined' nil else (country == 'us' ? 1 : 2) end company_sql = country == 'combined' ? ' ' : (' and voc.company_id = ' << company_id.to_s << ' ') report_groupings_sql = report_grouping_clause('voc.report_grouping', report_groupings) sql = <<-SQL with table1 as ( select voc.primary_sales_rep_id,full_name, sum(case when year = #{prev_year} then 1 else 0 end) as opps_created_prev, sum(case when year = #{cur_year} then 1 else 0 end) as opps_created_cur, sum(case when year = #{prev_year} and voc.state = 'won' then 1 else 0 end) as opps_won_prev, sum(case when year = #{cur_year} and voc.state = 'won' then 1 else 0 end) as opps_won_cur from view_opportunity_conversions voc inner join parties e on voc.primary_sales_rep_id = e.id and e.type = 'Employee' and e.inactive = false inner join analytic_date_time_dimensions dt on voc.created_at = dt.date where (voc.created_at between '#{prev_start_date}' and '#{prev_end_date}' or voc.created_at between '#{start_date}' and '#{end_date}') and voc.primary_sales_rep_id not in (155,85,92) --and voc.days_conversion > 0 #{company_sql} #{report_groupings_sql} group by voc.primary_sales_rep_id,full_name ), table2 as ( select primary_sales_rep_id,round(avg(days_conversion)::decimal,0) as avg_closing_time_prev from view_opportunity_conversions voc where created_at between '#{prev_start_date}' and '#{prev_end_date}' and state = 'won' --and days_conversion > 0 #{company_sql} #{report_groupings_sql} group by primary_sales_rep_id ), table3 as ( select primary_sales_rep_id,round(avg(days_conversion)::decimal,0) as avg_closing_time_cur from view_opportunity_conversions voc where created_at between '#{start_date}' and '#{end_date}' and state = 'won' --and days_conversion > 0 #{company_sql} #{report_groupings_sql} group by primary_sales_rep_id ) select t1.primary_sales_rep_id,t1.full_name, opps_created_prev,opps_created_cur, case when opps_created_prev = 0 then 0 else round(((opps_created_cur::decimal - opps_created_prev::decimal) / opps_created_prev::decimal) * 100,0) end as opps_growth, opps_won_prev,opps_won_cur, (opps_won_cur - opps_won_prev) as opp_won_var, case when opps_created_prev = 0 then 0 else round((opps_won_prev::decimal / opps_created_prev::decimal) * 100,0) end as conversion_rate_prev, case when opps_created_cur = 0 then 0 else round((opps_won_cur::decimal / opps_created_cur::decimal) * 100,0) end as conversion_rate_cur, round(((case when opps_created_cur = 0 then 0 else (opps_won_cur::decimal / opps_created_cur::decimal) * 100 end) - (case when opps_created_prev = 0 then 0 else (opps_won_prev::decimal / opps_created_prev::decimal) * 100 end)),0) as conversion_rate_var, coalesce(avg_closing_time_prev,0) as avg_closing_time_prev,coalesce(avg_closing_time_cur,0) as avg_closing_time_cur, (coalesce(avg_closing_time_cur,0) - coalesce(avg_closing_time_prev,0)) as avg_closing_time_var from table1 t1 left join table2 t2 on t1.primary_sales_rep_id = t2.primary_sales_rep_id left join table3 t3 on t1.primary_sales_rep_id = t3.primary_sales_rep_id order by t1.opps_created_cur desc SQL ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.converison_per_rep_and_product_line(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) ⇒ Object
Converison per rep and product line.
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 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 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
# File 'app/services/report/conversion_report/conversion_report.rb', line 327 def self.converison_per_rep_and_product_line(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) cur_year = start_date.year prev_year = prev_start_date.year company_id = if country == 'combined' nil else (country == 'us' ? 1 : 2) end company_sql = country == 'combined' ? ' ' : (' and voc.company_id = ' << company_id.to_s << ' ') report_groupings_sql = report_grouping_clause('voc.report_grouping', report_groupings) sql = <<-SQL with table1 as ( select voc.primary_sales_rep_id,voc.group1, sum(case when year = #{prev_year} then 1 else 0 end) as opps_created_prev, sum(case when year = #{cur_year} then 1 else 0 end) as opps_created_cur, sum(case when year = #{prev_year} and voc.state = 'won' then 1 else 0 end) as opps_won_prev, sum(case when year = #{cur_year} and voc.state = 'won' then 1 else 0 end) as opps_won_cur from view_opportunity_conversions voc inner join parties e on voc.primary_sales_rep_id = e.id and e.type = 'Employee' and e.inactive = false inner join analytic_date_time_dimensions dt on voc.created_at = dt.date where (voc.created_at between '#{prev_start_date}' and '#{prev_end_date}' or voc.created_at between '#{start_date}' and '#{end_date}') and voc.primary_sales_rep_id not in (155,85,92) --and voc.days_conversion > 0 #{company_sql} #{report_groupings_sql} group by voc.primary_sales_rep_id,voc.group1 ), table2 as ( select primary_sales_rep_id,group1,round(avg(days_conversion)::decimal,0) as avg_closing_time_prev from view_opportunity_conversions voc where created_at between '#{prev_start_date}' and '#{prev_end_date}' and state = 'won' --and days_conversion > 0 #{company_sql} #{report_groupings_sql} group by primary_sales_rep_id,group1 ), table3 as ( select primary_sales_rep_id,group1,round(avg(days_conversion)::decimal,0) as avg_closing_time_cur from view_opportunity_conversions voc where created_at between '#{start_date}' and '#{end_date}' and state = 'won' --and days_conversion > 0 #{company_sql} #{report_groupings_sql} group by primary_sales_rep_id,group1 ) select t1.primary_sales_rep_id,t1.group1, opps_created_prev,opps_created_cur, case when opps_created_prev = 0 then 0 else round(((opps_created_cur::decimal - opps_created_prev::decimal) / opps_created_prev::decimal) * 100,0) end as opps_growth, opps_won_prev,opps_won_cur, (opps_won_cur - opps_won_prev) as opp_won_var, case when opps_created_prev = 0 then 0 else round((opps_won_prev::decimal / opps_created_prev::decimal) * 100,0) end as conversion_rate_prev, case when opps_created_cur = 0 then 0 else round((opps_won_cur::decimal / opps_created_cur::decimal) * 100,0) end as conversion_rate_cur, round(((case when opps_created_cur = 0 then 0 else (opps_won_cur::decimal / opps_created_cur::decimal) * 100 end) - (case when opps_created_prev = 0 then 0 else (opps_won_prev::decimal / opps_created_prev::decimal) * 100 end)),0) as conversion_rate_var, coalesce(avg_closing_time_prev,0) as avg_closing_time_prev,coalesce(avg_closing_time_cur,0) as avg_closing_time_cur, (coalesce(avg_closing_time_cur,0) - coalesce(avg_closing_time_prev,0)) as avg_closing_time_var from table1 t1 left join table2 t2 on t1.primary_sales_rep_id = t2.primary_sales_rep_id and t1.group1 = t2.group1 left join table3 t3 on t1.primary_sales_rep_id = t3.primary_sales_rep_id and t1.group1 = t3.group1 order by t1.opps_created_cur desc SQL ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.converison_per_rep_and_report_grouping(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) ⇒ Object
Converison per rep and report grouping.
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 |
# File 'app/services/report/conversion_report/conversion_report.rb', line 250 def self.converison_per_rep_and_report_grouping(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) cur_year = start_date.year prev_year = prev_start_date.year company_id = if country == 'combined' nil else (country == 'us' ? 1 : 2) end company_sql = country == 'combined' ? ' ' : (' and voc.company_id = ' << company_id.to_s << ' ') report_groupings_sql = report_grouping_clause('voc.report_grouping', report_groupings) sql = <<-SQL with table1 as ( select voc.primary_sales_rep_id,voc.report_grouping, sum(case when year = #{prev_year} then 1 else 0 end) as opps_created_prev, sum(case when year = #{cur_year} then 1 else 0 end) as opps_created_cur, sum(case when year = #{prev_year} and voc.state = 'won' then 1 else 0 end) as opps_won_prev, sum(case when year = #{cur_year} and voc.state = 'won' then 1 else 0 end) as opps_won_cur from view_opportunity_conversions voc inner join parties e on voc.primary_sales_rep_id = e.id and e.type = 'Employee' and e.inactive = false inner join analytic_date_time_dimensions dt on voc.created_at = dt.date where (voc.created_at between '#{prev_start_date}' and '#{prev_end_date}' or voc.created_at between '#{start_date}' and '#{end_date}') and voc.primary_sales_rep_id not in (155,85,92) --and voc.days_conversion > 0 #{company_sql} #{report_groupings_sql} group by voc.primary_sales_rep_id,voc.report_grouping ), table2 as ( select primary_sales_rep_id,report_grouping,round(avg(days_conversion)::decimal,0) as avg_closing_time_prev from view_opportunity_conversions voc where created_at between '#{prev_start_date}' and '#{prev_end_date}' and state = 'won' --and days_conversion > 0 #{company_sql} #{report_groupings_sql} group by primary_sales_rep_id,report_grouping ), table3 as ( select primary_sales_rep_id,report_grouping,round(avg(days_conversion)::decimal,0) as avg_closing_time_cur from view_opportunity_conversions voc where created_at between '#{start_date}' and '#{end_date}' and state = 'won' --and days_conversion > 0 #{company_sql} #{report_groupings_sql} group by primary_sales_rep_id,report_grouping ) select t1.primary_sales_rep_id,t1.report_grouping, opps_created_prev,opps_created_cur, case when opps_created_prev = 0 then 0 else round(((opps_created_cur::decimal - opps_created_prev::decimal) / opps_created_prev::decimal) * 100,0) end as opps_growth, opps_won_prev,opps_won_cur, (opps_won_cur - opps_won_prev) as opp_won_var, case when opps_created_prev = 0 then 0 else round((opps_won_prev::decimal / opps_created_prev::decimal) * 100,0) end as conversion_rate_prev, case when opps_created_cur = 0 then 0 else round((opps_won_cur::decimal / opps_created_cur::decimal) * 100,0) end as conversion_rate_cur, round(((case when opps_created_cur = 0 then 0 else (opps_won_cur::decimal / opps_created_cur::decimal) * 100 end) - (case when opps_created_prev = 0 then 0 else (opps_won_prev::decimal / opps_created_prev::decimal) * 100 end)),0) as conversion_rate_var, coalesce(avg_closing_time_prev,0) as avg_closing_time_prev,coalesce(avg_closing_time_cur,0) as avg_closing_time_cur, (coalesce(avg_closing_time_cur,0) - coalesce(avg_closing_time_prev,0)) as avg_closing_time_var from table1 t1 left join table2 t2 on t1.primary_sales_rep_id = t2.primary_sales_rep_id and t1.report_grouping = t2.report_grouping left join table3 t3 on t1.primary_sales_rep_id = t3.primary_sales_rep_id and t1.report_grouping = t3.report_grouping order by t1.opps_created_cur desc SQL ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.converison_per_rep_total(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) ⇒ Object
Converison per rep total.
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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'app/services/report/conversion_report/conversion_report.rb', line 103 def self.converison_per_rep_total(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) cur_year = start_date.year prev_year = prev_start_date.year company_id = if country == 'combined' nil else (country == 'us' ? 1 : 2) end company_sql = country == 'combined' ? ' ' : (' and voc.company_id = ' << company_id.to_s << ' ') report_groupings_sql = report_grouping_clause('voc.report_grouping', report_groupings) sql = <<-SQL with table1 as ( select sum(case when year = #{prev_year} then 1 else 0 end) as opps_created_prev, sum(case when year = #{cur_year} then 1 else 0 end) as opps_created_cur, sum(case when year = #{prev_year} and voc.state = 'won' then 1 else 0 end) as opps_won_prev, sum(case when year = #{cur_year} and voc.state = 'won' then 1 else 0 end) as opps_won_cur from view_opportunity_conversions voc inner join parties e on voc.primary_sales_rep_id = e.id and e.type = 'Employee' and e.inactive = false inner join analytic_date_time_dimensions dt on voc.created_at = dt.date where (voc.created_at between '#{prev_start_date}' and '#{prev_end_date}' or voc.created_at between '#{start_date}' and '#{end_date}') and voc.primary_sales_rep_id not in (155,85,92) --and voc.days_conversion > 0 #{company_sql} #{report_groupings_sql} ), table2 as ( select round(avg(days_conversion)::decimal,0) as avg_closing_time_prev from view_opportunity_conversions voc where created_at between '#{prev_start_date}' and '#{prev_end_date}' and state = 'won' --and days_conversion > 0 #{company_sql} #{report_groupings_sql} ), table3 as ( select round(avg(days_conversion)::decimal,0) as avg_closing_time_cur from view_opportunity_conversions voc where created_at between '#{start_date}' and '#{end_date}' and state = 'won' --and days_conversion > 0 #{company_sql} #{report_groupings_sql} ) select opps_created_prev,opps_created_cur, case when opps_created_prev = 0 then 0 else round(((opps_created_cur::decimal - opps_created_prev::decimal) / opps_created_prev::decimal) * 100,0) end as opps_growth, opps_won_prev,opps_won_cur, (opps_won_cur - opps_won_prev) as opp_won_var, case when opps_created_prev = 0 then 0 else round((opps_won_prev::decimal / opps_created_prev::decimal) * 100,0) end as conversion_rate_prev, case when opps_created_cur = 0 then 0 else round((opps_won_cur::decimal / opps_created_cur::decimal) * 100,0) end as conversion_rate_cur, round(((case when opps_created_cur = 0 then 0 else (opps_won_cur::decimal / opps_created_cur::decimal) * 100 end) - (case when opps_created_prev = 0 then 0 else (opps_won_prev::decimal / opps_created_prev::decimal) * 100 end)),0) as conversion_rate_var, coalesce(avg_closing_time_prev,0) as avg_closing_time_prev,coalesce(avg_closing_time_cur,0) as avg_closing_time_cur, (coalesce(avg_closing_time_cur,0) - coalesce(avg_closing_time_prev,0)) as avg_closing_time_var from table1 t1,table2 t2,table3 t3 order by t1.opps_created_cur desc SQL ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.converison_per_report_grouping(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) ⇒ Object
Converison per report grouping.
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 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 |
# File 'app/services/report/conversion_report/conversion_report.rb', line 482 def self.converison_per_report_grouping(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) cur_year = start_date.year prev_year = prev_start_date.year company_id = if country == 'combined' nil else (country == 'us' ? 1 : 2) end company_sql = country == 'combined' ? ' ' : (' and voc.company_id = ' << company_id.to_s << ' ') report_groupings_sql = report_grouping_clause('voc.report_grouping', report_groupings) sql = <<-SQL with table1 as ( select mg.master_grouping,voc.report_grouping, sum(case when year = #{prev_year} then 1 else 0 end) as opps_created_prev, sum(case when year = #{cur_year} then 1 else 0 end) as opps_created_cur, sum(case when year = #{prev_year} and voc.state = 'won' then 1 else 0 end) as opps_won_prev, sum(case when year = #{cur_year} and voc.state = 'won' then 1 else 0 end) as opps_won_cur, count(distinct group1) as subgroups from view_opportunity_conversions voc inner join parties e on voc.primary_sales_rep_id = e.id and e.type = 'Employee' and e.inactive = false inner join analytic_date_time_dimensions dt on voc.created_at = dt.date left join report_grouping_master_groups mg on voc.report_grouping = mg.report_grouping where (voc.created_at between '#{prev_start_date}' and '#{prev_end_date}' or voc.created_at between '#{start_date}' and '#{end_date}') --and voc.days_conversion > 0 #{company_sql} #{report_groupings_sql} group by mg.master_grouping,voc.report_grouping ), table2 as ( select mg.master_grouping,voc.report_grouping,round(avg(days_conversion)::decimal,0) as avg_closing_time_prev from view_opportunity_conversions voc left join report_grouping_master_groups mg on voc.report_grouping = mg.report_grouping where created_at between '#{prev_start_date}' and '#{prev_end_date}' and state = 'won' --and days_conversion > 0 #{company_sql} #{report_groupings_sql} group by mg.master_grouping,voc.report_grouping ), table3 as ( select mg.master_grouping,voc.report_grouping,round(avg(days_conversion)::decimal,0) as avg_closing_time_cur from view_opportunity_conversions voc left join report_grouping_master_groups mg on voc.report_grouping = mg.report_grouping where created_at between '#{start_date}' and '#{end_date}' and state = 'won' --and days_conversion > 0 #{company_sql} #{report_groupings_sql} group by mg.master_grouping,voc.report_grouping ) select t1.master_grouping,t1.report_grouping,subgroups,opps_created_prev,opps_created_cur, case when opps_created_prev = 0 then 0 else round(((opps_created_cur::decimal - opps_created_prev::decimal) / opps_created_prev::decimal) * 100,0) end as opps_growth, opps_won_prev,opps_won_cur,(opps_won_cur - opps_won_prev) as opp_won_var, case when opps_created_prev = 0 then 0 else round((opps_won_prev::decimal / opps_created_prev::decimal) * 100,0) end as conversion_rate_prev, case when opps_created_cur = 0 then 0 else round((opps_won_cur::decimal / opps_created_cur::decimal) * 100,0) end as conversion_rate_cur, round(((case when opps_created_cur = 0 then 0 else (opps_won_cur::decimal / opps_created_cur::decimal) * 100 end) - (case when opps_created_prev = 0 then 0 else (opps_won_prev::decimal / opps_created_prev::decimal) * 100 end)),0) as conversion_rate_var, coalesce(avg_closing_time_prev,0) as avg_closing_time_prev,coalesce(avg_closing_time_cur,0) as avg_closing_time_cur, (coalesce(avg_closing_time_cur,0) - coalesce(avg_closing_time_prev,0)) as avg_closing_time_var from table1 t1 left join table2 t2 on t1.master_grouping = t2.master_grouping and t1.report_grouping = t2.report_grouping left join table3 t3 on t1.master_grouping = t3.master_grouping and t1.report_grouping = t3.report_grouping order by t1.opps_created_cur desc SQL ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.converison_per_report_grouping_and_product_line(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) ⇒ Object
Converison per report grouping and product line.
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 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/conversion_report/conversion_report.rb', line 560 def self.converison_per_report_grouping_and_product_line(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) cur_year = start_date.year prev_year = prev_start_date.year company_id = if country == 'combined' nil else (country == 'us' ? 1 : 2) end company_sql = country == 'combined' ? ' ' : (' and voc.company_id = ' << company_id.to_s << ' ') report_groupings_sql = report_grouping_clause('voc.report_grouping', report_groupings) sql = <<-SQL with table1 as ( select voc.report_grouping,voc.group1, sum(case when year = #{prev_year} then 1 else 0 end) as opps_created_prev, sum(case when year = #{cur_year} then 1 else 0 end) as opps_created_cur, sum(case when year = #{prev_year} and voc.state = 'won' then 1 else 0 end) as opps_won_prev, sum(case when year = #{cur_year} and voc.state = 'won' then 1 else 0 end) as opps_won_cur from view_opportunity_conversions voc inner join parties e on voc.primary_sales_rep_id = e.id and e.type = 'Employee' and e.inactive = false inner join analytic_date_time_dimensions dt on voc.created_at = dt.date where (voc.created_at between '#{prev_start_date}' and '#{prev_end_date}' or voc.created_at between '#{start_date}' and '#{end_date}') --and voc.days_conversion > 0 #{company_sql} #{report_groupings_sql} group by voc.report_grouping,voc.group1 ), table2 as ( select report_grouping,group1,round(avg(days_conversion)::decimal,0) as avg_closing_time_prev from view_opportunity_conversions voc where created_at between '#{prev_start_date}' and '#{prev_end_date}' and state = 'won' --and days_conversion > 0 #{company_sql} #{report_groupings_sql} group by report_grouping,group1 ), table3 as ( select report_grouping,group1,round(avg(days_conversion)::decimal,0) as avg_closing_time_cur from view_opportunity_conversions voc where created_at between '#{start_date}' and '#{end_date}' and state = 'won' --and days_conversion > 0 #{company_sql} #{report_groupings_sql} group by report_grouping,group1 ) select t1.report_grouping,t1.group1, opps_created_prev,opps_created_cur, case when opps_created_prev = 0 then 0 else round(((opps_created_cur::decimal - opps_created_prev::decimal) / opps_created_prev::decimal) * 100,0) end as opps_growth, opps_won_prev,opps_won_cur, (opps_won_cur - opps_won_prev) as opp_won_var, case when opps_created_prev = 0 then 0 else round((opps_won_prev::decimal / opps_created_prev::decimal) * 100,0) end as conversion_rate_prev, case when opps_created_cur = 0 then 0 else round((opps_won_cur::decimal / opps_created_cur::decimal) * 100,0) end as conversion_rate_cur, round(((case when opps_created_cur = 0 then 0 else (opps_won_cur::decimal / opps_created_cur::decimal) * 100 end) - (case when opps_created_prev = 0 then 0 else (opps_won_prev::decimal / opps_created_prev::decimal) * 100 end)),0) as conversion_rate_var, coalesce(avg_closing_time_prev,0) as avg_closing_time_prev,coalesce(avg_closing_time_cur,0) as avg_closing_time_cur, (coalesce(avg_closing_time_cur,0) - coalesce(avg_closing_time_prev,0)) as avg_closing_time_var from table1 t1 left join table2 t2 on t1.report_grouping = t2.report_grouping and t1.group1 = t2.group1 left join table3 t3 on t1.report_grouping = t3.report_grouping and t1.group1 = t3.group1 order by t1.opps_created_cur desc SQL ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.conversion_per_discount(start_date, end_date, country, report_groupings) ⇒ Object
Conversion per discount.
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 |
# File 'app/services/report/conversion_report/conversion_report.rb', line 933 def self.conversion_per_discount(start_date, end_date, country, report_groupings) company_id = if country == 'combined' nil else (country == 'us' ? 1 : 2) end company_sql = country == 'combined' ? ' ' : (' and company_id = ' << company_id.to_s << ' ') report_groupings_sql = report_grouping_clause('report_grouping', report_groupings) sql = <<-SQL.squish with opps as ( select distinct opportunity_id,state,discount_numeric_range,discount_name_range from view_opportunity_conversions where created_at between '#{start_date}' and '#{end_date}' #{company_sql} #{report_groupings_sql} ) select opps.discount_numeric_range,opps.discount_name_range,count(*) as total_opportunities, sum(case when opps.state = 'won' then 1 else 0 end) as won_opportunities, case when count(*) = 0 then 0 else (sum(case when opps.state = 'won' then 1 else 0 end)::numeric / count(*)::numeric) * 100 end as conversion_rate from opps group by opps.discount_numeric_range,opps.discount_name_range order by opps.discount_numeric_range; SQL ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.conversion_per_discount_report_grouping(start_date, end_date, country, report_groupings) ⇒ Object
Conversion per discount report grouping.
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 |
# File 'app/services/report/conversion_report/conversion_report.rb', line 968 def self.conversion_per_discount_report_grouping(start_date, end_date, country, report_groupings) company_id = if country == 'combined' nil else (country == 'us' ? 1 : 2) end company_sql = country == 'combined' ? ' ' : (' and company_id = ' << company_id.to_s << ' ') report_groupings_sql = report_grouping_clause('report_grouping', report_groupings) sql = <<-SQL.squish with opps_rg as ( select distinct opportunity_id,report_grouping,state,discount_numeric_range,discount_name_range from view_opportunity_conversions where created_at between '#{start_date}' and '#{end_date}' #{company_sql} #{report_groupings_sql} ) select discount_numeric_range,discount_name_range,opps_rg.report_grouping, count(*) as total_opportunities, sum(case when state = 'won' then 1 else 0 end) as won_opportunities, case when count(*) = 0 then 0 else (sum(case when state = 'won' then 1 else 0 end)::numeric / count(*)::numeric) * 100 end as conversion_rate from opps_rg group by discount_numeric_range,discount_name_range,opps_rg.report_grouping order by discount_numeric_range,conversion_rate; SQL ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.opportunity_report(options = {}) ⇒ Result
Runs the opportunity conversion report for a period and the same period one year earlier.
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 |
# File 'app/services/report/conversion_report/conversion_report.rb', line 52 def self.opportunity_report( = {}) start_date = [:period1_gteq] end_date = [:period1_lteq] prev_start_date = start_date.years_ago(1) prev_end_date = end_date.years_ago(1) country = [:country] report_groupings = [:report_groupings].filter_map(&:presence) data_converison_per_rep_total = converison_per_rep_total(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) data_converison_per_rep = converison_per_rep(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) data_converison_per_rep_and_report_grouping = converison_per_rep_and_report_grouping(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) data_converison_per_rep_and_product_line = converison_per_rep_and_product_line(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) data_converison_per_master_grouping = converison_per_master_grouping(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) data_converison_per_report_grouping = converison_per_report_grouping(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) data_converison_per_report_grouping_and_product_line = converison_per_report_grouping_and_product_line(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) data_converison_per_product_line_total = converison_per_product_line_total(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) data_converison_per_product_line_group1 = converison_per_product_line_group1(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) data_converison_per_product_line_group2 = converison_per_product_line_group2(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) data_converison_per_product_line_group3 = converison_per_product_line_group3(start_date, end_date, prev_start_date, prev_end_date, country, report_groupings) conversion_per_discount = conversion_per_discount(start_date, end_date, country, report_groupings) conversion_per_discount_report_grouping = conversion_per_discount_report_grouping(start_date, end_date, country, report_groupings) Result.new(success: true, cur_year: start_date.year, prev_year: prev_start_date.year, data_converison_per_rep_total: data_converison_per_rep_total, data_converison_per_rep: data_converison_per_rep, data_converison_per_rep_and_report_grouping: data_converison_per_rep_and_report_grouping, data_converison_per_rep_and_product_line: data_converison_per_rep_and_product_line, data_converison_per_master_grouping: data_converison_per_master_grouping, data_converison_per_report_grouping: data_converison_per_report_grouping, data_converison_per_report_grouping_and_product_line: data_converison_per_report_grouping_and_product_line, data_converison_per_product_line_total: data_converison_per_product_line_total, data_converison_per_product_line_group1: data_converison_per_product_line_group1, data_converison_per_product_line_group2: data_converison_per_product_line_group2, data_converison_per_product_line_group3: data_converison_per_product_line_group3, conversion_per_discount: conversion_per_discount, conversion_per_discount_report_grouping: conversion_per_discount_report_grouping) end |
.report_grouping_clause(column, values) ⇒ Object
connection.quote escapes every SQL grammar quirk (not just single quotes
like the old sub("'", "''") pattern) and returns the SQL literal form.
38 39 40 41 42 43 |
# File 'app/services/report/conversion_report/conversion_report.rb', line 38 def self.report_grouping_clause(column, values) return '' if values.blank? quoted = Array(values).map { |v| ActiveRecord::Base.lease_connection.quote(v) } " and #{column} in (#{quoted.join(',')})" end |