1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-08-05 13:35:21 +02:00

Account balance anchors

This commit is contained in:
Zach Gollwitzer 2025-07-07 11:31:37 -04:00
parent 662f2c04ce
commit 15f8d827b5
8 changed files with 226 additions and 21 deletions

View file

@ -59,28 +59,22 @@ class Account < ApplicationRecord
def create_and_sync(attributes)
attributes[:accountable_attributes] ||= {} # Ensure accountable is created, even if empty
account = new(attributes.merge(cash_balance: attributes[:balance]))
initial_balance = attributes.dig(:accountable_attributes, :initial_balance)&.to_d || 0
initial_balance = attributes.dig(:accountable_attributes, :initial_balance)&.to_d || account.balance
transaction do
# Create 2 valuations for new accounts to establish a value history for users to see
account.entries.build(
name: "Current Balance",
date: Date.current,
amount: account.balance,
currency: account.currency,
entryable: Valuation.new
)
account.entries.build(
name: "Initial Balance",
date: 1.day.ago.to_date,
amount: initial_balance,
currency: account.currency,
entryable: Valuation.new
account.entries.build(
name: Valuation::Name.new("opening_anchor", account.accountable_type).to_s,
date: 2.years.ago.to_date,
amount: initial_balance,
currency: account.currency,
entryable: Valuation.new(
kind: "opening_anchor",
balance: initial_balance,
cash_balance: initial_balance,
currency: account.currency
)
)
account.save!
end
account.save!
account.sync_later
account
end