Class: Spree::Order

Inherits:
Base
  • Object
show all
Extended by:
DisplayMoney
Includes:
Checkout, Payments
Defined in:
app/models/spree/order.rb,
app/models/spree/order/checkout.rb,
app/models/spree/order/payments.rb

Overview

The customers cart until completed, then acts as permanent record of the transaction.

`Spree::Order` is the heart of the Solidus system, as it acts as the customer's cart as they shop. Once an order is complete, it serves as the permanent record of their purchase. It has many responsibilities:

  • Records and validates attributes like `total` and relationships like

`Spree::LineItem` as an ActiveRecord model.

  • Implements a customizable state machine to manage the lifecycle of an order.

  • Implements business logic to provide a single interface for quesitons like

`checkout_allowed?` or `payment_required?`.

* Implements an interface for mutating the order with methods like

`empty!` and `fulfill!`.

Defined Under Namespace

Modules: Checkout, Payments Classes: CannotRebuildShipments, InsufficientStock, NumberGenerator

Constant Summary

ORDER_NUMBER_LENGTH =
9
ORDER_NUMBER_LETTERS =
false
ORDER_NUMBER_PREFIX =
'R'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DisplayMoney

money_methods

Methods included from Payments

#authorize_payments!, #capture_payments!, #process_payments!, #unprocessed_payments

Methods included from Checkout

#can_go_to_state?, #checkout_step_index, #checkout_steps, #has_checkout_step?, included, #passed_checkout_step?

Methods inherited from Base

display_includes, #initialize_preference_defaults, page, preference

Methods included from Preferences::Preferable

#default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference

Instance Attribute Details

#coupon_codeObject

Returns the value of attribute coupon_code



50
51
52
# File 'app/models/spree/order.rb', line 50

def coupon_code
  @coupon_code
end

#temporary_addressObject

Returns the value of attribute temporary_address



51
52
53
# File 'app/models/spree/order.rb', line 51

def temporary_address
  @temporary_address
end

#temporary_payment_sourceObject Also known as: temporary_credit_card

Returns the value of attribute temporary_payment_source



53
54
55
# File 'app/models/spree/order.rb', line 53

def temporary_payment_source
  @temporary_payment_source
end

#use_billingObject

Returns the value of attribute use_billing



124
125
126
# File 'app/models/spree/order.rb', line 124

def use_billing
  @use_billing
end

Class Method Details

.by_customer(customer) ⇒ Object



156
157
158
# File 'app/models/spree/order.rb', line 156

def self.by_customer(customer)
  joins(:user).where("#{Spree.user_class.table_name}.email" => customer)
end

.by_state(state) ⇒ Object



160
161
162
# File 'app/models/spree/order.rb', line 160

def self.by_state(state)
  where(state: state)
end

.completeObject



164
165
166
# File 'app/models/spree/order.rb', line 164

def self.complete
  where.not(completed_at: nil)
end

.incompleteObject



168
169
170
# File 'app/models/spree/order.rb', line 168

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.



180
181
182
# File 'app/models/spree/order.rb', line 180

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



174
175
176
# File 'app/models/spree/order.rb', line 174

def self.register_update_hook(hook)
  update_hooks.add(hook)
end

Instance Method Details

#add_default_payment_from_walletObject Also known as: assign_default_credit_card



739
740
741
742
743
744
745
746
747
748
749
750
751
# File 'app/models/spree/order.rb', line 739

def add_default_payment_from_wallet
  builder = Spree::Config.default_payment_builder_class.new(self)

  if payment = builder.build
    payments << payment

    if bill_address.nil?
      # this is one of 2 places still using User#bill_address
      self.bill_address = payment.source.try(:address) ||
                          user.bill_address
    end
  end
end

#add_payment_sources_to_walletObject Also known as: persist_user_credit_card



731
732
733
734
735
# File 'app/models/spree/order.rb', line 731

def add_payment_sources_to_wallet
  Spree::Config.
    add_payment_sources_to_wallet_class.new(self).
    add_to_wallet
end

#add_store_credit_paymentsObject



622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
# File 'app/models/spree/order.rb', line 622

def add_store_credit_payments
  return if user.nil?
  return if payments.store_credits.checkout.empty? && user.total_available_store_credit.zero?

  payments.store_credits.checkout.each(&:invalidate!)

  # this can happen when multiple payments are present, auto_capture is
  # turned off, and one of the payments fails when the user tries to
  # complete the order, which sends the order back to the 'payment' state.
  authorized_total = payments.pending.sum(:amount)

  remaining_total = outstanding_balance - authorized_total

  if user.store_credits.any?
    payment_method = Spree::PaymentMethod::StoreCredit.first

    user.store_credits.order_by_priority.each do |credit|
      break if remaining_total.zero?
      next if credit.amount_remaining.zero?

      amount_to_take = [credit.amount_remaining, remaining_total].min
      payments.create!(source: credit,
                       payment_method: payment_method,
                       amount: amount_to_take,
                       state: 'checkout',
                       response_code: credit.generate_authorization_code)
      remaining_total -= amount_to_take
    end
  end

  other_payments = payments.checkout.not_store_credits
  if remaining_total.zero?
    other_payments.each(&:invalidate!)
  elsif other_payments.size == 1
    other_payments.first.update_attributes!(amount: remaining_total)
  end

  payments.reset

  if payments.where(state: %w(checkout pending)).sum(:amount) != total
    errors.add(:base, Spree.t("store_credit.errors.unable_to_fund")) && (return false)
  end
end

#all_inventory_units_returned?Boolean

Returns:

  • (Boolean)


281
282
283
# File 'app/models/spree/order.rb', line 281

def all_inventory_units_returned?
  inventory_units.all?(&:returned?)
end

#allow_cancel?Boolean

Returns:

  • (Boolean)


276
277
278
279
# File 'app/models/spree/order.rb', line 276

def allow_cancel?
  return false unless completed? && state != 'canceled'
  shipment_state.nil? || %w{ready backorder pending}.include?(shipment_state)
end

#amountObject

For compatiblity with Calculator::PriceSack



185
186
187
# File 'app/models/spree/order.rb', line 185

def amount
  line_items.map(&:amount).sum
end

#apply_free_shipping_promotionsObject



540
541
542
543
# File 'app/models/spree/order.rb', line 540

def apply_free_shipping_promotions
  Spree::PromotionHandler::FreeShipping.new(self).activate
  recalculate
end

#approved?Boolean

Returns:

  • (Boolean)


596
597
598
# File 'app/models/spree/order.rb', line 596

def approved?
  !!approved_at
end

#assign_billing_to_shipping_addressObject



271
272
273
274
# File 'app/models/spree/order.rb', line 271

def assign_billing_to_shipping_address
  self.ship_address = bill_address if bill_address
  true
end

#assign_default_user_addressesObject Also known as: assign_default_user_addresses!, assign_default_addresses!

Note:

This doesn't persist the change bill_address or ship_address

Assigns a default bill_address and ship_address to the order based on the associated user's bill_address and ship_address.



708
709
710
711
712
713
714
715
716
717
718
# File 'app/models/spree/order.rb', line 708

def assign_default_user_addresses
  if user
    bill_address = (user.bill_address || user.default_address)
    ship_address = (user.ship_address || user.default_address)
    # this is one of 2 places still using User#bill_address
    self.bill_address ||= bill_address if bill_address.try!(:valid?)
    # Skip setting ship address if order doesn't have a delivery checkout step
    # to avoid triggering validations on shipping address
    self.ship_address ||= ship_address if ship_address.try!(:valid?) && checkout_steps.include?("delivery")
  end
end

#associate_user!(user, override_email = true) ⇒ Object

Associates the specified user with the order.



298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'app/models/spree/order.rb', line 298

def associate_user!(user, override_email = true)
  self.user = user
  attrs_to_set = { user_id: user.try(:id) }
  attrs_to_set[:email] = user.try(:email) if override_email
  attrs_to_set[:created_by_id] = user.try(:id) if created_by.blank?

  if persisted?
    # immediately persist the changes we just made, but don't use save since we might have an invalid address associated
    self.class.unscoped.where(id: id).update_all(attrs_to_set)
  end

  assign_attributes(attrs_to_set)
end

#available_payment_methodsObject



449
450
451
452
453
454
455
# File 'app/models/spree/order.rb', line 449

def available_payment_methods
  @available_payment_methods ||= Spree::PaymentMethod
    .active
    .available_to_store(store)
    .available_to_users
    .order(:position)
end

#backordered?Boolean

Returns:

  • (Boolean)


233
234
235
# File 'app/models/spree/order.rb', line 233

def backordered?
  shipments.any?(&:backordered?)
end

#bill_address_attributes=(attributes) ⇒ Object



697
698
699
# File 'app/models/spree/order.rb', line 697

def bill_address_attributes=(attributes)
  self.bill_address = Spree::Address.immutable_merge(bill_address, attributes)
end

#can_add_coupon?Boolean

Returns:

  • (Boolean)


515
516
517
# File 'app/models/spree/order.rb', line 515

def can_add_coupon?
  Spree::Promotion.order_activatable?(self)
end

#can_approve?Boolean

Returns:

  • (Boolean)


600
601
602
# File 'app/models/spree/order.rb', line 600

def can_approve?
  !approved?
end

#can_ship?Boolean

Returns:

  • (Boolean)


397
398
399
# File 'app/models/spree/order.rb', line 397

def can_ship?
  complete? || resumed? || awaiting_return? || returned?
end

#canceled_by(user) ⇒ Object



586
587
588
589
590
591
592
593
594
# File 'app/models/spree/order.rb', line 586

def canceled_by(user)
  transaction do
    cancel!
    update_columns(
      canceler_id: user.id,
      canceled_at: Time.current
    )
  end
end

#cancellationsObject



293
294
295
# File 'app/models/spree/order.rb', line 293

def cancellations
  @cancellations ||= Spree::OrderCancellations.new(self)
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.

Returns:

  • (Boolean)


219
220
221
# File 'app/models/spree/order.rb', line 219

def checkout_allowed?
  line_items.count > 0
end

#completed?Boolean

Returns:

  • (Boolean)


211
212
213
# File 'app/models/spree/order.rb', line 211

def completed?
  completed_at.present?
end

#confirmation_required?Boolean

Returns:

  • (Boolean)


228
229
230
# File 'app/models/spree/order.rb', line 228

def confirmation_required?
  true
end

#contains?(variant, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


327
328
329
# File 'app/models/spree/order.rb', line 327

def contains?(variant, options = {})
  find_line_item_by_variant(variant, options).present?
end

#contentsObject



285
286
287
# File 'app/models/spree/order.rb', line 285

def contents
  @contents ||= Spree::OrderContents.new(self)
end

#covered_by_store_credit?Boolean Also known as: covered_by_store_credit

Returns:

  • (Boolean)


666
667
668
669
# File 'app/models/spree/order.rb', line 666

def covered_by_store_credit?
  return false unless user
  user.total_available_store_credit >= total
end

#create_proposed_shipmentsObject



529
530
531
532
533
534
535
536
537
538
# File 'app/models/spree/order.rb', line 529

def create_proposed_shipments
  if completed?
    raise CannotRebuildShipments.new(Spree.t(:cannot_rebuild_shipments_order_completed))
  elsif shipments.any? { |s| !s.pending? }
    raise CannotRebuildShipments.new(Spree.t(:cannot_rebuild_shipments_shipments_not_pending))
  else
    shipments.destroy_all
    self.shipments = Spree::Config.stock.coordinator_class.new(self).shipments
  end
end

#create_tax_charge!Object

Deprecated.

This now happens during #update!

Creates new tax charges if there are any applicable rates. If prices already include taxes then price adjustments are created instead.



363
364
365
# File 'app/models/spree/order.rb', line 363

def create_tax_charge!
  Spree::Config.tax_adjuster_class.new(self).adjust!
end

#credit_cardsObject



401
402
403
404
# File 'app/models/spree/order.rb', line 401

def credit_cards
  credit_card_ids = payments.from_credit_card.pluck(:source_id).uniq
  Spree::CreditCard.where(id: credit_card_ids)
end

#currencyObject



199
200
201
# File 'app/models/spree/order.rb', line 199

def currency
  self[:currency] || Spree::Config[:currency]
end

#deliver_order_confirmation_emailObject



439
440
441
442
# File 'app/models/spree/order.rb', line 439

def deliver_order_confirmation_email
  Spree::OrderMailer.confirm_email(self).deliver_later
  update_column(:confirmation_delivered, true)
end

#discounted_item_amountObject

Sum of all line item amounts after promotions, before added tax



195
196
197
# File 'app/models/spree/order.rb', line 195

def discounted_item_amount
  line_items.to_a.sum(&:discounted_amount)
end

#display_store_credit_remaining_after_captureObject



693
694
695
# File 'app/models/spree/order.rb', line 693

def display_store_credit_remaining_after_capture
  Spree::Money.new(total_available_store_credit - total_applicable_store_credit, { currency: currency })
end

#display_total_applicable_store_creditObject



689
690
691
# File 'app/models/spree/order.rb', line 689

def display_total_applicable_store_credit
  Spree::Money.new(-total_applicable_store_credit, { currency: currency })
end

#empty!Object



478
479
480
481
482
483
484
485
# File 'app/models/spree/order.rb', line 478

def empty!
  line_items.destroy_all
  adjustments.destroy_all
  shipments.destroy_all
  order_promotions.destroy_all

  recalculate
end

#ensure_line_item_variants_are_not_deletedObject

Check to see if any line item variants are soft, deleted. If so add error and restart checkout.



464
465
466
467
468
469
470
471
472
# File 'app/models/spree/order.rb', line 464

def ensure_line_item_variants_are_not_deleted
  if line_items.any? { |li| li.variant.paranoia_destroyed? }
    errors.add(:base, Spree.t(:deleted_variants_present))
    restart_checkout_flow
    false
  else
    true
  end
end

#ensure_shipping_addressObject



523
524
525
526
527
# File 'app/models/spree/order.rb', line 523

def ensure_shipping_address
  unless ship_address && ship_address.valid?
    errors.add(:base, Spree.t(:ship_address_required)) && (return false)
  end
end

#ensure_updated_shipmentsObject

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



550
551
552
553
554
555
556
# File 'app/models/spree/order.rb', line 550

def ensure_updated_shipments
  if !completed? && shipments.all?(&:pending?)
    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



413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# File 'app/models/spree/order.rb', line 413

def finalize!
  # lock all adjustments (coupon promotions, etc.)
  all_adjustments.each(&:finalize!)

  # update payment and shipment(s) states, and save
  updater.update_payment_state
  shipments.each do |shipment|
    shipment.update_state
    shipment.finalize!
  end

  updater.update_shipment_state
  save!
  updater.run_hooks

  touch :completed_at

  deliver_order_confirmation_email unless confirmation_delivered?
end

#find_line_item_by_variant(variant, options = {}) ⇒ Object



336
337
338
339
340
341
# File 'app/models/spree/order.rb', line 336

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



433
434
435
436
437
# File 'app/models/spree/order.rb', line 433

def fulfill!
  shipments.each { |shipment| shipment.update_state if shipment.persisted? }
  updater.update_shipment_state
  save!
end

#generate_order_number(options = nil) ⇒ Object



312
313
314
315
316
317
318
319
320
321
# File 'app/models/spree/order.rb', line 312

def generate_order_number(options = nil)
  if options
    Spree::Deprecation.warn \
      "Passing options to Order#generate_order_number is deprecated. " \
      "Please add your own instance of the order number generator " \
      "with your options (#{options.inspect}) and store it as " \
      "Spree::Config.order_number_generator in your stores config."
  end
  self.number ||= Spree::Config.order_number_generator.generate
end

Returns:

  • (Boolean)


608
609
610
611
# File 'app/models/spree/order.rb', line 608

def has_non_reimbursement_related_refunds?
  refunds.non_reimbursement.exists? ||
    payments.offset_payment.exists? # how old versions of spree stored refunds
end

#insufficient_stock_linesObject



457
458
459
# File 'app/models/spree/order.rb', line 457

def insufficient_stock_lines
  line_items.select(&:insufficient_stock?)
end

#is_risky?Boolean

Returns:

  • (Boolean)


582
583
584
# File 'app/models/spree/order.rb', line 582

def is_risky?
  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



352
353
354
355
356
357
358
# File 'app/models/spree/order.rb', line 352

def line_item_options_match(line_item, options)
  return true unless options

  line_item_comparison_hooks.all? { |hook|
    send(hook, line_item, options)
  }
end

#merge!(*args) ⇒ Object



474
475
476
# File 'app/models/spree/order.rb', line 474

def merge!(*args)
  Spree::Config.order_merger_class.new(self).merge!(*args)
end

#nameObject



391
392
393
394
395
# File 'app/models/spree/order.rb', line 391

def name
  if (address = bill_address || ship_address)
    "#{address.firstname} #{address.lastname}"
  end
end

#order_total_after_store_creditObject



677
678
679
# File 'app/models/spree/order.rb', line 677

def order_total_after_store_credit
  total - total_applicable_store_credit
end

#outstanding_balanceObject



372
373
374
375
376
377
378
379
380
381
# File 'app/models/spree/order.rb', line 372

def outstanding_balance
  # If reimbursement has happened add it back to total to prevent balance_due payment state
  # See: https://github.com/spree/spree/issues/6229

  if state == 'canceled'
    -1 * payment_total
  else
    total - reimbursement_total - payment_total
  end
end

#outstanding_balance?Boolean

Returns:

  • (Boolean)


383
384
385
# File 'app/models/spree/order.rb', line 383

def outstanding_balance?
  outstanding_balance != 0
end

#paid?Boolean

Helper methods for checkout steps

Returns:

  • (Boolean)


445
446
447
# File 'app/models/spree/order.rb', line 445

def paid?
  %w(paid credit_owed).include?(payment_state)
end

#payment_required?Boolean

Is this a free order in which case the payment step should be skipped

Returns:

  • (Boolean)


224
225
226
# File 'app/models/spree/order.rb', line 224

def payment_required?
  total > 0
end

#persist_user_address!Object



725
726
727
728
729
# File 'app/models/spree/order.rb', line 725

def persist_user_address!
  if !temporary_address && user && user.respond_to?(:persist_order_address) && bill_address_id
    user.persist_order_address(self)
  end
end

#pre_tax_item_amountObject

Sum of all line item amounts pre-tax



190
191
192
# File 'app/models/spree/order.rb', line 190

def pre_tax_item_amount
  line_items.to_a.sum(&:pre_tax_amount)
end

#quantityObject



604
605
606
# File 'app/models/spree/order.rb', line 604

def quantity
  line_items.sum(:quantity)
end

#quantity_of(variant, options = {}) ⇒ Object



331
332
333
334
# File 'app/models/spree/order.rb', line 331

def quantity_of(variant, options = {})
  line_item = find_line_item_by_variant(variant, options)
  line_item ? line_item.quantity : 0
end

#recalculateObject



258
259
260
# File 'app/models/spree/order.rb', line 258

def recalculate
  updater.update
end

#record_ip_address(ip_address) ⇒ Object



755
756
757
758
759
# File 'app/models/spree/order.rb', line 755

def record_ip_address(ip_address)
  if last_ip_address != ip_address
    update_attributes!(last_ip_address: ip_address)
  end
end

#refresh_shipment_ratesObject



568
569
570
# File 'app/models/spree/order.rb', line 568

def refresh_shipment_rates
  shipments.map(&:refresh_rates)
end

#refund_totalObject



387
388
389
# File 'app/models/spree/order.rb', line 387

def refund_total
  payments.flat_map(&:refunds).sum(&:amount)
end

#reimbursement_totalObject



368
369
370
# File 'app/models/spree/order.rb', line 368

def reimbursement_total
  reimbursements.sum(:total)
end

#restart_checkout_flowObject



558
559
560
561
562
563
564
565
566
# File 'app/models/spree/order.rb', line 558

def restart_checkout_flow
  return if state == 'cart'

  update_columns(
    state: 'cart',
    updated_at: Time.current
  )
  next! if line_items.size > 0
end

#set_shipments_costObject



576
577
578
579
# File 'app/models/spree/order.rb', line 576

def set_shipments_cost
  shipments.each(&:update_amounts)
  recalculate
end

#ship_address_attributes=(attributes) ⇒ Object



701
702
703
# File 'app/models/spree/order.rb', line 701

def ship_address_attributes=(attributes)
  self.ship_address = Spree::Address.immutable_merge(ship_address, attributes)
end

#shipped?Boolean

Returns:

  • (Boolean)


519
520
521
# File 'app/models/spree/order.rb', line 519

def shipped?
  %w(partial shipped).include?(shipment_state)
end

#shipped_shipmentsObject



323
324
325
# File 'app/models/spree/order.rb', line 323

def shipped_shipments
  shipments.shipped
end

#shippingObject



289
290
291
# File 'app/models/spree/order.rb', line 289

def shipping
  @shipping ||= Spree::OrderShipping.new(self)
end

#shipping_discountObject



203
204
205
# File 'app/models/spree/order.rb', line 203

def shipping_discount
  shipment_adjustments.eligible.sum(:amount) * - 1
end

#shipping_eq_billing_address?Boolean

Returns:

  • (Boolean)


572
573
574
# File 'app/models/spree/order.rb', line 572

def shipping_eq_billing_address?
  bill_address == ship_address
end

#state_changed(name) ⇒ Object



490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# File 'app/models/spree/order.rb', line 490

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_addressObject

Returns the address for taxation based on configuration



246
247
248
249
250
251
252
# File 'app/models/spree/order.rb', line 246

def tax_address
  if Spree::Config[:tax_using_ship_address]
    ship_address
  else
    bill_address
  end || store.default_cart_tax_location
end

#tax_totalObject



618
619
620
# File 'app/models/spree/order.rb', line 618

def tax_total
  additional_tax_total + included_tax_total
end

#tax_zoneObject

Returns the relevant zone (if any) to be used for taxation purposes. Uses default tax zone unless there is a specific match



239
240
241
# File 'app/models/spree/order.rb', line 239

def tax_zone
  Spree::Zone.match(tax_address) || Spree::Zone.default_tax
end

#to_paramObject



207
208
209
# File 'app/models/spree/order.rb', line 207

def to_param
  number
end

#tokenObject



613
614
615
616
# File 'app/models/spree/order.rb', line 613

def token
  Spree::Deprecation.warn("Spree::Order#token is DEPRECATED, please use #guest_token instead.", caller)
  guest_token
end

#total_applicable_store_creditObject



681
682
683
684
685
686
687
# File 'app/models/spree/order.rb', line 681

def total_applicable_store_credit
  if can_complete? || complete?
    payments.store_credits.valid.sum(:amount)
  else
    [total, (user.try(:total_available_store_credit) || 0.0)].min
  end
end

#total_available_store_creditObject



672
673
674
675
# File 'app/models/spree/order.rb', line 672

def total_available_store_credit
  return 0.0 unless user
  user.total_available_store_credit
end

#update!(*args) ⇒ Object



262
263
264
265
266
267
268
269
# File 'app/models/spree/order.rb', line 262

def update!(*args)
  if args.empty?
    Spree::Deprecation.warn "Calling order.update! with no arguments as a way to invoke the OrderUpdater is deprecated, since it conflicts with AR::Base#update! Please use order.recalculate instead"
    recalculate
  else
    super
  end
end

#updaterObject



254
255
256
# File 'app/models/spree/order.rb', line 254

def updater
  @updater ||= Spree::OrderUpdater.new(self)
end

#valid_credit_cardsObject



406
407
408
409
# File 'app/models/spree/order.rb', line 406

def valid_credit_cards
  credit_card_ids = payments.from_credit_card.valid.pluck(:source_id).uniq
  Spree::CreditCard.where(id: credit_card_ids)
end