<commit_before>class FecExportJob < ActiveJob::Base
  queue_as :default
  include Rails.application.routes.url_helpers

  def perform(financial_year, fiscal_position, interval, user)
    interval ||= 'year'
    financial_year.split_into_periods(interval).each_with_index do |period, index|
      begin
        filename = interval == 'year' ? "#{Entity.of_company.siren_number}FEC#{financial_year.stopped_on.l(format: '%Y%m%d')}.xml" : "#{Entity.of_company.siren_number}FEC#{financial_year.stopped_on.l(format: '%Y%m%d')}_#{index + 1}.xml"
        fec = FEC::Exporter::XML.new(financial_year, fiscal_position, period.first, period.last)
        file_path = Ekylibre::Tenant.private_directory.join('tmp', "#{filename}")
        FileUtils.mkdir_p(file_path.dirname)
        File.write(file_path, fec.generate)
        document = Document.create!(nature: "exchange_accountancy_file_fr", key: "#{Time.now.to_i}-#{filename}", name: filename, file: File.open(file_path))
        notification = user.notifications.build(valid_generation_notification_params(file_path, filename, document.id))
      rescue => error
        Rails.logger.error $!
        Rails.logger.error $!.backtrace.join("\n")
        ExceptionNotifier.notify_exception($!, env: request.env, data: { message: "Raised during telepac import." })
        notification = user.notifications.build(error_generation_notification_params(filename, 'exchange_accountancy_file_fr', error.message))
      end
      notification.save
    end
  end
end<commit_msg>Fixed FEC export if company doesn't have siren number<commit_after>class FecExportJob < ActiveJob::Base
  queue_as :default
  include Rails.application.routes.url_helpers

  def perform(financial_year, fiscal_position, interval, user)
    interval ||= 'year'
    financial_year.split_into_periods(interval).each_with_index do |period, index|
      begin
        siren = Entity.of_company.siret_number.present? ? Entity.of_company.siren_number : ''
        filename = interval == 'year' ? "#{siren}FEC#{financial_year.stopped_on.l(format: '%Y%m%d')}.xml" : "#{siren}FEC#{financial_year.stopped_on.l(format: '%Y%m%d')}_#{index + 1}.xml"
        fec = FEC::Exporter::XML.new(financial_year, fiscal_position, period.first, period.last)
        file_path = Ekylibre::Tenant.private_directory.join('tmp', "#{filename}")
        FileUtils.mkdir_p(file_path.dirname)
        File.write(file_path, fec.generate)
        document = Document.create!(nature: "exchange_accountancy_file_fr", key: "#{Time.now.to_i}-#{filename}", name: filename, file: File.open(file_path))
        notification = user.notifications.build(valid_generation_notification_params(file_path, filename, document.id))
      rescue => error
        Rails.logger.error $!
        Rails.logger.error $!.backtrace.join("\n")
        notification = user.notifications.build(error_generation_notification_params(filename, 'exchange_accountancy_file_fr', error.message))
      end
      notification.save
    end
  end
end
{'fraction_non_alphanumeric': 0.13190492132574488, 'fraction_numerical': 0.0006695681285570807, 'mean_word_length': 4.543599257884972, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '7217438', 'n_tokens_mistral': 964, 'n_tokens_neox': 951, 'n_words': 149}