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
|
2009-01-29 01:57:51 +01:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
before_filter :authenticate_orders
|
|
|
|
|
|
|
|
# List orders
|
|
|
|
def index
|
2009-01-29 01:57:51 +01:00
|
|
|
@open_orders = Order.open
|
2009-01-06 11:49:19 +01:00
|
|
|
@per_page = 15
|
|
|
|
if params['sort']
|
|
|
|
sort = case params['sort']
|
|
|
|
when "supplier" then "suppliers.name, ends DESC"
|
|
|
|
when "ends" then "ends DESC"
|
2009-01-29 01:57:51 +01:00
|
|
|
when "supplier_reverse" then "suppliers.name DESC"
|
2009-01-06 11:49:19 +01:00
|
|
|
when "ends_reverse" then "ends"
|
|
|
|
end
|
|
|
|
else
|
|
|
|
sort = "ends DESC"
|
|
|
|
end
|
2012-10-15 21:19:17 +02:00
|
|
|
@orders = Order.page(params[:page]).per(@per_page).order(sort).where("state != 'open'").includes(:supplier)
|
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])
|
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
|
|
|
|
@partial = case params[:view]
|
2012-10-15 21:19:17 +02:00
|
|
|
when 'default' then "articles"
|
2012-10-02 02:50:48 +02:00
|
|
|
when 'groups'then 'shared/articles_by_groups'
|
|
|
|
when 'articles'then 'shared/articles_by_articles'
|
|
|
|
else 'articles'
|
|
|
|
end
|
|
|
|
render :layout => false
|
|
|
|
end
|
|
|
|
format.pdf do
|
|
|
|
pdf = case params[:document]
|
|
|
|
when 'groups' then OrderByGroups.new(@order)
|
|
|
|
when 'articles' then OrderByArticles.new(@order)
|
|
|
|
when 'fax' then OrderFax.new(@order)
|
|
|
|
when 'matrix' then OrderMatrix.new(@order)
|
|
|
|
end
|
|
|
|
send_data pdf.to_pdf, filename: pdf.filename, type: 'application/pdf'
|
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Page to create a new order.
|
|
|
|
def new
|
2009-02-05 16:40:02 +01:00
|
|
|
@order = Order.new :ends => 4.days.from_now, :supplier_id => params[:supplier_id]
|
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
|
|
|
|
@order = Order.find(params[:id], :include => :articles)
|
|
|
|
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
|
|
|
|
|
|
|
|
# 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')
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# Renders the fax-text-file
|
|
|
|
# e.g. for easier use with online-fax-software, which don't accept pdf-files
|
|
|
|
def text_fax_template
|
|
|
|
order = Order.find(params[:id])
|
|
|
|
supplier = order.supplier
|
2012-08-24 19:52:38 +02:00
|
|
|
contact = FoodsoftConfig[:contact].symbolize_keys
|
2013-02-08 01:52:20 +01:00
|
|
|
text = I18n.t('orders.fax.heading', :name => FoodsoftConfig[:name])
|
2013-10-09 22:29:19 +02:00
|
|
|
text += "\n#{Supplier.human_attribute_name(:customer_number)}: #{supplier.customer_number}" unless supplier.customer_number.blank?
|
2013-02-08 01:52:20 +01:00
|
|
|
text += "\n" + I18n.t('orders.fax.delivery_day')
|
2013-10-09 22:29:19 +02:00
|
|
|
text += "\n\n#{supplier.name}\n#{supplier.address}\n#{Supplier.human_attribute_name(:fax)}: #{supplier.fax}\n\n"
|
2013-02-08 01:52:20 +01:00
|
|
|
text += "****** " + I18n.t('orders.fax.to_address') + "\n\n"
|
2012-08-24 19:52:38 +02:00
|
|
|
text += "#{FoodsoftConfig[:name]}\n#{contact[:street]}\n#{contact[:zip_code]} #{contact[:city]}\n\n"
|
2013-02-08 01:52:20 +01:00
|
|
|
text += "****** " + I18n.t('orders.fax.articles') + "\n\n"
|
2013-04-12 01:26:26 +02:00
|
|
|
text += I18n.t('orders.fax.number') + " " + I18n.t('orders.fax.amount') + " " + I18n.t('orders.fax.name') + "\n"
|
2009-01-06 11:49:19 +01:00
|
|
|
# now display all ordered articles
|
2009-02-03 21:14:48 +01:00
|
|
|
order.order_articles.ordered.all(:include => [:article, :article_price]).each do |oa|
|
2009-01-29 01:57:51 +01:00
|
|
|
number = oa.article.order_number
|
|
|
|
(8 - number.size).times { number += " " }
|
|
|
|
quantity = oa.units_to_order.to_i.to_s
|
2009-01-06 11:49:19 +01:00
|
|
|
quantity = " " + quantity if quantity.size < 2
|
2009-01-29 01:57:51 +01:00
|
|
|
text += "#{number} #{quantity} #{oa.article.name}\n"
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
send_data text,
|
|
|
|
:type => 'text/plain; charset=utf-8; header=present',
|
2009-02-05 16:40:02 +01:00
|
|
|
:disposition => "attachment; filename=#{order.name}"
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2013-02-08 01:52:20 +01:00
|
|
|
end
|