mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-05 05:25:24 +02:00
Checkpoint
This commit is contained in:
parent
15f8d827b5
commit
b7acef1e7a
9 changed files with 56 additions and 23 deletions
|
@ -57,20 +57,20 @@ class Account < ApplicationRecord
|
|||
|
||||
class << self
|
||||
def create_and_sync(attributes)
|
||||
start_date = attributes.delete(:tracking_start_date) || 2.years.ago.to_date
|
||||
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 || account.balance
|
||||
|
||||
account.entries.build(
|
||||
name: Valuation::Name.new("opening_anchor", account.accountable_type).to_s,
|
||||
date: 2.years.ago.to_date,
|
||||
date: start_date,
|
||||
amount: initial_balance,
|
||||
currency: account.currency,
|
||||
entryable: Valuation.new(
|
||||
kind: "opening_anchor",
|
||||
balance: initial_balance,
|
||||
cash_balance: initial_balance,
|
||||
currency: account.currency
|
||||
cash_balance: initial_balance
|
||||
)
|
||||
)
|
||||
|
||||
|
|
|
@ -18,12 +18,16 @@ class Account::BalanceUpdater
|
|||
end
|
||||
|
||||
valuation_entry = account.entries.valuations.find_or_initialize_by(date: date) do |entry|
|
||||
entry.entryable = Valuation.new
|
||||
entry.entryable = Valuation.new(
|
||||
kind: "recon",
|
||||
balance: balance,
|
||||
cash_balance: balance
|
||||
)
|
||||
end
|
||||
|
||||
valuation_entry.amount = balance
|
||||
valuation_entry.currency = currency if currency.present?
|
||||
valuation_entry.name = valuation_name(valuation_entry.entryable, account)
|
||||
valuation_entry.name = valuation_name(valuation_entry, account)
|
||||
valuation_entry.notes = notes if notes.present?
|
||||
valuation_entry.save!
|
||||
end
|
||||
|
|
|
@ -11,6 +11,7 @@ class Valuation < ApplicationRecord
|
|||
# Each account can have at most 1 opening anchor and 1 current anchor. All valuations between these anchors should
|
||||
# be either "recon" or "snapshot". This ensures we can reliably construct the account balance history solely from Entries.
|
||||
validate :unique_anchor_per_account, if: -> { opening_anchor? || current_anchor? }
|
||||
validate :manual_accounts_cannot_have_current_anchor
|
||||
|
||||
private
|
||||
def unique_anchor_per_account
|
||||
|
@ -26,4 +27,12 @@ class Valuation < ApplicationRecord
|
|||
errors.add(:kind, "#{kind.humanize} already exists for this account")
|
||||
end
|
||||
end
|
||||
|
||||
def manual_accounts_cannot_have_current_anchor
|
||||
return unless entry&.account
|
||||
|
||||
if entry.account.unlinked? && current_anchor?
|
||||
errors.add(:kind, "Manual accounts cannot have a current anchor")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue