<commit_before>#------------------------------------------------------------------------------
#
# DispositionUpdatesTemplateBuilder
#
# Creates a template for capturing status updates for existing inventory
#
#------------------------------------------------------------------------------
class TransitDispositionUpdatesTemplateBuilder < TemplateBuilder

  SHEET_NAME = DispositionUpdatesFileHandler::SHEET_NAME

  protected

  # Add a row for each of the asset for the org
  def add_rows(sheet)
    if @assets.nil?
      assets =  @asset_class_name.constantize.operational.where(organization_id: @organization.id).where(fta_asset_class_id: @search_parameter.id)
    else
      assets = @assets
    end

    assets.each do |asset|
      next unless asset.disposable?

      asset = Rails.application.config.asset_base_class_name.constantize.get_typed_asset(asset)

      row_data = []
      row_data << asset.object_key
      row_data << asset.organization.short_name
      row_data << asset.asset_tag
      row_data << asset.external_id
      row_data << asset.fta_asset_class.name
      row_data << asset.fta_type.name
      row_data << asset.asset_subtype
      row_data << asset.try(:esl_category).try(:name)
      row_data << asset.description
      if @fta_asset_class.class_name == 'Facility'|| asset.class.to_s == 'Facility'
        row_data << asset.parent.try(:facility_name) || asset.facility_name
      else
        row_data << asset.try(:serial_number)
      end


      # Disposition report
      row_data << nil
      row_data << nil
      row_data << nil

      if include_mileage_columns?
        row_data << nil
      end

      sheet.add_row row_data
    end
  end

  # Configure any other implementation specific options for the workbook
  # such as lookup table worksheets etc.
  def setup_workbook(workbook)

    # Add a lookup table worksheet and add the lookuptable values we need to it
    sheet = workbook.add_worksheet :name => 'lists', :state => :very_hidden
    sheet.sheet_protection.password = '<PASSWORD>'

    row = []
    @disposition_types = DispositionType.active.pluck(:name)
    @disposition_types.each do |x|
      row << x unless x.eql? "Unknown" or x.eql? "Transferred"
    end
    sheet.add_row row

  end

  # Performing post-processing
  def post_process(sheet)

    # protect sheet so you cannot update cells that are locked
    sheet.sheet_protection

    # Merge Cells
    sheet.merge_cells("A1:J1")

    if include_mileage_columns?
      sheet.merge_cells("K1:N1")
    else
      sheet.merge_cells("K1:M1")
    end


    # Add data validation constraints

    # This is used to get the column name of a lookup table based on its length
    alphabet = ('A'..'Z').to_a
    earliest_date = SystemConfig.instance.epoch

    # Disposition Type
    sheet.add_data_validation("K3:K1000", {
        :type => :list,
        :formula1 => "lists!$A$1:$#{<EMAIL>]}$1",
        :allow_blank => true,
        :showErrorMessage => true,
        :errorTitle => 'Wrong input',
        :error => 'Select a value from the list',
        :errorStyle => :stop,
        :showInputMessage => true,
        :promptTitle => 'Disposition Type',
        :prompt => 'Only values in the list are allowed'})

    # Disposition Date
    sheet.add_data_validation("L3:L1000", {
      :type => :time,
      :operator => :greaterThan,
      :formula1 => earliest_date.strftime("%-m/%d/%Y"),
      :allow_blank => true,
      :errorTitle => 'Wrong input',
      :error => "Date must be after #{earliest_date.strftime("%-m/%d/%Y")}",
      :errorStyle => :stop,
      :showInputMessage => true,
      :promptTitle => 'Disposition Date',
      :prompt => "Date must be after #{earliest_date.strftime("%-m/%d/%Y")}"})


    # Sales proceeds
    sheet.add_data_validation("M3:M1000", {
      :type => :whole,
      :operator => :greaterThanOrEqual,
      :formula1 => '0',
      :allow_blank => true,
      :showErrorMessage => true,
      :errorTitle => 'Wrong input',
      :error => 'Value must be greater than 0.',
      :errorStyle => :stop,
      :showInputMessage => true,
      :promptTitle => 'Sales proceeds',
      :prompt => 'Enter a value greater than or equal to 0'})

    if include_mileage_columns?
      # Mileage
      sheet.add_data_validation("N3:N1000", {
        :type => :whole,
        :operator => :greaterThanOrEqual,
        :formula1 => '0',
        :allow_blank => true,
        :showErrorMessage => true,
        :errorTitle => 'Wrong input',
        :error => 'Value must be greater than 0.',
        :errorStyle => :stop,
        :showInputMessage => true,
        :promptTitle => 'Mileage at disposition',
        :prompt => 'Enter a value greater than or equal to 0'})
    end

  end

  # header rows
  def header_rows

    title_row = [
        'Asset','','','','','','','',
    ]

    title_row << ''

    title_row.concat([
      'Disposition Report',
      '',
      ''
    ])

    if include_mileage_columns?
      title_row.concat([''])
    end

    detail_row = [
        'Object Key',
        'Agency',
        'Asset ID',
        'External ID',
        'Class',
        'Type',
        'Subtype',
        'ESL Category',
        'Description'
    ]
    if include_mileage_columns?
      detail_row << 'VIN'
    elsif @fta_asset_class.class_name == 'Facility' || (@assets && (@assets.very_specific.class.to_s == 'Facility'))
      detail_row << 'Facility Name'
    else
      detail_row << 'Serial Number'
    end

    detail_row.concat([
      # Disposition Update Columns
      'Disposition Type',<commit_msg>TTPLAT-693 fix check if fta_asset_class not set<commit_after>#------------------------------------------------------------------------------
#
# DispositionUpdatesTemplateBuilder
#
# Creates a template for capturing status updates for existing inventory
#
#------------------------------------------------------------------------------
class TransitDispositionUpdatesTemplateBuilder < TemplateBuilder

  SHEET_NAME = DispositionUpdatesFileHandler::SHEET_NAME

  protected

  # Add a row for each of the asset for the org
  def add_rows(sheet)
    if @assets.nil?
      assets =  @asset_class_name.constantize.operational.where(organization_id: @organization.id).where(fta_asset_class_id: @search_parameter.id)
    else
      assets = @assets
    end

    assets.each do |asset|
      next unless asset.disposable?

      asset = Rails.application.config.asset_base_class_name.constantize.get_typed_asset(asset)

      row_data = []
      row_data << asset.object_key
      row_data << asset.organization.short_name
      row_data << asset.asset_tag
      row_data << asset.external_id
      row_data << asset.fta_asset_class.name
      row_data << asset.fta_type.name
      row_data << asset.asset_subtype
      row_data << asset.try(:esl_category).try(:name)
      row_data << asset.description
      if @fta_asset_class.try(:class_name) == 'Facility'|| asset.class.to_s == 'Facility'
        row_data << asset.parent.try(:facility_name) || asset.facility_name
      else
        row_data << asset.try(:serial_number)
      end


      # Disposition report
      row_data << nil
      row_data << nil
      row_data << nil

      if include_mileage_columns?
        row_data << nil
      end

      sheet.add_row row_data
    end
  end

  # Configure any other implementation specific options for the workbook
  # such as lookup table worksheets etc.
  def setup_workbook(workbook)

    # Add a lookup table worksheet and add the lookuptable values we need to it
    sheet = workbook.add_worksheet :name => 'lists', :state => :very_hidden
    sheet.sheet_protection.password = '<PASSWORD>'

    row = []
    @disposition_types = DispositionType.active.pluck(:name)
    @disposition_types.each do |x|
      row << x unless x.eql? "Unknown" or x.eql? "Transferred"
    end
    sheet.add_row row

  end

  # Performing post-processing
  def post_process(sheet)

    # protect sheet so you cannot update cells that are locked
    sheet.sheet_protection

    # Merge Cells
    sheet.merge_cells("A1:J1")

    if include_mileage_columns?
      sheet.merge_cells("K1:N1")
    else
      sheet.merge_cells("K1:M1")
    end


    # Add data validation constraints

    # This is used to get the column name of a lookup table based on its length
    alphabet = ('A'..'Z').to_a
    earliest_date = SystemConfig.instance.epoch

    # Disposition Type
    sheet.add_data_validation("K3:K1000", {
        :type => :list,
        :formula1 => "lists!$A$1:$#{<EMAIL>]}$1",
        :allow_blank => true,
        :showErrorMessage => true,
        :errorTitle => 'Wrong input',
        :error => 'Select a value from the list',
        :errorStyle => :stop,
        :showInputMessage => true,
        :promptTitle => 'Disposition Type',
        :prompt => 'Only values in the list are allowed'})

    # Disposition Date
    sheet.add_data_validation("L3:L1000", {
      :type => :time,
      :operator => :greaterThan,
      :formula1 => earliest_date.strftime("%-m/%d/%Y"),
      :allow_blank => true,
      :errorTitle => 'Wrong input',
      :error => "Date must be after #{earliest_date.strftime("%-m/%d/%Y")}",
      :errorStyle => :stop,
      :showInputMessage => true,
      :promptTitle => 'Disposition Date',
      :prompt => "Date must be after #{earliest_date.strftime("%-m/%d/%Y")}"})


    # Sales proceeds
    sheet.add_data_validation("M3:M1000", {
      :type => :whole,
      :operator => :greaterThanOrEqual,
      :formula1 => '0',
      :allow_blank => true,
      :showErrorMessage => true,
      :errorTitle => 'Wrong input',
      :error => 'Value must be greater than 0.',
      :errorStyle => :stop,
      :showInputMessage => true,
      :promptTitle => 'Sales proceeds',
      :prompt => 'Enter a value greater than or equal to 0'})

    if include_mileage_columns?
      # Mileage
      sheet.add_data_validation("N3:N1000", {
        :type => :whole,
        :operator => :greaterThanOrEqual,
        :formula1 => '0',
        :allow_blank => true,
        :showErrorMessage => true,
        :errorTitle => 'Wrong input',
        :error => 'Value must be greater than 0.',
        :errorStyle => :stop,
        :showInputMessage => true,
        :promptTitle => 'Mileage at disposition',
        :prompt => 'Enter a value greater than or equal to 0'})
    end

  end

  # header rows
  def header_rows

    title_row = [
        'Asset','','','','','','','',
    ]

    title_row << ''

    title_row.concat([
      'Disposition Report',
      '',
      ''
    ])

    if include_mileage_columns?
      title_row.concat([''])
    end

    detail_row = [
        'Object Key',
        'Agency',
        'Asset ID',
        'External ID',
        'Class',
        'Type',
        'Subtype',
        'ESL Category',
        'Description'
    ]
    if include_mileage_columns?
      detail_row << 'VIN'
    elsif @fta_asset_class.try(:class_name) == 'Facility' || (@assets && (@assets.very_specific.class.to_s == 'Facility'))
      detail_row << 'Facility Name'
    else
      detail_row << 'Serial Number'
    end

    detail_row.concat([
      # Disposition Update Columns
      'Disposition Type',
{'fraction_non_alphanumeric': 0.1458109559613319, 'fraction_numerical': 0.00707124955245256, 'mean_word_length': 2.40640243902439, 'pattern_counts': {'":': 0, '<': 89, '<?xml version=': 0, '>': 95, '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': '138160', 'n_tokens_mistral': 3510, 'n_tokens_neox': 3333, 'n_words': 821}