From 317379ed4065cc527cb6e9222f434e2974a47501 Mon Sep 17 00:00:00 2001 From: viehlieb Date: Wed, 29 Mar 2023 16:16:54 +0200 Subject: [PATCH] add update_category as option --- ...l_with_file_format_option.html.haml.deface | 7 +++- .../articles_controller_override.rb | 2 + .../app/overrides/models/article_override.rb | 37 +++++++++++++++++++ .../app/overrides/models/supplier_override.rb | 10 ++++- .../lib/foodsoft_article_import.rb | 2 +- 5 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 plugins/article_import/app/overrides/models/article_override.rb diff --git a/plugins/article_import/app/overrides/articles/upload/replace_label_with_file_format_option.html.haml.deface b/plugins/article_import/app/overrides/articles/upload/replace_label_with_file_format_option.html.haml.deface index 1e5b8065..a662d619 100644 --- a/plugins/article_import/app/overrides/articles/upload/replace_label_with_file_format_option.html.haml.deface +++ b/plugins/article_import/app/overrides/articles/upload/replace_label_with_file_format_option.html.haml.deface @@ -2,4 +2,9 @@ - if FoodsoftArticleImport.enabled? %label(for="articles_file") %strong="select the file type you are about to upload" - =f.collection_select :type, FoodsoftArticleImport::FORMATS , :to_s, :to_s \ No newline at end of file + =f.collection_select :type, FoodsoftArticleImport::FORMATS , :to_s, :to_s +/ insert_before 'erb:contains("articles_outlist_absent")' +-if FoodsoftArticleImport.enabled? + %label(for="articles_update_category") + = f.check_box "update_category" + = t 'Kategorien aus der Datei übernehmen und erstellen.' \ No newline at end of file diff --git a/plugins/article_import/app/overrides/controllers/articles_controller_override.rb b/plugins/article_import/app/overrides/controllers/articles_controller_override.rb index 707bbefa..43e9773b 100644 --- a/plugins/article_import/app/overrides/controllers/articles_controller_override.rb +++ b/plugins/article_import/app/overrides/controllers/articles_controller_override.rb @@ -5,6 +5,8 @@ ArticlesController.class_eval do options = { filename: uploaded_file.original_filename } options[:outlist_absent] = (params[:articles]['outlist_absent'] == '1') options[:convert_units] = (params[:articles]['convert_units'] == '1') + options[:update_category] = (params[:articles]['update_category'] == '1') + @updated_article_pairs, @outlisted_articles, @new_articles = @supplier.sync_from_file uploaded_file.tempfile, type, options if @updated_article_pairs.empty? && @outlisted_articles.empty? && @new_articles.empty? redirect_to supplier_articles_path(@supplier), :notice => I18n.t('articles.controller.parse_upload.notice') diff --git a/plugins/article_import/app/overrides/models/article_override.rb b/plugins/article_import/app/overrides/models/article_override.rb new file mode 100644 index 00000000..2018a4e7 --- /dev/null +++ b/plugins/article_import/app/overrides/models/article_override.rb @@ -0,0 +1,37 @@ +Article.class_eval do + def unequal_attributes(new_article, options = {}) + # try to convert different units when desired + if options[:convert_units] == false + new_price = nil + new_unit_quantity = nil + else + new_price, new_unit_quantity = convert_units(new_article) + end + if new_price && new_unit_quantity + new_unit = self.unit + else + new_price = new_article.price + new_unit_quantity = new_article.unit_quantity + new_unit = new_article.unit + end + + attribute_hash = { + :name => [self.name, new_article.name], + :manufacturer => [self.manufacturer, new_article.manufacturer.to_s], + :origin => [self.origin, new_article.origin], + :unit => [self.unit, new_unit], + :price => [self.price.to_f.round(2), new_price.to_f.round(2)], + :tax => [self.tax, new_article.tax], + :deposit => [self.deposit.to_f.round(2), new_article.deposit.to_f.round(2)], + # take care of different num-objects. + :unit_quantity => [self.unit_quantity.to_s.to_f, new_unit_quantity.to_s.to_f], + :note => [self.note.to_s, new_article.note.to_s] + } + if options[:update_category] == true + new_article_category = new_article.article_category + attribute_hash[:article_category] = [self.article_category, new_article_category] unless new_article_category.blank? + end + + Article.compare_attributes(attribute_hash) + end +end diff --git a/plugins/article_import/app/overrides/models/supplier_override.rb b/plugins/article_import/app/overrides/models/supplier_override.rb index ec6838d4..504bc26d 100644 --- a/plugins/article_import/app/overrides/models/supplier_override.rb +++ b/plugins/article_import/app/overrides/models/supplier_override.rb @@ -13,14 +13,20 @@ Supplier.class_eval do custom_codes_file_path = custom_codes_path if File.exist?(custom_codes_path) FoodsoftArticleImport.parse(file, custom_file_path: custom_codes_file_path, type: type, **opts) do |new_attrs, status, line| article = articles.undeleted.where(order_number: new_attrs[:order_number]).first - new_attrs[:article_category] = ArticleCategory.find_match(new_attrs[:article_category]) + + if new_attrs[:article_category].present? && options[:update_category] + new_attrs[:article_category] = ArticleCategory.find_match(new_attrs[:article_category]) || ArticleCategory.create_or_find_by!(name: new_attrs[:article_category]) + else + new_attrs[:article_category] = nil + end + new_attrs[:tax] ||= FoodsoftConfig[:tax_default] new_article = articles.build(new_attrs) if status.nil? if article.nil? new_articles << new_article else - unequal_attributes = article.unequal_attributes(new_article, options.slice(:convert_units)) + unequal_attributes = article.unequal_attributes(new_article, options.slice(:convert_units, :update_category)) unless unequal_attributes.empty? article.attributes = unequal_attributes updated_article_pairs << [article, unequal_attributes] diff --git a/plugins/article_import/lib/foodsoft_article_import.rb b/plugins/article_import/lib/foodsoft_article_import.rb index 5f82a74f..5bd48dd8 100644 --- a/plugins/article_import/lib/foodsoft_article_import.rb +++ b/plugins/article_import/lib/foodsoft_article_import.rb @@ -66,7 +66,7 @@ module FoodsoftArticleImport opts[:csv_options][:encoding] = encoding if encoding opts[:csv_options][:col_sep] = col_sep if col_sep opts[:csv_options][:liberal_parsing] = true if liberal_parsing - opts[:extension] = File.extname(File.basename(@@filename)) if @@filename + opts[:extension] = File.extname(File.basename(file)) if file begin Roo::Spreadsheet.open(file, **opts) rescue StandardError => e