Class: DeliveryPresenter

Inherits:
BasePresenter show all
Includes:
Presenters::StandardOperationsForRecord
Defined in:
app/presenters/delivery_presenter.rb

Overview

Presenter: delivery presenter.

Instance Attribute Summary

Attributes inherited from BasePresenter

#current_account, #options, #url_helper

Instance Method Summary collapse

Methods inherited from BasePresenter

#can?, #capture, #concat, #content_tag, #fa_icon, #h, #initialize, #link_to, #number_to_currency, #present, presents, #r, #safe_present, #simple_format, #u

Constructor Details

This class inherits a constructor from BasePresenter

Instance Method Details

#action_buttonObject



163
164
165
# File 'app/presenters/delivery_presenter.rb', line 163

def action_button
  h.render_simple_drop_down [r.human_state_name.titleize] + actions
end

#actions(return_path: nil, shipping_document_print_profile_id: nil, warehouse_context: nil) ⇒ Object



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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'app/presenters/delivery_presenter.rb', line 21

def actions(return_path: nil, shipping_document_print_profile_id: nil, warehouse_context: nil)
  return_path ||= h.delivery_path(delivery)

  actions = []

  if delivery.future_release?
    actions << warehouse_action_link('Release Now', :release_delivery_path, return_path:, warehouse_context:)
    actions << h.link_to('Edit Release Date', h.edit_release_date_delivery_path(delivery, return_path: return_path), class: 'dropdown-item')
    actions << h.link_to('Cancel/Hold Order (and reserve stock)', h.hold_delivery_path(delivery, reserve_stock: '1'), class: 'dropdown-item') unless delivery.locked_for_fba? && !h.user_has_role?('admin')
    actions << h.link_to('Cancel/Hold Order (and DO NOT reserve stock', h.hold_delivery_path(delivery), class: 'dropdown-item') unless delivery.locked_for_fba? && !h.user_has_role?('admin')
  elsif delivery.at_warehouse?
    if shipping_document_print_profile_id
      actions << h.link_to('Print Pick Slip', h.print_pick_slip_delivery_path(delivery, return_path: return_path, print_profile_id: shipping_document_print_profile_id), class: 'dropdown-item')
      actions << h.link_to('Print Pick Slip - Expand Kits/SN', h.print_pick_slip_delivery_path(delivery, split_kits: true, return_path: return_path, print_profile_id: shipping_document_print_profile_id), class: 'dropdown-item')
    end
    actions << h.link_to('Print Pick Slip (Open PDF)', h.print_pick_slip_delivery_path(delivery, return_path: return_path), class: 'dropdown-item')
    actions << h.link_to('Print Pick Slip - Expand Kits/SN (Open PDF)', h.print_pick_slip_delivery_path(delivery, split_kits: true, return_path: return_path), class: 'dropdown-item')
    actions << workflow_action_link('Advance to Picking', 'picking', return_path:, warehouse_context:)
    actions << h.link_to('Schedule Future Release', h.edit_release_date_delivery_path(delivery, return_path: return_path), class: 'dropdown-item')
    actions << h.link_to('Print Serial Numbers', h.print_serial_numbers_delivery_path(delivery, return_path: return_path), class: 'dropdown-item') if delivery.line_items.any?(&:require_reservation?)
    actions << workflow_action_link('Advance to Pending Ship Confirm', 'advance_to_pending_ship_confirm', return_path:, warehouse_context:) if delivery.can_advance_to_pending_ship_confirm?
    actions << h.link_to('Cancel/Hold Order (and reserve stock)', h.hold_delivery_path(delivery, reserve_stock: '1'), class: 'dropdown-item')
    actions << h.link_to('Cancel/Hold Order (and DO NOT reserve stock', h.hold_delivery_path(delivery), class: 'dropdown-item')
  elsif delivery.awaiting_po_fulfillment?
    actions << h.link_to('Cancel/Hold Order (and reserve stock)', h.hold_delivery_path(delivery, reserve_stock: '1'), class: 'dropdown-item')
    actions << h.link_to('Cancel/Hold Order (and DO NOT reserve stock', h.hold_delivery_path(delivery), class: 'dropdown-item')
  elsif delivery.pending_pickup_confirm? && !delivery.has_dropship_items? # when it does have dropship items, this is a warehouse pickup after we have dropshipped from a supplier to the warehouse: no going back
    actions << warehouse_action_link('Cancel Pickup/Re-pick', :cancel_pickup_delivery_path, return_path:, warehouse_context:)
    actions << warehouse_action_link('Delivery Shipped', :shipped_delivery_path, return_path:, warehouse_context:)
  elsif delivery.picking?
    actions << h.link_to('Picked', h.picked_delivery_path(delivery, return_path: return_path), class: 'dropdown-item')
    actions << h.link_to('Reprint Pick Slip', h.print_pick_slip_delivery_path(delivery, return_path: return_path, print_profile_id: shipping_document_print_profile_id), class: 'dropdown-item') if shipping_document_print_profile_id
    actions << h.link_to('Reprint Pick Slip (PDF)', h.print_pick_slip_delivery_path(delivery, return_path: return_path), class: 'dropdown-item')
    if shipping_document_print_profile_id
      actions << h.link_to('Reprint Pick Slip - Split Kit/SN', h.print_pick_slip_delivery_path(delivery, return_path: return_path, split_kits: true, print_profile_id: shipping_document_print_profile_id),
class: 'dropdown-item')
    end
    actions << h.link_to('Reprint Pick Slip - Split Kit/SN (PDF)', h.print_pick_slip_delivery_path(delivery, return_path: return_path, split_kits: true), class: 'dropdown-item')
    actions << h.link_to('Schedule Future Release', h.edit_release_date_delivery_path(delivery, return_path: return_path), class: 'dropdown-item')
    actions << h.link_to('Print Serial Numbers', h.print_serial_numbers_delivery_path(delivery, return_path: return_path), class: 'dropdown-item') if delivery.line_items.any?(&:require_reservation?)
    actions << h.link_to('Cancel/Hold Order (and reserve stock)', h.hold_delivery_path(delivery, reserve_stock: '1'), class: 'dropdown-item')
    actions << h.link_to('Cancel/Hold Order (and DO NOT reserve stock', h.hold_delivery_path(delivery), class: 'dropdown-item')
  elsif delivery.pre_pack?
    actions << h.link_to('Estimate Packaging', h.picked_delivery_path(delivery, return_path: return_path), class: 'dropdown-item')
  # rb_any_ship_from
  # elsif delivery.processing_po_fulfillment? # here we are going to provide the option to ship-label the drop-ship delivery
  #   actions << h.link_to("Picked", h.picked_delivery_path(delivery, return_path: return_path), class: 'dropdown-item')
  #   actions << h.link_to("Ship Label", h.ship_label_delivery_path(delivery, return_path: return_path), class: 'dropdown-item')
  elsif delivery.pending_ship_labels?
    actions << h.link_to('Ship Label', h.ship_label_delivery_path(delivery, return_path: return_path), class: 'dropdown-item')
    actions << workflow_action_link('Re-Pick', 'repick', return_path:, warehouse_context:) if delivery.can_repick?
    actions << h.link_to('Void Shipments', h.void_shipments_delivery_path(delivery, return_path: return_path), class: 'dropdown-item') if delivery.shipments.completed.any? && delivery.carrier != 'UPSFreight'
    actions << h.link_to('Picked', h.picked_delivery_path(delivery, return_path: return_path), class: 'dropdown-item')
    actions << h.link_to('Reprint Pick Slip', h.print_pick_slip_delivery_path(delivery, return_path: return_path, print_profile_id: shipping_document_print_profile_id), class: 'dropdown-item') if shipping_document_print_profile_id
    actions << h.link_to('Reprint Pick Slip (PDF)', h.print_pick_slip_delivery_path(delivery, return_path: return_path), class: 'dropdown-item')
    if shipping_document_print_profile_id
      actions << h.link_to('Reprint Pick Slip - Split Kit/SN', h.print_pick_slip_delivery_path(delivery, return_path: return_path, split_kits: true, print_profile_id: shipping_document_print_profile_id),
class: 'dropdown-item')
    end
    actions << h.link_to('Reprint Pick Slip - Split Kit/SN (PDF)', h.print_pick_slip_delivery_path(delivery, return_path: return_path, split_kits: true), class: 'dropdown-item')
    # actions << h.link_to("Add New Manual Shipment", h.new_shipment_path(delivery_id: delivery.id, return_path: return_path))
    actions << h.link_to('Schedule Future Release', h.edit_release_date_delivery_path(delivery, return_path: return_path), class: 'dropdown-item')
    actions << h.link_to('Cancel/Hold Order (and reserve stock)', h.hold_delivery_path(delivery, reserve_stock: '1'), class: 'dropdown-item') unless delivery.locked_for_fba? && !h.user_has_role?('admin')
    actions << h.link_to('Cancel/Hold Order (and DO NOT reserve stock)', h.hold_delivery_path(delivery), class: 'dropdown-item') unless delivery.locked_for_fba? && !h.user_has_role?('admin')
    actions << workflow_action_link('Advance to Pending Ship Confirm', 'advance_to_pending_ship_confirm', return_path:, warehouse_context:) if delivery.can_advance_to_pending_ship_confirm?

  elsif delivery.pending_ship_confirm? || delivery.pending_pickup_confirm?
    actions << warehouse_action_link('Delivery Shipped', :shipped_delivery_path, return_path:, warehouse_context:)
    actions << h.link_to('Picked', h.picked_delivery_path(delivery, return_path: return_path), class: 'dropdown-item')
    actions << h.link_to('Reprint Pick Slip', h.print_pick_slip_delivery_path(delivery, return_path: return_path, print_profile_id: shipping_document_print_profile_id), class: 'dropdown-item') if shipping_document_print_profile_id
    actions << h.link_to('Reprint Pick Slip (PDF)', h.print_pick_slip_delivery_path(delivery, return_path: return_path), class: 'dropdown-item')
    if shipping_document_print_profile_id
      actions << h.link_to('Reprint Pick Slip - Split Kit/SN', h.print_pick_slip_delivery_path(delivery, return_path: return_path, split_kits: true, print_profile_id: shipping_document_print_profile_id),
class: 'dropdown-item')
    end
    actions << h.link_to('Reprint Pick Slip - Split Kit/SN (PDF)', h.print_pick_slip_delivery_path(delivery, return_path: return_path, split_kits: true), class: 'dropdown-item')
    if delivery.shipments.completed.any? && !delivery.is_part_of_manifest?
      actions << h.link_to('Void Shipments', h.void_shipments_delivery_path(delivery, return_path: return_path), class: 'dropdown-item') if delivery.carrier != 'UPSFreight'
    end
    actions << workflow_action_link('Cancel Shipment', 'cancel_shipments', return_path:, warehouse_context:) if delivery.can_cancel_shipments?
  elsif delivery.pending_manifest_completion?
    actions << h.link_to('Void Shipments', h.void_shipments_delivery_path(delivery, return_path: return_path), class: 'dropdown-item') if delivery.shipments.completed.any? && !delivery.is_part_of_manifest?
  elsif delivery.pending_carrier_confirm?
    actions << h.link_to('Retry Asynch Carrier Confirm', h.retry_asynch_carrier_confirm_delivery_path(delivery, return_path: return_path), class: 'dropdown-item')
  elsif delivery.shipped? && !delivery.is_part_of_manifest?
    actions << h.link_to('Picked', h.picked_delivery_path(delivery, return_path: return_path), class: 'dropdown-item')
    actions << h.link_to('Reprint Pick Slip', h.print_pick_slip_delivery_path(delivery, return_path: return_path, print_profile_id: shipping_document_print_profile_id), class: 'dropdown-item') if shipping_document_print_profile_id
    actions << h.link_to('Reprint Pick Slip (PDF)', h.print_pick_slip_delivery_path(delivery, return_path: return_path), class: 'dropdown-item')
    if shipping_document_print_profile_id
      actions << h.link_to('Reprint Pick Slip - Split Kit/SN', h.print_pick_slip_delivery_path(delivery, return_path: return_path, split_kits: true, print_profile_id: shipping_document_print_profile_id),
class: 'dropdown-item')
    end
    actions << h.link_to('Reprint Pick Slip - Split Kit/SN (PDF)', h.print_pick_slip_delivery_path(delivery, return_path: return_path, split_kits: true), class: 'dropdown-item')
    if h.user_has_role?('warehouse_rep')
      actions << warehouse_action_link('Mark as pending ship/pickup confirm', :pending_confirm_delivery_path, return_path:, warehouse_context:)
      actions << h.link_to('Void Shipments', h.void_shipments_delivery_path(delivery, return_path: return_path), class: 'dropdown-item') if delivery.shipments.completed.any? && (delivery.carrier != 'UPSFreight')
    end
    actions << warehouse_action_link('Mark Invoiced', :trigger_invoiced_delivery_path, return_path:, warehouse_context:, html_opts: { method: :post }) if h.user_has_role?('accounting_rep')
  elsif delivery.service_ready_to_fulfill?
    actions << warehouse_action_link('Mark as Shipped', :shipped_delivery_path, return_path:, warehouse_context:)
  end

  if (bol_pdf = begin
    delivery.shipments.completed.first.bol_pdf
  rescue StandardError
    nil
  end)
    actions << h.link_to('View Bill of Lading', h.upload_path(bol_pdf), class: 'dropdown-item')
  end
  actions << h.link_to(fa_icon('external-link-alt', text: 'Generate Heatwave Bill of Lading'), h.bol_delivery_path(delivery, format: :pdf), class: 'dropdown-item', target: '_blank', rel: 'noopener noreferrer') if delivery.ltl_freight?
  actions << h.link_to(fa_icon('external-link-alt', text: 'Generate Commercial Invoice'), h.ci_delivery_path(delivery, format: :pdf), class: 'dropdown-item', target: '_blank', rel: 'noopener noreferrer') if delivery.is_international?
  if (ship_ci_pdf = begin
    delivery.ship_ci_pdf
  rescue StandardError
    nil
  end)
    actions << h.link_to('View Commercial Invoice', h.upload_path(ship_ci_pdf), class: 'dropdown-item')
  end
  if (electronic_ship_ci_pdf = begin
    delivery.electronic_ship_ci_pdf
  rescue StandardError
    nil
  end)
    actions << h.link_to('View Electronic Commercial Invoice (DO NOT PRINT)', h.upload_path(electronic_ship_ci_pdf), class: 'dropdown-item')
  end
  if delivery.is_cross_border?
    unless delivery.electronic_ship_ci_pdf.presence || h.user_has_role?('admin')
      actions << h.link_to(fa_icon('external-link-alt', text: 'Generate Heatwave Commercial Invoice'), h.ci_delivery_path(delivery, format: :pdf), class: 'dropdown-item',
target: '_blank', rel: 'noopener noreferrer')
    end
  end

  if delivery.can_print_carton_labels?
    actions << h.link_to('Print Carton/UCC-128 Labels', h.generate_carton_labels_path(delivery_id: delivery.id, return_path: return_path), class: 'dropdown-item')
    actions << h.link_to('Export Shipments Contents To CSV', h.export_shipments_contents_to_csv_delivery_path(delivery_id: delivery.id, return_path: return_path), class: 'dropdown-item')
  end

  actions << h.link_to(fa_icon('barcode', text: 'Print UPC Labels'), h.generate_barcodes_path(delivery_id: delivery.id, return_path: return_path), class: 'dropdown-item')
  actions << h.link_to(fa_icon('truck', text: 'Edit tracking numbers'), h.edit_tracking_delivery_path(delivery), class: 'dropdown-item') if delivery.can_update_tracking_info?
  actions
end

#actual_weight_infoObject



208
209
210
211
212
213
214
215
# File 'app/presenters/delivery_presenter.rb', line 208

def actual_weight_info
  return unless has_valid_shipments?
  return if (aw = delivery.actual_weight).blank?

  style = nil
  style = 'color: red;font-weight: bold' if delivery.actual_weight_discrepancy_exceeds_threshold?
  h.(:span, aw.ceil, style: style)
end


8
9
10
# File 'app/presenters/delivery_presenter.rb', line 8

def breadcrumb
  h.build_breadcrumb delivery
end

#contentable_shipmentsObject



242
243
244
# File 'app/presenters/delivery_presenter.rb', line 242

def contentable_shipments
  delivery.shipments.contentable.order(:id)
end

#delivery_subtotalObject



12
13
14
15
16
17
18
19
# File 'app/presenters/delivery_presenter.rb', line 12

def delivery_subtotal
  order = delivery.order
  if order && (order.order_type == Order::CREDIT_ORDER)
    h. :span, h.number_to_currency(order.total, unit: order.currency_symbol), class: 'return-order-total'
  else
    h. :span, h.number_to_currency(delivery.calculate_grand_total, unit: delivery.currency_symbol)
  end
end

#status_flowObject



167
168
169
170
171
172
173
174
175
# File 'app/presenters/delivery_presenter.rb', line 167

def status_flow
  statuses = +''

  delivery.state_list.each do |state|
    statuses << event_status(Delivery.state_machines[:state].states[state])
  end

  raw(statuses)
end

#uploads_for_warehouseObject



217
218
219
220
221
222
223
# File 'app/presenters/delivery_presenter.rb', line 217

def uploads_for_warehouse
  uploads = []
  uploads += delivery.uploads.to_a
  uploads += delivery.shipments.includes(:uploads).map(&:uploads).flatten.reject { |u| u.category == 'ship_label_pdf' } # there's an all label pdf already
  uploads += (delivery.order || delivery.quote).uploads.to_a
  uploads.sort_by { |u| [u.category.to_s, -u.created_at.to_i] }
end

#uploads_for_warehouse_dropdownObject



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'app/presenters/delivery_presenter.rb', line 225

def uploads_for_warehouse_dropdown
  links = [h.fa_icon('paperclip')]
  uploads_for_warehouse.each do |psu|
    title = (:div, class: 'row') do
      h.concat h.(:div, psu.category.humanize.titleize, class: 'float-start') if psu.category
      h.concat h.tag(:br)
      h.concat h.(:p, h.render_datetime(psu.created_at), class: 'text-muted')
    end
    links << link_to(title,
                h.upload_path(psu),
                target: '_blank', rel: 'noopener noreferrer')
  end
  return h.render_simple_drop_down links if links.size > 1

  nil
end

#warehouse_shipping_cost_infoObject



195
196
197
198
199
200
201
# File 'app/presenters/delivery_presenter.rb', line 195

def warehouse_shipping_cost_info
  return if delivery.actual_shipping_cost.blank?

  style = nil
  style = 'color: red; font-weight: bold' if delivery.actual_shipping_cost_exceeds_threshold?
  h.(:span, h.number_to_currency(delivery.actual_shipping_cost_to_show), style: style)
end

#warehouse_shipping_estimate_infoObject



203
204
205
206
# File 'app/presenters/delivery_presenter.rb', line 203

def warehouse_shipping_estimate_info
  estimated_ship_cost = delivery.shipping_cost
  h.number_to_currency(estimated_ship_cost)
end

#who_infoObject



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'app/presenters/delivery_presenter.rb', line 177

def who_info
  info = []
  if (order_rep = resource.primary_sales_rep || resource.customer.primary_sales_rep)
    r = "Rep: #{order_rep.full_name}"
    r += " x#{order_rep.pbx_extension}" if order_rep.pbx_extension
    info << r
  end
  if (order_creator = resource.creator)
    r = "Creator: #{order_creator.full_name}"
    if (pbx_ext = order_creator.try(:pbx_extension))
      r += " ext: #{pbx_ext}"
    end
    info << r
  end
  info << "Created on: #{h.render_datetime(delivery.created_at)}" if delivery.created_at
  info.each_with_index.map { |n, idx| idx.positive? ? (:a, n) : n }
end