improvements

This commit is contained in:
2012-09-06 14:49:22 +02:00
parent 27d7fd304e
commit 161da7a82d
18 changed files with 209 additions and 81 deletions
@@ -1,4 +1,5 @@
root = exports ? this
data_host = ''
root.Qsupplier=
move_table_to_active_section: (table_id)->
table_container = $('#section-table-'+table_id)
@@ -56,7 +57,7 @@ root.Qsupplier=
)
load_active_lists: () ->
$.get('/supplier/active_lists.json', (res) ->
$.get('/supplier/active_lists.json?section_id='+($('#current_section_selector').val() || ''), (res) ->
body = $('#active-lists-table tbody')
body.find('tr').remove()
foot = $('#active-lists-table tfoot')
@@ -84,7 +85,7 @@ root.Qsupplier=
icons_td.append('<i class="icon-hand-up"></i>').append('&nbsp;') if list.needs_help # or icon-bell
icons_td.append('<i class="icon-check"></i>') if list.needs_payment
row.append($('<td class="numeric"></td>').text(list.table_number))
row.append($('<td class="numeric"></td>').append($('<a href="/supplier/lists/'+list._id+'"></a>').text(list.table_number)))
row.append($('<td></td>').text(list.section_title))
row.append($('<td class="currency"></td>').html(currency(list.total_amount)))
td_buttons = $('<td class="actions"></td>')
@@ -95,7 +96,7 @@ root.Qsupplier=
)
load_active_orders: () ->
$.get('/supplier/active_orders.json', (res) ->
$.get('/supplier/active_orders.json?section_id='+($('#current_section_selector').val() || ''), (res) ->
body = $('#active-orders-table tbody')
body.find('tr').remove()
foot = $('#active-orders-table tfoot')
@@ -132,3 +133,27 @@ root.Qsupplier=
row.append(td_buttons)
#foot.append('<tr><td></td><td class="currency"><strong>'+currency(res.total_amount)+'</strong></td></tr>');
)
load_list: (list_id) ->
$.get(data_host + '/supplier/lists/'+list_id+'.json', (res) ->
body = $('#list-table tbody')
foot = $('#list-table tfoot')
Qsupplier.build_list_table(body, foot, res)
)
build_list_table: (body, foot, res) ->
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 = $('<tr></tr>').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($('<td></td>').text(order_txts.join(', ')))
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>')
+30 -17
View File
@@ -12,6 +12,8 @@ window.Quser=
response ||= {}
if response.table_number
$('.table-number').text(response.table_number)
if response.supplier_name
$('.supplier-name').text(response.supplier_name)
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
@@ -72,24 +74,32 @@ window.Quser=
Quser.handle_active_list_default_actions(res)
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 = $('<tr></tr>').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($('<td></td>').text(order_txts.join(', ')))
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>')
Quser.build_list_table(body, foot, res)
)
load_history_list: (list_id) ->
$.get('/user/list_history/'+list_id+'.json', (res) ->
body = $('#history-list-table tbody')
foot = $('#history-list-table tfoot')
Quser.build_list_table(body, foot, res)
)
build_list_table: (body, foot, res) ->
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 = $('<tr></tr>').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($('<td></td>').text(order_txts.join(', ')))
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 ||= {}
@@ -133,6 +143,9 @@ window.Quser=
if res.table_number
$('.table-number').text(res.table_number)
delete(res['table_number'])
if res.supplier_name
$('.supplier-name').text(res.supplier_name)
delete(res['supplier_name'])
body.find('tr').remove()
for category, products of res
body.append('<tr><td colspan="4"><h4>'+category+'<h4></td></tr>')
@@ -69,5 +69,8 @@ body
.order-count-cell
white-space: nowrap
.page-header
.supplier-name
float: right
margin-right: 8px
.table-number
float: right
+13 -2
View File
@@ -39,7 +39,7 @@ class SupplierController < ApplicationController
h = @supplier.as_json
h[:orders] = []
list_total = 0.0
for order in @supplier.active_orders
for order in @supplier.active_orders(section_id: params[:section_id].presence)
ho = {products: []}
order_total = 0.0
for product_order in order.product_orders
@@ -70,7 +70,7 @@ class SupplierController < ApplicationController
h = @supplier.as_json
h[:lists] = []
grand_total = 0.0
for list in @supplier.active_lists
for list in @supplier.active_lists(section_id: params[:section_id].presence)
hl = list.as_json
hl[:total_amount] = list.orders.inject(0.0){|sum, o| sum + o.product_orders.inject(0.0){|s, po| s + (po.amount * po.price).round(2)}}.round(2)
grand_total += hl[:total_amount]
@@ -111,4 +111,15 @@ class SupplierController < ApplicationController
@order.is_delivered!
render nothing: true
end
# GET /supplier/lists/1
def show_list
@list = List.find_by_supplier_id_and_id(current_supplier.id, params[:list_id])
respond_to do |format|
format.html {}
format.json do
render json: @list.with_orders_as_json
end
end
end
end
+12 -41
View File
@@ -58,7 +58,7 @@ class UserController < ApplicationController
products = list.supplier.products
products.include_relation(:product_categories)
products.sort_by!{|p| p.product_category.try(:position) || 90000}
h = products.inject({table_number: list.table_number}){|h, p| n = p.product_category.try(:name) || 'other'; h[n] ||= []; h[n] << p; h}
h = products.inject({table_number: list.table_number, supplier_name: @supplier.name}){|h, p| n = p.product_category.try(:name) || 'other'; h[n] ||= []; h[n] << p; h}
render json: h
end
end
@@ -130,7 +130,7 @@ class UserController < ApplicationController
products = @table.supplier.products
products.include_relation(:product_categories)
products.sort_by!{|p| p.product_category.try(:position) || 90000}
h = products.inject({table_number: @table.number}){|h, p| n = p.product_category.try(:name) || 'other'; h[n] ||= []; h[n] << p; h}
h = products.inject({table_number: @table.number, supplier_name: @table.supplier.name}){|h, p| n = p.product_category.try(:name) || 'other'; h[n] ||= []; h[n] << p; h}
render json: h
end
end
@@ -146,35 +146,7 @@ class UserController < ApplicationController
render layout: 'phone'
end
format.json do
@list = list
@list.orders.include_relations(product_orders: :product)
h = @list.as_json
# Handle join requests
if @list.join_requests.any?
h[:join_requests] = []
for user in CouchPotato.database.load_document(@list.join_requests)
h[:join_requests] << {user_id: user.id, user_email: user.email}
end
end
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
render json: list.with_orders_and_join_requests_as_json.merge(supplier_name: list.supplier.name)
end
end
end
@@ -191,15 +163,7 @@ class UserController < ApplicationController
render json: {list_active: false}
return
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
list_obj = list.as_json.merge(list_active: list.active? ).merge(list.join_requests_as_json)
render json: list_obj
end
end
@@ -248,7 +212,14 @@ class UserController < ApplicationController
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'
respond_to do |format|
format.html do
render layout: 'phone'
end
format.json do
render json: list.with_orders_as_json
end
end
end
+44 -5
View File
@@ -24,6 +24,7 @@ class List
}
}|, reduce_function: '_sum'
# Create, a list given a table and a user
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
@@ -45,10 +46,6 @@ class List
save
end
def supplier
table.supplier
end
def table_number
@table_number ||= table.number
end
@@ -60,7 +57,7 @@ class List
def place_order(user, products)
return false unless products.any?
return false unless user
@order = Order.create list: self, supplier: supplier, user: user
@order = Order.create list: self, supplier: supplier, user: user, section_id: section_id
return unless @order.id
loaded_products = self.class.database.load_document products.keys
products.each do |product_id, number|
@@ -81,4 +78,46 @@ class List
def as_json
super.merge(table_number: table_number)
end
def with_orders_as_json
return @with_orders_as_json if @with_orders_as_json.present?
orders.include_relations(product_orders: :product)
h = as_json
h[:orders] = []
h[:list_active] = active?
list_total = 0.0
for order in 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)
@with_orders_as_json = h
end
def with_orders_and_join_requests_as_json
return @with_orders_and_join_requests_as_json if @with_orders_and_join_requests_as_json.present?
@with_orders_and_join_requests_as_json = with_orders_as_json.merge(join_requests_as_json)
end
# Return a join requests object in the form of:
# {join_request: [{user_id: '1saf3...', user_email: 'info@qwaiter.com'}, [....]]}
def join_requests_as_json
return @join_requests_as_json if @join_requests_as_json.present?
h = {join_requests: []}
# Handle join requests
if join_requests.any?
for user in self.class.database.load_document(join_requests)
h[:join_requests] << {user_id: user.id, user_email: user.email}
end
end
@join_requests_as_json = h
end
end
+12
View File
@@ -18,11 +18,23 @@ class Order
emit(doc.supplier_id, 1);
}
}], reduce_function: '_sum'
view :active_for_supplier_and_section_view, type: :custom, map_function: %[function(doc){
if(doc.ruby_class == 'Order' && (doc.state == 'placed' || doc.state == 'active')){
emit([doc.supplier_id, doc.section_id], 1);
}
}], reduce_function: '_sum'
view :by_supplier_id_and_id, key: [:supplier_id, :_id]
# Return all currently active orders for a given supplier
def self.active_for_supplier(supplier_id)
database.view(active_for_supplier_view(key: supplier_id, reduce: false, include_docs: true))
end
# Return all currently active orders for a given section
def self.active_for_supplier_and_section(supplier, section_id)
database.view(active_for_supplier_and_section_view(key: [supplier.id, section_id], reduce: false, include_docs: true))
end
def table_number
list.table.number
end
+9 -7
View File
@@ -22,24 +22,26 @@ class Supplier
after_create :add_section_on_create
def active_orders
return @active_orders if @active_orders
@active_orders = Order.active_for_supplier(id)
def active_orders(options = {})
return @active_orders if @active_orders && @active_orders_options == options
@active_orders_options = options
@active_orders = options[:section_id] ? Order.active_for_supplier_and_section(self, options[:section_id]) : Order.active_for_supplier(id)
@active_orders.include_relation(list: {table: :section}, product_orders: :order)
@active_orders
end
def active_lists
def active_lists(options = {})
return @active_lists if @active_lists
@tables ||= active_tables
@tables ||= active_tables(options)
@tables.include_relation(:lists)
@active_lists = @tables.map(&:lists).flatten.select(&:active?)
@active_lists.include_relations(table: :section, orders: {product_orders: :product})
end
def active_tables
tables
# Return the currently active tables for the supplier
def active_tables(options = {})
options[:section_id].present? ? tables.select{|t| t.section_id == options[:section_id]} : tables
end
def non_placed_tables
+2 -1
View File
@@ -1,4 +1,5 @@
.page-header= title t('supplier.active_lists.title', lists: List.model_name.human_plural)
.page-header
h3 = t('supplier.active_lists.title', lists: List.model_name.human_plural)
.well
table#active-lists-table.table
thead
+2 -1
View File
@@ -1,4 +1,5 @@
.page-header= title 'Active orders'
.page-header
h3 = t('supplier.active_orders.title', orders: Order.model_name.human_plural)
.well
table#active-orders-table.table
thead
+2
View File
@@ -1,2 +1,4 @@
.pull-right
= select_tag('current_section_selector', options_for_select([[current_supplier.name, '']] + current_supplier.sections.map{|s| ["- #{s.title}", s.id]}, params[:section_id]))
= render template: 'supplier/active_lists'
= render template: 'supplier/active_orders'
+19
View File
@@ -0,0 +1,19 @@
.page-header= title t('supplier.lists.show.title', list: List.model_name.human)
dl.dl-horizontal
dt= List.human_attribute_name(:created_at)
dd= l @list.created_at, format: :short
.well
table#list-table.table
thead
tr
th= Order.model_name.human
th.currency= Product.human_attribute_name(:price)
tbody
tr
td colspan=2 = slider_image
tfoot
- content_for :footer do
javascript:
jQuery(function(){
Qsupplier.load_list('#{@list.id}');
})
+1
View File
@@ -1,5 +1,6 @@
.page-header
.table-number
.supplier-name
h4= t('user.active_list.title', list: List.model_name.human)
.form-actions
= link_to t('helpers.links.place_order'), user_list_products_path, class: ['btn btn-primary']
+19 -3
View File
@@ -1,3 +1,19 @@
.page-header= title 'User list history'
p Todo
= render template: 'lists/show'
.page-header= title t('user.history_list.title', list: List.model_name.human)
dl.dl-horizontal
dt= List.human_attribute_name(:created_at)
dd= l @list.created_at, format: :short
.well
table#history-list-table.table
thead
tr
th= Order.model_name.human
th.currency= Product.human_attribute_name(:price)
tbody
tr
td colspan=2 = slider_image
tfoot
- content_for :footer do
javascript:
jQuery(function(){
Quser.load_history_list('#{@list.id}');
})
+1
View File
@@ -1,5 +1,6 @@
.page-header
.table-number
.supplier-name
h4= t('user.show_products.title', products: Product.model_name.human_plural)
.form-actions
= link_to t('helpers.links.show_active_list', list: List.model_name.human), user_active_list_path, class: ['btn btn']
@@ -1,5 +1,6 @@
.page-header
.table-number= @table.number
.supplier-name= @table.supplier.name
h4= t('user.show_products.title', products: Product.model_name.human_plural)
.well
table#products-table.table.table-hover
+8
View File
@@ -60,6 +60,8 @@ en:
attributes:
product:
price: Price
list:
created_at: Created
supplier:
menu:
active_lists: Active %{lists}
@@ -67,6 +69,7 @@ en:
title: Active %{lists}
price: Price
active_orders:
title: Active %{orders}
price: Price
close: Close the shop
you_are_currently_closed_alert: 'You are currently closed and not able to take orders'
@@ -75,11 +78,16 @@ en:
tables:
qr_codes:
link: Qr codes sheet
lists:
show:
title: Show %{list}
user:
active_list:
title: Active %{list}
needs_payment: Check please!
history_list:
title: Previous %{list}
show_products:
# The title gets products: Product.model_name.human_plural that can be used: e.g.: Showing %{products}
title: Menu
+3 -1
View File
@@ -18,10 +18,11 @@ Qrammer::Application.routes.draw do
resources :product_categories
end
# SUPPLIER
get '/supplier' => 'supplier#home', as: :supplier_root
get '/supplier/active_orders' => 'supplier#active_orders', as: :supplier_active_orders
get '/supplier/active_lists' => 'supplier#active_lists', as: :supplier_active_lists
get 'supplier/lists/:list_id' => 'supplier#show_list', as: :supplier_show_list
get '/supplier/lists/:list_id' => 'supplier#show_list', as: :supplier_show_list
post '/supplier/close_list' => 'supplier#close_list', as: :supplier_close_list
post '/supplier/mark_list_as_helped' => 'supplier#mark_list_as_helped', as: :supplier_mark_list_as_helped
@@ -35,6 +36,7 @@ Qrammer::Application.routes.draw do
get '/supplier/settings' => 'supplier#edit', as: :supplier_settings
match '/supplier/settings' => 'supplier#update', as: :supplier_update_settings, via: [:put, :post]
# USER
match '/user' => 'user#home', as: :user_root
get '/user/active_list(.:format)' => 'user#active_list', as: :user_active_list
get '/user/list_info' => 'user#list_info', as: :user_list_info