Jump to content


Photo

Tax adding when not supposed to


  • Please log in to reply
11 replies to this topic

#1 High_-_Tek

High_-_Tek
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 18 March 2006 - 12:08 AM

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:

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,'.',''); 
  }

Any and all help is very much appriciated :)

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 18 March 2006 - 12:49 AM

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
while(list($needsTAX,$price)=mysql_fetch_row($sql)) {

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

Having got a value in $needsTAX it isn't used.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 High_-_Tek

High_-_Tek
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 18 March 2006 - 12:58 AM

Ignore all commented-out lines :D

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



#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 18 March 2006 - 11:37 AM

A couple of assumptions

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

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>';

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 High_-_Tek

High_-_Tek
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 18 March 2006 - 10:02 PM

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:
$subTotal = vatTotal = 0;
(156) is the line

Thanks again

#6 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 18 March 2006 - 11:18 PM

Oops! Forgot the '$'. Try

$subTotal = [!--coloro:#FF6666--][span style=\"color:#FF6666\"][!--/coloro--]$[!--colorc--][/span][!--/colorc--]vatTotal = 0;
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#7 High_-_Tek

High_-_Tek
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 18 March 2006 - 11:24 PM

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!

#8 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 19 March 2006 - 12:35 AM

What is the query giving the error?

Spotted it.

Should be

$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]' ";

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#9 High_-_Tek

High_-_Tek
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 19 March 2006 - 12:59 AM

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

#10 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 19 March 2006 - 08:00 AM

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

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#11 High_-_Tek

High_-_Tek
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 19 March 2006 - 03:44 PM

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

#12 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 20 March 2006 - 08:58 AM

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

if ($needsTAX) {

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

        }
else
        $vat = 0;

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users