mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-05 13:35:21 +02:00
Fix transfers and form currencies (#1477)
This commit is contained in:
parent
fcb95207d7
commit
81d604f3d4
11 changed files with 41 additions and 19 deletions
|
@ -40,6 +40,7 @@ class Account::EntryBuilder
|
|||
date: date,
|
||||
amount: amount,
|
||||
account: account,
|
||||
currency: currency,
|
||||
transfer_account_id: transfer_account_id
|
||||
end
|
||||
end
|
||||
|
|
|
@ -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 @@ class Account::TransactionBuilder
|
|||
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
|
||||
|
|
|
@ -49,7 +49,7 @@ class Account::Transfer < ApplicationRecord
|
|||
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 @@ class Account::Transfer < ApplicationRecord
|
|||
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,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue