The personal finance app for everyone (by everyone)
Ruby
QIF Import: Improve Opening balance recognition (#5461)
Sailing7455e483598a1b
diff --git a/app/models/concerns/qif_parser.rb b/app/models/concerns/qif_parser.rbindex 24c7b07ab2..9c17064ae3 100644--- a/app/models/concerns/qif_parser.rb+++ b/app/models/concerns/qif_parser.rb@@ -40,6 +40,13 @@ module QifParserTRANSACTION_TYPES = %w[CCard Bank Cash Invst Oth\ L Oth\ A].freezeGENERIC_TRANSACTION_TYPES = TRANSACTION_TYPES - [ "Invst" ]+ # Payees that mark an account's starting balance. Quicken writes+ # "Opening Balance"; German exports (e.g. Lexware FinanzManager) write+ # "Eröffnungssaldo". Names from exports in other languages belong here too.+ # Not an I18n lookup: the name comes from the program that wrote the file,+ # not from the importing user's locale, so every known name must match.+ OPENING_BALANCE_PAYEES = [ "Opening Balance", "Eröffnungssaldo" ].freeze+# Investment action types that create Trade records (buy or sell shares).BUY_LIKE_ACTIONS = %w[Buy ReinvDiv Cover].freezeSELL_LIKE_ACTIONS = %w[Sell ShtSell].freeze@@ -174,8 +181,9 @@ module QifParser# Returns the opening balance entry from the QIF file, if present.# In Quicken's QIF format, the first transaction of a bank/cash account is often- # an "Opening Balance" record with payee "Opening Balance". This entry is NOT a- # real transaction – it is the account's starting balance.+ # an "Opening Balance" record with payee "Opening Balance" (see+ # OPENING_BALANCE_PAYEES). This entry is NOT a real transaction – it is the+ # account's starting balance.## Returns a hash { date: Date, amount: BigDecimal } or nil.def self.parse_opening_balance(content, date_format: "%m/%d/%Y")@@ -191,7 +199,7 @@ module QifParsercontent = normalize_line_endings(content)transaction_sections(content).filter_map do |section|- record = parse_records(section[:content]).find { |r| r["P"]&.strip == "Opening Balance" }+ record = parse_records(section[:content]).find { |r| opening_balance_record?(r) }next unless recorddate = parse_qif_date(record["D"], date_format: date_format)@@ -454,10 +462,15 @@ module QifParserendprivate_class_method :parse_records+ def self.opening_balance_record?(record)+ OPENING_BALANCE_PAYEES.include?(record["P"]&.strip)+ end+ private_class_method :opening_balance_record?+def self.build_transaction(record, date_format: "%m/%d/%Y", account: nil, account_type: nil)# "Opening Balance" is a Quicken convention for the account's starting balance –# it is not a real transaction and must not be imported as one.- return nil if record["P"]&.strip == "Opening Balance"+ return nil if opening_balance_record?(record)raw_date = record["D"]raw_amount = record["T"] || record["U"]diff --git a/test/models/qif_import_test.rb b/test/models/qif_import_test.rbindex f67e8a9b60..15f17e7b31 100644--- a/test/models/qif_import_test.rb+++ b/test/models/qif_import_test.rb@@ -84,6 +84,22 @@ class QifImportTest < ActiveSupport::TestCase^QIF+ # Lexware FinanzManager and other German exports name the entry "Eröffnungssaldo".+ QIF_WITH_GERMAN_OPENING_BALANCE = <<~QIF+ !Type:Bank+ D1/ 1'20+ U500.00+ T500.00+ PEröffnungssaldo+ L[Girokonto]+ ^+ D3/ 1'20+ U100.00+ T100.00+ PFirst Deposit+ ^+ QIF+# A minimal investment QIF with two securities, trades, a dividend, and a cash transfer.SAMPLE_INVST_QIF = <<~QIF!Type:Security@@ -537,6 +553,26 @@ class QifImportTest < ActiveSupport::TestCaseassert_nil QifParser.parse_opening_balance("")end+ test "parse skips an Eröffnungssaldo transaction" do+ transactions = QifParser.parse(QIF_WITH_GERMAN_OPENING_BALANCE)++ assert_equal [ "First Deposit" ], transactions.map(&:payee)+ end++ test "parse_opening_balance reads an Eröffnungssaldo entry" do+ ob = QifParser.parse_opening_balance(QIF_WITH_GERMAN_OPENING_BALANCE)++ assert_equal Date.new(2020, 1, 1), ob[:date]+ assert_equal BigDecimal("500"), ob[:amount]+ end++ test "parse_opening_balance reads an Eröffnungssaldo entry from a Windows-1252 file" do+ content = QIF_WITH_GERMAN_OPENING_BALANCE.encode("Windows-1252").b++ assert_equal BigDecimal("500"), QifParser.parse_opening_balance(content)[:amount]+ assert_equal [ "First Deposit" ], QifParser.parse(content).map(&:payee)+ end+# ── QifParser: split transactions ──────────────────────────────────────────test "parse flags split transactions" do@@ -986,6 +1022,22 @@ class QifImportTest < ActiveSupport::TestCaseassert_equal BigDecimal("500"), manager.opening_balanceend+ test "import! sets opening anchor from an Eröffnungssaldo entry" do+ @import.update!(raw_file_str: QIF_WITH_GERMAN_OPENING_BALANCE)+ @import.generate_rows_from_csv+ @import.sync_mappings++ assert_equal 1, @import.rows.count+ refute @import.rows.exists?(name: "Eröffnungssaldo")++ @import.import!++ manager = Account::OpeningBalanceManager.new(@account)+ assert manager.has_opening_anchor?+ assert_equal Date.new(2020, 1, 1), manager.opening_date+ assert_equal BigDecimal("500"), manager.opening_balance+ end+test "import! creates declared account that only has an opening balance" do@import.update!(account: nil, raw_file_str: QIF_WITH_BALANCE_ONLY_ACCOUNT)@import.generate_rows_from_csv