make product redistribution work!

This commit is contained in:
wvengen 2013-11-26 23:48:21 +01:00
parent 2d99141229
commit c900cf4988
5 changed files with 103 additions and 18 deletions

View file

@ -6,19 +6,38 @@ class Finance::ReceiveController < Finance::BaseController
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
oa.update_attributes!(oa_params)
# and process consequences
oa.redistribute(oa.units_received * oa.price.unit_quantity) unless oa.units_received.blank?
# 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')
#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