diff --git a/app/assets/images/supplier/settings/location-balloon.svg b/app/assets/images/supplier/settings/location-balloon.svg index 809b4a2e..b635604f 100644 --- a/app/assets/images/supplier/settings/location-balloon.svg +++ b/app/assets/images/supplier/settings/location-balloon.svg @@ -6,6 +6,7 @@ xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" + xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" version="1.2" @@ -20,10 +21,13 @@ xml:space="preserve" id="svg2" inkscape:version="0.48.2 r9819" - sodipodi:docname="location-balloon.svg">image/svg+xml Klik op de kaart aan te geven - + \ No newline at end of file diff --git a/app/assets/javascripts/supplier/list.js.coffee b/app/assets/javascripts/supplier/list.js.coffee index d61b7dde..80c58ce6 100644 --- a/app/assets/javascripts/supplier/list.js.coffee +++ b/app/assets/javascripts/supplier/list.js.coffee @@ -7,6 +7,7 @@ class List needs_help: -> @attributes.needs_help needs_payment: -> @attributes.needs_payment active: -> @attributes.state == 'active' + table_id: -> @attributes.table_id products: -> @attributes.products || [] supplier_name: -> @attributes.supplier_name created_at: -> @attributes.created_at diff --git a/app/assets/javascripts/supplier/qsupplier.js.coffee b/app/assets/javascripts/supplier/qsupplier.js.coffee index f124c4a8..5fb043f9 100644 --- a/app/assets/javascripts/supplier/qsupplier.js.coffee +++ b/app/assets/javascripts/supplier/qsupplier.js.coffee @@ -7,31 +7,50 @@ root.Qsupplier= body = $('#active-orders-table tbody') order = new Order(e.data) body.append @mustache('#active-order-template', order) + $('.section-table-list-'+order.list_id()).addClass('active_order') else if(e.event == 'list_needs_help') $('#list-needs-help-indicator-'+e.data.id).removeClass('hide') $('#list-is-helped-button-'+e.data.id).removeClass('hide') + $('.section-table-list-'+e.data.id).addClass('needs_help') else if(e.event == 'list_needs_payment') $('#list-needs-payment-indicator-'+e.data.id).removeClass('hide') - else if(e.event == 'list_added') + $('.section-table-list-'+e.data.id).addClass('needs_payment') + else if(e.event == 'list_added') # DEPRICATED now handled by list_update list = new List(e.data) $('#active-lists-table tbody').append @mustache('#active-list-template', list) + # Add classes to section table view + table = $('#section-table-'+list.table_id()) + table.addClass('section-table-list-'+list.id()) + table.addClass('occupied') + table.addClass('needs_help') if list.needs_help() + table.addClass('needs_payment') if list.needs_payment() else if e.event == 'list_update' list = new List(e.data) row = $('#list-row-'+list.id()) content = @mustache('#active-list-template', list) if row.length then row.replaceWith(content) else $('#active-lists-table tbody').append(content) + table = $('#section-table-'+list.table_id()) + if table.length + table.addClass('section-table-list-'+list.id()) + table.addClass('occupied') + table.addClass('needs_help') if list.needs_help() + table.addClass('needs_payment') if list.needs_payment() else if e.event == 'list_closed' $('#list-row-'+e.data.id).remove() $('.of-list-'+e.data.id).remove() + table_list_class = 'section-table-list-'+e.data.id + $('.'+table_list_class).removeClass('occupied needs_help needs_payment active_order').removeClass(table_list_class) else if e.event == 'list_helped' list_id = e.data.id $('#list-needs-help-indicator-'+list_id).addClass('hide') $('#list-is-helped-button-'+list_id).addClass('hide') + $('.section-table-list-'+list_id).removeClass('needs_help') else if e.event == 'order_being_processed' $('#order-in-process-button-'+e.data.id).hide() $('#order-row-'+e.data.id).removeClass('placed').addClass('active') else if e.event == 'order_being_delivered' $('#order-row-'+e.data.id).remove() + $('.section-table-list-'+e.data.list_id).removeClass('active_order') else if e.event == 'list_changed_table' list_row = $('#list-row-'+e.data.list_id) list_row.find('.table_number').text(e.data.table.number).addClass('changed') @@ -39,6 +58,14 @@ root.Qsupplier= order_rows = $('.of-list-'+e.data.list_id) order_rows.find('.table_number').text(e.data.table.number).addClass('changed') order_rows.find('.section_title').text(e.data.section_title) + # Move properties from table in section tables view + window.last_data = e.data + from_table = $('#section-table-'+e.data.from_table_id) + to_table = $('#section-table-'+e.data.table._id) + # Now switch classes + from_class = from_table.attr('class') + from_table.attr('class', to_table.attr('class')) + to_table.attr('class', from_class) console.log(e) @@ -168,10 +195,12 @@ root.Qsupplier= if response.ok then window.location.reload() else @alert(t(response.message)) false mustache: (selector, locals)-> + html = $(selector).html() + return '' unless html locs = $.extend(locals, currency: -> (val)-> currency(Mustache.render(val, this)) ) - Mustache.to_html($(selector).html(), locs) + Mustache.to_html(html, locs) alert: Qwaiter.alert diff --git a/app/controllers/dashboard_controller.rb b/app/controllers/dashboard_controller.rb index 0d0bd088..9651ec6a 100644 --- a/app/controllers/dashboard_controller.rb +++ b/app/controllers/dashboard_controller.rb @@ -7,7 +7,8 @@ class DashboardController < ApplicationController # Testing action def select_qrcode - @tables = Table.all.sample(2) | List.active.map(&:table) + #@tables = Table.all.sample(2) | List.active.map(&:table) + @tables = Supplier.first.tables.sample(2) | List.active.map(&:table) render layout: 'phone' end diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index 4159af00..cf58cc7f 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -48,7 +48,7 @@ class UserController < ApplicationController end # POST /user/create_list {table_id: 1234} - #DEPRICATED + #DEPRICATED, see order_selected_products, this one now handles list creation as well def create_list render nothing: true and return unless current_user.present? @table = Table.find(params[:table_id]) diff --git a/app/models/list.rb b/app/models/list.rb index 11fc9513..737818b5 100644 --- a/app/models/list.rb +++ b/app/models/list.rb @@ -70,6 +70,7 @@ class List list.save user.active_list_id = list.id user.save + # list_added is depricated, now handled by list_update #list.broadcast_supplier list.supplier_id, 'list_added', list.with_info_as_json list end @@ -96,7 +97,12 @@ class List end def self.active_for_table(table_id, options = {}) - database.view(active_by_table_id_view(options.reverse_merge(key: table_id, reduce: false, include_docs: true))) + if table_id.is_a?(Array) + database.view(active_by_table_id_view(options.reverse_merge(keys: table_id, reduce: false, include_docs: true))) + else + table_id = table_id.id if table_id.is_a?(SimplyStored::Couch) + database.view(active_by_table_id_view(options.reverse_merge(key: table_id, reduce: false, include_docs: true))) + end end def self.for_user(user, options = {}) @@ -158,13 +164,14 @@ class List def move_to_table! to_table UserTableMove.create list: self, from_table_id: table_id, to_table: to_table + from_table = self.table_id.try(:dup) self.table = to_table self.section_id = to_table.section_id if save for user_id in user_ids - broadcast_user user_id, 'list_changed_table', list_id: id, table: to_table, section_title: to_table.section.try(:title) + broadcast_user user_id, 'list_changed_table', list_id: id, table: to_table, section_title: to_table.section.try(:title), from_table_id: from_table end - broadcast_supplier supplier_id, 'list_changed_table', list_id: id, table: to_table, section_title: to_table.section.try(:title) + broadcast_supplier supplier_id, 'list_changed_table', list_id: id, table: to_table, section_title: to_table.section.try(:title), from_table_id: from_table end end diff --git a/app/models/order.rb b/app/models/order.rb index 02786f22..a81b13dd 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -33,7 +33,8 @@ class Order # 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)) + supplier_id = supplier.is_a?(SimplyStored::Couch) ? supplier.id : supplier + database.view(active_for_supplier_and_section_view(key: [supplier_id, section_id], reduce: false, include_docs: true)) end def table_number @@ -53,9 +54,9 @@ class Order self.state = 'active' if save for user_id in list.user_ids - broadcast_user user_id, 'order_being_processed', id: id + broadcast_user user_id, 'order_being_processed', id: id, list_id: list_id end - broadcast_supplier supplier_id, 'order_being_processed', id: id + broadcast_supplier supplier_id, 'order_being_processed', id: id, list_id: list_id end end @@ -63,9 +64,9 @@ class Order self.state = 'delivered' if save for user_id in list.user_ids - broadcast_user user_id, 'order_being_delivered', id: id + broadcast_user user_id, 'order_being_delivered', id: id, list_id: list_id end - broadcast_supplier supplier_id, 'order_being_delivered', id: id + broadcast_supplier supplier_id, 'order_being_delivered', id: id, list_id: list_id end end diff --git a/app/models/section.rb b/app/models/section.rb index 1ea80e0a..437c8447 100644 --- a/app/models/section.rb +++ b/app/models/section.rb @@ -29,6 +29,10 @@ class Section @active_lists = List.active_for_section(self.id) end + def active_orders + @active_orders ||= Order.active_for_supplier_and_section(supplier_id, id) + end + def width self.path.last.try(:first).to_f - self.path.first.try(:first).to_f @@ -66,9 +70,11 @@ class Section h[:tables] = [] for table in tables ht = table.as_json + table_list = active_lists.find{|l| l.table_id == table.id} ht[:occupied] = occupied_tables.include?(table) - ht[:needs_help] = ht[:occupied] ? active_lists.find{|l| l.table_id == table.id}.try(:needs_help).present? : false - ht[:needs_payment] = ht[:occupied] ? active_lists.find{|l| l.table_id == table.id}.try(:needs_payment).present? : false + ht[:needs_help] = ht[:occupied] && table_list ? table_list.needs_help? : false + ht[:needs_payment] = ht[:occupied] && table_list ? table_list.needs_payment? : false + ht[:active_order] = ht[:occupied] && table_list ? active_orders.any?{|o| o.list_id == table_list.id } : false h[:tables] << ht end @for_tables_as_json = h diff --git a/app/models/table.rb b/app/models/table.rb index f54ca156..610ede59 100644 --- a/app/models/table.rb +++ b/app/models/table.rb @@ -11,6 +11,7 @@ class Table has_many :lists attr_protected :supplier_id + attr_accessor :active_list_id validates :supplier_id, presence: true #validates :list_id, presence: true @@ -45,6 +46,21 @@ class Table @is_occupied = !self.class.database.view(List.active_by_table_id_view(key: id, reduce: true)).zero? end + def self.enrich_active_list_id(tables) + if tables.is_a?(Array) + lists = List.active_for_table(tables.map(&:id)) + for table in tables + table.active_list_id = lists.find{|l| l.table_id == table.id}.try(:id) + end + tables + else + table = tables + list = List.active_for_table(table).first + table.active_list_id = list.id if list.present? + table + end + end + def active_list @active_list ||= self.class.database.view(List.active_by_table_id_view(key: id, include_docs: true, reduce: false, limit: 1)).try(:first) end diff --git a/app/views/suppliers/sections/tables_view.html.slim b/app/views/suppliers/sections/tables_view.html.slim index 6e7970ad..bf7c9aea 100644 --- a/app/views/suppliers/sections/tables_view.html.slim +++ b/app/views/suppliers/sections/tables_view.html.slim @@ -4,8 +4,10 @@ .span12 .well.section-tables-container.section-tables-active .section-manage-tables.pull-right= link_to content_tag(:span, '', class: 'icon-pencil'), manage_tables_suppliers_section_path(@section), class: 'btn btn-mini' - - for table in @section.tables - .section-table.hide{ id="section-table-#{table.id}" data-position-x=table.position_x data-position-y=table.position_y data-table-id=table.id} + - for table in Table.enrich_active_list_id(@section.tables) + .section-table.hide[ + class="section-table-list-#{table.active_list_id}" id="section-table-#{table.id}" + data-position-x=table.position_x data-position-y=table.position_y data-table-id=table.id] .table-number = link_to table.number, suppliers_table_path(table) .status-icons span.needs_payment.icon-flag @@ -24,5 +26,6 @@ Qsupplier.position_table_in_active_section(active_section_container, $(this), false); }); Qsupplier.update_section_tables_view('#{@section.id}'); - setInterval("Qsupplier.update_section_tables_view('#{@section.id}')", 7500); + //setInterval("Qsupplier.update_section_tables_view('#{@section.id}')", 7500); + Qsupplier.watch_events(); }); diff --git a/spec/models/table_spec.rb b/spec/models/table_spec.rb index 50d3a12f..372aa5db 100644 --- a/spec/models/table_spec.rb +++ b/spec/models/table_spec.rb @@ -3,8 +3,8 @@ require 'spec_helper' describe Table do + let(:supplier){ create(:supplier) } describe 'for_supplier' do - let(:supplier){ create(:supplier) } let(:options){ Hash.new } subject{ Table.for_supplier(supplier, options) } it "should return an empty array by default" do @@ -46,4 +46,29 @@ describe Table do end end + describe 'enrich_active_list_id' do + before do + @table1 = create :table, supplier: supplier, number: 2 + @list = create :list, supplier: supplier, table: @table1, state: 'active' + end + + # False passes can be possible when list is not valid + it 'has created a list' do + @list.id.should be_present + end + + it 'enriches active_list_id using a single object' do + Table.enrich_active_list_id(@table1).active_list_id.should == @list.id + end + + it 'enriches active_list_id using an array' do + Table.enrich_active_list_id([@table1]).first.active_list_id.should == @list.id + end + + it "does not enrich non active lists" do + @list.update_attributes(state: 'closed') + Table.enrich_active_list_id([@table1]).first.active_list_id.should be_nil + end + end + end