mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-06 05:55:21 +02:00
Add RejectedTransfer model, simplify auto matching (#1690)
* Allow transfers to match when inflow is after outflow * Simplify transfer auto matching with RejectedTransfer model * Validations * Reset migrations
This commit is contained in:
parent
0b4e314f58
commit
de90b29201
18 changed files with 221 additions and 79 deletions
|
@ -77,12 +77,20 @@ class Account::Entry < ApplicationRecord
|
|||
end
|
||||
|
||||
def transfer_match_candidates
|
||||
account.family.entries
|
||||
.where.not(account_id: account_id)
|
||||
.where.not(id: id)
|
||||
.where(amount: -amount)
|
||||
.where(currency: currency)
|
||||
.where(date: (date - 4.days)..(date + 4.days))
|
||||
candidates_scope = account.transfer_match_candidates
|
||||
|
||||
candidates_scope = if amount.negative?
|
||||
candidates_scope.where("inflow_candidates.entryable_id = ?", entryable_id)
|
||||
else
|
||||
candidates_scope.where("outflow_candidates.entryable_id = ?", entryable_id)
|
||||
end
|
||||
|
||||
candidates_scope.map do |pm|
|
||||
Transfer.new(
|
||||
inflow_transaction_id: pm.inflow_transaction_id,
|
||||
outflow_transaction_id: pm.outflow_transaction_id,
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
class << self
|
||||
|
|
|
@ -5,7 +5,7 @@ class Account::Syncer
|
|||
end
|
||||
|
||||
def run
|
||||
Transfer.auto_match_for_account(account)
|
||||
account.auto_match_transfers!
|
||||
|
||||
holdings = sync_holdings
|
||||
balances = sync_balances(holdings)
|
||||
|
|
|
@ -9,6 +9,10 @@ class Account::Transaction < ApplicationRecord
|
|||
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) }
|
||||
|
@ -24,6 +28,6 @@ class Account::Transaction < ApplicationRecord
|
|||
end
|
||||
|
||||
def transfer?
|
||||
transfer.present? && transfer.status != "rejected"
|
||||
transfer.present?
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue