def calculate(account, args = {})
      options = Options.new(account, args)
      relations = RelationBuilder.new(options)
      lines = relations.build

      if options.between? || options.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(options)).
                 order('id DESC').
                 limit(1).
                 pluck(:balance)
        result.empty? ? Money.zero(account.currency) : Money.new(result.first, account.currency)
      end
    end
def calculate(account, args = {})
      options = Options.new(account, args)
      relations = RelationBuilder.new(options)
      lines = relations.build

      if options.between? || options.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(options)).
                 order('id DESC').
                 limit(1).
                 pluck(:balance)
        result.empty? ? Money.zero(account.currency) : Money.new(result.first, account.currency)
      end
    end
Get the current or historic balance of an account.

 @param account [DoubleEntry::Account:Instance]
 @option args :from [Time]
 @option args :to [Time]
 @option args :at [Time]
 @option args :code [Symbol]
 @option args :codes [Array<Symbol>]
 @return [Money]