Class: Report::ItemSale::ItemSale
- Inherits:
-
Object
- Object
- Report::ItemSale::ItemSale
- Defined in:
- app/services/report/item_sale/item_sale.rb
Overview
Service object: item sale.
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) ⇒ Array<Hash{Symbol => Object}>
Returns the top five SKUs for the requested product-line rank.
-
.customer_data_table(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) ⇒ Array<Hash{Symbol => Object}>
Returns weekly customer sales for the customer table view.
-
.data_column_char(data, dates) ⇒ Array<Array>
Pivots raw customer sales rows into detail-name/series pairs.
-
.dates_list(dates, type) ⇒ Array<Hash{Symbol => Object}>
Returns daily or weekly date dimension rows for the given range.
-
.dates_to_check(start_date, end_date, compare_start_date, compare_end_date) ⇒ Hash{Symbol => Date}
Normalizes the current and comparison date ranges.
-
.get_data_by_customer(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, type, native_currency) ⇒ Array<Hash{Symbol => Object}>
Returns daily or weekly sales grouped by customer.
-
.get_data_by_product_line_chart(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, type, native_currency) ⇒ Array<Hash{Symbol => Object}>
Returns daily or weekly sales by product line for chart rendering.
-
.get_data_by_product_line_table(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) ⇒ Array<Hash{Symbol => Object}>
Returns weekly sales by product line for the table view.
-
.get_data_by_report_grouping(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) ⇒ Array<Hash{Symbol => Object}>
Returns sales summarized by report grouping.
-
.get_data_by_report_grouping_bd(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) ⇒ Array<Hash{Symbol => Object}>
Returns sales summarized by report grouping and customer.
-
.get_data_category(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) ⇒ Array<Hash{Symbol => Object}>
Returns sales grouped by product category for the category pie chart.
-
.get_data_grouping(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) ⇒ Array<Hash{Symbol => Object}>
Returns sales grouped by top-level product line for the grouping pie chart.
-
.get_data_heatmap(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) ⇒ Array<Hash{Symbol => Object}>
Returns state-level sales for the US/CA heatmap.
-
.get_data_items_table(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) ⇒ Array<Hash{Symbol => Object}>
Returns item-level sales and returns for the items table view.
-
.get_eu_data_heatmap(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, native_currency) ⇒ Array<Hash{Symbol => Object}>
Returns country-level sales for the EU heatmap.
-
.int_in_clause(column, values) ⇒ String
Builds a safe SQL
INclause for integer columns. -
.quick_view_by_product_line(dates, native_currency) ⇒ Array<Hash{Symbol => Object}>
Returns product-line current and prior sales for the quick-view cards.
-
.quick_view_customer_growth(dates, native_currency) ⇒ Array<Hash{Symbol => Object}>
Returns customer-level current and prior sales for the quick-view cards.
-
.quick_view_sales(dates, native_currency, currency, company_ids) ⇒ Array<Hash{Symbol => Object}>
Returns current and prior period sales totals for the quick-view cards.
-
.string_in_clause(column, values) ⇒ String
Builds a safe SQL
INclause for string columns. -
.total_report(options = {}) ⇒ Result
Builds the full item sale report result for the given filters.
-
.week_dates(dates) ⇒ Array<Hash{Symbol => Object}>
Returns the last five week labels for the given date range.
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) ⇒ Array<Hash{Symbol => Object}>
Returns the top five SKUs for the requested product-line rank.
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 |
# File 'app/services/report/item_sale/item_sale.rb', line 1006 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 = string_in_clause('report_grouping', report_groupings) product_line_ids_sql = int_in_clause('pl.id', product_line_ids) customer_ids_sql = int_in_clause('customer_id', customer_ids) item_ids_sql = int_in_clause('item_id', item_ids) company_id_sql = int_in_clause('company_id', company_ids) state_codes_sql = string_in_clause('a.state_code', state_codes) revenue_sql = native_currency ? 'revenue_total' : 'revenue_total_consolidated' sql = <<-SQL.squish 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 ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.customer_data_table(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) ⇒ Array<Hash{Symbol => Object}>
Returns weekly customer sales for the customer table view.
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 |
# File 'app/services/report/item_sale/item_sale.rb', line 646 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 = string_in_clause('vsf.report_grouping', report_groupings) product_line_ids_sql = int_in_clause('pl.id', product_line_ids) customer_ids_sql = int_in_clause('vsf.customer_id', customer_ids) item_ids_sql = int_in_clause('item_id', item_ids) company_id_sql = int_in_clause('vsf.company_id', company_ids) state_codes_sql = string_in_clause('a.state_code', state_codes) 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 ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.data_column_char(data, dates) ⇒ Array<Array>
Pivots raw customer sales rows into detail-name/series pairs.
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 |
# File 'app/services/report/item_sale/item_sale.rb', line 611 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) ⇒ Array<Hash{Symbol => Object}>
Returns daily or weekly date dimension rows for the given range.
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'app/services/report/item_sale/item_sale.rb', line 209 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.squish 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 ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.dates_to_check(start_date, end_date, compare_start_date, compare_end_date) ⇒ Hash{Symbol => Date}
Normalizes the current and comparison date ranges.
195 196 197 198 199 200 201 202 |
# File 'app/services/report/item_sale/item_sale.rb', line 195 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) ⇒ Array<Hash{Symbol => Object}>
Returns daily or weekly sales grouped by customer.
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 |
# File 'app/services/report/item_sale/item_sale.rb', line 547 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 = string_in_clause('vsf.report_grouping', report_groupings) product_line_ids_sql = int_in_clause('pl.id', product_line_ids) customer_ids_sql = int_in_clause('vsf.customer_id', customer_ids) item_ids_sql = int_in_clause('item_id', item_ids) company_id_sql = int_in_clause('vsf.company_id', company_ids) state_codes_sql = string_in_clause('a.state_code', state_codes) 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.squish 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 ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&: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) ⇒ Array<Hash{Symbol => Object}>
Returns daily or weekly sales by product line for chart rendering.
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 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 |
# File 'app/services/report/item_sale/item_sale.rb', line 358 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 = string_in_clause('report_grouping', report_groupings) product_line_ids_sql = int_in_clause('pl.id', product_line_ids) customer_ids_sql = int_in_clause('customer_id', customer_ids) item_ids_sql = int_in_clause('item_id', item_ids) company_id_sql = int_in_clause('company_id', company_ids) state_codes_sql = string_in_clause('a.state_code', state_codes) 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.squish 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)) = 'Infrared Heating Panels' then (#{revenue_sql}) else 0 end) as infraredpanels, sum(case when trim(split_part(lineage_expanded,'>',1)) not in ('Floor Heating','Snow Melting','Towel Warmer','Infrared Heating Panels') 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(infraredpanels, 0) as infraredpanels,coalesce(otherproduct, 0) as otherproduct from dates left join sales_data on dates.date = sales_data.date SQL ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&: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) ⇒ Array<Hash{Symbol => Object}>
Returns weekly sales by product line for the table view.
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 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 |
# File 'app/services/report/item_sale/item_sale.rb', line 441 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 = string_in_clause('report_grouping', report_groupings) product_line_ids_sql = int_in_clause('pl.id', product_line_ids) customer_ids_sql = int_in_clause('customer_id', customer_ids) item_ids_sql = int_in_clause('item_id', item_ids) company_id_sql = int_in_clause('company_id', company_ids) state_codes_sql = string_in_clause('a.state_code', state_codes) revenue_sql = native_currency ? 'revenue_total' : 'revenue_total_consolidated' sql = <<-SQL.squish 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, 'Infrared Heating Panels' 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','Infrared Heating Panels') 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','Infrared Heating Panels') 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 ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.get_data_by_report_grouping(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) ⇒ Array<Hash{Symbol => Object}>
Returns sales summarized by report grouping.
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 852 853 854 855 856 857 858 859 860 861 |
# File 'app/services/report/item_sale/item_sale.rb', line 827 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 = string_in_clause('vs.report_grouping', report_groupings) product_line_ids_sql = int_in_clause('pl.id', product_line_ids) customer_ids_sql = int_in_clause('vs.customer_id', customer_ids) item_ids_sql = int_in_clause('vs.item_id', item_ids) company_id_sql = int_in_clause('vs.company_id', company_ids) state_codes_sql = string_in_clause('a.state_code', state_codes) native_currency ? 'revenue_total' : 'revenue_total_consolidated' consolidated_production = native_currency ? '* consolidated_exchange_rate' : nil sql = <<-SQL.squish 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 ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&: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) ⇒ Array<Hash{Symbol => Object}>
Returns sales summarized by report grouping and customer.
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 |
# File 'app/services/report/item_sale/item_sale.rb', line 874 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 = string_in_clause('vs.report_grouping', report_groupings) product_line_ids_sql = int_in_clause('pl.id', product_line_ids) customer_ids_sql = int_in_clause('vs.customer_id', customer_ids) item_ids_sql = int_in_clause('vs.item_id', item_ids) company_id_sql = int_in_clause('vs.company_id', company_ids) state_codes_sql = string_in_clause('a.state_code', state_codes) native_currency ? 'revenue_total' : 'revenue_total_consolidated' consolidated_production = native_currency ? '* consolidated_exchange_rate' : nil sql = <<-SQL.squish 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 ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.get_data_category(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) ⇒ Array<Hash{Symbol => Object}>
Returns sales grouped by product category for the category pie chart.
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 |
# File 'app/services/report/item_sale/item_sale.rb', line 962 def self.get_data_category(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) report_groupings_sql = string_in_clause('report_grouping', report_groupings) product_line_ids_sql = int_in_clause('pl.id', product_line_ids) customer_ids_sql = int_in_clause('customer_id', customer_ids) item_ids_sql = int_in_clause('item_id', item_ids) company_id_sql = int_in_clause('company_id', company_ids) state_codes_sql = string_in_clause('a.state_code', state_codes) revenue_sql = native_currency ? 'revenue_total' : 'revenue_total_consolidated' sql = <<-SQL.squish 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 ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.get_data_grouping(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) ⇒ Array<Hash{Symbol => Object}>
Returns sales grouped by top-level product line for the grouping pie chart.
921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 |
# File 'app/services/report/item_sale/item_sale.rb', line 921 def self.get_data_grouping(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) report_groupings_sql = string_in_clause('report_grouping', report_groupings) product_line_ids_sql = int_in_clause('pl.id', product_line_ids) customer_ids_sql = int_in_clause('customer_id', customer_ids) item_ids_sql = int_in_clause('item_id', item_ids) company_id_sql = int_in_clause('company_id', company_ids) state_codes_sql = string_in_clause('a.state_code', state_codes) revenue_sql = native_currency ? 'revenue_total' : 'revenue_total_consolidated' sql = <<-SQL.squish 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 ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.get_data_heatmap(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) ⇒ Array<Hash{Symbol => Object}>
Returns state-level sales for the US/CA heatmap.
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 |
# File 'app/services/report/item_sale/item_sale.rb', line 1063 def self.get_data_heatmap(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) report_groupings_sql = string_in_clause('report_grouping', report_groupings) product_line_ids_sql = int_in_clause('pl.id', product_line_ids) customer_ids_sql = int_in_clause('customer_id', customer_ids) item_ids_sql = int_in_clause('item_id', item_ids) company_id_sql = " and company_id = #{Company.find { |c| c.country_iso3 == company_ids }.id} " state_codes_sql = string_in_clause('a.state_code', state_codes) 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.squish 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 ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.get_data_items_table(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, state_codes, native_currency) ⇒ Array<Hash{Symbol => Object}>
Returns item-level sales and returns for the items table view.
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 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 |
# File 'app/services/report/item_sale/item_sale.rb', line 746 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 = string_in_clause('report_grouping', report_groupings) product_line_ids_sql = int_in_clause('pl.id', product_line_ids) customer_ids_sql = int_in_clause('customer_id', customer_ids) item_ids_sql = int_in_clause('item_id', item_ids) company_id_sql = int_in_clause('company_id', company_ids) state_codes_sql = string_in_clause('a.state_code', state_codes) native_currency ? 'revenue_total' : 'revenue_total_consolidated' consolidated_production = native_currency ? '* consolidated_exchange_rate' : nil sql = <<-SQL.squish 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 ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.get_eu_data_heatmap(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, native_currency) ⇒ Array<Hash{Symbol => Object}>
Returns country-level sales for the EU heatmap.
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 |
# File 'app/services/report/item_sale/item_sale.rb', line 1108 def self.get_eu_data_heatmap(dates, report_groupings, product_line_ids, customer_ids, item_ids, company_ids, native_currency) report_groupings_sql = string_in_clause('report_grouping', report_groupings) product_line_ids_sql = int_in_clause('pl.id', product_line_ids) customer_ids_sql = int_in_clause('customer_id', customer_ids) item_ids_sql = int_in_clause('item_id', item_ids) company_id_sql = " and company_id = #{Company.find { |c| c.country_iso3 == company_ids }.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.squish 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 ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.int_in_clause(column, values) ⇒ String
Builds a safe SQL IN clause for integer columns.
59 60 61 62 63 64 65 66 |
# File 'app/services/report/item_sale/item_sale.rb', line 59 def self.int_in_clause(column, values) return '' if values.blank? ids = Array(values).map { |v| Integer(v) } return '' if ids.empty? " and #{column} in (#{ids.join(',')})" end |
.quick_view_by_product_line(dates, native_currency) ⇒ Array<Hash{Symbol => Object}>
Returns product-line current and prior sales for the quick-view cards.
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 |
# File 'app/services/report/item_sale/item_sale.rb', line 315 def self.quick_view_by_product_line(dates, native_currency) revenue_sql = native_currency ? 'revenue_total' : 'revenue_total_consolidated' sql = <<-SQL.squish 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 ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.quick_view_customer_growth(dates, native_currency) ⇒ Array<Hash{Symbol => Object}>
Returns customer-level current and prior sales for the quick-view cards.
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'app/services/report/item_sale/item_sale.rb', line 289 def self.quick_view_customer_growth(dates, native_currency) revenue_sql = native_currency ? 'revenue_total' : 'revenue_total_consolidated' sql = <<-SQL.squish 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 ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.quick_view_sales(dates, native_currency, currency, company_ids) ⇒ Array<Hash{Symbol => Object}>
Returns current and prior period sales totals for the quick-view cards.
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 |
# File 'app/services/report/item_sale/item_sale.rb', line 257 def self.quick_view_sales(dates, native_currency, currency, company_ids) revenue_sql = native_currency ? 'revenue_total' : 'revenue_total_consolidated' company_id_sql = int_in_clause('company_id', company_ids) sql = <<-SQL.squish 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.lease_connection.execute(sql).to_a.map(&:symbolize_keys) [results[0].merge!(currency:)] end |
.string_in_clause(column, values) ⇒ String
Builds a safe SQL IN clause for string columns.
73 74 75 76 77 78 |
# File 'app/services/report/item_sale/item_sale.rb', line 73 def self.string_in_clause(column, values) return '' if values.blank? quoted = Array(values).map { |v| ActiveRecord::Base.lease_connection.quote(v) } " and #{column} in (#{quoted.join(',')})" end |
.total_report(options = {}) ⇒ Result
Builds the full item sale report result for the given filters.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 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 179 180 181 182 183 184 185 186 |
# File 'app/services/report/item_sale/item_sale.rb', line 94 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].filter_map(&:presence) product_lines = [:product_lines].filter_map(&:presence) 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].filter_map(&:presence) item_ids = [:item_ids].filter_map(&:presence) native_currency = [:native_currency] company_ids = [:company_ids].blank? ? Company.sales_companies.pluck(: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].filter_map(&:presence) 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) ⇒ Array<Hash{Symbol => Object}>
Returns the last five week labels for the given date range.
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'app/services/report/item_sale/item_sale.rb', line 233 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.squish 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.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |