Jump to content


Photo

VAT on Certain Items


  • Please log in to reply
15 replies to this topic

#1 Hilly_2004

Hilly_2004
  • Members
  • PipPipPip
  • Advanced Member
  • 63 posts

Posted 03 April 2006 - 12:58 AM

Hiya guys, Ive built my own shopping cart and there are certain rules here in UK regarding clothing for children (your not allowed to charge VAT - 17.5%). Anyway this is the code I have:

for($i=0; $i<count($myCart); $i++){
$query = "SELECT * FROM Items WHERE Item_ID = '".$myCart[$i]['Item_ID']."'";
$queryresult = mysql_query($query, $conn);
$MainRow = mysql_fetch_array($queryresult);
$VatInfo = $MainRow["VAT"];
}

if ($VatInfo > 0) {
      //echo $amountTotal;
$vat = (0);
     // $price_wovat=round($amountTotal+$price_wovat1, 2);
      //$vat = $amountTotal-$price_wovat1;
} else {
$vat = ($amountTotal/100) * 17.5;
}

It works when I have one item in the shopping cart that is exempt from VAT charges, however when I mix an item that is exempt from VAT and one that isn't, the shopping cart charges VAT on both the items. Can anyone see why?

#2 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 03 April 2006 - 01:21 AM

You got to calculate VAT when necessary at each individual cart item and not at the end using the total.

Right now, your $VatInfo variable is getting overridden with every query. So, whatever the last cart item VAT status is, seems to determine whether to apply VAT on the whole order or not.



#3 Hilly_2004

Hilly_2004
  • Members
  • PipPipPip
  • Advanced Member
  • 63 posts

Posted 03 April 2006 - 01:29 AM

Cheers mate, at least now I know why its doing it. Do you have any suggestions as to how I can adapt my query so that it applies it to every item rather the last item entered into the basket?

#4 earl_dc10

earl_dc10
  • Members
  • PipPipPip
  • Advanced Member
  • 71 posts

Posted 03 April 2006 - 01:33 AM

I would recommend having
if ($VatInfo > 0) {
      //echo $amountTotal;
$vat = (0);
     // $price_wovat=round($amountTotal+$price_wovat1, 2);
      //$vat = $amountTotal-$price_wovat1;
} else {
$vat = ($amountTotal/100) * 17.5;
}

inside your for() loop but add something like
$total += <price of item>;

so everytime it runs and calculates whether it'a VAT or not, it adds that value to $total
got a problem? Google helps many of those in need

#5 Hilly_2004

Hilly_2004
  • Members
  • PipPipPip
  • Advanced Member
  • 63 posts

Posted 03 April 2006 - 01:41 AM

Ive put it in the for loop however it's still adding VAT to both items. I think its the query thats doing it, just need to figure out how it should go...

#6 earl_dc10

earl_dc10
  • Members
  • PipPipPip
  • Advanced Member
  • 71 posts

Posted 03 April 2006 - 01:50 AM

is $amountTotal calculated before this query? try changing $amountTotal to the amount of the individual item for inside the loop so that the VAT calculation only applies to the individual price, not the whole.
got a problem? Google helps many of those in need

#7 Hilly_2004

Hilly_2004
  • Members
  • PipPipPip
  • Advanced Member
  • 63 posts

Posted 03 April 2006 - 01:57 AM

Heres what I changed it to mate, and it seemed like it was working, however when I added another couple of items and checked out what the VAT should be I found that it ended up charging VAT on the last item instead of adding all the items together.

for($i=0; $i<count($myCart); $i++){
$query = "SELECT * FROM Items WHERE Item_ID = '".$myCart[$i]['Item_ID']."'";
$queryresult = mysql_query($query, $conn);
$MainRow = mysql_fetch_array($queryresult);
$Price = $MainRow["UK_Price"];
$VatInfo = $MainRow["VAT"];

if ($VatInfo > 0) {
      //echo $amountTotal;
$vat = (0);
     // $price_wovat=round($amountTotal+$price_wovat1, 2);
      //$vat = $amountTotal-$price_wovat1;
} else {
$vat = ($Price/100) * 17.5;
}

}


#8 earl_dc10

earl_dc10
  • Members
  • PipPipPip
  • Advanced Member
  • 71 posts

Posted 03 April 2006 - 02:55 AM

can I see how you are adding these values together? it may be in there I just can't see where $amoutTotal = anything.
if this is what you have, sorry, I'll suggest it anyway since Im crashin

try this for adding the total
$amountTotal += ($Price-$vat); // since if VAT doesn't apply $vat equals 0, shouldn't be a problem
   // after loop is done and you want to display the total
$total = round($amountTotal, 2);
echo $total;

and I THINK that this line becomes irrelevent since we calculate VAT individually
$vat = $amountTotal-$price_wovat1;

Im sorry if this is entirely irrelevent to your question, Im a bit tired, hope it helps though!

-J
got a problem? Google helps many of those in need

#9 Hilly_2004

Hilly_2004
  • Members
  • PipPipPip
  • Advanced Member
  • 63 posts

Posted 03 April 2006 - 02:58 AM

Hiya mate, the $amountTotal variable looks like this...

$amountTotal=$HTTP_SESSION_VARS['total'];

and this is in the add item to cart page....


if($add){
$itemID = $HTTP_POST_VARS['product'];
$isFound = false;
for($i =0; $i< count($HTTP_SESSION_VARS['cart']); $i++){
if($HTTP_SESSION_VARS['cart'][$i]['Item_ID'] == $itemID){
$isFound = $i;
break;
}
}

if($isFound !== false){
$HTTP_SESSION_VARS['cart'][$isFound]['quantity']++;
}else{
$newItem = array();
$newItem['Item_ID'] = $itemID;
$newItem['quantity'] =1;
array_push($HTTP_SESSION_VARS['cart'], $newItem);
}


$HTTP_SESSION_VARS['items']++;
// array_push($HTTP_SESSION_VARS['cart'], $product);
$HTTP_SESSION_VARS['total']+=$price;
}
$conn = db_connect();
$products_sql = "select * from Items where Item_ID= \"$ItemID\"";
$products_result = mysql_query($products_sql, $conn);
$product = mysql_fetch_array($products_result);
$price_wovat1=($product['UK_Price']/100)*17.5;
$price_wovat=round($product['UK_Price']-$price_wovat1, 2);


#10 Hilly_2004

Hilly_2004
  • Members
  • PipPipPip
  • Advanced Member
  • 63 posts

Posted 03 April 2006 - 02:13 PM

Any help guys?

#11 Hilly_2004

Hilly_2004
  • Members
  • PipPipPip
  • Advanced Member
  • 63 posts

Posted 03 April 2006 - 09:24 PM

Pretty please with a cherry on top? :)

#12 Hilly_2004

Hilly_2004
  • Members
  • PipPipPip
  • Advanced Member
  • 63 posts

Posted 04 April 2006 - 12:50 AM

I tried calculating the VAT in the lib file, however thats still doing the same as before.

#13 Hilly_2004

Hilly_2004
  • Members
  • PipPipPip
  • Advanced Member
  • 63 posts

Posted 04 April 2006 - 02:26 AM

Where for art thou earl?! lol Can't believe how hard this is...

My end total is calculated using this:

echo money_format($fmt, $amountTotal+$delivery+$vat);

#14 earl_dc10

earl_dc10
  • Members
  • PipPipPip
  • Advanced Member
  • 71 posts

Posted 04 April 2006 - 02:59 AM

sorry, Im still thinkin about this one, ummm.....

just for clarification, if we are calculating the price and vat of each item in a loop, what does this do?
$price_wovat1=($product['UK_Price']/100)*17.5;
$price_wovat=round($product['UK_Price']-$price_wovat1, 2);


got a problem? Google helps many of those in need

#15 Hilly_2004

Hilly_2004
  • Members
  • PipPipPip
  • Advanced Member
  • 63 posts

Posted 04 April 2006 - 03:07 AM

Cheers for helping mate, I really appreciate it...

I think it was just part of my old way of coding...

One of my variables calls it further down...

$subtotal = 0;
$subtotal+=($price_wovat*$myCart[$i]['quantity']);


#16 Hilly_2004

Hilly_2004
  • Members
  • PipPipPip
  • Advanced Member
  • 63 posts

Posted 04 April 2006 - 07:03 PM

Basically the problem is that the $vat variable is set by the last item entered into the cart, instead of checking each individual item and then adding all the prices together.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users