2024-06-20 13:32:44 -04:00
|
|
|
class Account::Transfer < ApplicationRecord
|
2024-07-01 10:49:43 -04:00
|
|
|
has_many :entries, dependent: :nullify
|
2024-06-19 06:52:08 -04:00
|
|
|
|
2024-06-21 23:04:15 +02:00
|
|
|
validate :net_zero_flows, if: :single_currency_transfer?
|
|
|
|
validate :transaction_count, :from_different_accounts, :all_transactions_marked
|
2024-06-19 06:52:08 -04:00
|
|
|
|
2024-07-01 10:49:43 -04:00
|
|
|
def date
|
|
|
|
outflow_transaction&.date
|
|
|
|
end
|
|
|
|
|
|
|
|
def amount_money
|
2024-08-01 12:10:30 -04:00
|
|
|
entries.first&.amount_money&.abs || Money.new(0)
|
2024-07-01 10:49:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def from_name
|
2024-08-13 01:38:58 +01:00
|
|
|
outflow_transaction&.account&.name || I18n.t("account/transfer.from_fallback_name")
|
2024-07-01 10:49:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_name
|
2024-08-13 01:38:58 +01:00
|
|
|
inflow_transaction&.account&.name || I18n.t("account/transfer.to_fallback_name")
|
2024-07-01 10:49:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def name
|
2024-08-13 01:38:58 +01:00
|
|
|
I18n.t("account/transfer.name", from_account: from_name, to_account: to_name)
|
2024-07-01 10:49:43 -04:00
|
|
|
end
|
|
|
|
|
2024-06-19 06:52:08 -04:00
|
|
|
def inflow_transaction
|
2024-07-01 10:49:43 -04:00
|
|
|
entries.find { |e| e.inflow? }
|
2024-06-19 06:52:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def outflow_transaction
|
2024-07-01 10:49:43 -04:00
|
|
|
entries.find { |e| e.outflow? }
|
2024-06-19 06:52:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy_and_remove_marks!
|
|
|
|
transaction do
|
2024-07-01 10:49:43 -04:00
|
|
|
entries.each do |e|
|
|
|
|
e.update! marked_as_transfer: false
|
2024-06-19 06:52:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
destroy!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class << self
|
|
|
|
def build_from_accounts(from_account, to_account, date:, amount:, currency:, name:)
|
2024-07-01 10:49:43 -04:00
|
|
|
outflow = from_account.entries.build \
|
|
|
|
amount: amount.abs,
|
|
|
|
currency: currency,
|
|
|
|
date: date,
|
|
|
|
name: name,
|
|
|
|
marked_as_transfer: true,
|
|
|
|
entryable: Account::Transaction.new
|
|
|
|
|
|
|
|
inflow = to_account.entries.build \
|
|
|
|
amount: amount.abs * -1,
|
|
|
|
currency: currency,
|
|
|
|
date: date,
|
|
|
|
name: name,
|
|
|
|
marked_as_transfer: true,
|
|
|
|
entryable: Account::Transaction.new
|
2024-06-19 06:52:08 -04:00
|
|
|
|
2024-07-01 10:49:43 -04:00
|
|
|
new entries: [ outflow, inflow ]
|
2024-06-19 06:52:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2024-06-21 23:04:15 +02:00
|
|
|
def single_currency_transfer?
|
2024-07-01 10:49:43 -04:00
|
|
|
entries.map { |e| e.currency }.uniq.size == 1
|
2024-06-21 23:04:15 +02:00
|
|
|
end
|
|
|
|
|
2024-06-19 06:52:08 -04:00
|
|
|
def transaction_count
|
2024-07-01 10:49:43 -04:00
|
|
|
unless entries.size == 2
|
2024-08-13 01:38:58 +01:00
|
|
|
# i18n-tasks-use t('activerecord.errors.models.account/transfer.attributes.entries.must_have_exactly_2_entries')
|
|
|
|
errors.add :entries, :must_have_exactly_2_entries
|
2024-06-19 06:52:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def from_different_accounts
|
2024-07-01 10:49:43 -04:00
|
|
|
accounts = entries.map { |e| e.account_id }.uniq
|
2024-08-13 01:38:58 +01:00
|
|
|
# i18n-tasks-use t('activerecord.errors.models.account/transfer.attributes.entries.must_be_from_different_accounts')
|
|
|
|
errors.add :entries, :must_be_from_different_accounts if accounts.size < entries.size
|
2024-06-19 06:52:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def net_zero_flows
|
2024-07-01 10:49:43 -04:00
|
|
|
unless entries.sum(&:amount).zero?
|
2024-08-13 01:38:58 +01:00
|
|
|
# i18n-tasks-use t('activerecord.errors.models.account/transfer.attributes.entries.must_have_an_inflow_and_outflow_that_net_to_zero')
|
|
|
|
errors.add :entries, :must_have_an_inflow_and_outflow_that_net_to_zero
|
2024-06-19 06:52:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def all_transactions_marked
|
2024-07-01 10:49:43 -04:00
|
|
|
unless entries.all?(&:marked_as_transfer)
|
2024-08-13 01:38:58 +01:00
|
|
|
# i18n-tasks-use t('activerecord.errors.models.account/transfer.attributes.entries.must_be_marked_as_transfer')
|
|
|
|
errors.add :entries, :must_be_marked_as_transfer
|
2024-06-19 06:52:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|