mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-05 21:45:23 +02:00
Feature: Add "amount type" configuration column for CSV imports (#1947)
* Rough draft * Schema conflict update * Implement signage * Update system tests * Lint fixes
This commit is contained in:
parent
8cf077f28d
commit
c88fe2e3b2
21 changed files with 366 additions and 83 deletions
|
@ -10,6 +10,8 @@ class Import < ApplicationRecord
|
|||
"1,234" => { separator: "", delimiter: "," } # Zero-decimal currencies like JPY
|
||||
}.freeze
|
||||
|
||||
AMOUNT_TYPE_STRATEGIES = %w[signed_amount custom_column].freeze
|
||||
|
||||
belongs_to :family
|
||||
belongs_to :account, optional: true
|
||||
|
||||
|
@ -27,8 +29,9 @@ class Import < ApplicationRecord
|
|||
}, validate: true, default: "pending"
|
||||
|
||||
validates :type, inclusion: { in: TYPES }
|
||||
validates :amount_type_strategy, inclusion: { in: AMOUNT_TYPE_STRATEGIES }
|
||||
validates :col_sep, inclusion: { in: SEPARATORS.map(&:last) }
|
||||
validates :signage_convention, inclusion: { in: SIGNAGE_CONVENTIONS }
|
||||
validates :signage_convention, inclusion: { in: SIGNAGE_CONVENTIONS }, allow_nil: true
|
||||
validates :number_format, presence: true, inclusion: { in: NUMBER_FORMATS.keys }
|
||||
|
||||
has_many :rows, dependent: :destroy
|
||||
|
|
|
@ -44,7 +44,19 @@ class Import::Row < ApplicationRecord
|
|||
|
||||
# In the Maybe system, positive amounts == "outflows", so we must reverse signage
|
||||
def apply_transaction_signage_convention(value)
|
||||
value * (import.signage_convention == "inflows_positive" ? -1 : 1)
|
||||
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
|
||||
else
|
||||
value
|
||||
end
|
||||
else
|
||||
raise "Unknown amount type strategy for import: #{import.amount_type_strategy}"
|
||||
end
|
||||
end
|
||||
|
||||
def required_columns
|
||||
|
|
|
@ -31,6 +31,7 @@ class TradeImport < Import
|
|||
),
|
||||
)
|
||||
end
|
||||
|
||||
Trade.import!(trades, recursive: true)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -48,6 +48,12 @@ class TransactionImport < Import
|
|||
base
|
||||
end
|
||||
|
||||
def selectable_amount_type_values
|
||||
return [] if entity_type_col_label.nil?
|
||||
|
||||
csv_rows.map { |row| row[entity_type_col_label] }.uniq
|
||||
end
|
||||
|
||||
def csv_template
|
||||
template = <<-CSV
|
||||
date*,amount*,name,currency,category,tags,account,notes
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue