1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-08-04 21:15:19 +02:00

Allow CSV imports to be configured with single or multi-account mode (#1943)

* Allow CSV imports to be configured to a single account or multiple accounts

* Initialize import directly from accounts page

* Fix brakeman warnings

* Fix schema

* Fix Synth check
This commit is contained in:
Zach Gollwitzer 2025-03-03 12:47:30 -05:00 committed by GitHub
parent e907b073ed
commit c5da8ea550
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
20 changed files with 118 additions and 57 deletions

View file

@ -4,7 +4,11 @@ class TradeImport < Import
mappings.each(&:create_mappable!)
rows.each do |row|
account = mappings.accounts.mappable_for(row.account)
mapped_account = if account
account
else
mappings.accounts.mappable_for(row.account)
end
# Try to find or create security with ticker only
security = find_or_create_security(
@ -12,15 +16,15 @@ class TradeImport < Import
exchange_operating_mic: row.exchange_operating_mic
)
entry = account.entries.build \
entry = mapped_account.entries.build \
date: row.date_iso,
amount: row.signed_amount,
name: row.name,
currency: row.currency.presence || account.currency,
currency: row.currency.presence || mapped_account.currency,
entryable: Account::Trade.new(
security: security,
qty: row.qty,
currency: row.currency.presence || account.currency,
currency: row.currency.presence || mapped_account.currency,
price: row.price
),
import: self
@ -31,7 +35,9 @@ class TradeImport < Import
end
def mapping_steps
[ Import::AccountMapping ]
base = []
base << Import::AccountMapping if account.nil?
base
end
def required_column_keys
@ -39,14 +45,19 @@ class TradeImport < Import
end
def column_keys
%i[date ticker exchange_operating_mic currency qty price account name]
base = %i[date ticker exchange_operating_mic currency qty price name]
base.unshift(:account) if account.nil?
base
end
def dry_run
{
transactions: rows.count,
mappings = { transactions: rows.count }
mappings.merge(
accounts: Import::AccountMapping.for_import(self).creational.count
}
) if account.nil?
mappings
end
def csv_template
@ -57,7 +68,9 @@ class TradeImport < Import
05/17/2024,TSLA,XNAS,USD,2,700.50,Retirement Account,Tesla Inc. Purchase
CSV
CSV.parse(template, headers: true)
csv = CSV.parse(template, headers: true)
csv.delete("account") if account.present?
csv
end
private