diff --git a/app/models/my_module.rb b/app/models/my_module.rb index feb3630fc..f4fbe7634 100644 --- a/app/models/my_module.rb +++ b/app/models/my_module.rb @@ -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 diff --git a/app/models/repository_row.rb b/app/models/repository_row.rb index ad1bcc278..a06e9598d 100644 --- a/app/models/repository_row.rb +++ b/app/models/repository_row.rb @@ -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 diff --git a/app/services/repository_zip_export.rb b/app/services/repository_zip_export.rb index 13cc0ea91..feffd2a02 100644 --- a/app/services/repository_zip_export.rb +++ b/app/services/repository_zip_export.rb @@ -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