add update_category as option
This commit is contained in:
parent
e9df226580
commit
1ec40fef4f
5 changed files with 54 additions and 4 deletions
|
@ -2,4 +2,9 @@
|
||||||
- if FoodsoftArticleImport.enabled?
|
- if FoodsoftArticleImport.enabled?
|
||||||
%label(for="articles_file")
|
%label(for="articles_file")
|
||||||
%strong="select the file type you are about to upload"
|
%strong="select the file type you are about to upload"
|
||||||
=f.collection_select :type, FoodsoftArticleImport::FORMATS , :to_s, :to_s
|
=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.'
|
|
@ -5,6 +5,8 @@ ArticlesController.class_eval do
|
||||||
options = { filename: uploaded_file.original_filename }
|
options = { filename: uploaded_file.original_filename }
|
||||||
options[:outlist_absent] = (params[:articles]['outlist_absent'] == '1')
|
options[:outlist_absent] = (params[:articles]['outlist_absent'] == '1')
|
||||||
options[:convert_units] = (params[:articles]['convert_units'] == '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
|
@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?
|
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')
|
redirect_to supplier_articles_path(@supplier), :notice => I18n.t('articles.controller.parse_upload.notice')
|
||||||
|
|
|
@ -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
|
|
@ -13,14 +13,20 @@ Supplier.class_eval do
|
||||||
custom_codes_file_path = custom_codes_path if File.exist?(custom_codes_path)
|
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|
|
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
|
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_attrs[:tax] ||= FoodsoftConfig[:tax_default]
|
||||||
new_article = articles.build(new_attrs)
|
new_article = articles.build(new_attrs)
|
||||||
if status.nil?
|
if status.nil?
|
||||||
if article.nil?
|
if article.nil?
|
||||||
new_articles << new_article
|
new_articles << new_article
|
||||||
else
|
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?
|
unless unequal_attributes.empty?
|
||||||
article.attributes = unequal_attributes
|
article.attributes = unequal_attributes
|
||||||
updated_article_pairs << [article, unequal_attributes]
|
updated_article_pairs << [article, unequal_attributes]
|
||||||
|
|
|
@ -67,7 +67,7 @@ module FoodsoftArticleImport
|
||||||
opts[:csv_options][:encoding] = encoding if encoding
|
opts[:csv_options][:encoding] = encoding if encoding
|
||||||
opts[:csv_options][:col_sep] = col_sep if col_sep
|
opts[:csv_options][:col_sep] = col_sep if col_sep
|
||||||
opts[:csv_options][:liberal_parsing] = true if liberal_parsing
|
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
|
begin
|
||||||
Roo::Spreadsheet.open(file, **opts)
|
Roo::Spreadsheet.open(file, **opts)
|
||||||
rescue StandardError => e
|
rescue StandardError => e
|
||||||
|
|
Loading…
Reference in a new issue