make receive delta work when unit_quantity changes

This commit is contained in:
wvengen 2014-01-13 11:37:18 +01:00
parent 45e529b008
commit 3b2d50b47d
3 changed files with 27 additions and 10 deletions

View file

@ -58,9 +58,12 @@ module OrdersHelper
def receive_input_field(form) def receive_input_field(form)
order_article = form.object order_article = form.object
units_expected = (order_article.units_billed or order_article.units_to_order) units_expected = (order_article.units_billed or order_article.units_to_order) *
1.0 * order_article.article.unit_quantity / order_article.article_price.unit_quantity
input_html = form.text_field :units_received, class: 'input input-nano package units_received', input_classes = 'input input-nano units_received'
input_classes += ' package' unless order_article.article_price.unit_quantity == 1
input_html = form.text_field :units_received, class: input_classes,
data: {'units-expected' => units_expected}, data: {'units-expected' => units_expected},
disabled: order_article.result_manually_changed?, disabled: order_article.result_manually_changed?,
autocomplete: 'off' autocomplete: 'off'

View file

@ -6,6 +6,7 @@
function update_delta(input) { function update_delta(input) {
var units = $(input).val(); var units = $(input).val();
var expected = $(input).data('units-expected'); var expected = $(input).data('units-expected');
var delta = Math.round((units-expected)*100)/100.0;
var html; var html;
if (units.replace(/\s/g,"")=="") { if (units.replace(/\s/g,"")=="") {
@ -13,17 +14,20 @@
html = ''; html = '';
} else if (isNaN(units)) { } else if (isNaN(units)) {
html = '<i class="icon-remove" style="color: red"></i>'; html = '<i class="icon-remove" style="color: red"></i>';
} else if (units == expected) { } else if (delta == 0) {
// equal value // equal value
html = '<i class="icon-ok" style="color: green"></i>'; html = '<i class="icon-ok" style="color: green"></i>';
} else { } else {
if (units < expected) { if (delta < 0) {
html = '<span style="color: red">- '+(expected-units)+'</span>'; html = '<span style="color: red">- '+(-delta)+'</span>';
} else /*if (units> expected)*/ { } else /*if (units> expected)*/ {
html = '<span style="color: green">+ '+(units-expected)+'</span>'; html = '<span style="color: green">+ '+(delta)+'</span>';
} }
// show package icon only if the receive field has one
if ($(input).hasClass('package')) {
html += '#{j pkg_helper_icon}'; html += '#{j pkg_helper_icon}';
} }
}
$(input).closest('tr').find('.units_delta').html(html); $(input).closest('tr').find('.units_delta').html(html);
} }

View file

@ -4,9 +4,19 @@
// get old element and update the cell which is reused // get old element and update the cell which is reused
var old_order_article_entry = $('#order_article_<%= @order_article.id %>'); var old_order_article_entry = $('#order_article_<%= @order_article.id %>');
$('td.units_received_cell span.package', old_order_article_entry).replaceWith( // update package info after input
'<%= j pkg_helper(@order_article.article_price, icon: false) %>' $('td.units_received_cell span.package', old_order_article_entry).remove();
); $('<%= j pkg_helper(@order_article.article_price, icon: false) %>')
.appendTo($('td.units_received_cell', old_order_article_entry));
// update package icon on input too
$('input', old_order_article_entry).toggleClass('package', <%= @order_article.article_price.unit_quantity == 1 ? 'false' : 'true' %>);
// update expected units, since unit_quantity may have been changed
$('input', old_order_article_entry).data('units-expected', <%=
(@order_article.units_billed or @order_article.units_to_order) *
1.0 * @order_article.article.unit_quantity / @order_article.article_price.unit_quantity
%>);
// render new element and inject dynamic cell // render new element and inject dynamic cell
var new_order_article_entry = $( var new_order_article_entry = $(