<?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";
include_once "classes/cls_user.php";
include_once "classes/cls_products.php";
include_once "classes/cls_cart.php";
include_once "classes/cls_user.php";
    $productid                  =       $_REQUEST['productid'];
    $product_option_id          =       $_REQUEST["product_option_id"];
    $wishId                     =       $_REQUEST['wishId'];
    $txtSubscriptionPeriod      =       $_REQUEST['txtSubscriptionPeriod']; 
    $txtSubscriptionFlag        =       'N';
    
    if(isset($_REQUEST['txtSubscriptionFlag']) && $_REQUEST['txtSubscriptionFlag']!='')
    {
        $txtSubscriptionFlag            =   $_REQUEST['txtSubscriptionFlag'];
    }

/*-----------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($_REQUEST['txtQty']) && $_REQUEST['txtQty']!='')
{

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

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

else
{
        $qty=1;
}



if ($productid != "") {

    $objProductVo   =    Products::getProductBasicInfo($productid,$product_option_id);

    
    if($objProductVo->status==SUCCESS)
            {
                $productObjVal  =   $objProductVo->data;
                
                if($productObjVal->gift_certificate_flag=='')
                {
                    $productObjVal->gift_certificate_flag   =   'N';
                }
                if($productObjVal->vdigital_product=='')
                {
                    $productObjVal->vdigital_product   =   'N';
                }

                    $product_seller_id      =   $productObjVal->seller_id;
                     if($_SESSION["sess_userid"]!='')  {
                            //Check the product is logged users product
                            $sellerAccObj   =    User::is_seller_account_activated($_SESSION["sess_userid"]);
                                  //echopre($sellerAccObj);
                                if($sellerAccObj->status==SUCCESS)
                                    {
                                            $sellerAccObjVal            =   $sellerAccObj->data;
                                            $seller_activated_status    =   $sellerAccObjVal->is_seller;
                                            $activated_seller_id        =   $sellerAccObjVal->seller_id;


                                            if($activated_seller_id==$product_seller_id)
                                                {
                                                        header("Location:viewcart.php?msg=no_permission");
                                                        exit;
                                                }
                                    }
                        }
                
            }
       // echopre($productObjVal) ;   
            
             if($_SESSION["sess_userid"]!='')
             {
                 $visitorId     =  '';
             }else{
                 $visitorId     = generalMethods::sessionid();
             }

    $cartObj    =   Cart::getCartCount($_SESSION["sess_userid"],$visitorId);
    
    if($cartObj->status==SUCCESS)
            {
                    $cartObjVal =   $cartObj->data;

                    if($cartObjVal->cart_count>0)
                            { 
                               
                                if($productObjVal->gift_certificate_flag=="Y")
                                {
                                    // Check  Non Gift Products exist in cart 
                                   
                                    $nonGiftObj =   Cart::getNonGiftItemsCartCount($_SESSION["sess_userid"],$visitorId);
                                
                                    //echopre($nonGiftObj);
                                    if($nonGiftObj->status==SUCCESS)
                                    {
                                        $nonGiftObjVal  =   $nonGiftObj->data;
                                        //echo $nonGiftObj->itemscount;
                                        // Redirect user to cart page if non gift items exist in cart 
                                        if($nonGiftObjVal->itemscount>0)   {
                                           header("Location:viewcart.php?msg=cart_error_digital");
                                           exit;
                                        }
                                    }
                                }else{
                                    
                                    // check gift products exists in cart 
                                    
                                 $GiftObj    =   Cart::getGiftCertificateItemsCartCount($_SESSION["sess_userid"],$visitorId);
                                
                                    if($GiftObj->status==SUCCESS)
                                    {
                                       
                                        $GiftObjVal     =   $GiftObj->data;
                                        // Redirect user to cart page if non gift items exist in cart 
                                        if($GiftObjVal->gift_count>0)   {
                                             header("Location:viewcart.php?msg=cart_error_nondigital");
                                           
                                           exit;
                                        }
                                    }
                                    
                                }
                            }


            }
            
           
    $message = "" ;
	if(!isset($_SESSION["sess_userid"]) || ($_SESSION["sess_userid"]=="") ){
           
		 $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,digital_product_flag,gift_product_flag,seller_id,subscription_flag,subscription_duration) VALUES ('".addslashes(session_id())."','".addslashes($productid)."','".addslashes($product_option_id)."','".$qty."','0','".addslashes($time)."','".$productObjVal->vdigital_product."','".$productObjVal->gift_certificate_flag."','".$productObjVal->seller_id."','".$txtSubscriptionFlag."','".$txtSubscriptionPeriod."') ";
		$res = mysql_query($sql1) or die(mysql_error());
		//echo $sql1;

		$cart_insert_id = mysql_insert_id();
		$_SESSION['SESS_card_id'] = $cart_insert_id;
                header("Location:viewcart.php");
                exit;
	}else{

            
        // Remove Item from Favourites list once its moved to cart
            if($wishId!='' && $_SESSION["sess_userid"]!='')
            {
                    $resultData   = User::delete_user_favourite_list($wishId,$_SESSION["sess_userid"]);
            }

            // End Removal Wish List Data

		$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,digital_product_flag,gift_product_flag,seller_id,subscription_flag,subscription_duration) VALUES ('".addslashes($_SESSION["sess_userid"])."','".addslashes($productid)."','".addslashes($product_option_id)."','".$qty."','0','".addslashes($time)."','".$productObjVal->vdigital_product."','".$productObjVal->gift_certificate_flag."','".$productObjVal->seller_id."','".$txtSubscriptionFlag."','".$txtSubscriptionPeriod."') ";
		$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;
	}

}


?>