From 94083cf27091a89892413ec9f545f49c07b19cf7 Mon Sep 17 00:00:00 2001 From: Alex Kriuchykhin Date: Wed, 13 Apr 2022 11:11:07 +0200 Subject: [PATCH] Revert "tmp (#3935)" (#4014) This reverts commit 043086d55fe0c7adeac528662e1433ccdb91aed0. --- app/models/my_module_repository_row.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/models/my_module_repository_row.rb b/app/models/my_module_repository_row.rb index f0e23559d..b5c87762f 100644 --- a/app/models/my_module_repository_row.rb +++ b/app/models/my_module_repository_row.rb @@ -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