4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/bookkeeper/batch.rb', line 4
def pay(payment_transaction)
bills = journals
fee = BigDecimal(payment_transaction.notification.fee)
if fee > 0
logger.info "Recording the MassPay fee for MassPay subpayment #{payment_transaction.transaction_id}"
fee_journal = Journal::Disbursement.new(:transactable => payment_transaction)
fee_journal.postings << payment_transaction.payer_account.credit(fee)
fee_journal.postings << Account.paypal_fees_account.debit(fee)
fee_journal.save!
end
payment_dollars = BigDecimal(payment_transaction.notification.gross)
while bills.length > 0
bill = bills.shift
amount_to_pay = bill.amount - bill.amount_paid
logger.info "We have #{payment_dollars} left, and #{amount_to_pay} to pay for bill #{bill.id}, with #{bills.length} bills left to pay."
if(payment_dollars < amount_to_pay)
warn_mispayment(:under, bill.id, (amount_to_pay - payment_dollars))
amount_to_pay = payment_dollars
elsif(bills.length == 0 and payment_dollars > amount_to_pay)
warn_mispayment(:over, bill.id, (payment_dollars - amount_to_pay))
amount_to_pay = payment_dollars
end
if(amount_to_pay > 0)
logger.info "Recording a bill payment for bill #{bill.id}"
journal = Journal::Disbursement.new(:transactable => payment_transaction)
journal.postings << payment_transaction.payer_account.credit(amount_to_pay)
journal.postings << Account.accounts_payable.debit(amount_to_pay)
bill.payments << journal
bill.save!
payment_dollars -= amount_to_pay
end
end
end
|