Class: Account

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

Overview

Account model

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_tagsObject



57
58
59
# File 'app/models/account.rb', line 57

def self.default_tags
  ['invoice:debit', 'invoice:earnings', 'invoice:credit', 'invoice:costs', 'vat:credit', 'vat:debit']
end

.overview(value_range = Date.today, format = :default) ⇒ Object

Helpers



86
87
88
# File 'app/models/account.rb', line 86

def self.overview(value_range = Date.today, format = :default)
  Account.all.map { |a| a.to_s(value_range, format) }
end

.tag_collectionObject



61
62
63
# File 'app/models/account.rb', line 61

def self.tag_collection
  (default_tags + Account.tag_counts.pluck(:name)).uniq
end

Instance Method Details

#asset_account?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'app/models/account.rb', line 31

def asset_account?
  Account.by_type(%w(current_assets capital_assets costs)).exists?(self)
end

#balance_account?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'app/models/account.rb', line 39

def balance_account?
  Account.by_type(%w(current_assets capital_assets outside_capital equity_capital)).exists?(self)
end

#bookingsObject



75
76
77
# File 'app/models/account.rb', line 75

def bookings
  Booking.(id)
end

#liability_account?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'app/models/account.rb', line 35

def liability_account?
  !asset_account?
end

#profit_account?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'app/models/account.rb', line 43

def profit_account?
  !balance_account?
end

#saldo(selector = Date.today, inclusive = true) ⇒ Object



137
138
139
140
141
142
143
# File 'app/models/account.rb', line 137

def saldo(selector = Date.today, inclusive = true)
  credit_amount, debit_amount = turnover(selector, inclusive)

  amount = debit_amount - credit_amount

  asset_account? ? amount : -amount
end

#to_s(_format = :default) ⇒ Object

String



16
17
18
# File 'app/models/account.rb', line 16

def to_s(_format = :default)
  '%s (%s)' % [title, code]
end

#turnover(selector = Date.today, inclusive = true) ⇒ Object

Calculations



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'app/models/account.rb', line 91

def turnover(selector = Date.today, inclusive = true)
  equality = '=' if inclusive

  if selector.respond_to?(:first) && selector.respond_to?(:last)
    if selector.first.is_a? Booking
      if selector.first.value_date == selector.last.value_date
        condition = ["date(value_date) = :value_date AND id >#{equality} :first_id AND id <#{equality} :last_id", {
          value_date: selector.first.value_date,
          first_id: selector.first.id,
          last_id: selector.last.id
        }]
      else
        condition = ["(value_date > :first_value_date AND value_date < :latest_value_date) OR (date(value_date) = :first_value_date AND id >#{equality} :first_id) OR (date(value_date) = :latest_value_date AND id <#{equality} :last_id)", {
          first_value_date: selector.first.value_date,
          latest_value_date: selector.last.value_date,
          first_id: selector.first.id,
          last_id: selector.last.id
        }]
      end
    elsif
      if selector.first == selector.last
        condition = ['date(value_date) = :value_date', {
          value_date: selector.first
        }]
      else
        condition = ['date(value_date) BETWEEN :first_value_date AND :latest_value_date', {
          first_value_date: selector.first,
          latest_value_date: selector.last
        }]
      end
    end
  else
    if selector.is_a? Booking
      # date(value_date) is needed on sqlite!
      condition = ["(value_date < :value_date) OR (date(value_date) = :value_date AND id <#{equality} :id)", { value_date: selector.value_date, id: selector.id }]
    else
      condition = ["date(value_date) <#{equality} ?", selector]
    end
  end

  credit_amount = credit_bookings.where(condition).sum(:amount)
  debit_amount = debit_bookings.where(condition).sum(:amount)

  [credit_amount || 0.0, debit_amount || 0.0]
end

#unbalanced_referencesObject

Balances grouped by references which have not 0



80
81
82
# File 'app/models/account.rb', line 80

def unbalanced_references
  bookings.unbalanced_by_grouped_reference(id)
end