Class: Spree::Shipment
Overview
An order’s planned shipments including tracking and cost.
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
#generate_permalink, #save_permalink
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.
24
25
26
|
# File 'app/models/spree/shipment.rb', line 24
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.
24
25
26
|
# File 'app/models/spree/shipment.rb', line 24
def suppress_mailer
@suppress_mailer
end
|
Instance Method Details
#after_cancel ⇒ Object
70
71
72
|
# File 'app/models/spree/shipment.rb', line 70
def after_cancel
manifest.each { |item| manifest_restock(item) }
end
|
#after_resume ⇒ Object
74
75
76
|
# File 'app/models/spree/shipment.rb', line 74
def after_resume
manifest.each { |item| manifest_unstock(item) }
end
|
#backordered? ⇒ Boolean
78
79
80
|
# File 'app/models/spree/shipment.rb', line 78
def backordered?
inventory_units.any?(&:backordered?)
end
|
#can_transition_from_canceled_to_ready? ⇒ Boolean
59
60
61
|
# File 'app/models/spree/shipment.rb', line 59
def can_transition_from_canceled_to_ready?
can_transition_from_pending_to_ready?
end
|
#can_transition_from_pending_to_ready? ⇒ Boolean
53
54
55
56
57
|
# File 'app/models/spree/shipment.rb', line 53
def can_transition_from_pending_to_ready?
order.can_ship? &&
inventory_units.all? { |iu| iu.shipped? || iu.allow_ship? || iu.canceled? } &&
(order.paid? || !Spree::Config[:require_payment_to_ship])
end
|
#can_transition_from_pending_to_shipped? ⇒ Boolean
49
50
51
|
# File 'app/models/spree/shipment.rb', line 49
def can_transition_from_pending_to_shipped?
!requires_shipment?
end
|
#currency ⇒ Object
82
83
84
|
# File 'app/models/spree/shipment.rb', line 82
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
207
208
209
210
211
212
213
214
215
216
|
# File 'app/models/spree/shipment.rb', line 207
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
|
#editable_by?(_user) ⇒ Boolean
108
109
110
|
# File 'app/models/spree/shipment.rb', line 108
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.
116
117
118
|
# File 'app/models/spree/shipment.rb', line 116
def finalize!
finalize_pending_inventory_units
end
|
#include?(variant) ⇒ Boolean
120
121
122
|
# File 'app/models/spree/shipment.rb', line 120
def include?(variant)
inventory_units_for(variant).present?
end
|
#inventory_units_for(variant) ⇒ Object
124
125
126
|
# File 'app/models/spree/shipment.rb', line 124
def inventory_units_for(variant)
inventory_units.where(variant_id: variant.id)
end
|
#inventory_units_for_item(line_item, variant = nil) ⇒ Object
128
129
130
|
# File 'app/models/spree/shipment.rb', line 128
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
132
133
134
|
# File 'app/models/spree/shipment.rb', line 132
def item_cost
line_items.sum(&:total)
end
|
#ready_or_pending? ⇒ Boolean
136
137
138
|
# File 'app/models/spree/shipment.rb', line 136
def ready_or_pending?
ready? || pending?
end
|
#refresh_rates ⇒ Object
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
# File 'app/models/spree/shipment.rb', line 140
def refresh_rates
return shipping_rates if shipped?
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
303
304
305
|
# File 'app/models/spree/shipment.rb', line 303
def requires_shipment?
!stock_location || stock_location.fulfillable?
end
|
#select_shipping_method(shipping_method) ⇒ Object
165
166
167
168
169
170
171
|
# File 'app/models/spree/shipment.rb', line 165
def select_shipping_method(shipping_method)
estimator = Spree::Config.stock.estimator_class.new
rates = estimator.shipping_rates(to_package, false)
rate = rates.detect { |detected| detected.shipping_method_id == shipping_method.id }
rate.selected = true
self.shipping_rates = [rate]
end
|
#selected_shipping_rate ⇒ Object
173
174
175
|
# File 'app/models/spree/shipment.rb', line 173
def selected_shipping_rate
shipping_rates.detect(&:selected?)
end
|
#selected_shipping_rate_id ⇒ Object
181
182
183
|
# File 'app/models/spree/shipment.rb', line 181
def selected_shipping_rate_id
selected_shipping_rate.try(:id)
end
|
#selected_shipping_rate_id=(id) ⇒ Object
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
# File 'app/models/spree/shipment.rb', line 185
def selected_shipping_rate_id=(id)
return if selected_shipping_rate_id == id
new_rate = shipping_rates.detect { |rate| rate.id == id.to_i }
unless new_rate
fail(
ArgumentError,
"Could not find shipping rate id #{id} for shipment #{number}"
)
end
transaction do
selected_shipping_rate.update!(selected: false) if selected_shipping_rate
new_rate.update!(selected: true)
end
end
|
#set_up_inventory(state, variant, _order, line_item) ⇒ Object
218
219
220
221
222
223
224
|
# File 'app/models/spree/shipment.rb', line 218
def set_up_inventory(state, variant, _order, line_item)
inventory_units.create(
state:,
variant_id: variant.id,
line_item_id: line_item.id
)
end
|
#shipped=(value) ⇒ Object
226
227
228
229
|
# File 'app/models/spree/shipment.rb', line 226
def shipped=(value)
return unless value == '1' && shipped_at.nil?
self.shipped_at = Time.current
end
|
#shipping_method ⇒ Object
231
232
233
|
# File 'app/models/spree/shipment.rb', line 231
def shipping_method
selected_shipping_rate.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.
238
239
240
|
# File 'app/models/spree/shipment.rb', line 238
def tax_total
included_tax_total + additional_tax_total
end
|
#to_package ⇒ Object
242
243
244
245
246
247
248
249
|
# File 'app/models/spree/shipment.rb', line 242
def to_package
package = Stock::Package.new(stock_location)
package.shipment = self
inventory_units.includes(variant: :product).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
251
252
253
|
# File 'app/models/spree/shipment.rb', line 251
def to_param
number
end
|
#total ⇒ BigDecimal
Returns the amount of this shipment, taking into consideration all its adjustments.
88
89
90
|
# File 'app/models/spree/shipment.rb', line 88
def total
cost + adjustment_total
end
|
#total_before_tax ⇒ BigDecimal
Returns the amount of this item, taking into consideration all non-tax adjustments.
94
95
96
|
# File 'app/models/spree/shipment.rb', line 94
def total_before_tax
amount + adjustments.reject(&:tax?).sum(&:amount)
end
|
#total_excluding_vat ⇒ BigDecimal
Note:
just like ‘cost`, this does not include any additional tax
Returns the amount of this shipment before VAT tax.
100
101
102
|
# File 'app/models/spree/shipment.rb', line 100
def total_excluding_vat
total_before_tax - included_tax_total
end
|
#total_with_items ⇒ Object
104
105
106
|
# File 'app/models/spree/shipment.rb', line 104
def total_with_items
total + item_cost
end
|
#tracking_url ⇒ Object
255
256
257
258
259
|
# File 'app/models/spree/shipment.rb', line 255
def tracking_url
return nil unless tracking && shipping_method
@tracking_url ||= shipping_method.build_tracking_url(tracking)
end
|
#update_amounts ⇒ Object
261
262
263
264
265
266
267
268
269
270
271
|
# File 'app/models/spree/shipment.rb', line 261
def update_amounts
if selected_shipping_rate
self.cost = selected_shipping_rate.cost
if changed?
update_columns(
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
274
275
276
277
278
279
280
281
282
283
284
285
|
# File 'app/models/spree/shipment.rb', line 274
def update_attributes_and_order(params = {})
if update(params)
if params.key? :selected_shipping_rate_id
update_amounts
order.recalculate
end
true
end
end
|
#update_state ⇒ Object
Updates the state of the Shipment bypassing any callbacks.
If this moves the shipment to the ‘shipped’ state, after_ship will be called.
291
292
293
294
295
296
297
298
299
300
301
|
# File 'app/models/spree/shipment.rb', line 291
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
|