The personal finance app for everyone (by everyone)
Ruby
Read Mint amounts as magnitudes in Mint imports (#5690)
Andy Wangcd7a0e43c882
diff --git a/app/models/mint_import.rb b/app/models/mint_import.rbindex 1dc5e5fb5d..31603bdaa8 100644--- a/app/models/mint_import.rb+++ b/app/models/mint_import.rb@@ -88,18 +88,29 @@ class MintImport < ImportCSV.parse(template, headers: true)end+ # The Transaction Type column carries the direction, so the amount is read as+ # a magnitude whatever its sign.def signed_csv_amount(csv_row)- amount = csv_row[amount_col_label]+ amount = sanitize_number(with_leading_zero(csv_row[amount_col_label])).to_d.abstype = csv_row["Transaction Type"]if type == "credit"- amount.to_d+ amountelse- amount.to_d * -1+ amount * -1endendprivate+ # sanitize_number needs a digit before the decimal separator, so ".50"+ # becomes "0.50" first.+ def with_leading_zero(value)+ separator = NUMBER_FORMATS.dig(number_format, :separator)+ return value if value.nil? || separator.blank?++ value.to_s.strip.sub(/\A-?(?=#{Regexp.escape(separator)}\d)/) { |sign| "#{sign}0" }+ end+def set_mappingsassign_attributes(self.class.default_column_mappings)save!diff --git a/test/models/mint_import_test.rb b/test/models/mint_import_test.rbindex 599e18b10d..35c0b21225 100644--- a/test/models/mint_import_test.rb+++ b/test/models/mint_import_test.rb@@ -24,4 +24,64 @@ class MintImportTest < ActiveSupport::TestCaseassert_equal (1..10).to_a, import.rows.order(:source_row_number).pluck(:source_row_number)end++ test "the transaction type sets the direction whatever the amount's sign" do+ import = mint_import(<<~CSV)+ Date,Amount,Account Name,Description,Category,Labels,Currency,Notes,Transaction Type+ 01/01/2024,8.55,Checking,Coffee,Food & Drink,,USD,,debit+ 01/02/2024,-8.55,Checking,Coffee,Food & Drink,,USD,,debit+ 01/03/2024,2000,Checking,Paycheck,Income,,USD,,credit+ 01/04/2024,-2000,Checking,Paycheck,Income,,USD,,credit+ CSV++ assert_equal [ 8.55, 8.55, -2000, -2000 ], signed_amounts(import)+ end++ test "the sample CSV imports its debit as an outflow and its credit as an inflow" do+ import = mint_import(MintImport.new.csv_template.to_csv)++ assert_equal [ 8.55, -2000 ], signed_amounts(import)+ end++ test "formatted amounts are parsed with the import's number format" do+ import = mint_import(<<~CSV)+ Date,Amount,Account Name,Description,Category,Labels,Currency,Notes,Transaction Type+ 01/01/2024,"$1,234.56",Checking,Rent,Rent,,USD,,debit+ 01/02/2024,"1,234.56",Checking,Refund,Rent,,USD,,credit+ CSV++ assert_equal [ 1234.56, -1234.56 ], signed_amounts(import)+ end++ test "amounts without a leading zero keep their value" do+ import = mint_import(<<~CSV)+ Date,Amount,Account Name,Description,Category,Labels,Currency,Notes,Transaction Type+ 01/01/2024,.50,Checking,Fee,Fees,,USD,,debit+ 01/02/2024,-.50,Checking,Fee,Fees,,USD,,debit+ 01/03/2024,.50,Checking,Refund,Fees,,USD,,credit+ 01/04/2024,-.50,Checking,Refund,Fees,,USD,,credit+ CSV++ assert_equal [ 0.5, 0.5, -0.5, -0.5 ], signed_amounts(import)+ end++ test "amounts without a leading zero use the import's decimal separator" do+ csv = <<~CSV+ Date;Amount;Account Name;Description;Category;Labels;Currency;Notes;Transaction Type+ 01/01/2024;,50;Checking;Fee;Fees;;EUR;;debit+ CSV+ import = @family.imports.create!(type: "MintImport", raw_file_str: csv, col_sep: ";", number_format: "1.234,56")+ import.generate_rows_from_csv++ assert_equal [ 0.5 ], signed_amounts(import)+ end++ private+ def mint_import(csv)+ @family.imports.create!(type: "MintImport", raw_file_str: csv, col_sep: ",").tap(&:generate_rows_from_csv)+ end++ def signed_amounts(import)+ import.rows.order(:source_row_number).map { |row| row.signed_amount.to_f }+ endend