2017-06-23 21:19:08 +08:00
|
|
|
class MyModuleRepositoryRow < ApplicationRecord
|
2022-04-15 17:40:47 +08:00
|
|
|
include ActionView::Helpers::NumberHelper
|
|
|
|
|
2022-05-18 23:02:12 +08:00
|
|
|
attribute :last_modified_by, :integer
|
|
|
|
attribute :comment, :text
|
2022-01-28 18:10:56 +08:00
|
|
|
|
2017-06-28 21:21:32 +08:00
|
|
|
belongs_to :assigned_by,
|
|
|
|
foreign_key: 'assigned_by_id',
|
|
|
|
class_name: 'User',
|
|
|
|
optional: true
|
2018-03-16 20:12:33 +08:00
|
|
|
belongs_to :repository_row,
|
2019-01-17 22:04:20 +08:00
|
|
|
inverse_of: :my_module_repository_rows
|
|
|
|
belongs_to :my_module,
|
2019-01-03 21:11:00 +08:00
|
|
|
touch: true,
|
2018-03-16 20:12:33 +08:00
|
|
|
inverse_of: :my_module_repository_rows
|
2022-01-26 00:48:45 +08:00
|
|
|
belongs_to :repository_stock_unit_item, optional: true
|
2023-10-06 19:42:52 +08:00
|
|
|
has_many :repository_ledger_records, as: :reference
|
2017-06-06 23:35:29 +08:00
|
|
|
|
|
|
|
validates :repository_row, uniqueness: { scope: :my_module }
|
2022-01-13 00:13:50 +08:00
|
|
|
|
2022-04-13 17:11:07 +08:00
|
|
|
before_save :nulify_stock_consumption, if: :stock_consumption_changed?
|
2022-04-18 22:24:27 +08:00
|
|
|
around_save :deduct_stock_balance, if: :stock_consumption_changed?
|
2022-04-13 17:11:07 +08:00
|
|
|
|
2022-03-30 20:33:26 +08:00
|
|
|
def consume_stock(user, stock_consumption, comment = nil)
|
|
|
|
ActiveRecord::Base.transaction(requires_new: true) do
|
|
|
|
lock!
|
|
|
|
assign_attributes(
|
|
|
|
stock_consumption: stock_consumption,
|
|
|
|
repository_stock_unit_item_id:
|
|
|
|
repository_row.repository_stock_value.repository_stock_unit_item_id,
|
|
|
|
last_modified_by: user,
|
|
|
|
comment: comment
|
|
|
|
)
|
|
|
|
save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-15 17:40:47 +08:00
|
|
|
def formated_stock_consumption
|
|
|
|
if stock_consumption
|
|
|
|
number_with_precision(
|
|
|
|
stock_consumption,
|
|
|
|
precision: (repository_row.repository.repository_stock_column.metadata['decimals'].to_i || 0),
|
|
|
|
strip_insignificant_zeros: true
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-01-13 00:13:50 +08:00
|
|
|
private
|
|
|
|
|
2022-01-28 18:10:56 +08:00
|
|
|
def nulify_stock_consumption
|
|
|
|
self.stock_consumption = nil if stock_consumption.zero?
|
|
|
|
end
|
|
|
|
|
2022-01-13 00:13:50 +08:00
|
|
|
def deduct_stock_balance
|
|
|
|
stock_value = repository_row.repository_stock_value
|
2022-04-13 17:11:07 +08:00
|
|
|
stock_value.lock!
|
2022-04-18 22:24:27 +08:00
|
|
|
delta = stock_consumption.to_d - stock_consumption_was.to_d
|
2022-01-13 00:13:50 +08:00
|
|
|
stock_value.amount = stock_value.amount - delta
|
|
|
|
yield
|
2022-01-28 18:10:56 +08:00
|
|
|
stock_value.repository_ledger_records.create!(
|
|
|
|
reference: self,
|
2022-04-18 22:24:27 +08:00
|
|
|
user: last_modified_by || assigned_by,
|
2022-01-28 18:10:56 +08:00
|
|
|
amount: delta,
|
|
|
|
balance: stock_value.amount,
|
2023-08-22 16:47:58 +08:00
|
|
|
comment: comment,
|
2023-11-20 20:24:31 +08:00
|
|
|
unit: stock_value.repository_stock_unit_item&.data,
|
|
|
|
my_module_references: {
|
|
|
|
my_module_id: my_module.id,
|
|
|
|
experiment_id: my_module.experiment.id,
|
|
|
|
project_id: my_module.experiment.project.id,
|
|
|
|
team_id: my_module.experiment.project.team.id
|
|
|
|
}
|
2022-01-28 18:10:56 +08:00
|
|
|
)
|
2022-04-18 22:24:27 +08:00
|
|
|
stock_value.save!
|
2022-01-13 00:13:50 +08:00
|
|
|
save!
|
|
|
|
end
|
2017-06-06 23:35:29 +08:00
|
|
|
end
|