mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-12-10 06:06:24 +08:00
Merge pull request #7444 from G-Chubinidze/gc_SCI_10507
Enable "empty" inventory export [SCI-10507]
This commit is contained in:
commit
ce101130e3
6 changed files with 54 additions and 40 deletions
|
|
@ -365,7 +365,8 @@ class RepositoriesController < ApplicationController
|
|||
row_ids: params[:row_ids],
|
||||
header_ids: params[:header_ids]
|
||||
},
|
||||
file_type: params[:file_type]
|
||||
file_type: params[:empty_export] == '1' ? 'csv' : params[:file_type],
|
||||
empty_export: params[:empty_export] == '1'
|
||||
)
|
||||
log_activity(:export_inventory_items)
|
||||
render json: { message: t('zip_export.export_request_success') }
|
||||
|
|
|
|||
|
|
@ -35,9 +35,15 @@ class RepositoryZipExportJob < ZipExportJob
|
|||
params[:header_ids].map(&:to_i),
|
||||
@user,
|
||||
repository,
|
||||
in_module: params[:my_module_id].present?)
|
||||
in_module: params[:my_module_id].present?,
|
||||
empty_export: @empty_export)
|
||||
exported_data = service.export!
|
||||
File.binwrite("#{dir}/export.#{@file_type}", exported_data)
|
||||
|
||||
if @empty_export
|
||||
File.binwrite("#{dir}/Export_Inventory_Empty_#{Time.now.utc.strftime('%F %H-%M-%S_UTC')}.#{@file_type}", exported_data)
|
||||
else
|
||||
File.binwrite("#{dir}/export.#{@file_type}", exported_data)
|
||||
end
|
||||
end
|
||||
|
||||
def failed_notification_title
|
||||
|
|
|
|||
|
|
@ -3,9 +3,10 @@
|
|||
class ZipExportJob < ApplicationJob
|
||||
include FailedDeliveryNotifiableJob
|
||||
|
||||
def perform(user_id:, params: {}, file_type: :csv)
|
||||
def perform(user_id:, params: {}, file_type: :csv, empty_export: false)
|
||||
@user = User.find(user_id)
|
||||
@file_type = file_type.to_sym
|
||||
@empty_export = empty_export
|
||||
I18n.backend.date_format = @user.settings[:date_format] || Constants::DEFAULT_DATE_FORMAT
|
||||
zip_input_dir = FileUtils.mkdir_p(Rails.root.join("tmp/temp_zip_#{Time.now.to_i}").to_s).first
|
||||
zip_dir = FileUtils.mkdir_p(Rails.root.join('tmp/zip-ready').to_s).first
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
require 'csv'
|
||||
|
||||
module RepositoryCsvExport
|
||||
def self.to_csv(rows, column_ids, user, repository, handle_file_name_func, in_module)
|
||||
def self.to_csv(rows, column_ids, user, repository, handle_file_name_func, in_module, empty_export)
|
||||
# Parse column names
|
||||
csv_header = []
|
||||
add_consumption = in_module && !repository.is_a?(RepositorySnapshot) && repository.has_stock_management?
|
||||
|
|
@ -34,43 +34,45 @@ module RepositoryCsvExport
|
|||
|
||||
CSV.generate do |csv|
|
||||
csv << csv_header
|
||||
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.archived? && row.archived_by.present? ? row.archived_by.full_name : '')
|
||||
when -8
|
||||
csv_row << (row.archived? && row.archived_on.present? ? I18n.l(row.archived_on, format: :full) : '')
|
||||
when -9
|
||||
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)
|
||||
unless empty_export
|
||||
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.archived? && row.archived_by.present? ? row.archived_by.full_name : '')
|
||||
when -8
|
||||
csv_row << (row.archived? && row.archived_on.present? ? I18n.l(row.archived_on, format: :full) : '')
|
||||
when -9
|
||||
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)
|
||||
|
||||
csv_row << if cell
|
||||
if cell.value_type == 'RepositoryAssetValue' && handle_file_name_func
|
||||
handle_file_name_func.call(cell.value.asset)
|
||||
else
|
||||
SmartAnnotations::TagToText.new(
|
||||
user, repository.team, cell.value.export_formatted
|
||||
).text
|
||||
csv_row << if cell
|
||||
if cell.value_type == 'RepositoryAssetValue' && handle_file_name_func
|
||||
handle_file_name_func.call(cell.value.asset)
|
||||
else
|
||||
SmartAnnotations::TagToText.new(
|
||||
user, repository.team, cell.value.export_formatted
|
||||
).text
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
csv_row << row.row_consumption(row.stock_consumption) if add_consumption
|
||||
csv << csv_row
|
||||
end
|
||||
csv_row << row.row_consumption(row.stock_consumption) if add_consumption
|
||||
csv << csv_row
|
||||
end
|
||||
end.encode('UTF-8', invalid: :replace, undef: :replace)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class RepositoryExportService
|
||||
def initialize(file_type, rows, columns, user, repository, handle_name_func = nil, in_module: false)
|
||||
def initialize(file_type, rows, columns, user, repository, handle_name_func = nil, in_module: false, empty_export: false)
|
||||
@file_type = file_type
|
||||
@user = user
|
||||
@rows = rows
|
||||
|
|
@ -9,12 +9,13 @@ class RepositoryExportService
|
|||
@repository = repository
|
||||
@handle_name_func = handle_name_func
|
||||
@in_module = in_module
|
||||
@empty_export = empty_export
|
||||
end
|
||||
|
||||
def export!
|
||||
case @file_type
|
||||
when :csv
|
||||
file_data = RepositoryCsvExport.to_csv(@rows, @columns, @user, @repository, @handle_name_func, @in_module)
|
||||
file_data = RepositoryCsvExport.to_csv(@rows, @columns, @user, @repository, @handle_name_func, @in_module, @empty_export)
|
||||
when :xlsx
|
||||
file_data = RepositoryXlsxExport.to_xlsx(@rows, @columns, @user, @repository, @handle_name_func, @in_module)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -22,6 +22,9 @@
|
|||
|
||||
<%= f.radio_button :file_type, "csv", checked: true %>
|
||||
<%= f.label :file_type, "Csv", value: "csv", class: "mr-6 ml-3" %>
|
||||
|
||||
<%= f.label :empty_export, "Empty Export", class: "ml-1" %>
|
||||
<%= f.check_box :empty_export, id: "empty-export-checkbox", class: "mr-2" %>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type='button' data-e2e='e2e-BT-exportMD-cancel' class='btn btn-secondary' data-dismiss='modal' id='close-modal-export-repository-rows'><%= t('general.cancel')%></button>
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue