Refactored ordering part two. Integrated stock order.

This commit is contained in:
benni 2011-06-19 19:56:04 +02:00
parent eb4705857b
commit 38b5dcba1f
17 changed files with 177 additions and 374 deletions

View file

@ -51,47 +51,6 @@ class OrderingController < ApplicationController
end end
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 # Shows all Orders of the Ordergroup
# if selected, it shows all orders of the foodcoop # if selected, it shows all orders of the foodcoop
def archive def archive

View file

@ -1,24 +1,23 @@
module OrderingHelper module OrderingHelper
def data_to_js(ordering_data) def data_to_js(ordering_data)
ordering_data[:order_articles].map do |id, data| ordering_data[:order_articles].map { |id, data|
if Foodsoft.config[:tolerance_is_costly] [id, data[:price], data[:unit], data[:total_price], data[:others_quantity], data[:others_tolerance], data[:used_quantity], data[:quantity_available]]
[id, data[:price], data[:unit], data[:price] * (data[:tolerance] + data[:quantity]), data[:others_quantity], data[:others_tolerance], data[:used_quantity], 0] }.map { |row|
else "addData(#{row.join(', ')});"
[id, data[:price], data[:unit], data[:price] * data[:quantity], data[:others_quantity], data[:others_tolerance], data[:used_quantity], 0] }.join("\n")
end
end
end end
def link_to_ordering(order) def link_to_ordering(order, options = {})
path = if group_order = order.group_order(current_user.ordergroup) path = if group_order = order.group_order(current_user.ordergroup)
edit_group_order_path(group_order, :order_id => order.id) edit_group_order_path(group_order, :order_id => order.id)
else else
new_group_order_path(:order_id => order.id) new_group_order_path(:order_id => order.id)
end end
link_to order.name, path link_to order.name, path, options
end end
# Return css class names for order result table # Return css class names for order result table
def order_article_class_name(quantity, tolerance, result) def order_article_class_name(quantity, tolerance, result)
if (quantity + tolerance > 0) if (quantity + tolerance > 0)
result > 0 ? 'success' : 'failed' result > 0 ? 'success' : 'failed'

View file

@ -26,14 +26,17 @@ class GroupOrder < ActiveRecord::Base
# Group has already ordered, so get the results... # Group has already ordered, so get the results...
goas = {} goas = {}
group_order_articles.all.each do |goa| group_order_articles.all.each do |goa|
goas[goa.order_article_id] = {:quantity => goa.quantity, goas[goa.order_article_id] = {
:quantity => goa.quantity,
:tolerance => goa.tolerance, :tolerance => goa.tolerance,
:quantity_result => goa.result(:quantity), :quantity_result => goa.result(:quantity),
:tolerance_result => goa.result(:tolerance)} :tolerance_result => goa.result(:tolerance),
:total_price => goa.total_price
}
end end
end end
# load prices .... # load prices and other stuff....
data[:order_articles] = {} data[:order_articles] = {}
order.order_articles.each do |order_article| order.order_articles.each do |order_article|
data[:order_articles][order_article.id] = { 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]), :used_quantity => (new_record? ? 0 : goas[order_article.id][:quantity_result]),
:tolerance => (new_record? ? 0 : goas[order_article.id][:tolerance]), :tolerance => (new_record? ? 0 : goas[order_article.id][:tolerance]),
:others_tolerance => order_article.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 end

View file

@ -99,6 +99,13 @@ class OrderArticle < ActiveRecord::Base
end end
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 private
def article_and_price_exist def article_and_price_exist

View file

@ -1,25 +0,0 @@
<script>
//<![CDATA[
$(function() {
<% for row in data_to_js(@ordering_data) %>
addData(<%= row.join(", ") %>);
<% end %>
setGroupBalance(<%= @ordering_data[:available_funds] %>);
// localization
setDecimalSeparator(",");
// configuration
setToleranceBehaviour(<%= Foodsoft.config[:tolerance_is_costly] %>);
// initialize javascript
updateBalance();
// show article info on row hover
<%#*$$('tbody tr').observer('hover', function() {%>
<%#*});%>
});
//]]>
</script>

View file

@ -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}/

View file

@ -1,9 +1,52 @@
- content_for :head do - 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 :lock_version
= f.hidden_field :order_id = f.hidden_field :order_id
= f.hidden_field :updated_by_user_id = f.hidden_field :updated_by_user_id
@ -19,36 +62,32 @@
%th{:style => "width:13px;"} %th{:style => "width:13px;"}
%th{:style => "width:4.5em;"} Preis %th{:style => "width:4.5em;"} Preis
%th{:style => "width:4.5em;"} Einheit %th{:style => "width:4.5em;"} Einheit
- unless @order.stockit?
%th{:style => "width:70px;"} Fehlende Einheiten %th{:style => "width:70px;"} Fehlende Einheiten
%th#col_required Menge %th#col_required Menge
- if not @order.stockit?
%th#col_tolerance Toleranz %th#col_tolerance Toleranz
- else
%th(style="width:20px") Verfügbar
%th#col_required Menge
%th{:style => "width:15px;"} Summe %th{:style => "width:15px;"} Summe
%tbody %tbody
- total = 0
- @order.articles_grouped_by_category.each do |category, order_articles| - @order.articles_grouped_by_category.each do |category, order_articles|
%tr{:style => "background-color:#EFEFEF"} %tr{:style => "background-color:#EFEFEF"}
%td{:style => "text-align:left"} %td{:style => "text-align:left"}
%b= h category %b= h category
%td{:colspan => "9"} %td{:colspan => "9"}
- order_articles.each do |order_article| - 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"} %tr{:class => "#{cycle('even', 'odd', :name => 'articles')} order-article", :valign => "top"}
%td.name= order_article.article.name %td.name= order_article.article.name
%td= h order_article.article.origin %td= h order_article.article.origin
%td= number_to_currency(@ordering_data[:order_articles][order_article.id][:price]) %td= number_to_currency(@ordering_data[:order_articles][order_article.id][:price])
%td= order_article.article.unit %td= order_article.article.unit
%td %td
%span{:id => "missing_units_#{order_article.id}"}
- if @order.stockit? - if @order.stockit?
- order_article.article.quantity_available = @ordering_data[:order_articles][order_article.id][:quantity_available]
- else - 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]) %span{:id => "missing_units_#{order_article.id}"}= @ordering_data[:order_articles][order_article.id][:missing_units]
- missing_units < 0 ? 0 : missing_units
%td.quantity %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]}/ %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] %span.used{:id => "q_used_#{order_article.id}"}= @ordering_data[:order_articles][order_article.id][:used_quantity]
@ -56,8 +95,8 @@
%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] %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-increase_quantity' => order_article.id}
%input{:type => 'button', :value => '-', 'data-decrease_quantity' => order_article.id} %input{:type => 'button', :value => '-', 'data-decrease_quantity' => order_article.id}
- unless @order.stockit?
%td.tolerance %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]}/ %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) - 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.used{:id => "t_used_#{order_article.id}"}= @ordering_data[:order_articles][order_article.id][:used_tolerance]
@ -65,8 +104,9 @@
%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] %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-increase_tolerance' => order_article.id}
%input{:type => 'button', :value => '-', 'data-decrease_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"} %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 .article-info
%h3= order_article.article.name %h3= order_article.article.name
@ -87,4 +127,26 @@
%br/ %br/
Notiz: #{order_article.article.note} Notiz: #{order_article.article.note}
%br/ %br/
= render "footer", :total => total #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}/

View file

@ -1,25 +0,0 @@
<div id="order-footer">
<div id="info-box">
</div>
<div id="total-sum">
<table>
<tr>
<td>Gesamtbetrag:</td>
<td class="currency"><span id="total_price"><%= total %></span> €</td>
</tr>
<tr>
<td>Verfügbares Guthaben:</td>
<td class="currency"><%= number_to_currency(@availableFunds) %></td>
</tr>
<tr>
<td>Neuer Kontostand:</td>
<td class="currency"><strong><span id="new_balance"><%= @ordergroup.account_balance - total %></span> €</strong></td>
</tr>
</table>
<div id="order-button">
<%= submit_tag( "Bestellung speichern", :id => 'submit_button' ) %></span> oder <%= link_to "abbrechen", :controller => 'ordering' %>
</div>
</div>
</div>
<input type="hidden" id="total_balance" name="total_balance" value="<%= @ordergroup.account_balance - total %>"/>
<input type="hidden" name="version" value="<%= @version %>"/>

View file

@ -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

View file

@ -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

View file

@ -35,16 +35,7 @@
= "Abgerechnet von #{@order.updated_by.nick}" = "Abgerechnet von #{@order.updated_by.nick}"
= link_to "Kommentare lesen/schreiben", "#comments" = link_to "Kommentare lesen/schreiben", "#comments"
// directly switch to active orders = render :partial => "switch_order", :locals => {:current_order => @order}
.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
// Article box // Article box
.single_column{:style => "clear:both; width:70em;"} .single_column{:style => "clear:both; width:70em;"}

View file

@ -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();

View file

@ -1,7 +1,7 @@
= simple_form_for @order do |f| = simple_form_for @order do |f|
.single_column .single_column
.box_title .box_title
%h2 Bestellung für #{@order.supplier.name} %h2 Bestellung für #{@order.name}
.column_content .column_content
= f.hidden_field :supplier_id = f.hidden_field :supplier_id
= f.input :note = f.input :note
@ -40,9 +40,9 @@
/ check if the article is selected / check if the article is selected
- included = @order.order_articles.detect { |order_article| order_article.article_id == article.id } - included = @order.order_articles.detect { |order_article| order_article.article_id == article.id }
- included_class = included ? ' selected' : '' - included_class = included ? ' selected' : ''
%tr{:class => cycle('even', 'odd') + ' click-me' + included_class, :id => article.id.to_s, :onclick => "checkRow('#{article.id}')"} %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}", :onclick => "checkRow('#{article.id}')" } %td= check_box_tag "order[article_ids][]", article.id, included, :id => "checkbox_#{article.id}"
%td=h article.name %td.click-me{'data-check-this' => "#checkbox_#{article.id}"}= article.name
%td=h truncate article.note, :length => 25 %td=h truncate article.note, :length => 25
- if @order.stockit? - if @order.stockit?
%td= "#{article.quantity_available} * #{article.unit}" %td= "#{article.quantity_available} * #{article.unit}"

View file

@ -1,3 +1,3 @@
- title "Neue Bestellung anlegen" - title "Neue Bestellung anlegen"
= render :partial => 'form', :locals => { :f => f } = render :partial => 'form'

View file

@ -9,6 +9,7 @@ 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 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: // Article data arrays:
var price = new Array(); var price = new Array();
@ -27,12 +28,16 @@ function setToleranceBehaviour(value) {
toleranceIsCostly = value; toleranceIsCostly = value;
} }
function setStockit(value) {
isStockit = value;
}
function setGroupBalance(amount) { function setGroupBalance(amount) {
groupBalance = amount; groupBalance = amount;
} }
function addData(orderArticleId, itemPrice, itemUnit, itemSubtotal, itemQuantityOthers, itemToleranceOthers, allocated, available) { function addData(orderArticleId, itemPrice, itemUnit, itemSubtotal, itemQuantityOthers, itemToleranceOthers, allocated, available) {
i = orderArticleId; var i = orderArticleId;
price[i] = itemPrice; price[i] = itemPrice;
unit[i] = itemUnit; unit[i] = itemUnit;
itemTotal[i] = itemSubtotal; itemTotal[i] = itemSubtotal;
@ -43,24 +48,26 @@ function addData(orderArticleId, itemPrice, itemUnit, itemSubtotal, itemQuantity
} }
function increaseQuantity(item) { function increaseQuantity(item) {
value = Number($('#q_' + item).val()) + 1; var value = Number($('#q_' + item).val()) + 1;
if (!isStockit || (value <= (quantityAvailable[item] - quantityOthers[item]))) {
update(item, value, $('#t_' + item).val()); update(item, value, $('#t_' + item).val());
} }
}
function decreaseQuantity(item) { function decreaseQuantity(item) {
value = Number($('#q_' + item).val()) - 1; var value = Number($('#q_' + item).val()) - 1;
if (value >= 0) { if (value >= 0) {
update(item, value, $('#t_' + item).val()); update(item, value, $('#t_' + item).val());
} }
} }
function increaseTolerance(item) { function increaseTolerance(item) {
value = Number($('#t_' + item).val()) + 1; var value = Number($('#t_' + item).val()) + 1;
update(item, $('#q_' + item).val(), value); update(item, $('#q_' + item).val(), value);
} }
function decreaseTolerance(item) { function decreaseTolerance(item) {
value = Number($('#t_' + item).val()) - 1; var value = Number($('#t_' + item).val()) - 1;
if (value >= 0) { if (value >= 0) {
update(item, $('#q_' + item).val(), value); update(item, $('#q_' + item).val(), value);
} }
@ -75,7 +82,7 @@ function update(item, quantity, tolerance) {
$('#t_' + item).val(tolerance); $('#t_' + item).val(tolerance);
// calculate how many units would be ordered in total // 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))) { if (unitCompletedFromTolerance(unit[item], quantityOthers[item] + Number(quantity), toleranceOthers[item] + Number(tolerance))) {
$('#units_' + item).html('<span style=\"color:grey\">' + String(units) + '</span>'); $('#units_' + item).html('<span style=\"color:grey\">' + String(units) + '</span>');
} else { } else {
@ -83,8 +90,8 @@ function update(item, quantity, tolerance) {
} }
// update used/unused quantity // update used/unused quantity
available = Math.max(0, units * unit[item] - quantityOthers[item]); var available = Math.max(0, units * unit[item] - quantityOthers[item]);
q_used = Math.min(available, quantity); var q_used = Math.min(available, quantity);
// ensure that at least the amout of items this group has already been allocated is used // ensure that at least the amout of items this group has already been allocated is used
if (quantity >= itemsAllocated[item] && q_used < itemsAllocated[item]) { if (quantity >= itemsAllocated[item] && q_used < 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])); $('#price_' + item + '_display').html(asMoney(itemTotal[item]));
// update missing units // 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) { if (missing_units < 0) {
missing_units = 0; missing_units = 0;
} }
@ -121,77 +128,37 @@ function update(item, quantity, tolerance) {
updateBalance(); 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) { function asMoney(amount) {
return String(amount.toFixed(2)).replace(/\./, ","); return String(amount.toFixed(2)).replace(/\./, ",");
} }
function calcUnits(unitSize, quantity, tolerance) { function calcUnits(unitSize, quantity, tolerance) {
units = Math.floor(quantity / unitSize) var units = Math.floor(quantity / unitSize)
remainder = quantity % unitSize var remainder = quantity % unitSize
return units + ((remainder > 0) && (remainder + tolerance >= unitSize) ? 1 : 0) return units + ((remainder > 0) && (remainder + tolerance >= unitSize) ? 1 : 0)
} }
function unitCompletedFromTolerance(unitSize, quantity, tolerance) { function unitCompletedFromTolerance(unitSize, quantity, tolerance) {
remainder = quantity % unitSize var remainder = quantity % unitSize
return (remainder > 0 && (remainder + tolerance >= unitSize)); return (remainder > 0 && (remainder + tolerance >= unitSize));
} }
function updateBalance() { function updateBalance() {
// update total price and order balance // update total price and order balance
total = 0; var total = 0;
for (i in itemTotal) { for (i in itemTotal) {
total += itemTotal[i]; total += itemTotal[i];
} }
$('#total_price').html(asMoney(total)); $('#total_price').html(asMoney(total));
balance = groupBalance - total; var balance = groupBalance - total;
$('#new_balance').html(asMoney(balance)); $('#new_balance').html(asMoney(balance));
$('#total_balance').val(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) { if (balance < 0) {
bgcolor = '#FF0000'; bgcolor = '#FF0000';
$('#submit_button').disabled = true; $('#submit_button').disabled = true;
} else { } else {
bgcolor = '';
$('#submit_button').disabled = false; $('#submit_button').disabled = false;
} }
// update bgcolor // 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() { $(function() {
$('input[data-increase_quantity]').click(function() { $('input[data-increase_quantity]').click(function() {
increaseQuantity($(this).data('increase_quantity')); increaseQuantity($(this).data('increase_quantity'));
}) });
$('input[data-decrease_quantity]').click(function() { $('input[data-decrease_quantity]').click(function() {
decreaseQuantity($(this).data('decrease_quantity')); decreaseQuantity($(this).data('decrease_quantity'));
}) });
$('input[data-increase_tolerance]').click(function() { $('input[data-increase_tolerance]').click(function() {
increaseTolerance($(this).data('increase_tolerance')); increaseTolerance($(this).data('increase_tolerance'));
}) });
$('input[data-decrease_tolerance]').click(function() { $('input[data-decrease_tolerance]').click(function() {
decreaseTolerance($(this).data('decrease_tolerance')); 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?'));
});
});

View file

@ -77,7 +77,7 @@ option {
border-top: 1px solid #d7d7d7; border-top: 1px solid #d7d7d7;
margin: 0.2em 0; } margin: 0.2em 0; }
span.click-me { .click-me {
cursor: pointer; } cursor: pointer; }
.left { .left {
@ -245,8 +245,6 @@ table {
color: green; } color: green; }
table tr.selected, table tr.active { table tr.selected, table tr.active {
background-color: #ffffc2; } background-color: #ffffc2; }
table tr.click-me {
cursor: pointer; }
table tr.ignored { table tr.ignored {
color: grey; } color: grey; }
table tr.success { table tr.success {
@ -400,7 +398,7 @@ table#order {
-webkit-border-radius: 3px; -webkit-border-radius: 3px;
padding: 0; } padding: 0; }
table#order th#col_required, table#order th#col_tolerance { 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 { table#order th#col_packages, table#order th#col_left_units {
width: 50px; } width: 50px; }
table#order td.quantity, table#order td.tolerance { table#order td.quantity, table#order td.tolerance {

View file

@ -88,7 +88,7 @@ option
border-top: 1px solid #D7D7D7 border-top: 1px solid #D7D7D7
margin: .2em 0 margin: .2em 0
span.click-me .click-me
cursor: pointer cursor: pointer
.left .left
float: left float: left
@ -268,8 +268,6 @@ table
:color #008000 :color #008000
tr.selected, tr.active tr.selected, tr.active
:background-color #ffffc2 :background-color #ffffc2
tr.click-me
:cursor pointer
tr.ignored tr.ignored
color: grey color: grey
tr.success tr.success
@ -439,7 +437,7 @@ table#order
-webkit-border-radius: 3px -webkit-border-radius: 3px
padding: 0 padding: 0
th#col_required, th#col_tolerance th#col_required, th#col_tolerance
:width 140px :width 145px
th#col_packages, th#col_left_units th#col_packages, th#col_left_units
:width 50px :width 50px
td.quantity, td.tolerance td.quantity, td.tolerance