Class: Spree::Shipment
Instance Attribute Summary collapse
-
#special_instructions ⇒ Object
TODO: remove the suppress_mailer temporary variable once we are calling ‘ship’ from outside of the state machine and can actually pass variables through.
-
#suppress_mailer ⇒ Object
TODO: remove the suppress_mailer temporary variable once we are calling ‘ship’ from outside of the state machine and can actually pass variables through.
Instance Method Summary
collapse
money_methods
Methods inherited from Base
display_includes, #initialize_preference_defaults, page, preference
#default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference
Instance Attribute Details
#special_instructions ⇒ Object
TODO: remove the suppress_mailer temporary variable once we are calling ‘ship’ from outside of the state machine and can actually pass variables through.
19
20
21
|
# File 'app/models/spree/shipment.rb', line 19
def special_instructions
@special_instructions
end
|
#suppress_mailer ⇒ Object
TODO: remove the suppress_mailer temporary variable once we are calling ‘ship’ from outside of the state machine and can actually pass variables through.
19
20
21
|
# File 'app/models/spree/shipment.rb', line 19
def suppress_mailer
@suppress_mailer
end
|
Instance Method Details
#add_shipping_method(shipping_method, selected = false) ⇒ Object
93
94
95
|
# File 'app/models/spree/shipment.rb', line 93
def add_shipping_method(shipping_method, selected = false)
shipping_rates.create(shipping_method: shipping_method, selected: selected, cost: cost)
end
|
#address ⇒ Object
376
377
378
379
|
# File 'app/models/spree/shipment.rb', line 376
def address
Spree::Deprecation.warn("Calling Shipment#address is deprecated. Use Order#ship_address instead", caller)
order.ship_address if order
end
|
#after_cancel ⇒ Object
97
98
99
|
# File 'app/models/spree/shipment.rb', line 97
def after_cancel
manifest.each { |item| manifest_restock(item) }
end
|
#after_resume ⇒ Object
101
102
103
|
# File 'app/models/spree/shipment.rb', line 101
def after_resume
manifest.each { |item| manifest_unstock(item) }
end
|
#backordered? ⇒ Boolean
105
106
107
|
# File 'app/models/spree/shipment.rb', line 105
def backordered?
inventory_units.any?(&:backordered?)
end
|
#can_transition_from_canceled_to_ready? ⇒ Boolean
85
86
87
|
# File 'app/models/spree/shipment.rb', line 85
def can_transition_from_canceled_to_ready?
can_transition_from_pending_to_ready?
end
|
#can_transition_from_pending_to_ready? ⇒ Boolean
79
80
81
82
83
|
# File 'app/models/spree/shipment.rb', line 79
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
75
76
77
|
# File 'app/models/spree/shipment.rb', line 75
def can_transition_from_pending_to_shipped?
!requires_shipment?
end
|
#currency ⇒ Object
109
110
111
|
# File 'app/models/spree/shipment.rb', line 109
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
225
226
227
228
229
230
231
232
233
234
|
# File 'app/models/spree/shipment.rb', line 225
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_cost ⇒ Object
Also known as:
discounted_amount
113
114
115
|
# File 'app/models/spree/shipment.rb', line 113
def discounted_cost
cost + promo_total
end
|
#editable_by?(_user) ⇒ Boolean
118
119
120
|
# File 'app/models/spree/shipment.rb', line 118
def editable_by?(_user)
!shipped?
end
|
#final_price ⇒ Object
122
123
124
|
# File 'app/models/spree/shipment.rb', line 122
def final_price
cost + adjustment_total
end
|
#final_price_with_items ⇒ Object
126
127
128
|
# File 'app/models/spree/shipment.rb', line 126
def final_price_with_items
item_cost + final_price
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.
134
135
136
137
138
139
140
141
|
# File 'app/models/spree/shipment.rb', line 134
def finalize!
transaction do
pending_units = inventory_units.select(&:pending?)
pending_manifest = ShippingManifest.new(inventory_units: pending_units)
pending_manifest.items.each { |item| manifest_unstock(item) }
InventoryUnit.finalize_units!(pending_units)
end
end
|
#include?(variant) ⇒ Boolean
143
144
145
|
# File 'app/models/spree/shipment.rb', line 143
def include?(variant)
inventory_units_for(variant).present?
end
|
#inventory_units_for(variant) ⇒ Object
147
148
149
|
# File 'app/models/spree/shipment.rb', line 147
def inventory_units_for(variant)
inventory_units.where(variant_id: variant.id)
end
|
#inventory_units_for_item(line_item, variant = nil) ⇒ Object
151
152
153
|
# File 'app/models/spree/shipment.rb', line 151
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_cost ⇒ Object
155
156
157
|
# File 'app/models/spree/shipment.rb', line 155
def item_cost
line_items.map(&:final_amount).sum
end
|
#line_items ⇒ Object
159
160
161
|
# File 'app/models/spree/shipment.rb', line 159
def line_items
inventory_units.includes(:line_item).map(&:line_item).uniq
end
|
#manifest ⇒ Object
200
201
202
|
# File 'app/models/spree/shipment.rb', line 200
def manifest
@manifest ||= Spree::ShippingManifest.new(inventory_units: inventory_units).items
end
|
#pre_tax_amount ⇒ Object
163
164
165
|
# File 'app/models/spree/shipment.rb', line 163
def pre_tax_amount
discounted_amount - included_tax_total
end
|
#ready_or_pending? ⇒ Boolean
167
168
169
|
# File 'app/models/spree/shipment.rb', line 167
def ready_or_pending?
ready? || pending?
end
|
#refresh_rates ⇒ Object
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
# File 'app/models/spree/shipment.rb', line 171
def refresh_rates
return shipping_rates if shipped? || order.completed?
return [] unless can_get_rates?
original_shipping_method_id = shipping_method.try!(:id)
new_rates = Spree::Config.stock.estimator_class.new.shipping_rates(to_package)
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
372
373
374
|
# File 'app/models/spree/shipment.rb', line 372
def requires_shipment?
!stock_location || stock_location.fulfillable?
end
|
#selected_shipping_rate ⇒ Object
196
197
198
|
# File 'app/models/spree/shipment.rb', line 196
def selected_shipping_rate
shipping_rates.detect(&:selected?)
end
|
#selected_shipping_rate_id ⇒ Object
204
205
206
|
# File 'app/models/spree/shipment.rb', line 204
def selected_shipping_rate_id
selected_shipping_rate.try(:id)
end
|
#selected_shipping_rate_id=(id) ⇒ Object
208
209
210
211
212
213
214
215
216
217
|
# File 'app/models/spree/shipment.rb', line 208
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
236
237
238
239
240
241
242
243
|
# File 'app/models/spree/shipment.rb', line 236
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
245
246
247
248
|
# File 'app/models/spree/shipment.rb', line 245
def shipped=(value)
return unless value == '1' && shipped_at.nil?
self.shipped_at = Time.current
end
|
#shipping_method ⇒ Object
250
251
252
|
# File 'app/models/spree/shipment.rb', line 250
def shipping_method
selected_shipping_rate.try(:shipping_method) || shipping_rates.first.try(:shipping_method)
end
|
#tax_total ⇒ Object
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.
257
258
259
|
# File 'app/models/spree/shipment.rb', line 257
def tax_total
included_tax_total + additional_tax_total
end
|
#to_package ⇒ Object
261
262
263
264
265
266
267
268
|
# File 'app/models/spree/shipment.rb', line 261
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_param ⇒ Object
270
271
272
|
# File 'app/models/spree/shipment.rb', line 270
def to_param
number
end
|
#tracking_url ⇒ Object
274
275
276
277
278
|
# File 'app/models/spree/shipment.rb', line 274
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
# File 'app/models/spree/shipment.rb', line 339
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
|
# File 'app/models/spree/shipment.rb', line 356
def transfer_to_shipment(variant, quantity, 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.
327
328
329
330
331
332
333
334
335
336
337
|
# File 'app/models/spree/shipment.rb', line 327
def update!(order)
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
|
#update_amounts ⇒ Object
280
281
282
283
284
285
286
287
288
289
290
291
292
|
# File 'app/models/spree/shipment.rb', line 280
def update_amounts
if selected_shipping_rate
self.cost = selected_shipping_rate.cost
self.adjustment_total = adjustments.additional.map(&:update!).compact.sum
if changed?
update_columns(
cost: cost,
adjustment_total: adjustment_total,
updated_at: Time.current
)
end
end
end
|
#update_attributes_and_order(params = {}) ⇒ Object
Update Shipment and make sure Order states follow the shipment changes
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
# File 'app/models/spree/shipment.rb', line 295
def update_attributes_and_order(params = {})
if update_attributes params
if params.key? :selected_shipping_rate_id
update_amounts
order.updater.update_shipment_total
order.updater.update_payment_state
update_columns(
state: determine_state(order),
updated_at: Time.current
)
order.updater.update_shipment_state
order.updater.persist_totals
end
true
end
end
|