Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
12.1 KB
Raw Blame
<%# locals: (investment_metrics:) %>

<% if investment_metrics[:has_investments] %>
  <div class="@container space-y-6">
    <%# Investment Summary Cards %>
    <div class="grid grid-cols-1 @lg:grid-cols-2 @3xl:grid-cols-3 @6xl:grid-cols-5 gap-4">
      <%# Portfolio Value Card %>
      <div class="bg-container-inset rounded-lg p-4">
        <div class="flex items-center gap-2 mb-3">
          <%= icon("briefcase", size: "sm") %>
          <span class="text-sm text-secondary"><%= t("reports.investment_performance.portfolio_value") %></span>
        </div>
        <p class="text-xl font-semibold text-primary whitespace-nowrap privacy-sensitive">
          <%= 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-3">
          <%= icon("trending-up", size: "sm") %>
          <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 privacy-sensitive" style="color: <%= investment_metrics[:unrealized_trend].color %>">
            <span class="whitespace-nowrap"><%= format_money(Money.new(investment_metrics[:unrealized_trend].value, Current.family.currency)) %></span>
            <span class="whitespace-nowrap">(<%= investment_metrics[:unrealized_trend].percent_formatted %>)</span>
          </p>
        <% else %>
          <p class="text-xl font-semibold text-secondary"><%= t("reports.investment_performance.no_data") %></p>
        <% end %>
      </div>

      <%# Period Return Card %>
      <div class="bg-container-inset rounded-lg p-4">
        <div class="flex items-center gap-2 mb-3">
          <%= icon("bar-chart-2", size: "sm") %>
          <span class="text-sm text-secondary"><%= t("reports.investment_performance.period_return") %></span>
        </div>
        <% if investment_metrics[:period_return_trend] %>
          <p class="text-xl font-semibold privacy-sensitive" style="color: <%= investment_metrics[:period_return_trend].color %>">
            <span class="whitespace-nowrap"><%= format_money(Money.new(investment_metrics[:period_return_trend].value, Current.family.currency)) %></span>
            <span class="whitespace-nowrap">(<%= investment_metrics[:period_return_trend].percent_formatted %>)</span>
          </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-3">
          <%= icon("arrow-down-to-line", size: "sm") %>
          <span class="text-sm text-secondary"><%= t("reports.investment_performance.contributions") %></span>
        </div>
        <p class="text-xl font-semibold text-primary whitespace-nowrap privacy-sensitive">
          <%= 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-3">
          <%= icon("arrow-up-from-line", size: "sm") %>
          <span class="text-sm text-secondary"><%= t("reports.investment_performance.withdrawals") %></span>
        </div>
        <p class="text-xl font-semibold text-primary whitespace-nowrap privacy-sensitive">
          <%= 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-lg font-medium"><%= t("reports.investment_performance.top_holdings") %></h4>
        <%= render DS::Table.new(rows: investment_metrics[:top_holdings], inset: true, label: t("reports.investment_performance.top_holdings"), row_class: ->(_holding) { "text-secondary" }) do |table| %>
          <% table.with_column(t("reports.investment_performance.holding")) do |holding| %>
            <div class="flex items-center gap-3">
              <% if (logo = holding.security.display_logo_url).present? %>
                <img src="<%= Setting.transform_brand_fetch_url(logo) %>" alt="<%= holding.ticker %>" class="w-6 h-6 rounded-full">
              <% else %>
                <div class="w-8 h-8 rounded-full bg-container-inset 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>
          <% end %>
          <% table.with_column(t("reports.investment_performance.weight"), numeric: true) do |holding| %>
            <%= number_to_percentage(holding.weight || 0, precision: 1) %>
          <% end %>
          <% table.with_column(t("reports.investment_performance.value"), numeric: true, class: "font-medium text-primary privacy-sensitive") do |holding| %>
            <%= format_money(holding.amount_money) %>
          <% end %>
          <% table.with_column(t("reports.investment_performance.return"), numeric: true) do |holding| %>
            <% if holding.trend %>
              <span style="color: <%= holding.trend.color %>"><%= holding.trend.percent_formatted %></span>
            <% else %>
              <%= t("reports.investment_performance.no_data") %>
            <% end %>
          <% end %>
        <% end %>
      </div>
    <% end %>

    <%# Gains by Tax Treatment %>
    <% if investment_metrics[:gains_by_tax_treatment].present? %>
      <div class="space-y-3">
        <h4 class="text-lg font-medium"><%= 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" data-testid="tax-treatment-gains">
              <div class="flex flex-wrap items-center justify-between gap-x-3 gap-y-2">
                <%= render DS::Pill.new(
                  label: t("accounts.tax_treatments.#{treatment}"),
                  tone: tax_treatment_pill_tone(treatment),
                  marker: false,
                  show_dot: false,
                  title: t("accounts.tax_treatment_descriptions.#{treatment}")
                ) %>
                <span class="text-sm font-semibold text-primary whitespace-nowrap privacy-sensitive">
                  <%= 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 whitespace-nowrap privacy-sensitive"><%= 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 whitespace-nowrap privacy-sensitive"><%= format_money(data[:realized_gain]) %></span>
                </div>
              </div>

              <% if data[:unconverted_count].to_i > 0 %>
                <p class="text-xs text-secondary flex items-center gap-1" data-testid="unconverted-gains-note">
                  <%= icon("info", size: "sm") %>
                  <%= t("reports.investment_performance.unconverted_note", count: data[:unconverted_count], currency: Current.family.currency) %>
                </p>
              <% end %>

              <% 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 %>

              <%= render DS::Disclosure.new(variant: :inline) do |disclosure| %>
                <% disclosure.with_summary_content do %>
                  <span class="flex items-center gap-1 text-xs text-secondary hover:text-primary">
                    <%= disclosure_chevron %>
                    <%= 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) %>)
                  </span>
                <% end %>

                <div class="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| %>
                        <% gain = data[:unrealized_gain_by_holding][holding.id] %>
                        <div class="flex justify-between pl-2">
                          <span class="text-primary"><%= holding.ticker %></span>
                          <span class="whitespace-nowrap <%= gain ? "" : "text-secondary" %>" style="<%= gain ? "color: #{holding.trend.color}" : "" %>">
                            <%= gain ? format_money(gain) : 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 %>
                        <%# Converted in the controller, at the trade's own date: gain.value is carried in the currency the position is held in, and labelling it family currency here was reporting a EUR figure as USD. %>
                        <% amount = data[:realized_gain_by_trade][trade.id] %>
                        <div class="flex justify-between pl-2" data-testid="realized-gain-line">
                          <span class="text-primary"><%= trade.security.ticker %></span>
                          <span class="whitespace-nowrap <%= amount ? "" : "text-secondary" %>" style="<%= amount ? "color: #{gain.color}" : "" %>">
                            <%= amount ? format_money(amount) : 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>
              <% end %>
            </div>
          <% end %>
        </div>
      </div>
    <% end %>

    <%# Investment Accounts Summary %>
    <% if investment_metrics[:accounts].any? %>
      <div class="space-y-3">
        <h4 class="text-lg font-medium"><%= t("reports.investment_performance.accounts") %></h4>

        <div class="grid grid-cols-1 @2xl:grid-cols-2 @5xl:grid-cols-3 gap-3">
          <% investment_metrics[:accounts].each do |account| %>
            <%= render "accounts/balance_card", account: account, href: account_path(account) %>
          <% end %>
        </div>
      </div>
    <% end %>
  </div>
<% end %>