mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-24 15:49:39 +02:00
* Initial entryable models * Update transfer and tests * Update transaction controllers and tests * Update sync process to use new entries model * Get dashboard working again * Update transfers, imports, and accounts to use Account::Entry * Update system tests * Consolidate transaction management into entries controller * Add permitted partial key helper * Move account transactions list to entries controller * Delegate transaction entries search * Move transfer relation to entry * Update bulk transaction management flows to use entries * Remove test code * Test fix attempt * Update demo data script * Consolidate remaining transaction partials to entries * Consolidate valuations controller to entries controller * Lint fix * Remove unused files, additional cleanup * Add back valuation creation * Make migrations fully reversible * Stale routes cleanup * Migrations reversible fix * Move types to entryable concern * Fix search when no entries found * Remove more unused code
96 lines
2.2 KiB
Ruby
96 lines
2.2 KiB
Ruby
class Account::Transfer < ApplicationRecord
|
|
has_many :entries, dependent: :nullify
|
|
|
|
validate :net_zero_flows, if: :single_currency_transfer?
|
|
validate :transaction_count, :from_different_accounts, :all_transactions_marked
|
|
|
|
def date
|
|
outflow_transaction&.date
|
|
end
|
|
|
|
def amount_money
|
|
entries.first&.amount_money&.abs
|
|
end
|
|
|
|
def from_name
|
|
outflow_transaction&.account&.name
|
|
end
|
|
|
|
def to_name
|
|
inflow_transaction&.account&.name
|
|
end
|
|
|
|
def name
|
|
return nil unless from_name && to_name
|
|
I18n.t("account.transfer.name", from_account: from_name, to_account: to_name)
|
|
end
|
|
|
|
def inflow_transaction
|
|
entries.find { |e| e.inflow? }
|
|
end
|
|
|
|
def outflow_transaction
|
|
entries.find { |e| e.outflow? }
|
|
end
|
|
|
|
def destroy_and_remove_marks!
|
|
transaction do
|
|
entries.each do |e|
|
|
e.update! marked_as_transfer: false
|
|
end
|
|
|
|
destroy!
|
|
end
|
|
end
|
|
|
|
class << self
|
|
def build_from_accounts(from_account, to_account, date:, amount:, currency:, name:)
|
|
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
|
|
|
|
new entries: [ outflow, inflow ]
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def single_currency_transfer?
|
|
entries.map { |e| e.currency }.uniq.size == 1
|
|
end
|
|
|
|
def transaction_count
|
|
unless entries.size == 2
|
|
errors.add :entries, "must have exactly 2 entries"
|
|
end
|
|
end
|
|
|
|
def from_different_accounts
|
|
accounts = entries.map { |e| e.account_id }.uniq
|
|
errors.add :entries, "must be from different accounts" if accounts.size < entries.size
|
|
end
|
|
|
|
def net_zero_flows
|
|
unless entries.sum(&:amount).zero?
|
|
errors.add :transactions, "must have an inflow and outflow that net to zero"
|
|
end
|
|
end
|
|
|
|
def all_transactions_marked
|
|
unless entries.all?(&:marked_as_transfer)
|
|
errors.add :entries, "must be marked as transfer"
|
|
end
|
|
end
|
|
end
|