Skip to content

Commit

Permalink
On #47 Adapt unit ratio export/import to be interpreted as a conversi…
Browse files Browse the repository at this point in the history
…on chain instead of a conversion table
  • Loading branch information
lentschi committed Oct 11, 2024
1 parent 46326a3 commit 53ace88
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
13 changes: 10 additions & 3 deletions app/lib/articles_csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,20 @@ def data
article.minimum_order_quantity,
ArticleUnitsLib.get_translated_name_for_code(article.billing_unit),
article.article_category.try(:name),
article.article_unit_ratios.map do |ratio|
"#{ratio.quantity} #{escape_csv_ratio(ArticleUnitsLib.get_translated_name_for_code(ratio.unit))}"
end.join(', ')
get_csv_ratios(article)
]
end
end

def get_csv_ratios(article)
previous_quantity = nil
article.article_unit_ratios.each_with_index.map do |ratio, _index|
quantity = previous_quantity.nil? ? ratio.quantity : ratio.quantity / previous_quantity
previous_quantity = ratio.quantity
"#{quantity} #{escape_csv_ratio(ArticleUnitsLib.get_translated_name_for_code(ratio.unit))}"
end.join(', ')
end

def escape_csv_ratio(str)
str.gsub('\\', '\\\\').gsub(',', '\\,')
end
Expand Down
6 changes: 5 additions & 1 deletion app/lib/foodsoft_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@ def self.parse(file, options = {})
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: md[:quantity],
quantity: calculated_quantity,
unit: ArticleUnitsLib.get_code_for_unit_name(md[:unit_name])
}
end
Expand Down

0 comments on commit 53ace88

Please sign in to comment.