update article category implemented

This commit is contained in:
viehlieb 2023-02-20 19:56:45 +01:00
parent 78da4feafe
commit 12adfe4289
5 changed files with 31 additions and 17 deletions

View file

@ -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')

View file

@ -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.

View file

@ -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]

View file

@ -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

View file

@ -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'