mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-02-23 23:35:00 +08:00
Improve stock ledger records creation [SCI-6419]
This commit is contained in:
parent
02677aaaba
commit
e50d52e8ef
4 changed files with 24 additions and 33 deletions
|
@ -59,14 +59,10 @@ class RepositoryStockValuesController < ApplicationController
|
|||
repository_stock_value_params,
|
||||
repository_cell: repository_cell,
|
||||
created_by: current_user,
|
||||
last_modified_by: current_user
|
||||
last_modified_by: current_user,
|
||||
comment: repository_stock_value_params[:comment].presence
|
||||
)
|
||||
@repository_stock_value.save!
|
||||
@repository_stock_value.update_stock_with_ledger!(
|
||||
repository_stock_value_params[:amount],
|
||||
@repository,
|
||||
repository_stock_value_params[:comment].presence
|
||||
)
|
||||
end
|
||||
|
||||
def load_vars
|
||||
|
|
|
@ -15,9 +15,8 @@ class MyModuleRepositoryRow < ApplicationRecord
|
|||
|
||||
validates :repository_row, uniqueness: { scope: :my_module }
|
||||
|
||||
around_save :deduct_stock_balance, if: :stock_consumption_changed?
|
||||
|
||||
before_save :nulify_stock_consumption, if: :stock_consumption_changed?
|
||||
around_save :deduct_stock_balance, if: :stock_consumption_changed?
|
||||
|
||||
def consume_stock(user, stock_consumption, comment = nil)
|
||||
ActiveRecord::Base.transaction(requires_new: true) do
|
||||
|
@ -41,18 +40,18 @@ class MyModuleRepositoryRow < ApplicationRecord
|
|||
|
||||
def deduct_stock_balance
|
||||
stock_value = repository_row.repository_stock_value
|
||||
delta = stock_consumption.to_d - stock_consumption_was.to_d
|
||||
stock_value.lock!
|
||||
delta = stock_consumption.to_d - stock_consumption_was.to_d
|
||||
stock_value.amount = stock_value.amount - delta
|
||||
yield
|
||||
stock_value.save!
|
||||
stock_value.repository_ledger_records.create!(
|
||||
reference: self,
|
||||
user: last_modified_by,
|
||||
user: last_modified_by || assigned_by,
|
||||
amount: delta,
|
||||
balance: stock_value.amount,
|
||||
comment: comment
|
||||
)
|
||||
stock_value.save!
|
||||
save!
|
||||
end
|
||||
end
|
||||
|
|
|
@ -58,12 +58,6 @@ class RepositoryCell < ApplicationRecord
|
|||
last_modified_by: user)
|
||||
cell.value = value
|
||||
value.save!
|
||||
|
||||
if column.data_type == 'RepositoryStockValue'
|
||||
value.update_stock_with_ledger!(value.amount,
|
||||
value.repository_cell.repository_column.repository,
|
||||
'')
|
||||
end
|
||||
end
|
||||
cell
|
||||
end
|
||||
|
|
|
@ -4,6 +4,8 @@ class RepositoryStockValue < ApplicationRecord
|
|||
include RepositoryValueWithReminders
|
||||
include ActionView::Helpers::NumberHelper
|
||||
|
||||
attr_accessor :comment
|
||||
|
||||
belongs_to :repository_stock_unit_item, optional: true
|
||||
belongs_to :created_by, class_name: 'User', optional: true, inverse_of: :created_repository_stock_values
|
||||
belongs_to :last_modified_by, class_name: 'User', optional: true, inverse_of: :modified_repository_stock_values
|
||||
|
@ -13,6 +15,14 @@ class RepositoryStockValue < ApplicationRecord
|
|||
|
||||
validates :repository_cell, presence: true
|
||||
|
||||
after_create do
|
||||
repository_ledger_records.create!(user: created_by,
|
||||
amount: amount,
|
||||
balance: amount,
|
||||
reference: repository_cell.repository_column.repository,
|
||||
comment: comment)
|
||||
end
|
||||
|
||||
SORTABLE_COLUMN_NAME = 'repository_stock_values.amount'
|
||||
|
||||
def formatted
|
||||
|
@ -97,11 +107,14 @@ class RepositoryStockValue < ApplicationRecord
|
|||
.repository_stock_unit_items
|
||||
.find(new_data[:unit_item_id])
|
||||
self.last_modified_by = user
|
||||
|
||||
update_stock_with_ledger!(new_data[:amount],
|
||||
repository_cell.repository_column.repository,
|
||||
new_data[:comment].presence)
|
||||
|
||||
delta = new_data[:amount].to_d - amount.to_d
|
||||
repository_ledger_records.create!(
|
||||
user: last_modified_by,
|
||||
amount: delta,
|
||||
balance: amount,
|
||||
reference: repository_cell.repository_column.repository,
|
||||
comment: new_data[:comment].presence
|
||||
)
|
||||
self.amount = BigDecimal(new_data[:amount].to_s)
|
||||
save!
|
||||
end
|
||||
|
@ -168,16 +181,5 @@ class RepositoryStockValue < ApplicationRecord
|
|||
nil
|
||||
end
|
||||
|
||||
def update_stock_with_ledger!(amount, reference, comment)
|
||||
delta = amount.to_d - self.amount.to_d
|
||||
repository_ledger_records.create!(
|
||||
user: last_modified_by,
|
||||
amount: delta,
|
||||
balance: amount,
|
||||
reference: reference,
|
||||
comment: comment
|
||||
)
|
||||
end
|
||||
|
||||
alias export_formatted formatted
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue