2009-01-14 12:46:01 +01:00
|
|
|
# Controller for all ordering-actions that are performed by a user who is member of an Ordergroup.
|
2009-01-06 11:49:19 +01:00
|
|
|
# Management actions that require the "orders" role are handled by the OrdersController.
|
2012-08-24 11:11:40 +02:00
|
|
|
class GroupOrdersController < ApplicationController
|
2009-01-06 11:49:19 +01:00
|
|
|
# Security
|
2009-01-20 19:37:15 +01:00
|
|
|
before_filter :ensure_ordergroup_member
|
2011-06-19 15:30:33 +02:00
|
|
|
before_filter :ensure_open_order, :only => [:new, :create, :edit, :update, :order, :stock_order, :saveOrder]
|
2012-08-24 11:11:40 +02:00
|
|
|
before_filter :ensure_my_group_order, only: [:show, :edit, :update]
|
2012-09-29 17:52:25 +02:00
|
|
|
before_filter :enough_apples?, only: [:new, :create]
|
2011-06-13 16:45:16 +02:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
# Index page.
|
2009-02-06 16:26:35 +01:00
|
|
|
def index
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2011-06-13 16:45:16 +02:00
|
|
|
|
2011-06-19 15:30:33 +02:00
|
|
|
def new
|
|
|
|
@group_order = @order.group_orders.build(:ordergroup => @ordergroup, :updated_by => current_user)
|
|
|
|
@ordering_data = @group_order.load_data
|
|
|
|
end
|
2009-02-04 16:41:01 +01:00
|
|
|
|
2011-06-19 15:30:33 +02:00
|
|
|
def create
|
|
|
|
@group_order = GroupOrder.new(params[:group_order])
|
|
|
|
begin
|
|
|
|
@group_order.save_ordering!
|
|
|
|
redirect_to group_order_url(@group_order), :notice => 'Die Bestellung wurde gespeichert.'
|
|
|
|
rescue ActiveRecord::StaleObjectError
|
|
|
|
redirect_to group_orders_url, :alert => 'In der Zwischenzeit hat jemand anderes auch bestellt, daher konnte die Bestellung nicht aktualisiert werden.'
|
|
|
|
rescue => exception
|
|
|
|
logger.error('Failed to update order: ' + exception.message)
|
|
|
|
redirect_to group_orders_url, :alert => 'Die Bestellung konnte nicht aktualisiert werden, da ein Fehler auftrat.'
|
2009-01-20 19:37:15 +01:00
|
|
|
end
|
2011-06-19 15:30:33 +02:00
|
|
|
end
|
2009-01-20 19:37:15 +01:00
|
|
|
|
2011-06-19 15:30:33 +02:00
|
|
|
def show
|
|
|
|
@order= @group_order.order
|
|
|
|
end
|
2011-06-13 16:45:16 +02:00
|
|
|
|
2011-06-19 15:30:33 +02:00
|
|
|
def edit
|
|
|
|
@ordering_data = @group_order.load_data
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
@group_order.attributes = params[:group_order]
|
|
|
|
begin
|
|
|
|
@group_order.save_ordering!
|
|
|
|
redirect_to group_order_url(@group_order), :notice => 'Die Bestellung wurde gespeichert.'
|
|
|
|
rescue ActiveRecord::StaleObjectError
|
|
|
|
redirect_to group_orders_url, :alert => 'In der Zwischenzeit hat jemand anderes auch bestellt, daher konnte die Bestellung nicht aktualisiert werden.'
|
|
|
|
rescue => exception
|
|
|
|
logger.error('Failed to update order: ' + exception.message)
|
|
|
|
redirect_to group_orders_url, :alert => 'Die Bestellung konnte nicht aktualisiert werden, da ein Fehler auftrat.'
|
2011-06-13 16:45:16 +02:00
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2011-06-19 19:56:04 +02:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
# Shows all Orders of the Ordergroup
|
|
|
|
# if selected, it shows all orders of the foodcoop
|
2011-06-19 15:30:33 +02:00
|
|
|
def archive
|
2009-01-29 01:57:51 +01:00
|
|
|
# get only orders belonging to the ordergroup
|
|
|
|
@closed_orders = Order.paginate :page => params[:page], :per_page => 10,
|
2011-06-13 16:45:16 +02:00
|
|
|
:conditions => { :state => 'closed' }, :order => "orders.ends DESC"
|
2009-01-29 01:57:51 +01:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
respond_to do |format|
|
2012-10-03 20:08:23 +02:00
|
|
|
format.html # archive.html.haml
|
|
|
|
format.js # archive.js.erb
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
end
|
2011-06-13 16:45:16 +02:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
private
|
2011-06-13 16:45:16 +02:00
|
|
|
|
2009-01-29 01:57:51 +01:00
|
|
|
# Returns true if @current_user is member of an Ordergroup.
|
|
|
|
# Used as a :before_filter by OrderingController.
|
|
|
|
def ensure_ordergroup_member
|
2009-02-01 20:56:23 +01:00
|
|
|
@ordergroup = @current_user.ordergroup
|
2009-01-29 01:57:51 +01:00
|
|
|
if @ordergroup.nil?
|
2011-06-10 13:58:50 +02:00
|
|
|
redirect_to root_url, :alert => "Du bist kein Mitglieder einer Bestellgruppe."
|
2009-01-20 19:37:15 +01:00
|
|
|
end
|
2009-01-29 01:57:51 +01:00
|
|
|
end
|
2009-01-20 19:37:15 +01:00
|
|
|
|
2009-01-29 01:57:51 +01:00
|
|
|
def ensure_open_order
|
2011-06-19 15:30:33 +02:00
|
|
|
@order = Order.find((params[:order_id] || params[:group_order][:order_id]),
|
|
|
|
:include => [:supplier, :order_articles])
|
2009-01-29 01:57:51 +01:00
|
|
|
unless @order.open?
|
|
|
|
flash[:notice] = 'Diese Bestellung ist bereits abgeschlossen.'
|
|
|
|
redirect_to :action => 'index'
|
2009-01-20 19:37:15 +01:00
|
|
|
end
|
2009-01-29 01:57:51 +01:00
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
|
2012-08-24 11:11:40 +02:00
|
|
|
def ensure_my_group_order
|
|
|
|
@group_order = @ordergroup.group_orders.find(params[:id])
|
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
redirect_to group_orders_url, alert: 'Fehlerhafte URL, das ist nicht Deine Bestellung.'
|
|
|
|
end
|
|
|
|
|
2012-09-29 17:52:25 +02:00
|
|
|
def enough_apples?
|
|
|
|
if @ordergroup.not_enough_apples?
|
|
|
|
redirect_to group_orders_url,
|
|
|
|
alert: t('not_enough_apples', scope: 'group_orders.messages', apples: @ordergroup.apples,
|
|
|
|
stop_ordering_under: FoodsoftConfig[:stop_ordering_under])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|