141 lines
5.5 KiB
Ruby
141 lines
5.5 KiB
Ruby
# Controller for all ordering-actions that are performed by a user who is member of an Ordergroup.
|
|
# Management actions that require the "orders" role are handled by the OrdersController.
|
|
class OrderingController < ApplicationController
|
|
# Security
|
|
before_filter :ensure_ordergroup_member
|
|
before_filter :ensure_open_order, :only => [:new, :create, :edit, :update, :order, :stock_order, :saveOrder]
|
|
|
|
# Index page.
|
|
def index
|
|
end
|
|
|
|
def new
|
|
@group_order = @order.group_orders.build(:ordergroup => @ordergroup, :updated_by => current_user)
|
|
@ordering_data = @group_order.load_data
|
|
end
|
|
|
|
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.'
|
|
end
|
|
end
|
|
|
|
def show
|
|
@group_order = GroupOrder.find(params[:id])
|
|
@order= @group_order.order
|
|
end
|
|
|
|
def edit
|
|
@group_order = GroupOrder.find(params[:id])
|
|
@ordering_data = @group_order.load_data
|
|
end
|
|
|
|
def update
|
|
@group_order = GroupOrder.find(params[:id])
|
|
@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.'
|
|
end
|
|
end
|
|
|
|
def stock_order
|
|
# Load order article data...
|
|
@articles_grouped_by_category = @order.articles_grouped_by_category
|
|
# save results of earlier orders in array
|
|
ordered_articles = Array.new
|
|
@group_order = @order.group_orders.find(:first,
|
|
:conditions => "ordergroup_id = #{@ordergroup.id}", :include => :group_order_articles)
|
|
|
|
if @group_order
|
|
# Group has already ordered, so get the results...
|
|
for goa in @group_order.group_order_articles
|
|
ordered_articles[goa.order_article_id] = {:quantity => goa.quantity,
|
|
:tolerance => goa.tolerance,
|
|
:quantity_result => goa.result(:quantity),
|
|
:tolerance_result => goa.result(:tolerance)}
|
|
end
|
|
@version = @group_order.lock_version
|
|
@availableFunds = @ordergroup.get_available_funds(@group_order)
|
|
else
|
|
@version = 0
|
|
@availableFunds = @ordergroup.get_available_funds
|
|
end
|
|
|
|
# load prices ....
|
|
@price = Array.new; @quantity_available = Array.new
|
|
@others_quantity = Array.new; @quantity = Array.new; @quantity_result = Array.new; @used_quantity = Array.new; @unused_quantity = Array.new
|
|
i = 0;
|
|
@articles_grouped_by_category.each do |category_name, order_articles|
|
|
for order_article in order_articles
|
|
# price/unit size
|
|
@price[i] = order_article.article.fc_price
|
|
@quantity_available[i] = order_article.article.quantity_available(@order)
|
|
# quantity
|
|
@quantity[i] = (ordered_articles[order_article.id] ? ordered_articles[order_article.id][:quantity] : 0)
|
|
@others_quantity[i] = order_article.quantity - @quantity[i]
|
|
@used_quantity[i] = (ordered_articles[order_article.id] ? ordered_articles[order_article.id][:quantity_result] : 0)
|
|
i += 1
|
|
end
|
|
end
|
|
end
|
|
|
|
# Shows all Orders of the Ordergroup
|
|
# if selected, it shows all orders of the foodcoop
|
|
def archive
|
|
# get only orders belonging to the ordergroup
|
|
@closed_orders = Order.paginate :page => params[:page], :per_page => 10,
|
|
:conditions => { :state => 'closed' }, :order => "orders.ends DESC"
|
|
|
|
respond_to do |format|
|
|
format.html # myOrders.haml
|
|
format.js { render :partial => "orders", :locals => {:orders => @closed_orders, :pagination => true} }
|
|
end
|
|
end
|
|
|
|
# adds a Comment to the Order
|
|
def add_comment
|
|
order = Order.find(params[:id])
|
|
comment = order.comments.build(params[:comment])
|
|
comment.user = @current_user
|
|
if !comment.text.blank? and comment.save
|
|
flash[:notice] = "Kommentar wurde erstellt."
|
|
else
|
|
flash[:error] = "Kommentar konnte nicht erstellt werden. Leerer Kommentar?"
|
|
end
|
|
redirect_to :action => 'show', :id => order
|
|
end
|
|
|
|
private
|
|
|
|
# Returns true if @current_user is member of an Ordergroup.
|
|
# Used as a :before_filter by OrderingController.
|
|
def ensure_ordergroup_member
|
|
@ordergroup = @current_user.ordergroup
|
|
if @ordergroup.nil?
|
|
redirect_to root_url, :alert => "Du bist kein Mitglieder einer Bestellgruppe."
|
|
end
|
|
end
|
|
|
|
def ensure_open_order
|
|
@order = Order.find((params[:order_id] || params[:group_order][:order_id]),
|
|
:include => [:supplier, :order_articles])
|
|
unless @order.open?
|
|
flash[:notice] = 'Diese Bestellung ist bereits abgeschlossen.'
|
|
redirect_to :action => 'index'
|
|
end
|
|
end
|
|
|
|
end
|