2022-01-13 00:13:50 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class RepositoryStockValue < ApplicationRecord
|
2022-03-23 16:51:42 +08:00
|
|
|
include RepositoryValueWithReminders
|
2022-03-28 18:43:09 +08:00
|
|
|
include ActionView::Helpers::NumberHelper
|
2022-03-23 16:51:42 +08:00
|
|
|
|
2022-06-09 19:18:00 +08:00
|
|
|
attribute :comment, :text
|
2022-04-18 22:24:27 +08:00
|
|
|
|
2022-01-26 00:48:45 +08:00
|
|
|
belongs_to :repository_stock_unit_item, optional: true
|
2022-01-20 19:44:57 +08:00
|
|
|
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
|
2022-01-13 00:13:50 +08:00
|
|
|
has_one :repository_cell, as: :value, dependent: :destroy, inverse_of: :value
|
2023-09-07 19:12:27 +08:00
|
|
|
has_one :repository_row, through: :repository_cell
|
2022-01-13 00:13:50 +08:00
|
|
|
has_many :repository_ledger_records, dependent: :destroy
|
|
|
|
accepts_nested_attributes_for :repository_cell
|
|
|
|
|
|
|
|
validates :repository_cell, presence: true
|
2022-04-25 18:04:07 +08:00
|
|
|
validates :low_stock_threshold, numericality: { greater_than_or_equal_to: 0 }, allow_nil: true
|
2022-01-13 00:13:50 +08:00
|
|
|
|
2022-05-10 16:19:37 +08:00
|
|
|
before_save :update_consumption_stock_units, if: :repository_stock_unit_item_id_changed?
|
2022-04-18 22:24:27 +08:00
|
|
|
after_create do
|
2022-04-22 16:49:42 +08:00
|
|
|
next if is_a?(RepositoryStockConsumptionValue)
|
|
|
|
|
2022-04-18 22:24:27 +08:00
|
|
|
repository_ledger_records.create!(user: created_by,
|
|
|
|
amount: amount,
|
|
|
|
balance: amount,
|
|
|
|
reference: repository_cell.repository_column.repository,
|
2023-08-22 16:47:58 +08:00
|
|
|
comment: comment,
|
|
|
|
unit: repository_stock_unit_item&.data)
|
2022-04-18 22:24:27 +08:00
|
|
|
end
|
|
|
|
|
2022-01-13 00:13:50 +08:00
|
|
|
SORTABLE_COLUMN_NAME = 'repository_stock_values.amount'
|
|
|
|
|
|
|
|
def formatted
|
2022-03-30 20:03:07 +08:00
|
|
|
"#{formatted_value} #{repository_stock_unit_item&.data}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def formatted_value
|
|
|
|
if repository_cell
|
|
|
|
number_with_precision(
|
|
|
|
amount,
|
|
|
|
precision: (repository_cell.repository_column.metadata['decimals'].to_i || 0),
|
|
|
|
strip_insignificant_zeros: true
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def formatted_treshold
|
|
|
|
if repository_cell && low_stock_threshold
|
|
|
|
number_with_precision(
|
|
|
|
low_stock_threshold,
|
|
|
|
precision: (repository_cell.repository_column.metadata['decimals'].to_i || 0),
|
|
|
|
strip_insignificant_zeros: true
|
|
|
|
)
|
|
|
|
end
|
2022-01-13 00:13:50 +08:00
|
|
|
end
|
|
|
|
|
2022-04-14 20:27:31 +08:00
|
|
|
def status
|
|
|
|
return :empty if amount <= 0
|
2022-03-03 18:13:17 +08:00
|
|
|
|
2022-04-14 20:27:31 +08:00
|
|
|
return :low if low_stock_threshold && amount <= low_stock_threshold
|
|
|
|
|
|
|
|
:normal
|
2022-03-03 18:13:17 +08:00
|
|
|
end
|
|
|
|
|
2022-03-23 17:05:07 +08:00
|
|
|
def self.add_filter_condition(repository_rows, join_alias, filter_element)
|
|
|
|
parameters = filter_element.parameters
|
|
|
|
if filter_element.operator == 'between'
|
|
|
|
return repository_rows if parameters['from'].blank? || parameters['to'].blank?
|
|
|
|
elsif parameters['value'].blank?
|
|
|
|
return repository_rows
|
|
|
|
end
|
|
|
|
|
|
|
|
repository_rows = case parameters['stock_unit']
|
|
|
|
when 'all'
|
|
|
|
repository_rows.where("#{join_alias}.repository_stock_unit_item_id IS NOT NULL")
|
|
|
|
when 'none'
|
|
|
|
repository_rows.where("#{join_alias}.repository_stock_unit_item_id IS NULL")
|
|
|
|
else
|
|
|
|
repository_rows.where("#{join_alias}.repository_stock_unit_item_id = ?", parameters['stock_unit'])
|
|
|
|
end
|
|
|
|
|
|
|
|
case filter_element.operator
|
|
|
|
when 'equal_to'
|
|
|
|
repository_rows.where("#{join_alias}.amount = ?", parameters['value'].to_d)
|
|
|
|
when 'unequal_to'
|
|
|
|
repository_rows.where.not("#{join_alias}.amount = ?", parameters['value'].to_d)
|
|
|
|
when 'greater_than'
|
|
|
|
repository_rows.where("#{join_alias}.amount > ?", parameters['value'].to_d)
|
|
|
|
when 'greater_than_or_equal_to'
|
|
|
|
repository_rows.where("#{join_alias}.amount >= ?", parameters['value'].to_d)
|
|
|
|
when 'less_than'
|
|
|
|
repository_rows.where("#{join_alias}.amount < ?", parameters['value'].to_d)
|
|
|
|
when 'less_than_or_equal_to'
|
|
|
|
repository_rows.where("#{join_alias}.amount <= ?", parameters['value'].to_d)
|
|
|
|
when 'between'
|
|
|
|
repository_rows
|
|
|
|
.where("#{join_alias}.amount > ? AND #{join_alias}.amount < ?", parameters['from'].to_d, parameters['to'].to_d)
|
|
|
|
else
|
|
|
|
raise ArgumentError, 'Wrong operator for RepositoryStockValue!'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-08 17:30:44 +08:00
|
|
|
def data_different?(new_data)
|
|
|
|
BigDecimal(new_data[:amount].to_s) != amount ||
|
|
|
|
(new_data[:unit_item_id].present? && new_data[:unit_item_id] != repository_stock_unit_item.id)
|
2022-01-13 00:13:50 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_data!(new_data, user)
|
2022-04-13 17:35:45 +08:00
|
|
|
self.low_stock_threshold = new_data[:low_stock_threshold].presence if new_data[:low_stock_threshold]
|
2022-04-08 17:30:44 +08:00
|
|
|
self.repository_stock_unit_item = repository_cell
|
|
|
|
.repository_column
|
|
|
|
.repository_stock_unit_items
|
|
|
|
.find(new_data[:unit_item_id])
|
2022-01-13 00:13:50 +08:00
|
|
|
self.last_modified_by = user
|
2023-08-22 16:47:58 +08:00
|
|
|
new_amount = new_data[:amount].to_d
|
|
|
|
delta = new_amount - amount.to_d
|
2022-06-09 19:18:00 +08:00
|
|
|
self.comment = new_data[:comment].presence
|
2022-04-18 22:24:27 +08:00
|
|
|
repository_ledger_records.create!(
|
|
|
|
user: last_modified_by,
|
|
|
|
amount: delta,
|
2023-08-22 16:47:58 +08:00
|
|
|
balance: new_amount,
|
2022-04-18 22:24:27 +08:00
|
|
|
reference: repository_cell.repository_column.repository,
|
2023-08-22 16:47:58 +08:00
|
|
|
comment: comment,
|
|
|
|
unit: repository_stock_unit_item&.data
|
2022-04-18 22:24:27 +08:00
|
|
|
)
|
2023-08-22 16:47:58 +08:00
|
|
|
self.amount = new_amount
|
2022-01-13 00:13:50 +08:00
|
|
|
save!
|
|
|
|
end
|
|
|
|
|
|
|
|
def snapshot!(cell_snapshot)
|
|
|
|
value_snapshot = dup
|
2022-01-26 00:48:45 +08:00
|
|
|
stock_unit_item =
|
|
|
|
if repository_stock_unit_item.present?
|
|
|
|
cell_snapshot.repository_column
|
|
|
|
.repository_stock_unit_items
|
|
|
|
.find { |item| item.data == repository_stock_unit_item.data }
|
|
|
|
end
|
2022-01-13 00:13:50 +08:00
|
|
|
value_snapshot.assign_attributes(
|
|
|
|
repository_cell: cell_snapshot,
|
2022-01-26 00:48:45 +08:00
|
|
|
repository_stock_unit_item: stock_unit_item,
|
2022-01-13 00:13:50 +08:00
|
|
|
created_at: created_at,
|
|
|
|
updated_at: updated_at
|
|
|
|
)
|
|
|
|
value_snapshot.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
def data
|
|
|
|
amount
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.new_with_payload(payload, attributes)
|
2022-04-08 17:30:44 +08:00
|
|
|
if payload[:amount].present?
|
|
|
|
value = new(attributes)
|
|
|
|
value.amount = payload[:amount]
|
|
|
|
value.low_stock_threshold = payload[:low_stock_threshold]
|
|
|
|
value.repository_stock_unit_item = value.repository_cell
|
|
|
|
.repository_column
|
|
|
|
.repository_stock_unit_items
|
|
|
|
.find(payload['unit_item_id'])
|
|
|
|
value
|
|
|
|
else
|
|
|
|
raise ActiveRecord::RecordInvalid, 'Missing amount value'
|
|
|
|
end
|
2022-01-13 00:13:50 +08:00
|
|
|
end
|
|
|
|
|
2022-02-04 17:29:48 +08:00
|
|
|
def self.import_from_text(text, attributes, _options = {})
|
|
|
|
digit, unit = text.match(/(^\d*\.?\d*)(\D*)/).captures
|
|
|
|
digit.strip!
|
|
|
|
unit.strip!
|
|
|
|
return nil if digit.blank?
|
|
|
|
|
|
|
|
value = new(attributes.merge(amount: BigDecimal(digit)))
|
|
|
|
return value if unit.blank?
|
|
|
|
|
|
|
|
column = attributes.dig(:repository_cell_attributes, :repository_column)
|
|
|
|
stock_unit_item = column.repository_stock_unit_items.find { |item| item.data == unit }
|
|
|
|
|
|
|
|
if stock_unit_item.blank?
|
|
|
|
stock_unit_item = column.repository_stock_unit_items.new(data: unit,
|
|
|
|
created_by: value.created_by,
|
|
|
|
last_modified_by: value.last_modified_by)
|
|
|
|
|
|
|
|
return value unless stock_unit_item.save
|
|
|
|
end
|
|
|
|
|
|
|
|
value.repository_stock_unit_item = stock_unit_item
|
|
|
|
value
|
|
|
|
rescue ArgumentError
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2022-01-13 00:13:50 +08:00
|
|
|
alias export_formatted formatted
|
2022-05-10 16:19:37 +08:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def update_consumption_stock_units
|
|
|
|
repository_cell.repository_row
|
|
|
|
.my_module_repository_rows
|
|
|
|
.update_all(repository_stock_unit_item_id: repository_stock_unit_item_id)
|
|
|
|
end
|
2022-01-13 00:13:50 +08:00
|
|
|
end
|