implement joining table

This commit is contained in:
2012-08-30 18:32:30 +02:00
parent 6696992320
commit aa0821d0ae
15 changed files with 228 additions and 35 deletions
BIN
View File
Binary file not shown.
Binary file not shown.
Binary file not shown.

After

Width:  |  Height:  |  Size: 723 B

-3
View File
@@ -20,9 +20,6 @@ root.Qrammer =
currency: (num) -> currency: (num) ->
num = 0.0 if isNaN(num) || num == '' || num == null num = 0.0 if isNaN(num) || num == '' || num == null
'€ ' + parseFloat(num).toFixed(2) '€ ' + parseFloat(num).toFixed(2)
clear_active_list: ->
window.active_products_list = {}
$('#active-order-table').hide()
load_active_orders: () -> load_active_orders: () ->
$.get('/supplier/active_orders.json', (res) -> $.get('/supplier/active_orders.json', (res) ->
body = $('#active-orders-table tbody') body = $('#active-orders-table tbody')
@@ -0,0 +1,38 @@
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require twitter/bootstrap
//= require_directory .
//= require_self
var path_mapping = {
user_root: '/user',
join_occupied_table: '/user/join_occupied_table',
list_products_for_table: '/user/list_products_for_table',
list_products: '/user/list_products'
}
function redirect_to(mapping, variables){
variables || (variables = {});
var vars = []
for(var name in variables){
vars.push(name + '=' +variables[name])
}
window.location = path_mapping[mapping] + '?' + vars.join('&')
}
function currency(num) {
if (isNaN(num) || num === '' || num === null) {
num = 0.0;
}
return '€ ' + parseFloat(num).toFixed(2);
}
@@ -9,10 +9,40 @@ window.Quser=
Quser.handle_active_list_default_actions(res) Quser.handle_active_list_default_actions(res)
) )
handle_active_list_default_actions: (response)-> handle_active_list_default_actions: (response)->
if typeof(response) == 'object' && response.table_number response ||= {}
if response.table_number
$('.table-number').text(response.table_number) $('.table-number').text(response.table_number)
Quser.list_needs_payment_default_action() Quser.list_needs_payment_default_action()
Quser.list_needs_help_default_action() Quser.list_needs_help_default_action()
# join_request_active is to ensure that there are no more modals loaded when one is still active
console.log('join_request_active=' + window.join_request_active)
if !window.join_request_active && response.join_requests && response.join_requests.length
window.join_request_active = true
for join_request in response.join_requests
wrapper = $('<div class="modal"></div>')
join_callback = ( (request)->
->
$.post('/user/approve_join_request', {user_id: request.user_id}, -> window.join_request_active = false; wrapper.modal('hide') )
)(join_request)
reject_callback = ( (request)->
->
$.post('/user/reject_join_request', {user_id: request.user_id}, -> window.join_request_active = false; wrapper.modal('hide' ))
)(join_request)
header = $('<div class="modal-header"></div>')
.append('<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>')
.append('<h3>Join request</h3>').appendTo(wrapper)
body = $('<div class="modal-body"></div>')
body.append(join_request.user_email + ' wants to join the table')
body.appendTo(wrapper)
footer = $('<div class="modal-footer"></div>')
.append($('<a href="#" class="btn">Reject</a>').click(reject_callback))
.append($('<a href="#" class="btn btn-primary">Approve</a>').click(join_callback))
.appendTo(wrapper)
wrapper.modal()
list_needs_help_default_action: -> list_needs_help_default_action: ->
needs_help_container = $('#list-needs-help-button') needs_help_container = $('#list-needs-help-button')
if needs_help_container.length if needs_help_container.length
@@ -57,10 +87,11 @@ window.Quser=
for product in order.products for product in order.products
order_txts.push(product.name + ' (' + product['number'] + ')') order_txts.push(product.name + ' (' + product['number'] + ')')
row.append($('<td></td>').text(order_txts.join(', '))) row.append($('<td></td>').text(order_txts.join(', ')))
row.append($('<td class="currency"></td>').html(Qrammer.currency(order.total_amount))) row.append($('<td class="currency"></td>').html(currency(order.total_amount)))
foot.append('<tr><td></td><td class="currency"><strong>'+Qrammer.currency(res.total_amount)+'</strong></td></tr>') foot.append('<tr><td></td><td class="currency"><strong>'+currency(res.total_amount)+'</strong></td></tr>')
) )
order_selected_products: (h)-> order_selected_products: (h)->
return if $.isEmptyObject(window.active_products_list)
h ||= {} h ||= {}
for product_id, info of window.active_products_list for product_id, info of window.active_products_list
h['products['+product_id+']'] = info.number h['products['+product_id+']'] = info.number
@@ -86,10 +117,10 @@ window.Quser=
row = $('<tr></tr>').attr('id', 'active-order-row-'+product_id).appendTo(tbody) row = $('<tr></tr>').attr('id', 'active-order-row-'+product_id).appendTo(tbody)
row.append('<td>'+info.product.name+'</td>') row.append('<td>'+info.product.name+'</td>')
row.append('<td>'+info.number+'</td>') row.append('<td>'+info.number+'</td>')
row.append('<td class="currency">'+Qrammer.currency(info.product.price * info.number)+'</td>') row.append('<td class="currency">'+currency(info.product.price * info.number)+'</td>')
x_btn = $('<button class="btn btn-warning btn-mini">x</button>').click(-> delete(window.active_products_list[product_id]) && Quser.build_product_list() ) x_btn = $('<button class="btn btn-warning btn-mini">x</button>').click(-> delete(window.active_products_list[product_id]) && Quser.build_product_list() )
row.append($('<td></td>').append(x_btn)) row.append($('<td></td>').append(x_btn))
$('#active-order-total').html(Qrammer.currency(total)) $('#active-order-total').html(currency(total))
table.show() table.show()
load_active_list_products: -> load_active_list_products: ->
@@ -131,36 +162,68 @@ window.Quser=
val_holder.text(val + 1) val_holder.text(val + 1)
) )
row.append($('<td class="order-count-cell"></td>').append(order_count_minus).append('&nbsp;').append(order_product_count).append('&nbsp;').append(order_count_plus)) row.append($('<td class="order-count-cell"></td>').append(order_count_minus).append('&nbsp;').append(order_product_count).append('&nbsp;').append(order_count_plus))
row.append('<td>'+Qrammer.currency(product.price)+'</td>') row.append('<td>'+currency(product.price)+'</td>')
row.append($('<td></td>').append(button)) if include_order_buttons row.append($('<td></td>').append(button)) if include_order_buttons
body.append(row) body.append(row)
) )
actions_for_table: (table)-> actions_for_table: (table)->
table = JSON.parse(table) if typeof(table) == 'string' table = JSON.parse(table) if typeof(table) == 'string'
$.get('/user/table_info.json?table_id='+table.table_id, (res)-> $.get('/user/table_info.json?table_id='+table.table_id, (res)->
if res.occupied if res.current_table_id
alert('Table is occupied')
else
if res.other_supplier if res.other_supplier
#TODO cannot do something with other supplier when list is active #TODO cannot do something with other supplier when list is active
else if res.current_table_id && res.current_table_id == table.table_id else if res.current_table_id == table.table_id
#nothing has changed, show product list #nothing has changed, show product list
window.location = '/user/list_products' window.location = '/user/list_products'
else if res.current_table_id && res.current_table_id != table.table_id else if res.current_table_id != table.table_id
#TODO Offer to move table if res.occupied
$.post('/user/move_table', {table_id: table.table_id}, (res2)-> redirect_to 'user_root', {message: 'table_is_occupied'}
if res2.occupied else if res.reserved
alert('Cannot move to occupied table') redirect_to 'user_root', {message: 'table_is_reserved'}
else else if table.closed
window.location = '/user/list_products' redirect_to 'user_root', {message: 'table_is_closed'}
) else if table.supplier_closed
redirect_to 'user_root', {message: 'supplier_is_closed'}
else
#TODO Offer to move table
$.post('/user/move_table', {table_id: table.table_id}, (res2)->
if res2.occupied
alert('Cannot move to occupied table')
else
window.location = '/user/list_products'
)
else
if res.occupied
redirect_to 'join_occupied_table', {table_id: table.table_id}
else else
#$.post('/user/create_list.json', {table_id: table.table_id}, (res)-> Quser.handle_response(res)) #$.post('/user/create_list.json', {table_id: table.table_id}, (res)-> Quser.handle_response(res))
window.location = '/user/list_products_for_table?table_id='+table.table_id redirect_to 'list_products_for_table', {table_id: table.table_id}
, 'json') , 'json')
join_occupied_table: (table_id) ->
$('.form-actions').remove()
cont = $('#join-occupied-table-progress-container')
cont.html('')
cont.append $('<img src="/assets/spinner.gif" />')
cont.append $('<p>Waiting for approval of the person on this table</p>')
$.post('/user/join_occupied_table', {table_id: table_id})
setInterval('Quser.check_if_can_join_occupied_table("'+table_id+'")', 7500)
check_if_can_join_occupied_table: (table_id)->
$.post('/user/check_table_join_status', {table_id: table_id}, (res) ->
res ||= {}
if res.approved
redirect_to 'list_products'
else if res.waiting
# do nothing, keep waiting....
else
redirect_to 'user_root', {message: 'join_request_rejected'}
)
add_product: (product, count) -> add_product: (product, count) ->
count ||= 1 count ||= 1
window.active_products_list = {} unless window.active_products_list window.active_products_list = {} unless window.active_products_list
window.active_products_list[product._id] = {product: product, number: 0} unless window.active_products_list[product._id] window.active_products_list[product._id] = {product: product, number: 0} unless window.active_products_list[product._id]
window.active_products_list[product._id].number += count window.active_products_list[product._id].number += count
Quser.build_product_list() Quser.build_product_list()
clear_selected_products: ->
window.active_products_list = {}
$('#active-order-table').hide()
+71 -3
View File
@@ -1,10 +1,12 @@
class UserController < ApplicationController class UserController < ApplicationController
before_filter :authenticate_user! before_filter :authenticate_user!
layout 'phone'
alias :list :active_list alias :list :active_list
def home def home
flash.now[:notice] = t('messages.the_list_has_been_closed', list: List.model_name.human) if params[:list_closed].present? flash.now[:notice] = t('messages.the_list_has_been_closed', list: List.model_name.human) if params[:list_closed].present?
flash.now[:notice] = t("messages.#{params[:message]}") if params[:message].present? && params[:message] =~ /^\w+$/
render layout: 'phone' render layout: 'phone'
end end
@@ -30,7 +32,7 @@ class UserController < ApplicationController
def table_info def table_info
@table = Table.find(params[:table_id]) @table = Table.find(params[:table_id])
res = {} res = {}
res[:ocupied] = @table.occupied? res[:occupied] = @table.occupied?
res[:reserved] = @table.reserved? res[:reserved] = @table.reserved?
res[:supplier_closed] = @table.supplier.closed? res[:supplier_closed] = @table.supplier.closed?
if list.present? if list.present?
@@ -62,6 +64,62 @@ class UserController < ApplicationController
end end
end end
def join_occupied_table
@table = Table.find(params[:table_id])
end
def request_to_join_occupied_table
@table = Table.find(params[:table_id])
if @list = @table.active_list
unless @list.join_requests.include?(current_user.id)
@list.join_requests << current_user.id
@list.is_dirty
@list.save
end
end
render nothing: true
end
def reject_join_request
return unless params[:user_id]
if list && list.join_requests.include?(params[:user_id])
list.join_requests.delete(params[:user_id])
list.is_dirty
list.save
end
render js: ''
end
def approve_join_request
return unless params[:user_id]
@user = User.find(params[:user_id])
if list && list.join_requests.include?(params[:user_id])
list.join_requests.delete(params[:user_id])
@user.active_list_id = list.id
list.add_user(@user)
@user.save
list.is_dirty
list.save
end
render nothing: true
end
# POST /user/check_table_join_status table_id:12345
def check_table_join_status
@table = Table.find(params[:table_id])
if @list = @table.active_list
if @list.user_ids.include?(current_user.id)
render json: {approved: true}
elsif @list.join_requests.include?(current_user.id)
render json: {waiting: true}
else
render json: {rejected: true}
end
else
render json: {rejected: true}
end
end
def list_products_for_table def list_products_for_table
@table = Table.find(params[:table_id]) @table = Table.find(params[:table_id])
respond_to do |format| respond_to do |format|
@@ -115,6 +173,7 @@ class UserController < ApplicationController
# GET /user/list_info.json # GET /user/list_info.json
# Information about the currently active list # Information about the currently active list
# Fast version to verify wether the is is still currently active # Fast version to verify wether the is is still currently active
# for handle_active_list
def list_info def list_info
respond_to do |format| respond_to do |format|
format.json do format.json do
@@ -122,8 +181,17 @@ class UserController < ApplicationController
current_user.list_is_closed! current_user.list_is_closed!
render json: {list_active: false} render json: {list_active: false}
return return
else else
render json: list.as_json.merge(list_active: list.active? ) list_obj = list.as_json.merge(list_active: list.active? )
# Handle join requests
if list.join_requests.any?
list_obj[:join_requests] = []
for user in CouchPotato.database.load_document(list.join_requests)
list_obj[:join_requests] << {user_id: user.id, user_email: user.email}
end
end
render json: list_obj
end end
end end
end end
+8
View File
@@ -5,6 +5,8 @@ class List
property :needs_help, type: :boolean, default: false property :needs_help, type: :boolean, default: false
property :needs_payment, type: :boolean, default: false property :needs_payment, type: :boolean, default: false
property :closed_at, type: Time property :closed_at, type: Time
property :join_requests, type: Array, default: []
has_many :orders, dependent: :destroy has_many :orders, dependent: :destroy
belongs_to :table belongs_to :table
belongs_to :supplier belongs_to :supplier
@@ -16,6 +18,12 @@ class List
view :by_supplier_id_and_id, key: [:supplier_id, :_id] view :by_supplier_id_and_id, key: [:supplier_id, :_id]
view :active_by_table_id, type: :custom, map_function: %|function(doc){
if(doc.ruby_class == 'List' && doc.state == 'active'){
emit(doc.table_id, 1);
}
}|, reduce_function: '_sum'
def self.from_table table, user def self.from_table table, user
return if user.has_active_list? return if user.has_active_list?
list = new table: table, supplier_id: table.supplier_id, section_id: table.section_id list = new table: table, supplier_id: table.supplier_id, section_id: table.section_id
+5 -7
View File
@@ -16,15 +16,13 @@ class Table
validates :number, numericality: {greater_than: 0} validates :number, numericality: {greater_than: 0}
validates_uniqueness_of :number validates_uniqueness_of :number
view :active_lists, type: :custom, map_function: %|function(doc){
if(doc.ruby_class == 'List' && doc.state == 'active'){
emit(doc.table_id, 1);
}
}|, reduce_function: '_sum'
def occupied? def occupied?
return @is_occupied if instance_variable_defined?(:'@is_occupied') return @is_occupied if instance_variable_defined?(:'@is_occupied')
@is_occupied = !self.class.database.view(self.class.active_lists(key: id, reduce: true)).zero? @is_occupied = !self.class.database.view(List.active_by_table_id(key: id, reduce: true)).zero?
end
def active_list
@active_list ||= self.class.database.view(List.active_by_table_id(key: id, include_docs: true, reduce: false, limit: 1)).try(:first)
end end
def reserved? def reserved?
+1 -1
View File
@@ -56,5 +56,5 @@ html lang="en"
Javascripts Javascripts
\================================================== \==================================================
/! Placed at the end of the document so the pages load faster /! Placed at the end of the document so the pages load faster
= javascript_include_tag "application" = javascript_include_tag "user/application"
= yield :footer = yield :footer
@@ -0,0 +1,7 @@
.page-header
h4= t('user.join_occupied_table.title')
.form-actions
= link_to t('user.join_occupied_table.show_the_products'), user_list_products_for_table_path(table_id: @table.id), class: [:btn, 'btn-primary']
'
button.btn.btn-warning{onClick="Quser.join_occupied_table('#{@table.id}')"} = t('user.join_occupied_table.join_this_table')
#join-occupied-table-progress-container
+1 -1
View File
@@ -18,7 +18,7 @@ table#active-order-table.table.table-striped.hide
td colspan=2 td colspan=2
button class="btn btn-primary" onClick="Quser.handle_active_list(function(){Quser.order_selected_products()})" Bestellen button class="btn btn-primary" onClick="Quser.handle_active_list(function(){Quser.order_selected_products()})" Bestellen
|&nbsp; |&nbsp;
button class="btn btn btn-warning" onClick="Qrammer.clear_active_list()" Clear button class="btn btn btn-warning" onClick="Quser.clear_selected_products()" Clear
td.currency td.currency
strong#active-order-total strong#active-order-total
td td
@@ -16,7 +16,7 @@ table#active-order-table.table.table-striped.hide
td colspan=2 td colspan=2
button class="btn btn-primary" onClick="Quser.order_selected_products({table_id: '#{@table.id}'})" Bestellen button class="btn btn-primary" onClick="Quser.order_selected_products({table_id: '#{@table.id}'})" Bestellen
|&nbsp; |&nbsp;
button class="btn btn btn-warning" onClick="Qrammer.clear_active_list()" Clear button class="btn btn btn-warning" onClick="Quser.clear_selected_products()" Clear
td.currency td.currency
strong#active-order-total strong#active-order-total
td td
+8
View File
@@ -16,6 +16,10 @@ en:
the_list_has_been_closed: The %{list} has been closed the_list_has_been_closed: The %{list} has been closed
illegal_history_list_attempt: The list you want to access is not yours illegal_history_list_attempt: The list you want to access is not yours
table_is_occupied: The table you want to sit on is already occupied table_is_occupied: The table you want to sit on is already occupied
table_is_reserved: The table you want to sit on is reserved by someone else
table_is_closed: The table you want to sit on is not available for service
supplier_is_closed: The owner of this table is currently not handling orders
join_request_rejected: Your request to join the table has been rejected
action: action:
index: index:
label: Listing %{models} label: Listing %{models}
@@ -71,5 +75,9 @@ en:
show_products: show_products:
# The title gets products: Product.model_name.human_plural that can be used: e.g.: Showing %{products} # The title gets products: Product.model_name.human_plural that can be used: e.g.: Showing %{products}
title: Menu title: Menu
join_occupied_table:
title: This table is occupied
join_this_table: Join this table
show_the_products: Show me the menu
section: section:
first_section_title: Room first_section_title: Room
+6
View File
@@ -48,6 +48,12 @@ Qrammer::Application.routes.draw do
post '/user/order_selected_products' => 'user#order_selected_products', as: :user_order_selected_products post '/user/order_selected_products' => 'user#order_selected_products', as: :user_order_selected_products
post '/user/move_table' => 'user#move_table', as: :user_move_table post '/user/move_table' => 'user#move_table', as: :user_move_table
get '/user/table_info' => 'user#table_info', as: :user_table_info get '/user/table_info' => 'user#table_info', as: :user_table_info
get '/user/join_occupied_table' => 'user#join_occupied_table', as: :user_join_occupied_table
post '/user/join_occupied_table' => 'user#request_to_join_occupied_table'
post '/user/reject_join_request' => 'user#reject_join_request'
post '/user/approve_join_request' => 'user#approve_join_request'
post '/user/check_table_join_status' => 'user#check_table_join_status'
match '/show_products' => 'dashboard#show_products', as: :user_products match '/show_products' => 'dashboard#show_products', as: :user_products