mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-05 21:45:23 +02:00
* Allow transfers to match when inflow is after outflow * Simplify transfer auto matching with RejectedTransfer model * Validations * Reset migrations
33 lines
1.1 KiB
Ruby
33 lines
1.1 KiB
Ruby
class Account::Transaction < ApplicationRecord
|
|
include Account::Entryable
|
|
|
|
belongs_to :category, optional: true
|
|
belongs_to :merchant, optional: true
|
|
has_many :taggings, as: :taggable, dependent: :destroy
|
|
has_many :tags, through: :taggings
|
|
|
|
has_one :transfer_as_inflow, class_name: "Transfer", foreign_key: "inflow_transaction_id", dependent: :destroy
|
|
has_one :transfer_as_outflow, class_name: "Transfer", foreign_key: "outflow_transaction_id", dependent: :destroy
|
|
|
|
# We keep track of rejected transfers to avoid auto-matching them again
|
|
has_one :rejected_transfer_as_inflow, class_name: "RejectedTransfer", foreign_key: "inflow_transaction_id", dependent: :destroy
|
|
has_one :rejected_transfer_as_outflow, class_name: "RejectedTransfer", foreign_key: "outflow_transaction_id", dependent: :destroy
|
|
|
|
accepts_nested_attributes_for :taggings, allow_destroy: true
|
|
|
|
scope :active, -> { where(excluded: false) }
|
|
|
|
class << self
|
|
def search(params)
|
|
Account::TransactionSearch.new(params).build_query(all)
|
|
end
|
|
end
|
|
|
|
def transfer
|
|
transfer_as_inflow || transfer_as_outflow
|
|
end
|
|
|
|
def transfer?
|
|
transfer.present?
|
|
end
|
|
end
|