Module: DoubleEntry::BalanceCalculator
- Extended by:
- BalanceCalculator
- Included in:
- BalanceCalculator
- Defined in:
- lib/double_entry/balance_calculator.rb
Defined Under Namespace
Classes: Options, RelationBuilder
Instance Method Summary collapse
-
#calculate(account, args = {}) ⇒ Money
Get the current or historic balance of an account.
Instance Method Details
#calculate(account, args = {}) ⇒ Money
Get the current or historic balance of an account.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/double_entry/balance_calculator.rb', line 16 def calculate(account, args = {}) = Options.new(account, args) relations = RelationBuilder.new() lines = relations.build if .between? || .code? # from and to or code lookups have to be done via sum Money.new(lines.sum(:amount), account.currency) else # all other lookups can be performed with running balances result = lines. from(lines_table_name()). order('id DESC'). limit(1). pluck(:balance) result.empty? ? Money.zero(account.currency) : Money.new(result.first, account.currency) end end |