<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
// +----------------------------------------------------------------------+
// | PHP version 4/5                                                      |
// +----------------------------------------------------------------------+
// | This source file is a part of iScripts Multicart                     |
// +----------------------------------------------------------------------+
// | Authors: Programmer<programmer@armia.com>        		              |
// +----------------------------------------------------------------------+
// | Copyrights Armia Systems, Inc and iScripts.com � 2005                |
// | All rights reserved                                                  |
// +----------------------------------------------------------------------+
// | This script may not be distributed, sold, given away for free to     |
// | third party, or used as a part of any internet services such as      |
// | webdesign etc.                                                       |
// +----------------------------------------------------------------------+
?>
<?php
include "includes/config.php";
include "includes/session.php";
include "includes/functions.php";

    
    $productid          =       $_REQUEST['productid'];
    $product_option_id  =        $_REQUEST["product_option_id"];


/*-----------Multicart 2.0 Upgradation------------*/

$check_shipping_enable = checkShippingEnable();

/*-----------Multicart 2.0 Upgradation------------*/

$currentProductStock	= getProductStock($product_option_id);

if($currentProductStock==0)
    {

    header("Location:productdetails.php?msg=stock&productid=".$productid);
    exit;
}



if(isset($_POST['txtQty']) && $_POST['txtQty']!='')
{

        if($currentProductStock<$_POST['txtQty'])
        {
                $qty 	= $currentProductStock;
        }
        else
        {

                $qty	= $_POST['txtQty'];
        }
}

else
{
        $qty=1;
}
		


if ($productid != "") {
    $message = "" ;
	if(!isset($_SESSION["sess_userid"]) || ($_SESSION["sess_userid"]=="") ){
		//header("Location:login.php?action=cart&productid=$productid");
		//exit;
		
		
/*----------Checking digital product starts------------*/		

//if($check_shipping_enable == "Y")
//{ // shipping enable checking starts

		
		$select_cart = "SELECT cart_id FROM ".$tableprefix."cart WHERE visitor_cart_id = ".GetSQLValueString(session_id(),"text");
												
		$result_cart = mysql_query($select_cart);
		
		if(mysql_num_rows($result_cart) > 0)
		{

		
					$select_digital_check = "SELECT ct.cart_id FROM ".$tableprefix."cart ct,".$tableprefix."products  prd
											 WHERE ct.product_id = prd.product_id 
											  AND prd.vdigital_product = 'Y'
											  AND ct.visitor_cart_id = '".addslashes(session_id())."'";
											  
					//echo $select_digital_check;						  
																  
					$result_digital_check = mysql_query($select_digital_check) or die(mysql_error());		
					
					
					/*---checking gift coupon--*/	
							
					$select_gift_coupon = "SELECT prd.product_id FROM ".$tableprefix."products prd,".$tableprefix."categories ctry,
												".$tableprefix."cart ct
										   WHERE prd.product_category = ctry.category_id
										    AND ct.product_id = prd.product_id
											AND ctry.gift_certificate_flag = 'Y'
											AND ct.visitor_cart_id = '".addslashes(session_id())."'";
											
										
					//echo $select_gift_coupon; 					
										
					$result_gift_coupon   = mysql_query($select_gift_coupon) or die(mysql_error());	
					
					
					//echo "digital=".mysql_num_rows($result_digital_check)."<br>";
					//echo "gift=".mysql_num_rows($result_gift_coupon)."<br>";
					
					
					
					if(mysql_num_rows($result_digital_check) > 0 || mysql_num_rows($result_gift_coupon) > 0)
					{
					
											
					
						$select_digital = "SELECT prd.product_id FROM ".$tableprefix."products prd
										   WHERE prd.vdigital_product = 'Y'
										    AND prd.product_id = '".addslashes($productid)."'";
											
						//echo $select_digital;					
											
											
						$result_digital   = mysql_query($select_digital) or die(mysql_error());
																								
						/*---checking gift coupon--*/	
							
						$select_gift_coupon = "SELECT prd.product_id FROM ".$tableprefix."products prd,".$tableprefix."categories ctry
											   WHERE prd.product_category = ctry.category_id
												AND ctry.gift_certificate_flag = 'Y'
												AND prd.product_id = '".addslashes($productid)."'";
												
						//echo $select_gift_coupon;						
						
						$result_gift_coupon   = mysql_query($select_gift_coupon) or die(mysql_error());	
						
						if(mysql_num_rows($result_digital) == 0 && mysql_num_rows($result_gift_coupon) == 0)
						{
												
							header("Location:viewcart.php?msg=cart_error_nondigital");
							//echo "Since the cart contains digital product adding a non digital product is not possible !!";
							
							exit;
						
						}
						
					
						
					
					}				  
					else
					{
/*----------Checking digital product ends------------*/		
			
			
/*----------Checking non digital product starts------------*/		
					
							$select_digital = "SELECT prd.product_id FROM ".$tableprefix."products prd
										   	   WHERE prd.vdigital_product = 'Y'
											   	AND prd.product_id = '".addslashes($productid)."'";
												
							//echo $select_digital; 					
												
							$result_digital   = mysql_query($select_digital) or die(mysql_error());	
							
							if(mysql_num_rows($result_digital) > 0)
							{
								
								//$cart_error = "Since the cart contains non digital product adding a digital product is not possible !!";
								header("Location:viewcart.php?msg=cart_error_digital");
								exit;
							
							}
							
						/*---checking gift coupon--*/	
							
							$select_gift_coupon = "SELECT prd.product_id FROM ".$tableprefix."products prd,".$tableprefix."categories ctry
										   	       WHERE prd.product_category = ctry.category_id
												    AND ctry.gift_certificate_flag = 'Y'
											   	    AND prd.product_id = '".addslashes($productid)."'";
												
							//echo $select_digital; 					
												
							$result_gift_coupon   = mysql_query($select_gift_coupon) or die(mysql_error());	
							
							if(mysql_num_rows($result_gift_coupon) > 0)
							{
								
								//$cart_error = "Since the cart contains non digital product adding a digital product is not possible !!";
								header("Location:viewcart.php?msg=cart_error_digital");
								exit;
							
							}
							
							
							
					}		  
					
/*----------Checking non digital product ends------------*/	
			
		}		
	
//} // shipping enable checking ends
		
		$sql = "SELECT cart_id,quantity FROM ".$tableprefix."cart  WHERE product_option_id = '".addslashes($product_option_id)."' and visitor_cart_id = '".addslashes(session_id())."' ";
		$res = mysql_query($sql);
		if(mysql_num_rows($res) > 0 )
		{
			$rs_cartobj=mysql_fetch_object($res);
			
			$txtQty		= $rs_cartobj->quantity;
			$txtNewQty = $txtQty + $qty;
                        
                        
			if($txtNewQty>$currentProductStock)
			{
                                $sql1_up= "UPDATE  ".$tableprefix."cart
							SET quantity = '".$currentProductStock."' WHERE cart_id = '".$rs_cartobj->cart_id."' AND  visitor_cart_id = '".addslashes(session_id())."' ";
				$res = mysql_query($sql1_up) or die(mysql_error());
				header("Location:viewcart.php?msg=cart_stock_less");
				exit;
			}
			
			else
			{
				  $sql1_up= "UPDATE  ".$tableprefix."cart
							SET quantity = '".$txtNewQty."' WHERE cart_id = '".$rs_cartobj->cart_id."' AND  visitor_cart_id = '".addslashes(session_id())."' ";
				  $res = mysql_query($sql1_up) or die(mysql_error());
	
				header("Location:viewcart.php");
					exit;
		  }			
		}
		$time =  time();
		
		
		$sql1 = "INSERT INTO ".$tableprefix."cart (visitor_cart_id,product_id,product_option_id,quantity,status,datetime_added) VALUES ('".addslashes(session_id())."','".addslashes($productid)."','".addslashes($product_option_id)."','".$qty."','0','".addslashes($time)."') ";
		$res = mysql_query($sql1);
		//echo $sql1;
		
		$cart_insert_id = mysql_insert_id();
		$_SESSION['SESS_card_id'] = $cart_insert_id;	
		header("Location:viewcart.php");
		
		exit;
	}else{
		

//if($check_shipping_enable == "Y")
//{ // shipping enable checking starts	
				
/*----------Checking digital product starts------------*/		

		$select_cart = "SELECT cart_id FROM ".$tableprefix."cart WHERE user_id = '".addslashes($_SESSION["sess_userid"])."'";
		$result_cart = mysql_query($select_cart);
		
		if(mysql_num_rows($result_cart) > 0)
		{

		
					$select_digital_check = "SELECT ct.cart_id FROM ".$tableprefix."cart ct,".$tableprefix."products  prd
											 WHERE ct.product_id = prd.product_id 
											  AND prd.vdigital_product = 'Y'
											  AND ct.user_id = '".addslashes($_SESSION["sess_userid"])."'";
					 
					$result_digital_check = mysql_query($select_digital_check) or die(mysql_error());		
					
					
					/*---checking gift coupon--*/	
							
					$select_gift_coupon = "SELECT prd.product_id FROM ".$tableprefix."products prd,".$tableprefix."categories ctry,
												".$tableprefix."cart ct
										   WHERE prd.product_category = ctry.category_id
										    AND ct.product_id = prd.product_id
											AND ctry.gift_certificate_flag = 'Y'
											AND ct.user_id = '".addslashes($_SESSION["sess_userid"])."'";
											
										
					//echo $select_gift_coupon; 					
										
					$result_gift_coupon   = mysql_query($select_gift_coupon) or die(mysql_error());	
					
					
																							
					if(mysql_num_rows($result_digital_check) > 0 || mysql_num_rows($result_gift_coupon) > 0)
					{
					
						$select_digital = "SELECT prd.product_id FROM ".$tableprefix."products prd
										   WHERE prd.vdigital_product = 'Y'
										    AND prd.product_id = '".addslashes($productid)."'";
						$result_digital   = mysql_query($select_digital) or die(mysql_error());
						
						
						
					/*--gift coupon check---*/	
						
						$select_gift_coupon = "SELECT prd.product_id FROM ".$tableprefix."products prd,".$tableprefix."categories ctry
											   WHERE prd.product_category = ctry.category_id
												AND ctry.gift_certificate_flag = 'Y'
												AND prd.product_id = '".addslashes($productid)."'";
												
						//echo $select_gift_coupon;						
						
						$result_gift_coupon   = mysql_query($select_gift_coupon) or die(mysql_error());
						
						if(mysql_num_rows($result_digital) == 0 && mysql_num_rows($result_gift_coupon) == 0)
						{
												
							header("Location:viewcart.php?msg=cart_error_nondigital");
							//echo "Since the cart contains digital product adding a non digital product is not possible !!";
							
							exit;
						
						}
					
					
					}				  
					else
					{
/*----------Checking digital product ends------------*/		
			
			
/*----------Checking non digital product starts------------*/		
					
							$select_digital = "SELECT prd.product_id FROM ".$tableprefix."products prd
										   	   WHERE prd.vdigital_product = 'Y'
											    AND prd.product_id = '".addslashes($productid)."'";
							$result_digital   = mysql_query($select_digital) or die(mysql_error());	
							
							if(mysql_num_rows($result_digital) > 0)
							{
								
								//$cart_error = "Since the cart contains non digital product adding a digital product is not possible !!";
								header("Location:viewcart.php?msg=cart_error_digital");
								exit;
							
							}
							
							/*---checking gift coupon--*/	
							
							$select_gift_coupon = "SELECT prd.product_id FROM ".$tableprefix."products prd,".$tableprefix."categories ctry
										   	       WHERE prd.product_category = ctry.category_id
												    AND ctry.gift_certificate_flag = 'Y'
											   	    AND prd.product_id = '".addslashes($productid)."'";
												
							//echo $select_digital; 					
												
							$result_gift_coupon   = mysql_query($select_gift_coupon) or die(mysql_error());	
							
							if(mysql_num_rows($result_gift_coupon) > 0)
							{
								
								//$cart_error = "Since the cart contains non digital product adding a digital product is not possible !!";
								header("Location:viewcart.php?msg=cart_error_digital");
								exit;
							
							}
							
										
							
					}		  
					
/*----------Checking non digital product ends------------*/	
			
		}		
	
//} // shipping enable checking ends		
		
		
		$sql = "SELECT cart_id,quantity FROM ".$tableprefix."cart  WHERE product_option_id = '".addslashes($product_option_id)."' and user_id = '".addslashes($_SESSION["sess_userid"])."' ";
		$res = mysql_query($sql);
		
                if(mysql_num_rows($res) > 0 ){
		
			$rs_cartobj=mysql_fetch_object($res);
			$txtQty		= $rs_cartobj->quantity ;
			$txtNewQty = $txtQty + $qty;
			
			if($txtNewQty>$currentProductStock)
			{
				 $sql1_up= "UPDATE  ".$tableprefix."cart 
							SET quantity = '".$currentProductStock."' WHERE cart_id = ".$rs_cartobj->cart_id." AND  user_id = '".addslashes($_SESSION["sess_userid"])."' ";
				 $res = mysql_query($sql1_up) or die(mysql_error());
                                header("Location:viewcart.php?msg=cart_stock_less");
				exit;
			}
			else
			{
				 $sql1_up= "UPDATE  ".$tableprefix."cart 
							SET quantity = '".$txtNewQty."' WHERE cart_id = ".$rs_cartobj->cart_id." AND  user_id = '".addslashes($_SESSION["sess_userid"])."' ";
				 $res = mysql_query($sql1_up) or die(mysql_error());
				header("Location:viewcart.php");
				exit;
			}
		}
		$time =  time();
		 $sql1 = "INSERT INTO ".$tableprefix."cart (user_id,product_id,product_option_id,quantity,status,datetime_added) VALUES ('".addslashes($_SESSION["sess_userid"])."','".addslashes($productid)."','".addslashes($product_option_id)."','".$qty."','0','".addslashes($time)."') ";
		$res = mysql_query($sql1) or die(mysql_error());
		$cart_insert_id = mysql_insert_id();
		$_SESSION['SESS_card_id'] = $cart_insert_id;		
		
		header("Location:viewcart.php");
		exit;
	}


}
?>