From 12adfe4289f502e304d8f116fd3c29d09df6e1af Mon Sep 17 00:00:00 2001 From: viehlieb Date: Mon, 20 Feb 2023 19:56:45 +0100 Subject: [PATCH] update article category implemented --- app/controllers/articles_controller.rb | 1 + app/models/article.rb | 32 +++++++++++++----------- app/models/supplier.rb | 9 +++++-- app/views/articles/_sync_table.html.haml | 3 ++- app/views/articles/upload.html.haml | 3 +++ 5 files changed, 31 insertions(+), 17 deletions(-) diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index 4161e66a..a3ae0676 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -151,6 +151,7 @@ class ArticlesController < ApplicationController 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, 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/app/models/article.rb b/app/models/article.rb index 76a68605..e329b49d 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -142,21 +142,25 @@ class Article < ApplicationRecord new_unit_quantity = new_article.unit_quantity new_unit = new_article.unit end + if options[:update_category] == true + new_article_category = new_article.article_category + end - return Article.compare_attributes( - { - :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] - } - ) + 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] + } + attribute_hash[:article_category] = [self.article_category, new_article_category] if new_article_category + + Article.compare_attributes(attribute_hash) end # Compare attributes from two different articles. diff --git a/app/models/supplier.rb b/app/models/supplier.rb index 862f5c24..6daab986 100644 --- a/app/models/supplier.rb +++ b/app/models/supplier.rb @@ -81,7 +81,12 @@ class Supplier < ApplicationRecord updated_article_pairs, outlisted_articles, new_articles = [], [], [] FoodsoftFile::parse file, options do |status, new_attrs, line| article = articles.undeleted.where(order_number: new_attrs[:order_number]).first - new_attrs[:article_category] = ArticleCategory.find_match(new_attrs[:article_category]) + + + unless new_attrs[:article_category].blank? + new_attrs[:article_category] = ArticleCategory.find_match(new_attrs[:article_category]) ? ArticleCategory.find_match(new_attrs[:article_category]) : ArticleCategory.create_or_find_by!(name: new_attrs[:article_category]) + end + new_attrs[:tax] ||= FoodsoftConfig[:tax_default] new_article = articles.build(new_attrs) @@ -89,7 +94,7 @@ class Supplier < ApplicationRecord 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/app/views/articles/_sync_table.html.haml b/app/views/articles/_sync_table.html.haml index ac17adfa..62640cbe 100644 --- a/app/views/articles/_sync_table.html.haml +++ b/app/views/articles/_sync_table.html.haml @@ -49,7 +49,8 @@ .input-prepend %span.add-on= t 'number.currency.format.unit' = form.text_field 'deposit', class: 'input-mini', style: 'width: 45px' - %td= form.select :article_category_id, ArticleCategory.all.map {|a| [ a.name, a.id ] }, + %td{:style => highlight_new(attrs, :article_category)} + = form.select :article_category_id, ArticleCategory.all.map {|a| [ a.name, a.id ] }, {include_blank: true}, class: 'input-small' - unless changed_article.errors.empty? %tr.alert diff --git a/app/views/articles/upload.html.haml b/app/views/articles/upload.html.haml index 8f91d790..7695d28b 100644 --- a/app/views/articles/upload.html.haml +++ b/app/views/articles/upload.html.haml @@ -76,6 +76,9 @@ = f.file_field "file" .control-group + %label(for="articles_update_category") + = f.check_box "update_category" + = "update category ?" %label(for="articles_outlist_absent") = f.check_box "outlist_absent" = t '.options.outlist_absent'