Class: Report::SmartServices::SmartServices
- Inherits:
-
Object
- Object
- Report::SmartServices::SmartServices
- Defined in:
- app/models/report/smart_services/smart_services.rb
Overview
Helper class scoped to Report::SmartServices.
Defined Under Namespace
Classes: Result
Class Method Summary collapse
- .get_data_grouping(cur_start_date, cur_end_date, sales_rep_ids, type) ⇒ Object
- .get_montly_sales(cur_start_date, cur_end_date, sales_rep_ids) ⇒ Object
- .get_totals(data) ⇒ Object
- .quick_view_by_product_line(cur_start_date, cur_end_date, prev_start_date, prev_end_date) ⇒ Object
- .quick_view_customer_growth(cur_start_date, cur_end_date, prev_start_date, prev_end_date) ⇒ Object
- .quick_view_sales(cur_start_date, cur_end_date, prev_start_date, prev_end_date) ⇒ Object
- .result(options = {}) ⇒ Object
- .smart_services_data(cur_start_date, cur_end_date, prev_start_date, prev_end_date, sales_rep_ids) ⇒ Object
- .smart_services_data_breakdown(cur_start_date, cur_end_date, prev_start_date, prev_end_date, sales_rep_ids) ⇒ Object
- .smart_services_data_breakdown_by_rep(cur_start_date, cur_end_date, prev_start_date, prev_end_date, sales_rep_ids) ⇒ Object
- .smart_services_data_by_rep(cur_start_date, cur_end_date, prev_start_date, prev_end_date, sales_rep_ids) ⇒ Object
Class Method Details
.get_data_grouping(cur_start_date, cur_end_date, sales_rep_ids, type) ⇒ Object
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 |
# File 'app/models/report/smart_services/smart_services.rb', line 393 def self.get_data_grouping(cur_start_date, cur_end_date, sales_rep_ids, type) sales_rep_sql = " and primary_sales_rep_id in (#{sales_rep_ids.map { |id| Integer(id) }.join(',')})" if sales_rep_ids.present? detail_sql1 = type == 1 ? " report_grouping as detail, " : " product_line_name as detail," detail_sql2 = type == 1 ? " report_grouping as detail, " : " (case when i.primary_product_line_id = 92 then 'SmartFit' when i.primary_product_line_id = 229 then 'SmartInstall' when i.primary_product_line_id = 230 then 'SmartGuide' when i.primary_product_line_id = 254 then 'SmartFix' else '' end) as detail," sql = <<-SQL.squish select detail, sum(quantity * discounted_price * consolidated_exchange_rate) as amount from ( select #{detail_sql1} quantity,discounted_price,consolidated_exchange_rate from view_smart_services_sales where gl_date between '#{cur_start_date}' and '#{cur_end_date}' #{sales_rep_sql} union all select #{detail_sql2} quantity,discounted_price,consolidated_exchange_rate from line_items li inner join invoices iv on li.resource_id = iv.id inner join items i on li.item_id = i.id where resource_type = 'Invoice' and resource_id in (141050,144784,141449,141352,142340,142569,142611,147018,147273,149059,149517,148942,148941,144780,144779,145722,149372,146945,147086,147087,145777,146946,146947,146948,147019,147020,147090,147316,149045,149726,152553,153644,153645,153648) and item_id is null and gl_date between '#{cur_start_date}' and '#{cur_end_date}' #{sales_rep_sql} )a group by detail; SQL ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.get_montly_sales(cur_start_date, cur_end_date, sales_rep_ids) ⇒ Object
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 |
# File 'app/models/report/smart_services/smart_services.rb', line 428 def self.get_montly_sales(cur_start_date, cur_end_date, sales_rep_ids) sales_rep_sql = " and primary_sales_rep_id in (#{sales_rep_ids.map { |id| Integer(id) }.join(',')})" if sales_rep_ids.present? sql = <<-SQL.squish with dates as ( select month,min(date) as date,product_line_name from analytic_date_time_dimensions,(select 'SmartFit' as product_line_name union all select 'SmartInstall' union all select 'SmartGuide' union all select 'SmartFix')a where date between '#{cur_start_date}' and '#{cur_end_date}' group by month,product_line_name ), smart_service_sales_data as ( select gl_date,month,report_grouping,product_line_id,product_line_name,sum(quantity * discounted_price * consolidated_exchange_rate) as amount from view_smart_services_sales where gl_date between '#{cur_start_date}' and '#{cur_end_date}' #{sales_rep_sql} group by gl_date,month,report_grouping,product_line_id,product_line_name union all select gl_date,month,report_grouping,i.primary_product_line_id, (case when i.primary_product_line_id = 92 then 'SmartFit' when i.primary_product_line_id = 229 then 'SmartInstall' when i.primary_product_line_id = 230 then 'SmartGuide' when i.primary_product_line_id = 254 then 'SmartFix' else '' end), sum(quantity * discounted_price * consolidated_exchange_rate) as amount from line_items li inner join invoices iv on li.resource_id = iv.id inner join items i on li.item_id = i.id inner join analytic_date_time_dimensions dt on dt.date = gl_date where resource_type = 'Invoice' and resource_id in (141050,144784,141449,141352,142340,142569,142611,147018,147273,149059,149517,148942,148941,144780,144779,145722,149372,146945,147086,147087,145777,146946,146947,146948,147019,147020,147090,147316,149045,149726,152553,153644,153645,153648) and item_id is null and gl_date between '#{cur_start_date}' and '#{cur_end_date}' #{sales_rep_sql} group by gl_date,month,report_grouping,i.primary_product_line_id ) select d.month,d.date, coalesce(sum(case when d.product_line_name = 'SmartFit' then amount else 0 end),0) as smart_fit, coalesce(sum(case when d.product_line_name = 'SmartFix' then amount else 0 end),0) as smart_fix, coalesce(sum(case when d.product_line_name = 'SmartGuide' then amount else 0 end),0) as smart_guide, coalesce(sum(case when d.product_line_name = 'SmartInstall' then amount else 0 end),0) as smart_install from dates d left join smart_service_sales_data ss on d.month = ss.month and d.product_line_name = ss.product_line_name group by d.month,d.date; SQL ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.get_totals(data) ⇒ Object
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 |
# File 'app/models/report/smart_services/smart_services.rb', line 341 def self.get_totals(data) ttlp_sf_qty = 0 ttlp_sg_qty = 0 ttlp_si_qty = 0 ttlp_sx_qty = 0 ttlp_qty = 0 ttlp_sf_val = 0 ttlp_sg_val = 0 ttlp_si_val = 0 ttlp_sx_val = 0 ttlc_qty = 0 ttlc_sf_qty = 0 ttlc_sg_qty = 0 ttlc_si_qty = 0 ttlc_sx_qty = 0 ttlp_val = 0 ttlc_sf_val = 0 ttlc_sg_val = 0 ttlc_si_val = 0 ttlc_sx_val = 0 ttlc_val = 0 data.each do |r| ttlp_sf_qty += r[:prev_sf_qty].to_i ttlc_sf_qty += r[:cur_sf_qty].to_i ttlp_sg_qty += r[:prev_sg_qty].to_i ttlc_sg_qty += r[:cur_sg_qty].to_i ttlp_si_qty += r[:prev_si_qty].to_i ttlc_si_qty += r[:cur_si_qty].to_i ttlp_sx_qty += r[:prev_sx_qty].to_i ttlc_sx_qty += r[:cur_sx_qty].to_i ttlp_qty += r[:prev_qty].to_i ttlc_qty += r[:cur_qty].to_i ttlp_sf_val += r[:prev_sf_val].to_f ttlc_sf_val += r[:cur_sf_val].to_f ttlp_sg_val += r[:prev_sg_val].to_f ttlc_sg_val += r[:cur_sg_val].to_f ttlp_si_val += r[:prev_si_val].to_f ttlc_si_val += r[:cur_si_val].to_f ttlp_sx_val += r[:prev_sx_val].to_f ttlc_sx_val += r[:cur_sx_val].to_f ttlp_val += r[:prev_val].to_f ttlc_val += r[:cur_val].to_f end { ttlp_sf_qty: ttlp_sf_qty, ttlp_sg_qty: ttlp_sg_qty, ttlp_si_qty: ttlp_si_qty, ttlp_sx_qty: ttlp_sx_qty, ttlp_sf_val: ttlp_sf_val, ttlp_sg_val: ttlp_sg_val, ttlp_si_val: ttlp_si_val, ttlp_sx_val: ttlp_sx_val, ttlp_qty: ttlp_qty, ttlp_val: ttlp_val, ttlc_sf_qty: ttlc_sf_qty, ttlc_sg_qty: ttlc_sg_qty, ttlc_si_qty: ttlc_si_qty, ttlc_sx_qty: ttlc_sx_qty, ttlc_sf_val: ttlc_sf_val, ttlc_sg_val: ttlc_sg_val, ttlc_si_val: ttlc_si_val, ttlc_sx_val: ttlc_sx_val, ttlc_qty: ttlc_qty, ttlc_val: ttlc_val } end |
.quick_view_by_product_line(cur_start_date, cur_end_date, prev_start_date, prev_end_date) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'app/models/report/smart_services/smart_services.rb', line 106 def self.quick_view_by_product_line(cur_start_date, cur_end_date, prev_start_date, prev_end_date) sql = <<-SQL.squish with sales as ( select product_line_name, sum(case when gl_date between '#{cur_start_date}' and '#{cur_end_date}' then (quantity * consolidated_exchange_rate * discounted_price) else 0 end) as cur_sales, sum(case when gl_date between '#{prev_start_date}' and '#{prev_end_date}' then (quantity * consolidated_exchange_rate * discounted_price) else 0 end) as prev_sales from view_smart_services_sales vss inner join analytic_date_time_dimensions dt on vss.gl_date = dt.date where (gl_date between '#{prev_start_date}' and '#{prev_end_date}' or gl_date between '#{cur_start_date}' and '#{cur_end_date}') group by product_line_name order by 1,2 ), total as ( select product_line_name,(cur_sales / 1000) as cur_sales,(prev_sales / 1000) as prev_sales,((cur_sales - prev_sales) / 1000) as sales_var, case when prev_sales = 0 then 0 else ((cur_sales / prev_sales) - 1) * 100 end as growth from sales ) select product_line_name,cur_sales,prev_sales,sales_var,growth from total; SQL ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.quick_view_customer_growth(cur_start_date, cur_end_date, prev_start_date, prev_end_date) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'app/models/report/smart_services/smart_services.rb', line 88 def self.quick_view_customer_growth(cur_start_date, cur_end_date, prev_start_date, prev_end_date) sql = <<-SQL.squish select p.full_name,vss.customer_id, sum(case when gl_date between '#{cur_start_date}' and '#{cur_end_date}' then quantity * discounted_price * consolidated_exchange_rate else 0 end ) as cur_sales, sum(case when gl_date between '#{prev_start_date}' and '#{prev_end_date}' then quantity * discounted_price * consolidated_exchange_rate else 0 end ) as prev_sales, sum(case when gl_date between '#{cur_start_date}' and '#{cur_end_date}' then quantity * discounted_price * consolidated_exchange_rate else 0 end ) - sum(case when gl_date between '#{prev_start_date}' and '#{prev_end_date}' then quantity * discounted_price * consolidated_exchange_rate else 0 end ) as sales_var, case when sum(case when gl_date between '#{prev_start_date}' and '#{prev_end_date}' then quantity * discounted_price * consolidated_exchange_rate else 0 end) = 0 then 0 else (sum(case when gl_date between '#{cur_start_date}' and '#{cur_end_date}' then quantity * discounted_price * consolidated_exchange_rate else 0 end) / sum(case when gl_date between '#{prev_start_date}' and '#{prev_end_date}' then quantity * discounted_price * consolidated_exchange_rate else 0 end) - 1) * 100 end as growth from view_smart_services_sales vss inner join parties p on vss.customer_id = p.id where (gl_date between '#{prev_start_date}' and '#{prev_end_date}' or gl_date between '#{cur_start_date}' and '#{cur_end_date}') group by p.full_name,vss.customer_id order by 5 desc; SQL ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.quick_view_sales(cur_start_date, cur_end_date, prev_start_date, prev_end_date) ⇒ Object
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 |
# File 'app/models/report/smart_services/smart_services.rb', line 53 def self.quick_view_sales(cur_start_date, cur_end_date, prev_start_date, prev_end_date) sql = <<-SQL.squish with sales as ( select sum(case when gl_date between '#{cur_start_date}' and '#{cur_end_date}' then quantity * discounted_price * consolidated_exchange_rate else 0 end ) as cur_sales, sum(case when gl_date between '#{prev_start_date}' and '#{prev_end_date}' then quantity * discounted_price * consolidated_exchange_rate else 0 end ) as prev_sales from view_smart_services_sales where (gl_date between '#{prev_start_date}' and '#{prev_end_date}' or gl_date between '#{cur_start_date}' and '#{cur_end_date}') union all select sum(case when gl_date between '#{cur_start_date}' and '#{cur_end_date}' then quantity * discounted_price * consolidated_exchange_rate else 0 end ) as cur_sales, sum(case when gl_date between '#{prev_start_date}' and '#{prev_end_date}' then quantity * discounted_price * consolidated_exchange_rate else 0 end ) as prev_sales from line_items li inner join invoices iv on li.resource_id = iv.id where resource_type = 'Invoice' and resource_id in (141050,144784,141449,141352,142340,142569,142611,147018,147273,149059,149517,148942,148941,144780,144779,145722,149372,146945,147086,147087,145777,146946,146947,146948,147019,147020,147090,147316,149045,149726,152553,153644,153645,153648) and item_id is null and (gl_date between '#{prev_start_date}' and '#{prev_end_date}' or gl_date between '#{cur_start_date}' and '#{cur_end_date}') ), sales_data as ( select sum(cur_sales) as cur_sales,sum(prev_sales) as prev_sales from sales ), total as ( select cur_sales,prev_sales, (cur_sales - prev_sales) as sales_var, (case when prev_sales = 0 then 0 else ((cur_sales / prev_sales) - 1) * 100 end ) as growth from sales_data ) select coalesce(cur_sales,0) as cur_sales,coalesce(prev_sales,0) as prev_sales,coalesce(sales_var,0) as sales_var,coalesce(growth,0) as growth from total; SQL ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.result(options = {}) ⇒ Object
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 |
# File 'app/models/report/smart_services/smart_services.rb', line 15 def self.result( = {}) cur_start_date = [:period1_gteq] cur_end_date = [:period1_lteq] prev_start_date = [:period2_gteq] prev_end_date = [:period2_lteq] # Coerce IDs through Integer() at entry — any non-numeric input raises # before reaching SQL. Downstream interpolation sites assume an int array. sales_rep_ids = [:primary_sales_rep_ids].filter_map(&:presence).map { |id| Integer(id) } quick_view_sales = quick_view_sales(cur_start_date, cur_end_date, prev_start_date, prev_end_date) quick_view_customer_growth = quick_view_customer_growth(cur_start_date, cur_end_date, prev_start_date, prev_end_date) quick_view_by_product_line = quick_view_by_product_line(cur_start_date, cur_end_date, prev_start_date, prev_end_date) smart_services_data = smart_services_data(cur_start_date, cur_end_date, prev_start_date, prev_end_date, sales_rep_ids) smart_services_data_breakdown = smart_services_data_breakdown(cur_start_date, cur_end_date, prev_start_date, prev_end_date, sales_rep_ids) smart_services_data_by_rep = smart_services_data_by_rep(cur_start_date, cur_end_date, prev_start_date, prev_end_date, sales_rep_ids) smart_services_data_breakdown_by_rep = smart_services_data_breakdown_by_rep(cur_start_date, cur_end_date, prev_start_date, prev_end_date, sales_rep_ids) totals = get_totals(smart_services_data) totals_by_rep = get_totals(smart_services_data_by_rep) data_report_grouping = get_data_grouping(cur_start_date, cur_end_date, sales_rep_ids, 1) data_product_line = get_data_grouping(cur_start_date, cur_end_date, sales_rep_ids, 2) data_monthly_sales = get_montly_sales(cur_start_date, cur_end_date, sales_rep_ids) Result.new(success: true, quick_view_sales: quick_view_sales, quick_view_customer_growth: quick_view_customer_growth, quick_view_by_product_line: quick_view_by_product_line, smart_services_data: smart_services_data, smart_services_data_breakdown: smart_services_data_breakdown, smart_services_data_by_rep: smart_services_data_by_rep, smart_services_data_breakdown_by_rep: smart_services_data_breakdown_by_rep, totals: totals, totals_by_rep: totals_by_rep, data_report_grouping: data_report_grouping, data_product_line: data_product_line, data_monthly_sales: data_monthly_sales, start_date1: cur_start_date, end_date1: cur_end_date, start_date2: prev_start_date, end_date2: prev_end_date) end |
.smart_services_data(cur_start_date, cur_end_date, prev_start_date, prev_end_date, sales_rep_ids) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'app/models/report/smart_services/smart_services.rb', line 131 def self.smart_services_data(cur_start_date, cur_end_date, prev_start_date, prev_end_date, sales_rep_ids) sales_rep_sql = " and primary_sales_rep_id in (#{sales_rep_ids.map { |id| Integer(id) }.join(',')})" if sales_rep_ids.present? sql = <<-SQL.squish select rank () over ( order by report_grouping ) rg_rank,report_grouping, sum(case when product_line_id = 92 and gl_date between '#{prev_start_date}' and '#{prev_end_date}' then num_orders else 0 end) as prev_sf_qty, sum(case when product_line_id = 92 and gl_date between '#{prev_start_date}' and '#{prev_end_date}' then amount else 0 end) as prev_sf_val, sum(case when product_line_id = 230 and gl_date between '#{prev_start_date}' and '#{prev_end_date}' then num_orders else 0 end) as prev_sg_qty, sum(case when product_line_id = 230 and gl_date between '#{prev_start_date}' and '#{prev_end_date}' then amount else 0 end) as prev_sg_val, sum(case when product_line_id = 229 and gl_date between '#{prev_start_date}' and '#{prev_end_date}' then num_orders else 0 end) as prev_si_qty, sum(case when product_line_id = 229 and gl_date between '#{prev_start_date}' and '#{prev_end_date}' then amount else 0 end) as prev_si_val, sum(case when product_line_id = 254 and gl_date between '#{prev_start_date}' and '#{prev_end_date}' then num_orders else 0 end) as prev_sx_qty, sum(case when product_line_id = 254 and gl_date between '#{prev_start_date}' and '#{prev_end_date}' then amount else 0 end) as prev_sx_val, sum(case when gl_date between '#{prev_start_date}' and '#{prev_end_date}' then num_orders else 0 end) as prev_qty, sum(case when gl_date between '#{prev_start_date}' and '#{prev_end_date}' then amount else 0 end) as prev_val, sum(case when product_line_id = 92 and gl_date between '#{cur_start_date}' and '#{cur_end_date}' then num_orders else 0 end) as cur_sf_qty, sum(case when product_line_id = 92 and gl_date between '#{cur_start_date}' and '#{cur_end_date}' then amount else 0 end) as cur_sf_val, sum(case when product_line_id = 230 and gl_date between '#{cur_start_date}' and '#{cur_end_date}' then num_orders else 0 end) as cur_sg_qty, sum(case when product_line_id = 230 and gl_date between '#{cur_start_date}' and '#{cur_end_date}' then amount else 0 end) as cur_sg_val, sum(case when product_line_id = 229 and gl_date between '#{cur_start_date}' and '#{cur_end_date}' then num_orders else 0 end) as cur_si_qty, sum(case when product_line_id = 229 and gl_date between '#{cur_start_date}' and '#{cur_end_date}' then amount else 0 end) as cur_si_val, sum(case when product_line_id = 254 and gl_date between '#{cur_start_date}' and '#{cur_end_date}' then num_orders else 0 end) as cur_sx_qty, sum(case when product_line_id = 254 and gl_date between '#{cur_start_date}' and '#{cur_end_date}' then amount else 0 end) as cur_sx_val, sum(case when gl_date between '#{cur_start_date}' and '#{cur_end_date}' then num_orders else 0 end) as cur_qty, sum(case when gl_date between '#{cur_start_date}' and '#{cur_end_date}' then amount else 0 end) as cur_val from ( select report_grouping,product_line_id,gl_date,count(distinct order_id) as num_orders,sum(quantity * discounted_price * consolidated_exchange_rate) as amount from view_smart_services_sales where (gl_date between '#{cur_start_date}' and '#{cur_end_date}' or gl_date between '#{prev_start_date}' and '#{prev_end_date}') #{sales_rep_sql} group by report_grouping,product_line_id,gl_date union all select report_grouping,254 as product_line_id,gl_date,count(distinct iv.id) as num_orders,sum(quantity * discounted_price * consolidated_exchange_rate) as amount from line_items li inner join invoices iv on li.resource_id = iv.id where resource_type = 'Invoice' and resource_id in (141050,144784,141449,141352,142340,142569,142611,147018,147273,149059,149517,148942,148941,144780,144779,145722,149372,146945,147086,147087,145777,146946,146947,146948,147019,147020,147090,147316,149045,149726,152553,153644,153645,153648) and item_id is null and (gl_date between '#{cur_start_date}' and '#{cur_end_date}' or gl_date between '#{prev_start_date}' and '#{prev_end_date}') #{sales_rep_sql} group by report_grouping,gl_date )a group by report_grouping order by report_grouping; SQL ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.smart_services_data_breakdown(cur_start_date, cur_end_date, prev_start_date, prev_end_date, sales_rep_ids) ⇒ Object
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 |
# File 'app/models/report/smart_services/smart_services.rb', line 180 def self.smart_services_data_breakdown(cur_start_date, cur_end_date, prev_start_date, prev_end_date, sales_rep_ids) sales_rep_sql1 = " and vsss.primary_sales_rep_id in (#{sales_rep_ids.map { |id| Integer(id) }.join(',')})" if sales_rep_ids.present? sales_rep_sql2 = " and iv.primary_sales_rep_id in (#{sales_rep_ids.map { |id| Integer(id) }.join(',')})" if sales_rep_ids.present? sql = <<-SQL.squish select report_grouping,type,order_id,cust_ord, case when product_line_id = 92 and gl_date between '#{prev_start_date}' and '#{prev_end_date}' then num_orders else 0 end as prev_sf_qty, case when product_line_id = 92 and gl_date between '#{prev_start_date}' and '#{prev_end_date}' then amount else 0 end as prev_sf_val, case when product_line_id = 230 and gl_date between '#{prev_start_date}' and '#{prev_end_date}' then num_orders else 0 end as prev_sg_qty, case when product_line_id = 230 and gl_date between '#{prev_start_date}' and '#{prev_end_date}' then amount else 0 end as prev_sg_val, case when product_line_id = 229 and gl_date between '#{prev_start_date}' and '#{prev_end_date}' then num_orders else 0 end as prev_si_qty, case when product_line_id = 229 and gl_date between '#{prev_start_date}' and '#{prev_end_date}' then amount else 0 end as prev_si_val, case when product_line_id = 254 and gl_date between '#{prev_start_date}' and '#{prev_end_date}' then num_orders else 0 end as prev_sx_qty, case when product_line_id = 254 and gl_date between '#{prev_start_date}' and '#{prev_end_date}' then amount else 0 end as prev_sx_val, case when gl_date between '#{prev_start_date}' and '#{prev_end_date}' then num_orders else 0 end as prev_qty, case when gl_date between '#{prev_start_date}' and '#{prev_end_date}' then amount else 0 end as prev_val, case when product_line_id = 92 and gl_date between '#{cur_start_date}' and '#{cur_end_date}' then num_orders else 0 end as cur_sf_qty, case when product_line_id = 92 and gl_date between '#{cur_start_date}' and '#{cur_end_date}' then amount else 0 end as cur_sf_val, case when product_line_id = 230 and gl_date between '#{cur_start_date}' and '#{cur_end_date}' then num_orders else 0 end as cur_sg_qty, case when product_line_id = 230 and gl_date between '#{cur_start_date}' and '#{cur_end_date}' then amount else 0 end as cur_sg_val, case when product_line_id = 229 and gl_date between '#{cur_start_date}' and '#{cur_end_date}' then num_orders else 0 end as cur_si_qty, case when product_line_id = 229 and gl_date between '#{cur_start_date}' and '#{cur_end_date}' then amount else 0 end as cur_si_val, case when product_line_id = 254 and gl_date between '#{cur_start_date}' and '#{cur_end_date}' then num_orders else 0 end as cur_sx_qty, case when product_line_id = 254 and gl_date between '#{cur_start_date}' and '#{cur_end_date}' then amount else 0 end as cur_sx_val, case when gl_date between '#{cur_start_date}' and '#{cur_end_date}' then num_orders else 0 end as cur_qty, case when gl_date between '#{cur_start_date}' and '#{cur_end_date}' then amount else 0 end as cur_val from ( select 'Order' as type,order_id,(c.full_name || ' - ' || o.reference_number) as cust_ord,vsss.report_grouping,product_line_id,gl_date,1 as num_orders,sum(quantity * discounted_price * consolidated_exchange_rate) as amount from view_smart_services_sales vsss inner join orders o on order_id = o.id inner join parties c on o.customer_id = c.id where (gl_date between '#{cur_start_date}' and '#{cur_end_date}' or gl_date between '#{prev_start_date}' and '#{prev_end_date}') #{sales_rep_sql1} group by order_id,vsss.report_grouping,product_line_id,gl_date,o.reference_number,c.full_name union all select 'Invoice' as type,iv.id,(c.full_name || ' - ' || iv.reference_number) as cust_ord,iv.report_grouping,254 as product_line_id,gl_date,1 as num_orders,sum(quantity * discounted_price * consolidated_exchange_rate) as amount from line_items li inner join invoices iv on li.resource_id = iv.id inner join parties c on iv.customer_id = c.id where resource_type = 'Invoice' and resource_id in (141050,144784,141449,141352,142340,142569,142611,147018,147273,149059,149517,148942,148941,144780,144779,145722,149372,146945,147086,147087,145777,146946,146947,146948,147019,147020,147090,147316,149045,149726,152553,153644,153645,153648) and item_id is null and (gl_date between '#{cur_start_date}' and '#{cur_end_date}' or gl_date between '#{prev_start_date}' and '#{prev_end_date}') #{sales_rep_sql2} group by iv.id,iv.report_grouping,gl_date,iv.reference_number,c.full_name )a order by report_grouping; SQL ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.smart_services_data_breakdown_by_rep(cur_start_date, cur_end_date, prev_start_date, prev_end_date, sales_rep_ids) ⇒ Object
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
# File 'app/models/report/smart_services/smart_services.rb', line 287 def self.smart_services_data_breakdown_by_rep(cur_start_date, cur_end_date, prev_start_date, prev_end_date, sales_rep_ids) sales_rep_sql1 = " and vsss.primary_sales_rep_id in (#{sales_rep_ids.map { |id| Integer(id) }.join(',')})" if sales_rep_ids.present? sales_rep_sql2 = " and iv.primary_sales_rep_id in (#{sales_rep_ids.map { |id| Integer(id) }.join(',')})" if sales_rep_ids.present? sql = <<-SQL.squish select primary_sales_rep_id,primary_sales_rep,type,order_id,cust_ord, case when product_line_id = 92 and gl_date between '#{prev_start_date}' and '#{prev_end_date}' then num_orders else 0 end as prev_sf_qty, case when product_line_id = 92 and gl_date between '#{prev_start_date}' and '#{prev_end_date}' then amount else 0 end as prev_sf_val, case when product_line_id = 230 and gl_date between '#{prev_start_date}' and '#{prev_end_date}' then num_orders else 0 end as prev_sg_qty, case when product_line_id = 230 and gl_date between '#{prev_start_date}' and '#{prev_end_date}' then amount else 0 end as prev_sg_val, case when product_line_id = 229 and gl_date between '#{prev_start_date}' and '#{prev_end_date}' then num_orders else 0 end as prev_si_qty, case when product_line_id = 229 and gl_date between '#{prev_start_date}' and '#{prev_end_date}' then amount else 0 end as prev_si_val, case when product_line_id = 254 and gl_date between '#{prev_start_date}' and '#{prev_end_date}' then num_orders else 0 end as prev_sx_qty, case when product_line_id = 254 and gl_date between '#{prev_start_date}' and '#{prev_end_date}' then amount else 0 end as prev_sx_val, case when gl_date between '#{prev_start_date}' and '#{prev_end_date}' then num_orders else 0 end as prev_qty, case when gl_date between '#{prev_start_date}' and '#{prev_end_date}' then amount else 0 end as prev_val, case when product_line_id = 92 and gl_date between '#{cur_start_date}' and '#{cur_end_date}' then num_orders else 0 end as cur_sf_qty, case when product_line_id = 92 and gl_date between '#{cur_start_date}' and '#{cur_end_date}' then amount else 0 end as cur_sf_val, case when product_line_id = 230 and gl_date between '#{cur_start_date}' and '#{cur_end_date}' then num_orders else 0 end as cur_sg_qty, case when product_line_id = 230 and gl_date between '#{cur_start_date}' and '#{cur_end_date}' then amount else 0 end as cur_sg_val, case when product_line_id = 229 and gl_date between '#{cur_start_date}' and '#{cur_end_date}' then num_orders else 0 end as cur_si_qty, case when product_line_id = 229 and gl_date between '#{cur_start_date}' and '#{cur_end_date}' then amount else 0 end as cur_si_val, case when product_line_id = 254 and gl_date between '#{cur_start_date}' and '#{cur_end_date}' then num_orders else 0 end as cur_sx_qty, case when product_line_id = 254 and gl_date between '#{cur_start_date}' and '#{cur_end_date}' then amount else 0 end as cur_sx_val, case when gl_date between '#{cur_start_date}' and '#{cur_end_date}' then num_orders else 0 end as cur_qty, case when gl_date between '#{cur_start_date}' and '#{cur_end_date}' then amount else 0 end as cur_val from ( select 'Order' as type,order_id,(c.full_name || ' - ' || o.reference_number) as cust_ord,vsss.primary_sales_rep_id,sr.full_name as primary_sales_rep,product_line_id,gl_date,1 as num_orders,sum(quantity * discounted_price * consolidated_exchange_rate) as amount from view_smart_services_sales vsss inner join orders o on order_id = o.id inner join parties c on o.customer_id = c.id inner join parties sr on vsss.primary_sales_rep_id = sr.id and sr.type = 'Employee' where (gl_date between '#{prev_start_date}' and '#{prev_end_date}' or gl_date between '#{cur_start_date}' and '#{cur_end_date}') #{sales_rep_sql1} group by order_id,vsss.primary_sales_rep_id,sr.full_name,product_line_id,gl_date,o.reference_number,c.full_name union all select 'Invoice' as type,iv.id,(c.full_name || ' - ' || iv.reference_number) as cust_ord,iv.primary_sales_rep_id,sr.full_name as primary_sales_rep,254 as product_line_id,gl_date,1 as num_orders,sum(quantity * discounted_price * consolidated_exchange_rate) as amount from line_items li inner join invoices iv on li.resource_id = iv.id inner join parties c on iv.customer_id = c.id inner join parties sr on iv.primary_sales_rep_id = sr.id and sr.type = 'Employee' where resource_type = 'Invoice' and resource_id in (141050,144784,141449,141352,142340,142569,142611,147018,147273,149059,149517,148942,148941,144780,144779,145722,149372,146945,147086,147087,145777,146946,146947,146948,147019,147020,147090,147316,149045,149726,152553,153644,153645,153648) and item_id is null and (gl_date between '#{prev_start_date}' and '#{prev_end_date}' or gl_date between '#{cur_start_date}' and '#{cur_end_date}') #{sales_rep_sql2} group by iv.id,iv.primary_sales_rep_id,sr.full_name,gl_date,iv.reference_number,c.full_name )a order by primary_sales_rep; SQL ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.smart_services_data_by_rep(cur_start_date, cur_end_date, prev_start_date, prev_end_date, sales_rep_ids) ⇒ Object
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'app/models/report/smart_services/smart_services.rb', line 232 def self.smart_services_data_by_rep(cur_start_date, cur_end_date, prev_start_date, prev_end_date, sales_rep_ids) sales_rep_sql1 = " and vsss.primary_sales_rep_id in (#{sales_rep_ids.map { |id| Integer(id) }.join(',')})" if sales_rep_ids.present? sales_rep_sql2 = " and iv.primary_sales_rep_id in (#{sales_rep_ids.map { |id| Integer(id) }.join(',')})" if sales_rep_ids.present? sql = <<-SQL.squish select rank () over ( order by primary_sales_rep_id ) rg_rank,primary_sales_rep_id,primary_sales_rep, sum(case when product_line_id = 92 and gl_date between '#{prev_start_date}' and '#{prev_end_date}' then num_orders else 0 end) as prev_sf_qty, sum(case when product_line_id = 92 and gl_date between '#{prev_start_date}' and '#{prev_end_date}' then amount else 0 end) as prev_sf_val, sum(case when product_line_id = 230 and gl_date between '#{prev_start_date}' and '#{prev_end_date}' then num_orders else 0 end) as prev_sg_qty, sum(case when product_line_id = 230 and gl_date between '#{prev_start_date}' and '#{prev_end_date}' then amount else 0 end) as prev_sg_val, sum(case when product_line_id = 229 and gl_date between '#{prev_start_date}' and '#{prev_end_date}' then num_orders else 0 end) as prev_si_qty, sum(case when product_line_id = 229 and gl_date between '#{prev_start_date}' and '#{prev_end_date}' then amount else 0 end) as prev_si_val, sum(case when product_line_id = 254 and gl_date between '#{prev_start_date}' and '#{prev_end_date}' then num_orders else 0 end) as prev_sx_qty, sum(case when product_line_id = 254 and gl_date between '#{prev_start_date}' and '#{prev_end_date}' then amount else 0 end) as prev_sx_val, sum(case when gl_date between '#{prev_start_date}' and '#{prev_end_date}' then num_orders else 0 end) as prev_qty, sum(case when gl_date between '#{prev_start_date}' and '#{prev_end_date}' then amount else 0 end) as prev_val, sum(case when product_line_id = 92 and gl_date between '#{cur_start_date}' and '#{cur_end_date}' then num_orders else 0 end) as cur_sf_qty, sum(case when product_line_id = 92 and gl_date between '#{cur_start_date}' and '#{cur_end_date}' then amount else 0 end) as cur_sf_val, sum(case when product_line_id = 230 and gl_date between '#{cur_start_date}' and '#{cur_end_date}' then num_orders else 0 end) as cur_sg_qty, sum(case when product_line_id = 230 and gl_date between '#{cur_start_date}' and '#{cur_end_date}' then amount else 0 end) as cur_sg_val, sum(case when product_line_id = 229 and gl_date between '#{cur_start_date}' and '#{cur_end_date}' then num_orders else 0 end) as cur_si_qty, sum(case when product_line_id = 229 and gl_date between '#{cur_start_date}' and '#{cur_end_date}' then amount else 0 end) as cur_si_val, sum(case when product_line_id = 254 and gl_date between '#{cur_start_date}' and '#{cur_end_date}' then num_orders else 0 end) as cur_sx_qty, sum(case when product_line_id = 254 and gl_date between '#{cur_start_date}' and '#{cur_end_date}' then amount else 0 end) as cur_sx_val, sum(case when gl_date between '#{cur_start_date}' and '#{cur_end_date}' then num_orders else 0 end) as cur_qty, sum(case when gl_date between '#{cur_start_date}' and '#{cur_end_date}' then amount else 0 end) as cur_val from ( select order_id,vsss.primary_sales_rep_id,sr.full_name as primary_sales_rep,product_line_id,gl_date,1 as num_orders,sum(quantity * discounted_price * consolidated_exchange_rate) as amount from view_smart_services_sales vsss inner join orders o on order_id = o.id inner join parties c on o.customer_id = c.id inner join parties sr on vsss.primary_sales_rep_id = sr.id and sr.type = 'Employee' where (gl_date between '#{prev_start_date}' and '#{prev_end_date}' or gl_date between '#{cur_start_date}' and '#{cur_end_date}') #{sales_rep_sql1} group by order_id,vsss.primary_sales_rep_id,sr.full_name,product_line_id,gl_date,o.reference_number,c.full_name union all select iv.id,iv.primary_sales_rep_id,sr.full_name as primary_sales_rep,254 as product_line_id,gl_date,1 as num_orders,sum(quantity * discounted_price * consolidated_exchange_rate) as amount from line_items li inner join invoices iv on li.resource_id = iv.id inner join parties c on iv.customer_id = c.id inner join parties sr on iv.primary_sales_rep_id = sr.id and sr.type = 'Employee' where resource_type = 'Invoice' and resource_id in (141050,144784,141449,141352,142340,142569,142611,147018,147273,149059,149517,148942,148941,144780,144779,145722,149372,146945,147086,147087,145777,146946,146947,146948,147019,147020,147090,147316,149045,149726,152553,153644,153645,153648) and item_id is null and (gl_date between '#{prev_start_date}' and '#{prev_end_date}' or gl_date between '#{cur_start_date}' and '#{cur_end_date}') #{sales_rep_sql2} group by iv.id,iv.primary_sales_rep_id,sr.full_name,gl_date,iv.reference_number,c.full_name )a group by primary_sales_rep_id,primary_sales_rep order by primary_sales_rep; SQL ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |