2012-04-16 08:48:01 +02:00
|
|
|
# encoding: utf-8
|
|
|
|
#
|
2009-01-06 11:49:19 +01:00
|
|
|
# Controller for managing orders, i.e. all actions that require the "orders" role.
|
|
|
|
# Normal ordering actions of members of order groups is handled by the OrderingController.
|
|
|
|
class OrdersController < ApplicationController
|
2015-01-16 15:49:31 +01:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
before_filter :authenticate_orders
|
2014-05-06 11:40:57 +02:00
|
|
|
before_filter :remove_empty_article, only: [:create, :update]
|
2015-01-16 15:49:31 +01:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
# List orders
|
|
|
|
def index
|
2013-12-18 19:21:39 +01:00
|
|
|
@open_orders = Order.open.includes(:supplier)
|
2014-01-13 23:21:03 +01:00
|
|
|
@finished_orders = Order.finished_not_closed.includes(:supplier)
|
2009-01-06 11:49:19 +01:00
|
|
|
@per_page = 15
|
|
|
|
if params['sort']
|
|
|
|
sort = case params['sort']
|
2014-01-23 16:17:16 +01:00
|
|
|
when "supplier" then "suppliers.name, ends DESC"
|
|
|
|
when "ends" then "ends DESC"
|
|
|
|
when "supplier_reverse" then "suppliers.name DESC"
|
|
|
|
when "ends_reverse" then "ends"
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
else
|
|
|
|
sort = "ends DESC"
|
|
|
|
end
|
2014-03-08 17:15:09 +01:00
|
|
|
@orders = Order.closed.includes(:supplier).reorder(sort).page(params[:page]).per(@per_page)
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# Gives a view for the results to a specific order
|
2009-01-29 01:57:51 +01:00
|
|
|
# Renders also the pdf
|
2009-01-06 11:49:19 +01:00
|
|
|
def show
|
|
|
|
@order= Order.find(params[:id])
|
2015-01-14 22:56:32 +01:00
|
|
|
@view = (params[:view] || 'default').gsub(/[^-_a-zA-Z0-9]/, '')
|
2014-01-27 14:58:39 +01:00
|
|
|
@partial = case @view
|
2014-01-23 16:17:16 +01:00
|
|
|
when 'default' then 'articles'
|
|
|
|
when 'groups' then 'shared/articles_by/groups'
|
|
|
|
when 'articles' then 'shared/articles_by/articles'
|
2014-01-27 14:58:39 +01:00
|
|
|
else 'articles'
|
|
|
|
end
|
2009-01-29 01:57:51 +01:00
|
|
|
|
2012-10-02 02:50:48 +02:00
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
|
|
|
format.js do
|
|
|
|
render :layout => false
|
|
|
|
end
|
|
|
|
format.pdf do
|
|
|
|
pdf = case params[:document]
|
2014-01-23 16:17:16 +01:00
|
|
|
when 'groups' then OrderByGroups.new(@order)
|
2012-10-02 02:50:48 +02:00
|
|
|
when 'articles' then OrderByArticles.new(@order)
|
2014-01-23 16:17:16 +01:00
|
|
|
when 'fax' then OrderFax.new(@order)
|
|
|
|
when 'matrix' then OrderMatrix.new(@order)
|
2012-10-02 02:50:48 +02:00
|
|
|
end
|
|
|
|
send_data pdf.to_pdf, filename: pdf.filename, type: 'application/pdf'
|
|
|
|
end
|
2014-03-06 16:26:16 +01:00
|
|
|
format.csv do
|
|
|
|
send_data OrderCsv.new(@order).to_csv, filename: @order.name+'.csv', type: 'text/csv'
|
|
|
|
end
|
2013-10-30 01:56:23 +01:00
|
|
|
format.text do
|
2014-03-06 16:26:16 +01:00
|
|
|
send_data OrderTxt.new(@order).to_txt, filename: @order.name+'.txt', type: 'text/plain'
|
2013-10-30 01:56:23 +01:00
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Page to create a new order.
|
|
|
|
def new
|
2014-11-22 00:33:16 +01:00
|
|
|
@order = Order.new(supplier_id: params[:supplier_id]).init_dates
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# Save a new order.
|
|
|
|
# order_articles will be saved in Order.article_ids=()
|
|
|
|
def create
|
|
|
|
@order = Order.new(params[:order])
|
2013-01-26 16:24:45 +01:00
|
|
|
@order.created_by = current_user
|
2009-01-06 11:49:19 +01:00
|
|
|
if @order.save
|
2013-02-08 01:52:20 +01:00
|
|
|
flash[:notice] = I18n.t('orders.create.notice')
|
2009-01-29 01:57:51 +01:00
|
|
|
redirect_to @order
|
2009-01-06 11:49:19 +01:00
|
|
|
else
|
2012-10-30 00:20:47 +01:00
|
|
|
logger.debug "[debug] order errors: #{@order.errors.messages}"
|
2009-01-06 11:49:19 +01:00
|
|
|
render :action => 'new'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Page to edit an exsiting order.
|
|
|
|
# editing finished orders is done in FinanceController
|
|
|
|
def edit
|
2015-01-16 15:49:31 +01:00
|
|
|
@order = Order.includes(:articles).find(params[:id])
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# Update an existing order.
|
|
|
|
def update
|
|
|
|
@order = Order.find params[:id]
|
|
|
|
if @order.update_attributes params[:order]
|
2013-02-08 01:52:20 +01:00
|
|
|
flash[:notice] = I18n.t('orders.update.notice')
|
2009-01-06 11:49:19 +01:00
|
|
|
redirect_to :action => 'show', :id => @order
|
|
|
|
else
|
|
|
|
render :action => 'edit'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Delete an order.
|
|
|
|
def destroy
|
|
|
|
Order.find(params[:id]).destroy
|
|
|
|
redirect_to :action => 'index'
|
|
|
|
end
|
2015-01-16 15:49:31 +01:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
# Finish a current order.
|
|
|
|
def finish
|
|
|
|
order = Order.find(params[:id])
|
2009-01-29 01:57:51 +01:00
|
|
|
order.finish!(@current_user)
|
2013-04-04 00:56:45 +02:00
|
|
|
redirect_to order, notice: I18n.t('orders.finish.notice')
|
2013-10-08 19:58:11 +02:00
|
|
|
rescue => error
|
|
|
|
redirect_to orders_url, alert: I18n.t('errors.general_msg', :msg => error.message)
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2013-10-30 01:56:23 +01:00
|
|
|
|
2013-12-18 21:06:05 +01:00
|
|
|
def receive
|
|
|
|
@order = Order.find(params[:id])
|
|
|
|
unless request.post?
|
2014-09-11 13:47:03 +02:00
|
|
|
@order_articles = @order.order_articles.ordered_or_member.includes(:article).order('articles.order_number, articles.name')
|
2013-12-18 21:06:05 +01:00
|
|
|
else
|
2014-01-08 13:39:49 +01:00
|
|
|
s = update_order_amounts
|
|
|
|
flash[:notice] = (s ? I18n.t('orders.receive.notice', :msg => s) : I18n.t('orders.receive.notice_none'))
|
2013-12-18 21:06:05 +01:00
|
|
|
redirect_to @order
|
|
|
|
end
|
|
|
|
end
|
2015-01-16 15:49:31 +01:00
|
|
|
|
2013-12-31 13:46:25 +01:00
|
|
|
def receive_on_order_article_create # See publish/subscribe design pattern in /doc.
|
|
|
|
@order_article = OrderArticle.find(params[:order_article_id])
|
|
|
|
render :layout => false
|
|
|
|
end
|
2015-01-16 15:49:31 +01:00
|
|
|
|
2013-12-30 14:34:26 +01:00
|
|
|
def receive_on_order_article_update # See publish/subscribe design pattern in /doc.
|
|
|
|
@order_article = OrderArticle.find(params[:order_article_id])
|
|
|
|
render :layout => false
|
|
|
|
end
|
2013-12-18 21:06:05 +01:00
|
|
|
|
2013-10-30 01:56:23 +01:00
|
|
|
protected
|
2015-01-16 15:49:31 +01:00
|
|
|
|
2013-12-18 21:06:05 +01:00
|
|
|
def update_order_amounts
|
2014-01-08 13:39:49 +01:00
|
|
|
return if not params[:order_articles]
|
2013-12-18 21:06:05 +01:00
|
|
|
# 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
|
2014-01-13 11:48:43 +01:00
|
|
|
# count what happens to the articles:
|
|
|
|
# changed, rest_to_tolerance, rest_to_stock, left_over
|
|
|
|
counts = [0] * 4
|
|
|
|
cunits = [0] * 4
|
2014-08-22 09:35:05 +02:00
|
|
|
# This was once wrapped in a transaction, but caused
|
|
|
|
# "MySQL lock timeout exceeded" errors. It's ok to do
|
|
|
|
# this article-by-article anway.
|
|
|
|
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
|
|
|
|
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 }
|
2013-12-18 21:06:05 +01:00
|
|
|
end
|
|
|
|
end
|
2014-08-22 09:35:05 +02:00
|
|
|
oa.save!
|
2013-12-18 21:06:05 +01:00
|
|
|
end
|
|
|
|
end
|
2014-01-13 11:48:43 +01:00
|
|
|
return nil if counts[0] == 0
|
|
|
|
notice = []
|
|
|
|
notice << I18n.t('orders.update_order_amounts.msg1', count: counts[0], units: cunits[0])
|
|
|
|
notice << I18n.t('orders.update_order_amounts.msg2', count: counts[1], units: cunits[1]) if params[:rest_to_tolerance]
|
|
|
|
notice << I18n.t('orders.update_order_amounts.msg3', count: counts[2], units: cunits[2]) if params[:rest_to_stock]
|
2015-01-14 22:56:32 +01:00
|
|
|
if counts[3]>0 || cunits[3]>0
|
2014-01-13 11:48:43 +01:00
|
|
|
notice << I18n.t('orders.update_order_amounts.msg4', count: counts[3], units: cunits[3])
|
2014-01-08 13:39:49 +01:00
|
|
|
end
|
2014-01-13 11:48:43 +01:00
|
|
|
notice.join(', ')
|
2013-12-18 21:06:05 +01:00
|
|
|
end
|
|
|
|
|
2014-05-06 11:40:57 +02:00
|
|
|
def remove_empty_article
|
2015-01-14 22:56:32 +01:00
|
|
|
params[:order][:article_ids].reject!(&:blank?) if params[:order] && params[:order][:article_ids]
|
2014-05-06 11:40:57 +02:00
|
|
|
end
|
|
|
|
|
2013-02-08 01:52:20 +01:00
|
|
|
end
|