diff --git a/app/controllers/orders_controller.rb b/app/controllers/orders_controller.rb index 4aab2413..23788f86 100644 --- a/app/controllers/orders_controller.rb +++ b/app/controllers/orders_controller.rb @@ -171,11 +171,13 @@ class OrdersController < ApplicationController # update attributes; don't use update_attribute because it calls save # which makes received_changed? not work anymore oa.attributes = oa_params - counts[0] += 1 if oa.units_received_changed? - unless oa.units_received.blank? - cunits[0] += oa.units_received * oa.article.unit_quantity - oacounts = oa.redistribute oa.units_received * oa.price.unit_quantity, rest_to - oacounts.each_with_index {|c,i| cunits[i+1]+=c; counts[i+1]+=1 if c>0 } + if oa.units_received_changed? + counts[0] += 1 + unless oa.units_received.blank? + cunits[0] += oa.units_received * oa.article.unit_quantity + oacounts = oa.redistribute oa.units_received * oa.price.unit_quantity, rest_to + oacounts.each_with_index {|c,i| cunits[i+1]+=c; counts[i+1]+=1 if c>0 } + end end oa.save! end diff --git a/app/helpers/orders_helper.rb b/app/helpers/orders_helper.rb index db408790..2d2a78fa 100644 --- a/app/helpers/orders_helper.rb +++ b/app/helpers/orders_helper.rb @@ -34,4 +34,14 @@ module OrdersHelper " × #{article.unit_quantity}".html_safe end end + + def receive_input_field(form) + order_article = form.object + units_expected = (order_article.units_billed or order_article.units_to_order) + form.text_field :units_received, class: 'input-nano package units_received', + data: {'units-expected' => units_expected}, + readonly: order_article.result_manually_changed? ? "readonly" : nil, + title: order_article.result_manually_changed? ? t('.locked_to_protect_manual_update') : nil, + autocomplete: 'off' + end end diff --git a/app/models/group_order_article.rb b/app/models/group_order_article.rb index 4d7bff09..83693e10 100644 --- a/app/models/group_order_article.rb +++ b/app/models/group_order_article.rb @@ -165,9 +165,11 @@ class GroupOrderArticle < ActiveRecord::Base self[:result] || calculate_result[type] end - # This is used during order.finish!. + # This is used for automatic distribution, e.g., in order.finish! or when receiving orders def save_results!(article_total = nil) - self.update_attribute(:result, calculate_result(article_total)[:total]) + new_result = calculate_result(article_total)[:total] + self.update_attribute(:result_computed, new_result) + self.update_attribute(:result, new_result) end # Returns total price for this individual article @@ -186,6 +188,10 @@ class GroupOrderArticle < ActiveRecord::Base end end + # Check if the result deviates from the result_computed + def result_manually_changed? + result != result_computed + end end diff --git a/app/models/order_article.rb b/app/models/order_article.rb index 79b18d99..440ab896 100644 --- a/app/models/order_article.rb +++ b/app/models/order_article.rb @@ -195,6 +195,11 @@ class OrderArticle < ActiveRecord::Base units = 0 if units < 0 units end + + # Check if the result of any associated GroupOrderArticle was overridden manually + def result_manually_changed? + group_order_articles.any? {|goa| goa.result_manually_changed?} + end private diff --git a/app/views/orders/_edit_amount.html.haml b/app/views/orders/_edit_amount.html.haml index d81c537b..6aee7ed4 100644 --- a/app/views/orders/_edit_amount.html.haml +++ b/app/views/orders/_edit_amount.html.haml @@ -4,7 +4,6 @@ - order_title = [] - order_title.append Article.human_attribute_name(:manufacturer)+': ' + order_article.article.manufacturer unless order_article.article.manufacturer.to_s.empty? - order_title.append Article.human_attribute_name(:note)+': ' + order_article.article.note unless order_article.article.note.to_s.empty? - - units_expected = (order_article.units_billed or order_article.units_to_order) %td= order_article.article.order_number %td.name{title: order_title.join("\n")}= order_article.article.name %td.unit= order_article.article.unit @@ -18,8 +17,10 @@ = order_article.units_billed = pkg_helper order_article.article %td - = form.text_field :units_received, class: 'input-nano package', data: {'units-expected' => units_expected} + = receive_input_field(form) = pkg_helper order_article.article_price, false + - if order_article.result_manually_changed? + %input{type: :button, value: t('.override', default: 'Override'), class: 'btn btn-small unlocker'} / TODO add almost invisible text_field for entering single units %td.units_delta %td diff --git a/app/views/orders/_edit_amounts.html.haml b/app/views/orders/_edit_amounts.html.haml index 84f5b644..9c1adb5d 100644 --- a/app/views/orders/_edit_amounts.html.haml +++ b/app/views/orders/_edit_amounts.html.haml @@ -35,6 +35,8 @@ }); init_add_article('#add_article'); + + $('.unlocker', '#order_articles tbody').on('click', unlock_receive_input_field); }); function init_add_article(sel) { @@ -58,6 +60,12 @@ $('#add_article').select2('data', null); }).select2('data', null); } + + function unlock_receive_input_field() { + var order_article_entry = $(this).closest('tr'); + $('.units_received', order_article_entry).removeAttr('readonly'); + $(this).remove(); + } %table#order_articles.ordered-articles.table.table-striped.stupidtable %thead diff --git a/config/locales/en.yml b/config/locales/en.yml index dd5ac164..8919ab1d 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1102,6 +1102,8 @@ en: notice: The order was created. edit: title: Edit order + edit_amount: + locked_to_protect_manual_update: The distribution of this article among the ordergroups was changed manually. This field is locked in order to protect those changes. fax: amount: Amount articles: Articles