only allow to add articles not already present in receive

This commit is contained in:
wvengen 2013-11-26 13:31:07 +01:00
parent beabe22a01
commit 2d99141229
2 changed files with 21 additions and 6 deletions

View file

@ -1,8 +1,11 @@
# :encoding:utf-8:
module Finance::ReceiveHelper
# TODO currently duplicate a bit of DeliveriesHelper.articles_for_select2
def articles_for_select2(supplier)
supplier.articles.undeleted.reorder('articles.name ASC').map do |a|
# except is an array of article id's to omit
def articles_for_select2(supplier, except = [])
articles = supplier.articles.reorder('articles.name ASC')
articles.reject! {|a| not except.index(a.id).nil? } if except
articles.map do |a|
{:id => a.id, :text => "#{a.name} (#{a.unit_quantity}#{a.unit})"}
end
end

View file

@ -1,6 +1,8 @@
- content_for :javascript do
:javascript
var new_article_data = #{articles_for_select2(@order, @order_articles.map(&:article_id)).to_json};
function update_delta(input) {
var units = $(input).val();
var expected = $(input).data('units-expected');
@ -32,9 +34,13 @@
update_delta(this);
});
$('#add_article').removeAttr('disabled').select2({
init_add_article('#add_article');
});
function init_add_article(sel) {
$(sel).removeAttr('disabled').select2({
placeholder: '#{t '.add_article'}',
data: #{articles_for_select2(@order).to_json},
data: new_article_data,
// TODO implement adding a new article, like in deliveries
}).on('change', function(e) {
var selectedArticle = $(e.currentTarget).select2('data');
@ -48,11 +54,17 @@
data: {article_id: selectedArticle.id},
contentType: 'application/json; charset=UTF-8'
});
// clear selection
$('#add_article').select2('data', null);
// remove newly added item from list of items
new_article_data = $.grep(new_article_data, function(el, i) {
return el.id != selectedArticle.id;
});
init_add_article(sel)
return true;
});
});
}
%table.ordered-articles.table.table-striped.stupidtable