Allow synchronising units when uploading
This commit is contained in:
parent
4d1e102f47
commit
c30ec67548
6 changed files with 37 additions and 11 deletions
|
@ -138,8 +138,9 @@ class ArticlesController < ApplicationController
|
||||||
# Update articles from a spreadsheet
|
# Update articles from a spreadsheet
|
||||||
def parse_upload
|
def parse_upload
|
||||||
uploaded_file = params[:articles]['file']
|
uploaded_file = params[:articles]['file']
|
||||||
outlist_absent = (params[:articles]['outlist_absent'] == '1')
|
options = {filename: uploaded_file.original_filename}
|
||||||
options = {filename: uploaded_file.original_filename, outlist_absent: outlist_absent}
|
options[:outlist_absent] = (params[:articles]['outlist_absent'] == '1')
|
||||||
|
options[:convert_units] = (params[:articles]['convert_units'] == '1')
|
||||||
@updated_article_pairs, @outlisted_articles, @new_articles = @supplier.sync_from_file uploaded_file.tempfile, options
|
@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?
|
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')
|
||||||
|
|
|
@ -116,11 +116,16 @@ class Article < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
# Return article attributes that were changed (incl. unit conversion)
|
# Return article attributes that were changed (incl. unit conversion)
|
||||||
# @param [Article] New article to update self
|
# @param new_article [Article] New article to update self
|
||||||
|
# @option options [Boolean] :convert_units Omit or set to +true+ to keep current unit and recompute unit quantity and price.
|
||||||
# @return [Hash<Symbol, Object>] Attributes with new values
|
# @return [Hash<Symbol, Object>] Attributes with new values
|
||||||
def unequal_attributes(new_article)
|
def unequal_attributes(new_article, options={})
|
||||||
# try to convert different units
|
# try to convert different units when desired
|
||||||
|
if options[:convert_units] == false
|
||||||
|
new_price, new_unit_quantity = nil, nil
|
||||||
|
else
|
||||||
new_price, new_unit_quantity = convert_units(new_article)
|
new_price, new_unit_quantity = convert_units(new_article)
|
||||||
|
end
|
||||||
if new_price && new_unit_quantity
|
if new_price && new_unit_quantity
|
||||||
new_unit = self.unit
|
new_unit = self.unit
|
||||||
else
|
else
|
||||||
|
|
|
@ -60,6 +60,7 @@ class Supplier < ActiveRecord::Base
|
||||||
# @param file [File] Spreadsheet file to parse
|
# @param file [File] Spreadsheet file to parse
|
||||||
# @param options [Hash] Options passed to {FoodsoftFile#parse} except when listed here.
|
# @param options [Hash] Options passed to {FoodsoftFile#parse} except when listed here.
|
||||||
# @option options [Boolean] :outlist_absent Set to +true+ to remove articles not in spreadsheet.
|
# @option options [Boolean] :outlist_absent Set to +true+ to remove articles not in spreadsheet.
|
||||||
|
# @option options [Boolean] :convert_units Omit or set to +true+ to keep current units, recomputing unit quantity and price.
|
||||||
def sync_from_file(file, options={})
|
def sync_from_file(file, options={})
|
||||||
all_order_numbers = []
|
all_order_numbers = []
|
||||||
updated_article_pairs, outlisted_articles, new_articles = [], [], []
|
updated_article_pairs, outlisted_articles, new_articles = [], [], []
|
||||||
|
@ -73,7 +74,7 @@ class Supplier < ActiveRecord::Base
|
||||||
if article.nil?
|
if article.nil?
|
||||||
new_articles << new_article
|
new_articles << new_article
|
||||||
else
|
else
|
||||||
unequal_attributes = article.unequal_attributes(new_article)
|
unequal_attributes = article.unequal_attributes(new_article, options.slice(:convert_units))
|
||||||
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]
|
||||||
|
|
|
@ -78,7 +78,10 @@
|
||||||
.control-group
|
.control-group
|
||||||
%label(for="articles_outlist_absent")
|
%label(for="articles_outlist_absent")
|
||||||
= f.check_box "outlist_absent"
|
= f.check_box "outlist_absent"
|
||||||
= t '.outlist_absent'
|
= t '.options.outlist_absent'
|
||||||
|
%label(for="articles_convert_units")
|
||||||
|
= f.check_box "convert_units"
|
||||||
|
= t '.options.convert_units'
|
||||||
|
|
||||||
.form-actions
|
.form-actions
|
||||||
= submit_tag t('.submit'), class: 'btn btn-primary'
|
= submit_tag t('.submit'), class: 'btn btn-primary'
|
||||||
|
|
|
@ -438,6 +438,8 @@ en:
|
||||||
reserved: "(Reserved)"
|
reserved: "(Reserved)"
|
||||||
status: Status (x=skip)
|
status: Status (x=skip)
|
||||||
file_label: Please choose a compatible file
|
file_label: Please choose a compatible file
|
||||||
|
options:
|
||||||
|
convert_units: Keep current units, recompute unit quantity and price (like synchronize)
|
||||||
outlist_absent: Delete articles not in uploaded file
|
outlist_absent: Delete articles not in uploaded file
|
||||||
sample:
|
sample:
|
||||||
juices: Juices
|
juices: Juices
|
||||||
|
|
|
@ -63,12 +63,14 @@ describe ArticlesController, :type => :feature do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "can update existing article" do
|
describe "can update existing article" do
|
||||||
let!(:article) { create :article, supplier: supplier, name: 'Foobar', order_number: 1 }
|
let!(:article) { create :article, supplier: supplier, name: 'Foobar', order_number: 1, unit: '250 g' }
|
||||||
it do
|
it do
|
||||||
find('input[type="submit"]').click
|
find('input[type="submit"]').click
|
||||||
expect(find("#articles_#{article.id}_name").value).to eq 'Tomatoes'
|
expect(find("#articles_#{article.id}_name").value).to eq 'Tomatoes'
|
||||||
find('input[type="submit"]').click
|
find('input[type="submit"]').click
|
||||||
expect(article.reload.name).to eq 'Tomatoes'
|
article.reload
|
||||||
|
expect(article.name).to eq 'Tomatoes'
|
||||||
|
expect([article.unit, article.unit_quantity, article.price]).to eq ['500 g', 20, 1.2]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -97,5 +99,17 @@ describe ArticlesController, :type => :feature do
|
||||||
expect(article.reload.deleted?).to be true
|
expect(article.reload.deleted?).to be true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "can convert units when updating" do
|
||||||
|
let!(:article) { create :article, supplier: supplier, order_number: 1, unit: '250 g' }
|
||||||
|
it do
|
||||||
|
check('articles_convert_units')
|
||||||
|
find('input[type="submit"]').click
|
||||||
|
expect(find("#articles_#{article.id}_name").value).to eq 'Tomatoes'
|
||||||
|
find('input[type="submit"]').click
|
||||||
|
article.reload
|
||||||
|
expect([article.unit, article.unit_quantity, article.price]).to eq ['250 g', 40, 0.6]
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue