2019-10-09 18:53:41 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-06 23:32:39 +08:00
|
|
|
module RepositoryDatatableHelper
|
|
|
|
include InputSanitizeHelper
|
2023-10-27 16:31:38 +08:00
|
|
|
include Rails.application.routes.url_helpers
|
2019-10-25 20:30:45 +08:00
|
|
|
|
2020-04-22 21:21:04 +08:00
|
|
|
def prepare_row_columns(repository_rows, repository, columns_mappings, team, options = {})
|
2022-06-08 19:23:39 +08:00
|
|
|
has_stock_management = repository.has_stock_management?
|
2022-07-25 21:39:11 +08:00
|
|
|
reminders_enabled = Repository.reminders_enabled?
|
2023-01-16 17:46:35 +08:00
|
|
|
repository_rows = reminders_enabled ? with_reminders_status(repository_rows, repository) : repository_rows
|
2023-05-03 22:21:27 +08:00
|
|
|
stock_managable = has_stock_management && !options[:disable_stock_management] &&
|
|
|
|
can_manage_repository_stock?(repository)
|
|
|
|
stock_consumption_permitted = has_stock_management && options[:include_stock_consumption] && options[:my_module] &&
|
|
|
|
stock_consumption_permitted?(repository, options[:my_module])
|
2022-03-09 21:13:48 +08:00
|
|
|
|
2020-04-22 21:21:04 +08:00
|
|
|
repository_rows.map do |record|
|
2023-01-16 17:46:35 +08:00
|
|
|
row = public_send("#{repository.class.name.underscore}_default_columns", record)
|
|
|
|
row.merge!(
|
2022-07-25 21:39:11 +08:00
|
|
|
DT_RowId: record.id,
|
2023-10-20 16:05:42 +08:00
|
|
|
DT_RowAttr: { 'data-state': row_style(record), 'data-e2e': "e2e-RT-invInventory-row-#{record.id}" },
|
2022-07-25 21:39:11 +08:00
|
|
|
recordInfoUrl: Rails.application.routes.url_helpers.repository_repository_row_path(repository, record),
|
|
|
|
rowRemindersUrl:
|
2022-03-09 21:13:48 +08:00
|
|
|
Rails.application.routes.url_helpers
|
|
|
|
.active_reminder_repository_cells_repository_repository_row_url(
|
|
|
|
repository,
|
|
|
|
record
|
|
|
|
)
|
2023-01-16 17:46:35 +08:00
|
|
|
)
|
2022-01-29 00:53:56 +08:00
|
|
|
|
2023-01-24 16:36:13 +08:00
|
|
|
if reminders_enabled
|
|
|
|
row['hasActiveReminders'] = record.has_active_stock_reminders || record.has_active_datetime_reminders
|
|
|
|
end
|
|
|
|
|
2023-05-03 22:21:27 +08:00
|
|
|
unless options[:view_mode]
|
|
|
|
row['recordUpdateUrl'] =
|
|
|
|
Rails.application.routes.url_helpers.repository_repository_row_path(repository, record)
|
|
|
|
row['recordEditable'] = record.editable?
|
|
|
|
end
|
|
|
|
|
|
|
|
row['0'] = record[:row_assigned] if options[:my_module]
|
|
|
|
|
|
|
|
# Add custom columns
|
|
|
|
custom_cells = record.repository_cells.filter { |cell| cell.value_type != 'RepositoryStockValue' }
|
|
|
|
|
|
|
|
custom_cells.each do |cell|
|
|
|
|
row[columns_mappings[cell.repository_column.id]] =
|
2023-09-18 22:24:07 +08:00
|
|
|
serialize_repository_cell_value(cell, team, repository, reminders_enabled: reminders_enabled)
|
2023-05-03 22:21:27 +08:00
|
|
|
end
|
|
|
|
|
2022-06-08 19:23:39 +08:00
|
|
|
if has_stock_management
|
2023-05-03 22:21:27 +08:00
|
|
|
stock_cell = record.repository_cells.find { |cell| cell.value_type == 'RepositoryStockValue' }
|
2020-04-22 23:55:22 +08:00
|
|
|
|
2023-05-03 22:21:27 +08:00
|
|
|
# always add stock cell, even if empty
|
2023-09-18 22:24:07 +08:00
|
|
|
row['stock'] =
|
|
|
|
if stock_cell.present?
|
|
|
|
serialize_repository_cell_value(record.repository_stock_cell, team, repository)
|
|
|
|
else
|
2023-10-27 16:31:38 +08:00
|
|
|
{ stock_url: new_repository_stock_repository_repository_row_url(repository, record) }
|
2023-09-18 22:24:07 +08:00
|
|
|
end
|
2023-05-03 22:21:27 +08:00
|
|
|
row['stock'][:stock_managable] = stock_managable
|
|
|
|
row['stock']['displayWarnings'] = display_stock_warnings?(repository)
|
|
|
|
row['stock'][:stock_status] = stock_cell&.value&.status
|
2022-01-29 00:53:56 +08:00
|
|
|
|
2023-05-03 22:21:27 +08:00
|
|
|
row['stock']['value_type'] = 'RepositoryStockValue'
|
2020-05-12 22:26:44 +08:00
|
|
|
|
2023-05-03 22:21:27 +08:00
|
|
|
if options[:include_stock_consumption] && options[:my_module]
|
|
|
|
consumption_managable = stock_consumption_managable?(record, repository, options[:my_module])
|
|
|
|
consumed_stock_formatted =
|
|
|
|
number_with_precision(
|
|
|
|
record.consumed_stock,
|
|
|
|
precision: (repository.repository_stock_column.metadata['decimals'].to_i || 0),
|
|
|
|
strip_insignificant_zeros: true
|
|
|
|
)
|
|
|
|
row['consumedStock'] = {
|
|
|
|
stock_present: stock_cell.present?,
|
|
|
|
consumptionPermitted: stock_consumption_permitted,
|
|
|
|
consumptionManagable: consumption_managable,
|
|
|
|
updateStockConsumptionUrl: Rails.application.routes.url_helpers.consume_modal_my_module_repository_path(
|
|
|
|
options[:my_module],
|
|
|
|
repository,
|
|
|
|
row_id: record.id
|
|
|
|
),
|
|
|
|
value: {
|
|
|
|
consumed_stock: record.consumed_stock,
|
|
|
|
consumed_stock_formatted:
|
|
|
|
"#{consumed_stock_formatted || 0} #{record.repository_stock_value&.repository_stock_unit_item&.data}"
|
|
|
|
}
|
2022-01-29 00:53:56 +08:00
|
|
|
}
|
2023-05-03 22:21:27 +08:00
|
|
|
end
|
2022-01-29 00:53:56 +08:00
|
|
|
end
|
|
|
|
|
2020-04-22 21:21:04 +08:00
|
|
|
row
|
2020-04-07 01:52:41 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-18 21:46:24 +08:00
|
|
|
def prepare_simple_view_row_columns(repository_rows, repository, my_module, options = {})
|
2022-07-25 21:39:11 +08:00
|
|
|
has_stock_management = repository.has_stock_management?
|
2023-08-04 17:21:43 +08:00
|
|
|
reminders_enabled = !options[:disable_reminders] && Repository.reminders_enabled?
|
2023-01-16 17:46:35 +08:00
|
|
|
repository_rows = reminders_enabled ? with_reminders_status(repository_rows, repository) : repository_rows
|
2023-05-03 22:21:27 +08:00
|
|
|
# Always disabled in a simple view
|
|
|
|
stock_managable = false
|
2023-05-18 15:08:12 +08:00
|
|
|
stock_consumption_permitted = has_stock_management && stock_consumption_permitted?(repository, my_module)
|
2022-03-18 21:46:24 +08:00
|
|
|
|
2020-05-12 22:26:44 +08:00
|
|
|
repository_rows.map do |record|
|
2022-01-25 19:12:55 +08:00
|
|
|
row = {
|
|
|
|
DT_RowId: record.id,
|
|
|
|
DT_RowAttr: { 'data-state': row_style(record) },
|
2020-05-21 22:51:07 +08:00
|
|
|
'0': escape_input(record.name),
|
2022-07-25 21:39:11 +08:00
|
|
|
rowRemindersUrl:
|
2022-03-17 18:39:17 +08:00
|
|
|
Rails.application.routes.url_helpers
|
|
|
|
.active_reminder_repository_cells_repository_repository_row_url(
|
|
|
|
record.repository,
|
|
|
|
record
|
|
|
|
)
|
2020-05-12 22:26:44 +08:00
|
|
|
}
|
2022-01-25 19:12:55 +08:00
|
|
|
|
2023-10-12 22:30:58 +08:00
|
|
|
unless record.repository.is_a?(RepositorySnapshot)
|
|
|
|
row['recordInfoUrl'] = Rails.application.routes.url_helpers.repository_repository_row_path(record.repository,
|
|
|
|
record)
|
|
|
|
end
|
|
|
|
|
2023-01-24 16:36:13 +08:00
|
|
|
if reminders_enabled
|
|
|
|
row['hasActiveReminders'] = record.has_active_stock_reminders || record.has_active_datetime_reminders
|
|
|
|
end
|
|
|
|
|
2022-07-25 21:39:11 +08:00
|
|
|
if has_stock_management
|
2022-02-04 21:24:15 +08:00
|
|
|
stock_present = record.repository_stock_cell.present?
|
2022-03-18 22:02:15 +08:00
|
|
|
|
|
|
|
consumption_managable = stock_consumption_managable?(record, repository, my_module)
|
2022-02-04 21:24:15 +08:00
|
|
|
|
2023-09-18 22:24:07 +08:00
|
|
|
row['stock'] =
|
|
|
|
if stock_present
|
|
|
|
serialize_repository_cell_value(record.repository_stock_cell, record.repository.team, repository)
|
|
|
|
else
|
|
|
|
{}
|
|
|
|
end
|
2022-04-25 21:23:12 +08:00
|
|
|
row['stock']['displayWarnings'] = display_stock_warnings?(repository)
|
|
|
|
row['stock'][:stock_status] = record.repository_stock_cell&.value&.status
|
2022-04-14 20:27:31 +08:00
|
|
|
|
2022-02-16 16:40:48 +08:00
|
|
|
row['stock'][:stock_managable] = stock_managable
|
2022-02-11 17:46:19 +08:00
|
|
|
if record.repository.is_a?(RepositorySnapshot)
|
2022-02-16 16:40:48 +08:00
|
|
|
row['consumedStock'] =
|
|
|
|
if record.repository_stock_consumption_value.present?
|
2023-09-18 22:24:07 +08:00
|
|
|
serialize_repository_cell_value(record.repository_stock_consumption_cell,
|
|
|
|
record.repository.team,
|
|
|
|
repository)
|
2022-02-16 16:40:48 +08:00
|
|
|
else
|
|
|
|
{}
|
|
|
|
end
|
2022-02-11 17:46:19 +08:00
|
|
|
else
|
2022-02-16 16:40:48 +08:00
|
|
|
row['consumedStock'] = {}
|
2022-02-11 17:46:19 +08:00
|
|
|
if consumption_managable
|
2022-03-18 22:02:15 +08:00
|
|
|
row['consumedStock']['updateStockConsumptionUrl'] =
|
2022-02-11 17:46:19 +08:00
|
|
|
Rails.application.routes.url_helpers.consume_modal_my_module_repository_path(
|
|
|
|
my_module, record.repository, row_id: record.id
|
|
|
|
)
|
|
|
|
end
|
2022-04-25 19:08:33 +08:00
|
|
|
consumed_stock_formatted =
|
|
|
|
number_with_precision(
|
|
|
|
record.consumed_stock,
|
|
|
|
precision: (record.repository.repository_stock_column.metadata['decimals'].to_i || 0),
|
|
|
|
strip_insignificant_zeros: true
|
|
|
|
)
|
2022-04-14 20:27:31 +08:00
|
|
|
row['consumedStock'][:value] = {
|
|
|
|
consumed_stock: record.consumed_stock,
|
|
|
|
consumed_stock_formatted:
|
2022-04-25 19:08:33 +08:00
|
|
|
"#{consumed_stock_formatted || 0} #{record.repository_stock_value&.repository_stock_unit_item&.data}"
|
2022-04-14 20:27:31 +08:00
|
|
|
}
|
2022-02-04 21:24:15 +08:00
|
|
|
end
|
2022-04-14 20:27:31 +08:00
|
|
|
|
2022-02-16 16:40:48 +08:00
|
|
|
row['consumedStock']['stock_present'] = stock_present
|
2023-05-03 22:21:27 +08:00
|
|
|
row['consumedStock']['consumptionPermitted'] = stock_consumption_permitted
|
2022-03-18 22:02:15 +08:00
|
|
|
row['consumedStock']['consumptionManagable'] = consumption_managable
|
2022-01-25 19:12:55 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
row
|
2020-05-12 22:26:44 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-12 19:14:11 +08:00
|
|
|
def prepare_snapshot_row_columns(repository_rows, columns_mappings, team, repository_snapshot, options = {})
|
2022-07-25 21:39:11 +08:00
|
|
|
has_stock_management = repository_snapshot.has_stock_management?
|
2020-04-22 21:21:04 +08:00
|
|
|
repository_rows.map do |record|
|
2020-04-21 20:49:36 +08:00
|
|
|
row = {
|
|
|
|
'DT_RowId': record.id,
|
2020-06-12 20:14:00 +08:00
|
|
|
'DT_RowAttr': { 'data-state': row_style(record) },
|
2021-07-19 20:23:36 +08:00
|
|
|
'1': record.code,
|
2020-04-21 20:49:36 +08:00
|
|
|
'2': escape_input(record.name),
|
|
|
|
'3': I18n.l(record.created_at, format: :full),
|
|
|
|
'4': escape_input(record.created_by.full_name),
|
2023-09-18 22:24:07 +08:00
|
|
|
'recordInfoUrl': Rails.application.routes.url_helpers
|
|
|
|
.repository_repository_row_path(repository_snapshot, record)
|
2020-04-21 20:49:36 +08:00
|
|
|
}
|
|
|
|
|
2020-05-12 22:26:44 +08:00
|
|
|
# Add custom columns
|
|
|
|
record.repository_cells.each do |cell|
|
2023-09-18 22:24:07 +08:00
|
|
|
row[columns_mappings[cell.repository_column.id]] =
|
|
|
|
serialize_repository_cell_value(cell, team, repository_snapshot)
|
2020-04-21 20:49:36 +08:00
|
|
|
end
|
2020-05-12 22:26:44 +08:00
|
|
|
|
2022-07-25 21:39:11 +08:00
|
|
|
if has_stock_management
|
2022-05-11 04:21:48 +08:00
|
|
|
row['stock'] = if record.repository_stock_cell.present?
|
2023-09-18 22:24:07 +08:00
|
|
|
serialize_repository_cell_value(record.repository_stock_cell, team, repository_snapshot)
|
2022-04-25 21:23:12 +08:00
|
|
|
else
|
|
|
|
{ value_type: 'RepositoryStockValue' }
|
|
|
|
end
|
|
|
|
|
2022-02-16 16:40:48 +08:00
|
|
|
row['consumedStock'] =
|
2022-05-11 04:21:48 +08:00
|
|
|
if record.repository_stock_consumption_cell.present?
|
2023-09-18 22:24:07 +08:00
|
|
|
serialize_repository_cell_value(record.repository_stock_consumption_cell, team, repository_snapshot)
|
2022-02-16 16:40:48 +08:00
|
|
|
else
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-22 21:21:04 +08:00
|
|
|
row
|
2020-04-21 20:49:36 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-07 18:51:18 +08:00
|
|
|
def assigned_row(record)
|
2020-04-24 21:43:10 +08:00
|
|
|
{
|
|
|
|
tasks: record.assigned_my_modules_count,
|
2020-01-23 20:12:58 +08:00
|
|
|
experiments: record.assigned_experiments_count,
|
2020-04-24 21:43:10 +08:00
|
|
|
projects: record.assigned_projects_count,
|
2020-05-07 18:51:18 +08:00
|
|
|
task_list_url: assigned_task_list_repository_repository_row_path(record.repository, record)
|
2020-04-24 21:43:10 +08:00
|
|
|
}
|
2018-03-06 23:32:39 +08:00
|
|
|
end
|
2018-04-13 19:11:29 +08:00
|
|
|
|
2021-08-13 20:07:01 +08:00
|
|
|
def repository_default_columns(record)
|
|
|
|
{
|
|
|
|
'1': assigned_row(record),
|
|
|
|
'2': record.code,
|
|
|
|
'3': escape_input(record.name),
|
|
|
|
'4': I18n.l(record.created_at, format: :full),
|
|
|
|
'5': escape_input(record.created_by.full_name),
|
|
|
|
'6': (record.archived_on ? I18n.l(record.archived_on, format: :full) : ''),
|
|
|
|
'7': escape_input(record.archived_by&.full_name)
|
|
|
|
}
|
2018-04-20 15:22:23 +08:00
|
|
|
end
|
2019-10-09 18:53:41 +08:00
|
|
|
|
2021-08-13 20:07:01 +08:00
|
|
|
def linked_repository_default_columns(record)
|
|
|
|
{
|
|
|
|
'1': assigned_row(record),
|
|
|
|
'2': escape_input(record.external_id),
|
|
|
|
'3': record.code,
|
|
|
|
'4': escape_input(record.name),
|
|
|
|
'5': I18n.l(record.created_at, format: :full),
|
|
|
|
'6': escape_input(record.created_by.full_name),
|
|
|
|
'7': (record.archived_on ? I18n.l(record.archived_on, format: :full) : ''),
|
|
|
|
'8': escape_input(record.archived_by&.full_name)
|
|
|
|
}
|
2020-04-21 20:49:36 +08:00
|
|
|
end
|
|
|
|
|
2023-09-18 22:24:07 +08:00
|
|
|
def serialize_repository_cell_value(cell, team, repository, options = {})
|
2020-01-13 23:09:07 +08:00
|
|
|
serializer_class = "RepositoryDatatable::#{cell.repository_column.data_type}Serializer".constantize
|
|
|
|
serializer_class.new(
|
2021-07-07 23:43:51 +08:00
|
|
|
cell.value,
|
2022-04-12 19:14:11 +08:00
|
|
|
scope: {
|
|
|
|
team: team,
|
|
|
|
user: current_user,
|
|
|
|
column: cell.repository_column,
|
2022-07-25 21:39:11 +08:00
|
|
|
repository: repository,
|
|
|
|
options: options
|
2022-04-12 19:14:11 +08:00
|
|
|
}
|
2020-01-13 23:09:07 +08:00
|
|
|
).serializable_hash
|
2019-10-09 18:53:41 +08:00
|
|
|
end
|
2020-06-12 20:14:00 +08:00
|
|
|
|
|
|
|
def row_style(row)
|
|
|
|
return I18n.t('general.archived') if row.archived
|
|
|
|
|
|
|
|
''
|
|
|
|
end
|
2022-03-18 21:46:24 +08:00
|
|
|
|
2023-01-16 17:46:35 +08:00
|
|
|
def with_reminders_status(repository_rows, repository)
|
2023-01-24 16:36:13 +08:00
|
|
|
# don't load reminders for archived repositories or snapshots
|
|
|
|
if repository.archived? || repository.is_a?(RepositorySnapshot)
|
|
|
|
return repository_rows.select('FALSE AS has_active_stock_reminders')
|
|
|
|
.select('FALSE AS has_active_datetime_reminders')
|
|
|
|
end
|
2023-01-16 17:46:35 +08:00
|
|
|
|
|
|
|
repository_cells = RepositoryCell.joins(
|
|
|
|
"INNER JOIN repository_columns ON repository_columns.id = repository_cells.repository_column_id " \
|
|
|
|
"AND repository_columns.repository_id = #{repository.id}"
|
|
|
|
)
|
|
|
|
|
|
|
|
repository_rows
|
|
|
|
.joins(
|
|
|
|
"LEFT OUTER JOIN (#{RepositoryCell.stock_reminder_repository_cells_scope(repository_cells, current_user)
|
|
|
|
.select(:id, :repository_row_id).to_sql}) " \
|
|
|
|
"AS repository_cells_with_active_stock_reminders " \
|
|
|
|
"ON repository_cells_with_active_stock_reminders.repository_row_id = repository_rows.id"
|
|
|
|
)
|
|
|
|
.joins(
|
|
|
|
"LEFT OUTER JOIN (#{RepositoryCell.date_time_reminder_repository_cells_scope(repository_cells, current_user)
|
|
|
|
.select(:id, :repository_row_id).to_sql}) " \
|
|
|
|
"AS repository_cells_with_active_datetime_reminders " \
|
|
|
|
"ON repository_cells_with_active_datetime_reminders.repository_row_id = repository_rows.id"
|
|
|
|
)
|
|
|
|
.select('COUNT(repository_cells_with_active_stock_reminders.id) > 0 AS has_active_stock_reminders')
|
|
|
|
.select('COUNT(repository_cells_with_active_datetime_reminders.id) > 0 AS has_active_datetime_reminders')
|
2022-03-18 21:46:24 +08:00
|
|
|
end
|
2022-03-18 22:02:15 +08:00
|
|
|
|
2022-04-20 17:07:26 +08:00
|
|
|
def stock_consumption_permitted?(repository, my_module)
|
2023-08-04 17:21:43 +08:00
|
|
|
return false unless repository.is_a?(Repository) && current_user
|
2022-04-20 17:07:26 +08:00
|
|
|
|
|
|
|
can_update_my_module_stock_consumption?(my_module)
|
|
|
|
end
|
|
|
|
|
2022-03-18 22:02:15 +08:00
|
|
|
def stock_consumption_managable?(record, repository, my_module)
|
|
|
|
return false unless my_module
|
2022-03-31 20:56:23 +08:00
|
|
|
return false if repository.archived? || record.archived?
|
2022-03-18 22:02:15 +08:00
|
|
|
|
2022-04-14 20:27:31 +08:00
|
|
|
true
|
2022-03-18 22:02:15 +08:00
|
|
|
end
|
2022-04-25 21:23:12 +08:00
|
|
|
|
|
|
|
def display_stock_warnings?(repository)
|
|
|
|
!repository.is_a?(RepositorySnapshot)
|
|
|
|
end
|
2018-11-19 20:03:39 +08:00
|
|
|
end
|