class BillTrap::Invoice

Public Class Methods

completed() click to toggle source
# File lib/billtrap/models.rb, line 52
def self.completed
  exclude(:sent => nil).all.select { |i| i.paid? }
end
current() click to toggle source
# File lib/billtrap/models.rb, line 76
def self.current
  last = Meta.find(:key => 'current_invoice')
  Invoice[last.value]
end
current=(id) click to toggle source
# File lib/billtrap/models.rb, line 70
def self.current= id
  last = Meta.find_or_create(:key => 'current_invoice')
  last.value = id
  last.save
end
get(inv) click to toggle source

Retrieve invoice If numeric, assumes it’s an ID If string, returns first match inv == name Returns nil otherwise / if no match was found

# File lib/billtrap/models.rb, line 60
def self.get inv
  if is_i? inv
    Invoice[inv]
  elsif inv.is_a? String
    Invoice.find(:name => inv)
  else
    nil
  end
end
open() click to toggle source
# File lib/billtrap/models.rb, line 48
def self.open
  all.select { |i| !i.paid? }
end

Public Instance Methods

currency() click to toggle source
# File lib/billtrap/models.rb, line 81
def currency
  if client_id
    client.currency
  else
    BillTrap::Config['currency']
  end
end
due_date() click to toggle source
# File lib/billtrap/models.rb, line 131
 def due_date
  created + BillTrap::Config['due_date']
end
overdue?() click to toggle source
# File lib/billtrap/models.rb, line 127
def overdue?
  due_date > Date.today
end
paid?() click to toggle source
rate() click to toggle source
# File lib/billtrap/models.rb, line 112
def rate
  rate = if client.nil?
    BillTrap::Config['default_rate']
  else
    client.rate
  end
  Money.parse(rate, currency)
end
received_amount() click to toggle source
# File lib/billtrap/models.rb, line 122
def received_amount
  cents = Payment.where(:invoice_id => id).sum(:cents)
  Money.new(cents, currency)
end
set_attr(k,v) click to toggle source
# File lib/billtrap/models.rb, line 89
def set_attr k,v
  attributes[k] = v
  save
end
total() click to toggle source
# File lib/billtrap/models.rb, line 94
def total
  sum = Money.new(0, currency)
  entries = InvoiceEntry.filter(:invoice_id=>id)
  entries.each do |entry|
    sum += entry.total
  end
  return sum
end