The personal finance app for everyone (by everyone)
Ruby
Show a property's trend in the family currency (#5063)
arnaurodondev43e2e6124d57
diff --git a/app/models/exchange_rate/provided.rb b/app/models/exchange_rate/provided.rbindex 4145ddaf77..f90ad1b98b 100644--- a/app/models/exchange_rate/provided.rb+++ b/app/models/exchange_rate/provided.rb@@ -11,6 +11,15 @@ module ExchangeRate::Provided# Maximum number of days to look back for a cached rate before calling the provider.NEAREST_RATE_LOOKBACK_DAYS = 5+ # The rate #rate_sql would use, without the provider: the stored rate on+ # or before the date, however old, else the earliest stored one after it.+ # Rates of zero or below are skipped (see #usable_rates). Never calls the+ # provider, so it is safe to use while rendering a page; nil when no+ # usable rate is stored at all.+ def find_stored_rate(from:, to:, date: Date.current)+ any_stored_rates([ from ], to: to, date: date)[from]+ end+# A stored rate of zero or below is skipped, as everywhere else (see# #usable_rates), so the provider is still asked; its answer replaces the# unusable row rather than leaving it to be skipped on every request.diff --git a/app/models/property.rb b/app/models/property.rbindex b260c2857a..a0c56fff4d 100644--- a/app/models/property.rb+++ b/app/models/property.rb@@ -62,6 +62,36 @@ class Property < ApplicationRecordTrend.new(current: account.balance_money, previous: first_valuation_amount)end+ # Whether the overview should also show the trend in the family currency:+ # the property is valued in another currency and has a purchase value.+ def family_currency_trend_applicable?+ account.currency != account.family.currency && first_valuation_entry.present?+ end++ # The trend measured in the family currency, so a currency move that hides+ # or invents a gain shows up. The purchase value is converted at the rate on+ # its own date and the current value at today's rate.+ #+ # Only stored exchange rates are used (no provider call while rendering),+ # picked as ExchangeRate.find_stored_rate does: the latest on or before the+ # date, else the earliest after it. Never 1:1; nil when no rate is stored.+ def family_currency_trend+ return nil unless family_currency_trend_applicable?++ purchase = first_valuation_entry+ family_currency = account.family.currency+ previous = convert_with_stored_rate(purchase.amount_money, family_currency, purchase.date)+ current = convert_with_stored_rate(account.balance_money, family_currency, Date.current)+ return nil if previous.nil? || current.nil?++ Trend.new(current: current, previous: previous)+ end++ def reload(*)+ remove_instance_variable(:@first_valuation_entry) if defined?(@first_valuation_entry)+ super+ end+def balance_display_name"market value"end@@ -138,7 +168,24 @@ class Property < ApplicationRecordendprivate+ # Read up to three times per overview render (the card check, the trend+ # and its purchase value), so it is looked up once.+ def first_valuation_entry+ return @first_valuation_entry if defined?(@first_valuation_entry)++ @first_valuation_entry = account.entries.valuations.order(:date).first+ end+def first_valuation_amount- account.entries.valuations.order(:date).first&.amount_money || account.balance_money+ first_valuation_entry&.amount_money || account.balance_money+ end++ def convert_with_stored_rate(money, to_currency, date)+ return money if money.currency.iso_code == to_currency++ rate = ExchangeRate.find_stored_rate(from: money.currency.iso_code, to: to_currency, date: date)+ return nil unless rate++ money.exchange_to(to_currency, custom_rate: rate.rate)endenddiff --git a/app/views/properties/tabs/_overview.html.erb b/app/views/properties/tabs/_overview.html.erbindex e1fd250dc3..4df1161d0b 100644--- a/app/views/properties/tabs/_overview.html.erb+++ b/app/views/properties/tabs/_overview.html.erb@@ -19,6 +19,21 @@</div><% end %>+ <% if account.property.family_currency_trend_applicable? %>+ <div data-testid="family-currency-trend">+ <%= summary_card title: t(".trend_in_currency", currency: account.family.currency) do %>+ <% if (family_trend = account.property.family_currency_trend) %>+ <span class="flex items-center gap-1" style="color: <%= family_trend.color %>">+ <span><%= family_trend.value %></span>+ <span class="text-base font-normal">(<%= family_trend.percent_formatted %>)</span>+ </span>+ <% else %>+ <span class="text-sm font-normal text-secondary"><%= t(".rate_unavailable") %></span>+ <% end %>+ <% end %>+ </div>+ <% end %>+<%= summary_card title: t(".year_built") do %><%= account.property.year_built || t(".unknown") %><% end %>diff --git a/config/locales/views/properties/en.yml b/config/locales/views/properties/en.ymlindex c5b54ac0d3..132b5f1912 100644--- a/config/locales/views/properties/en.yml+++ b/config/locales/views/properties/en.yml@@ -98,6 +98,8 @@ en:market_value: Market Valuepurchase_price: Purchase Pricetrend: Trend+ trend_in_currency: "Trend in %{currency}"+ rate_unavailable: "Exchange rate unavailable"unknown: Unknownyear_built: Year Builtedit_account_details: "Edit account details"diff --git a/test/controllers/properties_controller_test.rb b/test/controllers/properties_controller_test.rbindex b51ca1f42c..092827f411 100644--- a/test/controllers/properties_controller_test.rb+++ b/test/controllers/properties_controller_test.rb@@ -855,7 +855,66 @@ class PropertiesControllerTest < ActionDispatch::IntegrationTestassert_select "form[action*='avm_provider=chimnie']", count: 0end+ test "overview shows the trend in the family currency for a foreign-currency property" do+ purchase_date = 2.years.ago.to_date+ foreign_property_with_value(purchase_date: purchase_date)+ ExchangeRate.create!(from_currency: "USD", to_currency: "EUR", date: purchase_date, rate: 1.0)+ ExchangeRate.create!(from_currency: "USD", to_currency: "EUR", date: Date.current, rate: 0.9)++ get account_path(@account, tab: "overview")++ assert_response :success+ assert_select "[data-testid='family-currency-trend']" do+ assert_select "h4", text: "Trend in EUR"+ assert_select "span", text: /-€1,000/+ assert_select "span", text: "(-1.0%)"+ end+ end++ test "overview shows an infinite family-currency trend from a zero purchase value" do+ purchase_date = 2.years.ago.to_date+ foreign_property_with_value(purchase_date: purchase_date, purchase_amount: 0)+ ExchangeRate.create!(from_currency: "USD", to_currency: "EUR", date: purchase_date, rate: 1.0)+ ExchangeRate.create!(from_currency: "USD", to_currency: "EUR", date: Date.current, rate: 0.9)++ get account_path(@account, tab: "overview")++ assert_response :success+ assert_select "[data-testid='family-currency-trend']" do+ assert_select "span", text: "(+∞)"+ assert_select "span", text: /Infinity|NaN/, count: 0+ end+ end++ test "overview says the rate is unavailable instead of guessing a family-currency trend" do+ foreign_property_with_value(purchase_date: 2.years.ago.to_date)+ ExchangeRate.stubs(:provider).returns(nil)++ get account_path(@account, tab: "overview")++ assert_response :success+ assert_select "[data-testid='family-currency-trend']", text: /Exchange rate unavailable/+ assert_select "[data-testid='family-currency-trend']", text: /€/, count: 0+ end++ test "overview has no family-currency trend when the property is in the family currency" do+ get account_path(@account, tab: "overview")++ assert_response :success+ assert_select "[data-testid='family-currency-trend']", count: 0+ end+private+ def foreign_property_with_value(purchase_date:, purchase_amount: 100_000)+ @account.family.update!(currency: "EUR")+ @account.entries.valuations.destroy_all+ @account.entries.create!(+ date: purchase_date, name: "Original purchase price", amount: purchase_amount, currency: "USD",+ entryable: Valuation.new(kind: "opening_anchor")+ )+ @account.update!(balance: 110_000)+ end+def valuation_data(valuation)Provider::PropertyValuationConcept::PropertyValuation.new(valuation: valuation, currency: "USD", property_type: "single_family_home",diff --git a/test/models/property_test.rb b/test/models/property_test.rbindex 2d7592a737..fa9a82a673 100644--- a/test/models/property_test.rb+++ b/test/models/property_test.rb@@ -65,4 +65,103 @@ class PropertyTest < ActiveSupport::TestCaseassert_equal "realie", property.avm_providerassert_equal Date.current, property.avm_last_synced_onend++ test "family currency trend converts the purchase at its date's rate and the value at today's" do+ purchase_date = 2.years.ago.to_date+ setup_foreign_property(purchase_date: purchase_date, purchase: 100_000, current: 110_000)+ store_rate("USD", "EUR", purchase_date, 1.0)+ store_rate("USD", "EUR", Date.current, 0.9)++ trend = @property.family_currency_trend++ # +10% in USD, but the dollar fell 10% against the euro: about -1% in EUR.+ assert_equal Money.new(100_000, "EUR"), trend.previous+ assert_equal Money.new(99_000, "EUR"), trend.current+ assert_equal Money.new(-1_000, "EUR"), trend.value+ assert_equal(-1.0, trend.percent)+ # The trend in the account's own currency is unchanged.+ assert_equal 10.0, @property.trend.percent+ end++ test "family currency trend uses the latest earlier stored rate, however old" do+ purchase_date = Date.new(2024, 3, 10)+ setup_foreign_property(purchase_date: purchase_date, purchase: 100_000, current: 100_000)+ store_rate("USD", "EUR", purchase_date - 30, 0.7)+ store_rate("USD", "EUR", purchase_date - 10, 0.8)+ store_rate("USD", "EUR", purchase_date + 1, 0.5)+ store_rate("USD", "EUR", Date.current - 2, 0.9)+ ExchangeRate.expects(:provider).never++ trend = @property.family_currency_trend++ assert_equal Money.new(80_000, "EUR"), trend.previous+ assert_equal Money.new(90_000, "EUR"), trend.current+ end++ test "family currency trend uses the earliest later stored rate when none is earlier" do+ purchase_date = Date.new(2024, 3, 10)+ setup_foreign_property(purchase_date: purchase_date, purchase: 100_000, current: 100_000)+ store_rate("USD", "EUR", purchase_date + 3, 0.8)+ store_rate("USD", "EUR", purchase_date + 20, 0.5)+ ExchangeRate.expects(:provider).never++ trend = @property.family_currency_trend++ assert_equal Money.new(80_000, "EUR"), trend.previous+ assert_equal Money.new(50_000, "EUR"), trend.current+ end++ test "family currency trend is unavailable without any stored rate and never fetches one" do+ setup_foreign_property(purchase_date: 2.years.ago.to_date, purchase: 100_000, current: 110_000)+ store_rate("USD", "GBP", Date.current, 0.8)+ ExchangeRate.expects(:provider).never++ assert @property.family_currency_trend_applicable?+ assert_nil @property.family_currency_trend+ end++ test "family currency trend does not apply when the property is in the family currency" do+ @account.family.update!(currency: "USD")++ assert_not @property.family_currency_trend_applicable?+ assert_nil @property.family_currency_trend+ end++ test "the purchase valuation is looked up once and again after a reload" do+ setup_foreign_property(purchase_date: 2.years.ago.to_date, purchase: 100_000, current: 110_000)++ # Uncached, so the request's query cache does not hide a repeat.+ ActiveRecord::Base.uncached do+ assert_queries_match(/FROM "entries"/, count: 1) do+ @property.family_currency_trend_applicable?+ @property.family_currency_trend+ @property.purchase_price+ end+ end++ @account.entries.create!(+ date: 3.years.ago.to_date, name: "Earlier", amount: 90_000, currency: "USD",+ entryable: Valuation.new(kind: "reconciliation")+ )+ assert_equal 90_000, @property.reload.purchase_price.amount+ end++ private+ def setup_foreign_property(purchase_date:, purchase:, current:)+ @account.family.update!(currency: "EUR")+ @account.entries.valuations.destroy_all+ @account.entries.create!(+ date: purchase_date,+ name: "Original purchase price",+ amount: purchase,+ currency: "USD",+ entryable: Valuation.new(kind: "opening_anchor")+ )+ @account.update!(balance: current)+ @property.reload+ end++ def store_rate(from, to, date, rate)+ ExchangeRate.create!(from_currency: from, to_currency: to, date: date, rate: rate)+ endend