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
|
2023-05-12 13:01:12 +02:00
|
|
|
before_action :authenticate_finance_or_invoices, except: %i[new create]
|
|
|
|
before_action :authenticate_finance_orders_or_pickup, except: %i[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
|
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
def edit
|
|
|
|
@order_article = OrderArticle.find(params[:id])
|
|
|
|
end
|
|
|
|
|
2012-06-21 17:19:00 +02:00
|
|
|
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.
|
2023-05-12 13:01:12 +02:00
|
|
|
@order_article = @order.order_articles.where(article_id: params[:order_article][:article_id]).first
|
|
|
|
@order_article = @order.order_articles.build(params[:order_article]) unless @order_article && @order_article.units_to_order == 0
|
2013-12-31 12:27:10 +01:00
|
|
|
@order_article.save!
|
2023-05-12 13:01:12 +02:00
|
|
|
rescue StandardError
|
2013-12-31 12:27:10 +01:00
|
|
|
render action: :new
|
2012-06-21 17:19:00 +02:00
|
|
|
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])
|
2023-05-12 13:01:12 +02:00
|
|
|
rescue StandardError
|
2012-06-21 17:19:00 +02:00
|
|
|
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
|