2019-12-12 22:33:29 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Reports::Docx::RepositoryHelper
|
|
|
|
include InputSanitizeHelper
|
2022-04-25 23:10:44 +08:00
|
|
|
include ActionView::Helpers::NumberHelper
|
2019-12-12 22:33:29 +08:00
|
|
|
|
2022-04-14 20:46:09 +08:00
|
|
|
def prepare_row_columns(repository_data, my_module, repository)
|
2019-12-12 22:33:29 +08:00
|
|
|
result = [repository_data[:headers]]
|
2020-01-16 21:42:26 +08:00
|
|
|
repository_data[:rows].each do |record|
|
2019-12-12 22:33:29 +08:00
|
|
|
row = []
|
2021-07-19 20:23:36 +08:00
|
|
|
row.push(record.code)
|
2020-06-18 14:52:59 +08:00
|
|
|
row.push(escape_input(record.archived ? "#{record.name} [#{I18n.t('general.archived')}]" : record.name))
|
2019-12-12 22:33:29 +08:00
|
|
|
row.push(I18n.l(record.created_at, format: :full))
|
|
|
|
row.push(escape_input(record.created_by.full_name))
|
|
|
|
|
2020-01-07 21:21:55 +08:00
|
|
|
cell_values = {}
|
2019-12-12 22:33:29 +08:00
|
|
|
custom_cells = record.repository_cells
|
2020-01-07 21:21:55 +08:00
|
|
|
custom_cells.each do |cell|
|
2022-02-11 17:09:52 +08:00
|
|
|
if cell.value.instance_of? RepositoryStockValue
|
2022-04-14 20:46:09 +08:00
|
|
|
if repository.is_a?(RepositorySnapshot)
|
|
|
|
consumed_stock = record.repository_stock_consumption_cell&.value&.formatted || 0
|
|
|
|
cell_values[cell.repository_column_id] = consumed_stock
|
|
|
|
else
|
2022-04-25 23:10:44 +08:00
|
|
|
consumption = number_with_precision(
|
|
|
|
record.my_module_repository_rows.find_by(my_module: my_module)&.stock_consumption || 0,
|
|
|
|
precision: (record.repository.repository_stock_column.metadata['decimals'].to_i || 0),
|
|
|
|
strip_insignificant_zeros: true
|
|
|
|
)
|
2022-04-14 20:46:09 +08:00
|
|
|
unit = cell.value.repository_stock_unit_item&.data
|
|
|
|
cell_values[cell.repository_column_id] = "#{consumption} #{unit}"
|
|
|
|
end
|
2022-02-11 17:09:52 +08:00
|
|
|
else
|
|
|
|
cell_values[cell.repository_column_id] = cell.value.formatted
|
|
|
|
end
|
2020-01-07 21:21:55 +08:00
|
|
|
end
|
2022-04-25 23:10:44 +08:00
|
|
|
if repository.repository_stock_column.present? && record.repository_stock_cell.blank?
|
|
|
|
cell_values[repository.repository_stock_column.id] = '-'
|
|
|
|
end
|
2020-01-07 21:21:55 +08:00
|
|
|
|
2020-01-16 21:42:26 +08:00
|
|
|
repository_data[:custom_columns].each do |column_id|
|
2020-01-07 21:21:55 +08:00
|
|
|
value = cell_values[column_id]
|
2019-12-12 22:33:29 +08:00
|
|
|
row.push(value)
|
|
|
|
end
|
|
|
|
|
|
|
|
result.push(row)
|
|
|
|
end
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
end
|