Class: Spree::Shipment

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DisplayMoney

money_methods

Methods inherited from Base

page

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.



24
25
26
# File 'app/models/spree/shipment.rb', line 24

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.



24
25
26
# File 'app/models/spree/shipment.rb', line 24

def suppress_mailer
  @suppress_mailer
end

Instance Method Details

#add_shipping_method(shipping_method, selected = false) ⇒ Object



94
95
96
# File 'app/models/spree/shipment.rb', line 94

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

#after_cancelObject



98
99
100
# File 'app/models/spree/shipment.rb', line 98

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

#after_resumeObject



102
103
104
# File 'app/models/spree/shipment.rb', line 102

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

#backordered?Boolean

Returns:

  • (Boolean)


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

def backordered?
  inventory_units.any? { |inventory_unit| inventory_unit.backordered? }
end

#can_transition_from_canceled_to_ready?Boolean

Returns:

  • (Boolean)


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

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
# File 'app/models/spree/shipment.rb', line 81

def can_transition_from_pending_to_ready?
  order.can_ship? && !inventory_units.any?(&:backordered?) &&
    (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



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

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



214
215
216
217
218
219
220
221
222
223
224
# File 'app/models/spree/shipment.rb', line 214

def determine_state(order)
  return 'canceled' if order.canceled?
  return 'pending' unless order.can_ship?
  return 'pending' if inventory_units.any? &:backordered?
  return 'shipped' if state == 'shipped'
  if order.paid? || !Spree::Config[:require_payment_to_ship]
    'ready'
  else
    'pending'
  end
end

#discounted_costObject Also known as: discounted_amount



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

def discounted_cost
  cost + promo_total
end

#editable_by?(user) ⇒ Boolean

Returns:

  • (Boolean)


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

def editable_by?(user)
  !shipped?
end

#final_priceObject



124
125
126
# File 'app/models/spree/shipment.rb', line 124

def final_price
  cost + adjustment_total
end

#final_price_with_itemsObject



128
129
130
# File 'app/models/spree/shipment.rb', line 128

def final_price_with_items
  item_cost + final_price
end

#finalize!Object



132
133
134
135
# File 'app/models/spree/shipment.rb', line 132

def finalize!
  InventoryUnit.finalize_units!(inventory_units)
  manifest.each { |item| manifest_unstock(item) }
end

#include?(variant) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#inventory_units_for(variant) ⇒ Object



141
142
143
# File 'app/models/spree/shipment.rb', line 141

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

#inventory_units_for_item(line_item, variant = nil) ⇒ Object



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

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



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

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

#line_itemsObject



153
154
155
# File 'app/models/spree/shipment.rb', line 153

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

#manifestObject



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

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

#ready_or_pending?Boolean

Returns:

  • (Boolean)


161
162
163
# File 'app/models/spree/shipment.rb', line 161

def ready_or_pending?
  self.ready? || self.pending?
end

#refresh_ratesObject



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'app/models/spree/shipment.rb', line 165

def refresh_rates
  return shipping_rates if shipped? || order.completed?
  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 = Stock::Estimator.new(order).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
  self.save!

  shipping_rates
end

#requires_shipment?Boolean

Returns:

  • (Boolean)


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

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

#selected_shipping_rateObject



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

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

#selected_shipping_rate_idObject



198
199
200
# File 'app/models/spree/shipment.rb', line 198

def selected_shipping_rate_id
  selected_shipping_rate.try(:id)
end

#selected_shipping_rate_id=(id) ⇒ Object



202
203
204
205
206
# File 'app/models/spree/shipment.rb', line 202

def selected_shipping_rate_id=(id)
  shipping_rates.update_all(selected: false)
  shipping_rates.update(id, selected: true)
  self.save!
end

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



226
227
228
229
230
231
232
233
# File 'app/models/spree/shipment.rb', line 226

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

#shipped=(value) ⇒ Object



235
236
237
238
# File 'app/models/spree/shipment.rb', line 235

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

#shipping_methodObject



240
241
242
# File 'app/models/spree/shipment.rb', line 240

def shipping_method
  selected_shipping_rate.try(:shipping_method) || shipping_rates.first.try(:shipping_method)
end

#tax_categoryObject



244
245
246
# File 'app/models/spree/shipment.rb', line 244

def tax_category
  selected_shipping_rate.try(:tax_rate).try(:tax_category)
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.



251
252
253
# File 'app/models/spree/shipment.rb', line 251

def tax_total
  included_tax_total + additional_tax_total
end

#to_packageObject



255
256
257
258
259
260
261
# File 'app/models/spree/shipment.rb', line 255

def to_package
  package = Stock::Package.new(stock_location)
  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



263
264
265
# File 'app/models/spree/shipment.rb', line 263

def to_param
  number
end

#tracking_urlObject



267
268
269
# File 'app/models/spree/shipment.rb', line 267

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

#transfer_to_location(variant, quantity, stock_location) ⇒ Object



324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'app/models/spree/shipment.rb', line 324

def transfer_to_location(variant, quantity, stock_location)
  if quantity <= 0
    raise ArgumentError
  end

  transaction do
    new_shipment = order.shipments.create!(stock_location: stock_location)

    order.contents.remove(variant, quantity, {shipment: self})
    order.contents.add(variant, quantity, {shipment: new_shipment})

    refresh_rates
    save!
    new_shipment.save!
  end
end

#transfer_to_shipment(variant, quantity, shipment_to_transfer_to) ⇒ Object



341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'app/models/spree/shipment.rb', line 341

def transfer_to_shipment(variant, quantity, shipment_to_transfer_to)
  quantity_already_shipment_to_transfer_to = shipment_to_transfer_to.manifest.find{|mi| mi.line_item.variant == variant}.try(:quantity) || 0
  final_quantity = quantity + quantity_already_shipment_to_transfer_to

  if (quantity <= 0 || self == shipment_to_transfer_to)
    raise ArgumentError
  end

  transaction do
    order.contents.remove(variant, quantity, {shipment: self})
    order.contents.add(variant, quantity, {shipment: shipment_to_transfer_to})

    refresh_rates
    save!
    shipment_to_transfer_to.refresh_rates
    shipment_to_transfer_to.save!
  end
end

#update!(order) ⇒ Object

Updates various aspects of the Shipment while bypassing any callbacks. Note that this method takes an explicit reference to the Order object. This is necessary because the association actually has a stale (and unsaved) copy of the Order and so it will not yield the correct results.



314
315
316
317
318
319
320
321
322
# File 'app/models/spree/shipment.rb', line 314

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

#update_amountsObject



271
272
273
274
275
276
277
278
279
# File 'app/models/spree/shipment.rb', line 271

def update_amounts
  if selected_shipping_rate
    self.update_columns(
      cost: selected_shipping_rate.cost,
      adjustment_total: adjustments.additional.map(&:update!).compact.sum,
      updated_at: Time.now,
    )
  end
end

#update_attributes_and_order(params = {}) ⇒ Object

Update Shipment and make sure Order states follow the shipment changes



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'app/models/spree/shipment.rb', line 282

def update_attributes_and_order(params = {})
  if self.update_attributes params
    if params.has_key? :selected_shipping_rate_id
      # Changing the selected Shipping Rate won't update the cost (for now)
      # so we persist the Shipment#cost before calculating order shipment
      # total and updating payment state (given a change in shipment cost
      # might change the Order#payment_state)
      self.update_amounts

      order.updater.update_shipment_total
      order.updater.update_payment_state

      # Update shipment state only after order total is updated because it
      # (via Order#paid?) affects the shipment state (YAY)
      self.update_columns(
        state: determine_state(order),
        updated_at: Time.now
      )

      # And then it's time to update shipment states and finally persist
      # order changes
      order.updater.update_shipment_state
      order.updater.persist_totals
    end

    true
  end
end