2024-10-01 10:47:59 -04:00
|
|
|
class Import::Row < ApplicationRecord
|
|
|
|
belongs_to :import
|
|
|
|
|
|
|
|
validates :amount, numericality: true, allow_blank: true
|
|
|
|
validates :currency, presence: true
|
|
|
|
|
2024-10-10 15:14:38 -04:00
|
|
|
validate :date_valid
|
2024-10-01 10:47:59 -04:00
|
|
|
validate :required_columns
|
|
|
|
validate :currency_is_valid
|
|
|
|
|
|
|
|
scope :ordered, -> { order(:id) }
|
|
|
|
|
|
|
|
def tags_list
|
|
|
|
if tags.blank?
|
|
|
|
[ "" ]
|
|
|
|
else
|
|
|
|
tags.split("|").map(&:strip)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def date_iso
|
|
|
|
Date.strptime(date, import.date_format).iso8601
|
|
|
|
end
|
|
|
|
|
|
|
|
def signed_amount
|
|
|
|
if import.type == "TradeImport"
|
2024-10-03 14:59:24 -04:00
|
|
|
price.to_d * apply_trade_signage_convention(qty.to_d)
|
2024-10-01 10:47:59 -04:00
|
|
|
else
|
2024-10-03 14:59:24 -04:00
|
|
|
apply_transaction_signage_convention(amount.to_d)
|
2024-10-01 10:47:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2025-03-04 13:10:01 -05:00
|
|
|
def update_and_sync(params)
|
|
|
|
assign_attributes(params)
|
|
|
|
save!(validate: false)
|
|
|
|
import.sync_mappings
|
2024-10-01 10:47:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2024-10-03 14:59:24 -04:00
|
|
|
# In the Maybe system, positive quantities == "inflows"
|
|
|
|
def apply_trade_signage_convention(value)
|
2024-10-01 10:47:59 -04:00
|
|
|
value * (import.signage_convention == "inflows_positive" ? 1 : -1)
|
|
|
|
end
|
|
|
|
|
2024-10-03 14:59:24 -04:00
|
|
|
# In the Maybe system, positive amounts == "outflows", so we must reverse signage
|
|
|
|
def apply_transaction_signage_convention(value)
|
2025-04-18 10:48:10 -04:00
|
|
|
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
|
2024-10-03 14:59:24 -04:00
|
|
|
end
|
|
|
|
|
2024-10-01 10:47:59 -04:00
|
|
|
def required_columns
|
|
|
|
import.required_column_keys.each do |required_key|
|
|
|
|
errors.add(required_key, "is required") if self[required_key].blank?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-10-10 15:14:38 -04:00
|
|
|
def date_valid
|
2024-10-01 10:47:59 -04:00
|
|
|
return if date.blank?
|
|
|
|
|
|
|
|
parsed_date = Date.strptime(date, import.date_format) rescue nil
|
|
|
|
|
|
|
|
if parsed_date.nil?
|
|
|
|
errors.add(:date, "must exactly match the format: #{import.date_format}")
|
2024-10-10 15:14:38 -04:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2025-04-14 11:40:34 -04:00
|
|
|
min_date = Entry.min_supported_date
|
2024-10-10 15:14:38 -04:00
|
|
|
max_date = Date.current
|
|
|
|
|
|
|
|
if parsed_date < min_date || parsed_date > max_date
|
|
|
|
errors.add(:date, "must be between #{min_date} and #{max_date}")
|
2024-10-01 10:47:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def currency_is_valid
|
|
|
|
return true if currency.blank?
|
|
|
|
|
|
|
|
begin
|
|
|
|
Money::Currency.new(currency)
|
|
|
|
rescue Money::Currency::UnknownCurrencyError
|
|
|
|
errors.add(:currency, "is not a valid currency code")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|