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) ->
num = 0.0 if isNaN(num) || num == '' || num == null
'€ ' + parseFloat(num).toFixed(2)
clear_active_list: ->
window.active_products_list = {}
$('#active-order-table').hide()
load_active_orders: () ->
$.get('/supplier/active_orders.json', (res) ->
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)
)
handle_active_list_default_actions: (response)->
if typeof(response) == 'object' && response.table_number
response ||= {}
if response.table_number
$('.table-number').text(response.table_number)
Quser.list_needs_payment_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: ->
needs_help_container = $('#list-needs-help-button')
if needs_help_container.length
@@ -57,10 +87,11 @@ window.Quser=
for product in order.products
order_txts.push(product.name + ' (' + product['number'] + ')')
row.append($('<td></td>').text(order_txts.join(', ')))
row.append($('<td class="currency"></td>').html(Qrammer.currency(order.total_amount)))
foot.append('<tr><td></td><td class="currency"><strong>'+Qrammer.currency(res.total_amount)+'</strong></td></tr>')
row.append($('<td class="currency"></td>').html(currency(order.total_amount)))
foot.append('<tr><td></td><td class="currency"><strong>'+currency(res.total_amount)+'</strong></td></tr>')
)
order_selected_products: (h)->
return if $.isEmptyObject(window.active_products_list)
h ||= {}
for product_id, info of window.active_products_list
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.append('<td>'+info.product.name+'</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() )
row.append($('<td></td>').append(x_btn))
$('#active-order-total').html(Qrammer.currency(total))
$('#active-order-total').html(currency(total))
table.show()
load_active_list_products: ->
@@ -131,36 +162,68 @@ window.Quser=
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>'+Qrammer.currency(product.price)+'</td>')
row.append('<td>'+currency(product.price)+'</td>')
row.append($('<td></td>').append(button)) if include_order_buttons
body.append(row)
)
actions_for_table: (table)->
table = JSON.parse(table) if typeof(table) == 'string'
$.get('/user/table_info.json?table_id='+table.table_id, (res)->
if res.occupied
alert('Table is occupied')
else
if res.current_table_id
if res.other_supplier
#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
window.location = '/user/list_products'
else if res.current_table_id && res.current_table_id != table.table_id
#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.current_table_id != table.table_id
if res.occupied
redirect_to 'user_root', {message: 'table_is_occupied'}
else if res.reserved
redirect_to 'user_root', {message: 'table_is_reserved'}
else if table.closed
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
#$.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')
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) ->
count ||= 1
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].number += count
Quser.build_product_list()
clear_selected_products: ->
window.active_products_list = {}
$('#active-order-table').hide()