Class: Spree::Shipment

Inherits:
Base
  • Object
show all
Extended by:
DisplayMoney
Defined in:
app/models/spree/shipment.rb

Overview

An order's planned shipments including tracking and cost.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DisplayMoney

money_methods

Methods inherited from Base

display_includes, #initialize_preference_defaults, page, preference

Methods included from Preferences::Preferable

#default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference

Instance Attribute Details

#special_instructionsObject

TODO: remove the suppress_mailer temporary variable once we are calling 'ship' from outside of the state machine and can actually pass variables through.



21
22
23
# File 'app/models/spree/shipment.rb', line 21

def special_instructions
  @special_instructions
end

#suppress_mailerObject

TODO: remove the suppress_mailer temporary variable once we are calling 'ship' from outside of the state machine and can actually pass variables through.



21
22
23
# File 'app/models/spree/shipment.rb', line 21

def suppress_mailer
  @suppress_mailer
end

Instance Method Details

#add_shipping_method(shipping_method, selected = false) ⇒ Object



100
101
102
# File 'app/models/spree/shipment.rb', line 100

def add_shipping_method(shipping_method, selected = false)
  shipping_rates.create(shipping_method: shipping_method, selected: selected, cost: cost)
end

#addressObject



380
381
382
383
# File 'app/models/spree/shipment.rb', line 380

def address
  Spree::Deprecation.warn("Calling Shipment#address is deprecated. Use Order#ship_address instead", caller)
  order.ship_address if order
end

#after_cancelObject



105
106
107
# File 'app/models/spree/shipment.rb', line 105

def after_cancel
  manifest.each { |item| manifest_restock(item) }
end

#after_resumeObject



109
110
111
# File 'app/models/spree/shipment.rb', line 109

def after_resume
  manifest.each { |item| manifest_unstock(item) }
end

#backordered?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'app/models/spree/shipment.rb', line 113

def backordered?
  inventory_units.any?(&:backordered?)
end

#can_transition_from_canceled_to_ready?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'app/models/spree/shipment.rb', line 87

def can_transition_from_canceled_to_ready?
  can_transition_from_pending_to_ready?
end

#can_transition_from_pending_to_ready?Boolean

Returns:

  • (Boolean)


81
82
83
84
85
# File 'app/models/spree/shipment.rb', line 81

def can_transition_from_pending_to_ready?
  order.can_ship? &&
    inventory_units.all? { |iu| iu.allow_ship? || iu.canceled? } &&
    (order.paid? || !Spree::Config[:require_payment_to_ship])
end

#can_transition_from_pending_to_shipped?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'app/models/spree/shipment.rb', line 77

def can_transition_from_pending_to_shipped?
  !requires_shipment?
end

#currencyObject



117
118
119
# File 'app/models/spree/shipment.rb', line 117

def currency
  order ? order.currency : Spree::Config[:currency]
end

#determine_state(order) ⇒ Object

Determines the appropriate state according to the following logic:

canceled if order is canceled pending unless order is complete and order.payment_state is paid shipped if already shipped (ie. does not change the state) ready all other cases



251
252
253
254
255
256
257
258
259
260
# File 'app/models/spree/shipment.rb', line 251

def determine_state(order)
  return 'canceled' if order.canceled?
  return 'shipped' if shipped?
  return 'pending' unless order.can_ship?
  if can_transition_from_pending_to_ready?
    'ready'
  else
    'pending'
  end
end

#discounted_costObject Also known as: discounted_amount



121
122
123
# File 'app/models/spree/shipment.rb', line 121

def discounted_cost
  cost + promo_total
end

#editable_by?(_user) ⇒ Boolean

Returns:

  • (Boolean)


156
157
158
# File 'app/models/spree/shipment.rb', line 156

def editable_by?(_user)
  !shipped?
end

#finalize!Object

Decrement the stock counts for all pending inventory units in this shipment and mark. Any previous non-pending inventory units are skipped as their stock had already been allocated.



164
165
166
167
168
169
170
171
# File 'app/models/spree/shipment.rb', line 164

def finalize!
  transaction do
    pending_units = inventory_units.select(&:pending?)
    pending_manifest = Spree::ShippingManifest.new(inventory_units: pending_units)
    pending_manifest.items.each { |item| manifest_unstock(item) }
    Spree::InventoryUnit.finalize_units!(pending_units)
  end
end

#include?(variant) ⇒ Boolean

Returns:

  • (Boolean)


173
174
175
# File 'app/models/spree/shipment.rb', line 173

def include?(variant)
  inventory_units_for(variant).present?
end

#inventory_units_for(variant) ⇒ Object



177
178
179
# File 'app/models/spree/shipment.rb', line 177

def inventory_units_for(variant)
  inventory_units.where(variant_id: variant.id)
end

#inventory_units_for_item(line_item, variant = nil) ⇒ Object



181
182
183
# File 'app/models/spree/shipment.rb', line 181

def inventory_units_for_item(line_item, variant = nil)
  inventory_units.where(line_item_id: line_item.id, variant_id: line_item.variant.id || variant.id)
end

#item_costObject



185
186
187
# File 'app/models/spree/shipment.rb', line 185

def item_cost
  line_items.map(&:total).sum
end

#line_itemsObject



189
190
191
# File 'app/models/spree/shipment.rb', line 189

def line_items
  inventory_units.includes(:line_item).map(&:line_item).uniq
end

#manifestObject



226
227
228
# File 'app/models/spree/shipment.rb', line 226

def manifest
  @manifest ||= Spree::ShippingManifest.new(inventory_units: inventory_units).items
end

#ready_or_pending?Boolean

Returns:

  • (Boolean)


193
194
195
# File 'app/models/spree/shipment.rb', line 193

def ready_or_pending?
  ready? || pending?
end

#refresh_ratesObject



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

def refresh_rates
  return shipping_rates if shipped?
  return [] unless can_get_rates?

  # StockEstimator.new assigment below will replace the current shipping_method
  original_shipping_method_id = shipping_method.try!(:id)

  new_rates = Spree::Config.stock.estimator_class.new.shipping_rates(to_package)

  # If one of the new rates matches the previously selected shipping
  # method, select that instead of the default provided by the estimator.
  # Otherwise, keep the default.
  selected_rate = new_rates.detect{ |rate| rate.shipping_method_id == original_shipping_method_id }
  if selected_rate
    new_rates.each do |rate|
      rate.selected = (rate == selected_rate)
    end
  end

  self.shipping_rates = new_rates
  save!

  shipping_rates
end

#requires_shipment?Boolean

Returns:

  • (Boolean)


376
377
378
# File 'app/models/spree/shipment.rb', line 376

def requires_shipment?
  !stock_location || stock_location.fulfillable?
end

#selected_shipping_rateObject



222
223
224
# File 'app/models/spree/shipment.rb', line 222

def selected_shipping_rate
  shipping_rates.detect(&:selected?)
end

#selected_shipping_rate_idObject



230
231
232
# File 'app/models/spree/shipment.rb', line 230

def selected_shipping_rate_id
  selected_shipping_rate.try(:id)
end

#selected_shipping_rate_id=(id) ⇒ Object



234
235
236
237
238
239
240
241
242
243
# File 'app/models/spree/shipment.rb', line 234

def selected_shipping_rate_id=(id)
  selected_shipping_rate.update(selected: false) if selected_shipping_rate
  new_rate = shipping_rates.detect { |rate| rate.id == id.to_i }
  fail(
    ArgumentError,
    "Could not find shipping rate id #{id} for shipment #{number}"
  ) unless new_rate
  new_rate.update(selected: true)
  save!
end

#set_up_inventory(state, variant, order, line_item) ⇒ Object



262
263
264
265
266
267
268
269
# File 'app/models/spree/shipment.rb', line 262

def set_up_inventory(state, variant, order, line_item)
  inventory_units.create(
    state: state,
    variant_id: variant.id,
    order_id: order.id,
    line_item_id: line_item.id
  )
end

#shipped=(value) ⇒ Object



271
272
273
274
# File 'app/models/spree/shipment.rb', line 271

def shipped=(value)
  return unless value == '1' && shipped_at.nil?
  self.shipped_at = Time.current
end

#shipping_methodObject



276
277
278
# File 'app/models/spree/shipment.rb', line 276

def shipping_method
  selected_shipping_rate.try(:shipping_method)
end

#tax_totalObject

Only one of either included_tax_total or additional_tax_total is set This method returns the total of the two. Saves having to check if tax is included or additional.



283
284
285
# File 'app/models/spree/shipment.rb', line 283

def tax_total
  included_tax_total + additional_tax_total
end

#to_packageObject



287
288
289
290
291
292
293
294
# File 'app/models/spree/shipment.rb', line 287

def to_package
  package = Stock::Package.new(stock_location)
  package.shipment = self
  inventory_units.includes(:variant).joins(:variant).group_by(&:state).each do |state, state_inventory_units|
    package.add_multiple state_inventory_units, state.to_sym
  end
  package
end

#to_paramObject



296
297
298
# File 'app/models/spree/shipment.rb', line 296

def to_param
  number
end

#totalBigDecimal Also known as: final_price

Returns the amount of this shipment, taking into consideration all its adjustments.

Returns:

  • (BigDecimal)

    the amount of this shipment, taking into consideration all its adjustments.



130
131
132
# File 'app/models/spree/shipment.rb', line 130

def total
  cost + adjustment_total
end

#total_before_taxBigDecimal

Returns the amount of this item, taking into consideration all non-tax adjustments.

Returns:

  • (BigDecimal)

    the amount of this item, taking into consideration all non-tax adjustments.



138
139
140
# File 'app/models/spree/shipment.rb', line 138

def total_before_tax
  amount + adjustments.select { |a| !a.tax? && a.eligible? }.sum(&:amount)
end

#total_excluding_vatBigDecimal Also known as: pre_tax_amount

Note:

just like `cost`, this does not include any additional tax

Returns the amount of this shipment before VAT tax

Returns:

  • (BigDecimal)

    the amount of this shipment before VAT tax



144
145
146
# File 'app/models/spree/shipment.rb', line 144

def total_excluding_vat
  total_before_tax - included_tax_total
end

#total_with_itemsObject Also known as: final_price_with_items



150
151
152
# File 'app/models/spree/shipment.rb', line 150

def total_with_items
  total + item_cost
end

#tracking_urlObject



300
301
302
303
304
# File 'app/models/spree/shipment.rb', line 300

def tracking_url
  return nil unless tracking && shipping_method

  @tracking_url ||= shipping_method.build_tracking_url(tracking)
end

#transfer_to_location(variant, quantity, stock_location) ⇒ Object



360
361
362
363
364
# File 'app/models/spree/shipment.rb', line 360

def transfer_to_location(variant, quantity, stock_location)
  Spree::Deprecation.warn("Please use the Spree::FulfilmentChanger class instead of Spree::Shipment#transfer_to_location", caller)
  new_shipment = order.shipments.create!(stock_location: stock_location)
  transfer_to_shipment(variant, quantity, new_shipment)
end

#transfer_to_shipment(variant, quantity, shipment_to_transfer_to) ⇒ Object



366
367
368
369
370
371
372
373
374
# File 'app/models/spree/shipment.rb', line 366

def transfer_to_shipment(variant, quantity, shipment_to_transfer_to)
  Spree::Deprecation.warn("Please use the Spree::FulfilmentChanger class instead of Spree::Shipment#transfer_to_location", caller)
  Spree::FulfilmentChanger.new(
    current_shipment: self,
    desired_shipment: shipment_to_transfer_to,
    variant: variant,
    quantity: quantity
  ).run!
end

#update!(order_or_attrs) ⇒ Object



348
349
350
351
352
353
354
355
356
357
358
# File 'app/models/spree/shipment.rb', line 348

def update!(order_or_attrs)
  if order_or_attrs.is_a?(Spree::Order)
    Spree::Deprecation.warn "Calling Shipment#update! with an order to update the shipments state is deprecated. Please use Shipment#update_state instead."
    if order_or_attrs.object_id != order.object_id
      Spree::Deprecation.warn "Additionally, update! is being passed an instance of order which isn't the same object as the shipment's order association"
    end
    update_state
  else
    super
  end
end

#update_amountsObject



306
307
308
309
310
311
312
313
314
315
316
# File 'app/models/spree/shipment.rb', line 306

def update_amounts
  if selected_shipping_rate
    self.cost = selected_shipping_rate.cost
    if changed?
      update_columns(
        cost: cost,
        updated_at: Time.current
      )
    end
  end
end

#update_attributes_and_order(params = {}) ⇒ Object

Update Shipment and make sure Order states follow the shipment changes



319
320
321
322
323
324
325
326
327
328
329
330
# File 'app/models/spree/shipment.rb', line 319

def update_attributes_and_order(params = {})
  if update_attributes params
    if params.key? :selected_shipping_rate_id
      # Changing the selected Shipping Rate won't update the cost (for now)
      # so we persist the Shipment#cost before running `order.recalculate`
      update_amounts
      order.recalculate
    end

    true
  end
end

#update_stateObject

Updates the state of the Shipment bypassing any callbacks.

If this moves the shipmnent to the 'shipped' state, after_ship will be called.



336
337
338
339
340
341
342
343
344
345
346
# File 'app/models/spree/shipment.rb', line 336

def update_state
  old_state = state
  new_state = determine_state(order)
  if new_state != old_state
    update_columns(
      state: new_state,
      updated_at: Time.current
    )
    after_ship if new_state == 'shipped'
  end
end