Skip to content
This repository was archived by the owner on Jul 27, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion app/controllers/account/trades_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions app/controllers/account/transfers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion app/controllers/account/valuations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/models/account/entry_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def create_transaction_builder
date: date,
amount: amount,
account: account,
currency: currency,
transfer_account_id: transfer_account_id
end
end
5 changes: 3 additions & 2 deletions app/models/account/transaction_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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
Expand Down
14 changes: 11 additions & 3 deletions app/models/account/transfer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions app/views/account/trades/_form.html.erb
Original file line number Diff line number Diff line change
@@ -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| %>
<div class="space-y-4">
Expand All @@ -15,7 +16,7 @@
<%= form.date_field :date, label: true, value: Date.today %>

<div data-trade-form-target="amountInput" hidden>
<%= form.money_field :amount, label: t(".amount"), disable_currency: true %>
<%= form.money_field :amount, label: t(".amount") %>
</div>

<div data-trade-form-target="transferAccountInput" hidden>
Expand All @@ -27,7 +28,7 @@
</div>

<div data-trade-form-target="priceInput">
<%= form.money_field :price, label: t(".price"), disable_currency: true %>
<%= form.money_field :price, label: t(".price"), currency_value_override: "USD", disable_currency: true %>
</div>
</div>

Expand Down
6 changes: 3 additions & 3 deletions app/views/account/transfers/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= styled_form_with model: transfer, class: "space-y-4", data: { turbo_frame: "_top" } do |f| %>
<%= styled_form_with model: transfer, class: "space-y-4", data: { turbo_frame: "_top", controller: "transfer-form" } do |f| %>
<% if transfer.errors.present? %>
<div class="text-red-600 flex items-center gap-2">
<%= lucide_icon "circle-alert", class: "w-5 h-5" %>
Expand Down Expand Up @@ -28,8 +28,8 @@
<section class="space-y-2">
<%= f.collection_select :from_account_id, Current.family.accounts.manual.alphabetically, :id, :name, { prompt: t(".select_account"), label: t(".from") }, required: true %>
<%= f.collection_select :to_account_id, Current.family.accounts.manual.alphabetically, :id, :name, { prompt: t(".select_account"), label: t(".to") }, required: true %>
<%= f.money_field :amount, label: t(".amount"), required: true, hide_currency: true %>
<%= f.date_field :date, value: transfer.date, label: t(".date"), required: true, max: Date.current %>
<%= f.number_field :amount, label: t(".amount"), required: true, min: 0, placeholder: "100", step: 0.00000001 %>
<%= f.date_field :date, value: transfer.date || Date.today, label: t(".date"), required: true, max: Date.current %>
</section>

<section>
Expand Down
2 changes: 1 addition & 1 deletion app/views/account/valuations/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
data: { turbo: false } do |form| %>
<div class="space-y-3">
<%= form.date_field :date, label: true, required: true, value: Date.today, min: Account::Entry.min_supported_date, max: Date.today %>
<%= form.money_field :amount, label: t(".amount"), required: true, default_currency: Current.family.currency %>
<%= form.money_field :amount, label: t(".amount"), required: true %>
</div>

<%= form.submit t(".submit") %>
Expand Down
6 changes: 4 additions & 2 deletions app/views/shared/_money_field.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<%# locals: (form:, amount_method:, currency_method:, **options) %>

<% currency_value = if form.object && form.object.respond_to?(currency_method)
<% currency_value = if options[:currency_value_override].present?
options[:currency_value_override]
elsif form.object && form.object.respond_to?(currency_method)
form.object.public_send(currency_method)
end
currency = Money::Currency.new(currency_value || options[:default_currency] || "USD") %>
Expand Down Expand Up @@ -44,7 +46,7 @@
<div>
<%= form.select currency_method,
currencies_for_select.map(&:iso_code),
{ inline: true },
{ inline: true, selected: currency_value },
{
class: "w-fit pr-5 disabled:text-gray-400 form-field__input",
disabled: options[:disable_currency],
Expand Down
8 changes: 6 additions & 2 deletions test/controllers/account/trades_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Account::TradesControllerTest < ActionDispatch::IntegrationTest
type: "transfer_in",
date: Date.current,
amount: 10,
currency: "USD",
transfer_account_id: from_account.id
}
}
Expand All @@ -46,6 +47,7 @@ class Account::TradesControllerTest < ActionDispatch::IntegrationTest
type: "transfer_out",
date: Date.current,
amount: 10,
currency: "USD",
transfer_account_id: to_account.id
}
}
Expand All @@ -62,7 +64,8 @@ class Account::TradesControllerTest < ActionDispatch::IntegrationTest
account_entry: {
type: "transfer_out",
date: Date.current,
amount: 10
amount: 10,
currency: "USD"
}
}
end
Expand All @@ -80,7 +83,8 @@ class Account::TradesControllerTest < ActionDispatch::IntegrationTest
account_entry: {
type: "interest",
date: Date.current,
amount: 10
amount: 10,
currency: "USD"
}
}
end
Expand Down