From 4765608e2d3e0a423315003055b66c56e740b9bf Mon Sep 17 00:00:00 2001 From: Benjamin ter Kuile Date: Fri, 24 Aug 2012 16:20:03 +0200 Subject: [PATCH] backup push --- app/assets/javascripts/qrammer.js.coffee | 57 +++++++++++++++---- .../stylesheets/phone/structure.css.sass | 14 +++-- app/assets/stylesheets/structure.css.sass | 8 +++ app/controllers/application_controller.rb | 2 +- app/controllers/dashboard_controller.rb | 17 +++++- app/controllers/lists_controller.rb | 14 ++++- app/controllers/suppliers_controller.rb | 7 ++- app/models/list.rb | 6 +- app/models/product_order.rb | 1 + app/views/dashboard/show_products.html.slim | 47 ++++++++------- .../dashboard/view_active_list.html.slim | 11 ++-- app/views/lists/_form.html.slim | 8 +++ app/views/lists/index.html.slim | 6 +- app/views/lists/show.html.slim | 4 ++ app/views/suppliers/active_lists.html.slim | 3 +- config/locales/en.yml | 14 ++++- config/routes.rb | 9 ++- 17 files changed, 172 insertions(+), 56 deletions(-) diff --git a/app/assets/javascripts/qrammer.js.coffee b/app/assets/javascripts/qrammer.js.coffee index c95afdc1..ff7bf860 100644 --- a/app/assets/javascripts/qrammer.js.coffee +++ b/app/assets/javascripts/qrammer.js.coffee @@ -21,9 +21,9 @@ root.Qrammer = num = 0.0 if isNaN(num) || num == '' || num == null '€ ' + parseFloat(num).toFixed(2) add_product: (product) -> - window.active_list = {} unless window.active_list - window.active_list[product._id] = {product: product, number: 0} unless window.active_list[product._id] - window.active_list[product._id].number += 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 += 1 Qrammer.build_product_list() build_product_list: -> table = $('#active-order-table') @@ -31,22 +31,22 @@ root.Qrammer = tbody = $('').appendTo(table) unless tbody.length tbody.find('tr').remove() total = 0.0 - for product_id, info of window.active_list + 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_list[product_id]) && Qrammer.build_product_list() ) + 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_list = {} + window.active_products_list = {} $('#active-order-table').hide() - order_active_list: (post_uri)-> + order_active_products_list: (post_uri)-> h = {list_id: active_list_id} - for product_id, info of window.active_list + 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) -> @@ -96,6 +96,29 @@ root.Qrammer = 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_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('Help requested') + 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('/active_user_list_needs_help.json', (res) -> window.active_list = res; Qrammer.list_needs_help_default_action()) + load_active_lists: (supplier_id) -> $.get('/suppliers/'+supplier_id+'/active_lists.json', (res) -> body = $('#active-lists-table tbody') @@ -104,21 +127,31 @@ root.Qrammer = for list in res.lists order_txts = [] row = $('').appendTo(body) - close_btn = $('') + close_btn = $('') close_callback = ( (lst, r) -> -> my_btn = $(this) $.post('/lists/'+lst._id+'/is_closed', {}, (res)-> r.slideUp('slow')) )(list, row) close_btn.click(close_callback) + + needs_help_btn = $('') + needs_help_callback = ( (lst, r) -> + -> + my_btn = $(this) + $.post('/lists/'+lst._id+'/is_helped', {}, (res)-> my_btn.remove() ) + )(list, row) + needs_help_btn.click(needs_help_callback) + icons_td = $('').appendTo(row) - icons_td.append('
  • ') if list.need_help # or icon-bell - icons_td.append('
  • ') if list.needs_payment + 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)+''); @@ -158,7 +191,7 @@ root.Qrammer = body = $('') table = $('
    ').appendTo(body) tbody = $('').appendTo(table) - for product_id, info of window.active_list + for product_id, info of window.active_products_list row = $('').appendTo(tbody) row.append(''+info.product.name+'') row.append(''+info.number+'') diff --git a/app/assets/stylesheets/phone/structure.css.sass b/app/assets/stylesheets/phone/structure.css.sass index 59ee6ae5..e2a448ee 100644 --- a/app/assets/stylesheets/phone/structure.css.sass +++ b/app/assets/stylesheets/phone/structure.css.sass @@ -1,7 +1,13 @@ +$side-spacing: 5px body - padding-left: 0px - padding-right: 0px + padding-left: $side-spacing + padding-right: $side-spacing //padding-top: 50px .navbar-fixed-top - margin-left: 0px - margin-right: 0px + margin-left: -$side-spacing + margin-right: -$side-spacing + margin-bottom: 3px + .page-header + margin-top: 4px + margin-bottom: 6px + padding-bottom: 0 diff --git a/app/assets/stylesheets/structure.css.sass b/app/assets/stylesheets/structure.css.sass index b3d14642..2529a79d 100644 --- a/app/assets/stylesheets/structure.css.sass +++ b/app/assets/stylesheets/structure.css.sass @@ -5,6 +5,7 @@ table text-align: right tbody td + &.status-icons &.currency text-align: right &.numeric @@ -61,3 +62,10 @@ table top: 100px width: 880px height: 590px + +#list-needs-help-button + button + margin-left: 5px +#list-needs-payment-button + button + margin-left: 5px diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 450d8692..2c256b8f 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -8,7 +8,7 @@ class ApplicationController < ActionController::Base if session[:active_list_id] unless active_list.active? session[:active_list_id] = nil - redirect_to root_path, alert: t('messages.the_list_has_been_closed') + redirect_to phone_root_path, alert: t('messages.the_list_has_been_closed', list: List.model_name.human) end end diff --git a/app/controllers/dashboard_controller.rb b/app/controllers/dashboard_controller.rb index b128434f..36688fb3 100644 --- a/app/controllers/dashboard_controller.rb +++ b/app/controllers/dashboard_controller.rb @@ -5,6 +5,7 @@ class DashboardController < ApplicationController end def phone_home + flash.now[:notice] = t('messages.the_list_has_been_closed', list: List.model_name.human) if params[:list_closed].present? render layout: 'phone' end @@ -32,7 +33,7 @@ class DashboardController < ApplicationController render layout: 'phone' end - def order_active_list + def order_active_products_list respond_to do |format| format.html do redirect_to(root_path, alert: t('messages.cannot_order_without_list_id')) and return if params[:list_id].blank? @@ -62,7 +63,7 @@ class DashboardController < ApplicationController render layout: 'phone' end - def list_info + def user_list_info respond_to do |format| format.json do render json: {list_active: false} and return unless session[:active_list_id].present? @@ -78,4 +79,16 @@ class DashboardController < ApplicationController def supplier_lists redirect_to active_lists_supplier_path(Supplier.first) end + + # POST /active_user_list_needs_help + def active_user_list_needs_help + respond_to do |format| + format.json do + render json: {list_active: false} and return unless session[:active_list_id].present? + active_list.needs_help = true + active_list.save + render json: active_list.as_json.merge(list_active: true) + end + end + end end diff --git a/app/controllers/lists_controller.rb b/app/controllers/lists_controller.rb index 47570537..0e57e00b 100644 --- a/app/controllers/lists_controller.rb +++ b/app/controllers/lists_controller.rb @@ -94,8 +94,8 @@ class ListsController < ApplicationController ho = {products: []} order_total = 0.0 for product_order in order.product_orders - order_total += (product_order.amount * product_order.product.price).round(2) - ho[:products] << {name: product_order.product.name, id: product_order.product_id, number: product_order.amount, price: product_order.product.price} + 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 @@ -110,12 +110,20 @@ class ListsController < ApplicationController end - # POST /orders/1/is_delivered + # POST /orders/1/is_closed def is_closed @list = List.find(params[:id]) @list.close! render nothing: true end + + # POST /orders/1/is_helped + def is_helped + @list = List.find(params[:id]) + @list.needs_help = false + @list.save + render nothing: true + end private def set_relation_options diff --git a/app/controllers/suppliers_controller.rb b/app/controllers/suppliers_controller.rb index 83b93934..0b560ff5 100644 --- a/app/controllers/suppliers_controller.rb +++ b/app/controllers/suppliers_controller.rb @@ -113,8 +113,8 @@ class SuppliersController < ApplicationController ho = {products: []} order_total = 0.0 for product_order in order.product_orders - order_total += (product_order.amount * product_order.product.price).round(2) - ho[:products] << {name: product_order.product.name, id: product_order.product_id, number: product_order.amount, price: product_order.product.price} + 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 @@ -141,7 +141,7 @@ class SuppliersController < ApplicationController grand_total = 0.0 for list in @supplier.active_lists 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.product.price).round(2)}}.round(2) + 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] h[:lists] << hl end @@ -150,6 +150,7 @@ class SuppliersController < ApplicationController end end end + private def set_relation_options diff --git a/app/models/list.rb b/app/models/list.rb index 17e35f52..dad1a17d 100644 --- a/app/models/list.rb +++ b/app/models/list.rb @@ -2,7 +2,7 @@ class List include SimplyStored::Couch property :state, default: 'active' # active, #closed - property :need_help, type: :boolean, default: false + property :needs_help, type: :boolean, default: false property :needs_payment, type: :boolean, default: false property :closed_at, type: Time has_many :orders, dependent: :destroy @@ -34,9 +34,11 @@ class List return unless products.any? @order = Order.create list: self, supplier: supplier return unless @order.id + loaded_products = self.class.database.load_document products.keys products.each do |product_id, number| number = number.to_i - ProductOrder.create order: @order, product_id: product_id, amount: number if number > 0 + product = loaded_products.find{|p| p.id == product_id} # to get the price + ProductOrder.create order: @order, product_id: product_id, amount: number, price: product.price if number > 0 end end diff --git a/app/models/product_order.rb b/app/models/product_order.rb index 76a1b54c..55645929 100644 --- a/app/models/product_order.rb +++ b/app/models/product_order.rb @@ -2,6 +2,7 @@ class ProductOrder include SimplyStored::Couch property :amount, type: Fixnum + property :price, type: Float belongs_to :product belongs_to :order diff --git a/app/views/dashboard/show_products.html.slim b/app/views/dashboard/show_products.html.slim index 8c008dfa..6ef68332 100644 --- a/app/views/dashboard/show_products.html.slim +++ b/app/views/dashboard/show_products.html.slim @@ -1,3 +1,7 @@ +.page-header + h4= t('user.show_products.title', products: Product.model_name.human_plural) += link_to t('helpers.links.show_active_list', list: List.model_name.human), user_active_list_path, class: ['btn btn'] +span#list-needs-help-button table#products-table.table.table-striped.table-hover tbody -# content_for :sidebar do @@ -12,7 +16,7 @@ table#active-order-table.table.table-striped.hide tfoot tr td colspan=2 - button class="btn btn-primary" onClick="Qrammer.order_active_list('/order_active_list')" Bestellen + button class="btn btn-primary" onClick="Qrammer.handle_active_user_list(function(){Qrammer.order_active_products_list('/order_active_products_list')})" Bestellen |  button class="btn btn btn-warning" onClick="Qrammer.clear_active_list()" Clear td.currency @@ -21,26 +25,29 @@ table#active-order-table.table.table-striped.hide - content_for :footer do javascript: jQuery(function(){ - $.get('#{product_list_supplier_path(@supplier, format: :json).html_safe}', function(res){ - window.products = res - body = $('#products-table tbody') - for(var category in window.products){ - body.append('

    '+category+'

    '); - var category_ref = window.products[category]; - for(var iproduct = 0; iproduct < window.products[category].length; iproduct++){ - var product_index = iproduct; - row = $(''); - button = $(''); - var callback = (function(ref){ - return function(){ Qrammer.add_product(ref[product_index]) } - })(category_ref) - button.click(callback); - row.append(''+window.products[category][iproduct].name+''); - row.append(''+Qrammer.currency(window.products[category][iproduct].price)+''); - row.append($('').append(button)); - body.append(row); + Qrammer.handle_active_user_list(function(){ + $.get('#{product_list_supplier_path(@supplier, format: :json).html_safe}', function(res){ + window.products = res + body = $('#products-table tbody') + for(var category in window.products){ + body.append('

    '+category+'

    '); + var category_ref = window.products[category]; + for(var iproduct = 0; iproduct < window.products[category].length; iproduct++){ + var product_index = iproduct; + row = $(''); + button = $(''); + var callback = (function(ref){ + return function(){ Qrammer.add_product(ref[product_index]) } + })(category_ref) + button.click(callback); + row.append(''+window.products[category][iproduct].name+''); + row.append(''+Qrammer.currency(window.products[category][iproduct].price)+''); + row.append($('').append(button)); + body.append(row); + } } - } + }) + setInterval('Qrammer.handle_active_user_list()', 7500); }) }) diff --git a/app/views/dashboard/view_active_list.html.slim b/app/views/dashboard/view_active_list.html.slim index e4bfa04b..836dffb7 100644 --- a/app/views/dashboard/view_active_list.html.slim +++ b/app/views/dashboard/view_active_list.html.slim @@ -1,10 +1,13 @@ .page-header - h1 List overview -table#active-list-table.table + h4= t('user.active_list.title', list: List.model_name.human) += link_to t('helpers.links.place_order'), user_products_path, class: ['btn btn-primary'] +span#list-needs-payment-button +span#list-needs-help-button +table#active-list-table.table.table-striped thead tr - th Order - th.currency Price + th= Order.model_name.human + th.currency= Product.human_attribute_name(:price) tbody tfoot - content_for :footer do diff --git a/app/views/lists/_form.html.slim b/app/views/lists/_form.html.slim index f720146f..e6ca7ce9 100644 --- a/app/views/lists/_form.html.slim +++ b/app/views/lists/_form.html.slim @@ -4,6 +4,14 @@ = f.label :state, class: 'control-label' .controls = f.text_field :state, class: 'text_field' + .control-group class=(@list.errors[:need_help].any? ? 'error' : nil) + = f.label :need_help, class: 'control-label' + .controls + = f.check_box :need_help, class: 'check_box' + .control-group class=(@list.errors[:needs_payment].any? ? 'error' : nil) + = f.label :needs_payment, class: 'control-label' + .controls + = f.check_box :needs_payment, class: 'check_box' .control-group class=(@list.errors[:closed_at].any? ? 'error' : nil) = f.label :closed_at, class: 'control-label' .controls diff --git a/app/views/lists/index.html.slim b/app/views/lists/index.html.slim index 521066c1..99f9fe0c 100644 --- a/app/views/lists/index.html.slim +++ b/app/views/lists/index.html.slim @@ -5,6 +5,8 @@ div.page-header= title :index, model_class thead tr th= model_class.human_attribute_name(:state) + th= model_class.human_attribute_name(:need_help) + th= model_class.human_attribute_name(:needs_payment) th= model_class.human_attribute_name(:closed_at) th= Table.model_name.human th= model_class.human_attribute_name(:created_at) @@ -13,8 +15,10 @@ div.page-header= title :index, model_class - @lists.each do |list| tr td= link_to list.state, list + td= list.need_help + td= list.needs_payment td= list.closed_at - td= link_to list.table.number, list.table + td= link_to_if list.table.present?, list.table.try(:number), list.table td=l list.created_at, format: :short td = link_to t('helpers.links.edit'), [:edit, list], class: 'btn btn-mini' diff --git a/app/views/lists/show.html.slim b/app/views/lists/show.html.slim index 64a23be7..c4a6e225 100644 --- a/app/views/lists/show.html.slim +++ b/app/views/lists/show.html.slim @@ -4,6 +4,10 @@ dl.dl-horizontal.show-list dt= model_class.human_attribute_name(:state) dd= @list.state + dt= model_class.human_attribute_name(:need_help) + dd= @list.need_help + dt= model_class.human_attribute_name(:needs_payment) + dd= @list.needs_payment dt= model_class.human_attribute_name(:closed_at) dd= @list.closed_at - if @list.table.present? diff --git a/app/views/suppliers/active_lists.html.slim b/app/views/suppliers/active_lists.html.slim index 1f5ad339..1f849c08 100644 --- a/app/views/suppliers/active_lists.html.slim +++ b/app/views/suppliers/active_lists.html.slim @@ -9,8 +9,9 @@ table#active-lists-table.table.table-striped tbody - content_for :footer do javascript: + var active_lists_interval; jQuery(function(){ Qrammer.load_active_lists('#{@supplier.id}') - setInterval( "Qrammer.load_active_lists('#{@supplier.id}')", 7500); + active_lists_interval = setInterval( "Qrammer.load_active_lists('#{@supplier.id}')", 7500); }); diff --git a/config/locales/en.yml b/config/locales/en.yml index 573220a0..2280f940 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -5,6 +5,8 @@ en: helpers: links: are_you_sure: 'Are you sure?' + place_order: Place order + show_active_list: Show %{list} forms: errors: title: There are problems found during saving (%{count}) @@ -12,7 +14,7 @@ en: cannot_order_without_list_id: You cannot place an order without specifying a list cannot_order_on_non_active_list: You cannot place an order on a closed list order_is_placed: Your order has been received in good order - the_list_has_been_closed: The list has been closed + the_list_has_been_closed: The %{list} has been closed action: index: label: Listing %{models} @@ -43,6 +45,16 @@ en: list: Lists product: Products product_category: Product categories + attributes: + product: + price: Price supplier: menu: active_lists: Active %{lists} + user: + active_list: + title: Active %{list} + needs_payment: Check please! + 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 a629d80f..ef7b62c0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -22,18 +22,23 @@ Qrammer::Application.routes.draw do member do get :current post :is_closed + post :is_helped end end resources :products resources :product_categories - match "/:action", controller: 'dashboard' - match '/view_active_list' => 'dashboard#view_active_list', as: :view_active_list + match '/view_active_list' => 'dashboard#view_active_list', as: :view_active_list # depricated + match '/view_active_list' => 'dashboard#view_active_list', as: :user_active_list match '/phone_home' => 'dashboard#phone_home', as: :phone_root match '/supplier_home' => 'dashboard#supplier_home', as: :supplier_root match '/supplier_home' => 'dashboard#supplier_home', as: :supplier_orders match '/supplier_lists' => 'dashboard#supplier_lists', as: :supplier_lists + match '/user_list_info' => 'dashboard#user_list_info', as: :user_list_info match '/user_history' => 'dashboard#user_history', as: :user_history + match '/show_products' => 'dashboard#show_products', as: :user_products + post '/active_user_list_needs_help' => 'dashboard#active_user_list_needs_help', as: :active_user_list_needs_help + match "/:action", controller: 'dashboard' # The priority is based upon order of creation: # first created -> highest priority.