root = exports ? this
root.Qrammer =
alert: (msg) ->
alert(msg)
confirm: (callback, content) ->
content ||= 'Are you sure?'
wrapper = $('
')
callback_wrapper = ->
wrapper.modal('hide')
callback()
header = $('')
.append('')
.append('Confirm
').appendTo(wrapper)
body = $('').append(''+content+'
').appendTo(wrapper)
footer = $('')
.append($('Close').click(-> wrapper.modal('hide')))
.append($('Yes').click(callback_wrapper))
.appendTo(wrapper)
wrapper.modal()
currency: (num) ->
num = 0.0 if isNaN(num) || num == '' || num == null
'€ ' + parseFloat(num).toFixed(2)
add_product: (product) ->
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 += 1
Qrammer.build_product_list()
build_product_list: ->
table = $('#active-order-table')
tbody = table.find('tbody')
tbody = $('').appendTo(table) unless tbody.length
tbody.find('tr').remove()
total = 0.0
for product_id, info of window.active_products_list
total += info.product.price * info.number
row = $('
').attr('id', 'active-order-row-'+product_id).appendTo(tbody)
row.append(''+info.product.name+' | ')
row.append(''+info.number+' | ')
row.append(''+Qrammer.currency(info.product.price * info.number)+' | ')
x_btn = $('').click(-> delete(window.active_products_list[product_id]) && Qrammer.build_product_list() )
row.append($(' | ').append(x_btn))
$('#active-order-total').html(Qrammer.currency(total))
table.show()
clear_active_list: ->
window.active_products_list = {}
$('#active-order-table').hide()
order_active_products_list: (post_uri)->
h = {list_id: active_list_id}
for product_id, info of window.active_products_list
h['products['+product_id+']'] = info.number
$.post(post_uri, h, ((res) -> Qrammer.handle_response(res)), 'json')
handle_response: (res) ->
if(typeof(res) == 'string')
return unless res.length
if res[0] == '{'
res = JSON.parse(res)
else
eval(res)
return
window.location = '/' if res['message'] && !res['ok']
window.location = '/user/active_list' if res['ok']
load_active_orders: () ->
$.get('/supplier/active_orders.json', (res) ->
body = $('#active-orders-table tbody')
body.find('tr').remove()
foot = $('#active-orders-table tfoot')
if(!res.orders && !res.orders.length)
alert('No orders in list');
return;
for order in res.orders
order_txts = []
row = $('
').appendTo(body)
process_btn = $('')
process_callback = ( (ord) ->
->
my_btn = $(this)
$.post('/supplier/mark_order_in_process', {order_id: ord.id}, (res)-> my_btn.remove())
)(order)
process_btn.click(process_callback)
delivered_btn = $('')
delivered_callback = ( (ord, r) ->
->
my_btn = $(this)
$.post('/supplier/order_is_delivered', {order_id: ord.id}, (res)-> r.slideUp('slow'))
)(order, row)
delivered_btn.click(delivered_callback)
for product in order.products
order_txts.push(product.name + ' (' + product['number'] + ')')
row.append($(' | ').text(order_txts.join(', ')))
row.append($(' | ').text(order.table_number))
row.append($(' | ').html(Qrammer.currency(order.total_amount)))
td_buttons = $(' | ')
td_buttons.append(process_btn).append(' ') if order.state == 'placed'
td_buttons.append(delivered_btn)
row.append(td_buttons)
#foot.append(' | '+Qrammer.currency(res.total_amount)+' |
');
)
handle_active_user_list: (callback) ->
$.get('/user/list_info.json', (res) ->
if !res.list_active
window.location = '/phone_home?list_closed=true'
return
window.active_list = res
callback.call() if callback
Qrammer.handle_active_user_list_default_actions()
)
handle_active_user_list_default_actions: ->
Qrammer.list_needs_payment_default_action()
Qrammer.list_needs_help_default_action()
list_needs_help_default_action: ->
needs_help_container = $('#list-needs-help-button')
if needs_help_container.length
if window.active_list.needs_help
needs_help_container.html('')
else
needs_help_container.html($('').click(Qrammer.list_needs_help)) #TODO TEXT
list_needs_help: ->
return unless window.active_list && !window.active_list.needs_help
$.post('/user/needs_help.json', (res) -> window.active_list = res; Qrammer.list_needs_help_default_action())
list_needs_payment_default_action: ->
needs_payment_container = $('#list-needs-payment-button')
if needs_payment_container.length
if window.active_list.needs_payment
needs_payment_container.html('')
else
needs_payment_container.html($('').click(Qrammer.list_needs_payment)) #TODO TEXT
list_needs_payment: ->
return unless window.active_list && !window.active_list.needs_payment
$.post('/user/list_needs_payment.json', (res) -> window.active_list = res; Qrammer.list_needs_payment_default_action())
load_active_lists: () ->
$.get('/supplier/active_lists.json', (res) ->
body = $('#active-lists-table tbody')
body.find('tr').remove()
foot = $('#active-lists-table tfoot')
for list in res.lists
order_txts = []
row = $('
').appendTo(body)
close_btn = $('')
close_callback = ( (lst, r) ->
->
my_btn = $(this)
$.post('/supplier/close_list', {list_id: list._id}, (res)-> r.slideUp('slow'))
)(list, row)
close_btn.click(close_callback)
needs_help_btn = $('')
needs_help_callback = ( (lst, r) ->
->
my_btn = $(this)
$.post('/supplier/mark_list_as_helped', {list_id: list._id}, (res)-> my_btn.remove() )
)(list, row)
needs_help_btn.click(needs_help_callback)
icons_td = $(' | ').appendTo(row)
icons_td.append('').append(' ') if list.needs_help # or icon-bell
icons_td.append('') if list.needs_payment
row.append($(' | ').text(list.table_number))
row.append($(' | ').html(Qrammer.currency(list.total_amount)))
td_buttons = $(' | ')
td_buttons.append(needs_help_btn).append(' ') if list.needs_help
td_buttons.append(close_btn)
row.append(td_buttons)
#foot.append(' | '+Qrammer.currency(res.total_amount)+' |
');
)
active_user_list: (list_id) ->
$.get('/user/active_list.json', (res) ->
window.active_list = res
unless res.list_active
window.location = '/user/list_history/'+res._id + '?list_closed=true'
return
Qrammer.handle_active_user_list_default_actions()
body = $('#active-list-table tbody')
foot = $('#active-list-table tfoot')
body.find('tr').remove()
foot.find('tr').remove()
if !res.orders && !res.orders.length
alert('No orders in list')
return
for order in res.orders
order_txts = []
row = $('
').appendTo(body)
row.addClass(order.state)
#if(order.state == 'placed') row.addClass('info');
#if(order.state == 'delivered') row.addClass('success');
row.addClass('error') if order.state == 'cancelled'
for product in order.products
order_txts.push(product.name + ' (' + product['number'] + ')')
row.append($(' | ').text(order_txts.join(', ')))
row.append($(' | ').html(Qrammer.currency(order.total_amount)))
foot.append(' | '+Qrammer.currency(res.total_amount)+' |
')
)
build_product_list_as_modal: ->
wrapper = $('')
callback_wrapper = ->
wrapper.modal('hide')
callback()
header = $('')
.append('')
.append('Product list
').appendTo(wrapper)
body = $('')
table = $('').appendTo(body)
tbody = $('').appendTo(table)
for product_id, info of window.active_products_list
row = $('
').appendTo(tbody)
row.append(''+info.product.name+' | ')
row.append(''+info.number+' | ')
row.append(''+Qrammer.currency(info.product.price * info.number)+' | ')
body.appendTo(wrapper)
footer = $('')
.append($('Close').click(-> wrapper.modal('hide')))
.append($('Yes').click(callback_wrapper))
.appendTo(wrapper)
wrapper.modal()
jQuery.ajaxSetup
'beforeSend': (xhr) ->
xhr.setRequestHeader("Accept", "text/javascript")