Class: Spree::Order
Defined Under Namespace
Modules: Checkout, CurrencyUpdater, Payments
Constant Summary
collapse
- PAYMENT_STATES =
%w(balance_due checkout completed credit_owed failed paid pending processing void).freeze
- SHIPMENT_STATES =
%w(backorder canceled partial pending ready shipped).freeze
NumberGenerator::NUMBER_LENGTH, NumberGenerator::NUMBER_LETTERS, NumberGenerator::NUMBER_PREFIX
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
money_methods
#generate_guest_token
by_number
#price_from_line_item, #update_line_item_currencies!, #update_line_item_price!
Methods included from Checkout
included
Methods inherited from Base
page
#clear_preferences, #default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference
Instance Attribute Details
#coupon_code ⇒ Object
Returns the value of attribute coupon_code.
43
44
45
|
# File 'app/models/spree/order.rb', line 43
def coupon_code
@coupon_code
end
|
#temporary_address ⇒ Object
Returns the value of attribute temporary_address.
44
45
46
|
# File 'app/models/spree/order.rb', line 44
def temporary_address
@temporary_address
end
|
#temporary_credit_card ⇒ Object
Returns the value of attribute temporary_credit_card.
44
45
46
|
# File 'app/models/spree/order.rb', line 44
def temporary_credit_card
@temporary_credit_card
end
|
#use_billing ⇒ Object
Returns the value of attribute use_billing.
100
101
102
|
# File 'app/models/spree/order.rb', line 100
def use_billing
@use_billing
end
|
Class Method Details
.complete ⇒ Object
125
126
127
|
# File 'app/models/spree/order.rb', line 125
def self.complete
where.not(completed_at: nil)
end
|
.incomplete ⇒ Object
129
130
131
|
# File 'app/models/spree/order.rb', line 129
def self.incomplete
where(completed_at: nil)
end
|
.register_line_item_comparison_hook(hook) ⇒ Object
Use this method in other gems that wish to register their own custom logic that should be called when determining if two line items are equal.
141
142
143
|
# File 'app/models/spree/order.rb', line 141
def self.register_line_item_comparison_hook(hook)
self.line_item_comparison_hooks.add(hook)
end
|
.register_update_hook(hook) ⇒ Object
Use this method in other gems that wish to register their own custom logic that should be called after Order#update
135
136
137
|
# File 'app/models/spree/order.rb', line 135
def self.register_update_hook(hook)
self.update_hooks.add(hook)
end
|
Instance Method Details
#all_inventory_units_returned? ⇒ Boolean
231
232
233
|
# File 'app/models/spree/order.rb', line 231
def all_inventory_units_returned?
inventory_units.all? { |inventory_unit| inventory_unit.returned? }
end
|
#allow_cancel? ⇒ Boolean
226
227
228
229
|
# File 'app/models/spree/order.rb', line 226
def allow_cancel?
return false unless completed? and state != 'canceled'
shipment_state.nil? || %w{ready backorder pending}.include?(shipment_state)
end
|
#amount ⇒ Object
For compatiblity with Calculator::PriceSack
146
147
148
|
# File 'app/models/spree/order.rb', line 146
def amount
line_items.inject(0.0) { |sum, li| sum + li.amount }
end
|
#approve! ⇒ Object
542
543
544
|
# File 'app/models/spree/order.rb', line 542
def approve!
update_column(:considered_risky, false)
end
|
#approved? ⇒ Boolean
524
525
526
|
# File 'app/models/spree/order.rb', line 524
def approved?
!!self.approved_at
end
|
#approved_by(user) ⇒ Object
514
515
516
517
518
519
520
521
522
|
# File 'app/models/spree/order.rb', line 514
def approved_by(user)
self.transaction do
approve!
self.update_columns(
approver_id: user.id,
approved_at: Time.now,
)
end
end
|
#associate_user!(user, override_email = true) ⇒ Object
Associates the specified user with the order.
240
241
242
243
244
245
246
247
248
249
250
251
252
|
# File 'app/models/spree/order.rb', line 240
def associate_user!(user, override_email = true)
self.user = user
self.email = user.email if override_email
self.created_by ||= user
self.bill_address ||= user.bill_address
self.ship_address ||= user.ship_address
changes = slice(:user_id, :email, :created_by_id, :bill_address_id, :ship_address_id)
self.class.unscoped.where(id: self).update_all(changes)
end
|
#available_payment_methods ⇒ Object
#backordered? ⇒ Boolean
190
191
192
|
# File 'app/models/spree/order.rb', line 190
def backordered?
shipments.any?(&:backordered?)
end
|
#billing_firstname ⇒ Object
373
374
375
|
# File 'app/models/spree/order.rb', line 373
def billing_firstname
bill_address.try(:firstname)
end
|
#billing_lastname ⇒ Object
377
378
379
|
# File 'app/models/spree/order.rb', line 377
def billing_lastname
bill_address.try(:lastname)
end
|
#can_add_coupon? ⇒ Boolean
#can_approve? ⇒ Boolean
528
529
530
|
# File 'app/models/spree/order.rb', line 528
def can_approve?
!approved?
end
|
#can_ship? ⇒ Boolean
315
316
317
|
# File 'app/models/spree/order.rb', line 315
def can_ship?
self.complete? || self.resumed? || self.awaiting_return? || self.returned?
end
|
#canceled_by(user) ⇒ Object
504
505
506
507
508
509
510
511
512
|
# File 'app/models/spree/order.rb', line 504
def canceled_by(user)
self.transaction do
cancel!
self.update_columns(
canceler_id: user.id,
canceled_at: Time.now,
)
end
end
|
#checkout_allowed? ⇒ Boolean
Indicates whether or not the user is allowed to proceed to checkout. Currently this is implemented as a check for whether or not there is at least one LineItem in the Order. Feel free to override this logic in your own application if you require additional steps before allowing a checkout.
171
172
173
|
# File 'app/models/spree/order.rb', line 171
def checkout_allowed?
line_items.count > 0
end
|
#clone_billing_address ⇒ Object
217
218
219
220
221
222
223
224
|
# File 'app/models/spree/order.rb', line 217
def clone_billing_address
if bill_address and self.ship_address.nil?
self.ship_address = bill_address.clone
else
self.ship_address.attributes = bill_address.attributes.except('id', 'updated_at', 'created_at')
end
true
end
|
#completed? ⇒ Boolean
163
164
165
|
# File 'app/models/spree/order.rb', line 163
def completed?
completed_at.present?
end
|
#confirmation_required? ⇒ Boolean
If true, causes the confirmation step to happen during the checkout process
181
182
183
184
185
186
187
188
|
# File 'app/models/spree/order.rb', line 181
def confirmation_required?
Spree::Config[:always_include_confirm_step] ||
payments.valid.map(&:payment_method).compact.any?(&:payment_profiles_supported?) ||
state == 'confirm'
end
|
#consider_risk ⇒ Object
532
533
534
535
536
|
# File 'app/models/spree/order.rb', line 532
def consider_risk
if is_risky? && !approved?
considered_risky!
end
end
|
#considered_risky! ⇒ Object
538
539
540
|
# File 'app/models/spree/order.rb', line 538
def considered_risky!
update_column(:considered_risky, true)
end
|
#contents ⇒ Object
235
236
237
|
# File 'app/models/spree/order.rb', line 235
def contents
@contents ||= Spree::OrderContents.new(self)
end
|
#create_proposed_shipments ⇒ Object
452
453
454
455
456
|
# File 'app/models/spree/order.rb', line 452
def create_proposed_shipments
all_adjustments.shipping.delete_all
shipments.destroy_all
self.shipments = Spree::Stock::Coordinator.new(self).shipments
end
|
#create_tax_charge! ⇒ Object
Creates new tax charges if there are any applicable rates. If prices already include taxes then price adjustments are created instead.
285
286
287
288
|
# File 'app/models/spree/order.rb', line 285
def create_tax_charge!
Spree::TaxRate.adjust(self, line_items)
Spree::TaxRate.adjust(self, shipments) if shipments.any?
end
|
#credit_cards ⇒ Object
319
320
321
322
|
# File 'app/models/spree/order.rb', line 319
def credit_cards
credit_card_ids = payments.from_credit_card.pluck(:source_id).uniq
CreditCard.where(id: credit_card_ids)
end
|
#currency ⇒ Object
155
156
157
|
# File 'app/models/spree/order.rb', line 155
def currency
self[:currency] || Spree::Config[:currency]
end
|
#deliver_order_confirmation_email ⇒ Object
359
360
361
362
|
# File 'app/models/spree/order.rb', line 359
def deliver_order_confirmation_email
OrderMailer.confirm_email(id).deliver_later
update_column(:confirmation_delivered, true)
end
|
#empty! ⇒ Object
408
409
410
411
412
413
414
415
416
417
|
# File 'app/models/spree/order.rb', line 408
def empty!
line_items.destroy_all
updater.update_item_count
adjustments.destroy_all
shipments.destroy_all
state_changes.destroy_all
update_totals
persist_totals
end
|
#ensure_line_item_variants_are_not_deleted ⇒ Object
Check to see if any line item variants are soft deleted. If so add error and restart checkout.
388
389
390
391
392
393
394
395
396
|
# File 'app/models/spree/order.rb', line 388
def ensure_line_item_variants_are_not_deleted
if line_items.any?{ |li| !li.variant || li.variant.paranoia_destroyed? }
errors.add(:base, Spree.t(:deleted_variants_present))
restart_checkout_flow
false
else
true
end
end
|
#ensure_line_items_are_in_stock ⇒ Object
398
399
400
401
402
403
404
405
406
|
# File 'app/models/spree/order.rb', line 398
def ensure_line_items_are_in_stock
if insufficient_stock_lines.present?
errors.add(:base, Spree.t(:insufficient_stock_lines_present))
restart_checkout_flow
false
else
true
end
end
|
#ensure_updated_shipments ⇒ Object
Clean shipments and make order back to address state
At some point the might need to force the order to transition from address to delivery again so that proper updated shipments are created. e.g. customer goes back from payment step and changes order items
470
471
472
473
474
475
476
|
# File 'app/models/spree/order.rb', line 470
def ensure_updated_shipments
if shipments.any? && !self.completed?
self.shipments.destroy_all
self.update_column(:shipment_total, 0)
restart_checkout_flow
end
end
|
#finalize! ⇒ Object
Finalizes an in progress order after checkout is complete. Called after transition to complete state when payments will have been processed
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
# File 'app/models/spree/order.rb', line 331
def finalize!
all_adjustments.each{|a| a.close}
updater.update_payment_state
shipments.each do |shipment|
shipment.update!(self)
shipment.finalize!
end
updater.update_shipment_state
save!
updater.run_hooks
touch :completed_at
deliver_order_confirmation_email unless confirmation_delivered?
consider_risk
end
|
#find_line_item_by_variant(variant, options = {}) ⇒ Object
259
260
261
262
263
264
|
# File 'app/models/spree/order.rb', line 259
def find_line_item_by_variant(variant, options = {})
line_items.detect { |line_item|
line_item.variant_id == variant.id &&
line_item_options_match(line_item, options)
}
end
|
#fulfill! ⇒ Object
353
354
355
356
357
|
# File 'app/models/spree/order.rb', line 353
def fulfill!
shipments.each { |shipment| shipment.update!(self) if shipment.persisted? }
updater.update_shipment_state
save!
end
|
#fully_discounted? ⇒ Boolean
Also known as:
fully_discounted
determines whether the inventory is fully discounted
Returns
569
570
571
|
# File 'app/models/spree/order.rb', line 569
def fully_discounted?
adjustment_total + line_items.map(&:final_amount).sum == 0.0
end
|
#generate_number(options = {}) ⇒ Object
18
19
20
21
|
# File 'app/models/spree/order.rb', line 18
def generate_number(options = {})
options[:prefix] ||= 'R'
super(options)
end
|
559
560
561
562
|
# File 'app/models/spree/order.rb', line 559
def has_non_reimbursement_related_refunds?
refunds.non_reimbursement.exists? ||
payments.offset_payment.exists? end
|
#has_step?(step) ⇒ Boolean
419
420
421
|
# File 'app/models/spree/order.rb', line 419
def has_step?(step)
checkout_steps.include?(step)
end
|
#insufficient_stock_lines ⇒ Object
381
382
383
|
# File 'app/models/spree/order.rb', line 381
def insufficient_stock_lines
line_items.select(&:insufficient_stock?)
end
|
#is_risky? ⇒ Boolean
500
501
502
|
# File 'app/models/spree/order.rb', line 500
def is_risky?
self.payments.risky.count > 0
end
|
#line_item_options_match(line_item, options) ⇒ Object
This method enables extensions to participate in the “Are these line items equal” decision.
When adding to cart, an extension would send something like: params=…
and would provide:
def product_customizations_match
275
276
277
278
279
280
281
|
# File 'app/models/spree/order.rb', line 275
def line_item_options_match(line_item, options)
return true unless options
self.line_item_comparison_hooks.all? { |hook|
self.send(hook, line_item, options)
}
end
|
#merger ⇒ Object
213
214
215
|
# File 'app/models/spree/order.rb', line 213
def merger
@merger ||= Spree::OrderMerger.new(self)
end
|
#name ⇒ Object
309
310
311
312
313
|
# File 'app/models/spree/order.rb', line 309
def name
if (address = bill_address || ship_address)
"#{address.firstname} #{address.lastname}"
end
end
|
#outstanding_balance ⇒ Object
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
# File 'app/models/spree/order.rb', line 290
def outstanding_balance
if state == 'canceled'
-1 * payment_total
elsif reimbursements.includes(:refunds).size > 0
reimbursed = reimbursements.includes(:refunds).inject(0) do |sum, reimbursement|
sum + reimbursement.refunds.sum(:amount)
end
total - (payment_total + reimbursed)
else
total - payment_total
end
end
|
#outstanding_balance? ⇒ Boolean
305
306
307
|
# File 'app/models/spree/order.rb', line 305
def outstanding_balance?
self.outstanding_balance != 0
end
|
#paid? ⇒ Boolean
Helper methods for checkout steps
365
366
367
|
# File 'app/models/spree/order.rb', line 365
def paid?
payment_state == 'paid' || payment_state == 'credit_owed'
end
|
#payment_required? ⇒ Boolean
Is this a free order in which case the payment step should be skipped
176
177
178
|
# File 'app/models/spree/order.rb', line 176
def payment_required?
total.to_f > 0.0
end
|
#pre_tax_item_amount ⇒ Object
Sum of all line item amounts pre-tax
151
152
153
|
# File 'app/models/spree/order.rb', line 151
def pre_tax_item_amount
line_items.to_a.sum(&:pre_tax_amount)
end
|
#quantity ⇒ Object
555
556
557
|
# File 'app/models/spree/order.rb', line 555
def quantity
line_items.sum(:quantity)
end
|
#quantity_of(variant, options = {}) ⇒ Object
254
255
256
257
|
# File 'app/models/spree/order.rb', line 254
def quantity_of(variant, options = {})
line_item = find_line_item_by_variant(variant, options)
line_item ? line_item.quantity : 0
end
|
#refresh_shipment_rates(shipping_method_filter = ShippingMethod::DISPLAY_ON_FRONT_END) ⇒ Object
486
487
488
|
# File 'app/models/spree/order.rb', line 486
def refresh_shipment_rates(shipping_method_filter = ShippingMethod::DISPLAY_ON_FRONT_END)
shipments.map { |s| s.refresh_rates(shipping_method_filter) }
end
|
#reload(options = nil) ⇒ Object
546
547
548
549
|
# File 'app/models/spree/order.rb', line 546
def reload(options=nil)
remove_instance_variable(:@tax_zone) if defined?(@tax_zone)
super
end
|
#restart_checkout_flow ⇒ Object
478
479
480
481
482
483
484
|
# File 'app/models/spree/order.rb', line 478
def restart_checkout_flow
self.update_columns(
state: 'cart',
updated_at: Time.now,
)
self.next! if self.line_items.size > 0
end
|
#set_shipments_cost ⇒ Object
494
495
496
497
498
|
# File 'app/models/spree/order.rb', line 494
def set_shipments_cost
shipments.each(&:update_amounts)
updater.update_shipment_total
persist_totals
end
|
#shipped? ⇒ Boolean
448
449
450
|
# File 'app/models/spree/order.rb', line 448
def shipped?
%w(partial shipped).include?(shipment_state)
end
|
#shipping_discount ⇒ Object
159
160
161
|
# File 'app/models/spree/order.rb', line 159
def shipping_discount
shipment_adjustments.eligible.sum(:amount) * - 1
end
|
#shipping_eq_billing_address? ⇒ Boolean
490
491
492
|
# File 'app/models/spree/order.rb', line 490
def shipping_eq_billing_address?
(bill_address.empty? && ship_address.empty?) || bill_address.same_as?(ship_address)
end
|
#state_changed(name) ⇒ Object
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
|
# File 'app/models/spree/order.rb', line 423
def state_changed(name)
state = "#{name}_state"
if persisted?
old_state = self.send("#{state}_was")
new_state = self.send(state)
unless old_state == new_state
self.state_changes.create(
previous_state: old_state,
next_state: new_state,
name: name,
user_id: self.user_id
)
end
end
end
|
#tax_address ⇒ Object
Returns the address for taxation based on configuration
201
202
203
|
# File 'app/models/spree/order.rb', line 201
def tax_address
Spree::Config[:tax_using_ship_address] ? ship_address : bill_address
end
|
#tax_total ⇒ Object
551
552
553
|
# File 'app/models/spree/order.rb', line 551
def tax_total
included_tax_total + additional_tax_total
end
|
#tax_zone ⇒ Object
Returns the relevant zone (if any) to be used for taxation purposes. Uses default tax zone unless there is a specific match
196
197
198
|
# File 'app/models/spree/order.rb', line 196
def tax_zone
@tax_zone ||= Zone.match(tax_address) || Zone.default_tax
end
|
#update! ⇒ Object
209
210
211
|
# File 'app/models/spree/order.rb', line 209
def update!
updater.update
end
|
#updater ⇒ Object
205
206
207
|
# File 'app/models/spree/order.rb', line 205
def updater
@updater ||= OrderUpdater.new(self)
end
|
#valid_credit_cards ⇒ Object
324
325
326
327
|
# File 'app/models/spree/order.rb', line 324
def valid_credit_cards
credit_card_ids = payments.from_credit_card.valid.pluck(:source_id).uniq
CreditCard.where(id: credit_card_ids)
end
|