Jump to content

Archived

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

Hilly_2004

VAT on Certain Items

Recommended Posts

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:

[code]
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;
}
[/code]

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?

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
I would recommend having
[code]
if ($VatInfo > 0) {
      //echo $amountTotal;
$vat = (0);
     // $price_wovat=round($amountTotal+$price_wovat1, 2);
      //$vat = $amountTotal-$price_wovat1;
} else {
$vat = ($amountTotal/100) * 17.5;
}
[/code]

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

so everytime it runs and calculates whether it'a VAT or not, it adds that value to $total

Share this post


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

Share this post


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

Share this post


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

[code]
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;
}

}
[/code]

Share this post


Link to post
Share on other sites
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
[code]
$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;
[/code]

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

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

-J

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
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?
[code]
$price_wovat1=($product['UK_Price']/100)*17.5;
$price_wovat=round($product['UK_Price']-$price_wovat1, 2);
[/code]

Share this post


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

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

Share this post


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

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.