Module: Crm::OrderDashboardHelper

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

Overview

View helpers for the CRM order dashboard.

Builds the tab counters/URLs rendered by the order dashboard views and
provides the sortable-column links and stock-status badges used by its
orders table.

Instance Method Summary collapse

Instance Method Details

#get_orders_from_selectionsActiveRecord::Relation<Order>

Builds the base Order relation for the dashboard from the current
selections (report grouping, company, sales rep) and applies sorting.

Returns:

  • (ActiveRecord::Relation<Order>)

    the filtered, sorted orders relation



72
73
74
75
76
77
78
# File 'app/helpers/crm/order_dashboard_helper.rb', line 72

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_optionsHash{Symbol => Hash}

Builds the tab options hash for the order dashboard.

Counters for the state-filtered tabs are collapsed into a single GROUP BY
query; the remaining counters (delivery-joined and future-release) run
separately. Tabs with a zero counter are omitted.

Returns:

  • (Hash{Symbol => Hash})

    tab key to { counter:, remote_href: } options



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
# File 'app/helpers/crm/order_dashboard_helper.rb', line 18

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

Renders a sortable column header link for the dashboard orders table.

Clicking the link toggles the sort direction when the column is already
the active sort; an arrow icon marks the active column.

Parameters:

  • sort_by (String)

    the column/expression to sort by

  • sort_by_name (String, nil) (defaults to: nil)

    display label; derived from sort_by when nil

Returns:

  • (String)

    HTML-safe link markup



98
99
100
101
102
# File 'app/helpers/crm/order_dashboard_helper.rb', line 98

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) ⇒ String

Renders a badge showing an order's stock status.

Parameters:

  • order (Order)

    the order whose stock status to display

Returns:

  • (String)

    HTML-safe badge markup (success when :ok, else warning)



84
85
86
87
88
# File 'app/helpers/crm/order_dashboard_helper.rb', line 84

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