<commit_before>module Ekylibre
  # Permits to import accounts from a CSV file with 3 columns:
  #  - Name
  #  - Family (from nomenclature ActivityFamily)
  #  - Cultivation variety (from nomenclature Variety)
  class AccountsExchanger < ActiveExchanger::Base
    def check
      valid = true
      rows = CSV.read(file, headers: true).delete_if { |r| r[0].blank? }
      w.count = rows.size
      rows.each do |row|
        r = {
          number: row[0].to_s.strip.gsub(/0+\z/, ''),
          name: (row[1].blank? ? nil : row[1].to_s.strip),
          nature: (row[2].blank? ? nil : row[2].to_sym)
        }.to_struct

        w.check_point
      end
      valid
    end

    # Create or updates accounts
    def import
      rows = CSV.read(file, headers: true).delete_if { |r| r[0].blank? }
      w.count = rows.size

      rows.each_with_index do |row, index|
        line_number = index + 2
        r = {
          number: row[0].to_s.strip.gsub(/0+\z/, ''),
          name: (row[1].blank? ? nil : row[1].to_s),
          nature: (row[2].blank? ? nil : row[2].to_sym)
        }.to_struct

        # Exclude number dedicated to centralizing accounts
        next if r.number.eql?('401') || r.number.eql?('411')

        # get usage from parent account or import account from nomenclature
        usages = Account.find_parent_usage(r.number)

        attributes = {
          name: r.name,
          number: r.number,
          usages: usages,
          already_existing: true
        }

        account = Account.find_by(number: r.number) || Account.find_or_initialize_by(number: r.number.ljust(8, '0'))
        if r.number.start_with?('401', '411')
          attributes[:centralizing_account_name] = r.number.start_with?('401') ? 'suppliers' : 'clients'
          attributes[:auxiliary_number] = r.number[3, r.number.length]
          attributes[:nature] = 'auxiliary'
        end
        account.attributes = attributes
        account.save!

        w.check_point
      end
    end
  end
end
<commit_msg>Fix bad check of client supplier account during importing external account.<commit_after>module Ekylibre
  # Permits to import accounts from a CSV file with 3 columns:
  #  - Name
  #  - Family (from nomenclature ActivityFamily)
  #  - Cultivation variety (from nomenclature Variety)
  class AccountsExchanger < ActiveExchanger::Base
    def check
      valid = true
      rows = CSV.read(file, headers: true).delete_if { |r| r[0].blank? }
      w.count = rows.size
      rows.each do |row|
        r = {
          number: row[0].to_s,
          name: (row[1].blank? ? nil : row[1].to_s.strip),
          nature: (row[2].blank? ? nil : row[2].to_sym)
        }.to_struct

        w.check_point
      end
      valid
    end

    # Create or updates accounts
    def import
      rows = CSV.read(file, headers: true).delete_if { |r| r[0].blank? }
      w.count = rows.size

      rows.each_with_index do |row, index|
        line_number = index + 2
        r = {
          number: row[0].to_s.strip,
          name: (row[1].blank? ? nil : row[1].to_s),
          nature: (row[2].blank? ? nil : row[2].to_sym)
        }.to_struct

        # Exclude number dedicated to centralizing accounts
        next if r.number.strip.gsub(/0+\z/, '').in?(['401','411'])

        # get usage from parent account or import account from nomenclature
        usages = Account.find_parent_usage(r.number)

        attributes = {
          name: r.name,
          number: r.number,
          usages: usages,
          already_existing: true
        }

        account = Account.find_by(number: r.number) || Account.find_or_initialize_by(number: r.number.ljust(Preference[:account_number_digits], '0'))
        if r.number.start_with?('401', '411')
          attributes[:centralizing_account_name] = r.number.start_with?('401') ? 'suppliers' : 'clients'
          attributes[:auxiliary_number] = r.number[3, r.number.length]
          attributes[:nature] = 'auxiliary'
        end
        account.attributes = attributes
        account.save!

        w.check_point
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.12435740514075887, 'fraction_numerical': 0.016156670746634027, 'mean_word_length': 2.204705882352941, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '1878112', 'n_tokens_mistral': 1359, 'n_tokens_neox': 1287, 'n_words': 306}