Class: Spree::Shipment
- Inherits:
-
Object
show all
- Extended by:
- DisplayMoney
- Includes:
- Metadata, Metafields, NumberAsParam, NumberIdentifier, Spree::Security::Shipments, CustomEvents, Emails, Webhooks, VendorConcern
- Defined in:
- app/models/spree/shipment.rb,
app/models/spree/shipment/emails.rb,
app/models/spree/shipment/webhooks.rb,
app/models/spree/shipment/custom_events.rb
Defined Under Namespace
Modules: CustomEvents, Emails, Webhooks
Classes: ManifestItem
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#add_shipping_method(shipping_method, selected = false) ⇒ Object
-
#after_cancel ⇒ Object
-
#after_resume ⇒ Object
-
#amount ⇒ Object
-
#backordered? ⇒ Boolean
Returns true if the shipment has any backordered inventory units.
-
#determine_state(order) ⇒ Object
Determines the appropriate state according to the following logic:.
-
#digital? ⇒ Boolean
-
#discounted_cost ⇒ Object
(also: #discounted_amount)
-
#final_price ⇒ Object
-
#final_price_with_items ⇒ Object
-
#finalize! ⇒ Object
-
#free? ⇒ Boolean
-
#include?(variant) ⇒ Boolean
-
#inventory_units_for(variant) ⇒ Object
-
#inventory_units_for_item(line_item, variant = nil) ⇒ Object
-
#item_cost ⇒ BigDecimal
Returns the cost of the shipment.
-
#item_quantity ⇒ Object
Returns the total quantity of all line items in the shipment.
-
#item_weight ⇒ BigDecimal
Returns the weight of the shipment.
-
#line_items ⇒ Object
-
#manifest ⇒ Object
-
#name ⇒ String
Returns the shipment number and shipping method name.
-
#partial? ⇒ Boolean
Returns true if not all of the shipment’s line items are fully shipped.
-
#process_order_payments ⇒ Object
-
#ready_or_pending? ⇒ Boolean
-
#refresh_rates(shipping_method_filter = ShippingMethod::DISPLAY_ON_FRONT_END) ⇒ Object
-
#selected_shipping_rate_id ⇒ Object
-
#selected_shipping_rate_id=(id) ⇒ Object
-
#set_up_inventory(state, variant, order, line_item, quantity = 1) ⇒ Object
-
#shippable? ⇒ Boolean
Returns true if the shipment is shippable.
-
#shipped=(value) ⇒ Object
-
#shipping_method ⇒ Spree::ShippingMethod
Returns the shipping method of the selected shipping rate.
-
#tax_category ⇒ Spree::TaxCategory
Returns the tax category of the selected shipping rate.
-
#tax_category_id ⇒ Integer
Returns the tax category ID of the selected shipping rate.
-
#tax_total ⇒ Object
Only one of either included_tax_total or additional_tax_total is set This method returns the total of the two.
-
#to_package ⇒ Object
-
#tracked? ⇒ Boolean
Returns true if the shipment is tracked.
-
#tracking_url ⇒ Object
-
#transfer_to_location(variant, quantity, stock_location) ⇒ Object
-
#transfer_to_shipment(variant, quantity, shipment_to_transfer_to) ⇒ Object
-
#update!(order) ⇒ Object
Updates various aspects of the Shipment while bypassing any callbacks.
-
#update_amounts ⇒ Object
-
#update_attributes_and_order(params = {}) ⇒ Object
-
#weight_unit ⇒ String
Returns the weight unit of the shipment.
-
#with_free_shipping_promotion? ⇒ Boolean
Returns true if the shipment has a free shipping promotion applied.
money_methods
#publish_shipment_canceled_event, #publish_shipment_resumed_event, #publish_shipment_shipped_event
Methods included from Webhooks
#send_shipment_shipped_webhook
Methods included from Emails
#send_shipped_email
Instance Attribute Details
#special_instructions ⇒ Object
Returns the value of attribute special_instructions.
44
45
46
|
# File 'app/models/spree/shipment.rb', line 44
def special_instructions
@special_instructions
end
|
Instance Method Details
#add_shipping_method(shipping_method, selected = false) ⇒ Object
135
136
137
|
# File 'app/models/spree/shipment.rb', line 135
def add_shipping_method(shipping_method, selected = false)
shipping_rates.create(shipping_method: shipping_method, selected: selected, cost: cost)
end
|
#after_cancel ⇒ Object
139
140
141
|
# File 'app/models/spree/shipment.rb', line 139
def after_cancel
manifest.each { |item| manifest_restock(item) }
end
|
#after_resume ⇒ Object
143
144
145
|
# File 'app/models/spree/shipment.rb', line 143
def after_resume
manifest.each { |item| manifest_unstock(item) }
end
|
#amount ⇒ Object
127
128
129
|
# File 'app/models/spree/shipment.rb', line 127
def amount
cost
end
|
#backordered? ⇒ Boolean
Returns true if the shipment has any backordered inventory units
150
151
152
|
# File 'app/models/spree/shipment.rb', line 150
def backordered?
inventory_units.any?(&:backordered?)
end
|
#determine_state(order) ⇒ Object
Determines the appropriate state according to the following logic:
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
183
184
185
186
187
188
189
190
|
# File 'app/models/spree/shipment.rb', line 183
def determine_state(order)
return 'canceled' if canceled? || order.canceled?
return 'pending' unless order.can_ship?
return 'pending' if inventory_units.any?(&:backordered?)
return 'shipped' if shipped?
order.paid? || Spree::Config[:auto_capture_on_dispatch] ? 'ready' : 'pending'
end
|
131
132
133
|
# File 'app/models/spree/shipment.rb', line 131
def digital?
shipping_method&.digital? || false
end
|
#discounted_cost ⇒ Object
Also known as:
discounted_amount
192
193
194
|
# File 'app/models/spree/shipment.rb', line 192
def discounted_cost
cost + promo_total
end
|
#final_price ⇒ Object
197
198
199
|
# File 'app/models/spree/shipment.rb', line 197
def final_price
cost + adjustment_total
end
|
#final_price_with_items ⇒ Object
201
202
203
|
# File 'app/models/spree/shipment.rb', line 201
def final_price_with_items
item_cost + final_price
end
|
#finalize! ⇒ Object
219
220
221
222
|
# File 'app/models/spree/shipment.rb', line 219
def finalize!
inventory_units.finalize_units!
after_resume
end
|
205
206
207
208
209
|
# File 'app/models/spree/shipment.rb', line 205
def free?
return true if final_price == BigDecimal(0)
with_free_shipping_promotion?
end
|
#include?(variant) ⇒ Boolean
224
225
226
|
# File 'app/models/spree/shipment.rb', line 224
def include?(variant)
inventory_units_for(variant).present?
end
|
#inventory_units_for(variant) ⇒ Object
228
229
230
|
# File 'app/models/spree/shipment.rb', line 228
def inventory_units_for(variant)
inventory_units.where(variant_id: variant.id)
end
|
#inventory_units_for_item(line_item, variant = nil) ⇒ Object
232
233
234
|
# File 'app/models/spree/shipment.rb', line 232
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 ⇒ BigDecimal
Returns the cost of the shipment
244
245
246
|
# File 'app/models/spree/shipment.rb', line 244
def item_cost
manifest.map { |m| (m.line_item.price + (m.line_item.adjustment_total / m.line_item.quantity)) * m.quantity }.sum
end
|
#item_quantity ⇒ Object
Returns the total quantity of all line items in the shipment
237
238
239
|
# File 'app/models/spree/shipment.rb', line 237
def item_quantity
manifest.sum(&:quantity)
end
|
#item_weight ⇒ BigDecimal
Returns the weight of the shipment
251
252
253
|
# File 'app/models/spree/shipment.rb', line 251
def item_weight
manifest.map { |m| m.line_item.item_weight }.sum
end
|
#line_items ⇒ Object
262
263
264
|
# File 'app/models/spree/shipment.rb', line 262
def line_items
inventory_units.includes(:line_item).map(&:line_item).uniq
end
|
#manifest ⇒ Object
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
# File 'app/models/spree/shipment.rb', line 268
def manifest
inventory_units.group_by(&:variant_id).map do |_variant_id, units|
units.group_by(&:line_item_id).map do |_line_item_id, units|
states = {}
units.group_by(&:state).each { |state, iu| states[state] = iu.sum(&:quantity) }
line_item = units.first.line_item
variant = units.first.variant
ManifestItem.new(line_item, variant, units.sum(&:quantity), states)
end
end.flatten
end
|
#name ⇒ String
Returns the shipment number and shipping method name
123
124
125
|
# File 'app/models/spree/shipment.rb', line 123
def name
[number, shipping_method&.name].compact.join(' ').strip
end
|
Returns true if not all of the shipment’s line items are fully shipped
171
172
173
174
175
176
|
# File 'app/models/spree/shipment.rb', line 171
def partial?
manifest.any? do |manifest_item|
line_item = manifest_item.line_item
line_item.quantity > manifest_item.quantity
end
end
|
#process_order_payments ⇒ Object
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
|
# File 'app/models/spree/shipment.rb', line 283
def process_order_payments
pending_payments = order.pending_payments.
sort_by(&:uncaptured_amount).reverse
shipment_to_pay = final_price_with_items
payments_amount = 0
payments_pool = pending_payments.each_with_object([]) do |payment, pool|
break if payments_amount >= shipment_to_pay
payments_amount += payment.uncaptured_amount
pool << payment
end
payments_pool.each do |payment|
capturable_amount = if payment.amount >= shipment_to_pay
shipment_to_pay
else
payment.amount
end
cents = (capturable_amount * 100).to_i
payment.capture!(cents)
shipment_to_pay -= capturable_amount
end
end
|
#ready_or_pending? ⇒ Boolean
310
311
312
|
# File 'app/models/spree/shipment.rb', line 310
def ready_or_pending?
ready? || pending?
end
|
#refresh_rates(shipping_method_filter = ShippingMethod::DISPLAY_ON_FRONT_END) ⇒ Object
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
# File 'app/models/spree/shipment.rb', line 314
def refresh_rates(shipping_method_filter = ShippingMethod::DISPLAY_ON_FRONT_END)
return shipping_rates if shipped?
return [] unless can_get_rates?
original_shipping_method_id = shipping_method.try(:id)
self.shipping_rates = Stock::Estimator.new(order).
shipping_rates(to_package, shipping_method_filter)
if shipping_method
selected_rate = shipping_rates.detect do |rate|
if original_shipping_method_id
rate.shipping_method_id == original_shipping_method_id
else
rate.selected
end
end
save!
self.selected_shipping_rate_id = selected_rate.id if selected_rate
reload
end
shipping_rates
end
|
#selected_shipping_rate_id ⇒ Object
340
341
342
|
# File 'app/models/spree/shipment.rb', line 340
def selected_shipping_rate_id
selected_shipping_rate.try(:id)
end
|
#selected_shipping_rate_id=(id) ⇒ Object
344
345
346
347
348
349
|
# File 'app/models/spree/shipment.rb', line 344
def selected_shipping_rate_id=(id)
shipping_rates.update_all(selected: false, updated_at: Time.current)
shipping_rates.update(id, selected: true)
save!
end
|
#set_up_inventory(state, variant, order, line_item, quantity = 1) ⇒ Object
351
352
353
354
355
356
357
358
359
360
361
|
# File 'app/models/spree/shipment.rb', line 351
def set_up_inventory(state, variant, order, line_item, quantity = 1)
return if quantity <= 0
inventory_units.create(
state: state,
variant_id: variant.id,
order_id: order.id,
line_item_id: line_item.id,
quantity: quantity
)
end
|
Returns true if the shipment is shippable
164
165
166
|
# File 'app/models/spree/shipment.rb', line 164
def shippable?
can_ship? && (tracked? || digital?)
end
|
#shipped=(value) ⇒ Object
363
364
365
366
367
|
# File 'app/models/spree/shipment.rb', line 363
def shipped=(value)
return unless value == '1' && shipped_at.nil?
self.shipped_at = Time.current
end
|
Returns the shipping method of the selected shipping rate
372
373
374
|
# File 'app/models/spree/shipment.rb', line 372
def shipping_method
selected_shipping_rate&.shipping_method || shipping_rates.first&.shipping_method
end
|
Returns the tax category of the selected shipping rate
379
380
381
|
# File 'app/models/spree/shipment.rb', line 379
def tax_category
selected_shipping_rate.try(:tax_rate).try(:tax_category)
end
|
#tax_category_id ⇒ Integer
Returns the tax category ID of the selected shipping rate
386
387
388
|
# File 'app/models/spree/shipment.rb', line 386
def tax_category_id
selected_shipping_rate.try(:tax_rate).try(:tax_category_id)
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.
393
394
395
|
# File 'app/models/spree/shipment.rb', line 393
def tax_total
included_tax_total + additional_tax_total
end
|
#to_package ⇒ Object
397
398
399
400
401
402
403
|
# File 'app/models/spree/shipment.rb', line 397
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
|
Returns true if the shipment is tracked
157
158
159
|
# File 'app/models/spree/shipment.rb', line 157
def tracked?
tracking.present? || tracking_url.present?
end
|
#tracking_url ⇒ Object
405
406
407
|
# File 'app/models/spree/shipment.rb', line 405
def tracking_url
@tracking_url ||= shipping_method&.build_tracking_url(tracking)
end
|
#transfer_to_location(variant, quantity, stock_location) ⇒ Object
436
437
438
439
440
441
442
|
# File 'app/models/spree/shipment.rb', line 436
def transfer_to_location(variant, quantity, stock_location)
transfer_to_shipment(
variant,
quantity,
order.shipments.build(stock_location: stock_location)
)
end
|
#transfer_to_shipment(variant, quantity, shipment_to_transfer_to) ⇒ Object
444
445
446
447
448
449
450
451
452
453
|
# File 'app/models/spree/shipment.rb', line 444
def transfer_to_shipment(variant, quantity, shipment_to_transfer_to)
Spree::FulfilmentChanger.new(
current_stock_location: stock_location,
desired_stock_location: shipment_to_transfer_to.stock_location,
current_shipment: self,
desired_shipment: shipment_to_transfer_to,
variant: variant,
quantity: quantity
)
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.
426
427
428
429
430
431
432
433
434
|
# File 'app/models/spree/shipment.rb', line 426
def update!(order)
old_state = state
new_state = determine_state(order)
update_columns(
state: new_state,
updated_at: Time.current
)
after_ship if new_state == 'shipped' && old_state != 'shipped'
end
|
#update_amounts ⇒ Object
409
410
411
412
413
414
415
416
417
|
# File 'app/models/spree/shipment.rb', line 409
def update_amounts
if selected_shipping_rate
update_columns(
cost: selected_shipping_rate.cost,
adjustment_total: adjustments.additional.map(&:update!).compact.sum,
updated_at: Time.current
)
end
end
|
#update_attributes_and_order(params = {}) ⇒ Object
419
420
421
|
# File 'app/models/spree/shipment.rb', line 419
def update_attributes_and_order(params = {})
Shipments::Update.call(shipment: self, shipment_attributes: params).success?
end
|
#weight_unit ⇒ String
Returns the weight unit of the shipment
258
259
260
|
# File 'app/models/spree/shipment.rb', line 258
def weight_unit
manifest.first.line_item.weight_unit
end
|
Returns true if the shipment has a free shipping promotion applied