Class: Keepr::Account
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Keepr::Account
- Defined in:
- lib/keepr/account.rb
Class Method Summary collapse
Instance Method Summary collapse
- #balance(date = nil) ⇒ Object
- #keepr_postings ⇒ Object
- #number_as_string ⇒ Object
- #profit_and_loss? ⇒ Boolean
- #to_s ⇒ Object
Class Method Details
.merged_with_sums(options = {}) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/keepr/account.rb', line 50 def self.merged_with_sums( = {}) accounts = with_sums().to_a # Sum up child accounts to parent position = 0 while (account = accounts[position]) if account.parent_id && account.sum_amount parent_account = accounts.find { |a| a.id == account.parent_id } raise RuntimeError unless parent_account parent_account.sum_amount ||= 0 parent_account.sum_amount += account.sum_amount accounts.delete_at(position) else position += 1 end end accounts end |
.with_sums(options = {}) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/keepr/account.rb', line 24 def self.with_sums( = {}) raise ArgumentError unless .is_a?(Hash) subquery = Keepr::Posting .select('SUM(keepr_postings.amount)') .joins(:keepr_journal) .where('keepr_postings.keepr_account_id = keepr_accounts.id') date = [:date] permanent_only = [:permanent_only] case date when nil when Date subquery = subquery.where('keepr_journals.date <= ?', date) when Range subquery = subquery.where(keepr_journals: { date: date.first..date.last }) else raise ArgumentError end subquery = subquery.where(keepr_journals: { permanent: true }) if permanent_only select "keepr_accounts.*, (#{subquery.to_sql}) AS sum_amount" end |
Instance Method Details
#balance(date = nil) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/keepr/account.rb', line 81 def balance(date = nil) scope = case date when nil keepr_postings when Date keepr_postings .joins(:keepr_journal) .where('keepr_journals.date <= ?', date) when Range keepr_postings .joins(:keepr_journal) .where(keepr_journals: { date: date.first..date.last }) else raise ArgumentError end scope.sum(:amount) end |
#keepr_postings ⇒ Object
75 76 77 78 79 |
# File 'lib/keepr/account.rb', line 75 def keepr_postings Keepr::Posting .joins(:keepr_account) .merge(subtree) end |
#number_as_string ⇒ Object
100 101 102 103 104 105 106 |
# File 'lib/keepr/account.rb', line 100 def number_as_string if number < 1000 format('%04d', number) else number.to_s end end |
#profit_and_loss? ⇒ Boolean
71 72 73 |
# File 'lib/keepr/account.rb', line 71 def profit_and_loss? revenue? || expense? end |
#to_s ⇒ Object
108 109 110 |
# File 'lib/keepr/account.rb', line 108 def to_s "#{number_as_string} (#{name})" end |