Files
sure/app/views/reports/_investment_performance.html.erb
LPW bf9bcae600 Add gains by tax treatment to investment report with grouped subtype dropdown (#701)
* Add tax treatment metrics to reports, forms, and models

- Implement `build_gains_by_tax_treatment` for grouping gains by tax treatment
- Update investment performance view with tax treatment breakdown
- Add tax treatment field to crypto and investments forms
- Introduce `realized_gain_loss` calculation in the Trade model
- Group investment subtypes by region for improved dropdown organization

* Optimize investment performance report by reducing N+1 queries

- Eager-load associations in `build_gains_by_tax_treatment` to minimize database queries
- Preload holdings for realized gain/loss calculations in trades
- Refactor views to standardize "no data" placeholder using translations
- Adjust styling in tax treatment breakdown for improved layout

* Enhance investment performance translations and optimize holdings lookup logic

- Update `holdings_count` and `sells_count` translations to handle pluralization
- Refactor views to use pluralized translation keys with count interpolation
- Optimize preloaded holdings lookup in `Trade` to ensure deterministic selection using `select` and `max_by`

* Refine preloaded holdings logic in `Trade` model

- Treat empty preloaded holdings as authoritative to prevent unnecessary DB queries
- Add explicit fallback behavior for database query when holdings are not preloaded

---------

Co-authored-by: luckyPipewrench <luckypipewrench@proton.me>
2026-01-19 15:44:49 +01:00

223 lines
12 KiB
Plaintext

<%# locals: (investment_metrics:) %>
<% if investment_metrics[:has_investments] %>
<div class="space-y-6">
<h3 class="text-lg font-medium text-primary"><%= t("reports.investment_performance.title") %></h3>
<%# Investment Summary Cards %>
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
<%# Portfolio Value Card %>
<div class="bg-container-inset rounded-lg p-4">
<div class="flex items-center gap-2 mb-2">
<%= icon("briefcase", class: "w-4 h-4 text-secondary") %>
<span class="text-sm text-secondary"><%= t("reports.investment_performance.portfolio_value") %></span>
</div>
<p class="text-xl font-semibold text-primary">
<%= format_money(investment_metrics[:portfolio_value]) %>
</p>
</div>
<%# Total Return Card %>
<div class="bg-container-inset rounded-lg p-4">
<div class="flex items-center gap-2 mb-2">
<%= icon("trending-up", class: "w-4 h-4 text-secondary") %>
<span class="text-sm text-secondary"><%= t("reports.investment_performance.total_return") %></span>
</div>
<% if investment_metrics[:unrealized_trend] %>
<p class="text-xl font-semibold" style="color: <%= investment_metrics[:unrealized_trend].color %>">
<%= format_money(Money.new(investment_metrics[:unrealized_trend].value, Current.family.currency)) %>
(<%= investment_metrics[:unrealized_trend].percent_formatted %>)
</p>
<% else %>
<p class="text-xl font-semibold text-secondary"><%= t("reports.investment_performance.no_data") %></p>
<% end %>
</div>
<%# Period Contributions Card %>
<div class="bg-container-inset rounded-lg p-4">
<div class="flex items-center gap-2 mb-2">
<%= icon("arrow-down-to-line", class: "w-4 h-4 text-secondary") %>
<span class="text-sm text-secondary"><%= t("reports.investment_performance.contributions") %></span>
</div>
<p class="text-xl font-semibold text-primary">
<%= format_money(investment_metrics[:period_contributions]) %>
</p>
</div>
<%# Period Withdrawals Card %>
<div class="bg-container-inset rounded-lg p-4">
<div class="flex items-center gap-2 mb-2">
<%= icon("arrow-up-from-line", class: "w-4 h-4 text-secondary") %>
<span class="text-sm text-secondary"><%= t("reports.investment_performance.withdrawals") %></span>
</div>
<p class="text-xl font-semibold text-primary">
<%= format_money(investment_metrics[:period_withdrawals]) %>
</p>
</div>
</div>
<%# Top Holdings Table %>
<% if investment_metrics[:top_holdings].any? %>
<div class="space-y-3">
<h4 class="text-sm font-medium text-secondary uppercase"><%= t("reports.investment_performance.top_holdings") %></h4>
<div class="bg-container-inset rounded-lg overflow-hidden">
<table class="w-full text-sm">
<thead class="bg-container">
<tr class="text-left text-secondary uppercase text-xs">
<th class="px-4 py-3 font-medium"><%= t("reports.investment_performance.holding") %></th>
<th class="px-4 py-3 font-medium text-right"><%= t("reports.investment_performance.weight") %></th>
<th class="px-4 py-3 font-medium text-right"><%= t("reports.investment_performance.value") %></th>
<th class="px-4 py-3 font-medium text-right"><%= t("reports.investment_performance.return") %></th>
</tr>
</thead>
<tbody class="divide-y divide-primary">
<% investment_metrics[:top_holdings].each do |holding| %>
<tr>
<td class="px-4 py-3">
<div class="flex items-center gap-3">
<% if holding.security.logo_url.present? %>
<img src="<%= holding.security.logo_url %>" alt="<%= holding.ticker %>" class="w-6 h-6 rounded-full">
<% else %>
<div class="w-6 h-6 rounded-full bg-container flex items-center justify-center text-xs font-medium text-secondary">
<%= holding.ticker[0..1] %>
</div>
<% end %>
<div>
<p class="font-medium text-primary"><%= holding.ticker %></p>
<p class="text-xs text-secondary"><%= truncate(holding.name, length: 25) %></p>
</div>
</div>
</td>
<td class="px-4 py-3 text-right text-secondary">
<%= number_to_percentage(holding.weight || 0, precision: 1) %>
</td>
<td class="px-4 py-3 text-right font-medium text-primary">
<%= format_money(holding.amount_money) %>
</td>
<td class="px-4 py-3 text-right">
<% if holding.trend %>
<span style="color: <%= holding.trend.color %>">
<%= holding.trend.percent_formatted %>
</span>
<% else %>
<span class="text-secondary"><%= t("reports.investment_performance.no_data") %></span>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
<% end %>
<%# Gains by Tax Treatment %>
<% if investment_metrics[:gains_by_tax_treatment].present? %>
<div class="space-y-3">
<h4 class="text-sm font-medium text-secondary uppercase"><%= t("reports.investment_performance.gains_by_tax_treatment") %></h4>
<div class="flex flex-wrap gap-4">
<% investment_metrics[:gains_by_tax_treatment].each do |treatment, data| %>
<div class="bg-container-inset rounded-lg p-4 space-y-3 flex-1 min-w-64">
<div class="flex items-center justify-between">
<span class="inline-flex items-center rounded-full px-2 py-0.5 text-xs font-medium <%= tax_treatment_badge_classes(treatment) %>" title="<%= t("accounts.tax_treatment_descriptions.#{treatment}") %>">
<%= t("accounts.tax_treatments.#{treatment}") %>
</span>
<span class="text-sm font-semibold text-primary">
<%= format_money(data[:total_gain]) %>
</span>
</div>
<div class="space-y-2 text-sm">
<div class="flex justify-between">
<span class="text-secondary"><%= t("reports.investment_performance.unrealized_gains") %></span>
<span class="text-primary"><%= format_money(data[:unrealized_gain]) %></span>
</div>
<div class="flex justify-between">
<span class="text-secondary"><%= t("reports.investment_performance.realized_gains") %></span>
<span class="text-primary"><%= format_money(data[:realized_gain]) %></span>
</div>
</div>
<% if treatment == :taxable && data[:realized_gain].amount > 0 %>
<p class="text-xs text-warning flex items-center gap-1">
<%= icon("alert-triangle", size: "sm") %>
<%= t("reports.investment_performance.taxable_realized_note") %>
</p>
<% end %>
<details class="group">
<summary class="cursor-pointer text-xs text-secondary hover:text-primary flex items-center gap-1">
<%= icon "chevron-right", size: "sm", class: "group-open:rotate-90 transition-transform" %>
<%= t("reports.investment_performance.view_details") %>
(<%= t("reports.investment_performance.holdings_count", count: data[:holdings].count) %>, <%= t("reports.investment_performance.sells_count", count: data[:sell_trades].count) %>)
</summary>
<div class="mt-2 space-y-2 text-xs">
<% if data[:holdings].any? %>
<div class="space-y-1">
<p class="text-secondary font-medium"><%= t("reports.investment_performance.holdings") %></p>
<% data[:holdings].first(5).each do |holding| %>
<div class="flex justify-between pl-2">
<span class="text-primary"><%= holding.ticker %></span>
<span class="<%= holding.trend ? "" : "text-secondary" %>" style="<%= holding.trend ? "color: #{holding.trend.color}" : "" %>">
<%= holding.trend ? format_money(Money.new(holding.trend.value, Current.family.currency)) : t("reports.investment_performance.no_data") %>
</span>
</div>
<% end %>
<% if data[:holdings].count > 5 %>
<p class="text-secondary pl-2"><%= t("reports.investment_performance.and_more", count: data[:holdings].count - 5) %></p>
<% end %>
</div>
<% end %>
<% if data[:sell_trades].any? %>
<div class="space-y-1">
<p class="text-secondary font-medium"><%= t("reports.investment_performance.sell_trades") %></p>
<% data[:sell_trades].first(5).each do |trade| %>
<% gain = trade.realized_gain_loss %>
<div class="flex justify-between pl-2">
<span class="text-primary"><%= trade.security.ticker %></span>
<span class="<%= gain ? "" : "text-secondary" %>" style="<%= gain ? "color: #{gain.color}" : "" %>">
<%= gain ? format_money(Money.new(gain.value, Current.family.currency)) : t("reports.investment_performance.no_data") %>
</span>
</div>
<% end %>
<% if data[:sell_trades].count > 5 %>
<p class="text-secondary pl-2"><%= t("reports.investment_performance.and_more", count: data[:sell_trades].count - 5) %></p>
<% end %>
</div>
<% end %>
</div>
</details>
</div>
<% end %>
</div>
</div>
<% end %>
<%# Investment Accounts Summary %>
<% if investment_metrics[:accounts].any? %>
<div class="space-y-3">
<h4 class="text-sm font-medium text-secondary uppercase"><%= t("reports.investment_performance.accounts") %></h4>
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-3">
<% investment_metrics[:accounts].each do |account| %>
<%= link_to account_path(account), class: "bg-container-inset rounded-lg p-4 flex items-center justify-between hover:bg-container-hover transition-colors" do %>
<div class="flex items-center gap-3">
<%= render "accounts/logo", account: account, size: "sm" %>
<div>
<p class="font-medium text-primary text-sm"><%= account.name %></p>
<p class="text-xs text-secondary"><%= account.short_subtype_label %></p>
</div>
</div>
<p class="font-medium text-primary"><%= format_money(account.balance_money) %></p>
<% end %>
<% end %>
</div>
</div>
<% end %>
</div>
<% end %>