Class: Report::ItemSale::ItemSale
- Inherits:
-
Object
- Object
- Report::ItemSale::ItemSale
- Defined in:
- app/services/report/item_sale/item_sale.rb
Defined Under Namespace
Classes: Result
Class Method Summary collapse
- .best_seller_by_product_line(start_date, end_date, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, top, native_currency) ⇒ Object
- .customer_data_table(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) ⇒ Object
- .data_column_char(data, dates) ⇒ Object
- .dates_list(dates, type) ⇒ Object
- .dates_to_check(start_date, end_date, compare_start_date, compare_end_date) ⇒ Object
- .get_data_by_customer(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, type, native_currency) ⇒ Object
- .get_data_by_product_line_chart(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, type, native_currency) ⇒ Object
- .get_data_by_product_line_table(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) ⇒ Object
- .get_data_by_report_grouping(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) ⇒ Object
- .get_data_by_report_grouping_bd(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) ⇒ Object
- .get_data_category(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) ⇒ Object
- .get_data_grouping(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) ⇒ Object
- .get_data_heatmap(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) ⇒ Object
- .get_data_items_table(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) ⇒ Object
- .get_eu_data_heatmap(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, native_currency) ⇒ Object
- .quick_view_by_product_line(dates, native_currency) ⇒ Object
- .quick_view_customer_growth(dates, native_currency) ⇒ Object
- .quick_view_sales(dates, native_currency, currency, company_ids) ⇒ Object
- .total_report(options = {}) ⇒ Object
- .week_dates(dates) ⇒ Object
Class Method Details
.best_seller_by_product_line(start_date, end_date, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, top, native_currency) ⇒ Object
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 816 |
# File 'app/services/report/item_sale/item_sale.rb', line 772 def self.best_seller_by_product_line(start_date, end_date, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, top, native_currency) report_groupings_sql = report_groupings.empty? ? '' : " and report_grouping in (#{report_groupings.map { |rg| "'#{rg.sub("'", "''")}'" }.join(',')})" product_line_ids_sql = product_line_ids.nil? || product_line_ids.empty? ? '' : " and pl.id in (#{product_line_ids.map { |pg| pg.to_i }.join(',')})" customer_ids_sql = customer_ids.nil? || customer_ids.empty? ? '' : " and customer_id in (#{customer_ids.map { |c| c.to_i }.join(',')})" item_ids_sql = item_ids.nil? || item_ids.empty? ? '' : " and item_id in (#{item_ids.map { |i| i.to_i }.join(',')})" company_id_sql = company_ids.present? ? " and company_id in (#{company_ids.join(',')})" : '' state_codes_sql = state_codes.empty? ? '' : " and a.state_code in (#{state_codes.map { |sc| "'#{sc}'" }.join(',')})" revenue_sql = native_currency ? 'revenue_total' : 'revenue_total_consolidated' sql = <<-SQL with sales_fact as ( select trim(split_part(pl.lineage_expanded,'>',1)) as group1,sku,sum(#{revenue_sql}) as total from view_sales_facts vsf inner join items i on vsf.item_id = i.id left join addresses a on vsf.shipping_address_id = a.id inner join product_lines pl on vsf.primary_product_line_id = pl.id where gl_date between '#{start_date}' and '#{end_date}' and vsf.invoice_id not in (117052,117054,117065,117066,117375) #{report_groupings_sql} #{product_line_ids_sql} #{customer_ids_sql} #{item_ids_sql} #{company_id_sql} #{state_codes_sql} group by trim(split_part(pl.lineage_expanded,'>',1)),sku having sum(#{revenue_sql}) > 0 ), top_result as ( select cast(row_number() over (ORDER BY sum(total) desc) as int) as row_ord,group1,sum(total) as total from ( select * from sales_fact )a group by group1 order by 3 desc limit 4 ) select sku as detail,cast(sum(total) as numeric) as total_amount from sales_fact where group1 = (select group1 from top_result where row_ord = #{top}) group by sku order by 2 desc limit 5 SQL results = ActiveRecord::Base.connection.execute(sql).to_a.map { |r| r.symbolize_keys } end |
.customer_data_table(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) ⇒ Object
480 481 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 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 |
# File 'app/services/report/item_sale/item_sale.rb', line 480 def self.customer_data_table(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) date_from = dates[:date_from].beginning_of_week - 7 date_to = dates[:date_to].end_of_week - 2 report_groupings_sql = report_groupings.empty? ? '' : " and vsf.report_grouping in (#{report_groupings.map { |rg| "'#{rg.sub("'", "''")}'" }.join(',')})" product_line_ids_sql = product_line_ids.nil? || product_line_ids.empty? ? '' : " and pl.id in (#{product_line_ids.map { |pg| pg.to_i }.join(',')})" customer_ids_sql = customer_ids.nil? || customer_ids.empty? ? '' : " and vsf.customer_id in (#{customer_ids.map { |c| c.to_i }.join(',')})" item_ids_sql = item_ids.nil? || item_ids.empty? ? '' : " and item_id in (#{item_ids.map { |i| i.to_i }.join(',')})" company_id_sql = company_ids.present? ? " and vsf.company_id in (#{company_ids.join(',')})" : '' state_codes_sql = state_codes.empty? ? '' : " and a.state_code in (#{state_codes.map { |sc| "'#{sc}'" }.join(',')})" selects = '' group_by = '' revenue_sql = native_currency ? 'revenue_total' : 'revenue_total_consolidated' if customer_ids.present? selects = ' full_name,vsf.customer_id,week,' group_by = ' group by full_name,vsf.customer_id,week_year,week ' else selects = " 'All Customers' as full_name,9999999999 as customer_id,week," group_by = ' group by week_year,week ' end sql = <<-SQL with dates as ( select *,(row_number() over (order by week_year,week)) as rank from ( select distinct week_year,week,(week_year + week)::int as week_id from ( select *,case when week = 53 and to_char(week_start_date, 'YYYY')::int < year then to_char(week_start_date, 'YYYY')::int when week = 1 and extract(month from week_start_date) = 12 then to_char(week_start_date, 'YYYY')::int + 1 else to_char(week_start_date, 'YYYY')::int end as week_year from analytic_date_time_dimensions ) as dt where date between '#{date_from}' and '#{date_to}' order by week_year,week desc limit 6 )a ), sales_data as ( select #{selects} (week_year + week)::int as week_id, sum(#{revenue_sql}) as cur_sales from view_sales_facts vsf inner join ( select *,case when week = 53 and to_char(week_start_date, 'YYYY')::int < year then to_char(week_start_date, 'YYYY')::int when week = 1 and extract(month from week_start_date) = 12 then to_char(week_start_date, 'YYYY')::int + 1 else to_char(week_start_date, 'YYYY')::int end as week_year from analytic_date_time_dimensions ) as dt on vsf.gl_date = dt.date inner join parties p on vsf.customer_id = p.id inner join addresses a on vsf.shipping_address_id = a.id inner join product_lines pl on vsf.primary_product_line_id = pl.id where gl_date between '#{date_from}' and '#{date_to}' --and vsf.customer_id in (2385944,2742665,5768174,5322434,10132752,2741053,4267467,19293788) #{report_groupings_sql} #{product_line_ids_sql} #{customer_ids_sql} #{item_ids_sql} #{company_id_sql} #{state_codes_sql} #{group_by} ) select full_name,customer_id, sum(case when (select week_id from dates where rank = 2) = week_id then cur_sales else 0 end) as week_1, case when sum(case when (select week_id from dates where rank = 1) = week_id then cur_sales else 0 end) = 0 and sum(case when (select week_id from dates where rank = 2) = week_id then cur_sales else 0 end) > 0 then 100 when sum(case when (select week_id from dates where rank = 1) = week_id then cur_sales else 0 end) = 0 and sum(case when (select week_id from dates where rank = 2) = week_id then cur_sales else 0 end) = 0 then 0 else ((sum(case when (select week_id from dates where rank = 2) = week_id then cur_sales else 0 end) / sum(case when (select week_id from dates where rank = 1) = week_id then cur_sales else 0 end)) - 1) * 100 end as week_growth_1, sum(case when (select week_id from dates where rank = 3) = week_id then cur_sales else 0 end) as week_2, case when sum(case when (select week_id from dates where rank = 2) = week_id then cur_sales else 0 end) = 0 and sum(case when (select week_id from dates where rank = 3) = week_id then cur_sales else 0 end) > 0 then 100 when sum(case when (select week_id from dates where rank = 2) = week_id then cur_sales else 0 end) = 0 and sum(case when (select week_id from dates where rank = 3) = week_id then cur_sales else 0 end) = 0 then 0 else ((sum(case when (select week_id from dates where rank = 3) = week_id then cur_sales else 0 end) / sum(case when (select week_id from dates where rank = 2) = week_id then cur_sales else 0 end)) - 1) * 100 end as week_growth_2, sum(case when (select week_id from dates where rank = 4) = week_id then cur_sales else 0 end) as week_3, case when sum(case when (select week_id from dates where rank = 3) = week_id then cur_sales else 0 end) = 0 and sum(case when (select week_id from dates where rank = 4) = week_id then cur_sales else 0 end) > 0 then 100 when sum(case when (select week_id from dates where rank = 3) = week_id then cur_sales else 0 end) = 0 and sum(case when (select week_id from dates where rank = 4) = week_id then cur_sales else 0 end) = 0 then 0 else ((sum(case when (select week_id from dates where rank = 4) = week_id then cur_sales else 0 end) / sum(case when (select week_id from dates where rank = 3) = week_id then cur_sales else 0 end)) - 1) * 100 end as week_growth_3, sum(case when (select week_id from dates where rank = 5) = week_id then cur_sales else 0 end) as week_4, case when sum(case when (select week_id from dates where rank = 4) = week_id then cur_sales else 0 end) = 0 and sum(case when (select week_id from dates where rank = 5) = week_id then cur_sales else 0 end) > 0 then 100 when sum(case when (select week_id from dates where rank = 4) = week_id then cur_sales else 0 end) = 0 and sum(case when (select week_id from dates where rank = 5) = week_id then cur_sales else 0 end) = 0 then 0 else ((sum(case when (select week_id from dates where rank = 5) = week_id then cur_sales else 0 end) / sum(case when (select week_id from dates where rank = 4) = week_id then cur_sales else 0 end)) - 1) * 100 end as week_growth_4, sum(case when (select week_id from dates where rank = 6) = week_id then cur_sales else 0 end) as week_5, case when sum(case when (select week_id from dates where rank = 5) = week_id then cur_sales else 0 end) = 0 and sum(case when (select week_id from dates where rank = 6) = week_id then cur_sales else 0 end) > 0 then 100 when sum(case when (select week_id from dates where rank = 5) = week_id then cur_sales else 0 end) = 0 and sum(case when (select week_id from dates where rank = 6) = week_id then cur_sales else 0 end) = 0 then 0 else ((sum(case when (select week_id from dates where rank = 6) = week_id then cur_sales else 0 end) / sum(case when (select week_id from dates where rank = 5) = week_id then cur_sales else 0 end)) - 1) * 100 end as week_growth_5 from sales_data group by full_name,customer_id; SQL results = ActiveRecord::Base.connection.execute(sql).to_a.map { |r| r.symbolize_keys } end |
.data_column_char(data, dates) ⇒ Object
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 |
# File 'app/services/report/item_sale/item_sale.rb', line 456 def self.data_column_char(data, dates) dates_list = [] detail_name_list = [] detail_list = [] data.each do |r| dates_list << r[:date] detail_name_list << r[:detail_name] end dates_list = dates_list.uniq.compact detail_name_list = detail_name_list.uniq.compact detail_name_list.each do |c| sales = [] dates.each do |d| amount = 0 data.each do |r| amount = r[:cur_sales].to_f.round if c == r[:detail_name] && d == r[:date] end sales << amount end detail_list << [c, sales] end detail_list end |
.dates_list(dates, type) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'app/services/report/item_sale/item_sale.rb', line 115 def self.dates_list(dates, type) if type == 'weekly' date_from = dates[:date_from].beginning_of_week date_to = dates[:date_to].end_of_week - 2 else date_from = dates[:date_from] date_to = dates[:date_to] end sql = <<-SQL select date,week,week_start_date from analytic_date_time_dimensions where date between '#{date_from}' and '#{date_to}' and weekend = 'Weekday' order by date SQL results = ActiveRecord::Base.connection.execute(sql).to_a.map { |r| r.symbolize_keys } end |
.dates_to_check(start_date, end_date, compare_start_date, compare_end_date) ⇒ Object
106 107 108 109 110 111 112 113 |
# File 'app/services/report/item_sale/item_sale.rb', line 106 def self.dates_to_check(start_date, end_date, compare_start_date, compare_end_date) date_from = start_date date_to = end_date date_from_prev = compare_start_date date_to_prev = compare_end_date { date_from:, date_to:, date_from_prev:, date_to_prev: } end |
.get_data_by_customer(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, type, native_currency) ⇒ Object
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 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 |
# File 'app/services/report/item_sale/item_sale.rb', line 397 def self.get_data_by_customer(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, type, native_currency) report_groupings_sql = report_groupings.empty? ? '' : " and vsf.report_grouping in (#{report_groupings.map { |rg| "'#{rg.sub("'", "''")}'" }.join(',')})" product_line_ids_sql = product_line_ids.nil? || product_line_ids.empty? ? '' : " and pl.id in (#{product_line_ids.map { |pg| pg.to_i }.join(',')})" customer_ids_sql = customer_ids.nil? || customer_ids.empty? ? '' : " and vsf.customer_id in (#{customer_ids.map { |c| c.to_i }.join(',')})" item_ids_sql = item_ids.nil? || item_ids.empty? ? '' : " and item_id in (#{item_ids.map { |i| i.to_i }.join(',')})" company_id_sql = company_ids.present? ? " and vsf.company_id in (#{company_ids.join(',')})" : '' state_codes_sql = state_codes.empty? ? '' : " and a.state_code in (#{state_codes.map { |sc| "'#{sc}'" }.join(',')})" selects = '' group_by = '' revenue_sql = native_currency ? 'revenue_total' : 'revenue_total_consolidated' if customer_ids.present? && type == 'daily' date_from = dates[:date_from] date_to = dates[:date_to] selects = ' full_name as detail_name,vsf.customer_id,gl_date as date,' group_by = ' group by full_name,vsf.customer_id,gl_date ' elsif customer_ids.present? && type == 'weekly' date_from = dates[:date_from].beginning_of_week date_to = dates[:date_to].end_of_week - 2 selects = ' full_name as detail_name,vsf.customer_id,week_year,week as date,' group_by = ' group by full_name,vsf.customer_id,week_year,week ' elsif customer_ids.blank? && type == 'daily' date_from = dates[:date_from] date_to = dates[:date_to] selects = " 'All Customers' as detail_name,9999999999 as customer_id,gl_date as date," group_by = ' group by gl_date ' elsif customer_ids.blank? && type == 'weekly' date_from = dates[:date_from].beginning_of_week date_to = dates[:date_to].end_of_week - 2 selects = " 'All Customers' as detail_name,9999999999 as customer_id,week_year,week as date," group_by = ' group by week_year,week ' end sql = <<-SQL select #{selects} sum(#{revenue_sql}) as cur_sales from ( select *,case when week = 53 and to_char(week_start_date, 'YYYY')::int < year then to_char(week_start_date, 'YYYY')::int when week = 1 and extract(month from week_start_date) = 12 then to_char(week_start_date, 'YYYY')::int + 1 else to_char(week_start_date, 'YYYY')::int end as week_year from analytic_date_time_dimensions ) as dt left join view_sales_facts vsf on vsf.gl_date = dt.date inner join parties p on vsf.customer_id = p.id inner join addresses a on vsf.shipping_address_id = a.id inner join product_lines pl on vsf.primary_product_line_id = pl.id where gl_date between '#{date_from}' and '#{date_to}' #{report_groupings_sql} #{product_line_ids_sql} #{customer_ids_sql} #{item_ids_sql} #{company_id_sql} #{state_codes_sql} #{group_by} order by 3; SQL results = ActiveRecord::Base.connection.execute(sql).to_a.map { |r| r.symbolize_keys } end |
.get_data_by_product_line_chart(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, type, native_currency) ⇒ Object
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'app/services/report/item_sale/item_sale.rb', line 231 def self.get_data_by_product_line_chart(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, type, native_currency) report_groupings_sql = report_groupings.empty? ? '' : " and report_grouping in (#{report_groupings.map { |rg| "'#{rg.sub("'", "''")}'" }.join(',')})" product_line_ids_sql = product_line_ids.nil? || product_line_ids.empty? ? '' : " and pl.id in (#{product_line_ids.map { |pg| pg.to_i }.join(',')})" customer_ids_sql = customer_ids.nil? || customer_ids.empty? ? '' : " and customer_id in (#{customer_ids.map { |c| c.to_i }.join(',')})" item_ids_sql = item_ids.nil? || item_ids.empty? ? '' : " and item_id in (#{item_ids.map { |i| i.to_i }.join(',')})" company_id_sql = company_ids.present? ? " and company_id in (#{company_ids.join(',')})" : '' state_codes_sql = state_codes.empty? ? '' : " and a.state_code in (#{state_codes.map { |sc| "'#{sc}'" }.join(',')})" selects = '' group_by = '' selects2 = '' if type == 'daily' date_from = dates[:date_from] date_to = dates[:date_to] selects = ' gl_date as date, ' group_by = ' group by gl_date ' selects2 = ' date as date ' elsif type == 'weekly' date_from = dates[:date_from].beginning_of_week date_to = dates[:date_to].end_of_week - 2 selects = ' week_year as year,week as date, ' group_by = ' group by week,week_year ' selects2 = " distinct case when week = 53 and to_char(week_start_date, 'YYYY')::int < year then to_char(week_start_date, 'YYYY')::int when week = 1 and extract(month from week_start_date) = 12 then to_char(week_start_date, 'YYYY')::int + 1 else to_char(week_start_date, 'YYYY')::int end as week_year,week as date " end revenue_sql = native_currency ? 'revenue_total' : 'revenue_total_consolidated' sql = <<-SQL with dates as ( select #{selects2} from analytic_date_time_dimensions where date between '#{date_from}' and '#{date_to}' and weekend = 'Weekday' order by 1 ), sales_data as ( select #{selects} sum(case when trim(split_part(lineage_expanded,'>',1)) = 'Floor Heating' then (#{revenue_sql}) else 0 end) as floorheating, sum(case when trim(split_part(lineage_expanded,'>',1)) = 'Snow Melting' then (#{revenue_sql}) else 0 end) as snowdeicing, sum(case when trim(split_part(lineage_expanded,'>',1)) = 'Towel Warmer' then (#{revenue_sql}) else 0 end) as towelwarmer, sum(case when trim(split_part(lineage_expanded,'>',1)) = 'Radiant Panel' then (#{revenue_sql}) 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 (#{revenue_sql}) else 0 end) as otherproduct from ( select *,case when week = 53 and to_char(week_start_date, 'YYYY')::int < year then to_char(week_start_date, 'YYYY')::int when week = 1 and extract(month from week_start_date) = 12 then to_char(week_start_date, 'YYYY')::int + 1 else to_char(week_start_date, 'YYYY')::int end as week_year from analytic_date_time_dimensions ) as dt left join view_sales_facts vsf on vsf.gl_date = dt.date inner join addresses a on vsf.shipping_address_id = a.id inner join product_lines pl on vsf.primary_product_line_id = pl.id where gl_date between '#{date_from}' and '#{date_to}' and trim(split_part(lineage_expanded,'>',1)) <> 'Shipping' #{report_groupings_sql} #{product_line_ids_sql} #{customer_ids_sql} #{item_ids_sql} #{company_id_sql} #{state_codes_sql} #{group_by} order by 1 ) select dates.date,coalesce(floorheating, 0) as floorheating,coalesce(snowdeicing, 0) as snowdeicing,coalesce(towelwarmer, 0) as towelwarmer, coalesce(radiantpanels, 0) as radiantpanels,coalesce(otherproduct, 0) as otherproduct from dates left join sales_data on dates.date = sales_data.date SQL results = ActiveRecord::Base.connection.execute(sql).to_a.map { |r| r.symbolize_keys } end |
.get_data_by_product_line_table(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) ⇒ Object
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 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 395 |
# File 'app/services/report/item_sale/item_sale.rb', line 303 def self.get_data_by_product_line_table(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) date_to = dates[:date_to].end_of_week - 2 date_from = (date_to - 42).beginning_of_week report_groupings_sql = report_groupings.empty? ? '' : " and report_grouping in (#{report_groupings.map { |rg| "'#{rg.sub("'", "''")}'" }.join(',')})" product_line_ids_sql = product_line_ids.nil? || product_line_ids.empty? ? '' : " and pl.id in (#{product_line_ids.map { |pg| pg.to_i }.join(',')})" customer_ids_sql = customer_ids.nil? || customer_ids.empty? ? '' : " and customer_id in (#{customer_ids.map { |c| c.to_i }.join(',')})" item_ids_sql = item_ids.nil? || item_ids.empty? ? '' : " and item_id in (#{item_ids.map { |i| i.to_i }.join(',')})" company_id_sql = company_ids.present? ? " and company_id in (#{company_ids.join(',')})" : '' state_codes_sql = state_codes.empty? ? '' : " and a.state_code in (#{state_codes.map { |sc| "'#{sc}'" }.join(',')})" revenue_sql = native_currency ? 'revenue_total' : 'revenue_total_consolidated' sql = <<-SQL with pl_list as ( select 1 as pl_id, 'Floor Heating' as pl_group union all select 2, 'Snow Melting' union all select 3, 'Towel Warmer' union all select 5, 'Radiant Panel' union all select 8, 'Other Product' ),dates as ( select group1,week, (row_number() over (partition by group1 order by group1,week_year,week)) as rank from ( select *,case when week = 53 and to_char(week_start_date, 'YYYY')::int < year then to_char(week_start_date, 'YYYY')::int when week = 1 and extract(month from week_start_date) = 12 then to_char(week_start_date, 'YYYY')::int + 1 else to_char(week_start_date, 'YYYY')::int end as week_year from analytic_date_time_dimensions,( select distinct case when trim(split_part(lineage_expanded,'>',1)) not in ('Floor Heating','Snow Melting','Towel Warmer','Radiant Panel') then 'Other Product' else trim(split_part(lineage_expanded,'>',1)) end as group1 from product_lines where trim(split_part(lineage_expanded,'>',1)) <> 'Shipping' ) as grouping where date between '#{date_from}' and '#{date_to}' ) as dt group by group1,week_year,week ), sales as ( select case when trim(split_part(lineage_expanded,'>',1)) not in ('Floor Heating','Snow Melting','Towel Warmer','Radiant Panel') then 'Other Product' else trim(split_part(lineage_expanded,'>',1)) end as group1, week, sum(#{revenue_sql}) as cur_sales from ( select *,case when week = 53 and to_char(week_start_date, 'YYYY')::int < year then to_char(week_start_date, 'YYYY')::int when week = 1 and extract(month from week_start_date) = 12 then to_char(week_start_date, 'YYYY')::int + 1 else to_char(week_start_date, 'YYYY')::int end as week_year from analytic_date_time_dimensions ) as dt left join view_sales_facts vsf on vsf.gl_date = dt.date inner join addresses a on vsf.shipping_address_id = a.id inner join product_lines pl on vsf.primary_product_line_id = pl.id where gl_date between '#{date_from}' and '#{date_to}' and trim(split_part(lineage_expanded,'>',1)) <> 'Shipping' #{report_groupings_sql} #{product_line_ids_sql} #{customer_ids_sql} #{item_ids_sql} #{company_id_sql} #{state_codes_sql} group by trim(split_part(lineage_expanded,'>',1)),week_year,week ) select pl.pl_id, d.group1, sum(case when d.rank = 3 then cur_sales else 0 end) as week_1, case when sum(case when d.rank = 2 then cur_sales else 0 end) = 0 and sum(case when d.rank = 3 then cur_sales else 0 end) > 0 then 100 when sum(case when d.rank = 2 then cur_sales else 0 end) = 0 and sum(case when d.rank = 3 then cur_sales else 0 end) = 0 then 0 else ((sum(case when d.rank = 3 then cur_sales else 0 end) / sum(case when d.rank = 2 then cur_sales else 0 end)) - 1) * 100 end as week_growth_1, sum(case when d.rank = 4 then cur_sales else 0 end) as week_2, case when sum(case when d.rank = 3 then cur_sales else 0 end) = 0 and sum(case when d.rank = 4 then cur_sales else 0 end) > 0 then 100 when sum(case when d.rank = 3 then cur_sales else 0 end) = 0 and sum(case when d.rank = 4 then cur_sales else 0 end) = 0 then 0 else ((sum(case when d.rank = 4 then cur_sales else 0 end) / sum(case when d.rank = 3 then cur_sales else 0 end)) - 1) * 100 end as week_growth_2, sum(case when d.rank = 5 then cur_sales else 0 end) as week_3, case when sum(case when d.rank = 4 then cur_sales else 0 end) = 0 and sum(case when d.rank = 5 then cur_sales else 0 end) > 0 then 100 when sum(case when d.rank = 4 then cur_sales else 0 end) = 0 and sum(case when d.rank = 5 then cur_sales else 0 end) = 0 then 0 else ((sum(case when d.rank = 5 then cur_sales else 0 end) / sum(case when d.rank = 4 then cur_sales else 0 end)) - 1) * 100 end as week_growth_3, sum(case when d.rank = 6 then cur_sales else 0 end) as week_4, case when sum(case when d.rank = 5 then cur_sales else 0 end) = 0 and sum(case when d.rank = 6 then cur_sales else 0 end) > 0 then 100 when sum(case when d.rank = 5 then cur_sales else 0 end) = 0 and sum(case when d.rank = 6 then cur_sales else 0 end) = 0 then 0 else ((sum(case when d.rank = 6 then cur_sales else 0 end) / sum(case when d.rank = 5 then cur_sales else 0 end)) - 1) * 100 end as week_growth_4, sum(case when d.rank = 7 then cur_sales else 0 end) as week_5, case when sum(case when d.rank = 6 then cur_sales else 0 end) = 0 and sum(case when d.rank = 7 then cur_sales else 0 end) > 0 then 100 when sum(case when d.rank = 6 then cur_sales else 0 end) = 0 and sum(case when d.rank = 7 then cur_sales else 0 end) = 0 then 0 else ((sum(case when d.rank = 7 then cur_sales else 0 end) / sum(case when d.rank = 6 then cur_sales else 0 end)) - 1) * 100 end as week_growth_5 from pl_list pl left join dates d on pl.pl_group = d.group1 left join sales s on d.group1 = s.group1 and d.week = s.week group by pl.pl_id,d.group1 order by 1; SQL results = ActiveRecord::Base.connection.execute(sql).to_a.map { |r| r.symbolize_keys } end |
.get_data_by_report_grouping(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) ⇒ Object
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 |
# File 'app/services/report/item_sale/item_sale.rb', line 639 def self.get_data_by_report_grouping(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) report_groupings_sql = report_groupings.empty? ? '' : " and vs.report_grouping in (#{report_groupings.map { |rg| "'#{rg.sub("'", "''")}'" }.join(',')})" product_line_ids_sql = product_line_ids.nil? || product_line_ids.empty? ? '' : " and pl.id in (#{product_line_ids.map { |pg| pg.to_i }.join(',')})" customer_ids_sql = customer_ids.nil? || customer_ids.empty? ? '' : " and vs.customer_id in (#{customer_ids.map { |c| c.to_i }.join(',')})" item_ids_sql = item_ids.nil? || item_ids.empty? ? '' : " and vs.item_id in (#{item_ids.map { |i| i.to_i }.join(',')})" company_id_sql = company_ids.present? ? " and vs.company_id in (#{company_ids.join(',')})" : '' state_codes_sql = state_codes.empty? ? '' : " and a.state_code in (#{state_codes.map { |sc| "'#{sc}'" }.join(',')})" revenue_sql = native_currency ? 'revenue_total' : 'revenue_total_consolidated' consolidated_production = native_currency ? '* consolidated_exchange_rate' : nil sql = <<-SQL select vs.report_grouping, sum(case when gl_date between '#{dates[:date_from_prev]}' and '#{dates[:date_to_prev]}' then vs.quantity else 0 end) as prev_qty, sum(case when gl_date between '#{dates[:date_from_prev]}' and '#{dates[:date_to_prev]}' then (vs.quantity #{consolidated_production} * vs.discounted_price) else 0 end) as prev_amount, sum(case when gl_date between '#{dates[:date_from]}' and '#{dates[:date_to]}' then vs.quantity else 0 end) as curr_qty, sum(case when gl_date between '#{dates[:date_from]}' and '#{dates[:date_to]}' then (vs.quantity #{consolidated_production} * vs.discounted_price) else 0 end) as curr_amount from view_sales_facts vs inner join product_lines pl on vs.primary_product_line_id = pl.id inner join addresses a on vs.shipping_address_id = a.id left join parties p on vs.primary_sr_id = p.id left join parties c on vs.customer_id = c.id left join view_returned_items vri on vs.order_id = vri.order_id and vs.item_id = vri.item_id and vs.line_item_id = vri.line_item_id where (gl_date between '#{dates[:date_from_prev]}' and '#{dates[:date_to_prev]}' or gl_date between '#{dates[:date_from]}' and '#{dates[:date_to]}') #{report_groupings_sql} #{product_line_ids_sql} #{customer_ids_sql} #{item_ids_sql} #{company_id_sql} #{state_codes_sql} group by vs.report_grouping order by 5 desc SQL results = ActiveRecord::Base.connection.execute(sql).to_a.map { |r| r.symbolize_keys } end |
.get_data_by_report_grouping_bd(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) ⇒ Object
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 |
# File 'app/services/report/item_sale/item_sale.rb', line 675 def self.get_data_by_report_grouping_bd(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) report_groupings_sql = report_groupings.empty? ? '' : " and vs.report_grouping in (#{report_groupings.map { |rg| "'#{rg.sub("'", "''")}'" }.join(',')})" product_line_ids_sql = product_line_ids.nil? || product_line_ids.empty? ? '' : " and pl.id in (#{product_line_ids.map { |pg| pg.to_i }.join(',')})" customer_ids_sql = customer_ids.nil? || customer_ids.empty? ? '' : " and vs.customer_id in (#{customer_ids.map { |c| c.to_i }.join(',')})" item_ids_sql = item_ids.nil? || item_ids.empty? ? '' : " and vs.item_id in (#{item_ids.map { |i| i.to_i }.join(',')})" company_id_sql = company_ids.present? ? " and vs.company_id in (#{company_ids.join(',')})" : '' state_codes_sql = state_codes.empty? ? '' : " and a.state_code in (#{state_codes.map { |sc| "'#{sc}'" }.join(',')})" revenue_sql = native_currency ? 'revenue_total' : 'revenue_total_consolidated' consolidated_production = native_currency ? '* consolidated_exchange_rate' : nil sql = <<-SQL select vs.report_grouping,c.full_name, sum(case when gl_date between '#{dates[:date_from_prev]}' and '#{dates[:date_to_prev]}' then vs.quantity else 0 end) as prev_qty, sum(case when gl_date between '#{dates[:date_from_prev]}' and '#{dates[:date_to_prev]}' then (vs.quantity #{consolidated_production} * vs.discounted_price) else 0 end) as prev_amount, sum(case when gl_date between '#{dates[:date_from]}' and '#{dates[:date_to]}' then vs.quantity else 0 end) as curr_qty, sum(case when gl_date between '#{dates[:date_from]}' and '#{dates[:date_to]}' then (vs.quantity #{consolidated_production} * vs.discounted_price) else 0 end) as curr_amount from view_sales_facts vs inner join product_lines pl on vs.primary_product_line_id = pl.id inner join addresses a on vs.shipping_address_id = a.id left join parties p on vs.primary_sr_id = p.id left join parties c on vs.customer_id = c.id left join view_returned_items vri on vs.order_id = vri.order_id and vs.item_id = vri.item_id and vs.line_item_id = vri.line_item_id where (gl_date between '#{dates[:date_from_prev]}' and '#{dates[:date_to_prev]}' or gl_date between '#{dates[:date_from]}' and '#{dates[:date_to]}') #{report_groupings_sql} #{product_line_ids_sql} #{customer_ids_sql} #{item_ids_sql} #{company_id_sql} #{state_codes_sql} group by vs.report_grouping,c.full_name order by 1,6 desc SQL results = ActiveRecord::Base.connection.execute(sql).to_a.map { |r| r.symbolize_keys } end |
.get_data_category(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) ⇒ Object
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 |
# File 'app/services/report/item_sale/item_sale.rb', line 741 def self.get_data_category(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) report_groupings_sql = report_groupings.empty? ? '' : " and report_grouping in (#{report_groupings.map { |rg| "'#{rg.sub("'", "''")}'" }.join(',')})" product_line_ids_sql = product_line_ids.nil? || product_line_ids.empty? ? '' : " and pl.id in (#{product_line_ids.map { |pg| pg.to_i }.join(',')})" customer_ids_sql = customer_ids.nil? || customer_ids.empty? ? '' : " and customer_id in (#{customer_ids.map { |c| c.to_i }.join(',')})" item_ids_sql = item_ids.nil? || item_ids.empty? ? '' : " and item_id in (#{item_ids.map { |i| i.to_i }.join(',')})" company_id_sql = company_ids.present? ? " and company_id in (#{company_ids.join(',')})" : '' state_codes_sql = state_codes.empty? ? '' : " and a.state_code in (#{state_codes.map { |sc| "'#{sc}'" }.join(',')})" revenue_sql = native_currency ? 'revenue_total' : 'revenue_total_consolidated' sql = <<-SQL select pc.name as detail,sum(#{revenue_sql}) as total_amount from view_sales_facts vsf left join addresses a on vsf.shipping_address_id = a.id inner join product_lines pl on vsf.primary_product_line_id = pl.id inner join product_categories pc on vsf.product_category_id = pc.id where gl_date between '#{dates[:date_from]}' and '#{dates[:date_to]}' and vsf.invoice_id not in (117052,117054,117065,117066,117375) #{report_groupings_sql} #{product_line_ids_sql} #{customer_ids_sql} #{item_ids_sql} #{company_id_sql} #{state_codes_sql} group by pc.name having sum(#{revenue_sql}) > 0 order by 2 desc SQL results = ActiveRecord::Base.connection.execute(sql).to_a.map { |r| r.symbolize_keys } end |
.get_data_grouping(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) ⇒ Object
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 |
# File 'app/services/report/item_sale/item_sale.rb', line 711 def self.get_data_grouping(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) report_groupings_sql = report_groupings.empty? ? '' : " and report_grouping in (#{report_groupings.map { |rg| "'#{rg.sub("'", "''")}'" }.join(',')})" product_line_ids_sql = product_line_ids.nil? || product_line_ids.empty? ? '' : " and pl.id in (#{product_line_ids.map { |pg| pg.to_i }.join(',')})" customer_ids_sql = customer_ids.nil? || customer_ids.empty? ? '' : " and customer_id in (#{customer_ids.map { |c| c.to_i }.join(',')})" item_ids_sql = item_ids.nil? || item_ids.empty? ? '' : " and item_id in (#{item_ids.map { |i| i.to_i }.join(',')})" company_id_sql = company_ids.present? ? " and company_id in (#{company_ids.join(',')})" : '' state_codes_sql = state_codes.empty? ? '' : " and a.state_code in (#{state_codes.map { |sc| "'#{sc}'" }.join(',')})" revenue_sql = native_currency ? 'revenue_total' : 'revenue_total_consolidated' sql = <<-SQL select trim(split_part(pl.lineage_expanded,'>',1)) as detail,sum(#{revenue_sql}) as total_amount from view_sales_facts vsf left join addresses a on vsf.shipping_address_id = a.id inner join product_lines pl on vsf.primary_product_line_id = pl.id where gl_date between '#{dates[:date_from]}' and '#{dates[:date_to]}' and vsf.invoice_id not in (117052,117054,117065,117066,117375) #{report_groupings_sql} #{product_line_ids_sql} #{customer_ids_sql} #{item_ids_sql} #{company_id_sql} #{state_codes_sql} group by trim(split_part(pl.lineage_expanded,'>',1)) having sum(#{revenue_sql}) > 0 order by 2 desc SQL results = ActiveRecord::Base.connection.execute(sql).to_a.map { |r| r.symbolize_keys } end |
.get_data_heatmap(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) ⇒ Object
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 850 851 |
# File 'app/services/report/item_sale/item_sale.rb', line 818 def self.get_data_heatmap(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) report_groupings_sql = report_groupings.empty? ? '' : " and report_grouping in (#{report_groupings.map { |rg| "'#{rg.sub("'", "''")}'" }.join(',')})" product_line_ids_sql = product_line_ids.nil? || product_line_ids.empty? ? '' : " and pl.id in (#{product_line_ids.map { |pg| pg.to_i }.join(',')})" customer_ids_sql = customer_ids.nil? || customer_ids.empty? ? '' : " and customer_id in (#{customer_ids.map { |c| c.to_i }.join(',')})" item_ids_sql = item_ids.nil? || item_ids.empty? ? '' : " and item_id in (#{item_ids.map { |i| i.to_i }.join(',')})" company_id_sql = " and company_id = #{Company.select { |c| c.country_iso3 == company_ids }.first.id} " state_codes_sql = state_codes.empty? ? '' : " and a.state_code in (#{state_codes.map { |sc| "'#{sc}'" }.join(',')})" revenue_column = native_currency ? 'quantity * discounted_price' : 'quantity * consolidated_exchange_rate * discounted_price' native_currency_column_sql = " sum(#{revenue_column}) as total_amount " native_currency_sql = " having sum (#{revenue_column}) > 0 " sql = <<-SQL select ((case when company_id = 1 then 'us-' when company_id = 2 then 'ca-' else 'nl-' end) || lower(vsf.state_code)) as detail, #{native_currency_column_sql} from view_sales_facts vsf left join addresses a on vsf.shipping_address_id = a.id inner join product_lines pl on vsf.primary_product_line_id = pl.id inner join product_categories pc on vsf.product_category_id = pc.id where gl_date between '#{dates[:date_from]}' and '#{dates[:date_to]}' and vsf.invoice_id not in (117052,117054,117065,117066,117375) #{report_groupings_sql} #{product_line_ids_sql} #{customer_ids_sql} #{item_ids_sql} #{company_id_sql} #{state_codes_sql} group by company_id,vsf.state_code #{native_currency_sql} order by 2 desc SQL results = ActiveRecord::Base.connection.execute(sql).to_a.map { |r| r.symbolize_keys } end |
.get_data_items_table(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) ⇒ Object
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 627 628 629 630 631 632 633 634 635 636 637 |
# File 'app/services/report/item_sale/item_sale.rb', line 569 def self.get_data_items_table(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) report_groupings_sql = report_groupings.empty? ? '' : " and report_grouping in (#{report_groupings.map { |rg| "'#{rg.sub("'", "''")}'" }.join(',')})" product_line_ids_sql = product_line_ids.nil? || product_line_ids.empty? ? '' : " and pl.id in (#{product_line_ids.map { |pg| pg.to_i }.join(',')})" customer_ids_sql = customer_ids.nil? || customer_ids.empty? ? '' : " and customer_id in (#{customer_ids.map { |c| c.to_i }.join(',')})" item_ids_sql = item_ids.nil? || item_ids.empty? ? '' : " and item_id in (#{item_ids.map { |i| i.to_i }.join(',')})" company_id_sql = company_ids.present? ? " and company_id in (#{company_ids.join(',')})" : '' state_codes_sql = state_codes.empty? ? '' : " and a.state_code in (#{state_codes.map { |sc| "'#{sc}'" }.join(',')})" revenue_sql = native_currency ? 'revenue_total' : 'revenue_total_consolidated' consolidated_production = native_currency ? '* consolidated_exchange_rate' : nil sql = <<-SQL WITH sales_fact as ( select item_id,quantity,consolidated_exchange_rate,discounted_price,report_grouping,gl_date from view_sales_facts vsf inner join addresses a on vsf.shipping_address_id = a.id left join product_lines pl on vsf.primary_product_line_id = pl.id where (gl_date between '#{dates[:date_from_prev]}' and '#{dates[:date_to_prev]}' or gl_date between '#{dates[:date_from]}' and '#{dates[:date_to]}') and vsf.invoice_id not in (117052,117054,117065,117066,117375) and vsf.item_id is not null #{report_groupings_sql} #{product_line_ids_sql} #{customer_ids_sql} #{item_ids_sql} #{company_id_sql} #{state_codes_sql} ), returned_items as ( select item_id,quantity,exchange_rate,discounted_price,report_grouping,rma_date from view_returned_items vri inner join addresses a on vri.shipping_address_id = a.id where (rma_date between '#{dates[:date_from_prev]}' and '#{dates[:date_to_prev]}' or rma_date between '#{dates[:date_from]}' and '#{dates[:date_to]}') and vri.order_id not in (919564,919565,919591,919592,919593) and vri.item_id is not null #{report_groupings_sql} #{customer_ids_sql} #{item_ids_sql} #{company_id_sql} #{state_codes_sql} ), r_sales as ( select i.id,sku, sum(case when gl_date between '#{dates[:date_from]}' and '#{dates[:date_to]}' then quantity else 0 end) as qty, sum(case when gl_date between '#{dates[:date_from]}' and '#{dates[:date_to]}' then (quantity #{consolidated_production} * discounted_price) else 0 end) as sold_amount, sum(case when gl_date between '#{dates[:date_from_prev]}' and '#{dates[:date_to_prev]}' then quantity else 0 end) as previous_qty, sum(case when gl_date between '#{dates[:date_from_prev]}' and '#{dates[:date_to_prev]}' then (quantity #{consolidated_production} * discounted_price) else 0 end) as previous_sold_amount from sales_fact sf inner join items i on sf.item_id = i.id group by i.id,sku ), r_returned as ( select ri.item_id, sum(case when rma_date between '#{dates[:date_from]}' and '#{dates[:date_to]}' then quantity else 0 end) as r_qty, sum(case when rma_date between '#{dates[:date_from]}' and '#{dates[:date_to]}' then (quantity * exchange_rate * discounted_price) else 0 end) as r_sold_amount, sum(case when rma_date between '#{dates[:date_from_prev]}' and '#{dates[:date_to_prev]}' then quantity else 0 end) as r_previous_qty, sum(case when rma_date between '#{dates[:date_from_prev]}' and '#{dates[:date_to_prev]}' then (quantity * exchange_rate * discounted_price) else 0 end) as r_previous_sold_amount from returned_items ri group by ri.item_id ) select id as sku_id,sku,qty,sold_amount,coalesce(r_qty,0) as returned_qty,coalesce(r_sold_amount,0) as returned_sold_amount, previous_qty,previous_sold_amount,coalesce(r_previous_qty,0) as previous_returned_qty,coalesce(r_previous_sold_amount,0) as previous_returned_sold_amount from r_sales rs left join r_returned rr on rs.id = rr.item_id where (sold_amount > 0 or previous_sold_amount > 0) order by sold_amount desc; SQL results = ActiveRecord::Base.connection.execute(sql).to_a.map { |r| r.symbolize_keys } end |
.get_eu_data_heatmap(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, native_currency) ⇒ Object
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 |
# File 'app/services/report/item_sale/item_sale.rb', line 853 def self.get_eu_data_heatmap(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, native_currency) report_groupings_sql = report_groupings.empty? ? '' : " and report_grouping in (#{report_groupings.map { |rg| "'#{rg.sub("'", "''")}'" }.join(',')})" product_line_ids_sql = product_line_ids.nil? || product_line_ids.empty? ? '' : " and pl.id in (#{product_line_ids.map { |pg| pg.to_i }.join(',')})" customer_ids_sql = customer_ids.nil? || customer_ids.empty? ? '' : " and customer_id in (#{customer_ids.map { |c| c.to_i }.join(',')})" item_ids_sql = item_ids.nil? || item_ids.empty? ? '' : " and item_id in (#{item_ids.map { |i| i.to_i }.join(',')})" company_id_sql = " and company_id = #{Company.select { |c| c.country_iso3 == company_ids }.first.id} " revenue_column = native_currency ? 'quantity * discounted_price' : 'quantity * consolidated_exchange_rate * discounted_price' native_currency_column_sql = " sum(#{revenue_column}) as total_amount " native_currency_sql = " having sum (#{revenue_column}) > 0 " sql = <<-SQL select sa.country_iso3 as detail, #{native_currency_column_sql} from view_sales_facts vsf inner join product_lines pl on vsf.primary_product_line_id = pl.id inner join product_categories pc on vsf.product_category_id = pc.id inner join addresses sa on vsf.shipping_address_id = sa.id where gl_date between '#{dates[:date_from]}' and '#{dates[:date_to]}' and vsf.invoice_id not in (117052,117054,117065,117066,117375) #{report_groupings_sql} #{product_line_ids_sql} #{customer_ids_sql} #{item_ids_sql} #{company_id_sql} group by sa.country_iso3 #{native_currency_sql} order by 2 desc SQL results = ActiveRecord::Base.connection.execute(sql).to_a.map { |r| r.symbolize_keys } end |
.quick_view_by_product_line(dates, native_currency) ⇒ Object
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 |
# File 'app/services/report/item_sale/item_sale.rb', line 200 def self.quick_view_by_product_line(dates, native_currency) revenue_sql = native_currency ? 'revenue_total' : 'revenue_total_consolidated' sql = <<-SQL with sales as ( select trim(split_part(pl.lineage_expanded,'>',1)) as group1, case when (case when trim(split_part(pl.lineage_expanded,'>',2)) = '' then 'N/A' else trim(split_part(pl.lineage_expanded,'>',2)) end) in ('Control','Accessory','N/A') and trim(split_part(pl.lineage_expanded,'>',1)) = 'Floor Heating' then 'Floor Heating' || ' ' || (case when trim(split_part(pl.lineage_expanded,'>',2)) = '' then 'N/A' else trim(split_part(pl.lineage_expanded,'>',2)) end) when (case when trim(split_part(pl.lineage_expanded,'>',2)) = '' then 'N/A' else trim(split_part(pl.lineage_expanded,'>',2)) end) in ('Control','Accessory','N/A') and trim(split_part(pl.lineage_expanded,'>',1)) = 'Snow Melting' then 'Snow Melting' || ' ' || (case when trim(split_part(pl.lineage_expanded,'>',2)) = '' then 'N/A' else trim(split_part(pl.lineage_expanded,'>',2)) end) when (case when trim(split_part(pl.lineage_expanded,'>',2)) = '' then 'N/A' else trim(split_part(pl.lineage_expanded,'>',2)) end) in ('Control','Accessory','N/A') and trim(split_part(pl.lineage_expanded,'>',1)) = 'Towel Warmer' then 'Towel Warmer' || ' ' || (case when trim(split_part(pl.lineage_expanded,'>',2)) = '' then 'N/A' else trim(split_part(pl.lineage_expanded,'>',2)) end) else (case when trim(split_part(pl.lineage_expanded,'>',2)) = '' then 'N/A' else trim(split_part(pl.lineage_expanded,'>',2)) end) end as group2, sum(case when gl_date between '#{dates[:date_from]}' and '#{dates[:date_to]}' then (#{revenue_sql}) else 0 end) as cur_sales, sum(case when gl_date between '#{dates[:date_from_prev]}' and '#{dates[:date_to_prev]}' then (#{revenue_sql}) else 0 end) as prev_sales from view_sales_facts sf inner join analytic_date_time_dimensions dt on sf.gl_date = dt.date inner join product_lines pl on sf.primary_product_line_id = pl.id where (gl_date between '#{dates[:date_from_prev]}' and '#{dates[:date_to_prev]}' or gl_date between '#{dates[:date_from]}' and '#{dates[:date_to]}') and trim(split_part(pl.lineage_expanded,'>',1)) in ('Floor Heating','Snow Melting','Towel Warmer') and sf.item_id is not null group by trim(split_part(pl.lineage_expanded,'>',1)),case when trim(split_part(pl.lineage_expanded,'>',2)) = '' then 'N/A' else trim(split_part(pl.lineage_expanded,'>',2)) end order by 1,2 ), total as ( select group2,(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 group2,cur_sales,prev_sales,sales_var,growth from total; SQL results = ActiveRecord::Base.connection.execute(sql).to_a.map { |r| r.symbolize_keys } end |
.quick_view_customer_growth(dates, native_currency) ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'app/services/report/item_sale/item_sale.rb', line 179 def self.quick_view_customer_growth(dates, native_currency) revenue_sql = native_currency ? 'revenue_total' : 'revenue_total_consolidated' sql = <<-SQL select p.full_name,vsf.customer_id, sum(case when gl_date between '#{dates[:date_from]}' and '#{dates[:date_to]}' then #{revenue_sql} else 0 end ) as cur_sales, sum(case when gl_date between '#{dates[:date_from_prev]}' and '#{dates[:date_to_prev]}' then #{revenue_sql} else 0 end ) as prev_sales, sum(case when gl_date between '#{dates[:date_from]}' and '#{dates[:date_to]}' then #{revenue_sql} else 0 end ) - sum(case when gl_date between '#{dates[:date_from_prev]}' and '#{dates[:date_to_prev]}' then #{revenue_sql} else 0 end ) as sales_var, case when sum(case when gl_date between '#{dates[:date_from_prev]}' and '#{dates[:date_to_prev]}' then #{revenue_sql} else 0 end) = 0 then 0 else (sum(case when gl_date between '#{dates[:date_from]}' and '#{dates[:date_to]}' then #{revenue_sql} else 0 end) / sum(case when gl_date between '#{dates[:date_from_prev]}' and '#{dates[:date_to_prev]}' then #{revenue_sql} else 0 end) - 1) * 100 end as growth from view_sales_facts vsf inner join parties p on vsf.customer_id = p.id where (gl_date between '#{dates[:date_from_prev]}' and '#{dates[:date_to_prev]}' or gl_date between '#{dates[:date_from]}' and '#{dates[:date_to]}') and vsf.item_id is not null group by p.full_name,vsf.customer_id having sum(case when gl_date between '#{dates[:date_from]}' and '#{dates[:date_to]}' then #{revenue_sql} else 0 end ) > 0 and sum(case when gl_date between '#{dates[:date_from_prev]}' and '#{dates[:date_to_prev]}' then #{revenue_sql} else 0 end ) > 0 order by 5 desc; SQL results = ActiveRecord::Base.connection.execute(sql).to_a.map { |r| r.symbolize_keys } end |
.quick_view_sales(dates, native_currency, currency, company_ids) ⇒ Object
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 |
# File 'app/services/report/item_sale/item_sale.rb', line 152 def self.quick_view_sales(dates, native_currency, currency, company_ids) revenue_sql = native_currency ? 'revenue_total' : 'revenue_total_consolidated' company_id_sql = company_ids.present? ? " and company_id in (#{company_ids.join(',')})" : nil sql = <<-SQL with sales as ( select sum(case when gl_date between '#{dates[:date_from]}' and '#{dates[:date_to]}' then #{revenue_sql} else 0 end ) as cur_sales, sum(case when gl_date between '#{dates[:date_from_prev]}' and '#{dates[:date_to_prev]}' then #{revenue_sql} else 0 end ) as prev_sales from view_sales_facts where (gl_date between '#{dates[:date_from_prev]}' and '#{dates[:date_to_prev]}' or gl_date between '#{dates[:date_from]}' and '#{dates[:date_to]}') and item_id is not null #{company_id_sql} ), 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 ) 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 results = ActiveRecord::Base.connection.execute(sql).to_a.map { |r| r.symbolize_keys } results = [results[0].merge!(currency:)] end |
.total_report(options = {}) ⇒ Object
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'app/services/report/item_sale/item_sale.rb', line 12 def self.total_report( = {}) start_date = [:period1_gteq] end_date = [:period1_lteq] compare_start_date = [:period2_gteq] compare_end_date = [:period2_lteq] report_groupings = [:report_groupings].map(&:presence).compact product_lines = [:product_lines].map(&:presence).compact product_line_ids = [] if product_lines.present? all_product_lines = ProductLine.all.to_a product_lines.each do |product_line| all_product_lines.each { |pl| product_line_ids << pl.id if pl.&.split('>')&.first&.strip == product_line } end end customer_ids = [:customer_ids].map(&:presence).compact item_ids = [:item_ids].map(&:presence).compact native_currency = [:native_currency] company_ids = [:company_ids].blank? ? Company.sales_companies.all.map { |c| c.id } : [:company_ids].map(&:presence) company = company_ids.map { |id| Company.find(id).company_label[0, 3] } company_string = company.join('.') currency = native_currency ? Company.find([:company_ids]).map(&:currency).first : 'USD' currency_symbol = Money::Currency.new(currency).symbol state_codes = [:state_codes].map(&:presence).compact dates = dates_to_check(start_date, end_date, compare_start_date, compare_end_date) dates_by_day = dates_list(dates, 'daily') dates_by_week = dates_list(dates, 'weekly') days = [] weeks = [] dates_by_day.map { |r| days << r[:date] } dates_by_week.map { |r| weeks << r[:week] } weeks = weeks.uniq week_dates = week_dates(dates) quick_view_sales = quick_view_sales(dates, native_currency, currency, company_ids) quick_view_customer_growth = quick_view_customer_growth(dates, native_currency) quick_view_by_product_line = quick_view_by_product_line(dates, native_currency) data_product_line_daily_chart = get_data_by_product_line_chart(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, 'daily', native_currency) data_product_line_weekly_chart = get_data_by_product_line_chart(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, 'weekly', native_currency) data_product_line = get_data_by_product_line_table(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) data_customer_daily = get_data_by_customer(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, 'daily', native_currency) data_customer_weekly = get_data_by_customer(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, 'weekly', native_currency) customer_daily = data_column_char(data_customer_daily, days) customer_weekly = data_column_char(data_customer_weekly, weeks) customer_data_table = customer_data_table(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) data_by_report_grouping = get_data_by_report_grouping(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) data_by_report_grouping_bd = get_data_by_report_grouping_bd(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) data_items_table = get_data_items_table(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) data_grouping_pie = get_data_grouping(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) data_category_pie = get_data_category(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) data_hm_us = get_data_heatmap(dates, report_groupings, product_line_ids, customer_ids, item_ids, 'USA', state_codes, native_currency) data_hm_ca = get_data_heatmap(dates, report_groupings, product_line_ids, customer_ids, item_ids, 'CAN', state_codes, native_currency) data_hm_nl = get_eu_data_heatmap(dates, report_groupings, product_line_ids, customer_ids, item_ids, 'NLD', native_currency) data_top_1 = best_seller_by_product_line(start_date, end_date, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, 1, native_currency) data_top_2 = best_seller_by_product_line(start_date, end_date, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, 2, native_currency) data_top_3 = best_seller_by_product_line(start_date, end_date, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, 3, native_currency) data_top_4 = best_seller_by_product_line(start_date, end_date, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, 4, native_currency) Result.new(success: true, week_dates:, quick_view_sales:, quick_view_customer_growth:, quick_view_by_product_line:, data_product_line_daily_chart:, data_product_line_weekly_chart:, data_product_line:, dates_list: days, weeks_list: weeks, customer_daily:, customer_weekly:, customer_data_table:, data_by_report_grouping:, data_by_report_grouping_bd:, data_table: data_items_table, data_grouping_pie:, data_category_pie:, data_us_hm: data_hm_us, data_ca_hm: data_hm_ca, data_nl_hm: data_hm_nl, data_top_1:, data_top_2:, data_top_3:, data_top_4:, date_from: dates[:date_from], date_to: dates[:date_to], date_from_prev: dates[:date_from_prev], date_to_prev: dates[:date_to_prev], company_string:, native_currency:, currency:, company:, currency_symbol:) end |
.week_dates(dates) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'app/services/report/item_sale/item_sale.rb', line 135 def self.week_dates(dates) # Align date range with get_data_by_product_line_table to ensure week labels match table data # Uses 42 days (7 weeks) and skips the first 2 weeks (baseline + buffer) to return 5 weeks for display date_to = dates[:date_to].end_of_week - 2 date_from = (date_to - 42).beginning_of_week sql = <<-SQL SELECT DISTINCT ON (week_start_date) year, week, week_start_date FROM analytic_date_time_dimensions WHERE date BETWEEN '#{date_from}' AND '#{date_to}' ORDER BY week_start_date, year DESC OFFSET 2 LIMIT 5; SQL ActiveRecord::Base.connection.execute(sql).to_a.map { |r| r.symbolize_keys } end |