Module: DeliveriesHelper

Defined in:
app/helpers/deliveries_helper.rb

Instance Method Summary collapse

Instance Method Details



40
41
42
# File 'app/helpers/deliveries_helper.rb', line 40

def add_delivery_shipment_link(name)
  link_to name, '#', onclick: "$('#shipments').append('#{j(render(partial: 'shipment', locals: { remove_link: true, package: WarehousePackage.new, shipment: Shipment.new }))}');return false", class: 'btn btn-outline-primary me-3'
end


36
37
38
# File 'app/helpers/deliveries_helper.rb', line 36

def add_shipment_link(name)
  link_to name, '#', onclick: "$('#shipments').append('#{j(render(partial: 'shipment', object: Shipment.new))}')"
end

#address_hash_to_s(address_hash) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/helpers/deliveries_helper.rb', line 124

def address_hash_to_s(address_hash)
  freight_fields_text = if address_hash[:is_residential]
                          '(Residential'
                        else
                          '(Commercial'
                        end
  freight_fields_text += (address_hash[:require_signature_by_default] ? ', require signature by default' : '')
  freight_fields_text += (address_hash[:has_loading_dock] ? ', has loading dock' : '')
  freight_fields_text += (address_hash[:requires_inside_delivery] ? ', requires inside delivery' : '')
  freight_fields_text += (address_hash[:requires_liftgate] ? ', requires lift-gate' : '')
  freight_fields_text += (address_hash[:is_construction_site] ? ', is construction site' : '')
  freight_fields_text += (address_hash[:limited_access] ? ', limited access' : '')
  freight_fields_text += (address_hash[:requires_appointment] ? ', requires delivery appointment' : '')
  freight_fields_text += (address_hash[:timezone_name].present? ? ", TZ: #{address_hash[:timezone_name]}" : '')
  freight_fields_text += ')'
  "#{[address_hash[:street1], address_hash[:street2], address_hash[:city], address_hash[:state_code], address_hash[:country_iso]].compact.join(', ')} #{freight_fields_text}"
end

#already_pre_packed?(delivery) ⇒ Boolean

Returns:

  • (Boolean)


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

def already_pre_packed?(delivery)
  delivery.shipments.all? { |s| s.packed_or_pre_packed? } && !(current_user.has_role?('warehouse_rep') || current_user.is_manager?) # don't allow pre-pack if already pre-packed, unless warehouse staff or admin
end

#amz_bs_badgeString

Get a badge for Amazon Buy Shipping carrier type

Returns:

  • (String)

    HTML-safe badge span



219
220
221
# File 'app/helpers/deliveries_helper.rb', line 219

def amz_bs_badge
  (:span, 'Amazon Buy Shipping', class: 'badge bg-warning text-dark', title: 'Amazon Buy Shipping rate with A-to-Z protections')
end

#delivery_request_estimated_packaging_form(delivery, return_path = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/helpers/deliveries_helper.rb', line 75

def delivery_request_estimated_packaging_form(delivery, return_path = nil)
  return unless show_delivery_pre_pack_option(delivery)

  pp_disabled = already_pre_packed?(delivery)
  simple_form_for delivery, url: request_estimated_packaging_delivery_path(delivery, return_path: return_path || polymorphic_url(delivery.resource, tab: 'shipping')),
                            html: { data: { turbo_frame: '_top' } } do |f|
    (:div, class: 'btn-group') do
      concat f.input_field(:suggested_packaging_text,
                           as: :string,
                           placeholder: 'Packaging directions (optional)',
                           size: 40,
                           class: 'form-control rounded-0 rounded-start')
      concat f.button(:submit, "#{pp_disabled ? 'Already packaged, contact Warehouse to request repackaging' : 'Request Estimated Packaging'}", class: 'btn btn-outline-primary', disabled: pp_disabled)
    end
  end
end


92
93
94
95
96
97
98
# File 'app/helpers/deliveries_helper.rb', line 92

def delivery_request_estimated_packaging_link(delivery, return_path = nil)
  return unless show_delivery_pre_pack_option(delivery)

  pp_disabled = already_pre_packed?(delivery)
  link_to("#{pp_disabled ? 'Already packaged, contact Warehouse to request repackaging' : 'Request Estimated Packaging'}",
          (pp_disabled ? 'javascript:void(0);' : workflow_action_delivery_path(delivery, return_path: return_path || polymorphic_url(delivery.resource, tab: 'shipping'), wf_action: 'request_estimated_packaging')), data: { turbo_method: :post, turbo_frame: '_top' }, class: 'btn btn-outline-primary')
end

#delivery_tab_options(delivery_presenter) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/helpers/deliveries_helper.rb', line 106

def delivery_tab_options(delivery_presenter)
  tab_panels = {
    main: { remote_href: tab_main_delivery_path(delivery_presenter.delivery) },
    shipping: { remote_href: tab_shipping_delivery_path(delivery_presenter.delivery) },
    line_items: { remote_href: tab_line_items_delivery_path(delivery_presenter.delivery) },
    activities: { counter: delivery_presenter.open_activities_counter, remote_href: delivery_activities_path(delivery_presenter.delivery, selected_activity: params[:selected_activity]) },
    attachments: { counter: delivery_presenter.uploads.size, remote_href: delivery_uploads_path(delivery_presenter.delivery) }
  }
  if delivery_presenter.delivery.order
    tab_panels.merge!({
                        invoices: { remote_href: tab_invoices_delivery_path(delivery_presenter.delivery) },
                        item_ledger: { remote_href: tab_item_ledger_delivery_path(delivery_presenter.delivery) },
                        account_ledger: { remote_href: (delivery_presenter.delivery) }
                      })
  end
  tab_panels
end


2
3
4
5
6
7
8
9
10
11
# File 'app/helpers/deliveries_helper.rb', line 2

def edit_delivery_packing_link(delivery = @delivery, return_path: nil)
  return unless delivery.shipment_contents_editable?(current_user)

  data_hsh = if current_user.has_role?('warehouse_rep')
               nil
             else
               { turbo_confirm: "Are you sure? Packaging is usually defined by the warehouse, but if you know what you're doing, please go ahead." }
             end
  link_to 'Edit Packing', picked_delivery_path(delivery, return_path:), data: (data_hsh || {}).merge(turbo_frame: '_top'), class: 'btn btn-outline-primary m-2'
end

#electronic_commercial_invoice_messageObject



142
143
144
145
146
# File 'app/helpers/deliveries_helper.rb', line 142

def electronic_commercial_invoice_message
  return unless @delivery.electronic_ship_ci_pdf || @delivery.should_have_electronic_commercial_invoice?

  'DO NOT PRINT COMMERCIAL INVOICE WHEN UPS OR FEDEX LABEL INDICATES EDI, EDI-PULL or ETD: IT IS HANDLED ELECTRONICALLY.'
end

#fields_for_shipment(shipment) ⇒ Object



31
32
33
34
# File 'app/helpers/deliveries_helper.rb', line 31

def fields_for_shipment(shipment, &)
  prefix = shipment.new_record? ? 'new' : 'existing'
  fields_for("delivery[#{prefix}_shipment_attributes][]", shipment, &)
end

#format_shipping_rate_with_marketplace_badge(shipping_cost, show_price: true) ⇒ String Also known as: format_shipping_rate_with_sww_badge

Format a shipping rate with appropriate badge if it's a marketplace rate
(Ship with Walmart or Amazon Buy Shipping)

Parameters:

  • shipping_cost (ShippingCost)

    The shipping cost record

  • show_price (Boolean) (defaults to: true)

    Whether to show the price

Returns:

  • (String)

    HTML-safe formatted string with optional marketplace badge



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

def format_shipping_rate_with_marketplace_badge(shipping_cost, show_price: true)
  return '' unless shipping_cost

  carrier = shipping_cost.respond_to?(:carrier) ? shipping_cost.carrier : shipping_cost.shipping_option&.carrier
  rd = shipping_cost.rate_data&.with_indifferent_access || {}

  parts = []

  if carrier == 'WalmartSeller' || is_sww_rate?(shipping_cost)
    parts << (:span, 'Ship with Walmart', class: 'badge bg-primary me-1')
    parts << [rd[:sww_carrier_name] || 'Unknown', rd[:sww_service_name].presence].compact.join(' - ')
  elsif carrier == 'AmazonSeller' || is_amz_bs_rate?(shipping_cost)
    parts << (:span, 'Amazon Buy Shipping', class: 'badge bg-warning text-dark me-1')
    parts << [rd[:amz_carrier_name] || 'Unknown', rd[:amz_service_name].presence].compact.join(' - ')
  else
    parts << (shipping_cost.try(:description) || shipping_cost.shipping_option&.name)
  end

  # Add price if requested
  if show_price && shipping_cost.cost.present?
    parts << ' - '
    parts << number_to_currency(shipping_cost.cost)
  end

  safe_join(parts)
end

#is_amz_bs_rate?(shipping_cost) ⇒ Boolean

Check if a shipping cost is an Amazon Buy Shipping rate

Parameters:

Returns:

  • (Boolean)


199
200
201
202
203
204
205
206
# File 'app/helpers/deliveries_helper.rb', line 199

def is_amz_bs_rate?(shipping_cost)
  return false unless shipping_cost

  rate_data = shipping_cost.rate_data
  return false unless rate_data.is_a?(Hash)

  rate_data['amz_carrier_id'].present? || rate_data[:amz_carrier_id].present?
end

#is_sww_rate?(shipping_cost) ⇒ Boolean

Check if a shipping cost is a Ship with Walmart rate

Parameters:

Returns:

  • (Boolean)


186
187
188
189
190
191
192
193
# File 'app/helpers/deliveries_helper.rb', line 186

def is_sww_rate?(shipping_cost)
  return false unless shipping_cost

  rate_data = shipping_cost.rate_data
  return false unless rate_data.is_a?(Hash)

  rate_data['sww_carrier_id'].present? || rate_data[:sww_carrier_id].present?
end

#pack_shipment_delete_button(f) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'app/helpers/deliveries_helper.rb', line 19

def pack_shipment_delete_button(f)
  if f.object.persisted?
    capture do
      concat f.hidden_field '_destroy'
      concat f.label '_destroy', fa_icon('trash'), class: 'inline'
      concat f.check_box '_destroy', class: 'destroy'
    end
  else
    button_tag(fa_icon('trash'), type: 'button', class: 'btn pack-shipment-remove', data: { action: 'delivery-nested-form#remove' })
  end
end

#render_ship_quotable_command_options(ship_quotable) ⇒ Object



100
101
102
103
104
# File 'app/helpers/deliveries_helper.rb', line 100

def render_ship_quotable_command_options(ship_quotable)
  (:ul, class: 'list-inline') do
    ship_quotable_command_options(ship_quotable).map { |o| (:li, o, class: 'list-inline-item') }.join.html_safe
  end
end

#setup_delivery_for_packing(delivery) ⇒ Object



13
14
15
16
17
# File 'app/helpers/deliveries_helper.rb', line 13

def setup_delivery_for_packing(delivery)
  delivery.tap do |d|
    d.shipments.build(state: 'suggested') unless d.shipments.any? { |s| s.suggested? || s.packed? || s.awaiting_label? }
  end
end

#ship_quotable_command_options(ship_quotable) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/helpers/deliveries_helper.rb', line 44

def ship_quotable_command_options(ship_quotable)
  opts = []
  unless ship_quotable.is_warehouse_pickup? or ship_quotable.try(:order_type) == Order::CREDIT_ORDER
    # Use data attributes to pass ship dates when recalculating
    opts << link_to('Recalculate Shipping for all Deliveries',
                    recalculate_shipping_all_deliveries_path(ship_quotable_type: @ship_quotable.class.to_s, ship_quotable_id: @ship_quotable.id, return_path: @return_path),
                    class: 'btn btn-outline-primary',
                    data: { controller: 'recalculate-shipping', action: 'click->recalculate-shipping#recalculate', turbo_frame: 'shipping_services' })
    if ship_quotable.single_origin
      opts << link_to('Allow multiple deliveries (drop ships)', multiple_origin_deliveries_path(ship_quotable_type: @ship_quotable.class.to_s, ship_quotable_id: @ship_quotable.id, return_path: @return_path),
                      class: 'btn btn-outline-primary',
                      data: { turbo_frame: 'shipping_services' })
    elsif can?(
      :change_delivery_origin, @ship_quotable
    )
      opts << link_to('Force one delivery from our warehouse', single_origin_deliveries_path(ship_quotable_type: @ship_quotable.class.to_s, ship_quotable_id: @ship_quotable.id, return_path: @return_path),
                      class: 'btn btn-outline-primary',
                      data: { turbo_frame: 'shipping_services' })
    end
  end
  opts
end

#show_delivery_pre_pack_option(delivery) ⇒ Object



67
68
69
# File 'app/helpers/deliveries_helper.rb', line 67

def show_delivery_pre_pack_option(delivery)
  !delivery.pre_pack? && delivery.resource && delivery.resource.can_request_estimated_packaging? && delivery.can_request_estimated_packaging? && !delivery.has_dropship_items? # don't allow pre-pack on custom or dropship items
end

#sww_badgeString

Get a badge for Walmart SWW carrier type
Can be used inline in any view to identify Walmart rates

Returns:

  • (String)

    HTML-safe badge span



212
213
214
# File 'app/helpers/deliveries_helper.rb', line 212

def sww_badge
  (:span, 'Ship with Walmart', class: 'badge bg-primary', title: 'Discounted shipping rate from Walmart marketplace')
end