add product sell discount js changes

This commit is contained in:
2015-10-20 17:23:52 +02:00
parent 1a1477b26b
commit f2e30779f0
10 changed files with 165 additions and 90 deletions

View File

@@ -19,6 +19,7 @@ function ProductSell(o){
message_paid: 'Tételek fizetve',
/**list of sold items by current user*/
sold_items: [],
discounts: [],
};
init();
@@ -34,12 +35,17 @@ function ProductSell(o){
setFocus();
addEnterPressedBehaviours();
addBehaviorCountChangedListener();
addBehaviourDisocuntChanged();
createUserSoldItemsTable();
addBehaviourBtnPaid();
productChanged();
addDocumentKeypressedListener();
}
function addBehaviourDisocuntChanged(){
$("#productsaleform-id_discount").change(refreshCalculatedValues);
}
function addDocumentKeypressedListener(){
$( document ).keypress(function( event ) {
@@ -235,15 +241,23 @@ function ProductSell(o){
}
function refreshCalculatedValues(){
var count,price;
var count,price,id_discount,discount;
var table;
table = $('.table-product');
count = $("#productsaleform-count").val();
id_discount = $("#productsaleform-id_discount").val();
app.discount = findDiscount( id_discount );
count = +count;
if ( isNaN(count)){
count = 0;
}
price = count * app.product.sale_price;
discount = calcDiscount(price, app.discount);
price = deductDiscount(price,discount);
price = normalizePrice(price);
table.find('.product-count').html(count);
table.find('.product-price').html(price);
}
@@ -345,13 +359,54 @@ function ProductSell(o){
});
}
function createUserSoldItemsTable(){
$('.sold-items-container').transferList({
'transfers' : app.defaults.sold_items
});
}
function findDiscount(id_discount){
var discount;
discount = null;
$.each( app.defaults.discounts , function(e,i){
if ( e.id_discount == id_discount ){
discount = e;
}
});
return discount;
}
function calcDiscount(price,discount){
var result, discountMoney;
result = 0;
if ( discount ){
result = price * ( discount.value / 100 );
}
return result;
}
function deductDiscount( price , discount ){
var result;
result = price - discount;
return result;
}
function normalizePrice( price ){
var result;
result = hufRound(price);
return result;
}
function hufRound(x)
{
if (x > 0) {
return (x % 5) >= 2.5 ? parseInt(x / 5) * 5 + 5 : parseInt(x / 5) * 5;
} else {
return ((Math.abs(x) % 5) >= 2.5 ? parseInt(Math.abs(x) / 5) * 5 + 5 : parseInt(Math.abs(x) / 5) * 5)*-1;
}
}
}