Merge pull request #718 from luckyPipewrench/plug-black-hole

Fix "black hole" - Make investment contributions visible in cashflow with auto-categorization
This commit is contained in:
soky srm
2026-01-21 09:21:45 +01:00
committed by GitHub
19 changed files with 189 additions and 44 deletions

View File

@@ -32,6 +32,13 @@
data: { turbo_frame: :modal }) %>
<% end %>
<% end %>
<% menu.with_item(
variant: "link",
text: t("accounts.show.activity.new_transfer"),
icon: "arrow-right-left",
href: new_transfer_path(from_account_id: account.id),
data: { turbo_frame: :modal }) %>
<% end %>
</div>

View File

@@ -5,6 +5,7 @@ class TransfersController < ApplicationController
def new
@transfer = Transfer.new
@from_account_id = params[:from_account_id]
end
def show

View File

@@ -150,13 +150,15 @@ class Account::ProviderImportAdapter
# Auto-set kind for internal movements and contributions
auto_kind = nil
auto_category = nil
if Transaction::INTERNAL_MOVEMENT_LABELS.include?(detected_label)
auto_kind = "funds_movement"
elsif detected_label == "Contribution"
auto_kind = "investment_contribution"
auto_category = account.family.investment_contributions_category
end
# Set investment activity label and kind if detected
# Set investment activity label, kind, and category if detected
if entry.entryable.is_a?(Transaction)
if detected_label.present? && entry.transaction.investment_activity_label.blank?
entry.transaction.assign_attributes(investment_activity_label: detected_label)
@@ -165,6 +167,10 @@ class Account::ProviderImportAdapter
if auto_kind.present?
entry.transaction.assign_attributes(kind: auto_kind)
end
if auto_category.present? && entry.transaction.category_id.blank?
entry.transaction.assign_attributes(category: auto_category)
end
end
entry.save!

View File

@@ -35,9 +35,10 @@ class Category < ApplicationRecord
PAYMENT_COLOR = "#db5a54"
TRADE_COLOR = "#e99537"
# Synthetic category name keys for i18n
# Category name keys for i18n
UNCATEGORIZED_NAME_KEY = "models.category.uncategorized"
OTHER_INVESTMENTS_NAME_KEY = "models.category.other_investments"
INVESTMENT_CONTRIBUTIONS_NAME_KEY = "models.category.investment_contributions"
class Group
attr_reader :category, :subcategories
@@ -113,6 +114,11 @@ class Category < ApplicationRecord
I18n.t(OTHER_INVESTMENTS_NAME_KEY)
end
# Helper to get the localized name for investment contributions
def investment_contributions_name
I18n.t(INVESTMENT_CONTRIBUTIONS_NAME_KEY)
end
private
def default_categories
[
@@ -137,7 +143,7 @@ class Category < ApplicationRecord
[ "Services", "#7c3aed", "briefcase", "expense" ],
[ "Fees", "#6b7280", "receipt", "expense" ],
[ "Savings & Investments", "#059669", "piggy-bank", "expense" ],
[ "Investment Contributions", "#0d9488", "trending-up", "expense" ]
[ investment_contributions_name, "#0d9488", "trending-up", "expense" ]
]
end
end

View File

@@ -79,6 +79,12 @@ class Family < ApplicationRecord
@income_statement ||= IncomeStatement.new(self)
end
# Returns the Investment Contributions category for this family, or nil if not found.
# This is a bootstrapped category used for auto-categorizing transfers to investment accounts.
def investment_contributions_category
categories.find_by(name: Category.investment_contributions_name)
end
def investment_statement
@investment_statement ||= InvestmentStatement.new(self)
end

View File

@@ -35,8 +35,8 @@ class IncomeStatement::CategoryStats
SELECT
c.id as category_id,
date_trunc(:interval, ae.date) as period,
CASE WHEN ae.amount < 0 THEN 'income' ELSE 'expense' END as classification,
SUM(ae.amount * COALESCE(er.rate, 1)) as total
CASE WHEN t.kind = 'investment_contribution' THEN 'expense' WHEN ae.amount < 0 THEN 'income' ELSE 'expense' END as classification,
SUM(CASE WHEN t.kind = 'investment_contribution' THEN ABS(ae.amount * COALESCE(er.rate, 1)) ELSE ae.amount * COALESCE(er.rate, 1) END) as total
FROM transactions t
JOIN entries ae ON ae.entryable_id = t.id AND ae.entryable_type = 'Transaction'
JOIN accounts a ON a.id = ae.account_id
@@ -47,11 +47,11 @@ class IncomeStatement::CategoryStats
er.to_currency = :target_currency
)
WHERE a.family_id = :family_id
AND t.kind NOT IN ('funds_movement', 'one_time', 'cc_payment', 'investment_contribution')
AND t.kind NOT IN ('funds_movement', 'one_time', 'cc_payment')
AND ae.excluded = false
AND (t.extra -> 'simplefin' ->> 'pending')::boolean IS DISTINCT FROM true
AND (t.extra -> 'plaid' ->> 'pending')::boolean IS DISTINCT FROM true
GROUP BY c.id, period, CASE WHEN ae.amount < 0 THEN 'income' ELSE 'expense' END
GROUP BY c.id, period, CASE WHEN t.kind = 'investment_contribution' THEN 'expense' WHEN ae.amount < 0 THEN 'income' ELSE 'expense' END
)
SELECT
category_id,

View File

@@ -33,8 +33,8 @@ class IncomeStatement::FamilyStats
WITH period_totals AS (
SELECT
date_trunc(:interval, ae.date) as period,
CASE WHEN ae.amount < 0 THEN 'income' ELSE 'expense' END as classification,
SUM(ae.amount * COALESCE(er.rate, 1)) as total
CASE WHEN t.kind = 'investment_contribution' THEN 'expense' WHEN ae.amount < 0 THEN 'income' ELSE 'expense' END as classification,
SUM(CASE WHEN t.kind = 'investment_contribution' THEN ABS(ae.amount * COALESCE(er.rate, 1)) ELSE ae.amount * COALESCE(er.rate, 1) END) as total
FROM transactions t
JOIN entries ae ON ae.entryable_id = t.id AND ae.entryable_type = 'Transaction'
JOIN accounts a ON a.id = ae.account_id
@@ -44,11 +44,11 @@ class IncomeStatement::FamilyStats
er.to_currency = :target_currency
)
WHERE a.family_id = :family_id
AND t.kind NOT IN ('funds_movement', 'one_time', 'cc_payment', 'investment_contribution')
AND t.kind NOT IN ('funds_movement', 'one_time', 'cc_payment')
AND ae.excluded = false
AND (t.extra -> 'simplefin' ->> 'pending')::boolean IS DISTINCT FROM true
AND (t.extra -> 'plaid' ->> 'pending')::boolean IS DISTINCT FROM true
GROUP BY period, CASE WHEN ae.amount < 0 THEN 'income' ELSE 'expense' END
GROUP BY period, CASE WHEN t.kind = 'investment_contribution' THEN 'expense' WHEN ae.amount < 0 THEN 'income' ELSE 'expense' END
)
SELECT
classification,

View File

@@ -56,8 +56,8 @@ class IncomeStatement::Totals
SELECT
c.id as category_id,
c.parent_id as parent_category_id,
CASE WHEN ae.amount < 0 THEN 'income' ELSE 'expense' END as classification,
ABS(SUM(ae.amount * COALESCE(er.rate, 1))) as total,
CASE WHEN at.kind = 'investment_contribution' THEN 'expense' WHEN ae.amount < 0 THEN 'income' ELSE 'expense' END as classification,
ABS(SUM(CASE WHEN at.kind = 'investment_contribution' THEN ABS(ae.amount * COALESCE(er.rate, 1)) ELSE ae.amount * COALESCE(er.rate, 1) END)) as total,
COUNT(ae.id) as transactions_count,
false as is_uncategorized_investment
FROM (#{@transactions_scope.to_sql}) at
@@ -69,11 +69,11 @@ class IncomeStatement::Totals
er.from_currency = ae.currency AND
er.to_currency = :target_currency
)
WHERE at.kind NOT IN ('funds_movement', 'one_time', 'cc_payment', 'investment_contribution')
WHERE at.kind NOT IN ('funds_movement', 'one_time', 'cc_payment')
AND ae.excluded = false
AND a.family_id = :family_id
AND a.status IN ('draft', 'active')
GROUP BY c.id, c.parent_id, CASE WHEN ae.amount < 0 THEN 'income' ELSE 'expense' END;
GROUP BY c.id, c.parent_id, CASE WHEN at.kind = 'investment_contribution' THEN 'expense' WHEN ae.amount < 0 THEN 'income' ELSE 'expense' END;
SQL
end
@@ -82,8 +82,8 @@ class IncomeStatement::Totals
SELECT
c.id as category_id,
c.parent_id as parent_category_id,
CASE WHEN ae.amount < 0 THEN 'income' ELSE 'expense' END as classification,
ABS(SUM(ae.amount * COALESCE(er.rate, 1))) as total,
CASE WHEN at.kind = 'investment_contribution' THEN 'expense' WHEN ae.amount < 0 THEN 'income' ELSE 'expense' END as classification,
ABS(SUM(CASE WHEN at.kind = 'investment_contribution' THEN ABS(ae.amount * COALESCE(er.rate, 1)) ELSE ae.amount * COALESCE(er.rate, 1) END)) as total,
COUNT(ae.id) as entry_count,
false as is_uncategorized_investment
FROM (#{@transactions_scope.to_sql}) at
@@ -95,7 +95,7 @@ class IncomeStatement::Totals
er.from_currency = ae.currency AND
er.to_currency = :target_currency
)
WHERE at.kind NOT IN ('funds_movement', 'one_time', 'cc_payment', 'investment_contribution')
WHERE at.kind NOT IN ('funds_movement', 'one_time', 'cc_payment')
AND (
at.investment_activity_label IS NULL
OR at.investment_activity_label NOT IN ('Transfer', 'Sweep In', 'Sweep Out', 'Exchange')
@@ -103,7 +103,7 @@ class IncomeStatement::Totals
AND ae.excluded = false
AND a.family_id = :family_id
AND a.status IN ('draft', 'active')
GROUP BY c.id, c.parent_id, CASE WHEN ae.amount < 0 THEN 'income' ELSE 'expense' END
GROUP BY c.id, c.parent_id, CASE WHEN at.kind = 'investment_contribution' THEN 'expense' WHEN ae.amount < 0 THEN 'income' ELSE 'expense' END
SQL
end

View File

@@ -49,8 +49,8 @@ class Transaction::Search
Rails.cache.fetch("transaction_search_totals/#{cache_key_base}") do
result = transactions_scope
.select(
"COALESCE(SUM(CASE WHEN entries.amount >= 0 AND transactions.kind NOT IN ('funds_movement', 'cc_payment', 'investment_contribution') THEN ABS(entries.amount * COALESCE(er.rate, 1)) ELSE 0 END), 0) as expense_total",
"COALESCE(SUM(CASE WHEN entries.amount < 0 AND transactions.kind NOT IN ('funds_movement', 'cc_payment', 'investment_contribution') THEN ABS(entries.amount * COALESCE(er.rate, 1)) ELSE 0 END), 0) as income_total",
"COALESCE(SUM(CASE WHEN transactions.kind = 'investment_contribution' THEN ABS(entries.amount * COALESCE(er.rate, 1)) WHEN entries.amount >= 0 AND transactions.kind NOT IN ('funds_movement', 'cc_payment') THEN ABS(entries.amount * COALESCE(er.rate, 1)) ELSE 0 END), 0) as expense_total",
"COALESCE(SUM(CASE WHEN entries.amount < 0 AND transactions.kind NOT IN ('funds_movement', 'cc_payment', 'investment_contribution') THEN ABS(entries.amount * COALESCE(er.rate, 1)) ELSE 0 END), 0) as income_total",
"COUNT(entries.id) as transactions_count"
)
.joins(
@@ -100,14 +100,14 @@ class Transaction::Search
if parent_category_ids.empty?
query = query.left_joins(:category).where(
"categories.name IN (?) OR (
categories.id IS NULL AND (transactions.kind NOT IN ('funds_movement', 'cc_payment', 'investment_contribution'))
categories.id IS NULL AND (transactions.kind NOT IN ('funds_movement', 'cc_payment'))
)",
categories
)
else
query = query.left_joins(:category).where(
"categories.name IN (?) OR categories.parent_id IN (?) OR (
categories.id IS NULL AND (transactions.kind NOT IN ('funds_movement', 'cc_payment', 'investment_contribution'))
categories.id IS NULL AND (transactions.kind NOT IN ('funds_movement', 'cc_payment'))
)",
categories, parent_category_ids
)
@@ -125,8 +125,11 @@ class Transaction::Search
return query if types.sort == [ "expense", "income", "transfer" ]
transfer_condition = "transactions.kind IN ('funds_movement', 'cc_payment', 'loan_payment')"
expense_condition = "entries.amount >= 0"
income_condition = "entries.amount <= 0"
# investment_contribution is always an expense regardless of amount sign
# (handles both manual outflows and provider-imported inflows like 401k contributions)
investment_contribution_condition = "transactions.kind = 'investment_contribution'"
expense_condition = "(entries.amount >= 0 OR #{investment_contribution_condition})"
income_condition = "(entries.amount <= 0 AND NOT #{investment_contribution_condition})"
condition = case types.sort
when [ "transfer" ]

View File

@@ -27,18 +27,25 @@ class Transfer::Creator
def outflow_transaction
name = "#{name_prefix} to #{destination_account.name}"
kind = outflow_transaction_kind
Transaction.new(
kind: outflow_transaction_kind,
kind: kind,
category: (investment_contributions_category if kind == "investment_contribution"),
entry: source_account.entries.build(
amount: amount.abs,
currency: source_account.currency,
date: date,
name: name,
user_modified: true, # Protect from provider sync claiming this entry
)
)
end
def investment_contributions_category
source_account.family.investment_contributions_category
end
def inflow_transaction
name = "#{name_prefix} from #{source_account.name}"
@@ -49,6 +56,7 @@ class Transfer::Creator
currency: destination_account.currency,
date: date,
name: name,
user_modified: true, # Protect from provider sync claiming this entry
)
)
end
@@ -70,11 +78,21 @@ class Transfer::Creator
"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?
end
def name_prefix
if destination_account.liability?
"Payment"

View File

@@ -11,8 +11,8 @@
</section>
<section class="space-y-2">
<%= f.collection_select :from_account_id, Current.family.accounts.manual.alphabetically, :id, :name, { prompt: t(".select_account"), label: t(".from") }, required: true %>
<%= f.collection_select :to_account_id, Current.family.accounts.manual.alphabetically, :id, :name, { prompt: t(".select_account"), label: t(".to") }, required: true %>
<%= f.collection_select :from_account_id, Current.family.accounts.alphabetically, :id, :name, { prompt: t(".select_account"), label: t(".from"), selected: @from_account_id }, required: true %>
<%= f.collection_select :to_account_id, Current.family.accounts.alphabetically, :id, :name, { prompt: t(".select_account"), label: t(".to") }, required: true %>
<%= f.number_field :amount, label: t(".amount"), required: true, min: 0, placeholder: "100", step: 0.00000001 %>
<%= f.date_field :date, value: transfer.inflow_transaction&.entry&.date || Date.current, label: t(".date"), required: true, max: Date.current %>
</section>