Class: Invoice
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Invoice
- Defined in:
- app/models/invoice.rb
Overview
Table Definition ###########################
create_table :invoices do |t|
t.string :invoice_number
t.string :description
t.string :message
t.date :invoice_date
t.date :due_date
t.string :external_identifier
t.string :external_id_source
t.references :product
t.references :invoice_type
t.references :billing_account
t.references :invoice_payment_strategy_type
t.references :balance
t.references :calculate_balance_strategy_type
t.
end
Instance Method Summary collapse
- #add_party_with_role_type(party, role_type) ⇒ Object
- #balance ⇒ Object
- #balance=(amount, currency = Currency.usd) ⇒ Object
- #calculate_balance ⇒ Object
- #find_parties_by_role_type(role_type) ⇒ Object
- #get_payment_applications(status = :all) ⇒ Object
- #has_payments?(status) ⇒ Boolean
- #payment_due ⇒ Object
- #total_payments ⇒ Object
- #transactions ⇒ Object
Instance Method Details
#add_party_with_role_type(party, role_type) ⇒ Object
141 142 143 144 |
# File 'app/models/invoice.rb', line 141 def add_party_with_role_type(party, role_type) self.invoice_party_roles << InvoicePartyRole.create(:party => party, :role_type => convert_role_type(role_type)) self.save end |
#balance ⇒ Object
96 97 98 |
# File 'app/models/invoice.rb', line 96 def balance self.balance_record.amount end |
#balance=(amount, currency = Currency.usd) ⇒ Object
100 101 102 103 104 105 106 107 |
# File 'app/models/invoice.rb', line 100 def balance=(amount, currency=Currency.usd) if self.balance_record self.balance_record.amount = amount else self.balance_record = Money.create(:amount => amount, :currency => currency) end self.balance_record.save end |
#calculate_balance ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'app/models/invoice.rb', line 79 def calculate_balance unless self.calculate_balance_strategy_type.nil? case self.calculate_balance_strategy_type.internal_identifier when 'invoice_items_and_payments' (self.items.all.sum(&:total_amount) - self.total_payments) when 'payable_balances_and_payments' (self.payable_balances.all.sum(&:balance).amount - self.total_payments) when 'payments' (self.balance - self.total_payments) else self.balance end else self.balance end end |
#find_parties_by_role_type(role_type) ⇒ Object
146 147 148 |
# File 'app/models/invoice.rb', line 146 def find_parties_by_role_type(role_type) self.invoice_party_roles.where('role_type_id = ?', convert_role_type(role_type).id).all.collect(&:party) end |
#get_payment_applications(status = :all) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'app/models/invoice.rb', line 62 def get_payment_applications(status=:all) selected_payment_applications = case status.to_sym when :pending self.payment_applications.pending when :successful self.payment_applications.successful when :all self.payment_applications end unless self.items.empty? selected_payment_applications = (selected_payment_applications | self.items.collect{|item| item.get_payment_applications(status)}).flatten! unless (self.items.collect{|item| item.get_payment_applications(status)}.empty?) end selected_payment_applications end |
#has_payments?(status) ⇒ Boolean
57 58 59 60 |
# File 'app/models/invoice.rb', line 57 def has_payments?(status) selected_payment_applications = self.get_payment_applications(status) !(selected_payment_applications.nil? or selected_payment_applications.empty?) end |
#payment_due ⇒ Object
109 110 111 |
# File 'app/models/invoice.rb', line 109 def payment_due self.items.all.sum(&:total_amount) end |
#total_payments ⇒ Object
113 114 115 |
# File 'app/models/invoice.rb', line 113 def total_payments self.get_payment_applications(:successful).sum{|item| item.money.amount} end |
#transactions ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'app/models/invoice.rb', line 117 def transactions transactions = [] self.items.each do |item| transactions << { :date => item.created_at, :description => item.item_description, :quantity => item.quantity, :amount => item.amount } end self.get_payment_applications(:successful).each do |item| transactions << { :date => item.financial_txn.payments.last.created_at, :description => item.financial_txn.description, :quantity => 1, :amount => (0 - item.financial_txn.money.amount) } end transactions.sort_by{|item| [item[:date]]} end |