Jump to content

Recommended Posts

Hey All,

I have a page for my company that needs fixing, I am so tired right now I cant think straight. But anyway, if a product(s) selected are under a certain catagory, then they need to add TAX

But the way it is working now, it still adds tax for non-tax items.

Code:

[code]
foreach ($_SESSION['purchase_prods'] AS $arr){
    $price=$arr[1] * $arr[2];
    $sql=mysql_query("SELECT cat.catid AS catid FROM Products AS p, categories AS cat WHERE p.Product_ID='$arr[0]' AND p.Product_Cat_ID=cat.catid");
    $result=mysql_fetch_array(mysql_query("SELECT cat.catid AS catid FROM Products AS p, categories AS cat WHERE p.Product_ID='$arr[0]' AND p.Product_Cat_ID=cat.catid")) OR DIE (mysql_error());
    

    define('TAX_AMNT',.175);
    while(list($needsTAX,$price)=mysql_fetch_row($sql)) {
        $sub_total+=$price;
        if ($result['catid'] == 2) {
            $sub_total+=$price * TAX_AMNT;
            $vat=(TAX_AMNT * $sub_total);
            }
        //var_dump($needsTAX);
    }

    $vat=number_format($vat,2,'.','');
  }
[/code]

Any and all help is very much appriciated :)
Link to comment
https://forums.phpfreaks.com/topic/5202-tax-adding-when-not-supposed-to/
Share on other sites

Your code is pretty confusing.

Why the same query twice?

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]$sql=mysql_query("SELECT cat.catid AS catid FROM Products AS p, categories AS cat WHERE p.Product_ID='$arr[0]' AND p.Product_Cat_ID=cat.catid");
$result=mysql_fetch_array(mysql_query("SELECT cat.catid AS catid FROM Products AS p, categories AS cat WHERE p.Product_ID='$arr[0]' AND p.Product_Cat_ID=cat.catid")) OR DIE (mysql_error());[/quote]


Then there's
[code]while(list($needsTAX,$price)=mysql_fetch_row($sql)) {[/code]

You only select "catid" which will go into $needsTAX and $price will be NULL.

Having got a value in $needsTAX it isn't used.
A couple of assumptions

The product code in $arr[0] is unique
The criteria for adding tax is the the catid = 2

[code]define('TAX_AMNT', 0.175);

$subTotal = vatTotal = 0;
echo '<pre>';

foreach ($_SESSION['purchase_prods'] AS $arr) {
    $price=$arr[1] * $arr[2];

    $sql=mysql_query("SELECT cat.catid AS catid
         FROM Products AS p INNER JOIN categories AS cat
              ON p.Product_Cat_ID = cat.catid
         WHERE p.Product_ID='$arr[0]' ");

    $res = mysql_query($sql) or die (mysql_error());

    $needsTAX = mysql_result ($res, 0, 'catid') == 2;

    if ($needsTAX) {
        $price *= 1 + TAX_AMNT;
        $vat = TAX_AMNT * $price;
        }

    printf ('%-12s  %8.2f  %8.2f<br>', $arr[0], $price, $vat );

    $subTotal += $price;
    $vatTotal += $vat;
}

printf ('%-12s  %8.2f  %8.2f', 'Total, $subTotal, $vatTotal );
echo '</pre>';[/code]
Hey Barand thanks for that :)

But when i run I get this:


Parse error: parse error, unexpected '=' in /home/www/petalfoods.com/Test/order5.php on line 155

Which is probably attribuatted to this line:
[code]
$subTotal = vatTotal = 0;
(156) is the line
[/code]

Thanks again
What is the query giving the error?

Spotted it.

Should be

[code]$sql="SELECT cat.catid AS catid
         FROM Products AS p INNER JOIN categories AS cat
              ON p.Product_Cat_ID = cat.catid
         WHERE p.Product_ID='$arr[0]' ";[/code]
Another typo! Missing quote.

printf ('%-12s %8.2f %8.2f', 'Total[!--sizeo:4--][span style=\"font-size:14pt;line-height:100%\"][!--/sizeo--][!--coloro:#FF6666--][span style=\"color:#FF6666\"][!--/coloro--]'[!--colorc--][/span][!--/colorc--] [!--sizec--][/span][!--/sizec--], $subTotal, $vatTotal );
Thank You so much barand :)

We are soo close :). The tax added into the subtotal is spot on, but the tax LISTED off off by a few dollars.

Is there a way to splice the tax off when its added to the sub_total? Because only the LISTED tax is off and not the tax added to the total

BTW~ Here is what is outputted

5 2.00 3.32
10 4.00 3.32
12 2.00 3.32
42 1.18 0.21
43 11.75 2.06
Total 20.93 12.24

The tax is supposed to be 1.93

Thank You Again :)
Needs the "else" bit below and alter calc lines so vat is calculated first, but I don't understand how total is OK.

[code]if ($needsTAX) {

        $vat = TAX_AMNT * $price;        
                $price += $vat;

        }
else
        $vat = 0;[/code]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.