Support uncategorized investments (#593)

* Support uncategorized investments

* FIX sankey id collision

* Fix reports

* Fix hardcoded string and i8n

* FIX plurals

* Remove spending patterns section

add net worth section to reports
This commit is contained in:
soky srm
2026-01-09 19:45:42 +01:00
committed by GitHub
parent 76dc91377c
commit a4f70f4d4a
11 changed files with 306 additions and 212 deletions

View File

@@ -31,10 +31,15 @@ class Category < ApplicationRecord
COLORS = %w[#e99537 #4da568 #6471eb #db5a54 #df4e92 #c44fe9 #eb5429 #61c9ea #805dee #6ad28a]
UNCATEGORIZED_COLOR = "#737373"
OTHER_INVESTMENTS_COLOR = "#e99537"
TRANSFER_COLOR = "#444CE7"
PAYMENT_COLOR = "#db5a54"
TRADE_COLOR = "#e99537"
# Synthetic category name keys for i18n
UNCATEGORIZED_NAME_KEY = "models.category.uncategorized"
OTHER_INVESTMENTS_NAME_KEY = "models.category.other_investments"
class Group
attr_reader :category, :subcategories
@@ -82,12 +87,30 @@ class Category < ApplicationRecord
def uncategorized
new(
name: "Uncategorized",
name: I18n.t(UNCATEGORIZED_NAME_KEY),
color: UNCATEGORIZED_COLOR,
lucide_icon: "circle-dashed"
)
end
def other_investments
new(
name: I18n.t(OTHER_INVESTMENTS_NAME_KEY),
color: OTHER_INVESTMENTS_COLOR,
lucide_icon: "trending-up"
)
end
# Helper to get the localized name for uncategorized
def uncategorized_name
I18n.t(UNCATEGORIZED_NAME_KEY)
end
# Helper to get the localized name for other investments
def other_investments_name
I18n.t(OTHER_INVESTMENTS_NAME_KEY)
end
private
def default_categories
[
@@ -142,6 +165,21 @@ class Category < ApplicationRecord
subcategory? ? "#{parent.name} > #{name}" : name
end
# Predicate: is this the synthetic "Uncategorized" category?
def uncategorized?
!persisted? && name == I18n.t(UNCATEGORIZED_NAME_KEY)
end
# Predicate: is this the synthetic "Other Investments" category?
def other_investments?
!persisted? && name == I18n.t(OTHER_INVESTMENTS_NAME_KEY)
end
# Predicate: is this any synthetic (non-persisted) category?
def synthetic?
uncategorized? || other_investments?
end
private
def category_level_limit
if (subcategory? && parent.subcategory?) || (parent? && subcategory?)

View File

@@ -68,13 +68,22 @@ class IncomeStatement
classification_total = totals.sum(&:total)
uncategorized_category = family.categories.uncategorized
other_investments_category = family.categories.other_investments
category_totals = [ *categories, uncategorized_category ].map do |category|
category_totals = [ *categories, uncategorized_category, other_investments_category ].map do |category|
subcategory = categories.find { |c| c.id == category.parent_id }
parent_category_total = totals.select { |t| t.category_id == category.id }&.sum(&:total) || 0
parent_category_total = if category.uncategorized?
# Regular uncategorized: NULL category_id and NOT uncategorized investment
totals.select { |t| t.category_id.nil? && !t.is_uncategorized_investment }&.sum(&:total) || 0
elsif category.other_investments?
# Other investments: NULL category_id AND is_uncategorized_investment
totals.select { |t| t.category_id.nil? && t.is_uncategorized_investment }&.sum(&:total) || 0
else
totals.select { |t| t.category_id == category.id }&.sum(&:total) || 0
end
children_totals = if category == uncategorized_category
children_totals = if category.synthetic?
0
else
totals.select { |t| t.parent_category_id == category.id }&.sum(&:total) || 0

View File

@@ -15,13 +15,14 @@ class IncomeStatement::Totals
category_id: row["category_id"],
classification: row["classification"],
total: row["total"],
transactions_count: row["transactions_count"]
transactions_count: row["transactions_count"],
is_uncategorized_investment: row["is_uncategorized_investment"]
)
end
end
private
TotalsRow = Data.define(:parent_category_id, :category_id, :classification, :total, :transactions_count)
TotalsRow = Data.define(:parent_category_id, :category_id, :classification, :total, :transactions_count, :is_uncategorized_investment)
def query_sql
ActiveRecord::Base.sanitize_sql_array([
@@ -37,6 +38,7 @@ class IncomeStatement::Totals
category_id,
parent_category_id,
classification,
is_uncategorized_investment,
SUM(total) as total,
SUM(entry_count) as transactions_count
FROM (
@@ -44,7 +46,7 @@ class IncomeStatement::Totals
UNION ALL
#{trades_subquery_sql}
) combined
GROUP BY category_id, parent_category_id, classification;
GROUP BY category_id, parent_category_id, classification, is_uncategorized_investment;
SQL
end
@@ -56,7 +58,8 @@ class IncomeStatement::Totals
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,
COUNT(ae.id) as transactions_count
COUNT(ae.id) as transactions_count,
false as is_uncategorized_investment
FROM (#{@transactions_scope.to_sql}) at
JOIN entries ae ON ae.entryable_id = at.id AND ae.entryable_type = 'Transaction'
JOIN accounts a ON a.id = ae.account_id
@@ -81,7 +84,8 @@ class IncomeStatement::Totals
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,
COUNT(ae.id) as entry_count
COUNT(ae.id) as entry_count,
false as is_uncategorized_investment
FROM (#{@transactions_scope.to_sql}) at
JOIN entries ae ON ae.entryable_id = at.id AND ae.entryable_type = 'Transaction'
JOIN accounts a ON a.id = ae.account_id
@@ -101,14 +105,15 @@ class IncomeStatement::Totals
def trades_subquery_sql
# Get trades for the same family and date range as transactions
# Only include trades that have a category assigned
# Trades without categories appear as "Uncategorized Investments" (separate from regular uncategorized)
<<~SQL
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,
COUNT(ae.id) as entry_count
COUNT(ae.id) as entry_count,
CASE WHEN t.category_id IS NULL THEN true ELSE false END as is_uncategorized_investment
FROM trades t
JOIN entries ae ON ae.entryable_id = t.id AND ae.entryable_type = 'Trade'
JOIN accounts a ON a.id = ae.account_id
@@ -122,8 +127,7 @@ class IncomeStatement::Totals
AND a.status IN ('draft', 'active')
AND ae.excluded = false
AND ae.date BETWEEN :start_date AND :end_date
AND t.category_id IS NOT NULL
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 ae.amount < 0 THEN 'income' ELSE 'expense' END, CASE WHEN t.category_id IS NULL THEN true ELSE false END
SQL
end