Module: QuotesHelper

Defined in:
app/helpers/quotes_helper.rb

Overview

View helper: quotes.

Defined Under Namespace

Classes: QuoteRecipientPerson

Instance Method Summary collapse

Instance Method Details

#build_quote_party_hash(quote) ⇒ Object



61
62
63
64
65
66
67
68
# File 'app/helpers/quotes_helper.rb', line 61

def build_quote_party_hash(quote)
  parties_hash = {
    "#{quote.primary_party.full_name} (Primary)" => quote.primary_party,
    "#{quote.customer.full_name} (Billing Customer)" => { party: quote.customer, collapsed: false }
  }
  append_opportunity_participants_hash(quote.opportunity, parties_hash) if quote.opportunity
  parties_hash
end

#display_pre_pack_alerts(quote) ⇒ Object



80
81
82
# File 'app/helpers/quotes_helper.rb', line 80

def display_pre_pack_alerts(quote)
  render partial: '/shared/pre_pack_alerts', locals: { quote: quote }
end

#display_ship_insurance_alerts(quote) ⇒ Object



84
85
86
# File 'app/helpers/quotes_helper.rb', line 84

def display_ship_insurance_alerts(quote)
  render partial: '/shared/ship_insurance_alerts', locals: { ship_quotable: quote }
end

#fields_for_shipping_cost(shipping_cost) ⇒ Object



70
71
72
73
# File 'app/helpers/quotes_helper.rb', line 70

def fields_for_shipping_cost(shipping_cost, &)
  prefix = shipping_cost.new_record? ? 'new' : 'existing'
  fields_for("delivery[#{prefix}_shipping_cost_attributes][]", shipping_cost, &)
end

#gather_quote_phones(_opp) ⇒ Object



75
76
77
78
# File 'app/helpers/quotes_helper.rb', line 75

def gather_quote_phones(_opp)
  phones = [] + @quote.primary_party.contact_points.voice_callable.limit(3) + @opportunity.customer.contact_points.voice_callable.limit(3)
  phones.compact.uniq
end

#get_quote_profit_label(quote) ⇒ Object



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

def get_quote_profit_label(quote)
  profit_label = +'Profit: '
  profit_label << colored_profit_status(quote)
  profit_label << " (#{colored_profit(quote)}, min: #{quote.min_profit_markup} %)"
  profit_label.html_safe
end

#quote_command_options(quote = nil) ⇒ Object



88
89
90
91
92
93
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
# File 'app/helpers/quotes_helper.rb', line 88

def quote_command_options(quote = nil)
  quote ||= @quote
  opts = []
  opts << quote.human_state_name.titleize
  opts << link_to(fa_icon('envelope', text: 'Transmit'), select_pdf_template_quote_path(quote)) if quote.can_be_transmitted?
  opts << if quote.can_convert? && can?(:place_orders, quote.customer)
            link_to(fa_icon('circle-dollar', text: 'Convert to Order'), convert_to_order_quote_path(quote),
                    { class: 'transactional', data: { controller: 'ui-blocker', turbo_method: :put } })
          else
            { content: 'Cannot be converted to order', class: 'dropdown-item-text text-body-secondary' }
          end
  has_active_pre_pack = quote.has_pre_pack_deliveries?

  if quote.complete?
    requote_data = { controller: 'ui-blocker', turbo_method: :post }
    requote_data[:turbo_confirm] = 'WARNING: This quote has an active pre-pack in the warehouse. Requoting will notify the warehouse to stop. Continue?' if has_active_pre_pack
    opts << link_to(fa_icon('clone', text: 'Requote/Redesign'), requote_quote_path(quote), { class: 'transactional', data: requote_data })
  end
  opts << link_to(fa_icon('file', text: 'Preview Quote pdf'), quote_path(quote, format: :pdf))
  opts << link_to(fa_icon('triangle-exclamation', text: 'Preview Quote pdf (refresh ALL room plans)'),
                  quote_path(quote, format: :pdf, regenerate_plans: true),
                  data: { turbo_confirm: 'This will regenerate all linked room plans. Are you sure? Use only as needed!' })
  opts << link_to(fa_icon('file', text: 'Get Quote link'), link_quote_path(quote))
  opts << link_to(fa_icon('clock-rotate-left', text: 'View customer activity (/ql/)'),
                  activity_log_quote_path(quote))
  opts << link_to(fa_icon('file', text: 'Preview Materials pdf'), quote_path(quote, materials_only: true, format: :pdf))
  opts << link_to(fa_icon('forward', text: 'Process Quote'), shipping_quote_path(quote)) if quote.pending?
  if can?(:manage_profit_margins, quote)
    opts << link_to(fa_icon('chart-pie', text: 'Review/Update Profit Margins'),
                    profit_margins_quote_path(quote))
  end
  opts << link_to(fa_icon('face-frown', text: 'Opportunity Lost'), workflow_action_opportunity_path(quote.opportunity, wf_action: 'lose')) if quote.opportunity.can_lose?
  cancel_confirm = has_active_pre_pack ? { event: :cancel, prompt: 'WARNING: This quote has an active pre-pack in the warehouse. Cancelling will notify the warehouse to stop. Continue?' } : {}
  if (events = available_events_links(quote, {}, cancel_confirm, ['request_estimated_packaging'])).present?
    opts << { class: 'divider' }
    opts += events
    opts << { class: 'divider' }
  end
  pp_disabled = (show_quote_pre_pack_option(quote) ? false : true)
  pp_label = pp_disabled ? 'Already packaged, contact Warehouse to request repackaging' : 'Request Estimated Packaging'
  pp_link_opts = pp_disabled ? { class: 'disabled', aria: { disabled: true }, tabindex: -1 } : {}
  opts << link_to(fa_icon('right-from-bracket', text: pp_label),
                  (pp_disabled ? '#' : workflow_action_quote_path(quote, wf_action: 'request_estimated_packaging')),
                  pp_link_opts)

  opts << link_to(fa_icon('right-from-bracket', text: 'Move'), move_quote_path(quote))
  opts << link_to(fa_icon('copy', text: 'Copy'), copy_quote_path(quote))

  opts << link_to(fa_icon('pen-to-square', text: 'Edit'), edit_quote_path(quote)) if can?(:update, quote)
  if can? :destroy, quote
    opts << link_to(fa_icon('trash', text: 'Delete'),
                    quote_path(quote),
                    data: { turbo_confirm: 'Are you sure want to delete this quote?', turbo_method: :delete })
  end

  opts
end

#quote_quick_info(quote = @quote) ⇒ Object



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/quotes_helper.rb', line 11

def quote_quick_info(quote = @quote)
  info = []
  info << if @quote.hold_for_transmission
            fa_icon('ban', style: 'color: red', text: 'Hold for transmission')
          else
            fa_icon('check', style: 'color: green', text: 'Release when ready')
          end
  # `priority` is derived from room configurations (Quote#set_priority) and
  # stays nil until a quote has rooms — skip the badge rather than crash.
  info << fa_icon('clock', text: quote.priority.titleize, style: { 'same day' => 'color:red', 'next day' => 'color:orange' }[quote.priority]) if quote.priority.present?
  info << fa_icon('right-left', text: 'RMA Redesign') if quote.rma
  info << employee_badge(quote.primary_sales_rep, title: 'Primary Sales Rep') if quote.primary_sales_rep
  info << fa_icon('file-invoice-dollar', text: number_to_currency(quote.total, unit: quote.currency_symbol))
  info << fa_icon('tags', text: "Effective Discount: #{quote.effective_discount} %".html_safe)
  # Build the customer-facing URL with the right host + locale prefix so
  # the link works even when the running server's Rack::Rewrite is older
  # than the `ql` protection in the locale-normalization sweep. The
  # encrypted token comes from Encryption.encrypt_string(quote.id.to_s).
  ql_locale = (quote.catalog&.store&.country_iso3 == 'CAN' ? :'en-CA' : :'en-US')
  ql_url = Web::UrlBuilder.new.process(
    "/ql/#{Encryption.encrypt_string(quote.id.to_s)}",
    ql_locale,
    host: WEB_HOSTNAME_WITHOUT_PORT,
    port: APP_PORT_NUMBER
  )
  info << fa_icon('link', text: safe_join([
                                            link_to('Customer link', ql_url, target: '_blank', rel: 'noopener'),
                                            ' ',
                                            clipboard_copy(ql_url, mode: :icon, title: 'Copy customer link')
                                          ]))
  if quote.is_sales_quote? && quote.track_profit?
    if can?(:manage_profit_margins, quote)
      profit_info = link_to(profit_margins_quote_path(quote), class: 'px-2 nav-link') do
        safe_join([fa_icon('chart-pie'), (:span, get_quote_profit_label(quote), class: 'copy-target')], ' ')
      end
      info << profit_info
    else
      info << (:span, fa_icon('chart-pie', text: "Profit Status: #{colored_profit_status(quote)}".html_safe))
    end
  end
  info
end

#quote_recipient_people(quote) ⇒ Array<QuoteRecipientPerson>

Parameters:

Returns:



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'app/helpers/quotes_helper.rb', line 197

def quote_recipient_people(quote)
  customer = quote.opportunity&.customer
  return [] unless customer

  # Pre-checked = the customer (always receives the quote). Contacts with an
  # email on file can be ticked as additional recipients. If the quote has
  # previously-selected contact points from the edit page, pre-check those too.
  people = [[customer, 'Customer']] + customer.contacts.includes(:contact_points, :accounts).map { |c| [c, 'Contact'] }

  # Get the IDs of contact points already selected on the quote
  selected_contact_point_ids = quote.contact_points.map(&:id).to_set

  people.map do |party, role|
    email_cp = party.contact_points.detect { |cp| cp.category == ContactPoint::EMAIL && cp.detail.present? }
    QuoteRecipientPerson.new(
      party: party,
      role: role,
      email_contact_point: email_cp,
      can_login: party.accounts.any?,
      preselected: party.id == customer.id || (email_cp && selected_contact_point_ids.include?(email_cp.id))
    )
  end
end

#quote_tab_optionsObject



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
# File 'app/helpers/quotes_helper.rb', line 146

def quote_tab_options
  {
    line_items: { remote_href: tab_line_items_quote_path(@quote) },
    material_alerts: { remote_href: tab_material_alerts_quote_path(@quote) },
    main: { remote_href: tab_main_quote_path(@quote) },
    contacts: { remote_href: tab_contacts_quote_path(@quote) },
    shipping: { remote_href: tab_shipping_quote_path(@quote) },
    rooms: {
      counter: @quote.room_configurations.size,
      title: +'Rooms & Areas',
      remote_href: tab_rooms_quote_path(@quote)
    },
    activities: {
      counter: @quote.open_activities_counter,
      remote_href: tab_activities_quote_path(@quote, selected_activity: params[:selected_activity])
    },
    communications: { remote_href: tab_communications_quote_path(@quote) },
    sms: {
      remote_href: tab_sms_quote_path(@quote),
      counter: @quote.sms_messages.unread.size
    },
    attachments: { remote_href: tab_attachments_quote_path(@quote) },
    tickets: {
      title: 'Tickets',
      counter_html: tech_counter_badge(@quote.support_cases),
      remote_href: tab_tickets_quote_path(@quote)
    },
    inventory: { remote_href: tab_inventory_quote_path(@quote) },
    revisions: { remote_href: tab_revisions_quote_path(@quote) }
  }
end

#setup_quote(quote) ⇒ Object



5
6
7
8
9
# File 'app/helpers/quotes_helper.rb', line 5

def setup_quote(quote)
  quote.tap do |q|
    q.quote_type ||= quote.prefix_for_reference_number
  end
end

#show_quote_pre_pack_option(quote) ⇒ Object



178
179
180
# File 'app/helpers/quotes_helper.rb', line 178

def show_quote_pre_pack_option(quote)
  (quote.deliveries.any? && quote.deliveries.any? { |d| d.shipments.any? { |s| !s.packed_or_pre_packed? } }) || (current_user&.has_role?('warehouse_rep') || current_user&.is_manager?)
end