Handle investment_contribution classification and update related logic.

This commit is contained in:
luckyPipewrench
2026-01-20 16:50:45 -05:00
parent 45c4bc595b
commit 196d12466f
11 changed files with 169 additions and 40 deletions

View File

@@ -286,7 +286,7 @@ class IncomeStatementTest < ActiveSupport::TestCase
assert_equal Money.new(1050, @family.currency), totals.expense_money # 900 + 150
end
test "excludes investment_contribution transactions from income statement" do
test "includes investment_contribution transactions as expenses in income statement" do
# Create a transfer to investment account (marked as investment_contribution)
investment_contribution = create_transaction(
account: @checking_account,
@@ -298,9 +298,39 @@ class IncomeStatementTest < ActiveSupport::TestCase
income_statement = IncomeStatement.new(@family)
totals = income_statement.totals(date_range: Period.last_30_days.date_range)
# investment_contribution should be excluded (it's in the exclusion list)
assert_equal 4, totals.transactions_count # Only original 4 transactions
# investment_contribution should be included as an expense (visible in cashflow)
assert_equal 5, totals.transactions_count # Original 4 + investment_contribution
assert_equal Money.new(1000, @family.currency), totals.income_money
assert_equal Money.new(900, @family.currency), totals.expense_money
assert_equal Money.new(1900, @family.currency), totals.expense_money # 900 + 1000 investment
end
test "includes provider-imported investment_contribution inflows as expenses" do
# Simulates a 401k contribution that was auto-deducted from payroll
# Provider imports this as an inflow to the investment account (negative amount)
# but it should still appear as an expense in cashflow
investment_account = @family.accounts.create!(
name: "401k",
currency: @family.currency,
balance: 10000,
accountable: Investment.new
)
# Provider-imported contribution shows as inflow (negative amount) to the investment account
# kind is investment_contribution, which should be treated as expense regardless of sign
provider_contribution = create_transaction(
account: investment_account,
amount: -500, # Negative = inflow to account
category: nil,
kind: "investment_contribution"
)
income_statement = IncomeStatement.new(@family)
totals = income_statement.totals(date_range: Period.last_30_days.date_range)
# The provider-imported contribution should appear as an expense
assert_equal 5, totals.transactions_count # Original 4 + provider contribution
assert_equal Money.new(1000, @family.currency), totals.income_money
assert_equal Money.new(1400, @family.currency), totals.expense_money # 900 + 500 (abs of -500)
end
end

View File

@@ -9,7 +9,14 @@ class Transfer::CreatorTest < ActiveSupport::TestCase
@amount = 100
end
test "creates basic transfer" do
test "creates investment contribution when transferring from depository to investment" do
# Ensure the Investment Contributions category exists with all required attributes
investment_category = @family.categories.find_or_create_by!(name: "Investment Contributions") do |c|
c.color = "#0d9488"
c.lucide_icon = "trending-up"
c.classification = "expense"
end
creator = Transfer::Creator.new(
family: @family,
source_account_id: @source_account.id,
@@ -22,17 +29,16 @@ class Transfer::CreatorTest < ActiveSupport::TestCase
assert transfer.persisted?
assert_equal "confirmed", transfer.status
assert transfer.regular_transfer?
assert_equal "transfer", transfer.transfer_type
# Verify outflow transaction (from source account)
# Verify outflow transaction is marked as investment_contribution
outflow = transfer.outflow_transaction
assert_equal "funds_movement", outflow.kind
assert_equal "investment_contribution", outflow.kind
assert_equal @amount, outflow.entry.amount
assert_equal @source_account.currency, outflow.entry.currency
assert_equal "Transfer to #{@destination_account.name}", outflow.entry.name
assert_equal investment_category, outflow.category, "Should auto-assign Investment Contributions category"
# Verify inflow transaction (to destination account)
# Verify inflow transaction (always funds_movement)
inflow = transfer.inflow_transaction
assert_equal "funds_movement", inflow.kind
assert_equal(@amount * -1, inflow.entry.amount)
@@ -40,9 +46,42 @@ class Transfer::CreatorTest < ActiveSupport::TestCase
assert_equal "Transfer from #{@source_account.name}", inflow.entry.name
end
test "creates multi-currency transfer" do
# Use crypto account which has USD currency but different from source
test "creates basic transfer between depository accounts" do
other_depository = @family.accounts.create!(name: "Savings", balance: 1000, currency: "USD", accountable: Depository.new)
creator = Transfer::Creator.new(
family: @family,
source_account_id: @source_account.id,
destination_account_id: other_depository.id,
date: @date,
amount: @amount
)
transfer = creator.create
assert transfer.persisted?
assert_equal "confirmed", transfer.status
assert transfer.regular_transfer?
assert_equal "transfer", transfer.transfer_type
# Verify outflow transaction (depository to depository = funds_movement)
outflow = transfer.outflow_transaction
assert_equal "funds_movement", outflow.kind
assert_nil outflow.category, "Should NOT auto-assign category for regular transfers"
# Verify inflow transaction
inflow = transfer.inflow_transaction
assert_equal "funds_movement", inflow.kind
end
test "creates investment contribution when transferring from depository to crypto" do
# Use crypto account - transfer from depository should be investment_contribution
crypto_account = accounts(:crypto)
investment_category = @family.categories.find_or_create_by!(name: "Investment Contributions") do |c|
c.color = "#0d9488"
c.lucide_icon = "trending-up"
c.classification = "expense"
end
creator = Transfer::Creator.new(
family: @family,
@@ -55,13 +94,12 @@ class Transfer::CreatorTest < ActiveSupport::TestCase
transfer = creator.create
assert transfer.persisted?
assert transfer.regular_transfer?
assert_equal "transfer", transfer.transfer_type
# Verify outflow transaction
# Verify outflow transaction is investment_contribution (not funds_movement)
outflow = transfer.outflow_transaction
assert_equal "funds_movement", outflow.kind
assert_equal "investment_contribution", outflow.kind
assert_equal "Transfer to #{crypto_account.name}", outflow.entry.name
assert_equal investment_category, outflow.category
# Verify inflow transaction with currency handling
inflow = transfer.inflow_transaction
@@ -70,6 +108,32 @@ class Transfer::CreatorTest < ActiveSupport::TestCase
assert_equal crypto_account.currency, inflow.entry.currency
end
test "creates funds_movement for investment to investment transfer (rollover)" do
# Rollover case: investment → investment should stay as funds_movement
other_investment = @family.accounts.create!(name: "IRA", balance: 5000, currency: "USD", accountable: Investment.new)
creator = Transfer::Creator.new(
family: @family,
source_account_id: @destination_account.id, # investment account
destination_account_id: other_investment.id,
date: @date,
amount: @amount
)
transfer = creator.create
assert transfer.persisted?
# Verify outflow is funds_movement (NOT investment_contribution for rollovers)
outflow = transfer.outflow_transaction
assert_equal "funds_movement", outflow.kind
assert_nil outflow.category, "Should NOT auto-assign category for investment→investment transfers"
# Verify inflow
inflow = transfer.inflow_transaction
assert_equal "funds_movement", inflow.kind
end
test "creates loan payment" do
loan_account = accounts(:loan)