Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

article import: add article category update #1024

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions app/controllers/articles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ def copy
render :layout => false
end

def edit
@article = Article.find(params[:id])
render :action => 'new', :layout => false
end

def create
@article = Article.new(params[:article])
if @article.valid? && @article.save
Expand All @@ -55,11 +60,6 @@ def create
end
end

def edit
@article = Article.find(params[:id])
render :action => 'new', :layout => false
end

# Updates one Article and highlights the line if succeded
def update
@article = Article.find(params[:id])
Expand Down Expand Up @@ -151,6 +151,7 @@ def parse_upload
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')
Expand Down
32 changes: 18 additions & 14 deletions app/models/article.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,20 +143,24 @@ def unequal_attributes(new_article, options = {})
new_unit = new_article.unit
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]
}
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

# Compare attributes from two different articles.
Expand Down
10 changes: 8 additions & 2 deletions app/models/supplier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,21 @@ def sync_from_file(file, options = {})
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])

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]
Expand Down
3 changes: 2 additions & 1 deletion app/views/articles/_sync_table.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions app/views/articles/upload.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@
= f.file_field "file"

.control-group
%label(for="articles_update_category")
= f.check_box "update_category"
= t '.options.update_category'
%label(for="articles_outlist_absent")
= f.check_box "outlist_absent"
= t '.options.outlist_absent'
Expand Down
1 change: 1 addition & 0 deletions config/locales/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,7 @@ de:
options:
convert_units: Derzeitige Einheiten beibehalten, berechne Mengeneinheit und Preis (wie Synchronisieren).
outlist_absent: Artikel löschen, die nicht in der hochgeladenen Datei sind.
update_category: Kategorien aus der Datei übernehmen und erstellen.
sample:
juices: Säfte
nuts: Nüsse
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@ en:
options:
convert_units: Keep current units, recompute unit quantity and price (like synchronize).
outlist_absent: Delete articles not in uploaded file.
update_category: Create and replace categories from uploaded file.
sample:
juices: Juices
nuts: Nuts
Expand Down
1 change: 1 addition & 0 deletions config/locales/es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,7 @@ es:
options:
convert_units: Mantener unidades actuales, recomputar la cantidad y precio de unidades (como sincronizar).
outlist_absent: Borrar artículos que no están en el archivo subido.
update_category: Toma las categorías del archivo subido.
sample:
juices: Jugos
nuts: Nueces
Expand Down
1 change: 1 addition & 0 deletions config/locales/nl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,7 @@ nl:
options:
convert_units: Bestaande eenheden behouden, herbereken groothandelseenheid en prijs (net als synchronizeren).
outlist_absent: Artikelen die niet in het bestand voorkomen, verwijderen.
upload_category: Categorieën overnemen uit bestand.
sample:
juices: Sappen
nuts: Noten
Expand Down
25 changes: 25 additions & 0 deletions spec/integration/articles_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,31 @@
end
end

describe "takes over category from file" do
it do
find(:css, '#articles_update_category[value="1"]').set(true) # check take over category from file
expect(ArticleCategory.count).to eq 1 # new Category vegetables should be created from file
find('input[type="submit"]').click # upload file
find('input[type="submit"]').click # submit changes
expect(ArticleCategory.count).to eq 2 # it is
expect(supplier.articles.count).to eq 1
expect(supplier.articles.first.article_category.name).to eq "Vegetables"
end
end

describe "overwrites article_category from file" do
let!(:article_category) { create(:article_category, name: "Fruit") }
let(:article) { create(:article, supplier: supplier, name: 'Tomatoes', order_number: 1, unit: '250 g', article_category: article_category) }

it do
find(:css, '#articles_update_category[value="1"]').set(true) # check take over category from file
find('input[type="submit"]').click #upload file
find('input[type="submit"]').click #submit changes
expect(supplier.articles.count).to eq 1
expect(supplier.articles.first.article_category.name).to eq "Vegetables"
end
end

describe "can remove an existing article" do
let!(:article) { create :article, supplier: supplier, name: 'Foobar', order_number: 99999 }

Expand Down