mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-09-15 09:34:29 +08:00
Implement import for repository stock values [SCI-6461] (#3818)
This commit is contained in:
parent
cdbf1f5fa0
commit
4627d15fc3
2 changed files with 27 additions and 1 deletions
|
@ -53,6 +53,32 @@ class RepositoryStockValue < ApplicationRecord
|
|||
value
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
def update_stock_with_ledger!(amount, reference, comment)
|
||||
delta = amount.to_d - self.amount.to_d
|
||||
repository_ledger_records.create!(
|
||||
|
|
|
@ -66,7 +66,7 @@ class Extends
|
|||
# name should match record in REPOSITORY_DATA_TYPES
|
||||
REPOSITORY_IMPORTABLE_TYPES = %i(RepositoryTextValue RepositoryListValue RepositoryNumberValue
|
||||
RepositoryDateValue RepositoryDateTimeValue RepositoryTimeValue
|
||||
RepositoryStatusValue RepositoryChecklistValue)
|
||||
RepositoryStatusValue RepositoryChecklistValue RepositoryStockValue)
|
||||
|
||||
REPOSITORY_IMPORT_COLUMN_PRELOADS = %i(repository_list_items repository_status_items repository_checklist_items)
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue