Files
sure/lib/money.rb
T
f0a0013da9 fix(charts): round series values to the currency's display precision (#3091)
* fix(charts): round chart values to the currency's display precision

Charts are drawn from the serialized amounts, not from the formatted strings,
so a sub-unit residue plotted as a visible move between two points that both
read $0.00, and the trend between them reported a change. Round in
`Series#as_json` so the series values stay exact for insights, goals and the
assistant.

Also hide the percentage when the previous value is zero, since that makes it
infinite.

* fix(charts): round the two payloads that bypass Series#as_json

`NetWorthBreakdownSeriesBuilder` builds its payload by hand, so the reports
chart never went through the rounding added in `Series#as_json` and still
plotted raw amounts: adjacent points printing the same value rendered a
visible move, and the tooltip it inherits reported a change between them.

`Series#trend` had the same gap on the server side. It is rendered right above
the chart by `UI::Account::Chart` and by the reports summary, so an account
going from 0 to a sub-cent residue showed a coloured $0.00 change next to a
flat line.

Rounding the trend can make a previously finite percentage infinite, so guard
the three views that render `percent_formatted` without checking, as
`shared/_trend_change` already does.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix(charts): address net worth review comments

* fix(charts): tighten rounded trend handling

* fix(reports): restore positive sign in print trend

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-authored-by: sure-admin <sure-admin@splashblot.com>
2026-08-22 04:30:23 +02:00

103 lines
3.0 KiB
Ruby

class Money
include Comparable, Arithmetic, Formatting
include ActiveModel::Validations
class ConversionError < StandardError
attr_reader :from_currency, :to_currency, :date
def initialize(from_currency:, to_currency:, date:)
@from_currency = from_currency
@to_currency = to_currency
@date = date
error_message = message || "Couldn't find exchange rate from #{from_currency} to #{to_currency} on #{date}"
super(error_message)
end
end
attr_reader :amount, :currency, :store
validate :source_must_be_of_known_type
class << self
def default_currency
@default ||= Money::Currency.new(:usd)
end
def default_currency=(object)
@default = Money::Currency.new(object)
end
end
def initialize(obj, currency = Money.default_currency, store: ExchangeRate)
@source = obj
@amount = obj.is_a?(Money) ? obj.amount : BigDecimal(obj.to_s)
@currency = obj.is_a?(Money) ? obj.currency : Money::Currency.new(currency)
@store = store
validate!
end
# Exchange money to another currency
# Params:
# other_currency: target currency code (e.g. "USD")
# date: date for historical rates (default: Date.current)
# custom_rate: explicit exchange rate to use (skips lookup if provided, including nil check)
# Priority:
# 1. Use custom_rate if explicitly provided (not nil)
# 2. Look up rate via store.find_or_fetch_rate
# 3. Raise ConversionError if no valid rate available
def exchange_to(other_currency, date: Date.current, custom_rate: nil)
iso_code = currency.iso_code
other_iso_code = Money::Currency.new(other_currency).iso_code
if iso_code == other_iso_code
self
else
# Use custom rate if provided, otherwise look it up
if custom_rate.present?
exchange_rate = custom_rate.to_d
else
exchange_rate = store.find_or_fetch_rate(from: iso_code, to: other_iso_code, date: date)&.rate
end
raise ConversionError.new(from_currency: iso_code, to_currency: other_iso_code, date: date) unless exchange_rate && exchange_rate > 0
Money.new(amount * exchange_rate, other_iso_code)
end
end
# Rounded to the precision `format` actually prints.
def for_display
self.class.new(amount.round(currency.default_precision), currency)
end
def as_json
{ amount: amount, currency: currency.iso_code, formatted: format }.as_json
end
def <=>(other)
raise TypeError, "Money can only be compared with other Money objects except for 0" unless other.is_a?(Money) || other.eql?(0)
if other.is_a?(Numeric)
amount <=> other
else
amount_comparison = amount <=> other.amount
if amount_comparison == 0
currency <=> other.currency
else
amount_comparison
end
end
end
private
def source_must_be_of_known_type
unless @source.is_a?(Money) || @source.is_a?(Numeric) || @source.is_a?(BigDecimal)
errors.add :source, "must be a Money, Numeric, or BigDecimal"
end
end
end