Do not set quantity of newly created OrderArticles to 1

This commit is contained in:
Julius 2014-01-01 23:45:57 +01:00 committed by wvengen
parent a384532619
commit 8e52fca304
6 changed files with 44 additions and 9 deletions

View file

@ -6,7 +6,7 @@ class OrderArticlesController < ApplicationController
def new def new
@order = Order.find(params[:order_id]) @order = Order.find(params[:order_id])
@order_article = @order.order_articles.build @order_article = @order.order_articles.build(params[:order_article])
end end
def create def create
@ -15,9 +15,7 @@ class OrderArticlesController < ApplicationController
# If order_article is ordered and a new order_article is created, an error message will be # If order_article is ordered and a new order_article is created, an error message will be
# given mentioning that the article already exists, which is desired. # given mentioning that the article already exists, which is desired.
@order_article = @order.order_articles.where(:article_id => params[:order_article][:article_id]).first @order_article = @order.order_articles.where(:article_id => params[:order_article][:article_id]).first
if @order_article and @order_article.units_to_order == 0 unless (@order_article and @order_article.units_to_order == 0)
@order_article.units_to_order = 1 # FIXME: this is ugly if used in the receive form
else
@order_article = @order.order_articles.build(params[:order_article]) @order_article = @order.order_articles.build(params[:order_article])
end end
@order_article.save! @order_article.save!

View file

@ -16,7 +16,7 @@ module DeliveriesHelper
block_given? or block = Proc.new {|a| "#{a.name} (#{number_to_currency a.price}/#{a.unit})" } block_given? or block = Proc.new {|a| "#{a.name} (#{number_to_currency a.price}/#{a.unit})" }
articles.map do |a| articles.map do |a|
{:id => a.id, :text => block.call(a)} {:id => a.id, :text => block.call(a)}
end end.unshift({:id => '', :text => ''})
end end
def articles_for_table(articles) def articles_for_table(articles)

View file

@ -208,7 +208,6 @@ class OrderArticle < ActiveRecord::Base
def init_from_balancing def init_from_balancing
if order.present? and order.finished? if order.present? and order.finished?
self.article_price = article.article_prices.first self.article_price = article.article_prices.first
self.units_to_order = 1
end end
end end

View file

@ -1,9 +1,15 @@
// Handle more advanced DOM update after AJAX database manipulation. // Handle more advanced DOM update after AJAX database manipulation.
// See publish/subscribe design pattern in /doc. // See publish/subscribe design pattern in /doc.
(function(w) { (function(w) {
$('#result_table').prepend( $('#order_article_<%= @order_article.id %>').remove(); // just to be sure: remove table row which is added below
$('#ordered-articles tr').removeClass('success');
var order_article_entry = $(
'<%= j render('finance/balancing/order_article_result', order_article: @order_article) %>' '<%= j render('finance/balancing/order_article_result', order_article: @order_article) %>'
); ).addClass('success');
$('#result_table').prepend(order_article_entry);
$('#summaryChangedWarning').show(); $('#summaryChangedWarning').show();
})(window); })(window);

View file

@ -1,3 +1,5 @@
- new_articles = (@order.supplier.articles rescue @order.articles)
- new_article_data = articles_for_select2(new_articles, @order_articles.map(&:article_id)) {|a| "#{a.name} (#{a.unit_quantity}#{a.unit})"}
- content_for :javascript do - content_for :javascript do
:javascript :javascript
@ -31,8 +33,32 @@
$('input[data-units-expected]').each(function() { $('input[data-units-expected]').each(function() {
update_delta(this); update_delta(this);
}); });
init_add_article('#add_article');
}); });
function init_add_article(sel) {
$(sel).removeAttr('disabled').select2({
placeholder: '#{j t('orders.add_article.title')}',
formatNoMatches: function(term) { return '#{j t('.no_articles_available')}';}
// TODO implement adding a new article, like in deliveries
}).on('change', function(e) {
var selectedArticle = $(e.currentTarget).select2('data');
if(!selectedArticle) {
return false;
}
$.ajax({
url: '#{order_order_articles_path(@order)}',
type: 'post',
data: JSON.stringify({order_article: {article_id: selectedArticle.id}}),
contentType: 'application/json; charset=UTF-8'
});
$('#add_article').select2('data', null);
}).select2('data', null);
}
%table#order_articles.ordered-articles.table.table-striped.stupidtable %table#order_articles.ordered-articles.table.table-striped.stupidtable
%thead %thead
%tr %tr
@ -50,7 +76,9 @@
%tfoot %tfoot
%tr %tr
%th{:colspan => 10} %th{:colspan => 10}
= link_to t('.add_article'), new_order_order_article_path(@order), remote: true, class: 'btn btn-small' %select#add_article{:style => 'width: 500px;'}
- new_article_data.each do |option|
%option{id: "add_article_#{option[:id]}", value: option[:id]}= option[:text]
%tbody#result_table %tbody#result_table
- @order_articles.each do |order_article| - @order_articles.each do |order_article|
= render :partial => 'edit_amount', :locals => {:order_article => order_article} = render :partial => 'edit_amount', :locals => {:order_article => order_article}

View file

@ -1,6 +1,8 @@
// Handle more advanced DOM update after AJAX database manipulation. // Handle more advanced DOM update after AJAX database manipulation.
// See publish/subscribe design pattern in /doc. // See publish/subscribe design pattern in /doc.
(function(w) { (function(w) {
$('#order_article_<%= @order_article.id %>').remove(); // just to be sure: remove table row which is added below
$('#order_articles tr').removeClass('success'); $('#order_articles tr').removeClass('success');
var order_article_entry = $( var order_article_entry = $(
@ -9,5 +11,7 @@
$('#order_articles tbody').append(order_article_entry); $('#order_articles tbody').append(order_article_entry);
updateSort('#order_articles'); updateSort('#order_articles');
$('#add_article_<%= @order_article.article.id %>').remove(); // remove option to add this article
})(window); })(window);