Add stock and consumption to assigned items section [SCI-6434] (#3793)

This commit is contained in:
Alex Kriuchykhin 2022-01-25 12:12:55 +01:00 committed by GitHub
parent 95a36b6870
commit d2b5db13e9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 143 additions and 31 deletions

View file

@ -97,6 +97,62 @@ var MyModuleRepositories = (function() {
return columnDefs;
}
function simpleTableColumns(tableContainer) {
let columns = [
{
visible: true,
searchable: false,
data: 0
}
];
if ($(tableContainer).data('stock-management')) {
columns.push({
visible: true,
searchable: false,
data: 1
});
columns.push({
visible: true,
searchable: false,
data: 2
});
}
return columns;
}
function simpleViewColumnDefs(tableContainer) {
let columnDefs = [{
targets: 0,
className: 'item-name',
render: function(data, type, row) {
return "<a href='" + row.recordInfoUrl + "'"
+ "class='record-info-link'>" + data + '</a>';
}
}];
if ($(tableContainer).data('stock-management')) {
columnDefs.push({
targets: 1,
className: 'item-stock',
sWidth: '1%',
render: function(data) {
return $.fn.dataTable.render.RepositoryStockValue(data);
}
}, {
targets: 2,
className: 'item-consumed-stock',
sWidth: '1%',
render: function(data) {
return $.fn.dataTable.render.RepositoryConsumedStockValue(data);
}
});
}
return columnDefs;
}
function renderSimpleTable(tableContainer) {
if (SIMPLE_TABLE) SIMPLE_TABLE.destroy();
SIMPLE_TABLE = $(tableContainer).DataTable({
@ -121,14 +177,8 @@ var MyModuleRepositories = (function() {
global: false,
type: 'POST'
},
columnDefs: [{
targets: 0,
className: 'item-name',
render: function(data, type, row) {
return "<a href='" + row.recordInfoUrl + "'"
+ "class='record-info-link'>" + data + '</a>';
}
}],
columns: simpleTableColumns(tableContainer),
columnDefs: simpleViewColumnDefs(tableContainer),
drawCallback: function() {
var repositoryContainer = $(this).closest('.assigned-repository-container');
repositoryContainer.find('.table.dataTable').removeClass('hidden');
@ -299,12 +349,13 @@ var MyModuleRepositories = (function() {
function initSimpleTable() {
$('#assigned-items-container').on('shown.bs.collapse', '.assigned-repository-container', function() {
var repositoryContainer = $(this);
var repositoryTemplate = $($('#myModuleRepositorySimpleTemplate').html());
repositoryTemplate.attr('data-source', $(this).data('repository-url'));
repositoryTemplate.attr('data-version-label', $(this).data('footer-label'));
repositoryTemplate.attr('data-name-column-id', $(this).data('name-column-id'));
repositoryContainer.html(repositoryTemplate);
renderSimpleTable(repositoryTemplate);
var repositoryTable = repositoryContainer.find('.table');
repositoryTable.attr('data-source', $(this).data('repository-url'));
repositoryTable.attr('data-version-label', $(this).data('footer-label'));
repositoryTable.attr('data-name-column-id', $(this).data('name-column-id'));
repositoryTable.attr('data-stock-management', $(this).data('data-stock-management'));
repositoryContainer.html(repositoryTable);
renderSimpleTable(repositoryTable);
});
$('#wrapper').on('sideBar::shown sideBar::hidden', function() {

View file

@ -194,7 +194,8 @@ $.fn.dataTable.render.RepositoryStockValue = function(data) {
${data.value.stock_amount <= 0 ? 'stock-alert' : ''}">
${data.value.stock_formatted}
</span>`;
} else if (canManage) {
}
if (canManage) {
return `<a class="manage-repository-stock-value-link not-assigned-stock">
<i class="fas fa-box-open"></i>
${I18n.t('libraries.manange_modal_column.stock_type.add_stock')}
@ -205,6 +206,27 @@ $.fn.dataTable.render.RepositoryStockValue = function(data) {
</span>`;
};
$.fn.dataTable.render.RepositoryConsumedStockValue = function(data) {
let canManage = $('.repository-table').data('stock-consumption-editable');
if (data && data.value.consumed_stock_formatted) {
if (canManage) {
return `<a class="manage-repository-stock-value-link stock-value-view-render">
${data.value.consumed_stock_formatted}
</a>`;
}
return `<span class="stock-value-view-render">
${data.value.consumed_stock_formatted}
</span>`;
}
if (canManage && data && data.stock_present) {
return `<a class="manage-repository-consumed-stock-value-link">
<i class="fas fa-vial"></i>
${I18n.t('libraries.manange_modal_column.stock_type.add_stock_consumption')}
</a>`;
}
return '<span class="empty-consumed-stock-render"> - </span>';
};
$.fn.dataTable.render.defaultRepositoryStockValue = function() {
return $.fn.dataTable.render.RepositoryStockValue();
};

View file

@ -52,12 +52,27 @@ module RepositoryDatatableHelper
def prepare_simple_view_row_columns(repository_rows)
repository_rows.map do |record|
{
'DT_RowId': record.id,
'DT_RowAttr': { 'data-state': row_style(record) },
row = {
DT_RowId: record.id,
DT_RowAttr: { 'data-state': row_style(record) },
'0': escape_input(record.name),
'recordInfoUrl': Rails.application.routes.url_helpers.repository_repository_row_path(record.repository, record)
recordInfoUrl: Rails.application.routes.url_helpers.repository_repository_row_path(record.repository, record)
}
if record.repository.has_stock_management?
row['1'] =
if record.repository_stock_cell.present?
display_cell_value(record.repository_stock_cell, record.repository.team)
end
row['2'] = {
stock_present: record.repository_stock_cell.present?,
value: {
consumed_stock_formatted: record.consumed_stock
}
}
end
row
end
end

View file

@ -211,6 +211,14 @@ class Repository < RepositoryBase
.destroy_all
end
def self.stock_management_enabled?
true
end
def has_stock_management?
self.class.stock_management_enabled? && repository_columns.stock_type.exists?
end
private
def sync_name_with_snapshots

View file

@ -10,6 +10,12 @@ class RepositoryBase < ApplicationRecord
belongs_to :team
belongs_to :created_by, foreign_key: :created_by_id, class_name: 'User'
has_many :repository_columns, foreign_key: :repository_id, inverse_of: :repository, dependent: :destroy
has_one :repository_stock_column,
-> { where(data_type: 'RepositoryStockValue') },
class_name: 'RepositoryColumn',
foreign_key: :repository_id,
inverse_of: :repository,
dependent: :destroy
has_many :repository_rows, foreign_key: :repository_id, inverse_of: :repository, dependent: :destroy
has_many :repository_table_states, foreign_key: :repository_id, inverse_of: :repository, dependent: :destroy
has_many :report_elements, inverse_of: :repository, dependent: :destroy, foreign_key: :repository_id

View file

@ -40,7 +40,7 @@ class RepositoryColumn < ApplicationRecord
scope :asset_type, -> { where(data_type: 'RepositoryAssetValue') }
scope :status_type, -> { where(data_type: 'RepositoryStatusValue') }
scope :checkbox_type, -> { where(data_type: 'RepositoryChecklistValue') }
scope :stock_unit_type, -> { where(data_type: 'RepositoryStockUnitValue') }
scope :stock_type, -> { where(data_type: 'RepositoryStockValue') }
def self.name_like(query)
where('repository_columns.name ILIKE ?', "%#{query}%")

View file

@ -37,6 +37,10 @@ class RepositoryDatatableService
if @params[:assigned] == 'assigned'
repository_rows = repository_rows.joins(:my_module_repository_rows)
.where(my_module_repository_rows: { my_module_id: @my_module })
if @repository.has_stock_management?
repository_rows = repository_rows
.select('SUM(my_module_repository_rows.stock_consumption) AS "consumed_stock"')
end
else
repository_rows = repository_rows
.joins(:repository)

View file

@ -27,17 +27,20 @@
data-footer-label="<%= assigned_repository_simple_view_footer_label(repository) %>"
data-name-column-id="<%= assigned_repository_simple_view_name_column_id(repository) %>"
>
<table class="table hidden repository-table"
data-stock-management="<%= repository.has_stock_management? %>"
data-stock-consumption-editable="<%= can_manage_my_module_repository_rows?(@my_module) %>">
<thead>
<tr>
<th class="row-name"><%= t("repositories.table.row_name") %></th>
<% if repository.has_stock_management? %>
<th class="row-stock" data-columns-visible="false"><%= repository.repository_stock_column.name %></th>
<th class="row-consumption" data-columns-visible="false"><%= t("repositories.table.row_consumption") %></th>
<% end %>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
</div>
<% end %>
<template id="myModuleRepositorySimpleTemplate">
<table class="table hidden">
<thead>
<tr>
<th class="row-name"><%= t("repositories.table.row_name") %></th>
</tr>
</thead>
<tbody></tbody>
</table>
</template>

View file

@ -1391,6 +1391,7 @@ en:
no_items_matched: "No items matched your search request"
no_archived_items: "No archived items here"
no_archived_items_matched: "No archived items matched your search request"
no_stock: "No item stock"
table:
id: 'ID'
@ -1402,6 +1403,7 @@ en:
added_by: "Added by"
archived_on: "Archived on"
archived_by: "Archived by"
row_consumption: "Consumed"
enter_row_name: "Enter name"
locked_item: "This is read-only item."
assets:
@ -1617,6 +1619,7 @@ en:
stock_units: 'Stock units'
units_description: 'These units will appear when managing your stock. You can always add, remove or rename them in the list.'
add_stock: 'Add stock'
add_stock_consumption: 'Consume'
no_item_stock: 'No item stock'
checklist_type:
multiple_options: 'selected'