Skip to content

Commit

Permalink
Fixes #83
Browse files Browse the repository at this point in the history
  • Loading branch information
lentschi committed Jul 26, 2024
1 parent 9191f42 commit 3871c37
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 12 deletions.
4 changes: 4 additions & 0 deletions app/helpers/articles_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ def format_billing_unit(article)
format_unit(:billing_unit, article)
end

def format_price_unit(article)
format_unit(:price_unit, article)
end

def format_supplier_order_unit_with_ratios(article)
format_unit_with_ratios(:supplier_order_unit, article)
end
Expand Down
2 changes: 1 addition & 1 deletion app/lib/articles_csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def data
article.manufacturer,
article.origin,
article.unit,
article.price,
article.price_unit_price,
article.tax,
article.deposit,
ArticleUnitsLib.get_translated_name_for_code(article.supplier_order_unit),
Expand Down
6 changes: 6 additions & 0 deletions app/models/concerns/price_calculation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ def group_order_price(value = nil)
value / convert_quantity(1, supplier_order_unit, group_order_unit)
end

def price_unit_price(value = nil)
value ||= price
# price is always stored in supplier_order_unit:
value / convert_quantity(1, supplier_order_unit, price_unit)
end

def gross_group_order_price
group_order_price(gross_price)
end
Expand Down
29 changes: 20 additions & 9 deletions app/models/supplier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ def self.ransackable_associations(_auth_object = nil)
# @option options [Boolean] :convert_units Omit or set to +true+ to keep current units, recomputing unit quantity and price.
def sync_from_file(file, options = {})
data = FoodsoftFile.parse(file, options)
data.each do |new_attrs|
new_article = foodsoft_file_attrs_to_article(new_attrs.dup)
new_attrs[:price] = new_attrs[:price].to_d / new_article.convert_quantity(1, new_article.price_unit, new_article.supplier_order_unit)
end
parse_import_data({ articles: data }, options) + [data]
end

Expand Down Expand Up @@ -120,15 +124,7 @@ def parse_import_data(data, options = {})

data[:articles].each do |new_attrs|
article = articles.includes(:latest_article_version).undeleted.where(article_versions: { order_number: new_attrs[:order_number] }).first
new_attrs[:article_category] = ArticleCategory.find_match(new_attrs[:article_category])
new_attrs[:tax] ||= FoodsoftConfig[:tax_default]
new_attrs[:article_unit_ratios] = new_attrs[:article_unit_ratios].map do |ratio_hash|
ArticleUnitRatio.new(ratio_hash)
end
new_article = articles.build
new_article_version = new_article.article_versions.build(new_attrs)
new_article.article_versions << new_article_version
new_article.latest_article_version = new_article_version
new_article = foodsoft_file_attrs_to_article(new_attrs)

if new_attrs[:availability]
if article.nil?
Expand All @@ -152,4 +148,19 @@ def parse_import_data(data, options = {})
outlisted_articles += articles.includes(:latest_article_version).undeleted.where.not(article_versions: { order_number: all_order_numbers + [nil] }) if options[:outlist_absent]
[updated_article_pairs, outlisted_articles, new_articles]
end

def foodsoft_file_attrs_to_article(foodsoft_file_attrs)
foodsoft_file_attrs = foodsoft_file_attrs.dup
foodsoft_file_attrs[:article_category] = ArticleCategory.find_match(foodsoft_file_attrs[:article_category])
foodsoft_file_attrs[:tax] ||= FoodsoftConfig[:tax_default]
foodsoft_file_attrs[:article_unit_ratios] = foodsoft_file_attrs[:article_unit_ratios].map do |ratio_hash|
ArticleUnitRatio.new(ratio_hash)
end
new_article = articles.build
new_article_version = new_article.article_versions.build(foodsoft_file_attrs)
new_article.article_versions << new_article_version
new_article.latest_article_version = new_article_version

new_article
end
end
4 changes: 2 additions & 2 deletions app/views/articles/_sync_table.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
%td= article.manufacturer
%td= article.origin
%td= ArticleUnitsLib.get_translated_name_for_code(article.supplier_order_unit)
%td= number_to_currency article.price
%td= "#{number_to_currency(article.price_unit_price)} #{t('articles.form.per')} #{format_price_unit(article)}"
%td= number_to_percentage article.tax
%td= number_to_currency article.deposit
%td= article.article_category.name if article.article_category
Expand Down Expand Up @@ -67,7 +67,7 @@
- article.article_unit_ratios.each do |ratio|
%li
= "#{ratio.quantity} x #{ArticleUnitsLib.get_translated_name_for_code(ratio.unit)}"
= t 'per'
= t 'articles.form.per'
= ArticleUnitsLib.get_translated_name_for_code(article.supplier_order_unit)
.fold-line
= form.input :minimum_order_quantity, label: "Mininum order quantity" do
Expand Down

0 comments on commit 3871c37

Please sign in to comment.