<commit_before># encoding: utf-8

class ImportDeclaration < ActiveRecord::Base
  def new?
    status == 'new'
  end

  def error?
    status == 'error'
  end

  def data
    JSON.parse read_attribute('data')
  end

  def pretty_data
    JSON.pretty_generate data
  end

  def as_json(options = {})
    super.merge(
      :pretty_data => pretty_data
    )
  end

  def self.search(params)
    relation = order('created_at desc')
    relation = relation.where(:status => params[:status]) if params[:status]
    relation
  end

  def self.import_all!
    where("status in (?)", %w(new error)).each do |id|
      id.import!
    end
  end

  def self.delete_imported!
    Declaration.delete_all
    DeclarationOtherWorkplace.delete_all
    DeclarationRealEstate.delete_all
    DeclarationCompany.delete_all
    DeclarationSecurity.delete_all
    DeclarationVehicle.delete_all
    DeclarationCash.delete_all
    DeclarationIncome.delete_all
    DeclarationDeal.delete_all
    update_all("status = 'new'")
  end

  def import!
    import_head
    import_other_workplaces
    import_real_estates
    import_companies_and_securities
    import_vehicles
    import_cash
    import_income
    import_deals
    import_debts
    import_loans
    import_other_facts
    import_relatives

    self.error = nil
    self.status = 'imported'
    save!
  rescue => e
    self.error = "#{e.message}\n#{e.backtrace[0..2].join("\n")}"
    self.status = 'error'
    save!
  end

  def import_head
    head = data[1]
    kind, period_year = head["Deklarācijas veids"].split('-')
    kind = kind.strip
    if period_year =~ /(\d+)/
      period_year = $1
    end
    @declaration = Declaration.new(
      :kind => kind,
      :period_year => period_year,
      :full_name => head["<NAME>"],
      :workplace => head["Darbavieta"],
      :position => head["Amats"],
      :submitted_on => parse_date(head["Iesniegta VID"]),
      :published_on => parse_date(head["Publicēta"])
    )
    @declaration.save!
  end

  private

  def parse_date(string)
    if string =~ /^(\d\d)\.(\d\d)\.(\d\d\d\d)$/
      Date.new($3.to_i, $2.to_i, $1.to_i)
    end
  end

  # returns [registration_number, legal_address]
  def parse_legal_address(string)
    if string =~ /^\s*(\d+)\s*(.*)$/
      [$1, $2]
    else
      [nil, string]
    end
  end

  # returns [source, registration_number, legal_address]
  def parse_source(string)
    if string =~ /^\s*(.*),\s*(\d+),\s*(.*)$/
      [$1, $2, $3]
    else
      [nil, nil, string]
    end
  end

  RATES = {
    'LVL' => 1,
    'EUR' => 0.702804,
    'USD' => 0.544000,
    'GBP' => 0.875000,
    'RUB'  => 0.017500
  }

  def convert_amount_lvl(amount, currency)
    raise ArgumentError, "trūkst #{currency} valūtas kurss" unless rate = RATES[currency]
    amount.to_f * rate
  end

  # return [amount, currency, amount_lvl]
  def parse_amount(amount, currency)
    if currency.blank?
      amount, currency = amount.split(' ')
    end<commit_msg>ignore amount if it contains no digits<commit_after># encoding: utf-8

class ImportDeclaration < ActiveRecord::Base
  def new?
    status == 'new'
  end

  def error?
    status == 'error'
  end

  def data
    JSON.parse read_attribute('data')
  end

  def pretty_data
    JSON.pretty_generate data
  end

  def as_json(options = {})
    super.merge(
      :pretty_data => pretty_data
    )
  end

  def self.search(params)
    relation = order('created_at desc')
    relation = relation.where(:status => params[:status]) if params[:status]
    relation
  end

  def self.import_all!
    where("status in (?)", %w(new error)).each do |id|
      id.import!
    end
  end

  def self.delete_imported!
    Declaration.delete_all
    DeclarationOtherWorkplace.delete_all
    DeclarationRealEstate.delete_all
    DeclarationCompany.delete_all
    DeclarationSecurity.delete_all
    DeclarationVehicle.delete_all
    DeclarationCash.delete_all
    DeclarationIncome.delete_all
    DeclarationDeal.delete_all
    update_all("status = 'new'")
  end

  def import!
    import_head
    import_other_workplaces
    import_real_estates
    import_companies_and_securities
    import_vehicles
    import_cash
    import_income
    import_deals
    import_debts
    import_loans
    import_other_facts
    import_relatives

    self.error = nil
    self.status = 'imported'
    save!
  rescue => e
    self.error = "#{e.message}\n#{e.backtrace[0..2].join("\n")}"
    self.status = 'error'
    save!
  end

  def import_head
    head = data[1]
    kind, period_year = head["Deklarācijas veids"].split('-')
    kind = kind.strip
    if period_year =~ /(\d+)/
      period_year = $1
    end
    @declaration = Declaration.new(
      :kind => kind,
      :period_year => period_year,
      :full_name => head["<NAME>"],
      :workplace => head["Darbavieta"],
      :position => head["Amats"],
      :submitted_on => parse_date(head["Iesniegta VID"]),
      :published_on => parse_date(head["Publicēta"])
    )
    @declaration.save!
  end

  private

  def parse_date(string)
    if string =~ /^(\d\d)\.(\d\d)\.(\d\d\d\d)$/
      Date.new($3.to_i, $2.to_i, $1.to_i)
    end
  end

  # returns [registration_number, legal_address]
  def parse_legal_address(string)
    if string =~ /^\s*(\d+)\s*(.*)$/
      [$1, $2]
    else
      [nil, string]
    end
  end

  # returns [source, registration_number, legal_address]
  def parse_source(string)
    if string =~ /^\s*(.*),\s*(\d+),\s*(.*)$/
      [$1, $2, $3]
    else
      [nil, nil, string]
    end
  end

  RATES = {
    'LVL' => 1,
    'EUR' => 0.702804,
    'USD' => 0.544000,
    'GBP' => 0.875000,
    'RUB'  => 0.017500
  }

  def convert_amount_lvl(amount, currency)
    raise ArgumentError, "trūkst #{currency} valūtas kurss" unless rate = RATES[currency]
    amount.to_f * rate
  end

  # return [amount, currency, amount_lvl]
  def parse_amount(amount, currency)
    return [nil, nil, nil] unless amount.to_s =~ /\d/

    if currency.blank?
      amount, currency = amount.split(' ')
    end
{'fraction_non_alphanumeric': 0.13313313313313313, 'fraction_numerical': 0.014014014014014014, 'mean_word_length': 2.775188916876574, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '341288', 'n_tokens_mistral': 2227, 'n_tokens_neox': 2109, 'n_words': 440}