2025-07-15 11:42:41 -04:00
|
|
|
class Balance::ReverseCalculator < Balance::BaseCalculator
|
2025-05-15 10:19:56 -04:00
|
|
|
def calculate
|
|
|
|
Rails.logger.tagged("Balance::ReverseCalculator") do
|
2025-07-15 11:42:41 -04:00
|
|
|
# Since it's a reverse sync, we're starting with the "end of day" balance components and
|
|
|
|
# calculating backwards to derive the "start of day" balance components.
|
|
|
|
end_cash_balance = derive_cash_balance_on_date_from_total(
|
|
|
|
total_balance: account.current_anchor_balance,
|
|
|
|
date: account.current_anchor_date
|
|
|
|
)
|
|
|
|
end_non_cash_balance = account.current_anchor_balance - end_cash_balance
|
2025-03-07 17:35:55 -05:00
|
|
|
|
2025-07-15 11:42:41 -04:00
|
|
|
# Calculates in reverse-chronological order (End of day -> Start of day)
|
|
|
|
account.current_anchor_date.downto(account.opening_anchor_date).map do |date|
|
|
|
|
if use_opening_anchor_for_date?(date)
|
|
|
|
end_cash_balance = derive_cash_balance_on_date_from_total(
|
|
|
|
total_balance: account.opening_anchor_balance,
|
|
|
|
date: date
|
|
|
|
)
|
|
|
|
end_non_cash_balance = account.opening_anchor_balance - end_cash_balance
|
2025-03-07 17:35:55 -05:00
|
|
|
|
2025-07-15 11:42:41 -04:00
|
|
|
start_cash_balance = end_cash_balance
|
|
|
|
start_non_cash_balance = end_non_cash_balance
|
2025-03-07 17:35:55 -05:00
|
|
|
|
2025-07-15 11:42:41 -04:00
|
|
|
build_balance(
|
|
|
|
date: date,
|
|
|
|
cash_balance: end_cash_balance,
|
|
|
|
non_cash_balance: end_non_cash_balance
|
|
|
|
)
|
2025-03-07 17:35:55 -05:00
|
|
|
else
|
2025-07-15 11:42:41 -04:00
|
|
|
start_cash_balance = derive_start_cash_balance(end_cash_balance: end_cash_balance, date: date)
|
|
|
|
start_non_cash_balance = derive_start_non_cash_balance(end_non_cash_balance: end_non_cash_balance, date: date)
|
2025-03-07 17:35:55 -05:00
|
|
|
|
2025-07-15 11:42:41 -04:00
|
|
|
# Even though we've just calculated "start" balances, we set today equal to end of day, then use those
|
|
|
|
# in our next iteration (slightly confusing, but just the nature of a "reverse" sync)
|
|
|
|
output_balance = build_balance(
|
|
|
|
date: date,
|
|
|
|
cash_balance: end_cash_balance,
|
|
|
|
non_cash_balance: end_non_cash_balance
|
|
|
|
)
|
|
|
|
|
|
|
|
end_cash_balance = start_cash_balance
|
|
|
|
end_non_cash_balance = start_non_cash_balance
|
2025-03-07 17:35:55 -05:00
|
|
|
|
2025-07-15 11:42:41 -04:00
|
|
|
output_balance
|
|
|
|
end
|
2025-03-07 17:35:55 -05:00
|
|
|
end
|
2025-07-15 11:42:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2025-03-07 17:35:55 -05:00
|
|
|
|
2025-07-15 11:42:41 -04:00
|
|
|
# Negative entries amount on an "asset" account means, "account value has increased"
|
|
|
|
# Negative entries amount on a "liability" account means, "account debt has decreased"
|
|
|
|
# Positive entries amount on an "asset" account means, "account value has decreased"
|
|
|
|
# Positive entries amount on a "liability" account means, "account debt has increased"
|
|
|
|
def signed_entry_flows(entries)
|
|
|
|
entry_flows = entries.sum(&:amount)
|
|
|
|
account.asset? ? entry_flows : -entry_flows
|
2025-03-07 17:35:55 -05:00
|
|
|
end
|
2025-05-15 10:19:56 -04:00
|
|
|
|
2025-07-15 11:42:41 -04:00
|
|
|
# Reverse syncs are a bit different than forward syncs because we do not allow "reconciliation" valuations
|
|
|
|
# to be used at all. This is primarily to keep the code and the UI easy to understand. For a more detailed
|
|
|
|
# explanation, see the test suite.
|
|
|
|
def use_opening_anchor_for_date?(date)
|
|
|
|
account.has_opening_anchor? && date == account.opening_anchor_date
|
2025-05-15 10:19:56 -04:00
|
|
|
end
|
|
|
|
|
2025-07-15 11:42:41 -04:00
|
|
|
# Alias method, for algorithmic clarity
|
|
|
|
# Derives cash balance, starting from the end-of-day, applying entries in reverse to get the start-of-day balance
|
|
|
|
def derive_start_cash_balance(end_cash_balance:, date:)
|
|
|
|
derive_cash_balance(end_cash_balance, date)
|
2025-05-15 10:19:56 -04:00
|
|
|
end
|
|
|
|
|
2025-07-15 11:42:41 -04:00
|
|
|
# Alias method, for algorithmic clarity
|
|
|
|
# Derives non-cash balance, starting from the end-of-day, applying entries in reverse to get the start-of-day balance
|
|
|
|
def derive_start_non_cash_balance(end_non_cash_balance:, date:)
|
|
|
|
derive_non_cash_balance(end_non_cash_balance, date, direction: :reverse)
|
2025-05-15 10:19:56 -04:00
|
|
|
end
|
2025-03-07 17:35:55 -05:00
|
|
|
end
|