Implement import for repository stock values [SCI-6461] (#3818)

This commit is contained in:
Alex Kriuchykhin 2022-02-04 10:29:48 +01:00 committed by GitHub
parent cdbf1f5fa0
commit 4627d15fc3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 1 deletions

View file

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

View file

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