Class: Invoice

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/invoice.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(entries) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'app/models/invoice.rb', line 10

def self.build(entries)
  accounts = check_accounts entries
  invoice = Invoice.create! :buyer_account => accounts[:buyer],
                            :seller_account => accounts[:seller]
  entries.each do |entry|
    line = invoice.invoice_lines.create :line_item => entry
    line.save!
  end
  invoice.close
  invoice
end

Instance Method Details

#amount_billedObject



34
35
36
# File 'app/models/invoice.rb', line 34

def amount_billed
  line_items.inject(0) {|amount, item| amount + item.amount}
end

#amount_owedObject



42
43
44
# File 'app/models/invoice.rb', line 42

def amount_owed
  amount_billed - amount_paid
end

#amount_paidObject



38
39
40
# File 'app/models/invoice.rb', line 38

def amount_paid
  invoice_payments(true).inject(0) {|amount, payment| amount + payment.amount}
end

#closeObject



22
23
24
# File 'app/models/invoice.rb', line 22

def close
  update_attribute(:closed, true)
end

#formatted_idObject



59
60
61
# File 'app/models/invoice.rb', line 59

def formatted_id
  "%08i" % id
end

#open?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'app/models/invoice.rb', line 26

def open?
  !closed?
end

#paid?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'app/models/invoice.rb', line 30

def paid?
  amount_owed <= 0
end

#pay(amount, options = {}) ⇒ Object



50
51
52
53
54
55
56
57
# File 'app/models/invoice.rb', line 50

def pay(amount, options = {})
  options.merge!({:description => "Payment for Invoice ##{formatted_id}",
                  :auxilliary_model => self,
                  :account_from => ,
                  :account_to => ,
                  :amount => amount})
  InvoicePayment.create! options
end

#pay_in_full(options = {}) ⇒ Object



46
47
48
# File 'app/models/invoice.rb', line 46

def pay_in_full(options = {})
  pay(amount_owed, options)
end