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.
|
|
|
|
class OrderingController < ApplicationController
|
|
|
|
# Security
|
2009-01-20 19:37:15 +01:00
|
|
|
before_filter :ensure_ordergroup_member
|
2009-02-06 16:26:35 +01:00
|
|
|
before_filter :ensure_open_order, :only => [:order, :stock_order, :saveOrder]
|
2009-01-06 11:49:19 +01:00
|
|
|
|
2009-02-03 21:14:48 +01:00
|
|
|
verify :method => :post, :only => [:saveOrder], :redirect_to => {:action => :index}
|
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
|
|
|
|
|
|
|
|
# Edit a current order.
|
2009-02-06 16:26:35 +01:00
|
|
|
def order
|
|
|
|
redirect_to :action => 'stock_order', :id => @order if @order.stockit?
|
|
|
|
|
2009-01-20 19:37:15 +01:00
|
|
|
# Load order article data...
|
2009-02-04 16:41:01 +01:00
|
|
|
@articles_grouped_by_category = @order.articles_grouped_by_category
|
2009-01-20 19:37:15 +01:00
|
|
|
# save results of earlier orders in array
|
|
|
|
ordered_articles = Array.new
|
2009-02-04 16:41:01 +01:00
|
|
|
@group_order = @order.group_orders.find(:first,
|
|
|
|
:conditions => "ordergroup_id = #{@ordergroup.id}", :include => :group_order_articles)
|
|
|
|
|
2009-01-20 19:37:15 +01:00
|
|
|
if @group_order
|
|
|
|
# Group has already ordered, so get the results...
|
2009-02-04 16:41:01 +01:00
|
|
|
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)}
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2009-01-20 19:37:15 +01:00
|
|
|
@version = @group_order.lock_version
|
2009-01-29 01:57:51 +01:00
|
|
|
@availableFunds = @ordergroup.get_available_funds(@group_order)
|
2009-01-20 19:37:15 +01:00
|
|
|
else
|
|
|
|
@version = 0
|
2009-01-29 01:57:51 +01:00
|
|
|
@availableFunds = @ordergroup.get_available_funds
|
2009-01-20 19:37:15 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# load prices ....
|
|
|
|
@price = Array.new; @unit = Array.new;
|
|
|
|
@others_quantity = Array.new; @quantity = Array.new; @quantity_result = Array.new; @used_quantity = Array.new; @unused_quantity = Array.new
|
|
|
|
@others_tolerance = Array.new; @tolerance = Array.new; @tolerance_result = Array.new; @used_tolerance = Array.new; @unused_tolerance = Array.new
|
|
|
|
i = 0;
|
2009-02-04 16:41:01 +01:00
|
|
|
@articles_grouped_by_category.each do |category_name, order_articles|
|
2009-01-20 19:37:15 +01:00
|
|
|
for order_article in order_articles
|
|
|
|
# price/unit size
|
2009-01-29 01:57:51 +01:00
|
|
|
@price[i] = order_article.article.fc_price
|
2009-01-20 19:37:15 +01:00
|
|
|
@unit[i] = order_article.article.unit_quantity
|
|
|
|
# quantity
|
2009-02-04 16:41:01 +01:00
|
|
|
@quantity[i] = (ordered_articles[order_article.id] ? ordered_articles[order_article.id][:quantity] : 0)
|
2009-01-20 19:37:15 +01:00
|
|
|
@others_quantity[i] = order_article.quantity - @quantity[i]
|
2009-02-04 16:41:01 +01:00
|
|
|
@used_quantity[i] = (ordered_articles[order_article.id] ? ordered_articles[order_article.id][:quantity_result] : 0)
|
2009-01-20 19:37:15 +01:00
|
|
|
# tolerance
|
2009-02-04 16:41:01 +01:00
|
|
|
@tolerance[i] = (ordered_articles[order_article.id] ? ordered_articles[order_article.id][:tolerance] : 0)
|
2009-01-20 19:37:15 +01:00
|
|
|
@others_tolerance[i] = order_article.tolerance - @tolerance[i]
|
2009-02-04 16:41:01 +01:00
|
|
|
@used_tolerance[i] = (ordered_articles[order_article.id] ? ordered_articles[order_article.id][:tolerance_result] : 0)
|
2009-01-20 19:37:15 +01:00
|
|
|
i += 1
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-02-06 16:26:35 +01:00
|
|
|
|
|
|
|
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
|
2009-01-06 11:49:19 +01:00
|
|
|
|
|
|
|
# Update changes to a current order.
|
|
|
|
def saveOrder
|
2009-02-03 21:14:48 +01:00
|
|
|
if (params[:total_balance].to_i < 0) #TODO: Better use a real test on sufficiant funds
|
2009-01-20 19:37:15 +01:00
|
|
|
flash[:error] = 'Der Bestellwert übersteigt das verfügbare Guthaben.'
|
2009-01-06 11:49:19 +01:00
|
|
|
redirect_to :action => 'order'
|
|
|
|
elsif (ordered = params[:ordered])
|
2009-02-03 21:14:48 +01:00
|
|
|
begin
|
|
|
|
Order.transaction do
|
|
|
|
# Try to find group_order
|
|
|
|
group_order = @order.group_orders.first :conditions => "ordergroup_id = #{@ordergroup.id}",
|
|
|
|
:include => [:group_order_articles]
|
|
|
|
# Create group order if necessary...
|
|
|
|
unless group_order.nil?
|
|
|
|
# check for conflicts well ahead
|
|
|
|
if (params[:version].to_i != group_order.lock_version)
|
|
|
|
raise ActiveRecord::StaleObjectError
|
|
|
|
end
|
|
|
|
else
|
|
|
|
group_order = @ordergroup.group_orders.create!(:order => @order, :updated_by => @current_user, :price => 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Create/update group_order_articles...
|
|
|
|
for order_article in @order.order_articles
|
|
|
|
|
|
|
|
# Find the group_order_article, create a new one if necessary...
|
|
|
|
group_order_article = group_order.group_order_articles.detect { |v| v.order_article_id == order_article.id }
|
|
|
|
if group_order_article.nil?
|
|
|
|
group_order_article = group_order.group_order_articles.create(:order_article_id => order_article.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Get ordered quantities and update group_order_articles/_quantities...
|
2009-02-04 16:41:01 +01:00
|
|
|
quantities = ordered.fetch(order_article.id.to_s, {:quantity => 0, :tolerance => 0})
|
|
|
|
group_order_article.update_quantities(quantities[:quantity].to_i, quantities[:tolerance].to_i)
|
2009-02-03 21:14:48 +01:00
|
|
|
|
|
|
|
# Also update results for the order_article
|
|
|
|
order_article.update_results!
|
|
|
|
end
|
|
|
|
|
|
|
|
group_order.update_price!
|
|
|
|
group_order.update_attribute(:updated_by, @current_user)
|
|
|
|
end
|
|
|
|
flash[:notice] = 'Die Bestellung wurde gespeichert.'
|
|
|
|
rescue ActiveRecord::StaleObjectError
|
|
|
|
flash[:error] = '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)
|
|
|
|
flash[:error] = 'Die Bestellung konnte nicht aktualisiert werden, da ein Fehler auftrat.'
|
|
|
|
end
|
|
|
|
redirect_to :action => 'my_order_result', :id => @order
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-14 12:46:01 +01:00
|
|
|
# Shows the Result for the Ordergroup the current user belongs to
|
2009-01-06 11:49:19 +01:00
|
|
|
# this method decides between finished and unfinished orders
|
|
|
|
def my_order_result
|
|
|
|
@order= Order.find(params[:id])
|
2009-01-29 01:57:51 +01:00
|
|
|
@group_order = @order.group_order(@ordergroup)
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# Shows all Orders of the Ordergroup
|
|
|
|
# if selected, it shows all orders of the foodcoop
|
|
|
|
def myOrders
|
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,
|
|
|
|
:conditions => { :state => 'closed' }, :order => "orders.ends DESC"
|
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
respond_to do |format|
|
|
|
|
format.html # myOrders.haml
|
2009-01-29 01:57:51 +01:00
|
|
|
format.js { render :partial => "orders" }
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# adds a Comment to the Order
|
2009-01-29 01:57:51 +01:00
|
|
|
def add_comment
|
|
|
|
order = Order.find(params[:id])
|
|
|
|
comment = order.comments.build(params[:comment])
|
|
|
|
comment.user = @current_user
|
|
|
|
if !comment.text.empty? and comment.save
|
|
|
|
flash[:notice] = "Kommentar wurde erstellt."
|
2009-01-06 11:49:19 +01:00
|
|
|
else
|
2009-01-29 01:57:51 +01:00
|
|
|
flash[:error] = "Kommentar konnte nicht erstellt werden. Leerer Kommentar?"
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2009-01-29 01:57:51 +01:00
|
|
|
redirect_to :action => 'my_order_result', :id => order
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
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?
|
|
|
|
flash[:notice] = 'Sie gehören keiner Bestellgruppe an.'
|
|
|
|
redirect_to :controller => root_path
|
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
|
|
|
|
@order = Order.find(params[:id], :include => [:supplier, :order_articles])
|
|
|
|
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
|
|
|
|
|
|
|
end
|