diff --git a/app/controllers/loans_controller.rb b/app/controllers/loans_controller.rb index b9968faf..961c5acf 100644 --- a/app/controllers/loans_controller.rb +++ b/app/controllers/loans_controller.rb @@ -2,6 +2,6 @@ class LoansController < ApplicationController include AccountableResource permitted_accountable_attributes( - :id, :rate_type, :interest_rate, :term_months + :id, :rate_type, :interest_rate, :term_months, :initial_balance ) end diff --git a/app/models/account.rb b/app/models/account.rb index cd1bd8aa..b93a13e1 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -34,6 +34,7 @@ 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 transaction do # Create 2 valuations for new accounts to establish a value history for users to see @@ -47,7 +48,7 @@ class Account < ApplicationRecord account.entries.build( name: "Initial Balance", date: 1.day.ago.to_date, - amount: 0, + amount: initial_balance, currency: account.currency, entryable: Account::Valuation.new ) @@ -92,11 +93,6 @@ class Account < ApplicationRecord end end - def original_balance - balance_amount = balances.chronological.first&.balance || balance - Money.new(balance_amount, currency) - end - def current_holdings holdings.where(currency: currency, date: holdings.maximum(:date)).order(amount: :desc) end @@ -104,9 +100,13 @@ class Account < ApplicationRecord def update_with_sync!(attributes) should_update_balance = attributes[:balance] && attributes[:balance].to_d != balance + initial_balance = attributes.dig(:accountable_attributes, :initial_balance) + should_update_initial_balance = initial_balance && initial_balance.to_d != accountable.initial_balance + transaction do update!(attributes) update_balance!(attributes[:balance]) if should_update_balance + update_inital_balance!(attributes[:accountable_attributes][:initial_balance]) if should_update_initial_balance end sync_later @@ -127,11 +127,34 @@ class Account < ApplicationRecord end end + def update_inital_balance!(initial_balance) + valuation = first_valuation + + if valuation + valuation.update! amount: initial_balance + else + entries.create! \ + date: Date.current, + name: "Initial Balance", + amount: initial_balance, + currency: currency, + entryable: Account::Valuation.new + end + end + def start_date first_entry_date = entries.minimum(:date) || Date.current first_entry_date - 1.day end + def first_valuation + entries.account_valuations.order(:date).first + end + + def first_valuation_amount + first_valuation&.amount_money || balance_money + end + private def sync_balances strategy = linked? ? :reverse : :forward diff --git a/app/models/loan.rb b/app/models/loan.rb index 14b4d084..283e112e 100644 --- a/app/models/loan.rb +++ b/app/models/loan.rb @@ -3,20 +3,24 @@ class Loan < ApplicationRecord def monthly_payment return nil if term_months.nil? || interest_rate.nil? || rate_type.nil? || rate_type != "fixed" - return Money.new(0, account.currency) if account.original_balance.amount.zero? || term_months.zero? + return Money.new(0, account.currency) if account.loan.original_balance.amount.zero? || term_months.zero? annual_rate = interest_rate / 100.0 monthly_rate = annual_rate / 12.0 if monthly_rate.zero? - payment = account.original_balance.amount / term_months + payment = account.loan.original_balance.amount / term_months else - payment = (account.original_balance.amount * monthly_rate * (1 + monthly_rate)**term_months) / ((1 + monthly_rate)**term_months - 1) + payment = (account.loan.original_balance.amount * monthly_rate * (1 + monthly_rate)**term_months) / ((1 + monthly_rate)**term_months - 1) end Money.new(payment.round, account.currency) end + def original_balance + Money.new(account.first_valuation_amount, account.currency) + end + class << self def color "#D444F1" diff --git a/app/views/loans/_form.html.erb b/app/views/loans/_form.html.erb index bceee968..73dd7bb7 100644 --- a/app/views/loans/_form.html.erb +++ b/app/views/loans/_form.html.erb @@ -5,6 +5,13 @@