diff --git a/app/apps/investments/tests/test_services.py b/app/apps/investments/tests/test_services.py index 446228fb..174d0494 100644 --- a/app/apps/investments/tests/test_services.py +++ b/app/apps/investments/tests/test_services.py @@ -447,6 +447,34 @@ class InvestmentViewTests(TestCase): response = self.client.get(reverse("investments_charts"), **self.HTMX) self.assertContains(response, "investment-charts-0") + def test_detail_follows_show_exchanged_amounts(self): + brl = self.account.currency + usd = Currency.objects.create( + code="USD", name="Dollar", decimal_places=2, prefix="US$ ", + exchange_currency=brl, + ) + ExchangeRate.objects.create( + from_currency=usd, to_currency=brl, rate=D("5"), + date=timezone.make_aware(datetime.datetime(2026, 1, 1)), + ) + # Tracked by value in USD: movements and the "In Real" card come from USD's chain + account = Account.all_objects.create( + name="Broker", currency=usd, is_investment=True, owner=self.owner + ) + tx(account, IN, "10", datetime.date(2026, 1, 2)) + self.client.force_login(self.owner) + url = reverse("investment_detail", args=[account.id]) + + response = self.client.get(url, **self.HTMX) + self.assertContains(response, 'data-amount="50"', count=3) # 10 USD at 5: ≈ line, card, movement + self.assertContains(response, "In Real") + + usd.show_exchanged_amounts = False + usd.save() + response = self.client.get(url, **self.HTMX) + self.assertNotContains(response, 'data-amount="50"') + self.assertNotContains(response, "In Real") + def test_stranger_cannot_see_detail(self): self.client.force_login(self.stranger) response = self.client.get( diff --git a/app/apps/investments/views.py b/app/apps/investments/views.py index 5f94faf7..ddc18c45 100644 --- a/app/apps/investments/views.py +++ b/app/apps/investments/views.py @@ -209,7 +209,12 @@ def investment_detail(request, account_id): perf = get_holding_performance(account, with_history=True) transactions = ( Transaction.objects.filter(account=account) - .select_related("linked_transaction__account__currency", "category") + .select_related( + "linked_transaction__account__currency", + "category", + "account__currency__exchange_currency", + "account__exchange_currency", + ) .order_by("-date", "-id")[:50] ) income = [] @@ -246,7 +251,8 @@ def investment_detail(request, account_id): { "account": account, "perf": perf, - "home": in_home_currency(perf), + # The valuation currency provides the next hop, so its switch decides + "home": in_home_currency(perf) if perf.currency.show_exchanged_amounts else None, "transactions": transactions, "income": income, "income_entities": list(account.income_entities.all()), diff --git a/app/templates/investments/fragments/detail.html b/app/templates/investments/fragments/detail.html index 401d5446..2f31ad2d 100644 --- a/app/templates/investments/fragments/detail.html +++ b/app/templates/investments/fragments/detail.html @@ -286,11 +286,16 @@ + {# What the other side paid beats a rate conversion, so it's shown first #} {% if p and p.account.currency != a %}
- {% endif %} + {% else %}{% with exchanged=tx.displayed_exchanged_amount %}{% if exchanged %} +
+ +
+ {% endif %}{% endwith %}{% endif %}