Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
3.4 KB
Raw Blame
class MintImport < Import
  after_create :set_mappings

  DEFAULT_COLUMN_MAPPINGS = {
    signage_convention: "inflows_positive",
    date_col_label: "Date",
    date_format: "%m/%d/%Y",
    name_col_label: "Description",
    amount_col_label: "Amount",
    currency_col_label: "Currency",
    account_col_label: "Account Name",
    category_col_label: "Category",
    tags_col_label: "Labels",
    notes_col_label: "Notes",
    entity_type_col_label: "Transaction Type"
  }.freeze

  def self.default_column_mappings
    DEFAULT_COLUMN_MAPPINGS
  end

  def generate_rows_from_csv
    rows.destroy_all

    mapped_rows = csv_rows.map.with_index(1) do |row, index|
      {
        source_row_number: index,
        account: row[account_col_label].to_s,
        date: row[date_col_label].to_s,
        amount: signed_csv_amount(row).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,
        tags: row[tags_col_label].to_s,
        notes: row[notes_col_label].to_s
      }
    end

    rows.insert_all!(mapped_rows)
    update_column(:rows_count, rows.count)
  end

  def import!
    transaction do
      mappings.each(&:create_mappable!)

      rows.each do |row|
        account = mappings.accounts.mappable_for(row.account)
        category = mappings.categories.mappable_for(row.category)
        tags = row.tags_list.map { |tag| mappings.tags.mappable_for(tag) }.compact

        # Use account's currency when no currency column was mapped in CSV, with family currency as fallback
        effective_currency = currency_col_label.present? ? row.currency : (account.currency.presence || family.currency)

        entry = account.entries.build \
          date: row.date_iso,
          amount: row.signed_amount,
          name: row.name,
          currency: effective_currency,
          notes: row.notes,
          entryable: Transaction.new(category: category, tags: tags),
          import: self

        entry.save!
      end
    end
  end

  def mapping_steps
    [ Import::CategoryMapping, Import::TagMapping, Import::AccountMapping ]
  end

  def required_column_keys
    %i[date amount]
  end

  def column_keys
    %i[date amount name currency category tags account notes]
  end

  def csv_template
    template = <<-CSV
      Date,Amount,Account Name,Description,Category,Labels,Currency,Notes,Transaction Type
      01/01/2024,-8.55,Checking,Starbucks,Food & Drink,Coffee|Breakfast,USD,Morning coffee,debit
      04/15/2024,2000,Savings,Paycheck,Income,,USD,Bi-weekly salary,credit
    CSV

    CSV.parse(template, headers: true)
  end

  # The Transaction Type column carries the direction, so the amount is read as
  # a magnitude whatever its sign.
  def signed_csv_amount(csv_row)
    amount = sanitize_number(with_leading_zero(csv_row[amount_col_label])).to_d.abs
    type = csv_row["Transaction Type"]

    if type == "credit"
      amount
    else
      amount * -1
    end
  end

  private
    # sanitize_number needs a digit before the decimal separator, so ".50"
    # becomes "0.50" first.
    def with_leading_zero(value)
      separator = NUMBER_FORMATS.dig(number_format, :separator)
      return value if value.nil? || separator.blank?

      value.to_s.strip.sub(/\A-?(?=#{Regexp.escape(separator)}\d)/) { |sign| "#{sign}0" }
    end

    def set_mappings
      assign_attributes(self.class.default_column_mappings)
      save!
    end
end