mirror of
https://github.com/we-promise/sure.git
synced 2026-04-19 12:04:08 +00:00
FR: Add transaction type as rule condition option (#790)
* Add transaction type condition filter for rules Add ability to filter rules by transaction type (income, expense, transfer). This allows users to create rules that differentiate between transactions with the same name but different types. - Add Rule::ConditionFilter::TransactionType with select dropdown - Register in TransactionResource condition_filters - Add tests for income, expense, and transfer filtering Closes #373 * Address PR review feedback for transaction type filter - Fix income filter to exclude transfers and investment_contribution - Fix expense filter to include investment_contribution regardless of sign - Add i18n for option and operator labels - Add tests for edge cases (transfer inflows, investment contributions) Logic now matches Transaction::Search#apply_type_filter for consistency.
This commit is contained in:
42
app/models/rule/condition_filter/transaction_type.rb
Normal file
42
app/models/rule/condition_filter/transaction_type.rb
Normal file
@@ -0,0 +1,42 @@
|
||||
class Rule::ConditionFilter::TransactionType < Rule::ConditionFilter
|
||||
# Transfer kinds matching Transaction#transfer? method
|
||||
TRANSFER_KINDS = %w[funds_movement cc_payment loan_payment].freeze
|
||||
|
||||
def type
|
||||
"select"
|
||||
end
|
||||
|
||||
def options
|
||||
[
|
||||
[ I18n.t("rules.condition_filters.transaction_type.income"), "income" ],
|
||||
[ I18n.t("rules.condition_filters.transaction_type.expense"), "expense" ],
|
||||
[ I18n.t("rules.condition_filters.transaction_type.transfer"), "transfer" ]
|
||||
]
|
||||
end
|
||||
|
||||
def operators
|
||||
[ [ I18n.t("rules.condition_filters.transaction_type.equal_to"), "=" ] ]
|
||||
end
|
||||
|
||||
def prepare(scope)
|
||||
scope.with_entry
|
||||
end
|
||||
|
||||
def apply(scope, operator, value)
|
||||
# Logic matches Transaction::Search#apply_type_filter for consistency
|
||||
case value
|
||||
when "income"
|
||||
# Negative amounts, excluding transfers and investment_contribution
|
||||
scope.where("entries.amount < 0")
|
||||
.where.not(kind: TRANSFER_KINDS + %w[investment_contribution])
|
||||
when "expense"
|
||||
# Positive amounts OR investment_contribution (regardless of sign), excluding transfers
|
||||
scope.where("entries.amount >= 0 OR transactions.kind = 'investment_contribution'")
|
||||
.where.not(kind: TRANSFER_KINDS)
|
||||
when "transfer"
|
||||
scope.where(kind: TRANSFER_KINDS)
|
||||
else
|
||||
scope
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -7,6 +7,7 @@ class Rule::Registry::TransactionResource < Rule::Registry
|
||||
[
|
||||
Rule::ConditionFilter::TransactionName.new(rule),
|
||||
Rule::ConditionFilter::TransactionAmount.new(rule),
|
||||
Rule::ConditionFilter::TransactionType.new(rule),
|
||||
Rule::ConditionFilter::TransactionMerchant.new(rule),
|
||||
Rule::ConditionFilter::TransactionCategory.new(rule),
|
||||
Rule::ConditionFilter::TransactionDetails.new(rule),
|
||||
|
||||
Reference in New Issue
Block a user