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 # update attributes; don't use update_attribute because it calls save
# which makes received_changed? not work anymore # which makes received_changed? not work anymore
oa.attributes = oa_params oa.attributes = oa_params
counts[0] += 1 if oa.units_received_changed? if oa.units_received_changed?
unless oa.units_received.blank? counts[0] += 1
cunits[0] += oa.units_received * oa.article.unit_quantity unless oa.units_received.blank?
oacounts = oa.redistribute oa.units_received * oa.price.unit_quantity, rest_to cunits[0] += oa.units_received * oa.article.unit_quantity
oacounts.each_with_index {|c,i| cunits[i+1]+=c; counts[i+1]+=1 if c>0 } 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 end
oa.save! oa.save!
end end

View file

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

View file

@ -165,9 +165,11 @@ class GroupOrderArticle < ActiveRecord::Base
self[:result] || calculate_result[type] self[:result] || calculate_result[type]
end 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) 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 end
# Returns total price for this individual article # Returns total price for this individual article
@ -186,6 +188,10 @@ class GroupOrderArticle < ActiveRecord::Base
end end
end end
# Check if the result deviates from the result_computed
def result_manually_changed?
result != result_computed
end
end end

View file

@ -196,6 +196,11 @@ class OrderArticle < ActiveRecord::Base
units units
end 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 private
def article_and_price_exist def article_and_price_exist

View file

@ -4,7 +4,6 @@
- order_title = [] - 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(: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? - 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= order_article.article.order_number
%td.name{title: order_title.join("\n")}= order_article.article.name %td.name{title: order_title.join("\n")}= order_article.article.name
%td.unit= order_article.article.unit %td.unit= order_article.article.unit
@ -18,8 +17,10 @@
= order_article.units_billed = order_article.units_billed
= pkg_helper order_article.article = pkg_helper order_article.article
%td %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 = 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 / TODO add almost invisible text_field for entering single units
%td.units_delta %td.units_delta
%td %td

View file

@ -35,6 +35,8 @@
}); });
init_add_article('#add_article'); init_add_article('#add_article');
$('.unlocker', '#order_articles tbody').on('click', unlock_receive_input_field);
}); });
function init_add_article(sel) { function init_add_article(sel) {
@ -59,6 +61,12 @@
}).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 %table#order_articles.ordered-articles.table.table-striped.stupidtable
%thead %thead
%tr %tr

View file

@ -1102,6 +1102,8 @@ en:
notice: The order was created. notice: The order was created.
edit: edit:
title: Edit order 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: fax:
amount: Amount amount: Amount
articles: Articles articles: Articles