Module: Crm::OrderDashboardHelper

Defined in:
app/helpers/crm/order_dashboard_helper.rb

Overview

View helper: order dashboard.

Instance Method Summary collapse

Instance Method Details

#get_orders_from_selectionsObject



54
55
56
57
58
59
60
# File 'app/helpers/crm/order_dashboard_helper.rb', line 54

def get_orders_from_selections
  orders = Order.by_report_grouping_all_when_nil(@report_grouping)
  orders = orders.by_company_id(@company_id.to_i) if @company_id.present?
  orders = orders.by_primary_rep_id(@sales_rep_id.to_i) if @sales_rep_id.present?
  orders = orders.order("#{@sort_by} #{@sort_direction}") if @sort_by.present? && @sort_direction.present?
  orders
end

#order_dashboard_tab_optionsObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/helpers/crm/order_dashboard_helper.rb', line 4

def order_dashboard_tab_options
  params_hash = { company_id: @company_id, report_grouping: @report_grouping, sales_rep_id: @sales_rep_id, sort_by: @sort_by, sort_direction: @sort_direction }
  base = get_orders_from_selections

  # 9 of the 12 tab counters are simple `state = 'foo'` filters. Collapse
  # them into a single GROUP BY query — one DB round-trip instead of 9.
  state_to_tab = {
    'pending'                       => :pending,
    'profit_review'                 => :profit_review,
    'in_cr_hold'                    => :in_cr_hold,
    'crm_back_order'                => :back_order,
    'pending_release_authorization' => :pending_release_authorization,
    'pending_payment'               => :pending_payment,
    'pre_pack'                      => :pre_pack,
    'awaiting_carrier_assignment'   => :awaiting_carrier_assignment
  }
  state_to_tab['awaiting_deliveries'] = :awaiting_deliveries if @report_grouping == "Amazon"

  counters = state_to_tab.values.index_with { 0 }
  base.where(state: state_to_tab.keys).group(:state).count.each do |state, n|
    counters[state_to_tab[state]] = n
  end

  # Three remaining counters can't fold into the GROUP BY: two need a
  # JOIN onto deliveries, and future_release has a compound predicate.
  awaiting = base.all_awaiting_deliveries.with_associations.joins(:deliveries)
  counters[:orders_need_attention]          = awaiting.where("deliveries.state = 'quoting'").count
  counters[:dropship_orders_need_attention] = awaiting.where("deliveries.state = 'awaiting_po_fulfillment' OR deliveries.state = 'processing_po_fulfillment'").count
  counters[:future_release]                 = base.future_release.count

  tabs = {
    orders_need_attention:           order_dashboard_tab_orders_need_attention_path(params_hash),
    dropship_orders_need_attention:  order_dashboard_tab_dropship_orders_need_attention_path(params_hash),
    pending:                         order_dashboard_tab_pending_path(params_hash),
    profit_review:                   order_dashboard_tab_profit_review_path(params_hash),
    in_cr_hold:                      order_dashboard_tab_in_cr_hold_path(params_hash),
    back_order:                      order_dashboard_tab_back_order_path(params_hash),
    pending_release_authorization:   order_dashboard_tab_pending_release_authorization_path(params_hash),
    pending_payment:                 order_dashboard_tab_pending_payment_path(params_hash),
    pre_pack:                        order_dashboard_tab_pre_pack_path(params_hash),
    awaiting_carrier_assignment:     order_dashboard_tab_awaiting_carrier_assignment_path(params_hash),
    awaiting_deliveries:             order_dashboard_tab_awaiting_deliveries_path(params_hash),
    future_release:                  order_dashboard_tab_future_release_path(params_hash)
  }

  counters.each_with_object({}) do |(key, count), hsh|
    hsh[key] = { counter: count, remote_href: tabs[key] } if count.positive?
  end
end


68
69
70
71
72
# File 'app/helpers/crm/order_dashboard_helper.rb', line 68

def sort_column_link(sort_by, sort_by_name = nil)
  sort_by_name ||= sort_by.to_s.split(".").last.humanize.titleize
  link_to("#{sort_by_name} #{(params[:sort_direction] == 'ASC' ? fa_icon('chevron-up') : fa_icon('chevron-down')) if params[:sort_by] == sort_by}".html_safe,
order_dashboard_path(params.merge(sort_by: sort_by, sort_direction: (params[:sort_by] == sort_by && params[:sort_direction] == 'DESC' ? "ASC" : "DESC"))))
end

#stock_status_label(order) ⇒ Object



62
63
64
65
66
# File 'app/helpers/crm/order_dashboard_helper.rb', line 62

def stock_status_label(order)
  s = order.stock_status
  style = s == :ok ? 'success' : 'warning'
  (:span, s, class: "badge bg-#{style}", style: 'font-size: 1em')
end