Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Allow categorizing investment/loan transfer transactions (#3719)

GFRa92dec1e6177
diff --git a/app/controllers/transfer_matches_controller.rb b/app/controllers/transfer_matches_controller.rb
index 4e67653baa..a306a86ede 100644
--- a/app/controllers/transfer_matches_controller.rb
+++ b/app/controllers/transfer_matches_controller.rb
@@ -18,7 +18,8 @@ class TransferMatchesController < ApplicationController
# Use DESTINATION (inflow) account for kind, matching Transfer::Creator logic
destination_account = @transfer.inflow_transaction.entry.account
- outflow_kind = Transfer.kind_for_account(destination_account)
+ source_account = @transfer.outflow_transaction.entry.account
+ outflow_kind = Transfer.kind_for_account(destination_account, from_account: source_account)
outflow_attrs = { kind: outflow_kind }
if outflow_kind == "investment_contribution"
diff --git a/app/models/account/provider_import_adapter.rb b/app/models/account/provider_import_adapter.rb
index 524f0a62a9..111921dcdb 100644
--- a/app/models/account/provider_import_adapter.rb
+++ b/app/models/account/provider_import_adapter.rb
@@ -277,8 +277,15 @@ class Account::ProviderImportAdapter
entry.transaction.assign_attributes(investment_activity_label: detected_label)
end
+ # A leg that is already matched to a Transfer takes its kind from the
+ # transfer, not from this row. Re-classifying it here, e.g. a negative
+ # amount on a Loan turning the matched inflow back into loan_payment,
+ # makes budgets and reports count the same payment as an expense on
+ # both legs. Deriving it from the transfer also repairs legs an earlier
+ # sync already overwrote.
if auto_kind.present?
- entry.transaction.assign_attributes(kind: auto_kind)
+ transfer = matched_transfer_for(entry)
+ entry.transaction.assign_attributes(kind: transfer ? transfer.kind_for_leg(entry.transaction) : auto_kind)
end
if auto_category.present? && entry.transaction.category_id.blank?
@@ -1060,6 +1067,13 @@ class Account::ProviderImportAdapter
private
+ def matched_transfer_for(entry)
+ return nil unless entry.persisted?
+
+ transaction_id = entry.transaction.id
+ Transfer.where(inflow_transaction_id: transaction_id).or(Transfer.where(outflow_transaction_id: transaction_id)).first
+ end
+
# Memoized per adapter instance (which is per-account). Membership in
# goal_accounts is stable across a sync batch.
def account_linked_to_any_goal?
diff --git a/app/models/family/auto_transfer_matchable.rb b/app/models/family/auto_transfer_matchable.rb
index 579633d207..3c6c56e228 100644
--- a/app/models/family/auto_transfer_matchable.rb
+++ b/app/models/family/auto_transfer_matchable.rb
@@ -95,7 +95,8 @@ module Family::AutoTransferMatchable
inflow_transaction = transactions_by_id.fetch(match.inflow_transaction_id)
outflow_transaction = transactions_by_id.fetch(match.outflow_transaction_id)
destination_account = inflow_transaction.entry.account
- transfer_kind = Transfer.kind_for_account(destination_account)
+ source_account = outflow_transaction.entry.account
+ transfer_kind = Transfer.kind_for_account(destination_account, from_account: source_account)
# The kind is determined by the DESTINATION account (inflow), matching Transfer::Creator logic
inflow_transaction.update!(kind: "funds_movement")
diff --git a/app/models/family/data_importer.rb b/app/models/family/data_importer.rb
index 711e87416d..ad08d322f4 100644
--- a/app/models/family/data_importer.rb
+++ b/app/models/family/data_importer.rb
@@ -1072,15 +1072,7 @@ class Family::DataImporter
def imported_transfer_outflow_kind(transfer)
source_account = transfer.outflow_transaction.entry.account
destination_account = transfer.inflow_transaction.entry.account
- return "loan_payment" if destination_account.loan?
- return "cc_payment" if destination_account.liability?
- return "investment_contribution" if investment_account?(destination_account) && !investment_account?(source_account)
-
- "funds_movement"
- end
-
- def investment_account?(account)
- account.investment? || account.crypto?
+ Transfer.kind_for_account(destination_account, from_account: source_account)
end
def import_rejected_transfers(records)
diff --git a/app/models/rule/action_executor/set_as_transfer_or_payment.rb b/app/models/rule/action_executor/set_as_transfer_or_payment.rb
index 9f3a79b12a..6845e53ade 100644
--- a/app/models/rule/action_executor/set_as_transfer_or_payment.rb
+++ b/app/models/rule/action_executor/set_as_transfer_or_payment.rb
@@ -21,7 +21,8 @@ class Rule::ActionExecutor::SetAsTransferOrPayment < Rule::ActionExecutor
# Use DESTINATION (inflow) account for kind, matching Transfer::Creator logic
destination_account = transfer.inflow_transaction.entry.account
- outflow_kind = Transfer.kind_for_account(destination_account)
+ source_account = transfer.outflow_transaction.entry.account
+ outflow_kind = Transfer.kind_for_account(destination_account, from_account: source_account)
outflow_attrs = { kind: outflow_kind }
if outflow_kind == "investment_contribution"
diff --git a/app/models/transaction.rb b/app/models/transaction.rb
index b8ddc97fb5..5263da260c 100644
--- a/app/models/transaction.rb
+++ b/app/models/transaction.rb
@@ -154,6 +154,37 @@ class Transaction < ApplicationRecord
TRANSFER_KINDS.include?(kind)
end
+ # Whether the user can assign a category (and merchant/tags) to this
+ # transaction. Regular transactions always qualify. For transfers:
+ # - No Transfer record yet (e.g. an unmatched provider-imported leg):
+ # stay editable, same as a regular transaction, since there's no
+ # counterpart to defer to and no other way for the user to fix a
+ # provider mislabel.
+ # - Once matched, both legs defer to Transfer#categorizable?, which is
+ # based on the (stable) destination account rather than either leg's
+ # kind, so both legs of e.g. a loan payment agree and stay correct
+ # even if an older provider sync left a stale kind on this
+ # transaction.
+ def category_editable?
+ return true unless transfer?
+ return true unless transfer
+
+ transfer.categorizable?
+ end
+
+ # Whether this non-editable transfer leg is a liability payment (shown
+ # with the "Payment" badge instead of "Transfer"). Defers to the attached
+ # Transfer when one exists, since a matched transaction's own kind can be
+ # stale (rows a provider sync overwrote before the import adapter derived
+ # matched legs from their Transfer), which would otherwise let a stale
+ # "cc_payment" kind override a transfer that isn't actually a payment.
+ # Only falls back to the transaction's own kind when there's no Transfer
+ # record yet (e.g. a provider-imported cc_payment leg whose counterpart
+ # hasn't been matched), the same pattern category_editable? uses.
+ def payment?
+ transfer ? transfer.payment? : kind == "cc_payment"
+ end
+
def set_category!(category)
if category.is_a?(String)
category = entry.account.family.categories.find_or_create_by!(
diff --git a/app/models/transaction/transferable.rb b/app/models/transaction/transferable.rb
index 56354aa32d..0060e50b1a 100644
--- a/app/models/transaction/transferable.rb
+++ b/app/models/transaction/transferable.rb
@@ -2,8 +2,15 @@ module Transaction::Transferable
extend ActiveSupport::Concern
included do
- has_one :transfer_as_inflow, class_name: "Transfer", foreign_key: "inflow_transaction_id", dependent: :destroy
- has_one :transfer_as_outflow, class_name: "Transfer", foreign_key: "outflow_transaction_id", dependent: :destroy
+ # inverse_of is load-bearing, not cosmetic: Transfer#categorizable? calls
+ # from_account on every transfer row of the transactions index, which walks
+ # transfer.outflow_transaction.entry.account. The index only preloads the
+ # counterparty (inflow) leg, so without the inverse each outflow row
+ # re-queries its own transaction, entry and account (an N+1 that
+ # TransactionsControllerTest "index preloads transfer counterparty entry
+ # and account to avoid N+1" catches).
+ has_one :transfer_as_inflow, class_name: "Transfer", foreign_key: "inflow_transaction_id", inverse_of: :inflow_transaction, dependent: :destroy
+ has_one :transfer_as_outflow, class_name: "Transfer", foreign_key: "outflow_transaction_id", inverse_of: :outflow_transaction, dependent: :destroy
# We keep track of rejected transfers to avoid auto-matching them again
has_one :rejected_transfer_as_inflow, class_name: "RejectedTransfer", foreign_key: "inflow_transaction_id", dependent: :destroy
diff --git a/app/models/transfer.rb b/app/models/transfer.rb
index 88afa2a9d1..e3bd6df273 100644
--- a/app/models/transfer.rb
+++ b/app/models/transfer.rb
@@ -1,6 +1,6 @@
class Transfer < ApplicationRecord
- belongs_to :inflow_transaction, class_name: "Transaction"
- belongs_to :outflow_transaction, class_name: "Transaction"
+ belongs_to :inflow_transaction, class_name: "Transaction", inverse_of: :transfer_as_inflow
+ belongs_to :outflow_transaction, class_name: "Transaction", inverse_of: :transfer_as_outflow
has_many :fee_transactions, class_name: "Transaction", dependent: :destroy
@@ -17,13 +17,23 @@ class Transfer < ApplicationRecord
validate :transfer_has_same_family
class << self
- def kind_for_account(account)
+ # The outflow leg's kind for a transfer into this account. Transfer::Creator,
+ # Family::DataImporter, auto-matching, rules and manual matches all call
+ # it (the inflow leg is always funds_movement, see kind_for_leg).
+ # from_account: an investment/crypto destination only counts as a
+ # contribution when the source isn't itself an investment/crypto account,
+ # otherwise it's a plain funds movement.
+ def kind_for_account(account, from_account: nil)
if account.loan?
"loan_payment"
elsif account.credit_card?
"cc_payment"
elsif account.investment? || account.crypto?
- "investment_contribution"
+ if from_account && (from_account.investment? || from_account.crypto?)
+ "funds_movement"
+ else
+ "investment_contribution"
+ end
elsif account.liability?
"cc_payment"
else
@@ -91,8 +101,22 @@ class Transfer < ApplicationRecord
"transfer"
end
+ # The kind a leg carries while this transfer exists: the outflow by the
+ # destination account, the inflow always funds_movement.
+ def kind_for_leg(transaction)
+ return "funds_movement" if transaction.id == inflow_transaction_id
+
+ Transfer.kind_for_account(to_account, from_account: from_account)
+ end
+
+ # Based on the destination account rather than outflow_transaction.kind,
+ # which can be stale on rows a provider sync overwrote before
+ # Account::ProviderImportAdapter derived matched legs from their transfer
+ # (see Transaction#payment?, which has the same to_account-based reasoning).
def categorizable?
- to_account&.accountable_type == "Loan"
+ return false unless to_account
+
+ !Transaction::UNCATEGORIZED_EXCLUDED_KINDS.include?(Transfer.kind_for_account(to_account, from_account: from_account))
end
def reject!
diff --git a/app/models/transfer/creator.rb b/app/models/transfer/creator.rb
index 8119c2c22f..90ee290d2b 100644
--- a/app/models/transfer/creator.rb
+++ b/app/models/transfer/creator.rb
@@ -244,23 +244,7 @@ class Transfer::Creator
end
def outflow_transaction_kind
- if destination_account.loan?
- "loan_payment"
- elsif destination_account.liability?
- "cc_payment"
- elsif destination_is_investment? && !source_is_investment?
- "investment_contribution"
- else
- "funds_movement"
- end
- end
-
- def destination_is_investment?
- destination_account.investment? || destination_account.crypto?
- end
-
- def source_is_investment?
- source_account.investment? || source_account.crypto?
+ Transfer.kind_for_account(destination_account, from_account: source_account)
end
def name_prefix
diff --git a/app/views/categories/_category_name_mobile.html.erb b/app/views/categories/_category_name_mobile.html.erb
index 05404faa98..0c18f39480 100644
--- a/app/views/categories/_category_name_mobile.html.erb
+++ b/app/views/categories/_category_name_mobile.html.erb
@@ -1,7 +1,7 @@
<span id="category_name_mobile_<%= transaction.id %>" class="text-secondary lg:hidden min-w-0 truncate">
- <% if transaction.transfer&.categorizable? || transaction.transfer.nil? %>
+ <% if transaction.category_editable? %>
<%= transaction.category&.display_name || Category.uncategorized.display_name %>
<% else %>
- <%= transaction.transfer&.payment? ? payment_category.display_name : transfer_category.display_name %>
+ <%= transaction.payment? ? payment_category.display_name : transfer_category.display_name %>
<% end %>
</span>
diff --git a/app/views/transactions/_transaction_category.html.erb b/app/views/transactions/_transaction_category.html.erb
index 0d73016330..0da719edb5 100644
--- a/app/views/transactions/_transaction_category.html.erb
+++ b/app/views/transactions/_transaction_category.html.erb
@@ -1,14 +1,14 @@
<%# locals: (transaction:, variant:, in_split_group: false) %>
<div id="<%= dom_id(transaction, "category_menu_#{variant}") %>" class="min-w-0 overflow-hidden">
- <% if transaction.transfer&.categorizable? || transaction.transfer.nil? %>
+ <% if transaction.category_editable? %>
<%= render "categories/menu", transaction: transaction, in_split_group: in_split_group %>
<% else %>
<div class="hidden lg:flex">
- <%= render "categories/badge", category: transaction.transfer&.payment? ? payment_category : transfer_category %>
+ <%= render "categories/badge", category: transaction.payment? ? payment_category : transfer_category %>
</div>
<div class="flex lg:hidden">
- <%= render "categories/badge_mobile", category: transaction.transfer&.payment? ? payment_category : transfer_category %>
+ <%= render "categories/badge_mobile", category: transaction.payment? ? payment_category : transfer_category %>
</div>
<% end %>
</div>
diff --git a/app/views/transactions/show.html.erb b/app/views/transactions/show.html.erb
index 6fe28c5fb4..01f9e41fe4 100644
--- a/app/views/transactions/show.html.erb
+++ b/app/views/transactions/show.html.erb
@@ -82,6 +82,8 @@
disabled: @entry.linked? || split_locked || edit_locked,
disable_currency: @entry.linked? || split_locked || edit_locked %>
</div>
+ <% end %>
+ <% if @entry.transaction.category_editable? %>
<%= f.fields_for :entryable do |ef| %>
<%= render DS::CategorySelect.new(
form: ef,
diff --git a/test/controllers/transactions_controller_test.rb b/test/controllers/transactions_controller_test.rb
index fc3fd91577..9516ced6bb 100644
--- a/test/controllers/transactions_controller_test.rb
+++ b/test/controllers/transactions_controller_test.rb
@@ -1389,6 +1389,9 @@ end
# Transfer#categorizable? / #payment? walk to_account via
# transfer.inflow_transaction.entry.account. Without nested includes those
# become one lookup triad per transfer row during list render.
+ # categorizable? also walks from_account via the outflow leg, which is only
+ # loaded through the inverse_of on Transaction::Transferable; dropping it
+ # makes the transactions.id assertion below fail.
normalized_queries = queries.map { |sql| normalize_sql_query(sql) }
assert_empty single_record_lookups(normalized_queries, table: "transactions", column: "id"),
"Expected transfer counterparty transactions to be preloaded"
diff --git a/test/models/account/provider_import_adapter_test.rb b/test/models/account/provider_import_adapter_test.rb
index e19dc3d9d9..db60636e3b 100644
--- a/test/models/account/provider_import_adapter_test.rb
+++ b/test/models/account/provider_import_adapter_test.rb
@@ -86,6 +86,180 @@ class Account::ProviderImportAdapterTest < ActiveSupport::TestCase
"a repayment on a Loan account must stay loan_payment, not the provider's funds_movement"
end
+ test "re-importing a matched loan payment leg keeps the kinds its transfer set" do
+ loan_adapter = Account::ProviderImportAdapter.new(accounts(:loan))
+
+ loan_entry = loan_adapter.import_transaction(
+ external_id: "plaid_loan_inflow_1",
+ amount: -200.00,
+ currency: "USD",
+ date: Date.current,
+ name: "Loan Repayment Received",
+ source: "plaid"
+ )
+ checking_entry = @adapter.import_transaction(
+ external_id: "plaid_checking_outflow_1",
+ amount: 200.00,
+ currency: "USD",
+ date: Date.current,
+ name: "Loan Repayment",
+ source: "plaid"
+ )
+
+ @family.auto_match_transfers!
+
+ transfer = loan_entry.transaction.reload.transfer
+ assert transfer.present?, "expected the two legs to be auto-matched"
+ assert_equal "funds_movement", loan_entry.transaction.kind
+ assert_equal "loan_payment", checking_entry.transaction.reload.kind
+
+ # The next sync of the loan account sees a negative amount on a Loan and
+ # would classify the leg as loan_payment again, which makes budgets and
+ # reports count the same repayment as an expense on both legs.
+ loan_adapter.import_transaction(
+ external_id: "plaid_loan_inflow_1",
+ amount: -200.00,
+ currency: "USD",
+ date: Date.current,
+ name: "Loan Repayment Received",
+ source: "plaid"
+ )
+
+ assert_equal "funds_movement", loan_entry.transaction.reload.kind
+ assert_equal "loan_payment", checking_entry.transaction.reload.kind
+ end
+
+ test "a provider transfer hint does not overwrite the kind of a matched payment leg" do
+ loan_entry = Account::ProviderImportAdapter.new(accounts(:loan)).import_transaction(
+ external_id: "up_loan_inflow_1",
+ amount: -150.00,
+ currency: "USD",
+ date: Date.current,
+ name: "Loan Repayment Received",
+ source: "up"
+ )
+ checking_entry = @adapter.import_transaction(
+ external_id: "up_checking_outflow_1",
+ amount: 150.00,
+ currency: "USD",
+ date: Date.current,
+ name: "Loan Repayment",
+ source: "up"
+ )
+
+ @family.auto_match_transfers!
+ assert checking_entry.transaction.reload.transfer.present?, "expected the two legs to be auto-matched"
+ assert_equal "loan_payment", checking_entry.transaction.kind
+
+ # Up flags the outgoing leg as an internal transfer (transferAccount).
+ @adapter.import_transaction(
+ external_id: "up_checking_outflow_1",
+ amount: 150.00,
+ currency: "USD",
+ date: Date.current,
+ name: "Loan Repayment",
+ source: "up",
+ kind: "funds_movement"
+ )
+
+ assert_equal "loan_payment", checking_entry.transaction.reload.kind
+ end
+
+ test "re-importing a matched leg repairs a kind an earlier sync overwrote" do
+ loan_adapter = Account::ProviderImportAdapter.new(accounts(:loan))
+ loan_entry = loan_adapter.import_transaction(
+ external_id: "plaid_loan_inflow_legacy",
+ amount: -300.00,
+ currency: "USD",
+ date: Date.current,
+ name: "Loan Repayment Received",
+ source: "plaid"
+ )
+ @adapter.import_transaction(
+ external_id: "plaid_checking_outflow_legacy",
+ amount: 300.00,
+ currency: "USD",
+ date: Date.current,
+ name: "Loan Repayment",
+ source: "plaid"
+ )
+ @family.auto_match_transfers!
+ assert loan_entry.transaction.reload.transfer.present?, "expected the two legs to be auto-matched"
+
+ # State left behind by syncs before the fix: the matched inflow was
+ # turned back into loan_payment.
+ loan_entry.transaction.update_columns(kind: "loan_payment")
+
+ loan_adapter.import_transaction(
+ external_id: "plaid_loan_inflow_legacy",
+ amount: -300.00,
+ currency: "USD",
+ date: Date.current,
+ name: "Loan Repayment Received",
+ source: "plaid"
+ )
+
+ assert_equal "funds_movement", loan_entry.transaction.reload.kind
+ end
+
+ test "a rejected match takes the account kind again on re-import" do
+ loan_adapter = Account::ProviderImportAdapter.new(accounts(:loan))
+ loan_entry = loan_adapter.import_transaction(
+ external_id: "plaid_loan_inflow_rejected",
+ amount: -120.00,
+ currency: "USD",
+ date: Date.current,
+ name: "Loan Repayment Received",
+ source: "plaid"
+ )
+ @adapter.import_transaction(
+ external_id: "plaid_checking_outflow_rejected",
+ amount: 120.00,
+ currency: "USD",
+ date: Date.current,
+ name: "Loan Repayment",
+ source: "plaid"
+ )
+ @family.auto_match_transfers!
+ loan_entry.transaction.reload.transfer.reject!
+
+ loan_adapter.import_transaction(
+ external_id: "plaid_loan_inflow_rejected",
+ amount: -120.00,
+ currency: "USD",
+ date: Date.current,
+ name: "Loan Repayment Received",
+ source: "plaid"
+ )
+
+ assert_nil loan_entry.transaction.reload.transfer
+ assert_equal "loan_payment", loan_entry.transaction.kind
+ end
+
+ test "an unmatched provider leg still takes the provider kind on re-import" do
+ entry = @adapter.import_transaction(
+ external_id: "up_unmatched_1",
+ amount: -40.00,
+ currency: "USD",
+ date: Date.current,
+ name: "Round Up",
+ source: "up"
+ )
+ assert_equal "standard", entry.transaction.kind
+
+ @adapter.import_transaction(
+ external_id: "up_unmatched_1",
+ amount: -40.00,
+ currency: "USD",
+ date: Date.current,
+ name: "Round Up",
+ source: "up",
+ kind: "funds_movement"
+ )
+
+ assert_equal "funds_movement", entry.transaction.reload.kind
+ end
+
test "updates existing transaction instead of creating duplicate" do
# Create initial transaction
entry = @adapter.import_transaction(
diff --git a/test/models/transaction_test.rb b/test/models/transaction_test.rb
index 106cad4d99..48b331f735 100644
--- a/test/models/transaction_test.rb
+++ b/test/models/transaction_test.rb
@@ -143,6 +143,83 @@ class TransactionTest < ActiveSupport::TestCase
end
end
+ test "category_editable? is true for non-transfer kinds" do
+ assert Transaction.new(kind: "standard").category_editable?
+ assert Transaction.new(kind: "one_time").category_editable?
+ end
+
+ test "category_editable? without a Transfer record stays editable regardless of kind" do
+ # An unmatched provider-imported leg has no counterpart to defer to and
+ # no other way for the user to fix a provider mislabel, so it stays
+ # editable just like a regular transaction.
+ assert Transaction.new(kind: "loan_payment").category_editable?
+ assert Transaction.new(kind: "investment_contribution").category_editable?
+ assert Transaction.new(kind: "funds_movement").category_editable?
+ assert Transaction.new(kind: "cc_payment").category_editable?
+ end
+
+ test "category_editable? defers both legs to Transfer#categorizable? when a Transfer record exists" do
+ outflow_entry = create_transaction(date: Date.current, account: accounts(:depository), amount: 500, kind: "investment_contribution")
+ inflow_entry = create_transaction(date: Date.current, account: accounts(:investment), amount: -500, kind: "funds_movement")
+ Transfer.create!(inflow_transaction: inflow_entry.transaction, outflow_transaction: outflow_entry.transaction)
+
+ # Transfer#categorizable? is destination-account based, not leg based, so
+ # the inflow leg (whose stored kind is always "funds_movement" per
+ # Transfer::Creator) agrees with the outflow leg instead of being
+ # unconditionally locked.
+ assert outflow_entry.transaction.reload.category_editable?
+ assert inflow_entry.transaction.reload.category_editable?
+
+ fm_outflow = create_transaction(date: Date.current, account: accounts(:depository), amount: 500, kind: "funds_movement")
+ fm_inflow = create_transaction(date: Date.current, account: accounts(:connected), amount: -500, kind: "funds_movement")
+ Transfer.create!(inflow_transaction: fm_inflow.transaction, outflow_transaction: fm_outflow.transaction)
+
+ assert_not fm_outflow.transaction.reload.category_editable?
+ assert_not fm_inflow.transaction.reload.category_editable?
+ end
+
+ test "category_editable? stays true for the outflow leg even if a later sync leaves a stale funds_movement kind" do
+ # Account::ProviderImportAdapter can reassign an already-matched
+ # transaction's kind on a later sync without touching its Transfer.
+ # Transfer#categorizable? is destination-account based (stable), so
+ # delegating to it -- rather than reading this transaction's own kind
+ # directly -- keeps category_editable? correct despite the stale kind.
+ outflow_entry = create_transaction(date: Date.current, account: accounts(:depository), amount: 500, kind: "investment_contribution")
+ inflow_entry = create_transaction(date: Date.current, account: accounts(:investment), amount: -500, kind: "funds_movement")
+ Transfer.create!(inflow_transaction: inflow_entry.transaction, outflow_transaction: outflow_entry.transaction)
+ outflow_entry.transaction.update_column(:kind, "funds_movement")
+
+ assert outflow_entry.transaction.reload.category_editable?
+ end
+
+ test "payment? is true for cc_payment kind without a Transfer record" do
+ assert Transaction.new(kind: "cc_payment").payment?
+ assert_not Transaction.new(kind: "funds_movement").payment?
+ end
+
+ test "payment? defers to Transfer#payment? when a Transfer record exists" do
+ outflow_entry = create_transaction(date: Date.current, account: accounts(:depository), amount: 500, kind: "cc_payment")
+ inflow_entry = create_transaction(date: Date.current, account: accounts(:credit_card), amount: -500, kind: "cc_payment")
+ Transfer.create!(inflow_transaction: inflow_entry.transaction, outflow_transaction: outflow_entry.transaction)
+
+ assert outflow_entry.transaction.reload.payment?
+ assert inflow_entry.transaction.reload.payment?
+ end
+
+ test "payment? stays false for a non-payment transfer even if a later sync leaves a stale cc_payment kind" do
+ # Account::ProviderImportAdapter can reassign an already-matched
+ # transaction's kind on a later sync without touching its Transfer
+ # record. Simulate that here: a regular funds_movement transfer (to a
+ # non-liability account) whose outflow transaction's kind was later
+ # (incorrectly) left as "cc_payment".
+ outflow_entry = create_transaction(date: Date.current, account: accounts(:depository), amount: 500, kind: "funds_movement")
+ inflow_entry = create_transaction(date: Date.current, account: accounts(:connected), amount: -500, kind: "funds_movement")
+ Transfer.create!(inflow_transaction: inflow_entry.transaction, outflow_transaction: outflow_entry.transaction)
+ outflow_entry.transaction.update_column(:kind, "cc_payment")
+
+ assert_not outflow_entry.transaction.reload.payment?
+ end
+
test "all transaction kinds are valid" do
valid_kinds = %w[standard funds_movement cc_payment loan_payment one_time investment_contribution]
diff --git a/test/models/transfer_test.rb b/test/models/transfer_test.rb
index 0b4354edc5..6f39803db4 100644
--- a/test/models/transfer_test.rb
+++ b/test/models/transfer_test.rb
@@ -157,6 +157,81 @@ class TransferTest < ActiveSupport::TestCase
assert_equal "funds_movement", Transfer.kind_for_account(accounts(:depository))
end
+ test "kind_for_account returns funds_movement for investment-to-investment transfers" do
+ assert_equal "funds_movement", Transfer.kind_for_account(accounts(:crypto), from_account: accounts(:investment))
+ assert_equal "funds_movement", Transfer.kind_for_account(accounts(:investment), from_account: accounts(:crypto))
+ end
+
+ test "kind_for_account still returns investment_contribution when the source isn't an investment account" do
+ assert_equal "investment_contribution", Transfer.kind_for_account(accounts(:investment), from_account: accounts(:depository))
+ end
+
+ test "categorizable? is true for loan payment transfers" do
+ outflow_entry = create_transaction(date: Date.current, account: accounts(:depository), amount: 500, kind: "loan_payment")
+ inflow_entry = create_transaction(date: Date.current, account: accounts(:loan), amount: -500, kind: "loan_payment")
+
+ transfer = Transfer.create!(inflow_transaction: inflow_entry.transaction, outflow_transaction: outflow_entry.transaction)
+
+ assert transfer.categorizable?
+ end
+
+ test "categorizable? is true for investment contribution transfers" do
+ outflow_entry = create_transaction(date: Date.current, account: accounts(:depository), amount: 500, kind: "investment_contribution")
+ inflow_entry = create_transaction(date: Date.current, account: accounts(:investment), amount: -500, kind: "investment_contribution")
+
+ transfer = Transfer.create!(inflow_transaction: inflow_entry.transaction, outflow_transaction: outflow_entry.transaction)
+
+ assert transfer.categorizable?
+ end
+
+ test "categorizable? is false for regular funds movement transfers" do
+ outflow_entry = create_transaction(date: Date.current, account: accounts(:depository), amount: 500, kind: "funds_movement")
+ inflow_entry = create_transaction(date: Date.current, account: accounts(:connected), amount: -500, kind: "funds_movement")
+
+ transfer = Transfer.create!(inflow_transaction: inflow_entry.transaction, outflow_transaction: outflow_entry.transaction)
+
+ refute transfer.categorizable?
+ end
+
+ test "categorizable? is false for credit card payment transfers" do
+ outflow_entry = create_transaction(date: Date.current, account: accounts(:depository), amount: 500, kind: "cc_payment")
+ inflow_entry = create_transaction(date: Date.current, account: accounts(:credit_card), amount: -500, kind: "cc_payment")
+
+ transfer = Transfer.create!(inflow_transaction: inflow_entry.transaction, outflow_transaction: outflow_entry.transaction)
+
+ refute transfer.categorizable?
+ end
+
+ test "categorizable? bases on destination account even if the outflow's kind is stale" do
+ # Account::ProviderImportAdapter can reassign an already-matched leg's
+ # kind on a later sync without touching its Transfer. Simulate a stale
+ # "funds_movement" kind left on the outflow of an investment_contribution
+ # transfer -- categorizable? must still key off to_account, not the kind.
+ outflow_entry = create_transaction(date: Date.current, account: accounts(:depository), amount: 500, kind: "investment_contribution")
+ inflow_entry = create_transaction(date: Date.current, account: accounts(:investment), amount: -500, kind: "investment_contribution")
+
+ transfer = Transfer.create!(inflow_transaction: inflow_entry.transaction, outflow_transaction: outflow_entry.transaction)
+ outflow_entry.transaction.update_column(:kind, "funds_movement")
+
+ assert transfer.reload.categorizable?
+ end
+
+ test "categorizable? is false without a destination account" do
+ transfer = transfers(:one)
+ transfer.stubs(:to_account).returns(nil)
+
+ assert_not transfer.categorizable?
+ end
+
+ test "categorizable? matches how Transfer::Creator classifies investment-to-investment transfers" do
+ outflow_entry = create_transaction(date: Date.current, account: accounts(:investment), amount: 500, kind: "funds_movement")
+ inflow_entry = create_transaction(date: Date.current, account: accounts(:crypto), amount: -500, kind: "funds_movement")
+
+ transfer = Transfer.create!(inflow_transaction: inflow_entry.transaction, outflow_transaction: outflow_entry.transaction)
+
+ refute transfer.categorizable?
+ end
+
test "has_source_fee? returns true when source fee present" do
transfer = transfers(:one)
entry = accounts(:depository).entries.create!(name: "Fee", date: Date.current, amount: 5, currency: "USD", entryable: Transaction.new(kind: "standard"))
diff --git a/test/views/transactions/transaction_category_view_test.rb b/test/views/transactions/transaction_category_view_test.rb
new file mode 100644
index 0000000000..c1e5767ccb
--- /dev/null
+++ b/test/views/transactions/transaction_category_view_test.rb
@@ -0,0 +1,92 @@
+require "test_helper"
+
+class Transactions::TransactionCategoryViewTest < ActionView::TestCase
+ include CategoriesHelper
+
+ setup do
+ @family = families(:dylan_family)
+ @user = users(:family_admin)
+ Current.session = Session.create!(user: @user)
+ end
+
+ test "renders the category picker for an investment-contribution outflow leg" do
+ outflow_tx = Transaction.create!(kind: "investment_contribution")
+ Entry.create!(
+ account: accounts(:depository), entryable: outflow_tx,
+ name: "Contribution", amount: 500, currency: "USD", date: Date.today
+ )
+
+ inflow_tx = Transaction.create!(kind: "funds_movement")
+ Entry.create!(
+ account: accounts(:investment), entryable: inflow_tx,
+ name: "Contribution", amount: -500, currency: "USD", date: Date.today
+ )
+
+ Transfer.create!(inflow_transaction: inflow_tx, outflow_transaction: outflow_tx, status: "confirmed")
+
+ html = render(partial: "transactions/transaction_category", locals: {
+ transaction: outflow_tx, variant: "desktop", in_split_group: false
+ })
+
+ assert_includes html, "category_dropdown"
+ end
+
+ test "renders the category picker for an investment-contribution inflow leg" do
+ outflow_tx = Transaction.create!(kind: "investment_contribution")
+ Entry.create!(
+ account: accounts(:depository), entryable: outflow_tx,
+ name: "Contribution", amount: 500, currency: "USD", date: Date.today
+ )
+
+ inflow_tx = Transaction.create!(kind: "funds_movement")
+ Entry.create!(
+ account: accounts(:investment), entryable: inflow_tx,
+ name: "Contribution", amount: -500, currency: "USD", date: Date.today
+ )
+
+ Transfer.create!(inflow_transaction: inflow_tx, outflow_transaction: outflow_tx, status: "confirmed")
+
+ html = render(partial: "transactions/transaction_category", locals: {
+ transaction: inflow_tx, variant: "desktop", in_split_group: false
+ })
+
+ assert_includes html, "category_dropdown"
+ end
+
+ test "renders the transfer badge instead of a picker for a regular funds-movement transfer" do
+ outflow_tx = Transaction.create!(kind: "funds_movement")
+ Entry.create!(
+ account: accounts(:depository), entryable: outflow_tx,
+ name: "Transfer", amount: 500, currency: "USD", date: Date.today
+ )
+
+ inflow_tx = Transaction.create!(kind: "funds_movement")
+ Entry.create!(
+ account: accounts(:connected), entryable: inflow_tx,
+ name: "Transfer", amount: -500, currency: "USD", date: Date.today
+ )
+
+ Transfer.create!(inflow_transaction: inflow_tx, outflow_transaction: outflow_tx, status: "confirmed")
+
+ html = render(partial: "transactions/transaction_category", locals: {
+ transaction: outflow_tx, variant: "desktop", in_split_group: false
+ })
+
+ assert_includes html, transfer_category.display_name
+ assert_not_includes html, "category_dropdown"
+ end
+
+ test "renders the category picker for an unmatched funds-movement leg with no Transfer record" do
+ tx = Transaction.create!(kind: "funds_movement")
+ Entry.create!(
+ account: accounts(:depository), entryable: tx,
+ name: "Unmatched internal movement", amount: 500, currency: "USD", date: Date.today
+ )
+
+ html = render(partial: "transactions/transaction_category", locals: {
+ transaction: tx, variant: "desktop", in_split_group: false
+ })
+
+ assert_includes html, "category_dropdown"
+ end
+end