Class: FoodsoftFile
- Inherits:
-
Object
- Object
- FoodsoftFile
- Defined in:
- lib/foodsoft_file.rb
Overview
Foodsoft-file import
Class Method Summary collapse
-
.parse(file, options = {}) ⇒ Object
parses a string from a foodsoft-file returns two arrays with articles and outlisted_articles the parsed article is a simple hash.
- .parse_ratios_cell(ratios_cell) ⇒ Object
Class Method Details
.parse(file, options = {}) ⇒ Object
parses a string from a foodsoft-file returns two arrays with articles and outlisted_articles the parsed article is a simple hash
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/foodsoft_file.rb', line 6 def self.parse(file, = {}) foodsoft_url = nil articles = [] SpreadsheetFile.parse file, do |row, index| foodsoft_url = row[18] if index == 1 next if index == 1 || row[2].blank? article = { availability: row[0]&.strip == I18n.t('simple_form.yes'), order_number: row[1], name: row[2], supplier_order_unit: ArticleUnitsLib.get_code_for_unit_name(row[3]), unit: row[4], article_unit_ratios: FoodsoftFile.parse_ratios_cell(row[5]), minimum_order_quantity: row[6], billing_unit: ArticleUnitsLib.get_code_for_unit_name(row[7]), group_order_granularity: row[8], group_order_unit: ArticleUnitsLib.get_code_for_unit_name(row[9]), price: row[10], price_unit: ArticleUnitsLib.get_code_for_unit_name(row[11]), tax: row[12], deposit: (row[13].nil? ? '0' : row[13]), note: row[14], article_category: row[15], origin: row[16], manufacturer: row[17] } article[:id] = row[18] unless foodsoft_url.blank? || foodsoft_url != [:foodsoft_url] articles << article end articles end |
.parse_ratios_cell(ratios_cell) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/foodsoft_file.rb', line 38 def self.parse_ratios_cell(ratios_cell) return [] if ratios_cell.blank? previous_quantity = nil ratios = ratios_cell.split(/(?<!\\), /).each_with_index.map do |ratio_str, index| md = ratio_str.gsub('\\\\', '\\').gsub('\\,', ',').match(/(?<quantity>[+-]?(?:[0-9]*[.])?[0-9]+) (?<unit_name>.*)/) quantity = md[:quantity].to_d calculated_quantity = previous_quantity.nil? ? quantity : quantity * previous_quantity previous_quantity = calculated_quantity { sort: index + 1, quantity: calculated_quantity, unit: ArticleUnitsLib.get_code_for_unit_name(md[:unit_name]) } end ratios.reject { |ratio| ratio[:unit].nil? } end |