2024-02-02 09:05:04 -06:00
|
|
|
class Account < ApplicationRecord
|
2025-04-18 11:39:58 -04:00
|
|
|
include Syncable, Monetizable, Chartable, Linkable, Convertible, Enrichable
|
2024-02-29 08:32:52 -05:00
|
|
|
|
2024-07-17 14:18:12 -04:00
|
|
|
validates :name, :balance, :currency, presence: true
|
2024-03-15 12:21:59 -07:00
|
|
|
|
2024-02-02 09:05:04 -06:00
|
|
|
belongs_to :family
|
2024-10-01 10:47:59 -04:00
|
|
|
belongs_to :import, optional: true
|
2024-07-01 10:49:43 -04:00
|
|
|
|
2024-10-01 10:47:59 -04:00
|
|
|
has_many :import_mappings, as: :mappable, dependent: :destroy, class_name: "Import::Mapping"
|
2025-04-14 11:40:34 -04:00
|
|
|
has_many :entries, dependent: :destroy
|
|
|
|
has_many :transactions, through: :entries, source: :entryable, source_type: "Transaction"
|
|
|
|
has_many :valuations, through: :entries, source: :entryable, source_type: "Valuation"
|
|
|
|
has_many :trades, through: :entries, source: :entryable, source_type: "Trade"
|
|
|
|
has_many :holdings, dependent: :destroy
|
2024-03-29 12:53:08 -04:00
|
|
|
has_many :balances, dependent: :destroy
|
2024-02-02 11:09:31 -06:00
|
|
|
|
2024-12-10 17:41:20 -05:00
|
|
|
monetize :balance, :cash_balance
|
2024-03-18 11:21:00 -04:00
|
|
|
|
2024-07-01 10:49:43 -04:00
|
|
|
enum :classification, { asset: "asset", liability: "liability" }, validate: { allow_nil: true }
|
2024-03-11 16:32:13 -04:00
|
|
|
|
2025-02-21 11:57:59 -05:00
|
|
|
scope :active, -> { where(is_active: true) }
|
2024-03-11 16:32:13 -04:00
|
|
|
scope :assets, -> { where(classification: "asset") }
|
|
|
|
scope :liabilities, -> { where(classification: "liability") }
|
2024-04-16 12:44:31 -06:00
|
|
|
scope :alphabetically, -> { order(:name) }
|
2024-11-15 13:49:37 -05:00
|
|
|
scope :manual, -> { where(plaid_account_id: nil) }
|
2024-03-07 10:55:51 -05:00
|
|
|
|
2024-10-18 14:37:42 -04:00
|
|
|
has_one_attached :logo
|
|
|
|
|
2024-02-09 14:26:54 +00:00
|
|
|
delegated_type :accountable, types: Accountable::TYPES, dependent: :destroy
|
2024-02-02 23:06:29 +00:00
|
|
|
|
2024-11-04 20:27:31 -05:00
|
|
|
accepts_nested_attributes_for :accountable, update_only: true
|
2024-08-23 08:47:08 -04:00
|
|
|
|
2024-07-08 09:04:59 -04:00
|
|
|
class << self
|
2024-11-04 20:27:31 -05:00
|
|
|
def create_and_sync(attributes)
|
|
|
|
attributes[:accountable_attributes] ||= {} # Ensure accountable is created, even if empty
|
2024-12-10 17:41:20 -05:00
|
|
|
account = new(attributes.merge(cash_balance: attributes[:balance]))
|
2025-04-14 09:09:25 -04:00
|
|
|
initial_balance = attributes.dig(:accountable_attributes, :initial_balance)&.to_d || 0
|
2024-08-23 08:47:08 -04:00
|
|
|
|
2024-11-04 20:27:31 -05:00
|
|
|
transaction do
|
|
|
|
# Create 2 valuations for new accounts to establish a value history for users to see
|
|
|
|
account.entries.build(
|
|
|
|
name: "Current Balance",
|
2024-08-23 08:47:08 -04:00
|
|
|
date: Date.current,
|
|
|
|
amount: account.balance,
|
2024-07-08 09:04:59 -04:00
|
|
|
currency: account.currency,
|
2025-04-14 11:40:34 -04:00
|
|
|
entryable: Valuation.new
|
2024-11-04 20:27:31 -05:00
|
|
|
)
|
|
|
|
account.entries.build(
|
|
|
|
name: "Initial Balance",
|
|
|
|
date: 1.day.ago.to_date,
|
2025-04-14 09:09:25 -04:00
|
|
|
amount: initial_balance,
|
2024-11-04 20:27:31 -05:00
|
|
|
currency: account.currency,
|
2025-04-14 11:40:34 -04:00
|
|
|
entryable: Valuation.new
|
2024-11-04 20:27:31 -05:00
|
|
|
)
|
2024-08-23 08:47:08 -04:00
|
|
|
|
|
|
|
account.save!
|
|
|
|
end
|
2024-11-04 20:27:31 -05:00
|
|
|
|
|
|
|
account.sync_later
|
|
|
|
account
|
2024-07-08 09:04:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2025-02-21 11:57:59 -05:00
|
|
|
def institution_domain
|
|
|
|
return nil unless plaid_account&.plaid_item&.institution_url.present?
|
|
|
|
URI.parse(plaid_account.plaid_item.institution_url).host.gsub(/^www\./, "")
|
|
|
|
end
|
|
|
|
|
2024-11-15 13:49:37 -05:00
|
|
|
def destroy_later
|
2025-02-21 11:57:59 -05:00
|
|
|
update!(scheduled_for_deletion: true, is_active: false)
|
2024-11-15 13:49:37 -05:00
|
|
|
DestroyJob.perform_later(self)
|
|
|
|
end
|
|
|
|
|
2025-04-11 12:13:46 -04:00
|
|
|
def sync_data(sync, start_date: nil)
|
2024-11-15 13:49:37 -05:00
|
|
|
update!(last_synced_at: Time.current)
|
|
|
|
|
2025-03-07 17:35:55 -05:00
|
|
|
Rails.logger.info("Processing balances (#{linked? ? 'reverse' : 'forward'})")
|
|
|
|
sync_balances
|
2025-04-11 12:13:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def post_sync(sync)
|
|
|
|
family.remove_syncing_notice!
|
|
|
|
|
|
|
|
accountable.post_sync(sync)
|
2025-03-07 17:35:55 -05:00
|
|
|
|
2025-04-11 12:13:46 -04:00
|
|
|
unless sync.child?
|
|
|
|
family.auto_match_transfers!
|
|
|
|
end
|
2024-11-20 16:46:06 -05:00
|
|
|
end
|
|
|
|
|
2024-12-12 08:56:52 -05:00
|
|
|
def current_holdings
|
|
|
|
holdings.where(currency: currency, date: holdings.maximum(:date)).order(amount: :desc)
|
2024-02-20 09:07:55 -05:00
|
|
|
end
|
|
|
|
|
2024-10-08 17:16:37 -04:00
|
|
|
def update_with_sync!(attributes)
|
2025-01-23 21:14:01 -05:00
|
|
|
should_update_balance = attributes[:balance] && attributes[:balance].to_d != balance
|
|
|
|
|
2025-04-14 09:09:25 -04:00
|
|
|
initial_balance = attributes.dig(:accountable_attributes, :initial_balance)
|
|
|
|
should_update_initial_balance = initial_balance && initial_balance.to_d != accountable.initial_balance
|
|
|
|
|
2024-10-08 17:16:37 -04:00
|
|
|
transaction do
|
|
|
|
update!(attributes)
|
2025-01-23 21:14:01 -05:00
|
|
|
update_balance!(attributes[:balance]) if should_update_balance
|
2025-04-14 09:09:25 -04:00
|
|
|
update_inital_balance!(attributes[:accountable_attributes][:initial_balance]) if should_update_initial_balance
|
2024-10-08 17:16:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
sync_later
|
|
|
|
end
|
|
|
|
|
2024-07-12 13:47:39 -04:00
|
|
|
def update_balance!(balance)
|
2025-04-14 11:40:34 -04:00
|
|
|
valuation = entries.valuations.find_by(date: Date.current)
|
2024-07-12 13:47:39 -04:00
|
|
|
|
|
|
|
if valuation
|
|
|
|
valuation.update! amount: balance
|
|
|
|
else
|
|
|
|
entries.create! \
|
|
|
|
date: Date.current,
|
2024-12-19 10:16:09 -05:00
|
|
|
name: "Balance update",
|
2024-07-12 13:47:39 -04:00
|
|
|
amount: balance,
|
|
|
|
currency: currency,
|
2025-04-14 11:40:34 -04:00
|
|
|
entryable: Valuation.new
|
2024-07-12 13:47:39 -04:00
|
|
|
end
|
|
|
|
end
|
2025-01-27 16:56:46 -05:00
|
|
|
|
2025-04-14 09:09:25 -04:00
|
|
|
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,
|
2025-04-14 12:51:38 -04:00
|
|
|
entryable: Valuation.new
|
2025-04-14 09:09:25 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2025-03-07 17:35:55 -05:00
|
|
|
def start_date
|
|
|
|
first_entry_date = entries.minimum(:date) || Date.current
|
|
|
|
first_entry_date - 1.day
|
|
|
|
end
|
2025-01-27 16:56:46 -05:00
|
|
|
|
2025-04-18 11:39:58 -04:00
|
|
|
def lock_saved_attributes!
|
|
|
|
super
|
|
|
|
accountable.lock_saved_attributes!
|
|
|
|
end
|
|
|
|
|
2025-04-14 09:09:25 -04:00
|
|
|
def first_valuation
|
2025-04-14 11:40:34 -04:00
|
|
|
entries.valuations.order(:date).first
|
2025-04-14 09:09:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def first_valuation_amount
|
|
|
|
first_valuation&.amount_money || balance_money
|
|
|
|
end
|
|
|
|
|
2025-04-22 13:10:50 -05:00
|
|
|
# Get short version of the subtype label
|
|
|
|
def short_subtype_label
|
|
|
|
accountable_class.short_subtype_label_for(subtype) || accountable_class.display_name
|
|
|
|
end
|
|
|
|
|
|
|
|
# Get long version of the subtype label
|
|
|
|
def long_subtype_label
|
|
|
|
accountable_class.long_subtype_label_for(subtype) || accountable_class.display_name
|
|
|
|
end
|
|
|
|
|
2025-03-07 17:35:55 -05:00
|
|
|
private
|
|
|
|
def sync_balances
|
|
|
|
strategy = linked? ? :reverse : :forward
|
|
|
|
Balance::Syncer.new(self, strategy: strategy).sync_balances
|
2025-01-27 16:56:46 -05:00
|
|
|
end
|
2024-02-02 09:05:04 -06:00
|
|
|
end
|