diff --git a/app/models/my_module.rb b/app/models/my_module.rb index a20786781..8e36f2494 100644 --- a/app/models/my_module.rb +++ b/app/models/my_module.rb @@ -300,28 +300,35 @@ class MyModule < ApplicationRecord headers = [ I18n.t('repositories.table.id'), I18n.t('repositories.table.row_name'), - I18n.t('repositories.table.added_by'), - I18n.t('repositories.table.') + I18n.t('repositories.table.added_on'), + I18n.t('repositories.table.added_by') ] data = [] rows = repository.assigned_rows(self).includes(:created_by).order(created_at: order) if repository.has_stock_management? headers.push(I18n.t('repositories.table.row_consumption')) - rows = rows.joins(:my_module_repository_rows, repository_stock_value: :repository_stock_unit_item) - .where(my_module_repository_rows: { my_module: self }) - .select( + rows = rows.left_joins(my_module_repository_rows: :repository_stock_unit_item) + .select( 'repository_rows.*', 'my_module_repository_rows.stock_consumption', 'repository_stock_unit_items.data AS stock_unit' ) end rows.find_each do |row| + row_json = [] row_json << row.code row_json << (row.archived ? "#{row.name} [#{I18n.t('general.archived')}]" : row.name) row_json << I18n.l(row.created_at, format: :full) row_json << row.created_by.full_name - row_json << "#{row['stock_consumption'] || 0} #{row['stock_unit']}" if repository.has_stock_management? + if repository.has_stock_management? + if repository.is_a?(RepositorySnapshot) + consumed_stock = row.repository_stock_consumption_cell&.value&.formatted + row_json << (consumed_stock || 0) + else + row_json << "#{row['stock_consumption'] || 0} #{row['stock_unit']}" + end + end data << row_json end diff --git a/app/services/reports/docx/draw_my_module_repository.rb b/app/services/reports/docx/draw_my_module_repository.rb index 0575ce25d..e6560c46e 100644 --- a/app/services/reports/docx/draw_my_module_repository.rb +++ b/app/services/reports/docx/draw_my_module_repository.rb @@ -13,7 +13,7 @@ module Reports::Docx::DrawMyModuleRepository return false unless repository_data[:rows].any? && can_read_repository?(@user, repository) - table = prepare_row_columns(repository_data, my_module) + table = prepare_row_columns(repository_data, my_module, repository) @docx.p @docx.p I18n.t('projects.reports.elements.module_repository.name', diff --git a/app/services/reports/docx/repository_helper.rb b/app/services/reports/docx/repository_helper.rb index 1417ed94c..b63ce9aaa 100644 --- a/app/services/reports/docx/repository_helper.rb +++ b/app/services/reports/docx/repository_helper.rb @@ -3,7 +3,7 @@ module Reports::Docx::RepositoryHelper include InputSanitizeHelper - def prepare_row_columns(repository_data, my_module) + def prepare_row_columns(repository_data, my_module, repository) result = [repository_data[:headers]] repository_data[:rows].each do |record| row = [] @@ -16,9 +16,14 @@ module Reports::Docx::RepositoryHelper custom_cells = record.repository_cells custom_cells.each do |cell| if cell.value.instance_of? RepositoryStockValue - consumption = record.my_module_repository_rows.find_by(my_module: my_module).stock_consumption || 0 - unit = cell.value.repository_stock_unit_item&.data - cell_values[cell.repository_column_id] = "#{consumption} #{unit}" + if repository.is_a?(RepositorySnapshot) + consumed_stock = record.repository_stock_consumption_cell&.value&.formatted || 0 + cell_values[cell.repository_column_id] = consumed_stock + else + consumption = record.my_module_repository_rows.find_by(my_module: my_module)&.stock_consumption || 0 + unit = cell.value.repository_stock_unit_item&.data + cell_values[cell.repository_column_id] = "#{consumption} #{unit}" + end else cell_values[cell.repository_column_id] = cell.value.formatted end