Class: Stall::CreditUsageService
Defined Under Namespace
Classes: CartAlreadyPaidError
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of CreditUsageService.
7
8
9
10
|
# File 'app/services/stall/credit_usage_service.rb', line 7
def initialize(cart, params = {})
@cart = cart
@params = params
end
|
Instance Attribute Details
#cart ⇒ Object
Returns the value of attribute cart.
5
6
7
|
# File 'app/services/stall/credit_usage_service.rb', line 5
def cart
@cart
end
|
#params ⇒ Object
Returns the value of attribute params.
5
6
7
|
# File 'app/services/stall/credit_usage_service.rb', line 5
def params
@params
end
|
Instance Method Details
#amount ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'app/services/stall/credit_usage_service.rb', line 28
def amount
@amount ||= begin
amount = if params[:amount]
cents = BigDecimal.new(params[:amount]) * 100
Money.new(cents, cart.currency)
else
credit
end
[amount, cart.total_price].min
end
end
|
#available? ⇒ Boolean
FIXME: Ducktyping ShippingFeeCalculatorService and use this method in CartUpdateService#refresh_associated_services! to test if credit notes exists
62
63
64
|
# File 'app/services/stall/credit_usage_service.rb', line 62
def available?
cart.respond_to?(:adjustments)
end
|
#call ⇒ Object
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
# File 'app/services/stall/credit_usage_service.rb', line 12
def call
raise CartAlreadyPaidError, "Cannot udpate credit note from paid cart"
return false unless enough_credit?
clean_credit_note_adjustments!
available_credit_notes.reduce(amount) do |missing_amount, credit_note|
break true if missing_amount.to_d == 0
used_amount = [credit_note.remaining_amount, missing_amount].min
add_adjustment(used_amount, credit_note)
missing_amount - used_amount
end
end
|
#clean_credit_note_adjustments! ⇒ Object
52
53
54
55
56
57
|
# File 'app/services/stall/credit_usage_service.rb', line 52
def clean_credit_note_adjustments!
raise CartAlreadyPaidError, "Cannot remove credit note from paid cart"
credit_note_adjustments.each do |adjustment|
cart.adjustments.destroy(adjustment)
end
end
|
#credit ⇒ Object
41
42
43
44
45
46
|
# File 'app/services/stall/credit_usage_service.rb', line 41
def credit
@credit ||= begin
credit = cart.customer.try(:credit, cart.currency) || Money.new(0, cart.currency)
Money.new(credit + credit_note_adjustments.map(&:price).sum.abs, cart.currency)
end
end
|
#credit_used ⇒ Object
70
71
72
|
# File 'app/services/stall/credit_usage_service.rb', line 70
def credit_used
credit_note_adjustments.map(&:price).sum
end
|
#credit_used? ⇒ Boolean
66
67
68
|
# File 'app/services/stall/credit_usage_service.rb', line 66
def credit_used?
credit_note_adjustments.any?
end
|
#enough_credit? ⇒ Boolean
48
49
50
|
# File 'app/services/stall/credit_usage_service.rb', line 48
def enough_credit?
amount <= credit
end
|
#ensure_valid_or_remove! ⇒ Object
74
75
76
77
78
79
80
81
|
# File 'app/services/stall/credit_usage_service.rb', line 74
def ensure_valid_or_remove!
if !enough_credit?
clean_credit_note_adjustments!
elsif cart.total_price.to_d < 0
@amount = credit_used
call
end
end
|