This was an existing script written in PHP and Jquery, basically, what it does is the calculations of price and quantity for a Point of sale system.
I made a few changes so that the end-user can enter the price, as they enter the quantity amount.
Now, after I made the changes, what happens is, supposedly, the price of commodity is $1000 and if I change it to let's say $1500, my quantity becomes 501 and the price becomes $1750.
I have no idea, what's happening inside. Can anybody help me out with this?
Thanks
function add_row() {
$('button[id^="product"]').click(function() {
if (count >= 1000) {
bootbox.alert("<?php echo $this->lang->line('qty_limit'); ?>");
return false;
}
if (an >= 51) {
bootbox.alert("<?php echo $this->lang->line('max_pro_reached'); ?>");
$('#gmail_loading').hide();
var divElement = document.getElementById('protbldiv');
divElement.scrollTop = divElement.scrollHeight;
return false;
}
$('#gmail_loading').show();
var v = $(this).val();
$.ajax({
type: "get",
async: false,
url: "index.php?module=pos&view=price",
data: {code: v},
dataType: "json",
success: function(data) {
item_price = parseFloat(data.price);
prod_name = data.name;
prod_code = data.code;
pr_tax = data.tax_rate;
}
});
var leng = $(this).attr('id').length;
var last = $(this).attr('id').substr(leng - 4);
var pric = 'price' + last;
var quan = 'quantity' + last;
var code = 'code' + last;
var prod_tax = 0;
$.each(tax_rates, function() {
if (pr_tax) {
if (this.id == pr_tax.id) {
prod_tax = this.id;
}
}
});
var pt = prod_tax ? prod_tax : DT;
var newTr = $('<tr id="row_' + count + last + '"></tr>');
newTr.html('<td id="satu" style="text-align:center; width: 27px;"><button type="button" class="del_row" id="del-' + count + last + '" value="' + item_price + '"><i class="icon-trash"></i></button></td><td><input type="hidden" name="product' + count + '" value="' + prod_code + '" id="product-' + count + last + '"><input type="hidden" name="serial' + count + '" value="" id="serial-' + count + last + '"><input type="hidden" name="tax_rate' + count + '" value="' + pt + '" id="tax_rate-' + count + last + '"><input type="hidden" name="discount' + count + '" value="<?php echo DEFAULT_DISCOUNT; ?>" id="discount-' + count + last + '"><a href="#" id="model-' + count + last + '" class="code">' + prod_name + '</a><input type="hidden" name="price' + count + '" value="' + parseFloat(item_price).toFixed(2) + '" id="oprice-' + count + last + '"></td><td style="text-align:center;"><input class="keyboard" onClick="this.select();" name="quantity' + count + '" type="text" value="1" autocomplete="off" id="quantity-' + count + last + '"></td><td style="padding-right: 10px; text-align:right;"><input class="keyboard" onClick="this.select();" autocomplete="off" type="text" class="price" name="unit_price' + count + '" value="' + parseFloat(item_price).toFixed(2) + '" id="price-' + count + last + '"></td>');
newTr.appendTo("#saletbl");
total += item_price;
current = parseFloat(total).toFixed(2);
<?php if (TAX1) { ?>
if (pr_tax) {
if (pr_tax.type == 2) {
pr_tax_rate = parseFloat(pr_tax.rate);
}
if (pr_tax.type == 1) {
pr_tax_rate = (item_price * parseFloat(pr_tax.rate)) / 100;
}
tax_value += pr_tax_rate;
} else {
if (tax_type == 2) {
new_tax_value = tax_rate;
}
if (tax_type == 1) {
new_tax_value = (item_price * tax_rate) / 100;
}
tax_value += new_tax_value
}
current_tax = parseFloat(tax_value).toFixed(2);
<?php } ?>
<?php if (TAX2) { ?>
if (tax_type2 == 2) {
tax_value2 = tax_rate2;
}
if (tax_type2 == 1) {
tax_value2 = (total * tax_rate2) / 100;
}
current_tax2 = parseFloat(tax_value2).toFixed(2);
<?php } ?>
<?php if (DISCOUNT_METHOD == 1 && DISCOUNT_OPTION == 1) { ?>
if (discount_type == 2) {
new_discount_value = discount;
}
if (discount_type == 1) {
new_discount_value = (total * discount) / 100;
}
total_discount = new_discount_value;
current_discount = parseFloat(total_discount).toFixed(2);
<?php } elseif (DISCOUNT_METHOD == 2 && DISCOUNT_OPTION == 1) { ?>
if (discount_type == 2) {
new_discount_value = discount;
}
if (discount_type == 1) {
new_discount_value = ((total + tax_value + tax_value2) * discount) / 100;
}
total_discount = new_discount_value;
current_discount = parseFloat(total_discount).toFixed(2);
<?php } ?>
<?php if (DISCOUNT_OPTION == 2 && DISCOUNT_METHOD == 1) { ?>
if (discount_type2 == 2) {
new_discount_value2 = discount2;
}
if (discount_type2 == 1) {
new_discount_value2 = (item_price * discount2) / 100;
}
total_discount += new_discount_value2;
current_discount = parseFloat(total_discount).toFixed(2);
<?php } elseif (DISCOUNT_OPTION == 2 && DISCOUNT_METHOD == 2) { ?>
if (discount_type2 == 2) {
new_discount_value2 = discount2;
}
if (discount_type2 == 1) {
new_discount_value2 = ((item_price + new_tax_value) * discount2) / 100;
}
total_discount += new_discount_value2;
current_discount = parseFloat(total_discount).toFixed(2);
<?php } ?>
var g_total = total + tax_value + tax_value2 - total_discount;
grand_total = parseFloat(g_total).toFixed(2);
$("#total-payable").empty();
$("#total-payable").append(grand_total);
$("#total").empty();
$("#total").append(current);
$("#count").empty();
$("#count").append(count);
<?php if (TAX1) { ?>$("#tax").empty();
$("#tax").append(current_tax);<?php } ?>
<?php if (TAX2) { ?>$("#tax2").empty(); $("#tax2").append(current_tax2);<?php } ?>
<?php if (DISCOUNT_OPTION == 1 || DISCOUNT_OPTION == 2) { ?>
$("#ds").empty(); $("#ds").append(current_discount);
<?php } ?>
count++;
an++;
var divElement = document.getElementById('protbldiv');
divElement.scrollTop = divElement.scrollHeight;
$('#gmail_loading').hide();
});
}
function key_pad() {
$('.keyboard').keyboard({
restrictInput: true,
preventPaste: true,
autoAccept: true,
alwaysOpen: false,
openOn: 'click',
layout: 'costom',
display: {
'a': '\u2714:Accept (Enter)',
'accept': 'Accept:Accept (Enter)',
'b': '\u2190:Backspace',
'bksp': 'Bksp:Backspace',
'c': '\u2716:Cancel (Esc)',
'cancel': 'Cancel:Cancel (Esc)',
'clear': 'C:Clear'
},
position: {
of: null,
my: 'center top',
at: 'center top',
at2: 'center bottom'
},
usePreview: false,
customLayout: {
'default': [
'1 2 3 {b}',
'4 5 6 {clear}',
'7 8 9 0',
'{accept} {cancel}'
]
},
beforeClose: function(e, keyboard, el, accepted) {
var before_qty = parseInt(keyboard.originalContent);
var after_qty = parseInt(el.value);
var row_id = $(this).attr('id');
var sp_id = row_id.split("-");
var id_no = sp_id[1];
var p = '#price-' + id_no;
var row_price = parseFloat($.trim($(p).val()));
if (before_qty == 1) {
product_price = row_price;
}
if (before_qty > 1) {
product_price = (row_price / before_qty);
}
var gross_total = after_qty * product_price;
var b_count = (count - before_qty);
var a_count = (b_count + after_qty);
count = a_count;
var b_total = (total - row_price);
var a_total = (b_total + gross_total);
total = a_total;
gross_total = parseFloat(gross_total).toFixed(2);
$(p).val(gross_total);
current = parseFloat(total).toFixed(2);
<?php if (TAX1) { ?>
var pr_tax_id = $('#tax_rate-' + id_no).val();
$.each(tax_rates, function() {
if (this.id == pr_tax_id) {
new_tax_rate = parseFloat(this.rate);
new_tax_type = parseFloat(this.type);
}
});
if (new_tax_type == 2) {
new_tax_value = new_tax_rate;
}
if (new_tax_type == 1) {
new_tax_value = (product_price * new_tax_rate) / 100;
}
tax_value = tax_value - (new_tax_value * before_qty);
tax_value = tax_value + (new_tax_value * after_qty);
current_tax = parseFloat(tax_value).toFixed(2);
<?php } else { ?>
new_tax_value = 0;
<?php } ?>
<?php if (DISCOUNT_OPTION == 2) { ?>
var pr_ds_id = $('#discount-' + id_no).val();
$.each(discounts, function() {
if(this.id == pr_ds_id){
new_discount_rate = parseFloat(this.discount);
new_discount_type = parseFloat(this.type);
}
});
<?php if (DISCOUNT_METHOD == 1) { ?>
if (new_discount_type == 2) {
new_pr_discount = new_discount_rate;
}
if (new_discount_type == 1) {
new_pr_discount = (product_price * new_discount_rate) / 100;
}
<?php } elseif (DISCOUNT_METHOD == 2) { ?>
if (new_discount_type == 2) {
new_pr_discount = new_discount_rate;
}
if (new_discount_type == 1) {
new_pr_discount = ((product_price + new_tax_value) * new_discount_rate) / 100;
}
<?php } ?>
total_discount = total_discount - (new_pr_discount * before_qty);
total_discount = total_discount + (new_pr_discount * after_qty);
current_discount = parseFloat(total_discount).toFixed(2);
<?php } ?>
<?php if (TAX2) { ?>
if (tax_type2 == 2) {
tax_value2 = tax_rate2;
}
if (tax_type2 == 1) {
tax_value2 = (total * tax_rate2) / 100;
}
current_tax2 = parseFloat(tax_value2).toFixed(2);
<?php } ?>
<?php if (DISCOUNT_METHOD == 1 && DISCOUNT_OPTION == 1) { ?>
if (discount_type == 2) {
new_discount_value = discount;
}
if (discount_type == 1) {
new_discount_value = (total * discount) / 100;
}
total_discount = new_discount_value;
current_discount = parseFloat(total_discount).toFixed(2);
<?php } elseif (DISCOUNT_METHOD == 2 && DISCOUNT_OPTION == 1) { ?>
if (discount_type == 2) {
new_discount_value = discount;
}
if (discount_type == 1) {
new_discount_value = ((total + tax_value + tax_value2) * discount) / 100;
}
total_discount = new_discount_value;
current_discount = parseFloat(total_discount).toFixed(2);
<?php } ?>
var g_total = (total + tax_value + tax_value2) - total_discount;
grand_total = parseFloat(g_total).toFixed(2);
$("#total-payable").empty();
$("#total").empty();
$("#count").empty();
<?php if (TAX1) { ?>$("#tax").empty();
$("#tax").append(current_tax);<?php } ?>
<?php if (TAX2) { ?>$("#tax2").empty(); $("#tax2").append(current_tax2);<?php } ?>
<?php if (DISCOUNT_OPTION == 1 || DISCOUNT_OPTION == 2) { ?>
$("#ds").empty(); $("#ds").append(current_discount);
<?php } ?>
$("#total-payable").append(grand_total);
$("#total").append(current);
$("#count").append(count - 1);
}
});
}