diff --git a/app/controllers/account/trades_controller.rb b/app/controllers/account/trades_controller.rb index e0897c9f22f..f57c3089e06 100644 --- a/app/controllers/account/trades_controller.rb +++ b/app/controllers/account/trades_controller.rb @@ -5,7 +5,10 @@ class Account::TradesController < ApplicationController before_action :set_entry, only: :update def new - @entry = @account.entries.account_trades.new(entryable_attributes: {}) + @entry = @account.entries.account_trades.new( + currency: @account.currency, + entryable_attributes: {} + ) end def index diff --git a/app/controllers/account/transfers_controller.rb b/app/controllers/account/transfers_controller.rb index cdcca52dd9e..0aaac5c2d68 100644 --- a/app/controllers/account/transfers_controller.rb +++ b/app/controllers/account/transfers_controller.rb @@ -16,8 +16,7 @@ def create @transfer = Account::Transfer.build_from_accounts from_account, to_account, \ date: transfer_params[:date], - amount: transfer_params[:amount].to_d, - currency: transfer_params[:currency] + amount: transfer_params[:amount].to_d if @transfer.save @transfer.entries.each(&:sync_account_later) diff --git a/app/controllers/account/valuations_controller.rb b/app/controllers/account/valuations_controller.rb index 577dfbf4cc5..35b83b90d83 100644 --- a/app/controllers/account/valuations_controller.rb +++ b/app/controllers/account/valuations_controller.rb @@ -4,7 +4,10 @@ class Account::ValuationsController < ApplicationController before_action :set_account def new - @entry = @account.entries.account_valuations.new(entryable_attributes: {}) + @entry = @account.entries.account_valuations.new( + currency: @account.currency, + entryable_attributes: {} + ) end def create diff --git a/app/models/account/entry_builder.rb b/app/models/account/entry_builder.rb index 0cfa3189404..189acfdd87a 100644 --- a/app/models/account/entry_builder.rb +++ b/app/models/account/entry_builder.rb @@ -40,6 +40,7 @@ def create_transaction_builder date: date, amount: amount, account: account, + currency: currency, transfer_account_id: transfer_account_id end end diff --git a/app/models/account/transaction_builder.rb b/app/models/account/transaction_builder.rb index b6968443c34..6c87d6a44f9 100644 --- a/app/models/account/transaction_builder.rb +++ b/app/models/account/transaction_builder.rb @@ -3,7 +3,7 @@ class Account::TransactionBuilder TYPES = %w[income expense interest transfer_in transfer_out].freeze - attr_accessor :type, :amount, :date, :account, :transfer_account_id + attr_accessor :type, :amount, :date, :account, :currency, :transfer_account_id validates :type, :amount, :date, presence: true validates :type, inclusion: { in: TYPES } @@ -45,8 +45,9 @@ def create_transaction def build_entry(account_id, amount, marked_as_transfer: false) Account::Entry.new \ account_id: account_id, + name: marked_as_transfer ? (amount < 0 ? "Deposit" : "Withdrawal") : "Interest", amount: amount, - currency: account.currency, + currency: currency, date: date, marked_as_transfer: marked_as_transfer, entryable: Account::Transaction.new diff --git a/app/models/account/transfer.rb b/app/models/account/transfer.rb index ec464003586..b919c4b74cf 100644 --- a/app/models/account/transfer.rb +++ b/app/models/account/transfer.rb @@ -49,7 +49,7 @@ def update_entries!(params) end class << self - def build_from_accounts(from_account, to_account, date:, amount:, currency:) + def build_from_accounts(from_account, to_account, date:, amount:) outflow = from_account.entries.build \ amount: amount.abs, currency: from_account.currency, @@ -58,9 +58,17 @@ def build_from_accounts(from_account, to_account, date:, amount:, currency:) marked_as_transfer: true, entryable: Account::Transaction.new + # Attempt to convert the amount to the to_account's currency. If the conversion fails, + # use the original amount. + converted_amount = begin + Money.new(amount.abs, from_account.currency).exchange_to(to_account.currency) + rescue Money::ConversionError + Money.new(amount.abs, from_account.currency) + end + inflow = to_account.entries.build \ - amount: amount.abs * -1, - currency: from_account.currency, + amount: converted_amount.amount * -1, + currency: converted_amount.currency.iso_code, date: date, name: "Transfer from #{from_account.name}", marked_as_transfer: true, diff --git a/app/views/account/trades/_form.html.erb b/app/views/account/trades/_form.html.erb index 41180a203db..a3330927ce6 100644 --- a/app/views/account/trades/_form.html.erb +++ b/app/views/account/trades/_form.html.erb @@ -1,6 +1,7 @@ <%# locals: (entry:) %> <%= styled_form_with data: { turbo_frame: "_top", controller: "trade-form" }, + model: entry, scope: :account_entry, url: account_trades_path(entry.account) do |form| %>