Readonly receive input if GroupOrderArticle result has manually been changed

Conflicts:
	app/helpers/orders_helper.rb
	app/views/orders/_edit_amount.html.haml
This commit is contained in:
wvengen 2014-01-03 10:33:09 +01:00
parent 7aae7f4d55
commit 98f59a3de3
7 changed files with 43 additions and 9 deletions

View File

@ -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

View File

@ -34,4 +34,14 @@ module OrdersHelper
"<span class='package'> &times; #{article.unit_quantity}</span>".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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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