Class: RVGP::Validations::BalanceValidation

Inherits:
Base::JournalValidation show all
Defined in:
lib/rvgp/validations/balance_validation.rb

Overview

This validation asserts that the ledger-reported balance, matches a provided balance, on a given day. These balances, should be stipulated in a section of your reconciler, that looks like this: “‘ balances:

'2022-01-01': $ 105.63
'2022-09-01': $ 300.29
'2022-10-01': $ 400.33

“‘ These balances are expected to come from a bank statement, and this validation ensures that rvgp is matching the records of your financial institution

Instance Attribute Summary

Attributes inherited from Base::JournalValidation

#reconciler

Attributes inherited from Base::Validation

#errors, #warnings

Instance Method Summary collapse

Methods inherited from Base::JournalValidation

#initialize, #validate_no_balance, #validate_no_transactions

Methods inherited from Base::Validation

#error!, #initialize, #valid?, #warning!

Methods included from Pta::AvailabilityHelper

#hledger, #ledger, #pta

Constructor Details

This class inherits a constructor from RVGP::Base::JournalValidation

Instance Method Details

#validateObject

If there are no checkpoints in the ‘balances’ line of the reconciler, this fires a warning. If there are checkpoints, then, we scan the register to ensure that the balance of the reconciler.from, on the checkpoint date, matches the ledger/hledger balance, on that date. (and if it doesnt, fires an error)



22
23
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rvgp/validations/balance_validation.rb', line 22

def validate
  if reconciler.balances.nil? || reconciler.balances.empty?
    warning! 'No balance checkpoints found.'
  else
     = true
    cite_balances = reconciler.balances.map do |d, expected_balance_s|
      expected_balance = expected_balance_s.to_commodity

      balances_on_day = pta.balance format('^%s$', reconciler.from),
                                    depth: 1,
                                    end: d.to_s,
                                    file: RVGP.app.config.project_journal_path

      balances_found = balances_on_day.accounts.map(&:amounts).flatten.find_all do |amount|
        amount.code == expected_balance.code
      end

      found = if balances_found.empty?
                # Rather than operate from nil, we'll establish that we're '0' of units
                # of the expected symbol
                RVGP::Journal::Commodity.from_symbol_and_amount expected_balance.code, 0
              else
                balances_found.sum
              end

      found_as_s = if found
                     format('Found: %s', found.to_s)
                   else
                     found = RVGP::Journal::Commodity.from_symbol_and_amount expected_balance.code, 0
                     '(Nil)'
                   end

      is_valid = expected_balance == found
       = false unless is_valid

      format('(%<day>s) Expected: %<expected>s %<indicator>s',
             day: d.to_s,
             expected: expected_balance.to_s,
             indicator: RVGP.pastel.send(is_valid ? :green : :red, found_as_s))
    end

    error! 'Failed Checkpoint(s):', cite_balances unless 
  end
end