The personal finance app for everyone (by everyone)
Ruby
fix(enable-banking): prefer descriptive remittance over transaction type (#5065)
Thiago Diniz da Silveirac8f9e4727407
diff --git a/app/models/enable_banking_entry/processor.rb b/app/models/enable_banking_entry/processor.rbindex 1ebd31b4ed..ae201f8635 100644--- a/app/models/enable_banking_entry/processor.rb+++ b/app/models/enable_banking_entry/processor.rb@@ -23,6 +23,13 @@ class EnableBankingEntry::Processor# 2-letter FamilyMerchant name matching inside unrelated text, like "IT" would# inside "NAME IT" -- a real chain name observed in this issue's own data).MIN_KNOWN_MERCHANT_MATCH_LENGTH = 3+ # A remittance line that is only a labelled reference ("Reference 0394676",+ # "Faktura nr 12345", "OCR: 4711"), which names no payee.+ REFERENCE_LINE = /\A(?:ref(?:erence|erens)?|invoice|faktura|ocr)\b\.?\s*(?:no\.?|nr\.?|number)?\s*[:#]?\s*(?=[^\s]*\d)[\w\/-]+\z/i+ # A masked card number written with spaces ("474836 XXXXXX 6879"), whose mask+ # characters would otherwise pass as letters. Kept local so card-hold+ # detection, which uses the contiguous form, is unaffected.+ SPACED_MASKED_CARD = /\A\d{4,6}\s+[x*]{4,8}\s+\d{4}\z/i# Prefix of the identifier synthesised for a transaction the ASPSP returned# with neither transaction_id nor entry_reference.@@ -268,7 +275,8 @@ class EnableBankingEntry::Processorsource: "enable_banking",merchant: merchant,notes: notes,- extra: extra+ extra: extra,+ investment_activity_label: investment_activity_label)rescue ArgumentError => eRails.logger.error "EnableBankingEntry::Processor - Validation error for transaction #{safe_id}: #{e.message}"@@ -313,7 +321,9 @@ class EnableBankingEntry::Processorreturn HOLD_TRANSACTION_NAME if unidentified_hold?# Build name from available Enable Banking transaction fields- # Priority: counterparty name > bank_transaction_code description > remittance_information+ # Priority: counterparty name > descriptive remittance (combined with the+ # bank_transaction_code description) > bank_transaction_code description >+ # any remittance_informationcounterparty = counterparty_namereturn counterparty if counterparty.present? && !technical_card_counterparty?(counterparty)@@ -325,9 +335,22 @@ class EnableBankingEntry::Processorreturn remittance.truncate(100) if remittance.present?end+ # The bank_transaction_code description is the transaction *type* (SEB sends+ # "Card purchase", "Direct debit", "Payment" with no counterparty), so the+ # payee is only in the remittance. Combine the two, as the other providers+ # do, so rows of different types to the same payee keep distinct names.+ bank_tx_description = data.dig(:bank_transaction_code, :description).presence+ payee = descriptive_remittance_name+ if payee+ payee = payee.truncate(100)+ return payee if bank_tx_description.nil? || payee.casecmp?(bank_tx_description)++ @combined_name = true+ return "#{payee} - #{bank_tx_description}"+ end+# Fall back to bank_transaction_code description- bank_tx_description = data.dig(:bank_transaction_code, :description)- return bank_tx_description if bank_tx_description.present?+ return bank_tx_description if bank_tx_description# Fall back to remittance_informationremittance = primary_remittance_information@@ -359,6 +382,28 @@ class EnableBankingEntry::Processorendend+ # The adapter detects an investment account's activity label ("Dividend",+ # "Interest", "Fee") from the start of the name. A combined+ # "payee - description" name starts with the payee instead, so the label is+ # detected from the bank description, and "" stops the adapter from reading+ # one out of a payee such as "Dividend Growth Fund". nil leaves the adapter's+ # name-based detection in place for every other name.+ def investment_activity_label+ return unless account.investment?++ label = import_adapter.detect_activity_label(data.dig(:bank_transaction_code, :description), amount)+ return label if label++ "" if combined_name?+ end++ # True when #name is "payee - description", which #name records as it+ # builds it.+ def combined_name?+ name+ @combined_name == true+ end+def notesparts = []@@ -479,6 +524,34 @@ class EnableBankingEntry::Processorself.class.technical_remittance_line?(value)end+ # The first remittance line that names the payee, cleaned and matched to a+ # known merchant the same way as primary_remittance_information.+ # A known merchant is trusted before the letter-count heuristic, so short+ # names such as "H&M" are kept.+ def descriptive_remittance_name+ remittance_information_lines.each do |value|+ line = strip_payment_processor_prefix(value)+ next if technical_remittance_line?(line) || masked_card_text?(line) || account_holder_name?(line)++ known_merchant = matched_known_merchant_name(line)+ return known_merchant if known_merchant+ return line if descriptive_remittance?(line)+ end++ nil+ end++ # Remittance text that reads as a name rather than a reference such as+ # "52220151204 092408560500" (fewer than three letters) or "Reference+ # 0394676", or a spaced masked card number. Technical lines, contiguous+ # masked card numbers and the account holder are already skipped by+ # descriptive_remittance_name.+ def descriptive_remittance?(value)+ !value.match?(REFERENCE_LINE) &&+ !value.match?(SPACED_MASKED_CARD) &&+ value.scan(/\p{L}/).size >= 3+ end+def strip_payment_processor_prefix(value)return value if value.blank?value.sub(PAYMENT_PROCESSOR_PREFIX, "").strip.presence || valuediff --git a/test/models/enable_banking_entry/processor_test.rb b/test/models/enable_banking_entry/processor_test.rbindex 0f42c40973..7d939565cc 100644--- a/test/models/enable_banking_entry/processor_test.rb+++ b/test/models/enable_banking_entry/processor_test.rb@@ -1129,6 +1129,21 @@ class EnableBankingEntry::ProcessorTest < ActiveSupport::TestCaseEnableBankingEntry::Processor.new(data, enable_banking_account: @enable_banking_account).send(:name)end+ # An Enable Banking account linked to the investment fixture, for tests of+ # investment activity labels.+ def investment_enable_banking_account+ @investment_enable_banking_account ||= begin+ enable_banking_account = EnableBankingAccount.create!(+ enable_banking_item: @enable_banking_item,+ name: "Depot",+ uid: "eb_uid_investment",+ currency: "EUR"+ )+ AccountProvider.create!(account: accounts(:investment), provider: enable_banking_account)+ enable_banking_account+ end+ end+test "skips technical card counterparty and falls back to remittance_information" doname = build_name(credit_debit_indicator: "CRDT",@@ -1140,6 +1155,207 @@ class EnableBankingEntry::ProcessorTest < ActiveSupport::TestCaseassert_equal "ACME SHOP", nameend+ test "combines the remittance merchant with a generic transaction type description" do+ # SEB sends no counterparty for card purchases; the merchant is only in the remittance+ name = build_name(+ credit_debit_indicator: "DBIT",+ remittance_information: [ "RACKSTA KROG" ],+ bank_transaction_code: { description: "Card purchase" }+ )++ assert_equal "RACKSTA KROG - Card purchase", name+ end++ test "keeps payments of different types to the same payee apart" do+ direct_debit = build_name(+ credit_debit_indicator: "DBIT",+ remittance_information: [ "TELIA SVERIGE" ],+ bank_transaction_code: { description: "Direct debit" }+ )+ card_purchase = build_name(+ credit_debit_indicator: "DBIT",+ remittance_information: [ "TELIA SVERIGE" ],+ bank_transaction_code: { description: "Card purchase" }+ )++ assert_equal "TELIA SVERIGE - Direct debit", direct_debit+ assert_equal "TELIA SVERIGE - Card purchase", card_purchase+ end++ test "detects the investment activity label from the transaction type description" do+ tx = {+ transaction_id: "txn_dividend",+ booking_date: Date.current.to_s,+ transaction_amount: { amount: "12.50", currency: "EUR" },+ credit_debit_indicator: "CRDT",+ remittance_information: [ "ACME CORP" ],+ bank_transaction_code: { description: "Dividend" },+ status: "BOOK"+ }++ entry = EnableBankingEntry::Processor.new(tx, enable_banking_account: investment_enable_banking_account).process++ assert_equal "ACME CORP - Dividend", entry.name+ assert_equal "Dividend", entry.transaction.investment_activity_label+ end++ test "does not detect an investment activity label from the payee of a combined name" do+ tx = {+ transaction_id: "txn_fund_transfer",+ booking_date: Date.current.to_s,+ transaction_amount: { amount: "100.00", currency: "EUR" },+ credit_debit_indicator: "CRDT",+ remittance_information: [ "Dividend Growth Fund" ],+ bank_transaction_code: { description: "Credit transfer" },+ status: "BOOK"+ }++ entry = EnableBankingEntry::Processor.new(tx, enable_banking_account: investment_enable_banking_account).process++ assert_equal "Dividend Growth Fund - Credit transfer", entry.name+ assert_nil entry.transaction.investment_activity_label+ end++ test "still detects an investment activity label from a counterparty name" do+ tx = {+ transaction_id: "txn_counterparty_dividend",+ booking_date: Date.current.to_s,+ transaction_amount: { amount: "8.00", currency: "EUR" },+ credit_debit_indicator: "CRDT",+ debtor: { name: "Dividend payment ACME" },+ bank_transaction_code: { description: "Credit transfer" },+ status: "BOOK"+ }++ entry = EnableBankingEntry::Processor.new(tx, enable_banking_account: investment_enable_banking_account).process++ assert_equal "Dividend payment ACME", entry.name+ assert_equal "Dividend", entry.transaction.investment_activity_label+ end++ test "does not set an investment activity label on a non-investment account" do+ tx = {+ transaction_id: "txn_interest",+ booking_date: Date.current.to_s,+ transaction_amount: { amount: "3.10", currency: "EUR" },+ credit_debit_indicator: "CRDT",+ remittance_information: [ "SAVINGS BANK" ],+ bank_transaction_code: { description: "Interest" },+ status: "BOOK"+ }++ entry = EnableBankingEntry::Processor.new(tx, enable_banking_account: @enable_banking_account).process++ assert_equal "SAVINGS BANK - Interest", entry.name+ assert_nil entry.transaction.investment_activity_label+ end++ test "uses a later remittance line when the first one is only a reference" do+ name = build_name(+ credit_debit_indicator: "DBIT",+ remittance_information: [ "52220151204", "TELIA SVERIGE" ],+ bank_transaction_code: { description: "Direct debit" }+ )++ assert_equal "TELIA SVERIGE - Direct debit", name+ end++ test "keeps the transaction type description when the remittance is a spaced masked card number" do+ name = build_name(+ credit_debit_indicator: "DBIT",+ transaction_id: "txn_spaced_masked_pan",+ remittance_information: [ "474836 XXXXXX 6879" ],+ bank_transaction_code: { description: "Card purchase" }+ )++ assert_equal "Card purchase", name+ end++ test "keeps the transaction type description when the remittance is a labelled reference" do+ name = build_name(+ credit_debit_indicator: "DBIT",+ remittance_information: [ "Reference 0394676" ],+ bank_transaction_code: { description: "Credit transfer" }+ )++ assert_equal "Credit transfer", name+ end++ test "skips a labelled reference and uses the payee on a later line" do+ name = build_name(+ credit_debit_indicator: "DBIT",+ remittance_information: [ "Faktura nr 12345", "TELIA SVERIGE" ],+ bank_transaction_code: { description: "Direct debit" }+ )++ assert_equal "TELIA SVERIGE - Direct debit", name+ end++ test "keeps a short known merchant name over a later descriptive line" do+ @family.merchants.create!(name: "H&M")++ name = build_name_with_family(+ credit_debit_indicator: "DBIT",+ creditor: { name: "" },+ bank_transaction_code: nil,+ remittance_information: [ "H&M", "LONDON" ]+ )++ assert_equal "H&M", name+ end++ test "does not treat a merchant name starting with a reference keyword as a reference" do+ name = build_name(+ credit_debit_indicator: "DBIT",+ remittance_information: [ "REFILL STATION 2" ],+ bank_transaction_code: { description: "Card purchase" }+ )++ assert_equal "REFILL STATION 2 - Card purchase", name+ end++ test "uses the descriptive remittance alone when there is no transaction type description" do+ name = build_name(+ credit_debit_indicator: "DBIT",+ remittance_information: [ "RACKSTA KROG" ],+ bank_transaction_code: { code: "PMNT" }+ )++ assert_equal "RACKSTA KROG", name+ end++ test "keeps the transaction type description when the remittance is a masked card number" do+ name = build_name(+ credit_debit_indicator: "DBIT",+ transaction_id: "txn_masked_pan",+ remittance_information: [ "474836xxxxxx6879" ],+ bank_transaction_code: { description: "Card purchase" }+ )++ assert_equal "Card purchase", name+ end++ test "keeps the transaction type description when the remittance is only a reference" do+ name = build_name(+ credit_debit_indicator: "DBIT",+ remittance_information: [ "52220151204 092408560500" ],+ bank_transaction_code: { description: "Credit transfer" }+ )++ assert_equal "Credit transfer", name+ end++ test "keeps the transaction type description when the remittance is the account holder" do+ name = build_name_with_family(+ credit_debit_indicator: "CRDT",+ creditor: { name: "JANE DOE" },+ remittance_information: [ "Jane Doe" ],+ bank_transaction_code: { description: "Transfer" }+ )++ assert_equal "Transfer", name+ end+test "uses counterparty when it is human readable" doname = build_name(credit_debit_indicator: "CRDT",