Add support for inventory columns selection for docx report [SCI-11095]

This commit is contained in:
Andrej 2024-10-01 11:07:12 +02:00
parent e53ea89f14
commit 1a04e249ba
8 changed files with 127 additions and 35 deletions

View file

@ -968,6 +968,16 @@ function reportHandsonTableConverter() {
} }
(function() { (function() {
function getSelectedRepositoryColumnValues(element, selectedAll = false) {
const values = [];
$(element).find('option').each((_, option) => {
if ($(option).attr('selected-value') || selectedAll) {
values.push(option.value);
}
});
return values;
}
function getReportData() { function getReportData() {
var reportData = {}; var reportData = {};
@ -1046,8 +1056,17 @@ function reportHandsonTableConverter() {
reportData.report.settings.task[e.value] = e.checked; reportData.report.settings.task[e.value] = e.checked;
}); });
reportData.report.settings.task.repositories = []; reportData.report.settings.task.repositories = [];
$.each($('.task-contents-container .repositories-contents .repositories-setting:checked'), function(i, e) { reportData.report.settings.task.excluded_repository_columns = {};
reportData.report.settings.task.repositories.push(parseInt(e.value, 10));
$.each($('.task-contents-container .repositories-contents .repositories-setting:checked'), (_, e) => {
const value = parseInt(e.value, 10);
const $repositoryColumn = $(e).parent().siblings('.repository-columns')[0];
const selectedValues = dropdownSelector.getValues($repositoryColumn);
const excludedValues = getSelectedRepositoryColumnValues($repositoryColumn, true)
.filter((item) => !selectedValues.includes(item))
.map((el) => parseInt(el, 10));
reportData.report.settings.task.repositories.push(value);
reportData.report.settings.task.excluded_repository_columns[value] = excludedValues;
}); });
reportData.report.settings.task.result_order = dropdownSelector.getValues('#taskResultsOrder'); reportData.report.settings.task.result_order = dropdownSelector.getValues('#taskResultsOrder');
@ -1359,6 +1378,20 @@ function reportHandsonTableConverter() {
if (dropdownSelector.getValues('#docxTemplateSelector').length > 0) { if (dropdownSelector.getValues('#docxTemplateSelector').length > 0) {
loadDocxTemplate(); loadDocxTemplate();
} }
$('.repository-columns').each((_, element) => {
const elementId = `#${$(element).attr('id')}`;
const elements = getSelectedRepositoryColumnValues(elementId);
dropdownSelector.init(elementId, {
selectAppearance: 'simple',
optionClass: 'checkbox-icon'
});
if (elements.length) {
dropdownSelector.selectValues(elementId, elements);
}
});
} }
function loadTemplate() { function loadTemplate() {

View file

@ -260,6 +260,15 @@
} }
// scss-lint:enable ImportantRule // scss-lint:enable ImportantRule
.repositories-contents {
.dropdown-selector-container {
display: inline-flex;
flex-shrink: 0;
margin-left: auto;
width: 200px;
}
}
.project-selector-container { .project-selector-container {
background: $color-white; background: $color-white;
box-shadow: $modal-shadow; box-shadow: $modal-shadow;

View file

@ -391,17 +391,17 @@ class MyModule < ApplicationRecord
{ data: data, headers: headers } { data: data, headers: headers }
end end
def repository_docx_json(repository) def repository_docx_json(repository, excluded_columns)
headers = [ headers = Report.default_repository_columns.filter_map do |key, value|
I18n.t('repositories.table.id'), value unless excluded_columns.include?(key.to_s.to_i)
I18n.t('repositories.table.row_name'), end
I18n.t('repositories.table.added_on'),
I18n.t('repositories.table.added_by')
]
custom_columns = [] custom_columns = []
return false unless repository return false unless repository
repository.repository_columns.order(:id).each do |column| repository.repository_columns.order(:id).each do |column|
next if excluded_columns.include?(column.id)
if column.data_type == 'RepositoryStockValue' if column.data_type == 'RepositoryStockValue'
if repository.has_stock_consumption? if repository.has_stock_consumption?
headers.push(I18n.t('repositories.table.row_consumption')) headers.push(I18n.t('repositories.table.row_consumption'))
@ -416,7 +416,7 @@ class MyModule < ApplicationRecord
records = repository.assigned_rows(self) records = repository.assigned_rows(self)
.select(:id, :name, :created_at, :created_by_id, :repository_id, :parent_id, :archived) .select(:id, :name, :created_at, :created_by_id, :repository_id, :parent_id, :archived)
{ headers: headers, rows: records, custom_columns: custom_columns } { headers: headers, rows: records, custom_columns: custom_columns, excluded_columns: excluded_columns }
end end
def deep_clone(current_user) def deep_clone(current_user)

View file

@ -64,7 +64,8 @@ class Report < ApplicationRecord
result_comments: true, result_comments: true,
result_order: 'new', result_order: 'new',
activities: true, activities: true,
repositories: [] repositories: [],
excluded_repository_columns: {}
} }
}.freeze }.freeze
@ -126,4 +127,13 @@ class Report < ApplicationRecord
ReportActions::ReportContent.new(report, content, {}, current_user).save_with_content ReportActions::ReportContent.new(report, content, {}, current_user).save_with_content
report report
end end
def self.default_repository_columns
{
'-1': I18n.t('repositories.table.id'),
'-2': I18n.t('repositories.table.row_name'),
'-3': I18n.t('repositories.table.added_on'),
'-4': I18n.t('repositories.table.added_by')
}
end
end end

View file

@ -5,11 +5,12 @@ module Reports::Docx::DrawMyModuleRepository
my_module = subject.my_module my_module = subject.my_module
repository = subject.repository repository = subject.repository
repository = assigned_repository_or_snapshot(my_module, repository) repository = assigned_repository_or_snapshot(my_module, repository)
excluded_repository_columns = @settings.dig(:task, :excluded_repository_columns, repository.id.to_s) || {}
return unless repository && can_read_experiment?(@user, my_module.experiment) && return unless repository && can_read_experiment?(@user, my_module.experiment) &&
(repository.is_a?(RepositorySnapshot) || can_read_repository?(@user, repository)) (repository.is_a?(RepositorySnapshot) || can_read_repository?(@user, repository))
repository_data = my_module.repository_docx_json(repository) repository_data = my_module.repository_docx_json(repository, excluded_repository_columns)
return false unless repository_data[:rows].any? && can_read_repository?(@user, repository) return false unless repository_data[:rows].any? && can_read_repository?(@user, repository)

View file

@ -6,12 +6,16 @@ module Reports::Docx::RepositoryHelper
def prepare_row_columns(repository_data, my_module = nil, repository = nil) def prepare_row_columns(repository_data, my_module = nil, repository = nil)
result = [repository_data[:headers]] result = [repository_data[:headers]]
excluded_columns = repository_data[:excluded_columns]
repository_data[:rows].each do |record| repository_data[:rows].each do |record|
row = [] row = []
row.push(record.code) row.push(record.code) unless excluded_columns.include?(-1)
row.push(escape_input(record.archived ? "#{record.name} [#{I18n.t('general.archived')}]" : record.name)) unless excluded_columns.include?(-2)
row.push(I18n.l(record.created_at, format: :full)) row.push(escape_input(record.archived ? "#{record.name} [#{I18n.t('general.archived')}]" : record.name))
row.push(escape_input(record.created_by.full_name)) end
row.push(I18n.l(record.created_at, format: :full)) unless excluded_columns.include?(-3)
row.push(escape_input(record.created_by.full_name)) unless excluded_columns.include?(-4)
cell_values = {} cell_values = {}
custom_cells = record.repository_cells custom_cells = record.repository_cells
@ -38,6 +42,8 @@ module Reports::Docx::RepositoryHelper
end end
repository_data[:custom_columns].each do |column_id| repository_data[:custom_columns].each do |column_id|
next if excluded_columns.include?(column_id)
value = cell_values[column_id] value = cell_values[column_id]
row.push(value) row.push(value)
end end

View file

@ -55,7 +55,10 @@
</span><br> </span><br>
<span class="repositories-items-description"> <span class="repositories-items-description">
<i class="sn-icon sn-icon-info"></i> <i class="sn-icon sn-icon-info"></i>
<span><%= t("projects.reports.wizard.third_step.assigned_items_description") %></span> <div class="flex-col font-normal">
<p><%= t("projects.reports.wizard.third_step.assigned_items_description") %></p>
<p><%= t("projects.reports.wizard.third_step.assigned_items_repository_items_description_html") %></p>
</div>
</span> </span>
<ul class="collapse in" id="repositoriesContents"> <ul class="collapse in" id="repositoriesContents">
<li> <li>
@ -68,25 +71,50 @@
<ul class="repositories-contents"> <ul class="repositories-contents">
<% @repositories.each do |repository| %> <% @repositories.each do |repository| %>
<li> <li>
<span class="sci-checkbox-container"> <div class="flex items-center">
<input type="checkbox" <span class="sci-checkbox-container">
class="sci-checkbox repositories-setting" <input type="checkbox"
value="<%= repository.is_a?(RepositorySnapshot) ? repository.parent_id : repository.id %>" class="sci-checkbox repositories-setting"
<%= 'checked' if report.new_record? || value="<%= repository.is_a?(RepositorySnapshot) ? repository.parent_id : repository.id %>"
@project_contents[:repositories].include?(repository.id) || <%= 'checked' if report.new_record? ||
(repository.is_a?(Repository) && repository.repository_snapshots.exists?(id: @project_contents[:repositories])) %> /> @project_contents[:repositories].include?(repository.id) ||
<span class="sci-checkbox-label"></span> (repository.is_a?(Repository) && repository.repository_snapshots.exists?(id: @project_contents[:repositories])) %> />
</span> <span class="sci-checkbox-label"></span>
<%= repository.name %>
<% if repository.archived? %>
<span class="archived">
<%= t("projects.reports.wizard.third_step.archived") %>
</span> </span>
<% elsif repository.is_a?(RepositorySnapshot) %> <%= repository.name %>
<span class="deleted"> <% if repository.archived? %>
<%= t("projects.reports.wizard.third_step.deleted") %> <span class="archived">
</span> <%= t("projects.reports.wizard.third_step.archived") %>
<% end %> </span>
<% elsif repository.is_a?(RepositorySnapshot) %>
<span class="deleted">
<%= t("projects.reports.wizard.third_step.deleted") %>
</span>
<% end %>
<select class="repository-columns"
id=<%= "repository-#{repository.id}" %>
data-combine-tags="true"
data-placeholder="<%= t("projects.reports.wizard.third_step.repository_column.placeholder") %>"
data-select-multiple-all-selected="<%= t("projects.reports.wizard.third_step.repository_column.all_selected") %>"
data-select-multiple-name="<%= t("projects.reports.wizard.third_step.repository_column.selected") %>"
multiple
>
<% Report.default_repository_columns.each do |key, value| %>
<option value="<%= key %>"
selected-value="<%= report.settings.dig(:task, :excluded_repository_columns, repository.id.to_s)&.include?(key.to_s.to_i) ? '' : 'selected' %>"
>
<%= value %>
</option>
<% end %>
<% repository.repository_columns.find_each do |repository_column| %>
<option value="<%= repository_column.id %>"
selected-value="<%= report.settings.dig(:task, :excluded_repository_columns, repository.id.to_s)&.include?(repository_column.id.to_i) ? '' : 'selected' %>"
>
<%= repository_column.name %>
</option>
<% end %>
</select>
</div>
<div class="divider"></div> <div class="divider"></div>
</li> </li>
<% end %> <% end %>

View file

@ -844,6 +844,7 @@ en:
step_comments: "Step comments" step_comments: "Step comments"
assigned_items: "Assigned items" assigned_items: "Assigned items"
assigned_items_description: "Inventories selected below will only contain the items that you assigned to the tasks directly." assigned_items_description: "Inventories selected below will only contain the items that you assigned to the tasks directly."
assigned_items_repository_items_description_html: "You can customize inventory columns <strong>only for the docx report.</strong> The stock management column by default reflects item consumption."
include_all_assigned_iitems: "Include all assigned items from the following inventories" include_all_assigned_iitems: "Include all assigned items from the following inventories"
results: "Results" results: "Results"
all_results: "Include all results elements" all_results: "Include all results elements"
@ -866,6 +867,10 @@ en:
exclude_timestamps: "Exclude timestamps" exclude_timestamps: "Exclude timestamps"
archived: "[archived]" archived: "[archived]"
deleted: "[deleted]" deleted: "[deleted]"
repository_column:
placeholder: 'Select columns'
all_selected: 'All columns selected'
selected: 'columns selected'
new: new:
report_name_placeholder: "Name your report" report_name_placeholder: "Name your report"