mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-06 14:05:20 +02:00
Add confirmation dialog for balance reconciliation creates and updates (#2457)
This commit is contained in:
parent
c1d98fe73b
commit
89cc64418e
13 changed files with 180 additions and 49 deletions
|
@ -115,8 +115,8 @@ class Account < ApplicationRecord
|
|||
end
|
||||
|
||||
|
||||
def update_balance(balance:, date: Date.current, currency: nil, notes: nil)
|
||||
Account::BalanceUpdater.new(self, balance:, currency:, date:, notes:).update
|
||||
def update_balance(balance:, date: Date.current, currency: nil, notes: nil, existing_valuation_id: nil)
|
||||
Account::BalanceUpdater.new(self, balance:, currency:, date:, notes:, existing_valuation_id:).update
|
||||
end
|
||||
|
||||
def start_date
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
class Account::BalanceUpdater
|
||||
def initialize(account, balance:, currency: nil, date: Date.current, notes: nil)
|
||||
def initialize(account, balance:, currency: nil, date: Date.current, notes: nil, existing_valuation_id: nil)
|
||||
@account = account
|
||||
@balance = balance.to_d
|
||||
@currency = currency
|
||||
@date = date.to_date
|
||||
@notes = notes
|
||||
@existing_valuation_id = existing_valuation_id
|
||||
end
|
||||
|
||||
def update
|
||||
|
@ -17,10 +18,15 @@ class Account::BalanceUpdater
|
|||
account.save!
|
||||
end
|
||||
|
||||
valuation_entry = account.entries.valuations.find_or_initialize_by(date: date) do |entry|
|
||||
entry.entryable = Valuation.new(kind: "reconciliation")
|
||||
valuation_entry = if existing_valuation_id
|
||||
account.entries.find(existing_valuation_id)
|
||||
else
|
||||
account.entries.valuations.find_or_initialize_by(date: date) do |entry|
|
||||
entry.entryable = Valuation.new(kind: "reconciliation")
|
||||
end
|
||||
end
|
||||
|
||||
valuation_entry.date = date
|
||||
valuation_entry.amount = balance
|
||||
valuation_entry.currency = currency if currency.present?
|
||||
valuation_entry.name = Valuation.build_reconciliation_name(account.accountable_type)
|
||||
|
@ -37,7 +43,7 @@ class Account::BalanceUpdater
|
|||
end
|
||||
|
||||
private
|
||||
attr_reader :account, :balance, :currency, :date, :notes
|
||||
attr_reader :account, :balance, :currency, :date, :notes, :existing_valuation_id
|
||||
|
||||
Result = Struct.new(:success?, :updated?, :error_message)
|
||||
|
||||
|
|
|
@ -28,4 +28,19 @@ class Investment < ApplicationRecord
|
|||
"line-chart"
|
||||
end
|
||||
end
|
||||
|
||||
def holdings_value_for_date(date)
|
||||
# Find the most recent holding for each security on or before the given date
|
||||
# Using a subquery to get the max date for each security
|
||||
account.holdings
|
||||
.where(currency: account.currency)
|
||||
.where("date <= ?", date)
|
||||
.where("(security_id, date) IN (
|
||||
SELECT security_id, MAX(date) as max_date
|
||||
FROM holdings
|
||||
WHERE account_id = ? AND date <= ?
|
||||
GROUP BY security_id
|
||||
)", account.id, date)
|
||||
.sum(:amount)
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue