diff --git a/app/assets/javascripts/supplier/qsupplier.js.coffee b/app/assets/javascripts/supplier/qsupplier.js.coffee index a1f44df7..38238943 100644 --- a/app/assets/javascripts/supplier/qsupplier.js.coffee +++ b/app/assets/javascripts/supplier/qsupplier.js.coffee @@ -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('').append(' ') if list.needs_help # or icon-bell icons_td.append('') if list.needs_payment - row.append($('').text(list.table_number)) + row.append($('').append($('').text(list.table_number))) row.append($('').text(list.section_title)) row.append($('').html(currency(list.total_amount))) td_buttons = $('') @@ -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(''+currency(res.total_amount)+''); ) + 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 = $('').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(currency(order.total_amount))) + foot.append(''+currency(res.total_amount)+'') diff --git a/app/assets/javascripts/user/quser.js.coffee b/app/assets/javascripts/user/quser.js.coffee index d0e226f6..8ff6bed7 100644 --- a/app/assets/javascripts/user/quser.js.coffee +++ b/app/assets/javascripts/user/quser.js.coffee @@ -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 = $('').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(currency(order.total_amount))) - foot.append(''+currency(res.total_amount)+'') + 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 = $('').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(currency(order.total_amount))) + foot.append(''+currency(res.total_amount)+'') 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('

'+category+'

') diff --git a/app/assets/stylesheets/user/structure.css.sass b/app/assets/stylesheets/user/structure.css.sass index ef2f0489..4e1e0723 100644 --- a/app/assets/stylesheets/user/structure.css.sass +++ b/app/assets/stylesheets/user/structure.css.sass @@ -69,5 +69,8 @@ body .order-count-cell white-space: nowrap .page-header + .supplier-name + float: right + margin-right: 8px .table-number float: right diff --git a/app/controllers/supplier_controller.rb b/app/controllers/supplier_controller.rb index d49a0af6..15d15409 100644 --- a/app/controllers/supplier_controller.rb +++ b/app/controllers/supplier_controller.rb @@ -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 diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index 6a431aa2..c4324561 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -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 diff --git a/app/models/list.rb b/app/models/list.rb index 381a4624..2d357651 100644 --- a/app/models/list.rb +++ b/app/models/list.rb @@ -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 diff --git a/app/models/order.rb b/app/models/order.rb index a26b7759..bd95f81d 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -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 diff --git a/app/models/supplier.rb b/app/models/supplier.rb index 461215c6..60cd7518 100644 --- a/app/models/supplier.rb +++ b/app/models/supplier.rb @@ -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 diff --git a/app/views/supplier/active_lists.html.slim b/app/views/supplier/active_lists.html.slim index c8ef2252..90c458d2 100644 --- a/app/views/supplier/active_lists.html.slim +++ b/app/views/supplier/active_lists.html.slim @@ -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 diff --git a/app/views/supplier/active_orders.html.slim b/app/views/supplier/active_orders.html.slim index 1d352e11..ab6cf525 100644 --- a/app/views/supplier/active_orders.html.slim +++ b/app/views/supplier/active_orders.html.slim @@ -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 diff --git a/app/views/supplier/home.html.slim b/app/views/supplier/home.html.slim index 1e855cbc..e63569d4 100644 --- a/app/views/supplier/home.html.slim +++ b/app/views/supplier/home.html.slim @@ -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' diff --git a/app/views/supplier/show_list.html.slim b/app/views/supplier/show_list.html.slim new file mode 100644 index 00000000..30c041c0 --- /dev/null +++ b/app/views/supplier/show_list.html.slim @@ -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}'); + }) diff --git a/app/views/user/active_list.html.slim b/app/views/user/active_list.html.slim index 724b9f67..2143c993 100644 --- a/app/views/user/active_list.html.slim +++ b/app/views/user/active_list.html.slim @@ -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'] diff --git a/app/views/user/history_list.html.slim b/app/views/user/history_list.html.slim index 477444c6..744fd549 100644 --- a/app/views/user/history_list.html.slim +++ b/app/views/user/history_list.html.slim @@ -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}'); + }) diff --git a/app/views/user/list_products.html.slim b/app/views/user/list_products.html.slim index 6dad9164..4c5d5f36 100644 --- a/app/views/user/list_products.html.slim +++ b/app/views/user/list_products.html.slim @@ -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'] diff --git a/app/views/user/list_products_for_table.html.slim b/app/views/user/list_products_for_table.html.slim index 57c52c00..4cbe3173 100644 --- a/app/views/user/list_products_for_table.html.slim +++ b/app/views/user/list_products_for_table.html.slim @@ -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 diff --git a/config/locales/en.yml b/config/locales/en.yml index ee622898..ea2741f6 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 37425457..5c768f54 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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