diff --git a/app/models/import.rb b/app/models/import.rb index 639841ce1..536209371 100644 --- a/app/models/import.rb +++ b/app/models/import.rb @@ -244,7 +244,7 @@ class Import < ApplicationRecord end def default_currency - family.currency + account&.currency || family.currency end def parsed_csv diff --git a/app/models/mint_import.rb b/app/models/mint_import.rb index da9ced2af..d62da471d 100644 --- a/app/models/mint_import.rb +++ b/app/models/mint_import.rb @@ -29,11 +29,14 @@ class MintImport < Import category = mappings.categories.mappable_for(row.category) tags = row.tags_list.map { |tag| mappings.tags.mappable_for(tag) }.compact + # Use account's currency when no currency column was mapped in CSV, with family currency as fallback + effective_currency = currency_col_label.present? ? row.currency : (account.currency.presence || family.currency) + entry = account.entries.build \ date: row.date_iso, amount: row.signed_amount, name: row.name, - currency: row.currency, + currency: effective_currency, notes: row.notes, entryable: Transaction.new(category: category, tags: tags), import: self diff --git a/app/models/transaction_import.rb b/app/models/transaction_import.rb index 5a1adbafa..11c7d2326 100644 --- a/app/models/transaction_import.rb +++ b/app/models/transaction_import.rb @@ -27,6 +27,9 @@ class TransactionImport < Import category = mappings.categories.mappable_for(row.category) tags = row.tags_list.map { |tag| mappings.tags.mappable_for(tag) }.compact + # Use account's currency when no currency column was mapped in CSV, with family currency as fallback + effective_currency = currency_col_label.present? ? row.currency : (mapped_account.currency.presence || family.currency) + # Check for duplicate transactions using the adapter's deduplication logic # Pass claimed_entry_ids to exclude entries we've already matched in this import # This ensures identical rows within the CSV are all imported as separate transactions @@ -34,7 +37,7 @@ class TransactionImport < Import duplicate_entry = adapter.find_duplicate_transaction( date: row.date_iso, amount: row.signed_amount, - currency: row.currency, + currency: effective_currency, name: row.name, exclude_entry_ids: claimed_entry_ids ) @@ -57,7 +60,7 @@ class TransactionImport < Import date: row.date_iso, amount: row.signed_amount, name: row.name, - currency: row.currency, + currency: effective_currency, notes: row.notes, import: self ) diff --git a/test/models/transaction_import_test.rb b/test/models/transaction_import_test.rb index 88ebf0acd..c3e505d2e 100644 --- a/test/models/transaction_import_test.rb +++ b/test/models/transaction_import_test.rb @@ -277,6 +277,41 @@ class TransactionImportTest < ActiveSupport::TestCase ).count end + test "uses family currency as fallback when account has no currency and no CSV currency column" do + account = accounts(:depository) + family = account.family + + # Clear the account's currency to simulate an account without currency set + account.update_column(:currency, nil) + + import_csv = <<~CSV + date,name,amount + 01/01/2024,Test Transaction,100 + CSV + + @import.update!( + account: account, + raw_file_str: import_csv, + date_col_label: "date", + amount_col_label: "amount", + name_col_label: "name", + date_format: "%m/%d/%Y", + amount_type_strategy: "signed_amount", + signage_convention: "inflows_negative" + ) + + @import.generate_rows_from_csv + @import.reload + + assert_difference -> { Entry.count } => 1 do + @import.publish + end + + # The transaction should have the family's currency as fallback + entry = @import.entries.first + assert_equal family.currency, entry.currency + end + test "does not raise error when all accounts are properly mapped" do # Import CSV with multiple accounts, all mapped import_csv = <<~CSV