112 lines
3.2 KiB
Ruby
112 lines
3.2 KiB
Ruby
class UserController < ApplicationController
|
|
before_filter :authenticate_user!
|
|
|
|
alias :list :active_list
|
|
|
|
# POST /user/create_list {table_id: 1234}
|
|
def create_list
|
|
@table = Table.find(params[:table_id])
|
|
if @table.occupied?
|
|
redirect_to root_path, alert: t('table.is_occupied')
|
|
else
|
|
@list = List.new(table: @table, supplier_id: @table.supplier_id)
|
|
@list.add_user current_user
|
|
#@list.add_user(current_user)
|
|
@list.save
|
|
session[:active_list_id] = @list.id
|
|
redirect_to user_list_products_path
|
|
end
|
|
end
|
|
|
|
def list_products
|
|
@supplier = Supplier.first
|
|
render layout: 'phone'
|
|
end
|
|
|
|
# GET /user/current_list.json
|
|
# Information about the currently active list
|
|
# This information includes detailed order information
|
|
def active_list
|
|
respond_to do |format|
|
|
format.html do
|
|
redirect_to(root_path, alert: t('messages.there_is_no_list_active')) and return unless list.present?
|
|
render layout: 'phone'
|
|
end
|
|
format.json do
|
|
@list = list
|
|
@list.orders.include_relations(product_orders: :product)
|
|
h = @list.as_json
|
|
h[:orders] = []
|
|
h[:list_active] = @list.active?
|
|
list_total = 0.0
|
|
for order in @list.orders
|
|
ho = {products: []}
|
|
order_total = 0.0
|
|
for product_order in order.product_orders
|
|
order_total += (product_order.amount * product_order.price).round(2)
|
|
ho[:products] << {name: product_order.product.name, id: product_order.product_id, number: product_order.amount, price: product_order.price}
|
|
end
|
|
ho[:total_amount] = order_total.round(2)
|
|
ho[:state] = order.state
|
|
list_total += ho[:total_amount]
|
|
h[:orders] << ho
|
|
end
|
|
h[:total_amount] = list_total.round(2)
|
|
render json: h
|
|
end
|
|
end
|
|
end
|
|
|
|
# GET /user/list_info.json
|
|
# Information about the currently active list
|
|
# Fast version to verify wether the is is still currently active
|
|
def list_info
|
|
respond_to do |format|
|
|
format.json do
|
|
render json: {list_active: false} and return unless list.present?
|
|
render json: list.as_json.merge(list_active: true)
|
|
end
|
|
end
|
|
end
|
|
|
|
# POST /user/needs_help.json
|
|
def needs_help
|
|
respond_to do |format|
|
|
format.json do
|
|
render json: {list_active: false} and return unless list.present?
|
|
list.needs_help = true
|
|
list.save
|
|
render json: list.as_json.merge(list_active: true)
|
|
end
|
|
end
|
|
end
|
|
|
|
# POST /user/list_needs_payment.json
|
|
def list_needs_payment
|
|
respond_to do |format|
|
|
format.json do
|
|
render json: {list_active: false} and return unless list.present?
|
|
list.needs_payment = true
|
|
list.save
|
|
render json: list.as_json.merge(list_active: true)
|
|
end
|
|
end
|
|
end
|
|
|
|
##
|
|
# Displays the closed lists of the user
|
|
def list_history
|
|
render layout: 'phone'
|
|
end
|
|
|
|
##
|
|
# Displays a closed list of the user
|
|
# GET /user/list_history/:list_id
|
|
def history_list
|
|
@list = List.find(params[:list_id])
|
|
redirect_to user_root_path, alert: t('messages.illegal_history_list_attempt') and return unless @list.user_ids.include?(current_user.id)
|
|
render layout: 'phone'
|
|
end
|
|
|
|
end
|