views:

1100

answers:

0

Hi ,

Having a small issue with the following bit of jquery. Its used to calculate line items and aggregate totals.

e.g.

line_item 1: qty x rate + vat (if vatable) = subtotal line_item 1: qty x rate + vat (if vatable) = subtotal

subtotal: sum(qtys x rates) vat: sum (subtotal x vat formula) total : sum(subtotals)

Heres the code, any help will be much appreciated;

function vatable(checkbox) {
    if(checkbox.attr('checked')) {
        return 14/100;
    } else {
        return 0;
    }
}

function recalc(){
    jQuery("input[class^=ltotal]").calc(
        "qty * rate + (((qty * rate) * vat))",
        {
            qty: jQuery("input[class^=lqty]"),
            rate: jQuery("input[class^=lrate]"),
            vat : vatable(jQuery("input.lvat"))
        },
        function (s){
            return s.toFixed(2);
        },
        function ($this){
            var sum = $this.sum();
            if(jQuery("input[class^=lvat]").attr('checked')) {
                jQuery("input[class=subtotal]").calc( ((sum/114)*100).toFixed(2));
                jQuery("input[class=vat]").calc(((sum/114)*14).toFixed(2));
                jQuery("input[class=total]").calc((sum).toFixed(2));
            } else {
                jQuery("input[class=subtotal]").calc( sum.toFixed(2) );
                jQuery("input[class=vat]").calc(0);
                jQuery("input[class=total]").calc( sum.toFixed(2) );
            }
        }
    );
}

$(document).ready( function () {
    $('input[class^=lqty]').bind('keyup', recalc);
    $('input[class^=lvat]').click(recalc);
    $('input[class^=lrate]').bind('keyup', recalc);
    recalc();
});