Files
mozo-backend/app/controllers/user_controller.rb
T
2012-08-30 11:12:23 +02:00

216 lines
6.5 KiB
Ruby

class UserController < ApplicationController
before_filter :authenticate_user!
alias :list :active_list
def home
flash.now[:notice] = t('messages.the_list_has_been_closed', list: List.model_name.human) if params[:list_closed].present?
render layout: 'phone'
end
# POST /user/create_list {table_id: 1234}
#DEPRICATED
def create_list
@table = Table.find(params[:table_id])
if @table.occupied?
respond_to do |format|
format.html { redirect_to root_path, alert: t('table.is_occupied') }
format.json { render json: js_alert(t('messages.table_is_occupied'))}
end
else
if @list = List.from_table( @table, current_user )
end
respond_to do |format|
format.html { redirect_to user_list_products_path }
format.json { render json: js_notice('table_created')}
end
end
end
def table_info
@table = Table.find(params[:table_id])
res = {}
res[:ocupied] = @table.occupied?
res[:reserved] = @table.reserved?
res[:supplier_closed] = @table.supplier.closed?
if list.present?
res[:other_supplier] = true if list.supplier_id != @table.supplier_id
res[:current_table_id] = list.table_id
end
respond_to do |format|
format.json do
render json: res
end
end
end
# GET /suppliers/1/product_list
# GET /suppliers/1/product_list.json
def list_products
@supplier = list.supplier
respond_to do |format|
format.html do
render layout: 'phone'
end
format.json do
products = list.supplier.products
products.include_relation(:product_categories)
products.sort_by!{|p| p.product_category.try(:position) || 90000}
h = products.inject({}){|h, p| n = p.product_category.try(:name) || 'other'; h[n] ||= []; h[n] << p; h}
render json: h
end
end
end
def list_products_for_table
@table = Table.find(params[:table_id])
respond_to do |format|
format.html do
render layout: 'phone'
end
format.json do
products = @table.supplier.products
products.include_relation(:product_categories)
products.sort_by!{|p| p.product_category.try(:position) || 90000}
h = products.inject({}){|h, p| n = p.product_category.try(:name) || 'other'; h[n] ||= []; h[n] << p; h}
render json: h
end
end
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
if !list.try(:active?)
current_user.list_is_closed!
render json: {list_active: false}
return
else
render json: list.as_json.merge(list_active: list.active? )
end
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: list.active?)
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: list.active?)
end
end
end
##
# Displays the closed lists of the user
# GET /user/list_history
def list_history
@lists = current_user.lists
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])
if params[:list_closed].present? && current_user.active_list_id == @list.id
current_user.list_is_closed!
flash.now[:notice] = t('messages.the_list_has_been_closed', list: List.model_name.human)
end
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
def order_selected_products
if list.present?
@list = list
else
@table = Table.find(params[:table_id])
if @table.occupied?
#TODO handle placint order on occupied table
else
if @list = List.from_table( @table, current_user )
else
#TODO handle second list creation for user
end
end
end
respond_to do |format|
format.html do
redirect_to(root_path, alert: t('messages.cannot_order_on_non_active_list')) and return unless @list.active?
@list.place_order current_user, params[:products]
redirect_to root_path, notice: t('messages.order_is_placed')
end
format.js do
render js: js_alert(t('messages.cannot_order_on_non_active_list')) and return unless @list.active?
@list.place_order current_user, params[:products]
render js: js_notice( t('messages.order_is_placed'), location: user_active_list_path )
end
end
end
def move_table
return unless list.present?
@table = Table.find(params[:table_id])
if @table.occupied?
render json: {occupied: true}
else
list.move_to_table @table
render json: {occupied: false}
end
end
end