2022-01-20 19:44:57 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class RepositoryStockValuesController < ApplicationController
|
2023-09-18 22:24:07 +08:00
|
|
|
include RepositoryDatatableHelper # for use of serialize_repository_cell_value method on stock update
|
2022-04-22 17:13:00 +08:00
|
|
|
|
2022-01-20 19:44:57 +08:00
|
|
|
before_action :load_vars
|
|
|
|
before_action :check_manage_permissions
|
|
|
|
|
|
|
|
def new
|
|
|
|
render json: {
|
|
|
|
html: render_to_string(
|
|
|
|
partial: 'repository_stock_values/manage_modal_content',
|
|
|
|
locals: {
|
|
|
|
repository_row: @repository_row,
|
|
|
|
repository_stock_column: @repository_column,
|
2022-01-26 00:48:45 +08:00
|
|
|
unit_items: @repository_column.repository_stock_unit_items,
|
2022-01-20 19:44:57 +08:00
|
|
|
repository_stock_value: RepositoryStockValue.new
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
|
|
|
render json: {
|
|
|
|
html: render_to_string(
|
|
|
|
partial: 'repository_stock_values/manage_modal_content',
|
|
|
|
locals: {
|
|
|
|
repository_row: @repository_row,
|
|
|
|
repository_stock_column: @repository_column,
|
2022-01-26 00:48:45 +08:00
|
|
|
unit_items: @repository_column.repository_stock_unit_items,
|
2022-01-20 19:44:57 +08:00
|
|
|
repository_stock_value: @repository_stock_value
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_or_update
|
2022-02-02 17:33:02 +08:00
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
@repository_stock_value ? update! : create!
|
|
|
|
|
|
|
|
log_activity(
|
|
|
|
params[:operator],
|
|
|
|
params[:change_amount],
|
|
|
|
@repository_stock_value.repository_stock_unit_item.data,
|
2022-04-20 17:29:16 +08:00
|
|
|
@repository_stock_value.amount,
|
|
|
|
params[:repository_stock_value][:comment]
|
2022-02-02 17:33:02 +08:00
|
|
|
)
|
2022-01-20 19:44:57 +08:00
|
|
|
end
|
2022-02-02 17:33:02 +08:00
|
|
|
|
2022-04-22 17:13:00 +08:00
|
|
|
render json: {
|
|
|
|
stock_managable: true,
|
2022-05-09 17:20:38 +08:00
|
|
|
stock_status: @repository_stock_value.status,
|
|
|
|
manageStockUrl: edit_repository_stock_repository_repository_row_url(@repository, @repository_row)
|
2023-09-18 22:24:07 +08:00
|
|
|
}.merge(serialize_repository_cell_value(@repository_stock_value.repository_cell, current_team, @repository))
|
2022-01-20 19:44:57 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-02-02 17:33:02 +08:00
|
|
|
def update!
|
2022-03-09 21:19:15 +08:00
|
|
|
@repository_stock_value.update_data!(repository_stock_value_params, current_user)
|
2022-02-02 17:33:02 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def create!
|
|
|
|
repository_cell = @repository_row.repository_cells.create(repository_column: @repository_column)
|
|
|
|
@repository_stock_value = RepositoryStockValue.new_with_payload(
|
2022-03-09 21:19:15 +08:00
|
|
|
repository_stock_value_params,
|
2022-02-02 17:33:02 +08:00
|
|
|
repository_cell: repository_cell,
|
|
|
|
created_by: current_user,
|
2022-04-18 22:24:27 +08:00
|
|
|
last_modified_by: current_user,
|
|
|
|
comment: repository_stock_value_params[:comment].presence
|
2022-02-02 17:33:02 +08:00
|
|
|
)
|
|
|
|
@repository_stock_value.save!
|
|
|
|
end
|
|
|
|
|
2022-01-20 19:44:57 +08:00
|
|
|
def load_vars
|
|
|
|
@repository = Repository.find(params[:repository_id])
|
|
|
|
@repository_row = @repository.repository_rows.find(params[:id])
|
|
|
|
@repository_column = @repository.repository_columns.find_by(data_type: 'RepositoryStockValue')
|
|
|
|
@repository_stock_value = @repository_row.repository_stock_value
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_manage_permissions
|
2022-03-29 18:33:28 +08:00
|
|
|
render_403 unless can_manage_repository_stock?(@repository)
|
2022-01-20 19:44:57 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def repository_stock_value_params
|
2022-03-09 21:19:15 +08:00
|
|
|
params.require(:repository_stock_value).permit(:unit_item_id, :amount, :comment, :low_stock_threshold)
|
2022-01-20 19:44:57 +08:00
|
|
|
end
|
2022-02-02 17:33:02 +08:00
|
|
|
|
2022-04-20 17:29:16 +08:00
|
|
|
def log_activity(operator, change_amount, unit, new_amount, comment)
|
2022-02-02 17:33:02 +08:00
|
|
|
Activities::CreateActivityService
|
|
|
|
.call(activity_type: "inventory_item_stock_#{operator}",
|
|
|
|
owner: current_user,
|
2022-02-14 17:37:58 +08:00
|
|
|
subject: @repository,
|
2022-02-02 17:33:02 +08:00
|
|
|
team: @repository.team,
|
|
|
|
message_items: {
|
|
|
|
repository: @repository.id,
|
|
|
|
repository_row: @repository_row.id,
|
|
|
|
change_amount: change_amount,
|
|
|
|
unit: unit,
|
2022-04-20 17:29:16 +08:00
|
|
|
new_amount: new_amount,
|
|
|
|
comment: comment
|
2022-02-02 17:33:02 +08:00
|
|
|
})
|
|
|
|
end
|
2022-01-20 19:44:57 +08:00
|
|
|
end
|