From 38b5dcba1f69b2d5390c7351661b0f525d4ae508 Mon Sep 17 00:00:00 2001 From: benni Date: Sun, 19 Jun 2011 19:56:04 +0200 Subject: [PATCH] Refactored ordering part two. Integrated stock order. --- app/controllers/ordering_controller.rb | 43 +------- app/helpers/ordering_helper.rb | 17 ++- app/models/group_order.rb | 20 ++-- app/models/order_article.rb | 7 ++ app/views/ordering/_data.html.erb | 25 ----- app/views/ordering/_footer.html.haml | 23 ---- app/views/ordering/_form.html.haml | 120 ++++++++++++++++----- app/views/ordering/_order_footer.html.erb | 25 ----- app/views/ordering/_order_head.haml | 48 --------- app/views/ordering/_switch_order.html.haml | 12 +++ app/views/ordering/show.html.haml | 11 +- app/views/ordering/stock_order.html.haml | 75 ------------- app/views/orders/_form.html.haml | 8 +- app/views/orders/new.html.haml | 2 +- public/javascripts/ordering.js | 103 ++++++------------ public/stylesheets/main.css | 6 +- public/stylesheets/sass/main.sass | 6 +- 17 files changed, 177 insertions(+), 374 deletions(-) delete mode 100644 app/views/ordering/_data.html.erb delete mode 100644 app/views/ordering/_footer.html.haml delete mode 100644 app/views/ordering/_order_footer.html.erb delete mode 100644 app/views/ordering/_order_head.haml create mode 100644 app/views/ordering/_switch_order.html.haml delete mode 100644 app/views/ordering/stock_order.html.haml diff --git a/app/controllers/ordering_controller.rb b/app/controllers/ordering_controller.rb index 4c57dcda..6953b072 100644 --- a/app/controllers/ordering_controller.rb +++ b/app/controllers/ordering_controller.rb @@ -50,48 +50,7 @@ class OrderingController < ApplicationController redirect_to group_orders_url, :alert => 'Die Bestellung konnte nicht aktualisiert werden, da ein Fehler auftrat.' end end - - def stock_order - # Load order article data... - @articles_grouped_by_category = @order.articles_grouped_by_category - # save results of earlier orders in array - ordered_articles = Array.new - @group_order = @order.group_orders.find(:first, - :conditions => "ordergroup_id = #{@ordergroup.id}", :include => :group_order_articles) - - if @group_order - # Group has already ordered, so get the results... - for goa in @group_order.group_order_articles - ordered_articles[goa.order_article_id] = {:quantity => goa.quantity, - :tolerance => goa.tolerance, - :quantity_result => goa.result(:quantity), - :tolerance_result => goa.result(:tolerance)} - end - @version = @group_order.lock_version - @availableFunds = @ordergroup.get_available_funds(@group_order) - else - @version = 0 - @availableFunds = @ordergroup.get_available_funds - end - - # load prices .... - @price = Array.new; @quantity_available = Array.new - @others_quantity = Array.new; @quantity = Array.new; @quantity_result = Array.new; @used_quantity = Array.new; @unused_quantity = Array.new - i = 0; - @articles_grouped_by_category.each do |category_name, order_articles| - for order_article in order_articles - # price/unit size - @price[i] = order_article.article.fc_price - @quantity_available[i] = order_article.article.quantity_available(@order) - # quantity - @quantity[i] = (ordered_articles[order_article.id] ? ordered_articles[order_article.id][:quantity] : 0) - @others_quantity[i] = order_article.quantity - @quantity[i] - @used_quantity[i] = (ordered_articles[order_article.id] ? ordered_articles[order_article.id][:quantity_result] : 0) - i += 1 - end - end - end - + # Shows all Orders of the Ordergroup # if selected, it shows all orders of the foodcoop def archive diff --git a/app/helpers/ordering_helper.rb b/app/helpers/ordering_helper.rb index 3e0c9494..f5c4cdc1 100644 --- a/app/helpers/ordering_helper.rb +++ b/app/helpers/ordering_helper.rb @@ -1,24 +1,23 @@ module OrderingHelper def data_to_js(ordering_data) - ordering_data[:order_articles].map do |id, data| - if Foodsoft.config[:tolerance_is_costly] - [id, data[:price], data[:unit], data[:price] * (data[:tolerance] + data[:quantity]), data[:others_quantity], data[:others_tolerance], data[:used_quantity], 0] - else - [id, data[:price], data[:unit], data[:price] * data[:quantity], data[:others_quantity], data[:others_tolerance], data[:used_quantity], 0] - end - end + ordering_data[:order_articles].map { |id, data| + [id, data[:price], data[:unit], data[:total_price], data[:others_quantity], data[:others_tolerance], data[:used_quantity], data[:quantity_available]] + }.map { |row| + "addData(#{row.join(', ')});" + }.join("\n") end - def link_to_ordering(order) + def link_to_ordering(order, options = {}) path = if group_order = order.group_order(current_user.ordergroup) edit_group_order_path(group_order, :order_id => order.id) else new_group_order_path(:order_id => order.id) end - link_to order.name, path + link_to order.name, path, options end # Return css class names for order result table + def order_article_class_name(quantity, tolerance, result) if (quantity + tolerance > 0) result > 0 ? 'success' : 'failed' diff --git a/app/models/group_order.rb b/app/models/group_order.rb index b3af1b28..c5675ad9 100644 --- a/app/models/group_order.rb +++ b/app/models/group_order.rb @@ -26,14 +26,17 @@ class GroupOrder < ActiveRecord::Base # Group has already ordered, so get the results... goas = {} group_order_articles.all.each do |goa| - goas[goa.order_article_id] = {:quantity => goa.quantity, - :tolerance => goa.tolerance, - :quantity_result => goa.result(:quantity), - :tolerance_result => goa.result(:tolerance)} - end + goas[goa.order_article_id] = { + :quantity => goa.quantity, + :tolerance => goa.tolerance, + :quantity_result => goa.result(:quantity), + :tolerance_result => goa.result(:tolerance), + :total_price => goa.total_price + } + end end - # load prices .... + # load prices and other stuff.... data[:order_articles] = {} order.order_articles.each do |order_article| data[:order_articles][order_article.id] = { @@ -44,7 +47,10 @@ class GroupOrder < ActiveRecord::Base :used_quantity => (new_record? ? 0 : goas[order_article.id][:quantity_result]), :tolerance => (new_record? ? 0 : goas[order_article.id][:tolerance]), :others_tolerance => order_article.tolerance - (new_record? ? 0 : goas[order_article.id][:tolerance]), - :used_tolerance => (new_record? ? 0 : goas[order_article.id][:tolerance_result]) + :used_tolerance => (new_record? ? 0 : goas[order_article.id][:tolerance_result]), + :total_price => (new_record? ? 0 : goas[order_article.id][:total_price]), + :missing_units => order_article.missing_units, + :quantity_available => (order.stockit? ? order_article.article.quantity_available : 0) } end diff --git a/app/models/order_article.rb b/app/models/order_article.rb index 8fbe6b3a..fec7ec4e 100644 --- a/app/models/order_article.rb +++ b/app/models/order_article.rb @@ -99,6 +99,13 @@ class OrderArticle < ActiveRecord::Base end end + # Units missing for the next full unit_quantity of the article + def missing_units + units = article.unit_quantity - ((quantity % article.unit_quantity) + tolerance) + units = 0 if units < 0 + units + end + private def article_and_price_exist diff --git a/app/views/ordering/_data.html.erb b/app/views/ordering/_data.html.erb deleted file mode 100644 index 2bab910d..00000000 --- a/app/views/ordering/_data.html.erb +++ /dev/null @@ -1,25 +0,0 @@ - diff --git a/app/views/ordering/_footer.html.haml b/app/views/ordering/_footer.html.haml deleted file mode 100644 index 44ff391e..00000000 --- a/app/views/ordering/_footer.html.haml +++ /dev/null @@ -1,23 +0,0 @@ -#order-footer - #info-box - #total-sum - %table - %tr - %td Gesamtbetrag: - %td.currency - %span#total_price= total - € - %tr - %td Verfügbares Guthaben: - %td.currency= number_to_currency(@ordering_data[:available_funds]) - %tr - %td Neuer Kontostand: - %td.currency - %strong - %span#new_balance= @ordergroup.account_balance - total - € - #order-button - = submit_tag( "Bestellung speichern", :id => 'submit_button' ) - oder #{link_to "abbrechen", :controller => 'ordering'} -%input#total_balance{:name => "total_balance", :type => "hidden", :value => @ordergroup.account_balance - total}/ -%input{:name => "version", :type => "hidden", :value => @version}/ diff --git a/app/views/ordering/_form.html.haml b/app/views/ordering/_form.html.haml index bc399ba6..cc1332fb 100644 --- a/app/views/ordering/_form.html.haml +++ b/app/views/ordering/_form.html.haml @@ -1,9 +1,52 @@ - content_for :head do - = render 'data' + :javascript + $(function() { + #{data_to_js(@ordering_data)} + setGroupBalance(#{@ordering_data[:available_funds]}); + setDecimalSeparator(","); + setToleranceBehaviour(#{Foodsoft.config[:tolerance_is_costly]}); + setStockit(#{@order.stockit?}); + }); -= render :partial => 'order_head' +- title "Bestellen" -- form_for @group_order do |f| +.left_column{:style => "width:49em"} + .box_title + %h2=h @order.name + .column_content + %table + %tr{:valign => "top"} + %td{:width => "60%"} + %p + %b Lieferantin: + =h @order.name + %p + %b Ende: + =h format_time(@order.ends) + - if @group_order && @group_order.updated_by + %p + %b Zuletzt bestellt: + =h @group_order.updated_by.nick if @group_order.updated_by + = "(#{format_time(@group_order.updated_on)})" + %p + %b Verfügbares Guthaben: + = number_to_currency(@ordering_data[:available_funds]) + %td + - unless @order.note.empty? + %p + %b Notiz: + = simple_format(@order.note) + - unless @order.stockit? || @order.supplier.min_order_quantity.blank? + %p + %b Mindestellmenge: + =h @order.supplier.min_order_quantity + %p + %b Gesamtbestellmenge bisher: + = number_to_currency @order.sum + += render :partial => 'switch_order', :locals => {:current_order => @order} + += form_for @group_order do |f| = f.hidden_field :lock_version = f.hidden_field :order_id = f.hidden_field :updated_by_user_id @@ -19,36 +62,32 @@ %th{:style => "width:13px;"} %th{:style => "width:4.5em;"} Preis %th{:style => "width:4.5em;"} Einheit - %th{:style => "width:70px;"} Fehlende Einheiten - %th#col_required Menge - - if not @order.stockit? + - unless @order.stockit? + %th{:style => "width:70px;"} Fehlende Einheiten + %th#col_required Menge %th#col_tolerance Toleranz + - else + %th(style="width:20px") Verfügbar + %th#col_required Menge %th{:style => "width:15px;"} Summe %tbody - - total = 0 - @order.articles_grouped_by_category.each do |category, order_articles| %tr{:style => "background-color:#EFEFEF"} %td{:style => "text-align:left"} %b= h category %td{:colspan => "9"} - order_articles.each do |order_article| - - if Foodsoft.config[:tolerance_is_costly] - - article_total = @ordering_data[:order_articles][order_article.id][:price] * (@ordering_data[:order_articles][order_article.id][:tolerance] + @ordering_data[:order_articles][order_article.id][:quantity]) - - else - - article_total = @ordering_data[:order_articles][order_article.id][:price] * @ordering_data[:order_articles][order_article.id][:quantity] - - total += article_total %tr{:class => "#{cycle('even', 'odd', :name => 'articles')} order-article", :valign => "top"} %td.name= order_article.article.name %td= h order_article.article.origin %td= number_to_currency(@ordering_data[:order_articles][order_article.id][:price]) %td= order_article.article.unit %td - %span{:id => "missing_units_#{order_article.id}"} - - if @order.stockit? - - order_article.article.quantity_available - - else - - missing_units = @ordering_data[:order_articles][order_article.id][:unit] - (((@ordering_data[:order_articles][order_article.id][:quantity] + @ordering_data[:order_articles][order_article.id][:others_quantity]) % @ordering_data[:order_articles][order_article.id][:unit]) + @ordering_data[:order_articles][order_article.id][:tolerance] + @ordering_data[:order_articles][order_article.id][:others_tolerance]) - - missing_units < 0 ? 0 : missing_units + - if @order.stockit? + = @ordering_data[:order_articles][order_article.id][:quantity_available] + - else + %span{:id => "missing_units_#{order_article.id}"}= @ordering_data[:order_articles][order_article.id][:missing_units] + %td.quantity %input{:id => "q_#{order_article.id}", :name => "group_order[group_order_articles_attributes][#{order_article.id}][quantity]", :size => "2", :type => "hidden", :value => @ordering_data[:order_articles][order_article.id][:quantity]}/ %span.used{:id => "q_used_#{order_article.id}"}= @ordering_data[:order_articles][order_article.id][:used_quantity] @@ -56,17 +95,18 @@ %span.unused{:id => "q_unused_#{order_article.id}"}= @ordering_data[:order_articles][order_article.id][:quantity] - @ordering_data[:order_articles][order_article.id][:used_quantity] %input{:type => 'button', :value => '+', 'data-increase_quantity' => order_article.id} %input{:type => 'button', :value => '-', 'data-decrease_quantity' => order_article.id} - - unless @order.stockit? - %td.tolerance - %input{:id => "t_#{order_article.id}", :name => "group_order[group_order_articles_attributes][#{order_article.id}][tolerance]", :size => "2", :type => "hidden", :value => @ordering_data[:order_articles][order_article.id][:tolerance]}/ - - if (@ordering_data[:order_articles][order_article.id][:unit] > 1) - %span.used{:id => "t_used_#{order_article.id}"}= @ordering_data[:order_articles][order_article.id][:used_tolerance] - + - %span.unused{:id => "t_unused_#{order_article.id}"}= @ordering_data[:order_articles][order_article.id][:tolerance] - @ordering_data[:order_articles][order_article.id][:used_tolerance] - %input{:type => 'button', :value => '+', 'data-increase_tolerance' => order_article.id} - %input{:type => 'button', :value => '-', 'data-decrease_tolerance' => order_article.id} + + %td.tolerance{:style => ('display:none' if @order.stockit?)} + %input{:id => "t_#{order_article.id}", :name => "group_order[group_order_articles_attributes][#{order_article.id}][tolerance]", :size => "2", :type => "hidden", :value => @ordering_data[:order_articles][order_article.id][:tolerance]}/ + - if (@ordering_data[:order_articles][order_article.id][:unit] > 1) + %span.used{:id => "t_used_#{order_article.id}"}= @ordering_data[:order_articles][order_article.id][:used_tolerance] + + + %span.unused{:id => "t_unused_#{order_article.id}"}= @ordering_data[:order_articles][order_article.id][:tolerance] - @ordering_data[:order_articles][order_article.id][:used_tolerance] + %input{:type => 'button', :value => '+', 'data-increase_tolerance' => order_article.id} + %input{:type => 'button', :value => '-', 'data-decrease_tolerance' => order_article.id} + %td{:id => "td_price_#{order_article.id}", :style => "text-align:right; padding-right:10px; width:4em"} - %span{:id => "price_#{order_article.id}_display"}= number_to_currency(article_total, :unit => "") + %span{:id => "price_#{order_article.id}_display"}= number_to_currency(@ordering_data[:order_articles][order_article.id][:total_price], :unit => "") € .article-info %h3= order_article.article.name @@ -87,4 +127,26 @@ %br/ Notiz: #{order_article.article.note} %br/ - = render "footer", :total => total \ No newline at end of file + #order-footer + #info-box + #total-sum + %table + %tr + %td Gesamtbetrag: + %td.currency + %span#total_price= @group_order.price + € + %tr + %td Verfügbares Guthaben: + %td.currency= number_to_currency(@ordering_data[:available_funds]) + %tr + %td Neuer Kontostand: + %td.currency + %strong + %span#new_balance= @ordergroup.account_balance - @group_order.price + € + #order-button + = submit_tag( "Bestellung speichern", :id => 'submit_button' ) + oder #{link_to "abbrechen", :controller => 'ordering'} + %input#total_balance{:name => "total_balance", :type => "hidden", :value => @ordergroup.account_balance - @group_order.price}/ + %input{:name => "version", :type => "hidden", :value => @version}/ \ No newline at end of file diff --git a/app/views/ordering/_order_footer.html.erb b/app/views/ordering/_order_footer.html.erb deleted file mode 100644 index 8997fc8a..00000000 --- a/app/views/ordering/_order_footer.html.erb +++ /dev/null @@ -1,25 +0,0 @@ - - - \ No newline at end of file diff --git a/app/views/ordering/_order_head.haml b/app/views/ordering/_order_head.haml deleted file mode 100644 index 079201e6..00000000 --- a/app/views/ordering/_order_head.haml +++ /dev/null @@ -1,48 +0,0 @@ -- title "Bestellen" - -.left_column{:style => "width:49em"} - .box_title - %h2=h @order.name - .column_content - %table - %tr{:valign => "top"} - %td{:width => "60%"} - %p - %b Lieferantin: - =h @order.name - %p - %b Ende: - =h format_time(@order.ends) - - if @group_order && @group_order.updated_by - %p - %b Zuletzt bestellt: - =h @group_order.updated_by.nick if @group_order.updated_by - = "(#{format_time(@group_order.updated_on)})" - %p - %b Verfügbares Guthaben: - = number_to_currency(@ordering_data[:available_funds]) - %td - - unless @order.note.empty? - %p - %b Notiz: - = simple_format(@order.note) - - unless @order.stockit? || @order.supplier.min_order_quantity.blank? - %p - %b Mindestellmenge: - =h @order.supplier.min_order_quantity - %p - %b Gesamtbestellmenge bisher: - = number_to_currency @order.sum - -- orders = Order.open.reject{ |order| order == @order } -- unless orders.empty? - .right_column{:style => "width:22em"} - .box_title - %h2 Anderer Bestellungen - .column_content - %table - - for order in orders - %tr - %td - = link_to_function order.name, "if (confirmSwitchOrder()) (window.location = '#{ url_for(:action => 'order', :id => order) }' )" - %td= "noch #{time_ago_in_words(order.ends)}" if order.ends diff --git a/app/views/ordering/_switch_order.html.haml b/app/views/ordering/_switch_order.html.haml new file mode 100644 index 00000000..e30c86c6 --- /dev/null +++ b/app/views/ordering/_switch_order.html.haml @@ -0,0 +1,12 @@ +- orders = Order.open.reject{ |order| order == current_order } +- unless orders.empty? + .right_column{:style => "width:22em"} + .box_title + %h2 Laufende Bestellungen + .column_content + %table + - for order in orders + %tr + %td + = link_to_ordering(order, 'data-confirm_switch_order' => true) + %td= "noch #{time_ago_in_words(order.ends)}" if order.ends \ No newline at end of file diff --git a/app/views/ordering/show.html.haml b/app/views/ordering/show.html.haml index 3eb3e247..8ed5fe4f 100644 --- a/app/views/ordering/show.html.haml +++ b/app/views/ordering/show.html.haml @@ -35,16 +35,7 @@ = "Abgerechnet von #{@order.updated_by.nick}" = link_to "Kommentare lesen/schreiben", "#comments" -// directly switch to active orders -.right_column{:style => "width:23em;"} - .box_title - %h2 Laufende Bestellungen - .column_content - %table - - for order in Order.open - %tr - %td= link_to_ordering(order) - %td= "("+ time_ago_in_words(order.ends) + ")" if order.ends += render :partial => "switch_order", :locals => {:current_order => @order} // Article box .single_column{:style => "clear:both; width:70em;"} diff --git a/app/views/ordering/stock_order.html.haml b/app/views/ordering/stock_order.html.haml deleted file mode 100644 index 4f70b45b..00000000 --- a/app/views/ordering/stock_order.html.haml +++ /dev/null @@ -1,75 +0,0 @@ -= render :partial => 'order_head' - -.single_column{:style => 'clear:both;margin-bottom:7em;'} - - form_tag(:action => 'saveOrder', :id => @order) do - .box_title - %h2 Anderer Bestellungen - .column_content - %table#order.list - %thead - %tr - %th Name - %th - %th Lieferantin - %th{:style => "width:5em;"} Einheit - %th{:style => "width:4.5em;"} Preis - %th(style="width:20px") Verfügbar - %th#col_required(style="width:110px") Menge - %th Summe - %tbody - - total = 0 - - i = 0 - - for category_name, order_articles in @articles_grouped_by_category - %tr{:style => "background-color:#EFEFEF"} - %td{:style => "text-align:left"} - %b=h category_name - %td{:colspan => "9"} - - for order_article in order_articles - - article_total = @price[i] * @quantity[i] - - total += article_total - %tr{:class => cycle('even', 'odd', :name => 'articles')+' order-article', :valign => "top"} - %td.name= order_article.article.name - %td=h order_article.article.origin - %td=h truncate order_article.article.supplier.name, :length => 11 - %td=h order_article.article.unit - %td= number_to_currency @price[i] - %td= @quantity_available[i] - %td{:style => "text-align:right;"} - = hidden_field_tag "ordered[#{order_article.id}][quantity]", @quantity[i], :id => "q_#{i}", :size => "2" - %span.used{:id => "q_used_#{i}"}= @used_quantity[i] - = button_to_function('+', "increaseStockQuantity(#{i})") - = button_to_function('-', "decreaseStockQuantity(#{i})") - - %td{:id => "td_price_#{i}", :style => "text-align:right; padding-right:10px; width:4em"} - %span{:id => "price_#{i}_display"}= number_to_currency(article_total, :unit => "") - € - .article-info - %h3= order_article.article.name - .right - Gesamt-Einheiten: - %span{:id => "q_total_#{i}"}= @quantity[i] + @others_quantity[i] - %br/ - .left - Hersteller: - = order_article.article.manufacturer - %br/ - Gebinde: - = order_article.article.quantity_available - * - = order_article.article.unit - %br/ - Notiz: - = order_article.article.note - %br/ - - i = i + 1 - = render "order_footer", :total => total - -%script{:type => "text/" + "javascript"} - // preset data - - for i in 0...@price.size - = "addData(#{@price[i]}, 1, #{@price[i] * @quantity[i]}, #{@others_quantity[i]}, 0, #{@used_quantity[i]}, #{@quantity_available[i]});" - = "setGroupBalance(#{@availableFunds});" - // localization - setDecimalSeparator(","); - // initialize javascript - updateBalance(); diff --git a/app/views/orders/_form.html.haml b/app/views/orders/_form.html.haml index 3a153815..4912bcd1 100644 --- a/app/views/orders/_form.html.haml +++ b/app/views/orders/_form.html.haml @@ -1,7 +1,7 @@ = simple_form_for @order do |f| .single_column .box_title - %h2 Bestellung für #{@order.supplier.name} + %h2 Bestellung für #{@order.name} .column_content = f.hidden_field :supplier_id = f.input :note @@ -40,9 +40,9 @@ / check if the article is selected - included = @order.order_articles.detect { |order_article| order_article.article_id == article.id } - included_class = included ? ' selected' : '' - %tr{:class => cycle('even', 'odd') + ' click-me' + included_class, :id => article.id.to_s, :onclick => "checkRow('#{article.id}')"} - %td= check_box_tag "order[article_ids][]", article.id, included, { :id => "checkbox_#{article.id}", :onclick => "checkRow('#{article.id}')" } - %td=h article.name + %tr{:class => cycle('even', 'odd') + included_class, :id => article.id.to_s } + %td= check_box_tag "order[article_ids][]", article.id, included, :id => "checkbox_#{article.id}" + %td.click-me{'data-check-this' => "#checkbox_#{article.id}"}= article.name %td=h truncate article.note, :length => 25 - if @order.stockit? %td= "#{article.quantity_available} * #{article.unit}" diff --git a/app/views/orders/new.html.haml b/app/views/orders/new.html.haml index bc67fe2b..a48aac28 100644 --- a/app/views/orders/new.html.haml +++ b/app/views/orders/new.html.haml @@ -1,3 +1,3 @@ - title "Neue Bestellung anlegen" -= render :partial => 'form', :locals => { :f => f } += render :partial => 'form' diff --git a/public/javascripts/ordering.js b/public/javascripts/ordering.js index 3f7e54b5..cb940d0b 100644 --- a/public/javascripts/ordering.js +++ b/public/javascripts/ordering.js @@ -6,9 +6,10 @@ // Call setDecimalSeparator(char) to overwrite the default character "." with a localized value. var modified = false // indicates if anything has been clicked on this page -var groupBalance = 0; // available group money +var groupBalance = 0; // available group money var decimalSeparator = "."; // default decimal separator -var toleranceIsCostly = true; // default tolerance behaviour +var toleranceIsCostly = true; // default tolerance behaviour +var isStockit = false; // Wheter the order is from stock oder normal supplier // Article data arrays: var price = new Array(); @@ -27,12 +28,16 @@ function setToleranceBehaviour(value) { toleranceIsCostly = value; } +function setStockit(value) { + isStockit = value; +} + function setGroupBalance(amount) { groupBalance = amount; } function addData(orderArticleId, itemPrice, itemUnit, itemSubtotal, itemQuantityOthers, itemToleranceOthers, allocated, available) { - i = orderArticleId; + var i = orderArticleId; price[i] = itemPrice; unit[i] = itemUnit; itemTotal[i] = itemSubtotal; @@ -43,24 +48,26 @@ function addData(orderArticleId, itemPrice, itemUnit, itemSubtotal, itemQuantity } function increaseQuantity(item) { - value = Number($('#q_' + item).val()) + 1; - update(item, value, $('#t_' + item).val()); + var value = Number($('#q_' + item).val()) + 1; + if (!isStockit || (value <= (quantityAvailable[item] - quantityOthers[item]))) { + update(item, value, $('#t_' + item).val()); + } } function decreaseQuantity(item) { - value = Number($('#q_' + item).val()) - 1; + var value = Number($('#q_' + item).val()) - 1; if (value >= 0) { update(item, value, $('#t_' + item).val()); } } function increaseTolerance(item) { - value = Number($('#t_' + item).val()) + 1; + var value = Number($('#t_' + item).val()) + 1; update(item, $('#q_' + item).val(), value); } function decreaseTolerance(item) { - value = Number($('#t_' + item).val()) - 1; + var value = Number($('#t_' + item).val()) - 1; if (value >= 0) { update(item, $('#q_' + item).val(), value); } @@ -75,7 +82,7 @@ function update(item, quantity, tolerance) { $('#t_' + item).val(tolerance); // calculate how many units would be ordered in total - units = calcUnits(unit[item], quantityOthers[item] + Number(quantity), toleranceOthers[item] + Number(tolerance)); + var units = calcUnits(unit[item], quantityOthers[item] + Number(quantity), toleranceOthers[item] + Number(tolerance)); if (unitCompletedFromTolerance(unit[item], quantityOthers[item] + Number(quantity), toleranceOthers[item] + Number(tolerance))) { $('#units_' + item).html('' + String(units) + ''); } else { @@ -83,8 +90,8 @@ function update(item, quantity, tolerance) { } // update used/unused quantity - available = Math.max(0, units * unit[item] - quantityOthers[item]); - q_used = Math.min(available, quantity); + var available = Math.max(0, units * unit[item] - quantityOthers[item]); + var q_used = Math.min(available, quantity); // ensure that at least the amout of items this group has already been allocated is used if (quantity >= itemsAllocated[item] && q_used < itemsAllocated[item]) { q_used = itemsAllocated[item]; @@ -111,7 +118,7 @@ function update(item, quantity, tolerance) { $('#price_' + item + '_display').html(asMoney(itemTotal[item])); // update missing units - missing_units = unit[item] - (((quantityOthers[item] + Number(quantity)) % unit[item]) + Number(tolerance) + toleranceOthers[item]) + var missing_units = unit[item] - (((quantityOthers[item] + Number(quantity)) % unit[item]) + Number(tolerance) + toleranceOthers[item]) if (missing_units < 0) { missing_units = 0; } @@ -121,77 +128,37 @@ function update(item, quantity, tolerance) { updateBalance(); } -function increaseStockQuantity(item) { - value = Number($('#q_' + item).val()) + 1; - if (value <= quantityAvailable[item] - quantityOthers[item]) { - updateStockQuantity(item, value); - } -} - -function decreaseStockQuantity(item) { - value = Number($('#q_' + item).val()) - 1; - if (value >= 0) { - updateStockQuantity(item, value); - } -} - -function updateStockQuantity(item, quantity) { - // set modification flag - modified = true - - // update hidden input fields - $('#q_' + item).val(quantity); - - // update used/unused quantity - available = Math.max(0, quantityAvailable[item] - quantityOthers[item]); - q_used = Math.min(available, quantity); - - // ensure that at least the amout of items this group has already been allocated is used - if (quantity >= itemsAllocated[item] && q_used < itemsAllocated[item]) { - q_used = itemsAllocated[item]; - } - $('#q_used_' + item).html(String(q_used)); - $('#q_total_' + item).html(String(Number(quantity) + quantityOthers[item])); - - // update total price - itemTotal[item] = price[item] * (Number(quantity)); - $('#price_' + item + '_display').html(asMoney(itemTotal[item])); - - // update balance - updateBalance(); -} - function asMoney(amount) { return String(amount.toFixed(2)).replace(/\./, ","); } function calcUnits(unitSize, quantity, tolerance) { - units = Math.floor(quantity / unitSize) - remainder = quantity % unitSize + var units = Math.floor(quantity / unitSize) + var remainder = quantity % unitSize return units + ((remainder > 0) && (remainder + tolerance >= unitSize) ? 1 : 0) } function unitCompletedFromTolerance(unitSize, quantity, tolerance) { - remainder = quantity % unitSize + var remainder = quantity % unitSize return (remainder > 0 && (remainder + tolerance >= unitSize)); } function updateBalance() { // update total price and order balance - total = 0; + var total = 0; for (i in itemTotal) { total += itemTotal[i]; } $('#total_price').html(asMoney(total)); - balance = groupBalance - total; + var balance = groupBalance - total; $('#new_balance').html(asMoney(balance)); $('#total_balance').val(asMoney(balance)); - // determine bgcolor and submit button state according to balance + // determine bgcolor and submit button state according to balance + var bgcolor = ''; if (balance < 0) { bgcolor = '#FF0000'; $('#submit_button').disabled = true; } else { - bgcolor = ''; $('#submit_button').disabled = false; } // update bgcolor @@ -200,21 +167,21 @@ function updateBalance() { } } -function confirmSwitchOrder() { - return (!modified || confirm('Änderungen an dieser Bestellung gehen verloren, wenn zu einer anderen Bestellung gewechselt wird.')) -} - $(function() { $('input[data-increase_quantity]').click(function() { increaseQuantity($(this).data('increase_quantity')); - }) + }); $('input[data-decrease_quantity]').click(function() { decreaseQuantity($(this).data('decrease_quantity')); - }) + }); $('input[data-increase_tolerance]').click(function() { increaseTolerance($(this).data('increase_tolerance')); - }) + }); $('input[data-decrease_tolerance]').click(function() { decreaseTolerance($(this).data('decrease_tolerance')); - }) -}) + }); + + $('a[data-confirm_switch_order]').click(function() { + return (!modified || confirm('Änderungen an dieser Bestellung gehen verloren, wenn zu einer anderen Bestellung gewechselt wird. Möchtest Du trotzdem wechseln?')); + }); +}); diff --git a/public/stylesheets/main.css b/public/stylesheets/main.css index 536b6e50..53f30c48 100644 --- a/public/stylesheets/main.css +++ b/public/stylesheets/main.css @@ -77,7 +77,7 @@ option { border-top: 1px solid #d7d7d7; margin: 0.2em 0; } -span.click-me { +.click-me { cursor: pointer; } .left { @@ -245,8 +245,6 @@ table { color: green; } table tr.selected, table tr.active { background-color: #ffffc2; } - table tr.click-me { - cursor: pointer; } table tr.ignored { color: grey; } table tr.success { @@ -400,7 +398,7 @@ table#order { -webkit-border-radius: 3px; padding: 0; } table#order th#col_required, table#order th#col_tolerance { - width: 140px; } + width: 145px; } table#order th#col_packages, table#order th#col_left_units { width: 50px; } table#order td.quantity, table#order td.tolerance { diff --git a/public/stylesheets/sass/main.sass b/public/stylesheets/sass/main.sass index 39f67617..f161567f 100644 --- a/public/stylesheets/sass/main.sass +++ b/public/stylesheets/sass/main.sass @@ -88,7 +88,7 @@ option border-top: 1px solid #D7D7D7 margin: .2em 0 -span.click-me +.click-me cursor: pointer .left float: left @@ -268,8 +268,6 @@ table :color #008000 tr.selected, tr.active :background-color #ffffc2 - tr.click-me - :cursor pointer tr.ignored color: grey tr.success @@ -439,7 +437,7 @@ table#order -webkit-border-radius: 3px padding: 0 th#col_required, th#col_tolerance - :width 140px + :width 145px th#col_packages, th#col_left_units :width 50px td.quantity, td.tolerance