2018-11-07 12:04:29 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-05 22:54:04 +08:00
|
|
|
require 'csv'
|
2018-11-07 12:04:29 +08:00
|
|
|
|
2024-03-07 22:48:01 +08:00
|
|
|
module RepositoryCsvExport
|
2024-06-07 15:55:11 +08:00
|
|
|
def self.to_csv(rows, column_ids, user, repository, handle_file_name_func, in_module)
|
2018-03-05 22:54:04 +08:00
|
|
|
# Parse column names
|
|
|
|
csv_header = []
|
2022-06-10 21:46:22 +08:00
|
|
|
add_consumption = in_module && !repository.is_a?(RepositorySnapshot) && repository.has_stock_management?
|
2018-03-05 22:54:04 +08:00
|
|
|
column_ids.each do |c_id|
|
2023-12-06 17:24:07 +08:00
|
|
|
case c_id
|
2024-01-16 20:07:11 +08:00
|
|
|
when -1, -2
|
2023-12-06 17:24:07 +08:00
|
|
|
next
|
2024-01-16 20:07:11 +08:00
|
|
|
when -3
|
2023-12-06 17:24:07 +08:00
|
|
|
csv_header << I18n.t('repositories.table.id')
|
2024-01-16 20:07:11 +08:00
|
|
|
when -4
|
2023-12-06 17:24:07 +08:00
|
|
|
csv_header << I18n.t('repositories.table.row_name')
|
2024-01-16 20:07:11 +08:00
|
|
|
when -5
|
2023-12-06 17:24:07 +08:00
|
|
|
csv_header << I18n.t('repositories.table.added_by')
|
2024-01-16 20:07:11 +08:00
|
|
|
when -6
|
2023-12-06 17:24:07 +08:00
|
|
|
csv_header << I18n.t('repositories.table.added_on')
|
2024-01-16 20:07:11 +08:00
|
|
|
when -7
|
2024-05-22 23:23:39 +08:00
|
|
|
csv_header << I18n.t('repositories.table.updated_on')
|
2024-01-16 20:07:11 +08:00
|
|
|
when -8
|
2024-05-22 23:23:39 +08:00
|
|
|
csv_header << I18n.t('repositories.table.updated_by')
|
2024-01-16 20:07:11 +08:00
|
|
|
when -9
|
2024-05-22 23:23:39 +08:00
|
|
|
csv_header << I18n.t('repositories.table.archived_by')
|
|
|
|
when -10
|
|
|
|
csv_header << I18n.t('repositories.table.archived_on')
|
|
|
|
when -11
|
2023-12-06 17:24:07 +08:00
|
|
|
csv_header << I18n.t('repositories.table.parents')
|
|
|
|
csv_header << I18n.t('repositories.table.children')
|
|
|
|
else
|
|
|
|
csv_header << repository.repository_columns.find_by(id: c_id)&.name
|
|
|
|
end
|
2018-03-05 22:54:04 +08:00
|
|
|
end
|
2022-06-03 16:10:00 +08:00
|
|
|
csv_header << I18n.t('repositories.table.row_consumption') if add_consumption
|
2018-03-05 22:54:04 +08:00
|
|
|
|
2024-01-16 20:07:11 +08:00
|
|
|
CSV.generate do |csv|
|
2018-03-05 22:54:04 +08:00
|
|
|
csv << csv_header
|
2024-06-07 15:55:11 +08:00
|
|
|
rows.each do |row|
|
|
|
|
csv_row = []
|
|
|
|
column_ids.each do |c_id|
|
|
|
|
case c_id
|
|
|
|
when -1, -2
|
|
|
|
next
|
|
|
|
when -3
|
|
|
|
csv_row << (repository.is_a?(RepositorySnapshot) ? row.parent_id : row.code)
|
|
|
|
when -4
|
|
|
|
csv_row << row.name
|
|
|
|
when -5
|
|
|
|
csv_row << row.created_by.full_name
|
|
|
|
when -6
|
|
|
|
csv_row << I18n.l(row.created_at, format: :full)
|
|
|
|
when -7
|
|
|
|
csv_row << row.updated_at ? I18n.l(row.updated_at, format: :full) : ''
|
|
|
|
when -8
|
|
|
|
csv_row << row.last_modified_by.full_name
|
|
|
|
when -9
|
|
|
|
csv_row << (row.archived? && row.archived_by.present? ? row.archived_by.full_name : '')
|
|
|
|
when -10
|
|
|
|
csv_row << (row.archived? && row.archived_on.present? ? I18n.l(row.archived_on, format: :full) : '')
|
|
|
|
when -11
|
|
|
|
csv_row << row.parent_repository_rows.map(&:code).join(' | ')
|
|
|
|
csv_row << row.child_repository_rows.map(&:code).join(' | ')
|
|
|
|
else
|
|
|
|
cell = row.repository_cells.find_by(repository_column_id: c_id)
|
2018-09-17 05:01:44 +08:00
|
|
|
|
2023-12-06 17:24:07 +08:00
|
|
|
csv_row << if cell
|
2024-06-07 15:55:11 +08:00
|
|
|
if cell.value_type == 'RepositoryAssetValue' && handle_file_name_func
|
|
|
|
handle_file_name_func.call(cell.value.asset)
|
|
|
|
else
|
|
|
|
cell.value.export_formatted
|
|
|
|
end
|
2024-04-19 22:28:00 +08:00
|
|
|
end
|
2023-12-06 17:24:07 +08:00
|
|
|
end
|
2018-03-05 22:54:04 +08:00
|
|
|
end
|
2024-06-07 15:55:11 +08:00
|
|
|
csv_row << row.row_consumption(row.stock_consumption) if add_consumption
|
|
|
|
csv << csv_row
|
2018-03-05 22:54:04 +08:00
|
|
|
end
|
2024-03-07 22:48:01 +08:00
|
|
|
end.encode('UTF-8', invalid: :replace, undef: :replace)
|
2018-03-05 22:54:04 +08:00
|
|
|
end
|
|
|
|
end
|