Revert "tmp (#3935)" (#4014)

This reverts commit 043086d55f.
This commit is contained in:
Alex Kriuchykhin 2022-04-13 11:11:07 +02:00 committed by GitHub
parent 043086d55f
commit 94083cf270
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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