Class: Invoice

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

Instance Method Summary collapse

Instance Method Details

#add_party_with_role_type(party, role_type) ⇒ Object



109
110
111
112
# File 'app/models/invoice.rb', line 109

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

#calculate_balanceObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/models/invoice.rb', line 59

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



114
115
116
# File 'app/models/invoice.rb', line 114

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



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/models/invoice.rb', line 42

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

Returns:

  • (Boolean)


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

def has_payments?(status)
  selected_payment_applications = self.get_payment_applications(status)
  !(selected_payment_applications.nil? or selected_payment_applications.empty?)
end

#payment_dueObject



77
78
79
# File 'app/models/invoice.rb', line 77

def payment_due
  self.items.all.sum(&:total_amount)
end

#total_paymentsObject



81
82
83
# File 'app/models/invoice.rb', line 81

def total_payments
  self.get_payment_applications(:successful).sum{|item| item.money.amount}
end

#transactionsObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/models/invoice.rb', line 85

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