<commit_before># -*- coding: utf-8 -*-
# = Informations
#
# == License
#
# Ekylibre - Simple ERP
# Copyright (C) 2009-2013 <NAME>, <NAME>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see http://www.gnu.org/licenses.
#
# == Table: entities
#
#  active                    :boolean          default(TRUE), not null
#  activity_code             :string(30)
#  authorized_payments_count :integer
#  born_on                   :date
#  client                    :boolean          not null
#  client_account_id         :integer
#  country                   :string(2)
#  created_at                :datetime         not null
#  creator_id                :integer
#  currency                  :string(255)      not null
#  dead_on                   :date
#  deliveries_conditions     :string(60)
#  description               :text
#  first_met_on              :date
#  first_name                :string(255)
#  full_name                 :string(255)      not null
#  id                        :integer          not null, primary key
#  invoices_count            :integer
#  language                  :string(3)        not null
#  last_name                 :string(255)      not null
#  lock_version              :integer          default(0), not null
#  locked                    :boolean          not null
#  nature                    :string(255)      not null
#  number                    :string(60)
#  of_company                :boolean          not null
#  origin                    :string(255)
#  picture_content_type      :string(255)
#  picture_file_name         :string(255)
#  picture_file_size         :integer
#  picture_updated_at        :datetime
#  proposer_id               :integer
#  prospect                  :boolean          not null
#  reminder_submissive       :boolean          not null
#  responsible_id            :integer
#  siren                     :string(9)
#  supplier                  :boolean          not null
#  supplier_account_id       :integer
#  transporter               :boolean          not null
#  type                      :string(255)
#  updated_at                :datetime         not null
#  updater_id                :integer
#  vat_number                :string(20)
#  vat_subjected             :boolean          default(TRUE), not null
#

require "digest/sha2"

class Entity < Ekylibre::Record::Base
  attr_accessor :password_confirmation, :old_password
  # belongs_to :attorney_account, class_name: "Account"
  belongs_to :client_account, class_name: "Account"
  # belongs_to :nature, class_name: "EntityNature"
  enumerize :nature, in: Nomen::EntityNatures.all, default: Nomen::EntityNatures.default, predicates: {prefix: true}
  # belongs_to :payment_mode, class_name: "IncomingPaymentMode"
  belongs_to :proposer, class_name: "Entity"
  belongs_to :responsible, class_name: "User"
  belongs_to :supplier_account, class_name: "Account"
  has_many :clients, class_name: "Entity", foreign_key: :responsible_id, dependent: :nullify
  has_many :addresses, -> { where(:deleted_at => nil) }, class_name: "EntityAddress", inverse_of: :entity
  has_many :mails,     -> { where(:canal => "mail",    :deleted_at => nil) }, class_name: "EntityAddress", inverse_of: :entity
  has_many :emails,    -> { where(:canal => "email",   :deleted_at => nil) }, class_name: "EntityAddress", inverse_of: :entity
  has_many :phones,    -> { where(:canal => "phone",   :deleted_at => nil) }, class_name: "EntityAddress", inverse_of: :entity
  has_many :mobiles,   -> { where(:canal => "mobile",  :deleted_at => nil) }, class_name: "EntityAddress", inverse_of: :entity
  has_many :faxes,     -> { where(:canal => "fax",     :deleted_at => nil) }, class_name: "EntityAddress", inverse_of: :entity
  has_many :websites,  -> { where(:canal => "website", :deleted_at => nil) }, class_name: "EntityAddress", inverse_of: :entity
  has_many :auto_updateable_addresses, -> { where(:deleted_at => nil, :mail_auto_update => true) }, class_name: "EntityAddress"
  has_many :direct_links, class_name: "EntityLink", foreign_key: :entity_1_id
  has_many :product_events, class_name: "Log", foreign_key: :watcher_id
  has_many :godchildren, class_name: "Entity", foreign_key: "proposer_id"
  has_many :incoming_payments, foreign_key: :payer_id, inverse_of: :payer
  has_many :indirect_links, class_name: "EntityLink", foreign_key: :entity_2_id
  has_many :mandates
  has_many :ownerships, class_name: "ProductOwnership", foreign_key: :powner_id
  has_many :observations, :as => :subject
  has_many :participations, class_name: "EventParticipation", foreign_key: :participant_id
  has_many :prices, class_name: "ProductPriceTemplate"
  has_many :purchase_invoices, -> { where(:state => "invoice").order("created_on desc") }, class_name: "Purchase", foreign_key: :supplier_id
  has_many :purchases, foreign_key: :supplier_id
  has_many :outgoing_deliveries, foreign_key: :transporter_id
  has_many :outgoing_payments, foreign_key: :payee_id
  has_many :sales_invoices, -> { where(:state => "invoice").order("created_on desc") }, class_name: "Sale", foreign_key: :client_id
  has_many :sales, -> { order("created_on desc") }, foreign_key: :client_id
  has_many :sale_items, class_name: "SaleItem"
  has_many :subscriptions, foreign_key: :subscriber_id
  has_many :trackings, foreign_key: :producer_id
  has_many :transfers, foreign_key: :supplier_id
  has_many :transports, foreign_key: :transporter_id
  has_many :transporter_sales, -> { order("created_on desc") }, foreign_key: :transporter_id, class_name: "Sale"
  has_many :usable_incoming_payments, -> { where("used_amount < amount") }, class_name: "IncomingPayment", foreign_key: :payer_id
  has_many :waiting_deliveries, -> { where("moved_on IS NULL AND planned_on <= CURRENT_DATE") }, class_name: "OutgoingDelivery", foreign_key: :transporter_id
  has_one :default_mail_address, -> { where(:by_default => true, :canal => "mail") }, class_name: "EntityAddress"
  has_attached_file :picture, {
    :url => '/backend/:class/:id/picture/:style',
    :path => ':rails_root/private/:class/:attachment/:id_partition/:style.:extension',
    :styles => {
      :thumb => ["64x64#", :jpg],
      :identity => ["180x180#", :jpg]
      # :large => ["490x630#", :jpg] # Not used
    }
  }

  # # default_scope order(:last_name, :first_name)
  scope :necessary_transporters, -> { where("id IN (SELECT transporter_id FROM #{OutgoingDelivery.table_name} WHERE (sent_at IS NULL AND planned_at <= CURRENT_DATE) OR transport_id IS NULL)").order(:last_name, :first_name) }
  scope :suppliers,    -> { where(:supplier => true) }
  scope :transporters, -> { where(:transporter => true) }
  scope :clients,      -> { where(:client => true) }
  scope :related_to, lambda { |entity|
    where("id IN (SELECT entity_2_id FROM #{EntityLink.table_name} WHERE entity_1_id = ?) OR id IN (SELECT entity_1_id FROM #{EntityLink.table_name} WHERE entity_2_id = ?)", entity.id, entity.id)
  }

  #[VALIDATORS[ Do not edit these lines directly. Use `rake clean:validations`.
  validates_numericality_of :picture_file_size, allow_nil: true, only_integer: true
  validates_length_of :country, allow_nil: true, maximum: 2
  validates_length_of :language, allow_nil: true, maximum: 3
  validates_length_of :siren, allow_nil: true, maximum: 9
  validates_length_of :vat_number, allow_nil: true, maximum: 20
  validates_length_of :activity_code, allow_nil: true, maximum: 30
  validates_length_of :deliveries_conditions, :number, allow_nil: true, maximum: 60
  validates_length_of :currency, :first_name, :full_name, :last_name, :nature, :origin, :picture_content_type, :picture_file_name, allow_nil: true, maximum: 255
  validates_inclusion_of :active, :client, :locked, :of_company, :prospect, :reminder_submissive, :supplier, :transporter, :vat_subjected, in: [true, false]
  validates_presence_of :currency, :full_name, :language, :last_name, :nature
  #]VALIDATORS]

  alias_attribute :name, :full_name

  acts_as_numbered :number
  accepts_nested_attributes_for :mails,    :reject_if => :all_blank, :allow_destroy => true
  accepts_nested_attributes_for :emails,   :reject_if => :all_blank, :allow_destroy => true
  accepts_nested_attributes_for :phones,   :reject_if => :all_blank, :allow_destroy => true
  accepts_nested_attributes_for :mobiles,  :reject_if => :all_blank, :allow_destroy => true
  accepts_nested_attributes_for :faxes,    :reject_if => :all_blank, :allow_destroy => true
  accepts_nested_attributes_for :websites, :reject_if => :all_blank, :allow_destroy => true

  def self.of_company
    self.where(:of_company => true).first
  end

  before_validation do
    self.first_name = self.first_name.to_s.strip
    self.last_name  = self.last_name.to_s.strip
    self.full_name = (self.last_name.to_s + " " + self.first_name.to_s)
    # unless self.nature.nil?
    # self.full_name = (self.nature.title.to_s + ' ' + self.full_name).strip unless self.nature.in_name? # or self.nature.abbreviation == "-")
    # end
    self.full_name.strip!
    # self.name = self.name.to_s.strip.downcase.gsub(/[^a-z0-9\.\_]/,'')
    if entity = Entity.of_company
      self.language = entity.language if self.language.blank?
      self.currency = entity.currency if self.currency.blank?
    end
    return true
  end

  # validate do
    # if self.nature
      # if self.nature.in_name and not self.last_name.match(/( |^)#{self.nature.title}( |$)/i)
        # errors.add(:last_name, :missing_title, :title => self.nature.title)
      # end
    # end
    # return true
  # end

  after_save do
    self.auto_updateable_addresses.find_each do |a|
      a.mail_line_1 = self.full_name
      a.save
    end
  end

  protect(on: :destroy) do
    return false if self.id == self.of_company? or self.sales_invoices.count > 0 or self.participations.count > 0 and self.sales.count > 0 and self.transports.count > 0
    return true
  end


  def self.exportable_columns
    self.content_columns.delete_if{|c| [:active, :lock_version, :webpass, :soundex, :deliveries_conditions].include?(c.name.to_sym)}
  end

  # Returns an entity scope for.all other entities
  def others
    self.class.where("id != ?", (self.id || 0))
  end

  def label
    self.number.to_s + '. ' + self.full_name.to_s
  end

  #
  def created_on
    self.created_at.to_date
  end


  def last_incoming_payment
    self.incoming_payments.last_updateds.first
  end

  #
  def balance
    amount = 0.0
    amount += self.incoming_payments.sum(:amount)
    amount -= self.sales_invoices.sum(:amount)
    amount -= self.outgoing_payments.sum(:amount)
    amount += self.purchase_invoices.sum(:amount)
    return amount
  end

  def has_another_tracking?(serial, product_id)
    self.trackings.where("serial=? AND product_id!=? ", serial, product_id).count > 0
  end


  # This method creates automati.ally an account for the entity for its usage (client, supplier...)
  def account(nature)
    natures = {:client => :client, :payer => :client, :supplier => :supplier, :payee => :supplier, :attorney => :attorney}
    raise ArgumentError.new("Unknown nature #{nature.inspect} (#{natures.keys.to_sentence} are accepted)") unless natures.keys.include? nature
    valid_account = self.send(natures[nature].to_s + "_account")
    if valid_account.nil?
      prefix = Nomen::Accounts[nature.to_s.pluralize].send(Account.chart)
      if Preference[:use_entity_codes_for_account_numbers]
        number = prefix.to_s+self.number.to_s
        valid_account = Account.find_by_number(number)
        valid_account = Account.create(:number => number, :name => self.full_name, :reconcilable => true) unless valid_account
      else
        suffix = "1"
        suffix = suffix.upper_ascii[0..5].rjust(6, '0')
        account = 1
        #x=Time.now
        i = 0
        while not account.nil? do
          account = Account.where("number LIKE ?", prefix.to_s+suffix.to_s).first
          suffix.succ! unless account.nil?
          i=i+1
        end
        # puts "Find entity (#{x-Time.now}s) :"+i.to_s
        valid_account = Account.create(:number => prefix.to_s+suffix.to_s, :name => self.full_name, :reconcilable => true)
      end
      self.reload.update_column("#{natures[nature]}_account_id", valid_account.id)
    end
    return valid_account
  end

  def warning
    count = self.observations.find_all_by_importance("important").size<commit_msg>Try to fix bug on fr_pcg82 on NilClass<commit_after># -*- coding: utf-8 -*-
# = Informations
#
# == License
#
# Ekylibre - Simple ERP
# Copyright (C) 2009-2013 <NAME>, <NAME>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see http://www.gnu.org/licenses.
#
# == Table: entities
#
#  active                    :boolean          default(TRUE), not null
#  activity_code             :string(30)
#  authorized_payments_count :integer
#  born_on                   :date
#  client                    :boolean          not null
#  client_account_id         :integer
#  country                   :string(2)
#  created_at                :datetime         not null
#  creator_id                :integer
#  currency                  :string(255)      not null
#  dead_on                   :date
#  deliveries_conditions     :string(60)
#  description               :text
#  first_met_on              :date
#  first_name                :string(255)
#  full_name                 :string(255)      not null
#  id                        :integer          not null, primary key
#  invoices_count            :integer
#  language                  :string(3)        not null
#  last_name                 :string(255)      not null
#  lock_version              :integer          default(0), not null
#  locked                    :boolean          not null
#  nature                    :string(255)      not null
#  number                    :string(60)
#  of_company                :boolean          not null
#  origin                    :string(255)
#  picture_content_type      :string(255)
#  picture_file_name         :string(255)
#  picture_file_size         :integer
#  picture_updated_at        :datetime
#  proposer_id               :integer
#  prospect                  :boolean          not null
#  reminder_submissive       :boolean          not null
#  responsible_id            :integer
#  siren                     :string(9)
#  supplier                  :boolean          not null
#  supplier_account_id       :integer
#  transporter               :boolean          not null
#  type                      :string(255)
#  updated_at                :datetime         not null
#  updater_id                :integer
#  vat_number                :string(20)
#  vat_subjected             :boolean          default(TRUE), not null
#

require "digest/sha2"

class Entity < Ekylibre::Record::Base
  attr_accessor :password_confirmation, :old_password
  # belongs_to :attorney_account, class_name: "Account"
  belongs_to :client_account, class_name: "Account"
  # belongs_to :nature, class_name: "EntityNature"
  enumerize :nature, in: Nomen::EntityNatures.all, default: Nomen::EntityNatures.default, predicates: {prefix: true}
  # belongs_to :payment_mode, class_name: "IncomingPaymentMode"
  belongs_to :proposer, class_name: "Entity"
  belongs_to :responsible, class_name: "User"
  belongs_to :supplier_account, class_name: "Account"
  has_many :clients, class_name: "Entity", foreign_key: :responsible_id, dependent: :nullify
  has_many :addresses, -> { where(:deleted_at => nil) }, class_name: "EntityAddress", inverse_of: :entity
  has_many :mails,     -> { where(:canal => "mail",    :deleted_at => nil) }, class_name: "EntityAddress", inverse_of: :entity
  has_many :emails,    -> { where(:canal => "email",   :deleted_at => nil) }, class_name: "EntityAddress", inverse_of: :entity
  has_many :phones,    -> { where(:canal => "phone",   :deleted_at => nil) }, class_name: "EntityAddress", inverse_of: :entity
  has_many :mobiles,   -> { where(:canal => "mobile",  :deleted_at => nil) }, class_name: "EntityAddress", inverse_of: :entity
  has_many :faxes,     -> { where(:canal => "fax",     :deleted_at => nil) }, class_name: "EntityAddress", inverse_of: :entity
  has_many :websites,  -> { where(:canal => "website", :deleted_at => nil) }, class_name: "EntityAddress", inverse_of: :entity
  has_many :auto_updateable_addresses, -> { where(:deleted_at => nil, :mail_auto_update => true) }, class_name: "EntityAddress"
  has_many :direct_links, class_name: "EntityLink", foreign_key: :entity_1_id
  has_many :product_events, class_name: "Log", foreign_key: :watcher_id
  has_many :godchildren, class_name: "Entity", foreign_key: "proposer_id"
  has_many :incoming_payments, foreign_key: :payer_id, inverse_of: :payer
  has_many :indirect_links, class_name: "EntityLink", foreign_key: :entity_2_id
  has_many :mandates
  has_many :ownerships, class_name: "ProductOwnership", foreign_key: :powner_id
  has_many :observations, :as => :subject
  has_many :participations, class_name: "EventParticipation", foreign_key: :participant_id
  has_many :prices, class_name: "ProductPriceTemplate"
  has_many :purchase_invoices, -> { where(:state => "invoice").order("created_on desc") }, class_name: "Purchase", foreign_key: :supplier_id
  has_many :purchases, foreign_key: :supplier_id
  has_many :outgoing_deliveries, foreign_key: :transporter_id
  has_many :outgoing_payments, foreign_key: :payee_id
  has_many :sales_invoices, -> { where(:state => "invoice").order("created_on desc") }, class_name: "Sale", foreign_key: :client_id
  has_many :sales, -> { order("created_on desc") }, foreign_key: :client_id
  has_many :sale_items, class_name: "SaleItem"
  has_many :subscriptions, foreign_key: :subscriber_id
  has_many :trackings, foreign_key: :producer_id
  has_many :transfers, foreign_key: :supplier_id
  has_many :transports, foreign_key: :transporter_id
  has_many :transporter_sales, -> { order("created_on desc") }, foreign_key: :transporter_id, class_name: "Sale"
  has_many :usable_incoming_payments, -> { where("used_amount < amount") }, class_name: "IncomingPayment", foreign_key: :payer_id
  has_many :waiting_deliveries, -> { where("moved_on IS NULL AND planned_on <= CURRENT_DATE") }, class_name: "OutgoingDelivery", foreign_key: :transporter_id
  has_one :default_mail_address, -> { where(:by_default => true, :canal => "mail") }, class_name: "EntityAddress"
  has_attached_file :picture, {
    :url => '/backend/:class/:id/picture/:style',
    :path => ':rails_root/private/:class/:attachment/:id_partition/:style.:extension',
    :styles => {
      :thumb => ["64x64#", :jpg],
      :identity => ["180x180#", :jpg]
      # :large => ["490x630#", :jpg] # Not used
    }
  }

  # # default_scope order(:last_name, :first_name)
  scope :necessary_transporters, -> { where("id IN (SELECT transporter_id FROM #{OutgoingDelivery.table_name} WHERE (sent_at IS NULL AND planned_at <= CURRENT_DATE) OR transport_id IS NULL)").order(:last_name, :first_name) }
  scope :suppliers,    -> { where(:supplier => true) }
  scope :transporters, -> { where(:transporter => true) }
  scope :clients,      -> { where(:client => true) }
  scope :related_to, lambda { |entity|
    where("id IN (SELECT entity_2_id FROM #{EntityLink.table_name} WHERE entity_1_id = ?) OR id IN (SELECT entity_1_id FROM #{EntityLink.table_name} WHERE entity_2_id = ?)", entity.id, entity.id)
  }

  #[VALIDATORS[ Do not edit these lines directly. Use `rake clean:validations`.
  validates_numericality_of :picture_file_size, allow_nil: true, only_integer: true
  validates_length_of :country, allow_nil: true, maximum: 2
  validates_length_of :language, allow_nil: true, maximum: 3
  validates_length_of :siren, allow_nil: true, maximum: 9
  validates_length_of :vat_number, allow_nil: true, maximum: 20
  validates_length_of :activity_code, allow_nil: true, maximum: 30
  validates_length_of :deliveries_conditions, :number, allow_nil: true, maximum: 60
  validates_length_of :currency, :first_name, :full_name, :last_name, :nature, :origin, :picture_content_type, :picture_file_name, allow_nil: true, maximum: 255
  validates_inclusion_of :active, :client, :locked, :of_company, :prospect, :reminder_submissive, :supplier, :transporter, :vat_subjected, in: [true, false]
  validates_presence_of :currency, :full_name, :language, :last_name, :nature
  #]VALIDATORS]

  alias_attribute :name, :full_name

  acts_as_numbered :number
  accepts_nested_attributes_for :mails,    :reject_if => :all_blank, :allow_destroy => true
  accepts_nested_attributes_for :emails,   :reject_if => :all_blank, :allow_destroy => true
  accepts_nested_attributes_for :phones,   :reject_if => :all_blank, :allow_destroy => true
  accepts_nested_attributes_for :mobiles,  :reject_if => :all_blank, :allow_destroy => true
  accepts_nested_attributes_for :faxes,    :reject_if => :all_blank, :allow_destroy => true
  accepts_nested_attributes_for :websites, :reject_if => :all_blank, :allow_destroy => true

  def self.of_company
    self.where(:of_company => true).first
  end

  before_validation do
    self.first_name = self.first_name.to_s.strip
    self.last_name  = self.last_name.to_s.strip
    self.full_name = (self.last_name.to_s + " " + self.first_name.to_s)
    # unless self.nature.nil?
    # self.full_name = (self.nature.title.to_s + ' ' + self.full_name).strip unless self.nature.in_name? # or self.nature.abbreviation == "-")
    # end
    self.full_name.strip!
    # self.name = self.name.to_s.strip.downcase.gsub(/[^a-z0-9\.\_]/,'')
    if entity = Entity.of_company
      self.language = entity.language if self.language.blank?
      self.currency = entity.currency if self.currency.blank?
    end
    return true
  end

  # validate do
    # if self.nature
      # if self.nature.in_name and not self.last_name.match(/( |^)#{self.nature.title}( |$)/i)
        # errors.add(:last_name, :missing_title, :title => self.nature.title)
      # end
    # end
    # return true
  # end

  after_save do
    self.auto_updateable_addresses.find_each do |a|
      a.mail_line_1 = self.full_name
      a.save
    end
  end

  protect(on: :destroy) do
    return false if self.id == self.of_company? or self.sales_invoices.count > 0 or self.participations.count > 0 and self.sales.count > 0 and self.transports.count > 0
    return true
  end


  def self.exportable_columns
    self.content_columns.delete_if{|c| [:active, :lock_version, :webpass, :soundex, :deliveries_conditions].include?(c.name.to_sym)}
  end

  # Returns an entity scope for.all other entities
  def others
    self.class.where("id != ?", (self.id || 0))
  end

  def label
    self.number.to_s + '. ' + self.full_name.to_s
  end

  #
  def created_on
    self.created_at.to_date
  end


  def last_incoming_payment
    self.incoming_payments.last_updateds.first
  end

  #
  def balance
    amount = 0.0
    amount += self.incoming_payments.sum(:amount)
    amount -= self.sales_invoices.sum(:amount)
    amount -= self.outgoing_payments.sum(:amount)
    amount += self.purchase_invoices.sum(:amount)
    return amount
  end

  def has_another_tracking?(serial, product_id)
    self.trackings.where("serial=? AND product_id!=? ", serial, product_id).count > 0
  end


  # This method creates automati.ally an account for the entity for its usage (client, supplier...)
  def account(nature)
    natures = {:payer => :client, :payee => :supplier}
    nature = natures[nature] || nature
    raise ArgumentError.new("Unknown nature #{nature.inspect} (#{natures.keys.to_sentence} are accepted)") unless [:client, :supplier].include? nature
    valid_account = self.send(nature.to_s + "_account")
    if valid_account.nil?
      prefix = Nomen::Accounts[nature.to_s.pluralize].send(Account.chart)
      if Preference[:use_entity_codes_for_account_numbers]
        number = prefix.to_s+self.number.to_s
        valid_account = Account.find_by_number(number)
        valid_account = Account.create(:number => number, :name => self.full_name, :reconcilable => true) unless valid_account
      else
        suffix = "1"
        suffix = suffix.upper_ascii[0..5].rjust(6, '0')
        account = 1
        #x=Time.now
        i = 0
        while not account.nil? do
          account = Account.where("number LIKE ?", prefix.to_s+suffix.to_s).first
          suffix.succ! unless account.nil?
          i=i+1
        end
        # puts "Find entity (#{x-Time.now}s) :"+i.to_s
        valid_account = Account.create(:number => prefix.to_s+suffix.to_s, :name => self.full_name, :reconcilable => true)
      end
      self.reload.update_column("#{natures[nature]}_account_id", valid_account.id)
    end
    return valid_account
  end

  def warning
    count = self.observations.find_all_by_importance("important").size
{'fraction_non_alphanumeric': 0.11648069670697096, 'fraction_numerical': 0.00808677734147195, 'mean_word_length': 3.3834355828220857, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 160, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1028906', 'n_tokens_mistral': 8517, 'n_tokens_neox': 8235, 'n_words': 1954}