Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Detect autopay from explicit descriptor wording (#3624)

Will Wilson1888ccef9c9c
diff --git a/app/models/recurring_transaction/classifier.rb b/app/models/recurring_transaction/classifier.rb
index 3ebeea7995..3d459761f6 100644
--- a/app/models/recurring_transaction/classifier.rb
+++ b/app/models/recurring_transaction/classifier.rb
@@ -35,6 +35,14 @@ class RecurringTransaction
# Push-payment fingerprints in raw descriptors.
ACH_MARKERS = %w[ach web\ pmt webpmt billpay bill\ pay online\ pmt e-pay epay].freeze
+ # Explicit autopay wording banks stamp onto a descriptor. "auto pay" and
+ # "auto pmt" are left out: on a car loan or policy they read as
+ # "automobile payment", and a false match drops the bill off needs-action.
+ AUTOPAY_MARKERS = %w[
+ autopay autopmt autodraft auto\ draft auto-draft autodebit auto\ debit
+ auto-debit
+ ].freeze
+
# Above this, a flat recurring charge is more likely rent-or-service
# than a streaming plan.
SUBSCRIPTION_AMOUNT_CEILING = BigDecimal("150")
@@ -67,8 +75,10 @@ class RecurringTransaction
bill_type: kind,
category_id: modal_category_id,
# Subscriptions and BNPL plans are auto-charges: on the list, not a
- # task.
- autopay: kind != "bill"
+ # task. A bill defaults to needing action, but explicit autopay
+ # wording in the descriptor overrides that default -- the biller told
+ # us directly, so no other heuristic gets a vote.
+ autopay: kind != "bill" || matches?(AUTOPAY_MARKERS)
)
end
diff --git a/docs/hosting/recurring-bills.md b/docs/hosting/recurring-bills.md
index 7c846b22bd..35bd6a173b 100644
--- a/docs/hosting/recurring-bills.md
+++ b/docs/hosting/recurring-bills.md
@@ -57,7 +57,8 @@ anchors and never touch existing payment records.
bin/rails "recurring:backfill_history[12]"
# One-shot classification of auto-detected series still on defaults
-# (assigns bill/subscription/installment kind and a category)
+# (assigns bill/subscription/installment kind, a category, and autopay
+# where the descriptor says so)
bin/rails recurring:classify_existing
```
diff --git a/lib/tasks/recurring_bills.rake b/lib/tasks/recurring_bills.rake
index 39a9ac2272..37d1f08dbc 100644
--- a/lib/tasks/recurring_bills.rake
+++ b/lib/tasks/recurring_bills.rake
@@ -33,7 +33,7 @@ namespace :recurring do
classification = RecurringTransaction::Classifier.classify(
name: series.display_name, entries: entries, account: series.account
)
- next if classification.bill_type == "bill" && classification.category_id.nil?
+ next if classification.bill_type == "bill" && classification.category_id.nil? && !classification.autopay
series.update!(
bill_type: classification.bill_type,
@@ -41,7 +41,7 @@ namespace :recurring do
autopay: series.autopay || classification.autopay
)
reclassified += 1
- puts " #{series.display_name}: #{classification.bill_type}#{classification.category_id ? ' +category' : ''}"
+ puts " #{series.display_name}: #{classification.bill_type}#{classification.category_id ? ' +category' : ''}#{classification.autopay ? ' +autopay' : ''}"
end
puts "#{family.id}: #{reclassified} series classified"
diff --git a/test/models/recurring_transaction/classifier_test.rb b/test/models/recurring_transaction/classifier_test.rb
index 970ebe8253..9fb8f68ab9 100644
--- a/test/models/recurring_transaction/classifier_test.rb
+++ b/test/models/recurring_transaction/classifier_test.rb
@@ -23,6 +23,28 @@ class RecurringTransaction::ClassifierTest < ActiveSupport::TestCase
assert_not result.autopay
end
+ test "explicit autopay wording on a bill overrides the needs-action default" do
+ result = Classifier.classify(name: "XFINITY INTERNET AUTOPAY", entries: entries_of(89.99, 89.99), account: @credit_card)
+
+ assert_equal "bill", result.bill_type
+ assert result.autopay, "the descriptor says autopay, so trust it over the bill_type default"
+ end
+
+ test "autodraft and auto-debit wording also read as autopay" do
+ autodraft = Classifier.classify(name: "WATSON PROPERTY AUTODRAFT", entries: entries_of(537.50, 537.50), account: @depository)
+ assert autodraft.autopay
+
+ auto_debit = Classifier.classify(name: "STATE FARM AUTO-DEBIT PREM", entries: entries_of(140, 140), account: @depository)
+ assert auto_debit.autopay
+ end
+
+ test "spaced auto pay wording stays ambiguous with an automobile payment" do
+ result = Classifier.classify(name: "TOYOTA FINANCIAL AUTO PAY", entries: entries_of(412, 412), account: @depository)
+
+ assert_equal "bill", result.bill_type
+ assert_not result.autopay
+ end
+
test "ach and billpay descriptors are push payments, not subscriptions" do
result = Classifier.classify(name: "WATSON PROPERTY WEB PMT", entries: entries_of(537.50, 537.50), account: @depository)