move receive screen to orders

This commit is contained in:
wvengen 2013-12-18 21:06:05 +01:00
parent 94b4454a1b
commit d299fa4870
10 changed files with 75 additions and 79 deletions

View file

@ -1,56 +0,0 @@
class Finance::ReceiveController < Finance::BaseController
def edit
@order = Order.find(params[:id])
@order_articles = @order.order_articles.ordered.includes(:article)
end
def update
# where to leave remainder during redistribution
rest_to = []
rest_to << :tolerance if params[:rest_to_tolerance]
rest_to << :stock if params[:rest_to_stock]
rest_to << nil
# count what happens to the articles
counts = [0] * (rest_to.length+2)
cunits = [0] * (rest_to.length+2)
OrderArticle.transaction do
params[:order_articles].each do |oa_id, oa_params|
unless oa_params.blank?
oa = OrderArticle.find(oa_id)
# 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?
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
oacounts.each_with_index {|c,i| cunits[i+1]+=c; counts[i+1]+=1 if c>0 }
end
oa.save!
end
end
#flash[:notice] = I18n.t('finance.receive.update.notice')
notice = "Order received:"
notice += " #{counts.shift} articles (#{cunits.shift} units) updated"
notice += ", #{counts.shift} (#{cunits.shift}) using tolerance" if params[:rest_to_tolerance]
notice += ", #{counts.shift} (#{cunits.shift}) go to stock if foodsoft would support that" if params[:rest_to_stock]
notice += ", #{counts.shift} (#{cunits.shift}) left over"
flash[:notice] = notice
redirect_to finance_order_index_path
end
end
# ajax add article
def add_article
@order = Order.find(params[:receive_id])
@order_article = @order.order_articles.where(:article_id => params[:article_id]).includes(:article).first
# we need to create the order article if it's not part of the current order
if @order_article.nil?
@order_article = @order.order_articles.build({order: @order, article_id: params[:article_id]})
@order_article.save!
end
end
end

View file

@ -106,6 +106,27 @@ class OrdersController < ApplicationController
redirect_to orders_url, alert: I18n.t('errors.general_msg', :msg => error.message)
end
# ajax add article
def add_article
@order = Order.find(params[:id])
@order_article = @order.order_articles.where(:article_id => params[:article_id]).includes(:article).first
# we need to create the order article if it's not part of the current order
if @order_article.nil?
@order_article = @order.order_articles.build({order: @order, article_id: params[:article_id]})
@order_article.save!
end
end
def receive
@order = Order.find(params[:id])
unless request.post?
@order_articles = @order.order_articles.ordered.includes(:article)
else
flash[:notice] = "Order received: " + update_order_amounts
redirect_to @order
end
end
protected
# Renders the fax-text-file
@ -132,4 +153,37 @@ class OrdersController < ApplicationController
end
text
end
def update_order_amounts
# where to leave remainder during redistribution
rest_to = []
rest_to << :tolerance if params[:rest_to_tolerance]
rest_to << :stock if params[:rest_to_stock]
rest_to << nil
# count what happens to the articles
counts = [0] * (rest_to.length+2)
cunits = [0] * (rest_to.length+2)
OrderArticle.transaction do
params[:order_articles].each do |oa_id, oa_params|
unless oa_params.blank?
oa = OrderArticle.find(oa_id)
# 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?
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
oacounts.each_with_index {|c,i| cunits[i+1]+=c; counts[i+1]+=1 if c>0 }
end
oa.save!
end
end
end
notice = " #{counts.shift} articles (#{cunits.shift} units) updated"
notice += ", #{counts.shift} (#{cunits.shift}) using tolerance" if params[:rest_to_tolerance]
notice += ", #{counts.shift} (#{cunits.shift}) go to stock if foodsoft would support that" if params[:rest_to_stock]
notice += ", #{counts.shift} (#{cunits.shift}) left over"
end
end