Module: MercuryBanking::CLI::BalanceSheetHelper

Included in:
Financials::FinancialsCommand
Defined in:
lib/mercury_banking/cli/financials.rb

Overview

Module for balance check related functionality

Instance Method Summary collapse

Instance Method Details

#display_balance_cross_check(mercury_balances, ledger_balances, verbose) ⇒ Object

Display cross-check between Mercury and ledger balances



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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/mercury_banking/cli/financials.rb', line 30

def display_balance_cross_check(mercury_balances, ledger_balances, verbose)
  puts "\n=== Balance Sheet Cross-Check ==="
  puts "#{'Mercury Account'.ljust(30)}#{'Mercury Balance'.ljust(15)}#{'Ledger Balance'.ljust(15)}Difference"
  puts "-" * 75

  total_diff = 0
  
  if verbose
    puts "\nMatching accounts:"
    puts "Mercury accounts: #{mercury_balances.keys.join(', ')}"
    puts "Ledger accounts: #{ledger_balances.keys.join(', ')}"
  end
  
  mercury_balances.each do |, mercury_balance|
    # Find the corresponding ledger account 
     = nil
    ledger_balance = 0
    
    # Try different matching strategies
    if ledger_balances.key?("Assets:Mercury:#{.split.first}")
      # Direct match with first word (e.g., "Checking" or "Savings")
       = "Assets:Mercury:#{.split.first}"
      ledger_balance = ledger_balances[]
    elsif .include?("Checking") && ledger_balances.keys.any? { |k| k.include?("Checking") }
      # Match by account type
       = ledger_balances.keys.find { |k| k.include?("Checking") }
      ledger_balance = ledger_balances[]
    elsif .include?("Savings") && ledger_balances.keys.any? { |k| k.include?("Savings") }
      # Match by account type
       = ledger_balances.keys.find { |k| k.include?("Savings") }
      ledger_balance = ledger_balances[]
    end
    
    # Debug information
    if verbose
      puts "\nMatching for Mercury account '#{}':"
      puts "  Found match: #{ || 'None'}"
      puts "  Mercury balance: $#{format('%.2f', mercury_balance)}"
      puts "  Ledger balance: $#{format('%.2f', ledger_balance)}"
    end
    
    # Skip zero-balance accounts unless in verbose mode
    if mercury_balance == 0 && ledger_balance == 0
      next unless verbose
    end

    # Calculate difference
    diff = mercury_balance - ledger_balance
    total_diff += diff.abs

    # Format for display
    mercury_balance_str = format("$%.2f", mercury_balance)
    ledger_balance_str = format("$%.2f", ledger_balance)
    diff_str = format("$%.2f", diff)

    # Add warning marker for differences
    diff_marker = diff.abs > 0.01 ? " ⚠️" : ""

    puts .ljust(30) + mercury_balance_str.ljust(15) + ledger_balance_str.ljust(15) + diff_str + diff_marker
  end

  puts "-" * 75
  puts "Total Discrepancy: #{format('$%.2f', total_diff)}"

  if total_diff > 0.01
    puts "\n⚠️ Warning: There are discrepancies between Mercury account balances and the ledger balance sheet."
    puts "This could be due to:"
    puts "  - Transactions not yet recorded in the ledger"
    puts "  - Incorrect categorization of transactions"
    puts "  - Timing differences between when transactions were recorded"
  else
    puts "\n✓ Balance sheet matches Mercury account balances."
  end
end

#get_mercury_balances(accounts) ⇒ Object

Get current balances for all Mercury accounts



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/mercury_banking/cli/financials.rb', line 11

def get_mercury_balances(accounts)
  mercury_balances = {}
  accounts.each do ||
    # Use the simplified account name for easier matching with ledger accounts
     = ['name'].gsub(/•+\d+/, '').strip
    mercury_balances[] = ['currentBalance'].to_f
  end
  
  if options[:verbose]
    puts "\nMercury account balances:"
    mercury_balances.each do |name, balance|
      puts "  #{name}: $#{format('%.2f', balance)}"
    end
  end
  
  mercury_balances
end