Fix stock consumption in reports [SCI-6735]

This commit is contained in:
Anton 2022-04-14 14:46:09 +02:00
parent 4a232ac391
commit 84f91634d3
3 changed files with 23 additions and 11 deletions

View file

@ -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

View file

@ -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',

View file

@ -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