2024-05-17 09:09:32 -04:00
|
|
|
class Import < ApplicationRecord
|
2024-10-01 10:47:59 -04:00
|
|
|
TYPES = %w[TransactionImport TradeImport AccountImport MintImport].freeze
|
|
|
|
SIGNAGE_CONVENTIONS = %w[inflows_positive inflows_negative]
|
2025-03-03 12:47:30 -05:00
|
|
|
SEPARATORS = [ [ "Comma (,)", "," ], [ "Semicolon (;)", ";" ] ].freeze
|
2024-05-17 09:09:32 -04:00
|
|
|
|
2025-02-10 16:31:28 -04:00
|
|
|
NUMBER_FORMATS = {
|
|
|
|
"1,234.56" => { separator: ".", delimiter: "," }, # US/UK/Asia
|
|
|
|
"1.234,56" => { separator: ",", delimiter: "." }, # Most of Europe
|
|
|
|
"1 234,56" => { separator: ",", delimiter: " " }, # French/Scandinavian
|
|
|
|
"1,234" => { separator: "", delimiter: "," } # Zero-decimal currencies like JPY
|
|
|
|
}.freeze
|
|
|
|
|
2024-10-01 10:47:59 -04:00
|
|
|
belongs_to :family
|
2025-03-03 12:47:30 -05:00
|
|
|
belongs_to :account, optional: true
|
2024-05-17 09:09:32 -04:00
|
|
|
|
2025-02-10 16:31:28 -04:00
|
|
|
before_validation :set_default_number_format
|
|
|
|
|
2024-10-01 10:47:59 -04:00
|
|
|
scope :ordered, -> { order(created_at: :desc) }
|
2024-05-17 09:09:32 -04:00
|
|
|
|
2025-02-07 15:36:05 -05:00
|
|
|
enum :status, {
|
|
|
|
pending: "pending",
|
|
|
|
complete: "complete",
|
|
|
|
importing: "importing",
|
|
|
|
reverting: "reverting",
|
|
|
|
revert_failed: "revert_failed",
|
|
|
|
failed: "failed"
|
|
|
|
}, validate: true, default: "pending"
|
2024-05-17 09:09:32 -04:00
|
|
|
|
2024-10-01 10:47:59 -04:00
|
|
|
validates :type, inclusion: { in: TYPES }
|
2025-03-03 12:47:30 -05:00
|
|
|
validates :col_sep, inclusion: { in: SEPARATORS.map(&:last) }
|
2024-10-01 10:47:59 -04:00
|
|
|
validates :signage_convention, inclusion: { in: SIGNAGE_CONVENTIONS }
|
2025-02-10 16:31:28 -04:00
|
|
|
validates :number_format, presence: true, inclusion: { in: NUMBER_FORMATS.keys }
|
2024-05-17 09:09:32 -04:00
|
|
|
|
2024-10-01 10:47:59 -04:00
|
|
|
has_many :rows, dependent: :destroy
|
|
|
|
has_many :mappings, dependent: :destroy
|
|
|
|
has_many :accounts, dependent: :destroy
|
|
|
|
has_many :entries, dependent: :destroy, class_name: "Account::Entry"
|
2024-05-22 14:12:56 +02:00
|
|
|
|
2025-02-28 12:21:07 -05:00
|
|
|
class << self
|
|
|
|
def parse_csv_str(csv_str, col_sep: ",")
|
|
|
|
CSV.parse(
|
|
|
|
(csv_str || "").strip,
|
|
|
|
headers: true,
|
|
|
|
col_sep: col_sep,
|
|
|
|
converters: [ ->(str) { str&.strip } ],
|
|
|
|
liberal_parsing: true
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-05-17 09:09:32 -04:00
|
|
|
def publish_later
|
2024-10-01 10:47:59 -04:00
|
|
|
raise "Import is not publishable" unless publishable?
|
2024-05-17 09:09:32 -04:00
|
|
|
|
2024-10-01 10:47:59 -04:00
|
|
|
update! status: :importing
|
2024-05-17 09:09:32 -04:00
|
|
|
|
2024-10-01 10:47:59 -04:00
|
|
|
ImportJob.perform_later(self)
|
2024-05-17 09:09:32 -04:00
|
|
|
end
|
|
|
|
|
2024-10-01 10:47:59 -04:00
|
|
|
def publish
|
|
|
|
import!
|
2024-05-17 09:09:32 -04:00
|
|
|
|
2024-10-01 10:47:59 -04:00
|
|
|
family.sync
|
2024-05-17 09:09:32 -04:00
|
|
|
|
2024-10-01 10:47:59 -04:00
|
|
|
update! status: :complete
|
|
|
|
rescue => error
|
|
|
|
update! status: :failed, error: error.message
|
2024-05-17 09:09:32 -04:00
|
|
|
end
|
|
|
|
|
2025-02-07 15:36:05 -05:00
|
|
|
def revert_later
|
|
|
|
raise "Import is not revertable" unless revertable?
|
|
|
|
|
|
|
|
update! status: :reverting
|
|
|
|
|
|
|
|
RevertImportJob.perform_later(self)
|
|
|
|
end
|
|
|
|
|
|
|
|
def revert
|
|
|
|
Import.transaction do
|
|
|
|
accounts.destroy_all
|
|
|
|
entries.destroy_all
|
|
|
|
end
|
|
|
|
|
|
|
|
family.sync
|
|
|
|
|
|
|
|
update! status: :pending
|
|
|
|
rescue => error
|
|
|
|
update! status: :revert_failed, error: error.message
|
|
|
|
end
|
|
|
|
|
2024-10-01 10:47:59 -04:00
|
|
|
def csv_rows
|
|
|
|
@csv_rows ||= parsed_csv
|
2024-05-17 09:09:32 -04:00
|
|
|
end
|
|
|
|
|
2024-10-01 10:47:59 -04:00
|
|
|
def csv_headers
|
|
|
|
parsed_csv.headers
|
2024-05-17 09:09:32 -04:00
|
|
|
end
|
|
|
|
|
2024-10-01 10:47:59 -04:00
|
|
|
def csv_sample
|
|
|
|
@csv_sample ||= parsed_csv.first(2)
|
2024-05-17 09:09:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def dry_run
|
2025-03-03 12:47:30 -05:00
|
|
|
mappings = {
|
2024-10-01 10:47:59 -04:00
|
|
|
transactions: rows.count,
|
|
|
|
categories: Import::CategoryMapping.for_import(self).creational.count,
|
|
|
|
tags: Import::TagMapping.for_import(self).creational.count
|
|
|
|
}
|
2025-03-03 12:47:30 -05:00
|
|
|
|
|
|
|
mappings.merge(
|
|
|
|
accounts: Import::AccountMapping.for_import(self).creational.count,
|
|
|
|
) if account.nil?
|
|
|
|
|
|
|
|
mappings
|
2024-05-17 09:09:32 -04:00
|
|
|
end
|
|
|
|
|
2024-10-01 10:47:59 -04:00
|
|
|
def required_column_keys
|
|
|
|
[]
|
2024-05-17 09:09:32 -04:00
|
|
|
end
|
|
|
|
|
2024-10-01 10:47:59 -04:00
|
|
|
def column_keys
|
|
|
|
raise NotImplementedError, "Subclass must implement column_keys"
|
|
|
|
end
|
2024-05-17 09:09:32 -04:00
|
|
|
|
2024-10-01 10:47:59 -04:00
|
|
|
def generate_rows_from_csv
|
|
|
|
rows.destroy_all
|
|
|
|
|
2025-02-21 08:58:06 -05:00
|
|
|
mapped_rows = csv_rows.map do |row|
|
|
|
|
{
|
2024-10-01 10:47:59 -04:00
|
|
|
account: row[account_col_label].to_s,
|
|
|
|
date: row[date_col_label].to_s,
|
2024-10-10 15:14:38 -04:00
|
|
|
qty: sanitize_number(row[qty_col_label]).to_s,
|
2024-10-01 10:47:59 -04:00
|
|
|
ticker: row[ticker_col_label].to_s,
|
2025-02-24 16:00:24 +01:00
|
|
|
exchange_operating_mic: row[exchange_operating_mic_col_label].to_s,
|
2024-10-10 15:14:38 -04:00
|
|
|
price: sanitize_number(row[price_col_label]).to_s,
|
|
|
|
amount: sanitize_number(row[amount_col_label]).to_s,
|
2024-10-01 10:47:59 -04:00
|
|
|
currency: (row[currency_col_label] || default_currency).to_s,
|
|
|
|
name: (row[name_col_label] || default_row_name).to_s,
|
|
|
|
category: row[category_col_label].to_s,
|
|
|
|
tags: row[tags_col_label].to_s,
|
|
|
|
entity_type: row[entity_type_col_label].to_s,
|
|
|
|
notes: row[notes_col_label].to_s
|
2025-02-21 08:58:06 -05:00
|
|
|
}
|
2024-05-17 09:09:32 -04:00
|
|
|
end
|
2025-02-21 08:58:06 -05:00
|
|
|
|
|
|
|
rows.insert_all!(mapped_rows)
|
2024-10-01 10:47:59 -04:00
|
|
|
end
|
2024-05-17 09:09:32 -04:00
|
|
|
|
2024-10-01 10:47:59 -04:00
|
|
|
def sync_mappings
|
|
|
|
mapping_steps.each do |mapping|
|
|
|
|
mapping.sync(self)
|
2024-05-17 09:09:32 -04:00
|
|
|
end
|
2024-10-01 10:47:59 -04:00
|
|
|
end
|
2024-05-17 09:09:32 -04:00
|
|
|
|
2024-10-01 10:47:59 -04:00
|
|
|
def mapping_steps
|
|
|
|
[]
|
|
|
|
end
|
2024-05-22 10:02:03 -04:00
|
|
|
|
2024-10-01 10:47:59 -04:00
|
|
|
def uploaded?
|
|
|
|
raw_file_str.present?
|
|
|
|
end
|
2024-05-23 08:09:33 -04:00
|
|
|
|
2024-10-01 10:47:59 -04:00
|
|
|
def configured?
|
|
|
|
uploaded? && rows.any?
|
|
|
|
end
|
2024-05-22 10:02:03 -04:00
|
|
|
|
2024-10-01 10:47:59 -04:00
|
|
|
def cleaned?
|
|
|
|
configured? && rows.all?(&:valid?)
|
|
|
|
end
|
2024-05-17 09:09:32 -04:00
|
|
|
|
2024-10-01 10:47:59 -04:00
|
|
|
def publishable?
|
|
|
|
cleaned? && mappings.all?(&:valid?)
|
|
|
|
end
|
2024-05-17 09:09:32 -04:00
|
|
|
|
2025-02-07 15:36:05 -05:00
|
|
|
def revertable?
|
|
|
|
complete? || revert_failed?
|
|
|
|
end
|
|
|
|
|
2024-10-10 15:51:36 -04:00
|
|
|
def has_unassigned_account?
|
|
|
|
mappings.accounts.where(key: "").any?
|
|
|
|
end
|
|
|
|
|
2024-10-10 15:14:38 -04:00
|
|
|
def requires_account?
|
2024-10-10 15:51:36 -04:00
|
|
|
family.accounts.empty? && has_unassigned_account?
|
2024-10-10 15:14:38 -04:00
|
|
|
end
|
|
|
|
|
2024-10-01 10:47:59 -04:00
|
|
|
private
|
|
|
|
def import!
|
|
|
|
# no-op, subclasses can implement for customization of algorithm
|
2024-05-17 09:09:32 -04:00
|
|
|
end
|
|
|
|
|
2024-10-01 10:47:59 -04:00
|
|
|
def default_row_name
|
|
|
|
"Imported item"
|
2024-05-17 09:09:32 -04:00
|
|
|
end
|
|
|
|
|
2024-10-01 10:47:59 -04:00
|
|
|
def default_currency
|
|
|
|
family.currency
|
2024-05-17 09:09:32 -04:00
|
|
|
end
|
|
|
|
|
2024-10-01 10:47:59 -04:00
|
|
|
def parsed_csv
|
2025-02-28 12:21:07 -05:00
|
|
|
@parsed_csv ||= self.class.parse_csv_str(raw_file_str, col_sep: col_sep)
|
2024-05-17 09:09:32 -04:00
|
|
|
end
|
2024-10-10 15:14:38 -04:00
|
|
|
|
|
|
|
def sanitize_number(value)
|
|
|
|
return "" if value.nil?
|
2025-02-10 16:31:28 -04:00
|
|
|
|
|
|
|
format = NUMBER_FORMATS[number_format]
|
|
|
|
return "" unless format
|
|
|
|
|
|
|
|
# First, normalize spaces and remove any characters that aren't numbers, delimiters, separators, or minus signs
|
|
|
|
sanitized = value.to_s.strip
|
|
|
|
|
|
|
|
# Handle French/Scandinavian format specially
|
|
|
|
if format[:delimiter] == " "
|
|
|
|
sanitized = sanitized.gsub(/\s+/, "") # Remove all spaces first
|
|
|
|
else
|
|
|
|
sanitized = sanitized.gsub(/[^\d#{Regexp.escape(format[:delimiter])}#{Regexp.escape(format[:separator])}\-]/, "")
|
|
|
|
|
|
|
|
# Replace delimiter with empty string
|
|
|
|
if format[:delimiter].present?
|
|
|
|
sanitized = sanitized.gsub(format[:delimiter], "")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Replace separator with period for proper float parsing
|
|
|
|
if format[:separator].present?
|
|
|
|
sanitized = sanitized.gsub(format[:separator], ".")
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return empty string if not a valid number
|
|
|
|
unless sanitized =~ /\A-?\d+\.?\d*\z/
|
|
|
|
return ""
|
|
|
|
end
|
|
|
|
|
|
|
|
sanitized
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_default_number_format
|
|
|
|
self.number_format ||= "1,234.56" # Default to US/UK format
|
2024-10-10 15:14:38 -04:00
|
|
|
end
|
2024-05-17 09:09:32 -04:00
|
|
|
end
|