Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Transfer Matching: Larger match date window for manual matching (#514)

Dylan Corrales4e87eead2c4a
diff --git a/app/models/family/auto_transfer_matchable.rb b/app/models/family/auto_transfer_matchable.rb
index 8566cbdcf6..98c70775e6 100644
--- a/app/models/family/auto_transfer_matchable.rb
+++ b/app/models/family/auto_transfer_matchable.rb
@@ -1,5 +1,5 @@
module Family::AutoTransferMatchable
- def transfer_match_candidates
+ def transfer_match_candidates(date_window: 4)
Entry.select([
"inflow_candidates.entryable_id as inflow_transaction_id",
"outflow_candidates.entryable_id as outflow_transaction_id",
@@ -10,7 +10,7 @@ module Family::AutoTransferMatchable
inflow_candidates.amount < 0 AND
outflow_candidates.amount > 0 AND
inflow_candidates.account_id <> outflow_candidates.account_id AND
- inflow_candidates.date BETWEEN outflow_candidates.date - 4 AND outflow_candidates.date + 4
+ inflow_candidates.date BETWEEN outflow_candidates.date - #{date_window.to_i} AND outflow_candidates.date + #{date_window.to_i}
)
").joins("
LEFT JOIN transfers existing_transfers ON (
diff --git a/app/models/transaction/transferable.rb b/app/models/transaction/transferable.rb
index cea7978bae..0980876105 100644
--- a/app/models/transaction/transferable.rb
+++ b/app/models/transaction/transferable.rb
@@ -14,11 +14,11 @@ module Transaction::Transferable
transfer_as_inflow || transfer_as_outflow
end
- def transfer_match_candidates
+ def transfer_match_candidates(date_window: 30)
candidates_scope = if self.entry.amount.negative?
- family_matches_scope.where("inflow_candidates.entryable_id = ?", self.id)
+ family_matches_scope(date_window: date_window).where("inflow_candidates.entryable_id = ?", self.id)
else
- family_matches_scope.where("outflow_candidates.entryable_id = ?", self.id)
+ family_matches_scope(date_window: date_window).where("outflow_candidates.entryable_id = ?", self.id)
end
candidates_scope.map do |match|
@@ -30,7 +30,7 @@ module Transaction::Transferable
end
private
- def family_matches_scope
- self.entry.account.family.transfer_match_candidates
+ def family_matches_scope(date_window:)
+ self.entry.account.family.transfer_match_candidates(date_window: date_window)
end
end
diff --git a/app/models/transfer.rb b/app/models/transfer.rb
index f11867665c..93de0a068e 100644
--- a/app/models/transfer.rb
+++ b/app/models/transfer.rb
@@ -133,6 +133,7 @@ class Transfer < ApplicationRecord
return unless inflow_transaction&.entry && outflow_transaction&.entry
date_diff = (inflow_transaction.entry.date - outflow_transaction.entry.date).abs
- errors.add(:base, "Must be within 4 days") if date_diff > 4
+ max_days = status == "confirmed" ? 30 : 4
+ errors.add(:base, "Must be within #{max_days} days") if date_diff > max_days
end
end