2024-10-01 10:47:59 -04:00
|
|
|
class TransactionImport < Import
|
|
|
|
def import!
|
|
|
|
transaction do
|
|
|
|
mappings.each(&:create_mappable!)
|
|
|
|
|
2025-03-24 09:59:27 -04:00
|
|
|
transactions = rows.map do |row|
|
2025-03-03 12:47:30 -05:00
|
|
|
mapped_account = if account
|
|
|
|
account
|
|
|
|
else
|
|
|
|
mappings.accounts.mappable_for(row.account)
|
|
|
|
end
|
|
|
|
|
2024-10-01 10:47:59 -04:00
|
|
|
category = mappings.categories.mappable_for(row.category)
|
|
|
|
tags = row.tags_list.map { |tag| mappings.tags.mappable_for(tag) }.compact
|
|
|
|
|
2025-03-24 09:59:27 -04:00
|
|
|
Account::Transaction.new(
|
|
|
|
category: category,
|
|
|
|
tags: tags,
|
|
|
|
entry: Account::Entry.new(
|
|
|
|
account: mapped_account,
|
|
|
|
date: row.date_iso,
|
|
|
|
amount: row.signed_amount,
|
|
|
|
name: row.name,
|
|
|
|
currency: row.currency,
|
|
|
|
notes: row.notes,
|
|
|
|
import: self
|
|
|
|
)
|
|
|
|
)
|
2024-10-01 10:47:59 -04:00
|
|
|
end
|
2025-03-24 09:59:27 -04:00
|
|
|
|
|
|
|
Account::Transaction.import!(transactions, recursive: true)
|
2024-10-01 10:47:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def required_column_keys
|
|
|
|
%i[date amount]
|
|
|
|
end
|
|
|
|
|
|
|
|
def column_keys
|
2025-03-03 12:47:30 -05:00
|
|
|
base = %i[date amount name currency category tags notes]
|
|
|
|
base.unshift(:account) if account.nil?
|
|
|
|
base
|
2024-10-01 10:47:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def mapping_steps
|
2025-03-03 12:47:30 -05:00
|
|
|
base = [ Import::CategoryMapping, Import::TagMapping ]
|
|
|
|
base << Import::AccountMapping if account.nil?
|
|
|
|
base
|
2024-10-01 10:47:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def csv_template
|
|
|
|
template = <<-CSV
|
|
|
|
date*,amount*,name,currency,category,tags,account,notes
|
|
|
|
05/15/2024,-45.99,Grocery Store,USD,Food,groceries|essentials,Checking Account,Monthly grocery run
|
|
|
|
05/16/2024,1500.00,Salary,,Income,,Main Account,
|
|
|
|
05/17/2024,-12.50,Coffee Shop,,,coffee,,
|
|
|
|
CSV
|
|
|
|
|
2025-03-03 12:47:30 -05:00
|
|
|
csv = CSV.parse(template, headers: true)
|
|
|
|
csv.delete("account") if account.present?
|
|
|
|
csv
|
2024-10-01 10:47:59 -04:00
|
|
|
end
|
|
|
|
end
|