Skip to content
This repository was archived by the owner on Jul 27, 2025. It is now read-only.

Commit e277303

Browse files
committed
Tweaks to initial calc logic
1 parent 58b5f96 commit e277303

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

app/models/account/anchorable.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ def current_anchor_balance
2929
current_balance_manager.current_balance
3030
end
3131

32+
def current_anchor_date
33+
current_balance_manager.current_date
34+
end
35+
3236

3337
private
3438
def opening_balance_manager

app/models/account/current_balance_manager.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ def current_balance
1818
end
1919
end
2020

21+
def current_date
22+
if current_anchor_valuation
23+
current_anchor_valuation.entry.date
24+
else
25+
Date.current
26+
end
27+
end
28+
2129
def set_current_balance(balance)
2230
# A current balance anchor implies there is an external data source that will keep it updated. Since manual accounts
2331
# are tracked by the user, a current balance anchor is not appropriate.

app/models/balance/forward_calculator.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def calculate
1414
private
1515
def calculate_balances
1616
# Derive initial balances from opening anchor
17-
opening_balance = opening_balance_manager.opening_balance
17+
opening_balance = account.opening_anchor_balance
1818

1919
if cash_only_account?
2020
current_cash_balance = opening_balance
@@ -23,7 +23,7 @@ def calculate_balances
2323
current_cash_balance = 0
2424
current_non_cash_balance = opening_balance
2525
else # mixed_account?
26-
opening_holdings_value = holdings_value_for_date(opening_balance_manager.opening_date)
26+
opening_holdings_value = holdings_value_for_date(account.opening_anchor_date)
2727
current_cash_balance = opening_balance - opening_holdings_value
2828
current_non_cash_balance = opening_holdings_value
2929
end
@@ -87,9 +87,6 @@ def sync_cache
8787
@sync_cache ||= Balance::SyncCache.new(account)
8888
end
8989

90-
def opening_balance_manager
91-
@opening_balance_manager ||= Account::OpeningBalanceManager.new(account)
92-
end
9390

9491
def holdings_value_for_date(date)
9592
holdings = sync_cache.get_holdings(date)

test/support/entries_test_helper.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ def create_reconciliation_valuation(account:, balance:, date:)
3333
)
3434
end
3535

36+
def create_current_anchor_valuation(account:, balance:, date: Date.current)
37+
create_valuation(
38+
account: account,
39+
kind: "current_anchor",
40+
amount: balance,
41+
date: date
42+
)
43+
end
44+
3645
def create_valuation(attributes = {})
3746
entry_attributes = attributes.except(:kind)
3847
valuation_attributes = attributes.slice(:kind)

0 commit comments

Comments
 (0)