2022-01-20 19:44:57 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class RepositoryStockValuesController < ApplicationController
|
|
|
|
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,
|
|
|
|
@repository_stock_value.amount
|
|
|
|
)
|
2022-01-20 19:44:57 +08:00
|
|
|
end
|
2022-02-02 17:33:02 +08:00
|
|
|
|
2022-04-08 17:30:44 +08:00
|
|
|
render json: @repository_stock_value
|
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
|
|
|
|
|
|
|
def log_activity(operator, change_amount, unit, new_amount)
|
|
|
|
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,
|
|
|
|
new_amount: new_amount
|
|
|
|
})
|
|
|
|
end
|
2022-01-20 19:44:57 +08:00
|
|
|
end
|