Class: Payment
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Payment
- Includes:
- ExtensibleObjectHelper, MoneyModelHelper
- Defined in:
- app/models/payment.rb
Class Method Summary collapse
Instance Method Summary collapse
- #amount_allocated(force_reload = false) ⇒ Object
- #amount_unallocated(force_reload = false) ⇒ Object
-
#initialize(*args) ⇒ Payment
constructor
A new instance of Payment.
- #is_allocated?(force_reload = false) ⇒ Boolean
- #name ⇒ Object
- #validate_invoice_payments_not_greater_than_amount ⇒ Object
- #validate_on_update ⇒ Object
Methods included from MoneyModelHelper
Methods included from ExtensibleObjectHelper
Constructor Details
#initialize(*args) ⇒ Payment
Returns a new instance of Payment.
18 19 20 21 |
# File 'app/models/payment.rb', line 18 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
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'app/models/payment.rb', line 61 def self.find_with_totals( how_many = :all, = {} ) 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() ) end |
Instance Method Details
#amount_allocated(force_reload = false) ⇒ Object
29 30 31 32 33 34 35 |
# File 'app/models/payment.rb', line 29 def amount_allocated( force_reload = false ) Money.new( (attribute_present? :amount_allocated_in_cents and !force_reload) ? read_attribute(:amount_allocated_in_cents).to_i : ( InvoicePayment.sum(:amount_in_cents, :conditions => ['payment_id = ?', id]) || 0 ) ) end |
#amount_unallocated(force_reload = false) ⇒ Object
23 24 25 26 27 |
# File 'app/models/payment.rb', line 23 def amount_unallocated( force_reload = false ) (attribute_present? :amount_unallocated_in_cents and !force_reload) ? Money.new(read_attribute(:amount_unallocated_in_cents).to_i) : (amount - amount_allocated) end |
#is_allocated?(force_reload = false) ⇒ Boolean
37 38 39 40 41 |
# File 'app/models/payment.rb', line 37 def is_allocated?( force_reload = false ) (attribute_present? :is_allocated and !force_reload) ? (read_attribute(:is_allocated).to_i == 1) : amount_unallocated(true).zero? end |
#name ⇒ Object
57 58 59 |
# File 'app/models/payment.rb', line 57 def name '%.2f Payment from %s' % [ amount, client.company_name ] end |
#validate_invoice_payments_not_greater_than_amount ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'app/models/payment.rb', line 43 def validate_invoice_payments_not_greater_than_amount my_amount = self.amount assignment_amount = self.invoice_assignments.inject(Money.new(0)){|sum,ip| ip.amount+sum } # We use the funky :> /:< to differentiate between the case of a credit invoice and a (normal?) invoice errors.add :invoice_assignments, "exceeds payment amount" if assignment_amount.send( (my_amount >= 0) ? :> : :<, my_amount ) end |
#validate_on_update ⇒ Object
53 54 55 |
# File 'app/models/payment.rb', line 53 def validate_on_update errors.add_to_base "Payments can't be updated after creation" end |