Class: Invoice
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Invoice
- Defined in:
- app/models/invoice.rb
Class Method Summary collapse
Instance Method Summary collapse
- #amount_billed ⇒ Object
- #amount_owed ⇒ Object
- #amount_paid ⇒ Object
- #close ⇒ Object
- #formatted_id ⇒ Object
- #open? ⇒ Boolean
- #paid? ⇒ Boolean
- #pay(amount, options = {}) ⇒ Object
- #pay_in_full(options = {}) ⇒ Object
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_billed ⇒ Object
34 35 36 |
# File 'app/models/invoice.rb', line 34 def amount_billed line_items.inject(0) {|amount, item| amount + item.amount} end |
#amount_owed ⇒ Object
42 43 44 |
# File 'app/models/invoice.rb', line 42 def amount_owed amount_billed - amount_paid end |
#amount_paid ⇒ Object
38 39 40 |
# File 'app/models/invoice.rb', line 38 def amount_paid invoice_payments(true).inject(0) {|amount, payment| amount + payment.amount} end |
#close ⇒ Object
22 23 24 |
# File 'app/models/invoice.rb', line 22 def close update_attribute(:closed, true) end |
#formatted_id ⇒ Object
59 60 61 |
# File 'app/models/invoice.rb', line 59 def formatted_id "%08i" % id end |
#open? ⇒ Boolean
26 27 28 |
# File 'app/models/invoice.rb', line 26 def open? !closed? end |
#paid? ⇒ 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, = {}) .merge!({:description => "Payment for Invoice ##{formatted_id}", :auxilliary_model => self, :account_from => buyer_account, :account_to => seller_account, :amount => amount}) InvoicePayment.create! end |
#pay_in_full(options = {}) ⇒ Object
46 47 48 |
# File 'app/models/invoice.rb', line 46 def pay_in_full( = {}) pay(amount_owed, ) end |