1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-24 07:39:39 +02:00

Make balance editing easier (#976)

* Make balance editing easier

* Translations

* Fix money input option

* Fix balance sync logic

* Rework balance update flow
This commit is contained in:
Zach Gollwitzer 2024-07-12 13:47:39 -04:00 committed by GitHub
parent b002a41b35
commit 34e03c2d6a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 91 additions and 23 deletions

View file

@ -93,4 +93,18 @@ class Account < ApplicationRecord
rescue Money::ConversionError
TimeSeries.new([])
end
def update_balance!(balance)
valuation = entries.account_valuations.find_by(date: Date.current)
if valuation
valuation.update! amount: balance
else
entries.create! \
date: Date.current,
amount: balance,
currency: currency,
entryable: Account::Valuation.new
end
end
end

View file

@ -14,6 +14,11 @@ class Account::Balance::Syncer
Account::Balance.transaction do
upsert_balances!(daily_balances)
purge_stale_balances!
if daily_balances.any?
account.reload
account.update! balance: daily_balances.select { |db| db.currency == account.currency }.last&.balance
end
end
end
@ -53,16 +58,13 @@ class Account::Balance::Syncer
entries = account.entries.where("date >= ?", sync_start_date).to_a
prior_balance = find_prior_balance
daily_balances = (sync_start_date...Date.current).map do |date|
(sync_start_date..Date.current).map do |date|
current_balance = calculate_balance_for_date(date, entries:, prior_balance:)
prior_balance = current_balance
build_balance(date, current_balance)
end
# Last balance of series is always equal to account balance
daily_balances << build_balance(Date.current, account.balance)
end
def calculate_converted_balances(balances)