2009-01-06 11:49:19 +01:00
// JavaScript that handles the dynamic ordering quantities on the ordering page.
//
// In a JavaScript block on the actual view, define the article data by calls to setData().
// You should also set the available group balance through setGroupBalance(amount).
//
// Call setDecimalSeparator(char) to overwrite the default character "." with a localized value.
var modified = false // indicates if anything has been clicked on this page
2011-06-19 19:56:04 +02:00
var groupBalance = 0 ; // available group money
2009-01-06 11:49:19 +01:00
var decimalSeparator = "." ; // default decimal separator
2011-06-19 19:56:04 +02:00
var toleranceIsCostly = true ; // default tolerance behaviour
var isStockit = false ; // Wheter the order is from stock oder normal supplier
2009-01-06 11:49:19 +01:00
// Article data arrays:
var price = new Array ( ) ;
var unit = new Array ( ) ; // items per order unit
var itemTotal = new Array ( ) ; // total item price
2011-06-13 16:23:56 +02:00
var quantityOthers = new Array ( ) ;
2009-01-06 11:49:19 +01:00
var toleranceOthers = new Array ( ) ;
var itemsAllocated = new Array ( ) ; // how many items the group has been allocated and should definitely get
2009-02-06 16:26:35 +01:00
var quantityAvailable = new Array ( ) ; // stock_order. how many items are currently in stock
2009-01-06 11:49:19 +01:00
function setDecimalSeparator ( character ) {
2011-06-13 16:23:56 +02:00
decimalSeparator = character ;
2009-01-06 11:49:19 +01:00
}
2009-08-02 11:00:57 +02:00
function setToleranceBehaviour ( value ) {
toleranceIsCostly = value ;
}
2011-06-19 19:56:04 +02:00
function setStockit ( value ) {
isStockit = value ;
}
2009-01-06 11:49:19 +01:00
function setGroupBalance ( amount ) {
2011-06-13 16:23:56 +02:00
groupBalance = amount ;
2009-01-06 11:49:19 +01:00
}
2011-06-19 15:30:33 +02:00
function addData ( orderArticleId , itemPrice , itemUnit , itemSubtotal , itemQuantityOthers , itemToleranceOthers , allocated , available ) {
2011-06-19 19:56:04 +02:00
var i = orderArticleId ;
2011-06-13 16:23:56 +02:00
price [ i ] = itemPrice ;
unit [ i ] = itemUnit ;
itemTotal [ i ] = itemSubtotal ;
quantityOthers [ i ] = itemQuantityOthers ;
toleranceOthers [ i ] = itemToleranceOthers ;
itemsAllocated [ i ] = allocated ;
quantityAvailable [ i ] = available ;
2009-01-06 11:49:19 +01:00
}
function increaseQuantity ( item ) {
2011-06-19 19:56:04 +02:00
var value = Number ( $ ( '#q_' + item ) . val ( ) ) + 1 ;
if ( ! isStockit || ( value <= ( quantityAvailable [ item ] - quantityOthers [ item ] ) ) ) {
update ( item , value , $ ( '#t_' + item ) . val ( ) ) ;
}
2009-01-06 11:49:19 +01:00
}
function decreaseQuantity ( item ) {
2011-06-19 19:56:04 +02:00
var value = Number ( $ ( '#q_' + item ) . val ( ) ) - 1 ;
2009-01-06 11:49:19 +01:00
if ( value >= 0 ) {
2011-06-13 16:23:56 +02:00
update ( item , value , $ ( '#t_' + item ) . val ( ) ) ;
2009-01-06 11:49:19 +01:00
}
}
function increaseTolerance ( item ) {
2011-06-19 19:56:04 +02:00
var value = Number ( $ ( '#t_' + item ) . val ( ) ) + 1 ;
2011-06-13 16:23:56 +02:00
update ( item , $ ( '#q_' + item ) . val ( ) , value ) ;
2009-01-06 11:49:19 +01:00
}
function decreaseTolerance ( item ) {
2011-06-19 19:56:04 +02:00
var value = Number ( $ ( '#t_' + item ) . val ( ) ) - 1 ;
2009-01-06 11:49:19 +01:00
if ( value >= 0 ) {
2011-06-13 16:23:56 +02:00
update ( item , $ ( '#q_' + item ) . val ( ) , value ) ;
2009-01-06 11:49:19 +01:00
}
}
function update ( item , quantity , tolerance ) {
2011-06-13 16:23:56 +02:00
// set modification flag
modified = true
// update hidden input fields
$ ( '#q_' + item ) . val ( quantity ) ;
$ ( '#t_' + item ) . val ( tolerance ) ;
// calculate how many units would be ordered in total
2011-06-19 19:56:04 +02:00
var units = calcUnits ( unit [ item ] , quantityOthers [ item ] + Number ( quantity ) , toleranceOthers [ item ] + Number ( tolerance ) ) ;
2011-06-13 16:23:56 +02:00
if ( unitCompletedFromTolerance ( unit [ item ] , quantityOthers [ item ] + Number ( quantity ) , toleranceOthers [ item ] + Number ( tolerance ) ) ) {
2011-06-19 15:30:33 +02:00
$ ( '#units_' + item ) . html ( '<span style=\"color:grey\">' + String ( units ) + '</span>' ) ;
2011-06-13 16:23:56 +02:00
} else {
$ ( '#units_' + item ) . html ( String ( units ) ) ;
}
// update used/unused quantity
2011-06-19 19:56:04 +02:00
var available = Math . max ( 0 , units * unit [ item ] - quantityOthers [ item ] ) ;
var q _used = Math . min ( available , quantity ) ;
2011-06-13 16:23:56 +02:00
// 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_unused_' + item ) . html ( String ( quantity - q _used ) ) ;
$ ( '#q_total_' + item ) . html ( String ( Number ( quantity ) + quantityOthers [ item ] ) ) ;
// update used/unused tolerance
if ( unit [ item ] > 1 ) {
available = Math . max ( 0 , available - q _used - toleranceOthers [ item ] ) ;
t _used = Math . min ( available , tolerance ) ;
$ ( '#t_used_' + item ) . html ( String ( t _used ) ) ;
$ ( '#t_unused_' + item ) . html ( String ( tolerance - t _used ) ) ;
$ ( '#t_total_' + item ) . html ( String ( Number ( tolerance ) + toleranceOthers [ item ] ) ) ;
}
// update total price
2009-08-02 11:00:57 +02:00
if ( toleranceIsCostly == true ) {
itemTotal [ item ] = price [ item ] * ( Number ( quantity ) + Number ( tolerance ) ) ;
} else {
itemTotal [ item ] = price [ item ] * ( Number ( quantity ) ) ;
}
2011-06-13 16:23:56 +02:00
$ ( '#price_' + item + '_display' ) . html ( asMoney ( itemTotal [ item ] ) ) ;
// update missing units
2011-06-19 19:56:04 +02:00
var missing _units = unit [ item ] - ( ( ( quantityOthers [ item ] + Number ( quantity ) ) % unit [ item ] ) + Number ( tolerance ) + toleranceOthers [ item ] )
2011-06-13 16:23:56 +02:00
if ( missing _units < 0 ) {
missing _units = 0 ;
}
$ ( '#missing_units_' + item ) . html ( String ( missing _units ) ) ;
2009-01-06 11:49:19 +01:00
// update balance
updateBalance ( ) ;
2011-06-13 16:23:56 +02:00
}
2009-01-06 11:49:19 +01:00
function asMoney ( amount ) {
2011-06-13 16:23:56 +02:00
return String ( amount . toFixed ( 2 ) ) . replace ( /\./ , "," ) ;
2009-01-06 11:49:19 +01:00
}
function calcUnits ( unitSize , quantity , tolerance ) {
2011-06-19 19:56:04 +02:00
var units = Math . floor ( quantity / unitSize )
var remainder = quantity % unitSize
2011-06-13 16:23:56 +02:00
return units + ( ( remainder > 0 ) && ( remainder + tolerance >= unitSize ) ? 1 : 0 )
2009-01-06 11:49:19 +01:00
}
function unitCompletedFromTolerance ( unitSize , quantity , tolerance ) {
2011-06-19 19:56:04 +02:00
var remainder = quantity % unitSize
2009-01-06 11:49:19 +01:00
return ( remainder > 0 && ( remainder + tolerance >= unitSize ) ) ;
}
function updateBalance ( ) {
// update total price and order balance
2011-06-19 19:56:04 +02:00
var total = 0 ;
2011-06-19 15:30:33 +02:00
for ( i in itemTotal ) {
2009-01-06 11:49:19 +01:00
total += itemTotal [ i ] ;
2011-06-13 16:23:56 +02:00
}
$ ( '#total_price' ) . html ( asMoney ( total ) ) ;
2011-06-19 19:56:04 +02:00
var balance = groupBalance - total ;
2011-06-13 16:23:56 +02:00
$ ( '#new_balance' ) . html ( asMoney ( balance ) ) ;
$ ( '#total_balance' ) . val ( asMoney ( balance ) ) ;
2011-06-19 19:56:04 +02:00
// determine bgcolor and submit button state according to balance
var bgcolor = '' ;
2009-01-06 11:49:19 +01:00
if ( balance < 0 ) {
bgcolor = '#FF0000' ;
2011-06-13 16:23:56 +02:00
$ ( '#submit_button' ) . disabled = true ;
2009-01-06 11:49:19 +01:00
} else {
2011-06-13 16:23:56 +02:00
$ ( '#submit_button' ) . disabled = false ;
2009-01-06 11:49:19 +01:00
}
// update bgcolor
2011-06-19 15:30:33 +02:00
for ( i in itemTotal ) {
2011-06-13 16:23:56 +02:00
$ ( '#ltd_price_' + i ) . css ( 'background-color' , bgcolor ) ;
}
}
2009-01-06 11:49:19 +01:00
2011-06-19 15:30:33 +02:00
$ ( function ( ) {
$ ( 'input[data-increase_quantity]' ) . click ( function ( ) {
increaseQuantity ( $ ( this ) . data ( 'increase_quantity' ) ) ;
2011-06-19 19:56:04 +02:00
} ) ;
2011-06-19 15:30:33 +02:00
$ ( 'input[data-decrease_quantity]' ) . click ( function ( ) {
decreaseQuantity ( $ ( this ) . data ( 'decrease_quantity' ) ) ;
2011-06-19 19:56:04 +02:00
} ) ;
2011-06-19 15:30:33 +02:00
$ ( 'input[data-increase_tolerance]' ) . click ( function ( ) {
increaseTolerance ( $ ( this ) . data ( 'increase_tolerance' ) ) ;
2011-06-19 19:56:04 +02:00
} ) ;
2011-06-19 15:30:33 +02:00
$ ( 'input[data-decrease_tolerance]' ) . click ( function ( ) {
decreaseTolerance ( $ ( this ) . data ( 'decrease_tolerance' ) ) ;
2011-06-19 19:56:04 +02:00
} ) ;
$ ( '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?' ) ) ;
} ) ;
} ) ;