diff --git a/app/controllers/import/configurations_controller.rb b/app/controllers/import/configurations_controller.rb index f723c63efbb..535b2575167 100644 --- a/app/controllers/import/configurations_controller.rb +++ b/app/controllers/import/configurations_controller.rb @@ -38,6 +38,7 @@ def import_params :number_format, :signage_convention, :amount_type_strategy, + :amount_type_identifier_value, :amount_type_inflow_value, ) end diff --git a/app/javascript/controllers/import_controller.js b/app/javascript/controllers/import_controller.js index 7c555d3ff50..9da24035f87 100644 --- a/app/javascript/controllers/import_controller.js +++ b/app/javascript/controllers/import_controller.js @@ -11,6 +11,7 @@ export default class extends Controller { "signedAmountFieldset", "customColumnFieldset", "amountTypeValue", + "amountTypeInflowValue", "amountTypeStrategySelect", ]; @@ -20,6 +21,9 @@ export default class extends Controller { this.amountTypeColumnKeyValue ) { this.#showAmountTypeValueTargets(this.amountTypeColumnKeyValue); + if (this.amountTypeValueTarget.querySelector("select")?.value) { + this.#showAmountTypeInflowValueTargets(); + } } } @@ -31,6 +35,9 @@ export default class extends Controller { if (this.amountTypeColumnKeyValue) { this.#showAmountTypeValueTargets(this.amountTypeColumnKeyValue); + if (this.amountTypeValueTarget.querySelector("select")?.value) { + this.#showAmountTypeInflowValueTargets(); + } } } @@ -45,6 +52,10 @@ export default class extends Controller { this.#showAmountTypeValueTargets(amountTypeColumnKey); } + handleAmountTypeIdentifierChange(event) { + this.#showAmountTypeInflowValueTargets(); + } + #showAmountTypeValueTargets(amountTypeColumnKey) { const selectableValues = this.#uniqueValuesForColumn(amountTypeColumnKey); @@ -72,6 +83,29 @@ export default class extends Controller { select.appendChild(fragment); } + #showAmountTypeInflowValueTargets() { + // Called when amount_type_identifier_value changes + // Updates the displayed identifier value in the UI text and shows/hides the inflow value dropdown + const identifierValueSelect = this.amountTypeValueTarget.querySelector("select"); + const selectedValue = identifierValueSelect.value; + + if (!selectedValue) { + this.amountTypeInflowValueTarget.classList.add("hidden"); + this.amountTypeInflowValueTarget.classList.remove("flex"); + return; + } + + // Show the inflow value dropdown + this.amountTypeInflowValueTarget.classList.remove("hidden"); + this.amountTypeInflowValueTarget.classList.add("flex"); + + // Update the displayed identifier value in the text + const identifierSpan = this.amountTypeInflowValueTarget.querySelector("span.font-medium"); + if (identifierSpan) { + identifierSpan.textContent = selectedValue; + } + } + #uniqueValuesForColumn(column) { const colIdx = this.csvValue[0].indexOf(column); const values = this.csvValue.slice(1).map((row) => row[colIdx]); @@ -101,6 +135,12 @@ export default class extends Controller { this.customColumnFieldsetTarget.classList.add("hidden"); this.signedAmountFieldsetTarget.classList.remove("hidden"); + // Hide the inflow value targets when using signed amount strategy + this.amountTypeValueTarget.classList.add("hidden"); + this.amountTypeValueTarget.classList.remove("flex"); + this.amountTypeInflowValueTarget.classList.add("hidden"); + this.amountTypeInflowValueTarget.classList.remove("flex"); + // Remove required from custom column fields this.customColumnFieldsetTarget .querySelectorAll("select, input") diff --git a/app/models/import.rb b/app/models/import.rb index 5d6bea64796..28e0bb2f588 100644 --- a/app/models/import.rb +++ b/app/models/import.rb @@ -35,6 +35,7 @@ class Import < ApplicationRecord validates :col_sep, inclusion: { in: SEPARATORS.map(&:last) } validates :signage_convention, inclusion: { in: SIGNAGE_CONVENTIONS }, allow_nil: true validates :number_format, presence: true, inclusion: { in: NUMBER_FORMATS.keys } + validate :custom_column_import_requires_identifier has_many :rows, dependent: :destroy has_many :mappings, dependent: :destroy @@ -287,4 +288,11 @@ def sanitize_number(value) def set_default_number_format self.number_format ||= "1,234.56" # Default to US/UK format end + + def custom_column_import_requires_identifier + return unless amount_type_strategy == "custom_column" + return if amount_type_identifier_value.present? || amount_type_inflow_value.present? + + errors.add(:base, "Custom column imports require either an identifier value or a legacy inflow indicator") + end end diff --git a/app/models/import/row.rb b/app/models/import/row.rb index ef16d26e551..8e9b6dc9f9d 100644 --- a/app/models/import/row.rb +++ b/app/models/import/row.rb @@ -47,12 +47,25 @@ def apply_transaction_signage_convention(value) if import.amount_type_strategy == "signed_amount" value * (import.signage_convention == "inflows_positive" ? -1 : 1) elsif import.amount_type_strategy == "custom_column" - inflow_value = import.amount_type_inflow_value - - if entity_type == inflow_value - value * -1 + legacy_identifier = import.amount_type_inflow_value + selected_identifier = + if import.amount_type_identifier_value.present? + import.amount_type_identifier_value + else + legacy_identifier + end + + inflow_treatment = + if import.amount_type_inflow_value.in?(%w[inflows_positive inflows_negative]) + import.amount_type_inflow_value + else + "inflows_positive" + end + + if entity_type == selected_identifier + value * (inflow_treatment == "inflows_positive" ? -1 : 1) else - value + value * (inflow_treatment == "inflows_positive" ? 1 : -1) end else raise "Unknown amount type strategy for import: #{import.amount_type_strategy}" diff --git a/app/views/import/configurations/_transaction_import.html.erb b/app/views/import/configurations/_transaction_import.html.erb index 84301d6910e..7a391892640 100644 --- a/app/views/import/configurations/_transaction_import.html.erb +++ b/app/views/import/configurations/_transaction_import.html.erb @@ -78,11 +78,21 @@
" data-import-target="amountTypeValue"> Set - <%= form.select :amount_type_inflow_value, + <%= form.select :amount_type_identifier_value, @import.selectable_amount_type_values, - { prompt: "Select column", container_class: "w-48 px-3 py-1.5 border border-secondary rounded-md" }, + { prompt: "Select value", container_class: "w-48 px-3 py-1.5 border border-secondary rounded-md" }, + required: @import.amount_type_strategy == "custom_column", + data: { action: "import#handleAmountTypeIdentifierChange" } %> + as identifier value +
+ +
" data-import-target="amountTypeInflowValue"> + + Treat "<%= @import.amount_type_identifier_value %>" as + <%= form.select :amount_type_inflow_value, + [["Income (inflow)", "inflows_positive"], ["Expense (outflow)", "inflows_negative"]], + { prompt: "Select type", container_class: "w-48 px-3 py-1.5 border border-secondary rounded-md" }, required: @import.amount_type_strategy == "custom_column" %> - as "income" (inflow) value
<% end %> diff --git a/db/migrate/20251002120000_add_amount_type_identifier_value_to_imports.rb b/db/migrate/20251002120000_add_amount_type_identifier_value_to_imports.rb new file mode 100644 index 00000000000..bb910fdf2aa --- /dev/null +++ b/db/migrate/20251002120000_add_amount_type_identifier_value_to_imports.rb @@ -0,0 +1,5 @@ +class AddAmountTypeIdentifierValueToImports < ActiveRecord::Migration[7.2] + def change + add_column :imports, :amount_type_identifier_value, :string + end +end diff --git a/test/models/transaction_import_test.rb b/test/models/transaction_import_test.rb index dc94b20cfeb..25de8237589 100644 --- a/test/models/transaction_import_test.rb +++ b/test/models/transaction_import_test.rb @@ -84,7 +84,8 @@ class TransactionImportTest < ActiveSupport::TestCase date_format: "%m/%d/%Y", amount_col_label: "amount", entity_type_col_label: "amount_type", - amount_type_inflow_value: "debit", + amount_type_identifier_value: "debit", + amount_type_inflow_value: "inflows_positive", amount_type_strategy: "custom_column", signage_convention: nil # Explicitly set to nil to prove this is not needed )