2014-11-06 18:56:00 +01:00
|
|
|
class OrdersController < ApplicationController
|
2015-02-12 14:39:58 +01:00
|
|
|
include ActionView::Helpers::NumberHelper
|
|
|
|
|
2015-01-06 20:18:01 +01:00
|
|
|
load_and_authorize_resource
|
|
|
|
|
2014-11-25 15:27:12 +01:00
|
|
|
def new
|
2015-03-12 13:25:11 +01:00
|
|
|
init
|
2015-02-12 14:39:58 +01:00
|
|
|
@order = @user.orders.build
|
2015-03-12 13:25:11 +01:00
|
|
|
|
2015-02-20 09:21:18 +01:00
|
|
|
# products = @user.products.select("products.*", "sum(order_items.count) as count").group(:product_id).order("count desc")
|
|
|
|
# @order.g_order_items(products)
|
2015-03-12 13:25:11 +01:00
|
|
|
@order.g_order_items Product.all
|
2014-11-06 18:56:00 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2015-03-12 13:25:11 +01:00
|
|
|
init
|
|
|
|
@order = @user.orders.build order_params
|
2015-01-15 00:39:34 +01:00
|
|
|
|
2014-12-04 19:28:21 +01:00
|
|
|
if @order.save
|
2015-01-15 00:39:34 +01:00
|
|
|
flash[:success] = "#{@order.to_sentence} ordered. Enjoy it!"
|
2014-12-04 19:28:21 +01:00
|
|
|
redirect_to root_path
|
|
|
|
else
|
2015-03-12 13:25:11 +01:00
|
|
|
@order.g_order_items Product.all
|
2014-12-04 19:28:21 +01:00
|
|
|
render 'new'
|
2014-11-25 15:27:12 +01:00
|
|
|
end
|
2014-11-23 21:12:31 +01:00
|
|
|
end
|
|
|
|
|
2015-02-04 19:03:50 +01:00
|
|
|
def destroy
|
|
|
|
order = Order.find(params[:id])
|
|
|
|
if order.created_at > 5.minutes.ago
|
2015-03-10 11:37:48 +01:00
|
|
|
order.cancel
|
2015-02-04 19:03:50 +01:00
|
|
|
flash[:success] = "Order has been removed."
|
|
|
|
else
|
|
|
|
flash[:error] = "This order has been placed too long ago, it can't be removed. Please contact a sysadmin."
|
|
|
|
end
|
2015-02-12 14:39:58 +01:00
|
|
|
redirect_to root_path
|
2015-02-04 19:03:50 +01:00
|
|
|
end
|
|
|
|
|
2015-01-06 20:33:11 +01:00
|
|
|
def overview
|
2015-02-20 09:21:18 +01:00
|
|
|
@users = User.members.order(:name)
|
2014-12-06 12:03:08 +01:00
|
|
|
end
|
|
|
|
|
2014-12-10 13:21:06 +01:00
|
|
|
def quickpay
|
2015-01-15 00:39:34 +01:00
|
|
|
user = User.find(params[:user_id])
|
|
|
|
order = user.orders.build
|
2015-02-10 08:33:51 +01:00
|
|
|
order.order_items << OrderItem.new(count: 1, product: user.dagschotel, order: order)
|
2014-12-10 13:21:06 +01:00
|
|
|
if order.save
|
2015-02-04 19:03:50 +01:00
|
|
|
flash[:success] = "Quick pay succeeded. #{view_context.link_to("Undo", [user, order], method: :delete)}."
|
2014-12-10 13:21:06 +01:00
|
|
|
else
|
2015-01-15 00:39:34 +01:00
|
|
|
flash[:error] = order.errors.full_messages.first
|
2014-12-10 13:21:06 +01:00
|
|
|
end
|
|
|
|
redirect_to root_path
|
|
|
|
end
|
|
|
|
|
2014-11-10 02:30:42 +01:00
|
|
|
private
|
|
|
|
|
2015-03-12 13:25:11 +01:00
|
|
|
def init
|
|
|
|
@user = User.find(params[:user_id])
|
|
|
|
|
|
|
|
if @user.koelkast?
|
|
|
|
flash[:error] = "Koelkast can't order things."
|
|
|
|
redirect_to root_path
|
|
|
|
end
|
|
|
|
|
2015-03-19 14:59:37 +01:00
|
|
|
unless current_user.koelkast? || current_user.admin? || current_user == @user
|
2015-03-12 13:25:11 +01:00
|
|
|
flash[:error] = "Please don't order stuff for other people"
|
|
|
|
redirect_to root_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-10 02:30:42 +01:00
|
|
|
def order_params
|
2015-03-10 11:37:48 +01:00
|
|
|
params.require(:order).permit(order_items_attributes: [:count, :price, product_attributes: [:id]])
|
2014-11-10 02:30:42 +01:00
|
|
|
end
|
2014-11-06 18:56:00 +01:00
|
|
|
end
|