diff --git a/app/controllers/concerns/entryable_resource.rb b/app/controllers/concerns/entryable_resource.rb index 84aac1d48ca..918b32bb138 100644 --- a/app/controllers/concerns/entryable_resource.rb +++ b/app/controllers/concerns/entryable_resource.rb @@ -119,7 +119,7 @@ def prepared_entry_params def entry_params params.require(:account_entry).permit( - :account_id, :name, :date, :amount, :currency, :excluded, :notes, :nature, + :account_id, :name, :enriched_name, :date, :amount, :currency, :excluded, :notes, :nature, entryable_attributes: self.class.permitted_entryable_attributes ) end diff --git a/app/models/account.rb b/app/models/account.rb index 400e8beaa73..19d883ca094 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -151,6 +151,7 @@ def update_balance!(balance) else entries.create! \ date: Date.current, + name: "Balance update", amount: balance, currency: currency, entryable: Account::Valuation.new diff --git a/app/models/account/data_enricher.rb b/app/models/account/data_enricher.rb index e0615cc134b..0be57dc1bc5 100644 --- a/app/models/account/data_enricher.rb +++ b/app/models/account/data_enricher.rb @@ -50,7 +50,7 @@ def enrich_transactions category.save! if category.present? entry.update!( enriched_at: Time.current, - name: entry.enriched_at.nil? && info.name ? info.name : entry.name, + enriched_name: info.name, entryable_attributes: entryable_attributes ) end diff --git a/app/models/account/entry.rb b/app/models/account/entry.rb index 1801fb9ede4..4d7334fbb75 100644 --- a/app/models/account/entry.rb +++ b/app/models/account/entry.rb @@ -10,7 +10,7 @@ class Account::Entry < ApplicationRecord delegated_type :entryable, types: Account::Entryable::TYPES, dependent: :destroy accepts_nested_attributes_for :entryable - validates :date, :amount, :currency, presence: true + validates :date, :name, :amount, :currency, presence: true validates :date, uniqueness: { scope: [ :account_id, :entryable_type ] }, if: -> { account_valuation? } validates :date, comparison: { greater_than: -> { min_supported_date } } @@ -47,14 +47,6 @@ def sync_account_later account.sync_later(start_date: sync_start_date) end - def inflow? - amount <= 0 && account_transaction? - end - - def outflow? - amount > 0 && account_transaction? - end - def entryable_name_short entryable_type.demodulize.underscore end @@ -63,6 +55,10 @@ def balance_trend(entries, balances) Account::BalanceTrendCalculator.new(self, entries, balances).trend end + def display_name + enriched_name.presence || name + end + class << self # arbitrary cutoff date to avoid expensive sync operations def min_supported_date diff --git a/app/models/account/syncer.rb b/app/models/account/syncer.rb index df5a7b03ec9..5b2e4aba0a2 100644 --- a/app/models/account/syncer.rb +++ b/app/models/account/syncer.rb @@ -11,7 +11,8 @@ def run update_account_info(balances, holdings) unless account.plaid_account_id.present? convert_records_to_family_currency(balances, holdings) unless account.currency == account.family.currency - if account.family.data_enrichment_enabled? + # Enrich if user opted in or if we're syncing transactions from a Plaid account + if account.family.data_enrichment_enabled? || account.plaid_account_id.present? account.enrich_data_later else Rails.logger.info("Data enrichment is disabled, skipping enrichment for account #{account.id}") diff --git a/app/models/account/trade.rb b/app/models/account/trade.rb index b8ebd7b898a..70b0c8f32c2 100644 --- a/app/models/account/trade.rb +++ b/app/models/account/trade.rb @@ -26,11 +26,6 @@ def buy? qty > 0 end - def name - prefix = sell? ? "Sell " : "Buy " - prefix + "#{qty.abs} shares of #{security.ticker}" - end - def unrealized_gain_loss return nil if sell? current_price = security.current_price diff --git a/app/models/account/trade_builder.rb b/app/models/account/trade_builder.rb index 191d810029e..e62947f7313 100644 --- a/app/models/account/trade_builder.rb +++ b/app/models/account/trade_builder.rb @@ -31,7 +31,11 @@ def buildable end def build_trade + prefix = type == "sell" ? "Sell " : "Buy " + trade_name = prefix + "#{qty.to_i.abs} shares of #{security.ticker}" + account.entries.new( + name: trade_name, date: date, amount: signed_amount, currency: currency, diff --git a/app/models/account/transaction.rb b/app/models/account/transaction.rb index fbf2aa9e54c..6b8f4995ee5 100644 --- a/app/models/account/transaction.rb +++ b/app/models/account/transaction.rb @@ -48,10 +48,6 @@ def searchable_keys end end - def name - entry.name || (entry.amount.positive? ? "Expense" : "Income") - end - def eod_balance entry.amount_money end diff --git a/app/models/account/transfer.rb b/app/models/account/transfer.rb index 174576e8b3f..ea9084131c4 100644 --- a/app/models/account/transfer.rb +++ b/app/models/account/transfer.rb @@ -33,11 +33,11 @@ def to_account end def inflow_transaction - entries.find { |e| e.inflow? } + entries.find { |e| e.amount.negative? } end def outflow_transaction - entries.find { |e| e.outflow? } + entries.find { |e| e.amount.positive? } end def update_entries!(params) diff --git a/app/models/account/valuation.rb b/app/models/account/valuation.rb index 5a4d1b8f66c..93ebf5ffb1f 100644 --- a/app/models/account/valuation.rb +++ b/app/models/account/valuation.rb @@ -10,8 +10,4 @@ def requires_search?(_params) false end end - - def name - "Balance update" - end end diff --git a/app/models/demo/generator.rb b/app/models/demo/generator.rb index 29985a365b6..36e86200a28 100644 --- a/app/models/demo/generator.rb +++ b/app/models/demo/generator.rb @@ -303,6 +303,7 @@ def create_valuation!(account, date, amount) date: date, amount: amount, currency: "USD", + name: "Balance update", entryable: Account::Valuation.new end diff --git a/app/views/account/trades/_trade.html.erb b/app/views/account/trades/_trade.html.erb index 2215cd94bcc..8c5c924ee0b 100644 --- a/app/views/account/trades/_trade.html.erb +++ b/app/views/account/trades/_trade.html.erb @@ -13,14 +13,14 @@