mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-01-01 21:21:50 +08:00
parent
043086d55f
commit
94083cf270
1 changed files with 4 additions and 3 deletions
|
@ -15,9 +15,10 @@ class MyModuleRepositoryRow < ApplicationRecord
|
|||
|
||||
validates :repository_row, uniqueness: { scope: :my_module }
|
||||
|
||||
before_save :nulify_stock_consumption, if: :stock_consumption_changed?
|
||||
around_save :deduct_stock_balance, if: :stock_consumption_changed?
|
||||
|
||||
before_save :nulify_stock_consumption, if: :stock_consumption_changed?
|
||||
|
||||
def consume_stock(user, stock_consumption, comment = nil)
|
||||
ActiveRecord::Base.transaction(requires_new: true) do
|
||||
lock!
|
||||
|
@ -40,10 +41,11 @@ class MyModuleRepositoryRow < ApplicationRecord
|
|||
|
||||
def deduct_stock_balance
|
||||
stock_value = repository_row.repository_stock_value
|
||||
stock_value.lock!
|
||||
delta = stock_consumption.to_d - stock_consumption_was.to_d
|
||||
stock_value.lock!
|
||||
stock_value.amount = stock_value.amount - delta
|
||||
yield
|
||||
stock_value.save!
|
||||
stock_value.repository_ledger_records.create!(
|
||||
reference: self,
|
||||
user: last_modified_by,
|
||||
|
@ -51,7 +53,6 @@ class MyModuleRepositoryRow < ApplicationRecord
|
|||
balance: stock_value.amount,
|
||||
comment: comment
|
||||
)
|
||||
stock_value.save!
|
||||
save!
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue