Class: Spree::Order
Defined Under Namespace
Modules: Checkout, CurrencyUpdater, Payments, StoreCredit
Constant Summary
collapse
- PAYMENT_STATES =
%w(balance_due credit_owed failed paid void)
- SHIPMENT_STATES =
%w(backorder canceled partial pending ready shipped)
- MONEY_THRESHOLD =
100_000_000
- MONEY_VALIDATION =
{
presence: true,
numericality: {
greater_than: -MONEY_THRESHOLD,
less_than: MONEY_THRESHOLD,
allow_blank: true
},
format: { with: /\A-?\d+(?:\.\d{1,2})?\z/, allow_blank: true }
}.freeze
- POSITIVE_MONEY_VALIDATION =
MONEY_VALIDATION.deep_dup.tap do |validation|
validation.fetch(:numericality)[:greater_than_or_equal_to] = 0
end.freeze
- NEGATIVE_MONEY_VALIDATION =
MONEY_VALIDATION.deep_dup.tap do |validation|
validation.fetch(:numericality)[:less_than_or_equal_to] = 0
end.freeze
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
money_methods
#to_param
#generate_guest_token
#add_store_credit_payments, #could_use_store_credit?, #covered_by_store_credit?, #display_order_total_after_store_credit, #display_store_credit_remaining_after_capture, #display_total_applicable_store_credit, #display_total_applied_store_credit, #display_total_available_store_credit, #order_total_after_store_credit, #remove_store_credit_payments, #total_applicable_store_credit, #total_applied_store_credit, #total_available_store_credit, #using_store_credit?
#price_from_line_item, #update_line_item_currencies!, #update_line_item_price!
Methods included from Checkout
included
Methods inherited from Base
page, spree_base_scopes
#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.
57
58
59
|
# File 'app/models/spree/order.rb', line 57
def coupon_code
@coupon_code
end
|
#temporary_address ⇒ Object
Returns the value of attribute temporary_address.
58
59
60
|
# File 'app/models/spree/order.rb', line 58
def temporary_address
@temporary_address
end
|
#temporary_credit_card ⇒ Object
Returns the value of attribute temporary_credit_card.
58
59
60
|
# File 'app/models/spree/order.rb', line 58
def temporary_credit_card
@temporary_credit_card
end
|
#use_billing ⇒ Object
Returns the value of attribute use_billing.
120
121
122
|
# File 'app/models/spree/order.rb', line 120
def use_billing
@use_billing
end
|
Class Method Details
.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.
168
169
170
|
# File 'app/models/spree/order.rb', line 168
def self.register_line_item_comparison_hook(hook)
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
162
163
164
|
# File 'app/models/spree/order.rb', line 162
def self.register_update_hook(hook)
update_hooks.add(hook)
end
|
Instance Method Details
#all_inventory_units_returned? ⇒ Boolean
258
259
260
|
# File 'app/models/spree/order.rb', line 258
def all_inventory_units_returned?
inventory_units.all?(&:returned?)
end
|
#allow_cancel? ⇒ Boolean
253
254
255
256
|
# File 'app/models/spree/order.rb', line 253
def allow_cancel?
return false if !completed? || canceled?
shipment_state.nil? || %w{ready backorder pending}.include?(shipment_state)
end
|
#amount ⇒ Object
For compatiblity with Calculator::PriceSack
173
174
175
|
# File 'app/models/spree/order.rb', line 173
def amount
line_items.inject(0.0) { |sum, li| sum + li.amount }
end
|
#approve! ⇒ Object
585
586
587
|
# File 'app/models/spree/order.rb', line 585
def approve!
update_column(:considered_risky, false)
end
|
#approved? ⇒ Boolean
567
568
569
|
# File 'app/models/spree/order.rb', line 567
def approved?
!!approved_at
end
|
#approved_by(user) ⇒ Object
557
558
559
560
561
562
563
564
565
|
# File 'app/models/spree/order.rb', line 557
def approved_by(user)
transaction do
approve!
update_columns(
approver_id: user.id,
approved_at: Time.current,
)
end
end
|
#associate_user!(user, override_email = true) ⇒ Object
Associates the specified user with the order.
267
268
269
270
271
272
273
274
275
276
277
278
279
|
# File 'app/models/spree/order.rb', line 267
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
400
401
402
|
# File 'app/models/spree/order.rb', line 400
def available_payment_methods
@available_payment_methods ||= collect_payment_methods
end
|
#backordered? ⇒ Boolean
217
218
219
|
# File 'app/models/spree/order.rb', line 217
def backordered?
shipments.any?(&:backordered?)
end
|
#can_add_coupon? ⇒ Boolean
#can_approve? ⇒ Boolean
571
572
573
|
# File 'app/models/spree/order.rb', line 571
def can_approve?
!approved?
end
|
#can_ship? ⇒ Boolean
346
347
348
|
# File 'app/models/spree/order.rb', line 346
def can_ship?
complete? || resumed? || awaiting_return? || returned?
end
|
#canceled_by(user) ⇒ Object
547
548
549
550
551
552
553
554
555
|
# File 'app/models/spree/order.rb', line 547
def canceled_by(user)
transaction do
cancel!
update_columns(
canceler_id: user.id,
canceled_at: Time.current,
)
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.
198
199
200
|
# File 'app/models/spree/order.rb', line 198
def checkout_allowed?
line_items.count > 0
end
|
#clone_billing_address ⇒ Object
244
245
246
247
248
249
250
251
|
# File 'app/models/spree/order.rb', line 244
def clone_billing_address
if bill_address && ship_address.nil?
self.ship_address = bill_address.clone
else
ship_address.attributes = bill_address.attributes.except('id', 'updated_at', 'created_at')
end
true
end
|
#collect_backend_payment_methods ⇒ Object
607
608
609
|
# File 'app/models/spree/order.rb', line 607
def collect_backend_payment_methods
PaymentMethod.available_on_back_end.select { |pm| pm.available_for_order?(self) }
end
|
#completed? ⇒ Boolean
190
191
192
|
# File 'app/models/spree/order.rb', line 190
def completed?
completed_at.present?
end
|
#confirmation_required? ⇒ Boolean
If true, causes the confirmation step to happen during the checkout process
208
209
210
211
212
213
214
215
|
# File 'app/models/spree/order.rb', line 208
def confirmation_required?
Spree::Config[:always_include_confirm_step] ||
payments.valid.map(&:payment_method).compact.any?(&:payment_profiles_supported?) ||
confirm?
end
|
#consider_risk ⇒ Object
575
576
577
578
579
|
# File 'app/models/spree/order.rb', line 575
def consider_risk
if is_risky? && !approved?
considered_risky!
end
end
|
#considered_risky! ⇒ Object
581
582
583
|
# File 'app/models/spree/order.rb', line 581
def considered_risky!
update_column(:considered_risky, true)
end
|
#contents ⇒ Object
262
263
264
|
# File 'app/models/spree/order.rb', line 262
def contents
@contents ||= Spree::OrderContents.new(self)
end
|
#create_proposed_shipments ⇒ Object
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
|
# File 'app/models/spree/order.rb', line 485
def create_proposed_shipments
all_adjustments.shipping.delete_all
shipment_ids = shipments.map(&:id)
StateChange.where(stateful_type: "Spree::Shipment", stateful_id: shipment_ids).delete_all
ShippingRate.where(shipment_id: shipment_ids).delete_all
shipments.delete_all
inventory_units.on_hand_or_backordered.delete_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.
312
313
314
315
|
# File 'app/models/spree/order.rb', line 312
def create_tax_charge!
Spree::TaxRate.adjust(self, line_items)
Spree::TaxRate.adjust(self, shipments) if shipments.any?
end
|
#credit_cards ⇒ Object
350
351
352
353
|
# File 'app/models/spree/order.rb', line 350
def credit_cards
credit_card_ids = payments.from_credit_card.pluck(:source_id).uniq
CreditCard.where(id: credit_card_ids)
end
|
#currency ⇒ Object
182
183
184
|
# File 'app/models/spree/order.rb', line 182
def currency
self[:currency] || Spree::Config[:currency]
end
|
#deliver_order_confirmation_email ⇒ Object
390
391
392
393
|
# File 'app/models/spree/order.rb', line 390
def deliver_order_confirmation_email
OrderMailer.confirm_email(id).deliver_later
update_column(:confirmation_delivered, true)
end
|
#empty! ⇒ Object
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
|
# File 'app/models/spree/order.rb', line 431
def empty!
if completed?
raise Spree.t(:cannot_empty_completed_order)
else
line_items.destroy_all
updater.update_item_count
adjustments.destroy_all
shipments.destroy_all
state_changes.destroy_all
order_promotions.destroy_all
update_totals
persist_totals
restart_checkout_flow
self
end
end
|
#ensure_line_item_variants_are_not_discontinued ⇒ Object
Check to see if any line item variants are discontinued. If so add error and restart checkout.
411
412
413
414
415
416
417
418
419
|
# File 'app/models/spree/order.rb', line 411
def ensure_line_item_variants_are_not_discontinued
if line_items.any? { |li| !li.variant || li.variant.discontinued? }
restart_checkout_flow
errors.add(:base, Spree.t(:discontinued_variants_present))
false
else
true
end
end
|
#ensure_line_items_are_in_stock ⇒ Object
421
422
423
424
425
426
427
428
429
|
# File 'app/models/spree/order.rb', line 421
def ensure_line_items_are_in_stock
if insufficient_stock_lines.present?
restart_checkout_flow
errors.add(:base, Spree.t(:insufficient_stock_lines_present))
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
513
514
515
516
517
518
519
|
# File 'app/models/spree/order.rb', line 513
def ensure_updated_shipments
if shipments.any? && !completed?
shipments.destroy_all
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
|
# File 'app/models/spree/order.rb', line 362
def finalize!
all_adjustments.each(&: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
286
287
288
289
290
291
|
# File 'app/models/spree/order.rb', line 286
def find_line_item_by_variant(variant, options = {})
line_items.detect do |line_item|
line_item.variant_id == variant.id &&
line_item_options_match(line_item, options)
end
end
|
#fulfill! ⇒ Object
384
385
386
387
388
|
# File 'app/models/spree/order.rb', line 384
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
616
617
618
|
# File 'app/models/spree/order.rb', line 616
def fully_discounted?
adjustment_total + line_items.map(&:final_amount).sum == 0.0
end
|
602
603
604
605
|
# File 'app/models/spree/order.rb', line 602
def has_non_reimbursement_related_refunds?
refunds.non_reimbursement.exists? ||
payments.offset_payment.exists? end
|
#has_step?(step) ⇒ Boolean
449
450
451
|
# File 'app/models/spree/order.rb', line 449
def has_step?(step)
checkout_steps.include?(step)
end
|
#insufficient_stock_lines ⇒ Object
404
405
406
|
# File 'app/models/spree/order.rb', line 404
def insufficient_stock_lines
line_items.select(&:insufficient_stock?)
end
|
#is_risky? ⇒ Boolean
543
544
545
|
# File 'app/models/spree/order.rb', line 543
def is_risky?
!payments.risky.empty?
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
302
303
304
305
306
307
308
|
# File 'app/models/spree/order.rb', line 302
def line_item_options_match(line_item, options)
return true unless options
line_item_comparison_hooks.all? do |hook|
send(hook, line_item, options)
end
end
|
#merger ⇒ Object
240
241
242
|
# File 'app/models/spree/order.rb', line 240
def merger
@merger ||= Spree::OrderMerger.new(self)
end
|
#name ⇒ Object
340
341
342
343
344
|
# File 'app/models/spree/order.rb', line 340
def name
if (address = bill_address || ship_address)
address.full_name
end
end
|
#outstanding_balance ⇒ Object
324
325
326
327
328
329
330
331
332
333
334
|
# File 'app/models/spree/order.rb', line 324
def outstanding_balance
if canceled?
-1 * payment_total
elsif refunds.exists?
total - (payment_total + refunds.sum(:amount))
else
total - payment_total
end
end
|
#outstanding_balance? ⇒ Boolean
336
337
338
|
# File 'app/models/spree/order.rb', line 336
def outstanding_balance?
outstanding_balance != 0
end
|
#paid? ⇒ Boolean
Helper methods for checkout steps
396
397
398
|
# File 'app/models/spree/order.rb', line 396
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
203
204
205
|
# File 'app/models/spree/order.rb', line 203
def payment_required?
total.to_f > 0.0
end
|
#pre_tax_item_amount ⇒ Object
Sum of all line item amounts pre-tax
178
179
180
|
# File 'app/models/spree/order.rb', line 178
def pre_tax_item_amount
line_items.to_a.sum(&:pre_tax_amount)
end
|
621
622
623
|
# File 'app/models/spree/order.rb', line 621
def promo_code
promotions.pluck(:code).compact.first
end
|
#quantity ⇒ Object
598
599
600
|
# File 'app/models/spree/order.rb', line 598
def quantity
line_items.sum(:quantity)
end
|
#quantity_of(variant, options = {}) ⇒ Object
281
282
283
284
|
# File 'app/models/spree/order.rb', line 281
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
529
530
531
|
# File 'app/models/spree/order.rb', line 529
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
589
590
591
592
|
# File 'app/models/spree/order.rb', line 589
def reload(options = nil)
remove_instance_variable(:@tax_zone) if defined?(@tax_zone)
super
end
|
#restart_checkout_flow ⇒ Object
521
522
523
524
525
526
527
|
# File 'app/models/spree/order.rb', line 521
def restart_checkout_flow
update_columns(
state: 'cart',
updated_at: Time.current,
)
next! if !line_items.empty?
end
|
#set_shipments_cost ⇒ Object
537
538
539
540
541
|
# File 'app/models/spree/order.rb', line 537
def set_shipments_cost
shipments.each(&:update_amounts)
updater.update_shipment_total
persist_totals
end
|
#shipped? ⇒ Boolean
481
482
483
|
# File 'app/models/spree/order.rb', line 481
def shipped?
%w(partial shipped).include?(shipment_state)
end
|
#shipping_discount ⇒ Object
186
187
188
|
# File 'app/models/spree/order.rb', line 186
def shipping_discount
shipment_adjustments.non_tax.eligible.sum(:amount) * - 1
end
|
#shipping_eq_billing_address? ⇒ Boolean
533
534
535
|
# File 'app/models/spree/order.rb', line 533
def shipping_eq_billing_address?
(bill_address.empty? && ship_address.empty?) || bill_address.same_as?(ship_address)
end
|
#state_changed(name) ⇒ Object
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
|
# File 'app/models/spree/order.rb', line 453
def state_changed(name)
state = "#{name}_state"
if persisted?
old_state = send("#{state}_was")
new_state = send(state)
unless old_state == new_state
state_changes.create(
previous_state: old_state,
next_state: new_state,
name: name,
user_id: user_id
)
end
end
end
|
#tax_address ⇒ Object
Returns the address for taxation based on configuration
228
229
230
|
# File 'app/models/spree/order.rb', line 228
def tax_address
Spree::Config[:tax_using_ship_address] ? ship_address : bill_address
end
|
#tax_total ⇒ Object
594
595
596
|
# File 'app/models/spree/order.rb', line 594
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
223
224
225
|
# File 'app/models/spree/order.rb', line 223
def tax_zone
@tax_zone ||= Zone.match(tax_address) || Zone.default_tax
end
|
#update_line_item_prices! ⇒ Object
317
318
319
320
321
322
|
# File 'app/models/spree/order.rb', line 317
def update_line_item_prices!
transaction do
line_items.each(&:update_price)
save!
end
end
|
#update_with_updater! ⇒ Object
236
237
238
|
# File 'app/models/spree/order.rb', line 236
def update_with_updater!
updater.update
end
|
#updater ⇒ Object
232
233
234
|
# File 'app/models/spree/order.rb', line 232
def updater
@updater ||= OrderUpdater.new(self)
end
|
#valid_credit_cards ⇒ Object
355
356
357
358
|
# File 'app/models/spree/order.rb', line 355
def valid_credit_cards
credit_card_ids = payments.from_credit_card.valid.pluck(:source_id).uniq
CreditCard.where(id: credit_card_ids)
end
|