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_paymentsObject


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/models/spree/order/store_credit.rb', line 4

def add_store_credit_payments
  payments.store_credits.where(state: :checkout).map(&:invalidate!)

  remaining_total = outstanding_balance

  if user && user.store_credits.any?
    payment_method = Spree::PaymentMethod::StoreCredit.available.first
    raise 'Store credit payment method could not be found' unless payment_method

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

      amount_to_take = store_credit_amount(credit, remaining_total)
      create_store_credit_payment(payment_method, credit, amount_to_take)
      remaining_total -= amount_to_take
    end
    payments.store_credits.checkout
  end
end

#could_use_store_credit?Boolean


40
41
42
# File 'app/models/spree/order/store_credit.rb', line 40

def could_use_store_credit?
  total_available_store_credit > 0
end

#covered_by_store_credit?Boolean Also known as: covered_by_store_credit


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

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

#display_order_total_after_store_creditObject


72
73
74
# File 'app/models/spree/order/store_credit.rb', line 72

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

#display_store_credit_remaining_after_captureObject


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

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


64
65
66
# File 'app/models/spree/order/store_credit.rb', line 64

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

#display_total_applied_store_creditObject


68
69
70
# File 'app/models/spree/order/store_credit.rb', line 68

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

#display_total_available_store_creditObject


76
77
78
# File 'app/models/spree/order/store_credit.rb', line 76

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

#order_total_after_store_creditObject


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

def order_total_after_store_credit
  total - total_applicable_store_credit
end

#remove_store_credit_paymentsObject


25
26
27
# File 'app/models/spree/order/store_credit.rb', line 25

def remove_store_credit_payments
  payments.checkout.store_credits.map(&:invalidate!) unless completed?
end

#total_applicable_store_creditObject


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

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_creditObject


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

def total_applied_store_credit
  payments.store_credits.valid.sum(:amount)
end

#total_available_store_creditObject


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

def total_available_store_credit
  return 0.0 unless user
  user.total_available_store_credit
end

#using_store_credit?Boolean


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

def using_store_credit?
  total_applied_store_credit > 0
end