Class: Batch

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/bookkeeper/batch.rb

Instance Method Summary collapse

Instance Method Details

#pay(payment_transaction) ⇒ Object



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)
  # Get all of the bills associated with this batch
  bills = journals

  # What kind of fee have we paid as part of this payment?
  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..credit(fee)
    fee_journal.postings << Account..debit(fee)

    fee_journal.save!
  end
  
  # How much cash have we paid as part of this payment?
  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)
      # Record a bill payment in the ledger
      logger.info "Recording a bill payment for bill #{bill.id}"
    
      journal = Journal::Disbursement.new(:transactable => payment_transaction)

      journal.postings << payment_transaction..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