implement joining table
This commit is contained in:
Vendored
BIN
Binary file not shown.
Vendored
BIN
Binary file not shown.
Binary file not shown.
|
After Width: | Height: | Size: 723 B |
@@ -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);
|
||||
}
|
||||
+82
-19
@@ -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(' ').append(order_product_count).append(' ').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()
|
||||
@@ -1,10 +1,12 @@
|
||||
class UserController < ApplicationController
|
||||
before_filter :authenticate_user!
|
||||
layout 'phone'
|
||||
|
||||
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?
|
||||
flash.now[:notice] = t("messages.#{params[:message]}") if params[:message].present? && params[:message] =~ /^\w+$/
|
||||
render layout: 'phone'
|
||||
end
|
||||
|
||||
@@ -30,7 +32,7 @@ class UserController < ApplicationController
|
||||
def table_info
|
||||
@table = Table.find(params[:table_id])
|
||||
res = {}
|
||||
res[:ocupied] = @table.occupied?
|
||||
res[:occupied] = @table.occupied?
|
||||
res[:reserved] = @table.reserved?
|
||||
res[:supplier_closed] = @table.supplier.closed?
|
||||
if list.present?
|
||||
@@ -62,6 +64,62 @@ class UserController < ApplicationController
|
||||
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
|
||||
@table = Table.find(params[:table_id])
|
||||
respond_to do |format|
|
||||
@@ -115,6 +173,7 @@ class UserController < ApplicationController
|
||||
# GET /user/list_info.json
|
||||
# Information about the currently active list
|
||||
# Fast version to verify wether the is is still currently active
|
||||
# for handle_active_list
|
||||
def list_info
|
||||
respond_to do |format|
|
||||
format.json do
|
||||
@@ -122,8 +181,17 @@ class UserController < ApplicationController
|
||||
current_user.list_is_closed!
|
||||
render json: {list_active: false}
|
||||
return
|
||||
else
|
||||
render json: list.as_json.merge(list_active: list.active? )
|
||||
else
|
||||
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
|
||||
|
||||
@@ -5,6 +5,8 @@ class List
|
||||
property :needs_help, type: :boolean, default: false
|
||||
property :needs_payment, type: :boolean, default: false
|
||||
property :closed_at, type: Time
|
||||
property :join_requests, type: Array, default: []
|
||||
|
||||
has_many :orders, dependent: :destroy
|
||||
belongs_to :table
|
||||
belongs_to :supplier
|
||||
@@ -16,6 +18,12 @@ class List
|
||||
|
||||
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
|
||||
return if user.has_active_list?
|
||||
list = new table: table, supplier_id: table.supplier_id, section_id: table.section_id
|
||||
|
||||
+5
-7
@@ -16,15 +16,13 @@ class Table
|
||||
validates :number, numericality: {greater_than: 0}
|
||||
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?
|
||||
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
|
||||
|
||||
def reserved?
|
||||
|
||||
@@ -56,5 +56,5 @@ html lang="en"
|
||||
Javascripts
|
||||
\==================================================
|
||||
/! Placed at the end of the document so the pages load faster
|
||||
= javascript_include_tag "application"
|
||||
= javascript_include_tag "user/application"
|
||||
= 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
|
||||
@@ -18,7 +18,7 @@ table#active-order-table.table.table-striped.hide
|
||||
td colspan=2
|
||||
button class="btn btn-primary" onClick="Quser.handle_active_list(function(){Quser.order_selected_products()})" Bestellen
|
||||
|
|
||||
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
|
||||
strong#active-order-total
|
||||
td
|
||||
|
||||
@@ -16,7 +16,7 @@ table#active-order-table.table.table-striped.hide
|
||||
td colspan=2
|
||||
button class="btn btn-primary" onClick="Quser.order_selected_products({table_id: '#{@table.id}'})" Bestellen
|
||||
|
|
||||
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
|
||||
strong#active-order-total
|
||||
td
|
||||
|
||||
Reference in New Issue
Block a user