Jump to content

Archived

This topic is now archived and is closed to further replies.

High_-_Tek

Tax adding when not supposed to

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 :)

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Ignore all commented-out lines :D

And I was advised by another member to add the while loop in there

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Oops! Forgot the '$'. Try

$subTotal = [!--coloro:#FF6666--][span style=\"color:#FF6666\"][!--/coloro--]$[!--colorc--][/span][!--/colorc--]vatTotal = 0;

Share this post


Link to post
Share on other sites
Ok, That worked great :)

But now I get this:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #11' at line 1

Thanks again!

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Ok when I run the script I get this:

36 10.00 2.71
42 6.46 1.13

Warning: printf(): Too few arguments in /home/www/petalfoods.com/Test/order5.php on line 180

Share this post


Link to post
Share on other sites
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 );

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

×

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.