2013-12-31 11:41:14 +01:00
|
|
|
class OrderArticlesController < ApplicationController
|
2021-01-30 11:21:00 +01:00
|
|
|
before_action :fetch_order, except: :destroy
|
|
|
|
before_action :authenticate_finance_or_invoices, except: [:new, :create]
|
|
|
|
before_action :authenticate_finance_orders_or_pickup, except: [:edit, :update, :destroy]
|
2012-06-21 17:19:00 +02:00
|
|
|
|
2012-07-27 18:03:46 +02:00
|
|
|
layout false # We only use this controller to serve js snippets, no need for layout rendering
|
2012-06-21 17:19:00 +02:00
|
|
|
|
|
|
|
def new
|
2014-01-01 23:45:57 +01:00
|
|
|
@order_article = @order.order_articles.build(params[:order_article])
|
2012-06-21 17:19:00 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2014-01-01 23:54:01 +01:00
|
|
|
# The article may be ordered with zero units - in that case do not complain.
|
2013-06-24 13:32:07 +02:00
|
|
|
# If order_article is ordered and a new order_article is created, an error message will be
|
|
|
|
# given mentioning that the article already exists, which is desired.
|
|
|
|
@order_article = @order.order_articles.where(:article_id => params[:order_article][:article_id]).first
|
2015-01-14 22:56:32 +01:00
|
|
|
unless @order_article && @order_article.units_to_order == 0
|
2014-01-01 16:54:49 +01:00
|
|
|
@order_article = @order.order_articles.build(params[:order_article])
|
2012-06-21 17:19:00 +02:00
|
|
|
end
|
2013-12-31 12:27:10 +01:00
|
|
|
@order_article.save!
|
|
|
|
rescue
|
|
|
|
render action: :new
|
2012-06-21 17:19:00 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
|
|
|
@order_article = OrderArticle.find(params[:id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
@order_article = OrderArticle.find(params[:id])
|
|
|
|
begin
|
2013-03-17 18:33:04 +01:00
|
|
|
@order_article.update_article_and_price!(params[:order_article], params[:article], params[:article_price])
|
2012-06-21 17:19:00 +02:00
|
|
|
rescue
|
|
|
|
render action: :edit
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@order_article = OrderArticle.find(params[:id])
|
2013-09-13 15:37:30 +02:00
|
|
|
# only destroy if there are no associated GroupOrders; if we would, the requested
|
|
|
|
# quantity and tolerance would be gone. Instead of destroying, we set all result
|
|
|
|
# quantities to zero.
|
|
|
|
if @order_article.group_order_articles.count == 0
|
|
|
|
@order_article.destroy
|
|
|
|
else
|
|
|
|
@order_article.group_order_articles.each { |goa| goa.update_attribute(:result, 0) }
|
|
|
|
@order_article.update_results!
|
|
|
|
end
|
2012-06-21 17:19:00 +02:00
|
|
|
end
|
2021-01-30 11:21:00 +01:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def fetch_order
|
|
|
|
@order = Order.find(params[:order_id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def authenticate_finance_orders_or_pickup
|
|
|
|
return if current_user.role_finance? || current_user.role_orders?
|
|
|
|
|
|
|
|
return if current_user.role_pickups? && !@order.nil? && @order.state == 'finished'
|
|
|
|
|
|
|
|
deny_access
|
|
|
|
end
|
2012-06-21 17:19:00 +02:00
|
|
|
end
|