Module: Spree::Purchase::StoreCredits

Included in:
Cart, Order, OrderGroup
Defined in:
app/models/concerns/spree/purchase/store_credits.rb

Overview

Store-credit application surface shared by Spree::Cart and Spree::Order (carts reach user through the customer alias).

Instance Method Summary collapse

Instance Method Details

#add_store_credit_payments(amount = nil) ⇒ Object



6
7
8
# File 'app/models/concerns/spree/purchase/store_credits.rb', line 6

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

#amount_due ⇒ BigDecimal

Balance still to collect, never negative. Store credit already applied but not yet captured is taken off; captured credit is a completed payment, so the balance has counted it already.

Returns:

  • (BigDecimal)


76
77
78
# File 'app/models/concerns/spree/purchase/store_credits.rb', line 76

def amount_due
  [outstanding_balance - uncaptured_store_credit, 0].max
end

#available_store_credits ⇒ Array<Spree::StoreCredit>

Returns:



29
30
31
32
33
# File 'app/models/concerns/spree/purchase/store_credits.rb', line 29

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

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

#could_use_store_credit? ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
# File 'app/models/concerns/spree/purchase/store_credits.rb', line 35

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

Returns:

  • (Boolean)


14
15
16
# File 'app/models/concerns/spree/purchase/store_credits.rb', line 14

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

#display_order_total_after_store_credit ⇒ Object



105
106
107
# File 'app/models/concerns/spree/purchase/store_credits.rb', line 105

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

#display_store_credit_remaining_after_capture ⇒ Object



113
114
115
# File 'app/models/concerns/spree/purchase/store_credits.rb', line 113

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

#display_store_credit_total ⇒ Object



93
94
95
# File 'app/models/concerns/spree/purchase/store_credits.rb', line 93

def display_store_credit_total
  Spree::Money.new(-store_credit_total, currency: currency)
end

#display_total_applicable_store_credit ⇒ Object



97
98
99
# File 'app/models/concerns/spree/purchase/store_credits.rb', line 97

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

#display_total_applied_store_credit ⇒ Object



101
102
103
# File 'app/models/concerns/spree/purchase/store_credits.rb', line 101

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

#display_total_available_store_credit ⇒ Object



109
110
111
# File 'app/models/concerns/spree/purchase/store_credits.rb', line 109

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

#display_total_minus_store_credits ⇒ Object



117
118
119
# File 'app/models/concerns/spree/purchase/store_credits.rb', line 117

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

#order_total_after_store_credit ⇒ Object



41
42
43
# File 'app/models/concerns/spree/purchase/store_credits.rb', line 41

def order_total_after_store_credit
  total - total_applicable_store_credit
end

#remove_store_credit_payments ⇒ Object



10
11
12
# File 'app/models/concerns/spree/purchase/store_credits.rb', line 10

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

#store_credit_total ⇒ BigDecimal

What store credit paid, apart from gift cards. A gift card is redeemed as store credit, but it is reported on its own as gift_card_total, so a summary showing both rows deducts each amount once.

Returns:

  • (BigDecimal)


85
86
87
# File 'app/models/concerns/spree/purchase/store_credits.rb', line 85

def store_credit_total
  total_applied_store_credit - gift_card_total
end

#total_applicable_store_credit ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'app/models/concerns/spree/purchase/store_credits.rb', line 49

def total_applicable_store_credit
  # Once payments exist (or the record completed), report what was
  # actually applied; before that, what could be applied.
  if completed? || payments.valid.any?
    total_applied_store_credit
  else
    [total, customer.try(:total_available_store_credit) || 0.0].min
  end
end

#total_applied_store_credit ⇒ BigDecimal

Returns:

  • (BigDecimal)


60
61
62
63
64
65
66
67
68
69
# File 'app/models/concerns/spree/purchase/store_credits.rb', line 60

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

#total_available_store_credit ⇒ BigDecimal

Only store credit for this store and the record's currency.

Returns:

  • (BigDecimal)


22
23
24
25
26
# File 'app/models/concerns/spree/purchase/store_credits.rb', line 22

def total_available_store_credit
  return 0.0 unless customer

  customer.total_available_store_credit(currency, store)
end

#total_minus_store_credits ⇒ Object



45
46
47
# File 'app/models/concerns/spree/purchase/store_credits.rb', line 45

def total_minus_store_credits
  total - total_applied_store_credit
end

#using_store_credit? ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'app/models/concerns/spree/purchase/store_credits.rb', line 89

def using_store_credit?
  total_applied_store_credit > 0
end