Module: Spree::Order::StoreCredit

Included in:
Spree::Order
Defined in:
app/models/spree/order/store_credit.rb

Instance Method Summary collapse

Instance Method Details

#add_store_credit_payments(amount = nil) ⇒ Object



4
5
6
# File 'app/models/spree/order/store_credit.rb', line 4

def add_store_credit_payments(amount = nil)
  Spree.checkout_add_store_credit_service.call(order: self, amount: amount)
end

#available_store_creditsArray<Spree::StoreCredit>

Returns the available store credits for the user associated with the order.



30
31
32
33
34
# File 'app/models/spree/order/store_credit.rb', line 30

def available_store_credits
  return Spree::StoreCredit.none if user.nil?

  user.store_credits.for_store(store).where(currency: currency).available.sort_by(&:amount_remaining).reverse
end

#could_use_store_credit?Boolean



36
37
38
39
40
# File 'app/models/spree/order/store_credit.rb', line 36

def could_use_store_credit?
  return false if store.payment_methods.store_credit.available.empty?

  total_available_store_credit > 0
end

#covered_by_store_credit?Boolean Also known as: covered_by_store_credit



12
13
14
# File 'app/models/spree/order/store_credit.rb', line 12

def covered_by_store_credit?
  user.present? && total_applied_store_credit.positive? && total_applied_store_credit >= total
end

#display_order_total_after_store_creditObject



93
94
95
# File 'app/models/spree/order/store_credit.rb', line 93

def display_order_total_after_store_credit
  Spree::Money.new(order_total_after_store_credit, currency: currency)
end

#display_store_credit_remaining_after_captureObject



101
102
103
# File 'app/models/spree/order/store_credit.rb', line 101

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



85
86
87
# File 'app/models/spree/order/store_credit.rb', line 85

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

#display_total_applied_store_creditObject



89
90
91
# File 'app/models/spree/order/store_credit.rb', line 89

def display_total_applied_store_credit
  Spree::Money.new(-total_applied_store_credit, currency: currency)
end

#display_total_available_store_creditObject



97
98
99
# File 'app/models/spree/order/store_credit.rb', line 97

def display_total_available_store_credit
  Spree::Money.new(total_available_store_credit, currency: currency)
end

#display_total_minus_store_creditsObject



105
106
107
# File 'app/models/spree/order/store_credit.rb', line 105

def display_total_minus_store_credits
  Spree::Money.new(total_minus_store_credits, currency: currency)
end

#order_total_after_store_creditBigDecimal

Returns the total amount of the order minus the total amount of store credits applied to the order.



45
46
47
# File 'app/models/spree/order/store_credit.rb', line 45

def order_total_after_store_credit
  total - total_applicable_store_credit
end

#remove_store_credit_paymentsObject



8
9
10
# File 'app/models/spree/order/store_credit.rb', line 8

def remove_store_credit_payments
  Spree.checkout_remove_store_credit_service.call(order: self)
end

#total_applicable_store_creditObject



56
57
58
59
60
61
62
# File 'app/models/spree/order/store_credit.rb', line 56

def total_applicable_store_credit
  if payment? || confirm? || complete?
    total_applied_store_credit
  else
    [total, user.try(:total_available_store_credit) || 0.0].min
  end
end

#total_applied_store_creditBigDecimal

Returns the total amount of store credits applied to the order.



67
68
69
70
71
72
73
74
75
76
# File 'app/models/spree/order/store_credit.rb', line 67

def total_applied_store_credit
  if payments.loaded?
    payments.
      find_all(&:store_credit?).
      reject(&:has_invalid_state?).
      sum(&:amount) || BigDecimal::ZERO
  else
    payments.store_credits.valid.sum(:amount)
  end
end

#total_available_store_creditBigDecimal

Returns the total amount of store credits available to the user associated with the order. Returns only store credit for this store and same currency as order



21
22
23
24
25
# File 'app/models/spree/order/store_credit.rb', line 21

def total_available_store_credit
  return 0.0 unless user

  user.total_available_store_credit(currency, store)
end

#total_minus_store_creditsBigDecimal

Returns the total amount of the order minus the total amount of store credits applied to the order.



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

def total_minus_store_credits
  total - total_applied_store_credit
end

#using_store_credit?Boolean

Returns true if the order is using store credit.



81
82
83
# File 'app/models/spree/order/store_credit.rb', line 81

def using_store_credit?
  total_applied_store_credit > 0
end