Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    341
    Thanks
    22
    Thanked 1 Time in 1 Post

    adding and excluding sales taxes based on checkbox value

    Hello:

    I need some assistance with a jquery/javascript code. I have a form with chain-linked list box of product, and prices. Each product is inside a <tr> which are added as needed. So there can be many items added to the form. The total of the items cost (qty*cost) from all added <tr> goes inside an input box called subtotal. Once I have the total, another checkbox if check adds a percentage of tax based on that subtotal.

    the code to calculate my subtotal of all added <tr> is:

    PHP Code:
    function update() {
        var 
    subtotal 0;
        var 
    errorMsg "N/A";
        
        $(
    '.price' $item_rows_container).each(function(i) {
           var 
    row = $(this).closest('tr');
            var 
    price row.find('.costbox').val().fromCurrency() * Number(row.find('.qtybox').val());
            $(
    this).html(isNaN(price) ? errorMsg price.toCurrency());//Display value or 'error' if calculation failed
           
    subtotal += price.toCurrency().fromCurrency();
        
        });
        
        $(
    '#subtotal').html(isNaN(subtotal) ? errorMsg subtotal.toCurrency()); 
    code to calculate the tax on the subtotal:

    PHP Code:
    var tax = ($('#taxbox').attr('checked')) ? (subtotal taxRate) : 0;
        $(
    '#tax').val(isNaN(tax) ? errorMsg tax.toCurrency()); 
    Now inside each <tr>, i have a <td> with a checkbox which when checked, I would like to exclude the total of this <tr> from the form's subtotal. I would then want a separate inputbox with the total of all items that have been check as tax exempted.

    To finalize the form's transaction, I would have a box of a sutotal of taxable items, a box for the total of non-taxable items, a box of the tax amount and then add all three for my grand total.

    Can anyone help me on this, I'm appreciative of any assistance.

    Best,
    Mossa
    Last edited by mbarandao; 10-31-2013 at 11:53 PM.

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,175
    Thanks
    23
    Thanked 601 Times in 600 Posts
    Could you please post the html that this is working with.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #3
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    341
    Thanks
    22
    Thanked 1 Time in 1 Post
    Sunfighter, thanks for chiming in. My working file is quite large 1200 lines. Is it ok if I PM the file?

    Mossa--

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,175
    Thanks
    23
    Thanked 601 Times in 600 Posts
    If you use the # icon above you in the message box, it should hold that. If not split the file and post the second half in a second post. That way everyone one this form can see it and help. And yes you can PM me.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #5
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    341
    Thanks
    22
    Thanked 1 Time in 1 Post
    perhaps a link to the form would be helpful in understanding:

    https://autotechpro.net/autotechPro_...Production.php

    On the form, select from the two drown down menus, within that <tr> will be a checkbox for making the item taxable or nontaxable. at the bottom left, there is an icon for a register, click to get the option of calculating the sales on the order and then click on "Tax?".

    That is the logic I am attempting to achieve. if the item is not selected as taxable, once the second checkbox in the register is checked, there shouldn't be any tax calculations.

    let me know if this is helpful in understanding and visually seeing that which I'm trying to achieve.

    Your thoughts!

  • #6
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    341
    Thanks
    22
    Thanked 1 Time in 1 Post
    The code specific to the task I'm attempting to achieve.

    PHP Code:
        var rate =0.2;
        var 
    taxRate rate;
        function 
    update() {
        var 
    subtotal 0;
        var 
    errorMsg "Error";
        
        $(
    '.price' $item_rows_container).each(function(i) {
           var 
    row = $(this).closest('tr');
            
        
            var 
    price row.find('.costbox').val().fromCurrency() * Number(row.find('.qtybox').val());
            $(
    this).html(isNaN(price) ? errorMsg price.toCurrency());//Display value or 'error' if calculation failed
           
    subtotal += price.toCurrency().fromCurrency();//"double-shuffle" to ensure the calculation is consistent with the displayed data.
        
        
    });
        
        $(
    '#subtotal').html(isNaN(subtotal) ? errorMsg subtotal.toCurrency());//Display value or 'error' if calculation failed
        
    var taxes = [];
        var 
    is_checked false;
        $(
    $item_rows_container).each(function(i)
        {
          
    is_checked = $(this).find('.taxable').is(':checked') || false;
        if(!
    is_checked)
        {
        $(
    'table tbody#item_rows tr.item-row').each(function(i) {
        var 
    row = $(this).closest('tr');
            
    // This <tr> should be included in the tax calculation.
                    
    var price row.find('.costbox').val().fromCurrency() * Number(row.find('.qtybox').val());
            
    taxes.push(price);
            
        });
        }
        });
        
        
    //doMath (tax)  $('#taxable').is(':checked')    
        
        //omission
        //var notax =100;
        
    var tax = ($('#taxbox').attr('checked')) ? (subtotal taxRate) : 0;
        $(
    '#tax').val(isNaN(tax) ? errorMsg tax.toCurrency());//Display value or 'error' if calculation failed
        
        
    var total_cpy =price2 subtotal tax.toCurrency().fromCurrency();//here we add labor total to the rest to get grand total
        
    $('#total_cpy').html(isNaN(total_cpy) ? errorMsg total_cpy.toCurrency());//Display value or 'error' if calculation failed
        
        
    var total =price2 subtotal tax.toCurrency().fromCurrency();//"double-shuffle" to ensure the calculation is consistent with the displayed data.
        
    $('#total').html(isNaN(total) ? errorMsg total.toCurrency());//Display value or 'error' if calculation failed
        
        //update_balance
        
    var due total - $('#paid').val().fromCurrency();
        $(
    '.due').html(isNaN(due) ? errorMsg due.toCurrency());//Display value or 'error' if calculation failed
        
        //update_balance2
        
    var due_box total - $('#paid').val().fromCurrency();
        $(
    '.due_box').html(isNaN(due_box) ? errorMsg due_box.toCurrency());//Display value or 'error' if calculation failed
        
        //nontaxable item total
        //update_balance2
        
    var notaxableItems_ttl price2 notax;
        $(
    '.nonTaxesTtl').html(isNaN(notaxableItems_ttl) ? errorMsg notaxableItems_ttl.toCurrency());//Display value or 'error' if calculation failed
        
    }
        
        
    $item_rows_container.on('blur keyup''.costbox, .qtybox'update);
        
        
        
        $(
    document).ready(function() {
        $(
    'input').click(function(){
            $(
    this).select();
        });
        
        $(
    '#paid').blur(update);//changed from $("#paid").blur(update_balance)
        
    $('#paid').keyup(update);//added
        
    $('#tax').blur(update);//Should the be user-enterable? It's a calculated value.
        
    $('#taxbox').click(update);
        $(
    '.taxable').click(update);
        $(
    '#taxRate').html(taxRate 100);//Added. This ensures the rate show to the user is the same as the rate used in the calcs
        //bind(); 
    Again, the interest is to exclude specific<tr> containing a checkbox for omission of sales taxes (the form may have more then one nontaxable <tr>).
    At the grand total, I would like calculation to excluding any tax addition to those <t>.

    Any thoughts!
    any thoughts!
    Last edited by mbarandao; 11-02-2013 at 07:32 AM.

  • #7
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,302
    Thanks
    28
    Thanked 276 Times in 270 Posts
    Quote Originally Posted by mbarandao View Post
    Again, the interest is to exclude specific<tr> containing a checkbox for omission of sales taxes (the form may have more then one nontaxable <tr>).
    At the grand total, I would like calculation to excluding any tax addition to those <t>.
    Assuming that each tax row has a single cell of the class tax-line, you could add the following JavaScript as part of the toggle3 function:

    Edit: This code was broken; I fixed it. I forgot not to add to the taxCellIndex variable; there's no need since the length of the taxCells collection decreases by one each time a cell is removed. I also named the row with a variable to make the code more readable.

    Code:
    <!doctype html>
    <html lang="en">
    	<head>
    		<meta charset="utf-8">
    		<title>Demo</title>
    	</head>
    	<body>
    		<table>
    			<tbody>
    				<tr>
    					<th>Non‐Tax Cell Header</th>
    					<td>Non‐Tax Cell Data</td>
    				</tr>
    				<tr>
    					<th class="tax-line">Tax Cell Header</th>
    					<td>Tax Cell Data</td>
    				</tr>
    				<tr>
    					<th class="tax-line">Tax Cell Header</th>
    					<td>Tax Cell Data</td>
    				</tr>
    				<tr>
    					<th class="tax-line">Tax Cell Header</th>
    					<td>Tax Cell Data</td>
    				</tr>
    				<tr>
    					<th>Non‐Tax Cell Header</th>
    					<td>Non‐Tax Cell Data</td>
    				</tr>
    			</tbody>
    		</table>
    		<script>
    			(function () {
    				"use strict";
    				var taxRow = null;
    				var taxCells = document.getElementsByClassName("tax-line");
    				var taxCellIndex = 0;
    				var taxCell = null;
    				while (taxCellIndex < taxCells.length) {
    					taxCell = taxCells.item(taxCellIndex);
    					taxRow = taxCell.parentNode;
    					taxRow.parentNode.removeChild(taxRow);
    				}
    			})();
    		</script>
    	</body>
    </html>
    If you need IE8 compatibility, I would use querySelectorAll instead; getElementsByClassName wasn't supported until IE9.

    Probably also worth noting that your layout is broken in Firefox. The register toggle is missing in Firefox 26 Beta among other things. It gets even worse when Firefox's HiDPI mode is enabled.
    Last edited by Arbitrator; 11-03-2013 at 03:16 AM. Reason: I fixed the code. See the post.
    For every complex problem, there is an answer that is clear, simple, and wrong.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •