Class: Spree::Order
- Inherits:
-
Base
- Object
- ApplicationRecord
- Base
- Spree::Order
show all
- Extended by:
- DisplayMoney
- Includes:
- Core::TokenGenerator, MemoizedData, Metadata, NumberAsParam, NumberIdentifier, AddressBook, Checkout, CurrencyUpdater, Digital, Emails, Payments, StoreCredit, Security::Orders, SingleStoreResource, VendorConcern, Webhooks::HasWebhooks
- Defined in:
- app/models/spree/order.rb,
app/models/spree/order/emails.rb,
app/models/spree/order/digital.rb,
app/models/spree/order/checkout.rb,
app/models/spree/order/payments.rb,
app/models/spree/order/address_book.rb,
app/models/spree/order/store_credit.rb,
app/models/spree/order/currency_updater.rb
Defined Under Namespace
Modules: AddressBook, Checkout, CurrencyUpdater, Digital, Emails, Payments, StoreCredit
Constant Summary
collapse
- PAYMENT_STATES =
%w(balance_due credit_owed failed paid void)
- SHIPMENT_STATES =
%w(backorder canceled partial pending ready shipped)
- MEMOIZED_METHODS =
%w(tax_zone)
- 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_token
Methods included from Emails
#deliver_order_confirmation_email, #deliver_store_owner_order_notification_email, #deliver_store_owner_order_notification_email?, #send_cancel_email
#bill_address_attributes=, #bill_address_id=, #clone_billing_address, #clone_shipping_address, #ship_address_attributes=, #ship_address_id=
#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?
Methods included from Digital
#create_digital_links, #digital?, #digital_line_items, #digital_links, #some_digital?
#price_from_line_item, #update_line_item_currencies!, #update_line_item_price!
Methods included from Checkout
included
Methods inherited from Base
belongs_to_required_by_default, for_store, has_many_inversing, json_api_columns, json_api_permitted_attributes, json_api_type, page, spree_base_scopes, spree_base_uniqueness_scope
#clear_preferences, #default_preferences, #defined_preferences, #deprecated_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_deprecated, #preference_type, #set_preference
Instance Attribute Details
#coupon_code ⇒ Object
Returns the value of attribute coupon_code.
83
84
85
|
# File 'app/models/spree/order.rb', line 83
def coupon_code
@coupon_code
end
|
#temporary_address ⇒ Object
Returns the value of attribute temporary_address.
84
85
86
|
# File 'app/models/spree/order.rb', line 84
def temporary_address
@temporary_address
end
|
#temporary_credit_card ⇒ Object
Returns the value of attribute temporary_credit_card.
84
85
86
|
# File 'app/models/spree/order.rb', line 84
def temporary_credit_card
@temporary_credit_card
end
|
#use_billing ⇒ Object
Returns the value of attribute use_billing.
154
155
156
|
# File 'app/models/spree/order.rb', line 154
def use_billing
@use_billing
end
|
Class Method Details
.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
199
200
201
|
# File 'app/models/spree/order.rb', line 199
def self.register_update_hook(hook)
update_hooks.add(hook)
end
|
Instance Method Details
#all_inventory_units_returned? ⇒ Boolean
291
292
293
|
# File 'app/models/spree/order.rb', line 291
def all_inventory_units_returned?
inventory_units.all?(&:returned?)
end
|
#allow_cancel? ⇒ Boolean
285
286
287
288
289
|
# File 'app/models/spree/order.rb', line 285
def allow_cancel?
return false if !completed? || canceled?
shipment_state.nil? || %w{ready backorder pending}.include?(shipment_state)
end
|
#amount ⇒ Object
For compatibility with Calculator::PriceSack
204
205
206
|
# File 'app/models/spree/order.rb', line 204
def amount
line_items.inject(0.0) { |sum, li| sum + li.amount }
end
|
Applies user promotions when login after filling the cart
#approve! ⇒ Object
614
615
616
|
# File 'app/models/spree/order.rb', line 614
def approve!
update_column(:considered_risky, false)
end
|
#approved? ⇒ Boolean
594
595
596
|
# File 'app/models/spree/order.rb', line 594
def approved?
!!approved_at
end
|
#approved_by(user) ⇒ Object
584
585
586
587
588
589
590
591
592
|
# File 'app/models/spree/order.rb', line 584
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.
296
297
298
299
300
301
302
303
304
305
306
307
308
|
# File 'app/models/spree/order.rb', line 296
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.try(:clone)
self.ship_address ||= user.ship_address.try(:clone)
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(store = nil) ⇒ Object
416
417
418
419
420
421
422
|
# File 'app/models/spree/order.rb', line 416
def available_payment_methods(store = nil)
if store.present?
Spree::Deprecation.warn('The `store` parameter is deprecated and will be removed in Spree 5. Order is already associated with Store')
end
@available_payment_methods ||= collect_payment_methods(store)
end
|
#backordered? ⇒ Boolean
254
255
256
|
# File 'app/models/spree/order.rb', line 254
def backordered?
shipments.any?(&:backordered?)
end
|
#can_add_coupon? ⇒ Boolean
#can_approve? ⇒ Boolean
598
599
600
|
# File 'app/models/spree/order.rb', line 598
def can_approve?
!approved?
end
|
#can_be_destroyed? ⇒ Boolean
602
603
604
|
# File 'app/models/spree/order.rb', line 602
def can_be_destroyed?
!completed? && payments.completed.empty?
end
|
#can_ship? ⇒ Boolean
362
363
364
|
# File 'app/models/spree/order.rb', line 362
def can_ship?
complete? || resumed? || awaiting_return? || returned?
end
|
#canceled_by(user) ⇒ Object
574
575
576
577
578
579
580
581
582
|
# File 'app/models/spree/order.rb', line 574
def canceled_by(user)
transaction do
cancel!
update_columns(
canceler_id: user.id,
canceled_at: Time.current
)
end
end
|
Returns item and whole order discount amount for Order without Shipment disccounts (eg. Free Shipping)
679
680
681
682
683
|
# File 'app/models/spree/order.rb', line 679
def cart_promo_total
all_adjustments.eligible.nonzero.promotion.
where.not(adjustable_type: 'Spree::Shipment').
sum(:amount)
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.
230
231
232
|
# File 'app/models/spree/order.rb', line 230
def checkout_allowed?
line_items.exists?
end
|
#collect_backend_payment_methods ⇒ Object
631
632
633
|
# File 'app/models/spree/order.rb', line 631
def collect_backend_payment_methods
PaymentMethod.available_on_back_end.select { |pm| pm.available_for_order?(self) }
end
|
#completed? ⇒ Boolean
222
223
224
|
# File 'app/models/spree/order.rb', line 222
def completed?
completed_at.present?
end
|
#confirmation_required? ⇒ Boolean
If true, causes the confirmation step to happen during the checkout process
245
246
247
248
249
250
251
252
|
# File 'app/models/spree/order.rb', line 245
def confirmation_required?
Spree::Config[:always_include_confirm_step] ||
payments.valid.map(&:payment_method).compact.any?(&:payment_profiles_supported?) ||
confirm?
end
|
#consider_risk ⇒ Object
606
607
608
|
# File 'app/models/spree/order.rb', line 606
def consider_risk
considered_risky! if is_risky? && !approved?
end
|
#considered_risky! ⇒ Object
610
611
612
|
# File 'app/models/spree/order.rb', line 610
def considered_risky!
update_column(:considered_risky, true)
end
|
#create_proposed_shipments ⇒ Object
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
|
# File 'app/models/spree/order.rb', line 507
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_shipment_tax_charge! ⇒ Object
329
330
331
|
# File 'app/models/spree/order.rb', line 329
def create_shipment_tax_charge!
Spree::TaxRate.adjust(self, shipments) if shipments.any?
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.
324
325
326
327
|
# File 'app/models/spree/order.rb', line 324
def create_tax_charge!
Spree::TaxRate.adjust(self, line_items)
Spree::TaxRate.adjust(self, shipments) if shipments.any?
end
|
#credit_cards ⇒ Object
370
371
372
373
|
# File 'app/models/spree/order.rb', line 370
def credit_cards
credit_card_ids = payments.from_credit_card.pluck(:source_id).uniq
CreditCard.where(id: credit_card_ids)
end
|
#delivery_required? ⇒ Boolean
Does this order require a physical delivery.
235
236
237
|
# File 'app/models/spree/order.rb', line 235
def delivery_required?
!digital?
end
|
#empty! ⇒ Object
451
452
453
454
455
456
457
458
459
460
461
|
# File 'app/models/spree/order.rb', line 451
def empty!
Spree::Deprecation.warn(<<-DEPRECATION, caller)
`Order#empty!` is deprecated and will be removed in Spree 5.0.
Please use `Spree::Cart::Empty.call(order: order)` instead.
DEPRECATION
raise Spree.t(:cannot_empty_completed_order) if completed?
result = Spree::Dependencies.cart_empty_service.constantize.call(order: self)
result.value
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.
431
432
433
434
435
436
437
438
439
|
# File 'app/models/spree/order.rb', line 431
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
441
442
443
444
445
446
447
448
449
|
# File 'app/models/spree/order.rb', line 441
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_store_presence ⇒ Object
281
282
283
|
# File 'app/models/spree/order.rb', line 281
def ensure_store_presence
self.store ||= Spree::Store.default
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
540
541
542
543
544
545
546
|
# File 'app/models/spree/order.rb', line 540
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
|
# File 'app/models/spree/order.rb', line 382
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?
deliver_store_owner_order_notification_email if deliver_store_owner_order_notification_email?
consider_risk
end
|
#find_line_item_by_variant(variant, options = {}) ⇒ Object
315
316
317
318
319
320
|
# File 'app/models/spree/order.rb', line 315
def find_line_item_by_variant(variant, options = {})
line_items.detect do |line_item|
line_item.variant_id == variant.id &&
Spree::Dependencies.cart_compare_line_items_service.constantize.new.call(order: self, line_item: line_item, options: options).value
end
end
|
#fulfill! ⇒ Object
405
406
407
408
409
|
# File 'app/models/spree/order.rb', line 405
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
640
641
642
|
# File 'app/models/spree/order.rb', line 640
def fully_discounted?
adjustment_total + line_items.map(&:final_amount).sum == 0.0
end
|
#fully_shipped? ⇒ Boolean
503
504
505
|
# File 'app/models/spree/order.rb', line 503
def fully_shipped?
shipments.shipped.size == shipments.size
end
|
#has_free_shipping? ⇒ Boolean
685
686
687
688
689
690
|
# File 'app/models/spree/order.rb', line 685
def has_free_shipping?
shipment_adjustments.
joins(:promotion_action).
where(spree_adjustments: { eligible: true, source_type: 'Spree::PromotionAction' },
spree_promotion_actions: { type: 'Spree::Promotion::Actions::FreeShipping' }).exists?
end
|
626
627
628
629
|
# File 'app/models/spree/order.rb', line 626
def has_non_reimbursement_related_refunds?
refunds.non_reimbursement.exists? ||
payments.offset_payment.exists? end
|
#has_step?(step) ⇒ Boolean
463
464
465
|
# File 'app/models/spree/order.rb', line 463
def has_step?(step)
checkout_steps.include?(step)
end
|
#insufficient_stock_lines ⇒ Object
424
425
426
|
# File 'app/models/spree/order.rb', line 424
def insufficient_stock_lines
line_items.select(&:insufficient_stock?)
end
|
#is_risky? ⇒ Boolean
570
571
572
|
# File 'app/models/spree/order.rb', line 570
def is_risky?
!payments.risky.empty?
end
|
#log_state_changes(state_name:, old_state:, new_state:) ⇒ Object
478
479
480
481
482
483
484
485
|
# File 'app/models/spree/order.rb', line 478
def log_state_changes(state_name:, old_state:, new_state:)
state_changes.create(
previous_state: old_state,
next_state: new_state,
name: state_name,
user_id: user_id
)
end
|
#merger ⇒ Object
277
278
279
|
# File 'app/models/spree/order.rb', line 277
def merger
@merger ||= Spree::OrderMerger.new(self)
end
|
#name ⇒ Object
356
357
358
359
360
|
# File 'app/models/spree/order.rb', line 356
def name
if (address = bill_address || ship_address)
address.full_name
end
end
|
#outstanding_balance ⇒ Object
340
341
342
343
344
345
346
|
# File 'app/models/spree/order.rb', line 340
def outstanding_balance
if canceled?
-1 * payment_total
else
total - (payment_total + reimbursement_paid_total)
end
end
|
#outstanding_balance? ⇒ Boolean
352
353
354
|
# File 'app/models/spree/order.rb', line 352
def outstanding_balance?
outstanding_balance != 0
end
|
#paid? ⇒ Boolean
Helper methods for checkout steps
412
413
414
|
# File 'app/models/spree/order.rb', line 412
def paid?
payments.valid.completed.size == payments.valid.size && payments.valid.sum(:amount) >= total
end
|
#payment_required? ⇒ Boolean
Is this a free order in which case the payment step should be skipped
240
241
242
|
# File 'app/models/spree/order.rb', line 240
def payment_required?
total.to_f > 0.0
end
|
#pre_tax_item_amount ⇒ Object
Sum of all line item amounts pre-tax
209
210
211
|
# File 'app/models/spree/order.rb', line 209
def pre_tax_item_amount
line_items.sum(:pre_tax_amount)
end
|
#pre_tax_total ⇒ Object
Sum of all line item and shipment pre-tax
214
215
216
|
# File 'app/models/spree/order.rb', line 214
def pre_tax_total
pre_tax_item_amount + shipments.sum(:pre_tax_amount)
end
|
645
646
647
|
# File 'app/models/spree/order.rb', line 645
def promo_code
promotions.pluck(:code).compact.first
end
|
#quantity ⇒ Object
622
623
624
|
# File 'app/models/spree/order.rb', line 622
def quantity
line_items.sum(:quantity)
end
|
#quantity_of(variant, options = {}) ⇒ Object
310
311
312
313
|
# File 'app/models/spree/order.rb', line 310
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
556
557
558
|
# File 'app/models/spree/order.rb', line 556
def refresh_shipment_rates(shipping_method_filter = ShippingMethod::DISPLAY_ON_FRONT_END)
shipments.map { |s| s.refresh_rates(shipping_method_filter) }
end
|
#reimbursement_paid_total ⇒ Object
348
349
350
|
# File 'app/models/spree/order.rb', line 348
def reimbursement_paid_total
reimbursements.sum(&:paid_amount)
end
|
#restart_checkout_flow ⇒ Object
548
549
550
551
552
553
554
|
# File 'app/models/spree/order.rb', line 548
def restart_checkout_flow
update_columns(
state: 'cart',
updated_at: Time.current
)
next! unless line_items.empty?
end
|
#set_shipments_cost ⇒ Object
564
565
566
567
568
|
# File 'app/models/spree/order.rb', line 564
def set_shipments_cost
shipments.each(&:update_amounts)
updater.update_shipment_total
persist_totals
end
|
#shipped? ⇒ Boolean
499
500
501
|
# File 'app/models/spree/order.rb', line 499
def shipped?
%w(partial shipped).include?(shipment_state)
end
|
#shipping_discount ⇒ Object
218
219
220
|
# File 'app/models/spree/order.rb', line 218
def shipping_discount
shipment_adjustments.non_tax.eligible.sum(:amount) * - 1
end
|
#shipping_eq_billing_address? ⇒ Boolean
560
561
562
|
# File 'app/models/spree/order.rb', line 560
def shipping_eq_billing_address?
bill_address == ship_address
end
|
#state_changed(name) ⇒ Object
467
468
469
470
471
472
473
474
475
476
|
# File 'app/models/spree/order.rb', line 467
def state_changed(name)
state = "#{name}_state"
if persisted?
old_state = send("#{state}_was")
new_state = send(state)
unless old_state == new_state
log_state_changes(state_name: name, old_state: old_state, new_state: new_state)
end
end
end
|
#tax_address ⇒ Object
Returns the address for taxation based on configuration
265
266
267
|
# File 'app/models/spree/order.rb', line 265
def tax_address
Spree::Config[:tax_using_ship_address] ? ship_address : bill_address
end
|
#tax_total ⇒ Object
618
619
620
|
# File 'app/models/spree/order.rb', line 618
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
260
261
262
|
# File 'app/models/spree/order.rb', line 260
def tax_zone
@tax_zone ||= Zone.match(tax_address) || Zone.default_tax
end
|
#uneditable? ⇒ Boolean
366
367
368
|
# File 'app/models/spree/order.rb', line 366
def uneditable?
complete? || canceled? || returned?
end
|
#update_line_item_prices! ⇒ Object
333
334
335
336
337
338
|
# File 'app/models/spree/order.rb', line 333
def update_line_item_prices!
transaction do
line_items.reload.each(&:update_price)
save!
end
end
|
#update_with_updater! ⇒ Object
273
274
275
|
# File 'app/models/spree/order.rb', line 273
def update_with_updater!
updater.update
end
|
#updater ⇒ Object
269
270
271
|
# File 'app/models/spree/order.rb', line 269
def updater
@updater ||= OrderUpdater.new(self)
end
|
670
671
672
673
674
|
# File 'app/models/spree/order.rb', line 670
def valid_coupon_promotions
promotions.
where(id: valid_promotion_ids).
coupons
end
|
#valid_credit_cards ⇒ Object
375
376
377
378
|
# File 'app/models/spree/order.rb', line 375
def valid_credit_cards
credit_card_ids = payments.from_credit_card.valid.pluck(:source_id).uniq
CreditCard.where(id: credit_card_ids)
end
|
666
667
668
|
# File 'app/models/spree/order.rb', line 666
def valid_promotion_ids
all_adjustments.eligible.nonzero.promotion.map { |a| a.source.promotion_id }.uniq
end
|
662
663
664
|
# File 'app/models/spree/order.rb', line 662
def valid_promotions
order_promotions.where(promotion_id: valid_promotion_ids).uniq(&:promotion_id)
end
|
#validate_payments_attributes(attributes) ⇒ Object
649
650
651
652
653
654
655
656
657
658
659
660
|
# File 'app/models/spree/order.rb', line 649
def validate_payments_attributes(attributes)
Spree::Deprecation.warn('`Order#validate_payments_attributes` is deprecated and will be removed in Spree 5')
payment_method_ids = available_payment_methods.map(&:id).map(&:to_s)
attributes.each do |payment_attributes|
payment_method_id = payment_attributes[:payment_method_id].to_s
raise ActiveRecord::RecordNotFound unless payment_method_ids.include?(payment_method_id)
end
end
|