2014-01-23 16:17:16 +01:00
|
|
|
class GroupOrderArticlesController < ApplicationController
|
2019-10-28 21:11:35 +01:00
|
|
|
before_action :authenticate_finance
|
2023-05-12 13:01:12 +02:00
|
|
|
before_action :find_group_order_article, except: %i[new create]
|
2012-06-21 17:19:00 +02:00
|
|
|
|
|
|
|
layout false # We only use this controller to server js snippets, no need for layout rendering
|
|
|
|
|
|
|
|
def new
|
|
|
|
@order_article = OrderArticle.find(params[:order_article_id])
|
2012-07-27 18:03:46 +02:00
|
|
|
@group_order_article = GroupOrderArticle.new(order_article: @order_article)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2014-01-23 16:17:16 +01:00
|
|
|
# XXX when ordergroup_id appears before order_article_id in the parameters, you
|
|
|
|
# can get `NoMethodError - undefined method 'order_id' for nil:NilClass`
|
2012-07-27 18:03:46 +02:00
|
|
|
@group_order_article = GroupOrderArticle.new(params[:group_order_article])
|
|
|
|
@order_article = @group_order_article.order_article
|
|
|
|
|
|
|
|
# As we hide group_order_articles with a result of 0, we should not complain, when an existing group_order_article is found
|
|
|
|
goa = GroupOrderArticle.where(group_order_id: @group_order_article.group_order_id,
|
|
|
|
order_article_id: @order_article.id).first
|
|
|
|
|
2022-10-13 18:25:52 +02:00
|
|
|
if goa && goa.update(params[:group_order_article])
|
2012-07-27 18:03:46 +02:00
|
|
|
@group_order_article = goa
|
|
|
|
|
|
|
|
update_summaries(@group_order_article)
|
2014-01-23 16:17:16 +01:00
|
|
|
render :create
|
2012-07-27 18:03:46 +02:00
|
|
|
|
|
|
|
elsif @group_order_article.save
|
|
|
|
update_summaries(@group_order_article)
|
2014-01-23 16:17:16 +01:00
|
|
|
render :create
|
2012-07-27 18:03:46 +02:00
|
|
|
|
2021-03-01 15:27:26 +01:00
|
|
|
else # Validation failed, show form
|
2012-07-27 18:03:46 +02:00
|
|
|
render :new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2014-01-23 16:17:16 +01:00
|
|
|
if params[:delta]
|
|
|
|
@group_order_article.update_attribute :result, [@group_order_article.result + params[:delta].to_f, 0].max
|
2012-07-27 18:03:46 +02:00
|
|
|
else
|
2022-10-13 18:25:52 +02:00
|
|
|
@group_order_article.update(params[:group_order_article])
|
2012-07-27 18:03:46 +02:00
|
|
|
end
|
|
|
|
|
2014-01-23 16:17:16 +01:00
|
|
|
update_summaries(@group_order_article)
|
2012-07-27 18:03:46 +02:00
|
|
|
|
|
|
|
render :update
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2013-09-17 14:19:46 +02:00
|
|
|
# only destroy if quantity and tolerance was zero already, so that we don't
|
|
|
|
# lose what the user ordered, if any
|
2021-03-01 15:27:26 +01:00
|
|
|
if @group_order_article.quantity > 0 || @group_order_article.tolerance > 0
|
2014-01-23 16:17:16 +01:00
|
|
|
@group_order_article.update_attribute(:result, 0)
|
2013-09-17 14:19:46 +02:00
|
|
|
else
|
2014-01-23 16:17:16 +01:00
|
|
|
@group_order_article.destroy
|
2013-09-17 14:19:46 +02:00
|
|
|
end
|
2014-01-23 16:17:16 +01:00
|
|
|
update_summaries(@group_order_article)
|
2012-07-27 18:03:46 +02:00
|
|
|
|
|
|
|
render :update
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def update_summaries(group_order_article)
|
|
|
|
# Update the price attribute of new GroupOrder
|
|
|
|
group_order_article.group_order.update_price!
|
|
|
|
# Update units_to_order of order_article
|
|
|
|
group_order_article.order_article.update_results! if group_order_article.order_article.article.is_a?(StockArticle)
|
2012-06-21 17:19:00 +02:00
|
|
|
end
|
2014-01-23 16:17:16 +01:00
|
|
|
|
|
|
|
def find_group_order_article
|
|
|
|
@group_order_article = GroupOrderArticle.find(params[:id])
|
|
|
|
end
|
2012-06-21 17:19:00 +02:00
|
|
|
end
|