The personal finance app for everyone (by everyone)
Ruby
Leave buys, sells and other unit-moving trades out of the Reports activity breakdown (#5014)
arnaurodondev7041f689087b
diff --git a/app/controllers/reports_controller.rb b/app/controllers/reports_controller.rbindex e0d92836fe..fac1e02369 100644--- a/app/controllers/reports_controller.rb+++ b/app/controllers/reports_controller.rb@@ -492,6 +492,13 @@ class ReportsController < ApplicationController.joins(entry: :account).where(accounts: { family_id: Current.family.id, status: [ "draft", "active" ] }).merge(Account.included_in_reports)+ # Buys/sells move value between cash and holdings; keep cash income and fees, decided by label as Portfolio::FlowClassifier does.+ # A qty-0 trade also stays unless its label marks it as a movement (internal, external or transfer).+ .where(+ "trades.investment_activity_label IN (?) OR (trades.qty = 0 AND (trades.investment_activity_label IS NULL OR trades.investment_activity_label NOT IN (?)))",+ Portfolio::FlowClassifier::INCOME_LABELS + Portfolio::FlowClassifier::FEE_LABELS,+ Portfolio::FlowClassifier::INTERNAL_LABELS + Portfolio::FlowClassifier::EXTERNAL_LABELS + [ Portfolio::FlowClassifier::TRANSFER_LABEL ]+ ).where(entries: { entryable_type: "Trade", excluded: false, date: @period.date_range }).includes(entry: :account, category: :parent)trades = exclude_tax_advantaged_accounts(trades)diff --git a/test/controllers/reports_controller_test.rb b/test/controllers/reports_controller_test.rbindex 52527c6243..9c0c48f7b1 100644--- a/test/controllers/reports_controller_test.rb+++ b/test/controllers/reports_controller_test.rb@@ -843,8 +843,8 @@ class ReportsControllerTest < ActionDispatch::IntegrationTestcreate_transaction(account: taxable_account, name: "Taxable dividend", amount: -100, category: taxable_category)create_transaction(account: retirement_account, name: "401k dividend", amount: -200, category: retirement_category)- create_trade(securities(:aapl), account: taxable_account, qty: 1, date: Date.current, price: 100)- create_trade(securities(:aapl), account: retirement_account, qty: 1, date: Date.current, price: 200)+ create_income_trade(account: taxable_account, label: "Dividend", amount: -100)+ create_income_trade(account: retirement_account, label: "Dividend", amount: -200)get reports_path(period_type: :monthly)assert_response :ok@@ -862,6 +862,106 @@ class ReportsControllerTest < ActionDispatch::IntegrationTestassert_match(/\$100\.00/, other_investments_rows.first.text)end+ # Selling a holding turns securities into cash; it is not income, and the+ # Total Income card (IncomeStatement::Totals) already leaves trades out.+ test "activity breakdown leaves trades out of income and expenses" do+ @family.accounts.each { |account| account.entries.destroy_all }++ salary = @family.categories.create!(name: "Reports Salary", color: "#111111")+ brokerage = @family.accounts.create!(+ owner: @user,+ name: "Reports Brokerage",+ balance: 0,+ currency: "USD",+ accountable: Investment.new(subtype: "brokerage")+ )++ create_transaction(account: accounts(:depository), name: "Paycheck", amount: -100, category: salary)+ create_trade(securities(:aapl), account: brokerage, qty: -10, date: Date.current, price: 100)+ create_trade(securities(:aapl), account: brokerage, qty: 5, date: Date.current, price: 100)++ get reports_path(period_type: :monthly)+ assert_response :ok++ assert_select "tr[data-category='category-other_investments']", count: 0+ assert_select "tr[data-category='category-#{salary.id}']", text: /\$100\.00/+ income_total = css_select("span.text-success.privacy-sensitive").map { |node| node.text.strip }+ assert_includes income_total, "$100.00"+ assert_not_includes income_total, "$1,100.00"+ end++ # Manually entered dividends, interest and fees are qty-0 trades+ # (Trade::CreateForm#create_income_trade) and stay under Other Investments.+ test "activity breakdown keeps manually entered dividends, interest and fees" do+ @family.accounts.each { |account| account.entries.destroy_all }+ brokerage = @family.accounts.create!(owner: @user, name: "Reports Brokerage", balance: 0, currency: "USD",+ accountable: Investment.new(subtype: "brokerage"))+ create_transaction(account: accounts(:depository), name: "Coffee", amount: 3) # the page needs a transaction+ create_income_trade(account: brokerage, label: "Dividend", amount: -42.17)+ create_income_trade(account: brokerage, label: "Interest", amount: -13.37)+ create_income_trade(account: brokerage, label: "Fee", amount: 7.77)++ get reports_path(period_type: :monthly)+ assert_response :ok++ rows = css_select("tr[data-category='category-other_investments']").map(&:text).join+ [ "$55.54", "$7.77" ].each { |amount| assert_includes rows, amount }+ end++ # The activity-label editor relabels a trade without changing its quantity.+ test "activity breakdown keeps a trade relabelled as a dividend" do+ @family.accounts.each { |account| account.entries.destroy_all }+ brokerage = @family.accounts.create!(owner: @user, name: "Reports Brokerage", balance: 0, currency: "USD",+ accountable: Investment.new(subtype: "brokerage"))+ create_transaction(account: accounts(:depository), name: "Coffee", amount: 3)+ create_trade(securities(:aapl), account: brokerage, qty: -1, date: Date.current, price: 42.17)+ .trade.update!(investment_activity_label: "Dividend")++ get reports_path(period_type: :monthly)+ assert_response :ok++ assert_includes css_select("tr[data-category='category-other_investments']").map(&:text).join, "$42.17"+ end++ # The rule is by label, not only Buy and Sell: a trade that moves units+ # stays out unless it is labelled as income or a fee.+ test "activity breakdown leaves out trades with other labels or none" do+ @family.accounts.each { |account| account.entries.destroy_all }+ brokerage = @family.accounts.create!(owner: @user, name: "Reports Brokerage", balance: 0, currency: "USD",+ accountable: Investment.new(subtype: "brokerage"))+ create_transaction(account: accounts(:depository), name: "Coffee", amount: 3)+ [ "Contribution", "Withdrawal", "Reinvestment", "Transfer", "Exchange", "Other", nil ].each_with_index do |label, i|+ create_trade(securities(:aapl), account: brokerage, qty: i.even? ? 2 : -2, date: Date.current, price: 50)+ .trade.update!(investment_activity_label: label)+ end++ get reports_path(period_type: :monthly)+ assert_response :ok++ assert_select "tr[data-category='category-other_investments']", count: 0+ end++ # A cash-only trade relabelled as a movement (e.g. through the API) is not+ # income either; an unlabelled or "Other" one stays as before.+ test "activity breakdown leaves out qty-0 trades labelled as movements" do+ @family.accounts.each { |account| account.entries.destroy_all }+ brokerage = @family.accounts.create!(owner: @user, name: "Reports Brokerage", balance: 0, currency: "USD",+ accountable: Investment.new(subtype: "brokerage"))+ create_transaction(account: accounts(:depository), name: "Coffee", amount: 3)+ [ "Transfer", "Sweep In", "Contribution", "Withdrawal", "Buy" ].each do |label|+ create_income_trade(account: brokerage, label: "Dividend", amount: -100).trade.update!(investment_activity_label: label)+ end+ create_income_trade(account: brokerage, label: "Dividend", amount: -12.34).trade.update!(investment_activity_label: "Other")+ create_income_trade(account: brokerage, label: "Dividend", amount: -10).trade.update!(investment_activity_label: nil)++ get reports_path(period_type: :monthly)+ assert_response :ok++ rows = css_select("tr[data-category='category-other_investments']").map(&:text).join+ assert_includes rows, "$22.34"+ assert_not_includes rows, "$522.34"+ end+# The summary leaves out an amount in a currency with no rate; the# breakdown under it and the CSV export must leave it out too, not count# ₩130,000 as $130,000.@@ -1588,4 +1688,12 @@ class ReportsControllerTest < ActionDispatch::IntegrationTestcreate_trade(security, account: account, qty: -2, date: date, price: 150, currency: "EUR")endend++ # A cash-only trade, as Trade::CreateForm#create_income_trade records a+ # manually entered dividend, interest payment or fee.+ def create_income_trade(account:, label:, amount:)+ account.entries.create!(name: label, date: Date.current, amount: amount, currency: "USD",+ entryable: Trade.new(qty: 0, price: 0, currency: "USD", security: securities(:aapl),+ investment_activity_label: label))+ endend