The personal finance app for everyone (by everyone)
Ruby
QIF Import: Show new categories after changing the date format (#5565)
Sailing74ad88f54e47e7
diff --git a/app/controllers/import/qif_category_selections_controller.rb b/app/controllers/import/qif_category_selections_controller.rbindex 11910d3bc9..1bb0e6205d 100644--- a/app/controllers/import/qif_category_selections_controller.rb+++ b/app/controllers/import/qif_category_selections_controller.rb@@ -17,14 +17,29 @@ class Import::QifCategorySelectionsController < ApplicationController@split_categories = @import.split_categories@has_split_transactions = @import.has_split_transactions?@unknown_account_types = @import.unknown_qif_account_types+ @selection_list_changed = flash[:qif_selection_list_changed].present?enddef update# If the user changed the date format, re-generate rows with the new format.if selection_params[:date_format].present? && selection_params[:date_format] != @import.qif_date_format+ offered_lists = [ @import.row_categories, @import.row_tags ]++ # The rows are rebuilt from the file, so a selection saved for the old+ # rows no longer applies; it would also hide split categories and tags+ # that only appear with the new format.+ @import.column_mappings = @import.column_mappings.except("qif_selected_categories", "qif_selected_tags")@import.qif_date_format = selection_params[:date_format]@import.update_column(:column_mappings, @import.column_mappings)@import.generate_rows_from_csv++ # The form only listed what the rows held with the old format. If the+ # lists differ now, show them again so the user can choose from all+ # categories and tags before anything is cleared.+ if [ @import.row_categories, @import.row_tags ] != offered_lists+ flash[:qif_selection_list_changed] = true+ redirect_to import_qif_category_selection_path(@import) and return+ endendall_categories = @import.row_categoriesdiff --git a/app/models/qif_import.rb b/app/models/qif_import.rbindex 3bd93bfee4..1831578e78 100644--- a/app/models/qif_import.rb+++ b/app/models/qif_import.rb@@ -16,6 +16,7 @@ class QifImport < Importdef qif_date_format=(fmt)self.column_mappings = (column_mappings || {}).merge("qif_date_format" => fmt)+ reset_parsed_transactionsend# Parses the stored QIF content and creates Import::Row records.@@ -177,6 +178,14 @@ class QifImport < Import@parsed_transactions_with_splits ||= QifParser.parse(raw_file_str, date_format: qif_date_format)end+ # The parsed transactions depend on the date format; forget them when it changes.+ def reset_parsed_transactions+ remove_instance_variable(:@parsed_transactions_with_splits) if defined?(@parsed_transactions_with_splits)+ remove_instance_variable(:@parsed_transaction_by_source_row_number) if defined?(@parsed_transaction_by_source_row_number)+ remove_instance_variable(:@has_split_transactions) if defined?(@has_split_transactions)+ remove_instance_variable(:@split_categories) if defined?(@split_categories)+ end+def parsed_transaction_by_source_row_number@parsed_transaction_by_source_row_number ||= parsed_transactions_with_splits.each.with_index(1).to_h { |trn, index| [ index, trn ] }enddiff --git a/app/views/import/qif_category_selections/show.html.erb b/app/views/import/qif_category_selections/show.html.erbindex 94f38adfb2..0d9c8f4868 100644--- a/app/views/import/qif_category_selections/show.html.erb+++ b/app/views/import/qif_category_selections/show.html.erb@@ -40,6 +40,15 @@<% end %><% end %>+ <%# ── Lists changed with the new date format ──────────────── %>+ <% if @selection_list_changed %>+ <%= render DS::Alert.new(+ variant: :info,+ title: t(".selection_list_changed_title"),+ message: t(".selection_list_changed_description")+ ) %>+ <% end %>+<%# ── Unknown account types ───────────────────────────────── %><% if @unknown_account_types.any? %><%= render DS::Alert.new(diff --git a/config/locales/views/imports/de.yml b/config/locales/views/imports/de.ymlindex fd24de4206..6275b38954 100644--- a/config/locales/views/imports/de.yml+++ b/config/locales/views/imports/de.yml@@ -25,6 +25,8 @@ de:split_warning_title: Aufgeteilte Buchungen erkanntsplit_warning_description: "Diese QIF-Datei enthält aufgeteilte Buchungen. Aufgeteilte Buchungen werden noch nicht unterstützt – jede aufgeteilte Buchung wird als einzelne Buchung mit ihrem Gesamtbetrag und ohne Kategorie importiert. Die einzelnen Aufteilungsdetails werden nicht übernommen."split_badge: aufgeteilt+ selection_list_changed_title: Kategorien und Tags haben sich geändert+ selection_list_changed_description: "Mit dem neuen Datumsformat enthält die Datei andere Kategorien oder Tags. Prüfe die Auswahl unten; alle Einträge sind wieder ausgewählt."unknown_account_type_name: "„%{type}“"unknown_account_type_title:one: Unbekannter Kontotypdiff --git a/config/locales/views/imports/en.yml b/config/locales/views/imports/en.ymlindex 937844cbd0..f1967c0874 100644--- a/config/locales/views/imports/en.yml+++ b/config/locales/views/imports/en.yml@@ -24,6 +24,8 @@ en:split_warning_title: Split transactions detectedsplit_warning_description: "This QIF file contains split transactions. Review the detected split categories and tags below before importing."split_badge: split+ selection_list_changed_title: Categories and tags have changed+ selection_list_changed_description: "With the new date format, the file contains different categories or tags. Review the selection below; all entries are selected again."unknown_account_type_name: "“%{type}”"unknown_account_type_title:one: Unknown account typediff --git a/test/controllers/import/qif_category_selections_controller_test.rb b/test/controllers/import/qif_category_selections_controller_test.rbindex 37f147000d..3a7f7099ec 100644--- a/test/controllers/import/qif_category_selections_controller_test.rb+++ b/test/controllers/import/qif_category_selections_controller_test.rb@@ -37,10 +37,161 @@ class Import::QifCategorySelectionsControllerTest < ActionDispatch::IntegrationTassert_select "p", text: /Unknown account type/, count: 0end+ # Month-first dates with dots: read as day-first, only "7.10.20" is a valid+ # date, so the "Rent" transaction on "7.25.20" is missing until the user+ # switches to MM.DD.YYYY.+ DOTTED_MONTH_FIRST_QIF = <<~QIF+ !Type:Bank+ D7.10.20+ T-10.00+ PGrocery Store+ LFood/Holiday+ ^+ D7.25.20+ T-500.00+ PLandlord+ LRent/Home+ ^+ QIF++ test "update shows the selection again when the new date format brings new categories and tags" do+ import = qif_import_with(DOTTED_MONTH_FIRST_QIF, date_format: "%d.%m.%Y")+ assert_equal [ "Food" ], import.row_categories++ put import_qif_category_selection_url(import), params: {+ date_format: "%m.%d.%Y", categories: [ "Food" ], tags: [ "Holiday" ]+ }++ assert_redirected_to import_qif_category_selection_url(import)+ import.reload+ assert_equal "%m.%d.%Y", import.qif_date_format+ assert_equal({ "Grocery Store" => "Food", "Landlord" => "Rent" }, import.rows.pluck(:name, :category).to_h)+ assert_equal({ "Grocery Store" => "Holiday", "Landlord" => "Home" }, import.rows.pluck(:name, :tags).to_h)+ assert_nil import.column_mappings["qif_selected_categories"]++ follow_redirect!++ assert_response :success+ assert_select "div", text: /Categories and tags have changed/+ assert_select "input[name='categories[]'][value='Rent'][checked]"+ assert_select "input[name='tags[]'][value='Home'][checked]"+ end++ test "update shows the selection again when the new date format brings new split categories" do+ import = qif_import_with(<<~QIF, date_format: "%d.%m.%Y")+ !Type:Bank+ D7.10.20+ T-10.00+ PGrocery Store+ LFood+ ^+ D7.25.20+ T-500.00+ PLandlord+ SRent+ $-400.00+ SUtilities+ $-100.00+ ^+ QIF+ assert_equal [ "Food" ], import.row_categories++ put import_qif_category_selection_url(import), params: { date_format: "%m.%d.%Y", categories: [ "Food" ] }++ assert_redirected_to import_qif_category_selection_url(import)+ assert_equal [ "Food", "Rent", "Utilities" ], QifImport.find(import.id).row_categories+ end++ test "update offers new split categories after a format change on a revisited selection" do+ import = qif_import_with(<<~QIF, date_format: "%d.%m.%Y")+ !Type:Bank+ D7.10.20+ T-10.00+ PGrocery Store+ LFood+ ^+ D7.25.20+ T-500.00+ PLandlord+ SRent+ $-400.00+ SUtilities+ $-100.00+ ^+ QIF+ put import_qif_category_selection_url(import), params: { date_format: "%d.%m.%Y", categories: [ "Food" ] }+ assert_redirected_to import_clean_url(import)+ assert_equal [ "Food" ], import.reload.column_mappings["qif_selected_categories"]++ put import_qif_category_selection_url(import), params: { date_format: "%m.%d.%Y", categories: [ "Food" ] }++ assert_redirected_to import_qif_category_selection_url(import)+ assert_nil import.reload.column_mappings["qif_selected_categories"]+ assert_equal [ "Food", "Rent", "Utilities" ], QifImport.find(import.id).row_categories++ follow_redirect!++ assert_select "input[name='categories[]'][value='Utilities'][checked]"+ end++ test "update applies the selection when the new date format keeps the same categories and tags" do+ import = qif_import_with(<<~QIF, date_format: "%d.%m.%Y")+ !Type:Bank+ D1.2.20+ T-10.00+ PGrocery Store+ LFood+ ^+ D3.4.20+ T-500.00+ PLandlord+ LRent+ ^+ QIF++ put import_qif_category_selection_url(import), params: { date_format: "%m.%d.%Y", categories: [ "Rent" ] }++ assert_redirected_to import_clean_url(import)+ import.reload+ assert_equal "%m.%d.%Y", import.qif_date_format+ assert_equal "2020-01-02", import.rows.find_by(name: "Grocery Store").date+ assert_equal({ "Grocery Store" => "", "Landlord" => "Rent" }, import.rows.pluck(:name, :category).to_h)+ assert_equal [ "Rent" ], import.column_mappings["qif_selected_categories"]+ end++ test "update clears deselected categories and tags when the date format stays the same" do+ import = qif_import_with(DOTTED_MONTH_FIRST_QIF, date_format: "%m.%d.%Y")++ put import_qif_category_selection_url(import), params: {+ date_format: "%m.%d.%Y", categories: [ "Rent" ], tags: [ "Home" ]+ }++ assert_redirected_to import_clean_url(import)+ import.reload+ assert_equal({ "Grocery Store" => "", "Landlord" => "Rent" }, import.rows.pluck(:name, :category).to_h)+ assert_equal({ "Grocery Store" => "", "Landlord" => "Home" }, import.rows.pluck(:name, :tags).to_h)+ assert_equal [ "Rent" ], import.column_mappings["qif_selected_categories"]+ assert_equal [ "Home" ], import.column_mappings["qif_selected_tags"]+ end++ test "show does not report changed lists on a normal visit" do+ import = qif_import_with(DOTTED_MONTH_FIRST_QIF, date_format: "%m.%d.%Y")++ get import_qif_category_selection_url(import)++ assert_response :success+ assert_select "div", text: /Categories and tags have changed/, count: 0+ end+private- def qif_import_with(content)+ def qif_import_with(content, date_format: nil)import = QifImport.create!(family: @user.family, account: @account, raw_file_str: content)import.generate_rows_from_csv+ if date_format+ import.qif_date_format = date_format+ import.save!(validate: false)+ import.generate_rows_from_csv+ endimportendenddiff --git a/test/models/qif_import_test.rb b/test/models/qif_import_test.rbindex 15f17e7b31..82765c0d51 100644--- a/test/models/qif_import_test.rb+++ b/test/models/qif_import_test.rb@@ -729,6 +729,28 @@ class QifImportTest < ActiveSupport::TestCaseassert_equal BigDecimal("500"), manager.opening_balanceend+ test "changing the date format resets the parsed split categories" do+ @import.update!(raw_file_str: <<~QIF)+ !Type:Bank+ D7.25.20+ T-500.00+ PLandlord+ SRent+ $-400.00+ SUtilities+ $-100.00+ ^+ QIF+ @import.qif_date_format = "%d.%m.%Y"+ assert_empty @import.split_categories+ refute @import.has_split_transactions?++ @import.qif_date_format = "%m.%d.%Y"++ assert_equal [ "Rent", "Utilities" ], @import.split_categories+ assert @import.has_split_transactions?+ end+test "unknown_qif_account_types is empty for standard QIF files" do@import.update!(raw_file_str: SAMPLE_QIF)assert_empty @import.unknown_qif_account_types