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.
|
2012-08-24 11:11:40 +02:00
|
|
|
class GroupOrdersController < ApplicationController
|
2009-01-06 11:49:19 +01:00
|
|
|
# Security
|
2019-10-28 21:11:35 +01:00
|
|
|
before_action :ensure_ordergroup_member
|
2023-05-12 13:01:12 +02:00
|
|
|
before_action :ensure_open_order, only: %i[new create edit update order stock_order saveOrder]
|
|
|
|
before_action :ensure_my_group_order, only: %i[show edit update]
|
|
|
|
before_action :enough_apples?, only: %i[new create]
|
2011-06-13 16:45:16 +02:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
# Index page.
|
2009-02-06 16:26:35 +01:00
|
|
|
def index
|
2020-07-12 10:26:36 +02:00
|
|
|
@closed_orders_including_group_order = Order.closed.limit(5).ordergroup_group_orders_map(@ordergroup)
|
|
|
|
@finished_not_closed_orders_including_group_order = Order.finished_not_closed.ordergroup_group_orders_map(@ordergroup)
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2011-06-13 16:45:16 +02:00
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
def show
|
|
|
|
@order = @group_order.order
|
|
|
|
end
|
|
|
|
|
2011-06-19 15:30:33 +02:00
|
|
|
def new
|
2016-02-25 02:27:17 +01:00
|
|
|
ordergroup = params[:stock_order] ? nil : @ordergroup
|
2023-05-12 13:01:12 +02:00
|
|
|
@group_order = @order.group_orders.build(ordergroup: ordergroup, updated_by: current_user)
|
|
|
|
@ordering_data = @group_order.load_data
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
2011-06-19 15:30:33 +02:00
|
|
|
@ordering_data = @group_order.load_data
|
|
|
|
end
|
2009-02-04 16:41:01 +01:00
|
|
|
|
2011-06-19 15:30:33 +02:00
|
|
|
def create
|
|
|
|
@group_order = GroupOrder.new(params[:group_order])
|
|
|
|
begin
|
|
|
|
@group_order.save_ordering!
|
2023-05-12 13:01:12 +02:00
|
|
|
redirect_to group_order_url(@group_order), notice: I18n.t('group_orders.create.notice')
|
2011-06-19 15:30:33 +02:00
|
|
|
rescue ActiveRecord::StaleObjectError
|
2023-05-12 13:01:12 +02:00
|
|
|
redirect_to group_orders_url, alert: I18n.t('group_orders.create.error_stale')
|
|
|
|
rescue StandardError => e
|
|
|
|
logger.error('Failed to update order: ' + e.message)
|
|
|
|
redirect_to group_orders_url, alert: I18n.t('group_orders.create.error_general')
|
2009-01-20 19:37:15 +01:00
|
|
|
end
|
2011-06-19 15:30:33 +02:00
|
|
|
end
|
2009-01-20 19:37:15 +01:00
|
|
|
|
2011-06-19 15:30:33 +02:00
|
|
|
def update
|
|
|
|
@group_order.attributes = params[:group_order]
|
2020-10-10 20:40:22 +02:00
|
|
|
@group_order.updated_by = current_user
|
2011-06-19 15:30:33 +02:00
|
|
|
begin
|
|
|
|
@group_order.save_ordering!
|
2023-05-12 13:01:12 +02:00
|
|
|
redirect_to group_order_url(@group_order), notice: I18n.t('group_orders.update.notice')
|
2011-06-19 15:30:33 +02:00
|
|
|
rescue ActiveRecord::StaleObjectError
|
2023-05-12 13:01:12 +02:00
|
|
|
redirect_to group_orders_url, alert: I18n.t('group_orders.update.error_stale')
|
|
|
|
rescue StandardError => e
|
|
|
|
logger.error('Failed to update order: ' + e.message)
|
|
|
|
redirect_to group_orders_url, alert: I18n.t('group_orders.update.error_general')
|
2011-06-13 16:45:16 +02:00
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2014-09-23 10:39:10 +02:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
# Shows all Orders of the Ordergroup
|
|
|
|
# if selected, it shows all orders of the foodcoop
|
2011-06-19 15:30:33 +02:00
|
|
|
def archive
|
2009-01-29 01:57:51 +01:00
|
|
|
# get only orders belonging to the ordergroup
|
2012-10-15 21:19:17 +02:00
|
|
|
@closed_orders = Order.closed.page(params[:page]).per(10)
|
2020-07-12 10:26:36 +02:00
|
|
|
@closed_orders_including_group_order = @closed_orders.ordergroup_group_orders_map(@ordergroup)
|
|
|
|
@finished_not_closed_orders_including_group_order = Order.finished_not_closed.ordergroup_group_orders_map(@ordergroup)
|
2009-01-29 01:57:51 +01:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
respond_to do |format|
|
2012-10-03 20:08:23 +02:00
|
|
|
format.html # archive.html.haml
|
|
|
|
format.js # archive.js.erb
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
end
|
2011-06-13 16:45:16 +02:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
private
|
2011-06-13 16:45:16 +02:00
|
|
|
|
2009-01-29 01:57:51 +01:00
|
|
|
# Returns true if @current_user is member of an Ordergroup.
|
2019-10-28 21:11:35 +01:00
|
|
|
# Used as a :before_action by OrdersController.
|
2009-01-29 01:57:51 +01:00
|
|
|
def ensure_ordergroup_member
|
2009-02-01 20:56:23 +01:00
|
|
|
@ordergroup = @current_user.ordergroup
|
2023-05-12 13:01:12 +02:00
|
|
|
return unless @ordergroup.nil?
|
|
|
|
|
|
|
|
redirect_to root_url, alert: I18n.t('group_orders.errors.no_member')
|
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
|
2023-05-12 13:01:12 +02:00
|
|
|
@order = Order.includes(%i[supplier order_articles]).find(order_id_param)
|
2009-01-29 01:57:51 +01:00
|
|
|
unless @order.open?
|
2013-06-30 13:07:57 +02:00
|
|
|
flash[:notice] = I18n.t('group_orders.errors.closed')
|
2023-05-12 13:01:12 +02:00
|
|
|
redirect_to action: 'index'
|
2009-01-20 19:37:15 +01:00
|
|
|
end
|
2014-09-23 10:39:10 +02:00
|
|
|
rescue ActiveRecord::RecordNotFound
|
2016-02-25 02:27:17 +01:00
|
|
|
redirect_to group_orders_url, alert: I18n.t('group_orders.errors.notfound')
|
2009-01-29 01:57:51 +01:00
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
|
2012-08-24 11:11:40 +02:00
|
|
|
def ensure_my_group_order
|
2017-10-12 01:28:26 +02:00
|
|
|
@group_order = GroupOrder.find(params[:id])
|
2023-05-12 13:01:12 +02:00
|
|
|
return unless @group_order.ordergroup != @ordergroup && (@group_order.ordergroup || !current_user.role_orders?)
|
|
|
|
|
|
|
|
redirect_to group_orders_url, alert: I18n.t('group_orders.errors.notfound')
|
2012-08-24 11:11:40 +02:00
|
|
|
end
|
|
|
|
|
2012-09-29 17:52:25 +02:00
|
|
|
def enough_apples?
|
2023-05-12 13:01:12 +02:00
|
|
|
return unless @ordergroup.not_enough_apples?
|
|
|
|
|
|
|
|
redirect_to group_orders_url,
|
|
|
|
alert: t('not_enough_apples', scope: 'group_orders.messages', apples: @ordergroup.apples,
|
|
|
|
stop_ordering_under: FoodsoftConfig[:stop_ordering_under])
|
2012-09-29 17:52:25 +02:00
|
|
|
end
|
|
|
|
|
2014-09-23 10:39:10 +02:00
|
|
|
def order_id_param
|
|
|
|
params[:order_id] || (params[:group_order] && params[:group_order][:order_id])
|
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|