diff --git a/app/models/import.rb b/app/models/import.rb index c77ed6c7..04a9b09e 100644 --- a/app/models/import.rb +++ b/app/models/import.rb @@ -71,10 +71,10 @@ class Import < ApplicationRecord { account: row[account_col_label].to_s, date: row[date_col_label].to_s, - qty: row[qty_col_label].to_s, + qty: sanitize_number(row[qty_col_label]).to_s, ticker: row[ticker_col_label].to_s, - price: row[price_col_label].to_s, - amount: row[amount_col_label].to_s, + price: sanitize_number(row[price_col_label]).to_s, + amount: sanitize_number(row[amount_col_label]).to_s, 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, @@ -113,6 +113,10 @@ class Import < ApplicationRecord cleaned? && mappings.all?(&:valid?) end + def requires_account? + family.accounts.empty? && mappings.accounts.where(key: "").any? + end + private def import! # no-op, subclasses can implement for customization of algorithm @@ -134,4 +138,9 @@ class Import < ApplicationRecord converters: [ ->(str) { str&.strip } ] ) end + + def sanitize_number(value) + return "" if value.nil? + value.gsub(/[^\d.]/, "") + end end diff --git a/app/models/import/row.rb b/app/models/import/row.rb index 8d434925..d4316a60 100644 --- a/app/models/import/row.rb +++ b/app/models/import/row.rb @@ -4,7 +4,7 @@ class Import::Row < ApplicationRecord validates :amount, numericality: true, allow_blank: true validates :currency, presence: true - validate :date_matches_user_format + validate :date_valid validate :required_columns validate :currency_is_valid @@ -54,13 +54,21 @@ class Import::Row < ApplicationRecord end end - def date_matches_user_format + def date_valid 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}") + return + end + + min_date = Account::Entry.min_supported_date + max_date = Date.current + + if parsed_date < min_date || parsed_date > max_date + errors.add(:date, "must be between #{min_date} and #{max_date}") end end diff --git a/app/views/import/cleans/show.html.erb b/app/views/import/cleans/show.html.erb index ef0c230f..25276fb9 100644 --- a/app/views/import/cleans/show.html.erb +++ b/app/views/import/cleans/show.html.erb @@ -23,7 +23,7 @@
You have errors in your data
+<%= t(".errors_notice") %>
CSV <%= mapping_label(mapping_class) %>
-Maybe <%= mapping_label(mapping_class) %>
-Rows
+<%= t(".csv_mapping_label", mapping: mapping_label(mapping_class)) %>
+<%= t(".maybe_mapping_label", mapping: mapping_label(mapping_class)) %>
+<%= t(".rows_label") %>