Add consume column to assigned item export [SCI-6841] (#4115)

* Add consume column to assigned item export [SCI-6841]

* Fix hound [SCI-6841]
This commit is contained in:
ajugo 2022-06-03 10:10:00 +02:00 committed by GitHub
parent 9d4c97f191
commit 13798374ed
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 16 deletions

View file

@ -3,7 +3,6 @@
class MyModule < ApplicationRecord
SEARCHABLE_ATTRIBUTES = ['my_modules.name', 'my_modules.description']
include ActionView::Helpers::NumberHelper
include ArchivableModel
include SearchableModel
include SearchableByNameModel
@ -325,18 +324,7 @@ class MyModule < ApplicationRecord
consumed_stock = row.repository_stock_consumption_cell&.value&.formatted
row_json << (consumed_stock || 0)
else
consumed_stock_formatted =
if row.repository_stock_cell.present?
consumed_stock = number_with_precision(
row.stock_consumption || 0,
precision: (row.repository.repository_stock_column.metadata['decimals'].to_i || 0),
strip_insignificant_zeros: true
)
"#{consumed_stock} #{row.repository_stock_value&.repository_stock_unit_item&.data}"
else
'-'
end
row_json << consumed_stock_formatted
row_json << row.row_consumption(row.stock_consumption)
end
end
data << row_json

View file

@ -1,6 +1,7 @@
# frozen_string_literal: true
class RepositoryRow < ApplicationRecord
include ActionView::Helpers::NumberHelper
include SearchableModel
include SearchableByNameModel
include ArchivableModel
@ -149,4 +150,17 @@ class RepositoryRow < ApplicationRecord
repository_cells.each { |cell| cell.snapshot!(row_snapshot) }
row_snapshot
end
def row_consumption(stock_consumption)
if repository_stock_cell.present?
consumed_stock = number_with_precision(
stock_consumption || 0,
precision: (repository.repository_stock_column.metadata['decimals'].to_i || 0),
strip_insignificant_zeros: true
)
"#{consumed_stock} #{repository_stock_value&.repository_stock_unit_item&.data}"
else
'-'
end
end
end

View file

@ -13,6 +13,13 @@ module RepositoryZipExport
.joins(:my_module_repository_rows)
.where(my_module_repository_rows: { my_module_id: params[:my_module_id] })
end
if repository.has_stock_management?
rows = rows.left_joins(my_module_repository_rows: :repository_stock_unit_item)
.select(
'repository_rows.*',
'my_module_repository_rows.stock_consumption'
)
end
else
ordered_row_ids = params[:row_ids]
id_row_map = RepositoryRow.where(id: ordered_row_ids,
@ -24,14 +31,15 @@ module RepositoryZipExport
zip = ZipExport.create(user: current_user)
zip.generate_exportable_zip(
current_user,
to_csv(rows, params[:header_ids], current_user, repository.team),
to_csv(rows, params[:header_ids], current_user, repository),
:repositories
)
end
def self.to_csv(rows, column_ids, user, team, handle_file_name_func = nil)
def self.to_csv(rows, column_ids, user, repository, handle_file_name_func = nil)
# Parse column names
csv_header = []
add_consumption = !repository.is_a?(RepositorySnapshot) && repository.has_stock_management?
column_ids.each do |c_id|
csv_header << case c_id.to_i
when -1, -2
@ -53,6 +61,7 @@ module RepositoryZipExport
column ? column.name : nil
end
end
csv_header << I18n.t('repositories.table.row_consumption') if add_consumption
CSV.generate do |csv|
csv << csv_header
@ -84,12 +93,14 @@ module RepositoryZipExport
handle_file_name_func.call(cell.value.asset)
else
SmartAnnotations::TagToText.new(
user, team, cell.value.export_formatted
user, repository.team, cell.value.export_formatted
).text
end
end
end
end
csv_row << row.row_consumption(row.stock_consumption) if add_consumption
csv << csv_row
end
end