Class: Payment

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ExtensibleObjectHelper, MoneyModelHelper
Defined in:
app/models/payment.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MoneyModelHelper

append_features

Methods included from ExtensibleObjectHelper

append_features

Constructor Details

#initialize(*args) ⇒ Payment



19
20
21
22
# File 'app/models/payment.rb', line 19

def initialize(*args)
  super(*args)
  self.paid_on = Time.now.beginning_of_day if paid_on.nil?
end

Class Method Details

.find_with_totals(how_many = :all, options = {}) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/models/payment.rb', line 84

def self.find_with_totals( how_many = :all, options = {} )
  cast_amount_allocated = 'IF(payments_total.amount_allocated_in_cents IS NULL, 0, payments_total.amount_allocated_in_cents)'
  
  joins = 'LEFT JOIN ('+
    'SELECT payment_id, SUM(amount_in_cents) AS amount_allocated_in_cents FROM invoice_payments GROUP BY payment_id'+
  ') AS payments_total ON payments_total.payment_id = payments.id'
  
  Payment.find( 
    how_many,
    {
      :select => [
        'payments.id',
        'payments.amount_in_cents',
        "#{cast_amount_allocated} AS amount_allocated_in_cents",
        "payments.amount_in_cents - #{cast_amount_allocated} AS amount_unallocated_in_cents"
      ].join(', '),
      :order => 'paid_on ASC',
      :joins => joins,
      :group => 'payments.id'
    }.merge(options)
  )
end

Instance Method Details

#amount_allocatedObject



68
69
70
71
72
73
74
# File 'app/models/payment.rb', line 68

def amount_allocated
  Money.new(  
    (attribute_present? :amount_allocated_in_cents) ? 
      read_attribute(:amount_allocated_in_cents).to_i : 
      ( InvoicePayment.sum(:amount_in_cents, :conditions => ['payment_id = ?', id]) || 0 )
  )
end

#amount_unallocatedObject



62
63
64
65
66
# File 'app/models/payment.rb', line 62

def amount_unallocated
  (attribute_present? :amount_unallocated_in_cents) ? 
    Money.new(read_attribute(:amount_unallocated_in_cents).to_i) : 
    (amount - amount_allocated)
end

#create_invoice_paymentsObject



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
52
53
54
55
56
# File 'app/models/payment.rb', line 24

def create_invoice_payments    
  # NOTE: Orders by oldest outstanding date first:
  unpaid_invoices = Invoice.find_with_totals(
    :all, 
    :conditions => [
      [
      'client_id = ?',
      'IF(activities_total.total_in_cents IS NULL, 0,activities_total.total_in_cents) - '+
      'IF(invoices_total.total_in_cents IS NULL, 0,invoices_total.total_in_cents) > ?'
       ].join(' AND '),
      client_id, 
      0
    ]
  )

  current_client_balance = Money.new(0)
  unpaid_invoices.each { |inv| current_client_balance += inv.amount_outstanding }
  
  currently_unallocated = amount_unallocated

  unpaid_invoices.each do |unpaid_invoice|
    break if currently_unallocated <= 0 or current_client_balance <= 0

    payment_allocation = (currently_unallocated >= unpaid_invoice.amount_outstanding) ? 
      unpaid_invoice.amount_outstanding : 
      currently_unallocated
    
    InvoicePayment.create! :payment => self, :invoice => unpaid_invoice, :amount => payment_allocation

    current_client_balance -= payment_allocation
    currently_unallocated  -= payment_allocation
  end
end

#destroy_invoice_paymentsObject



58
59
60
# File 'app/models/payment.rb', line 58

def destroy_invoice_payments
  InvoicePayment.destroy_all ['payment_id = ?', id]
end

#nameObject



80
81
82
# File 'app/models/payment.rb', line 80

def name  
  '%.2f Payment from %s'  % [ amount, client.company_name ]
end

#validate_on_updateObject



76
77
78
# File 'app/models/payment.rb', line 76

def validate_on_update
  errors.add_to_base "Payments can't be updated after creation"
end