<?php

	session_start();

	require_once 'db_connection.php';

	$dis = 0; 

	$tempid = $_POST['tempid'];

	$info = mysqli_fetch_array(mysqli_query($conn, "SELECT proid,pro_qty FROM mobiletemporarycart WHERE tempid ='$tempid' AND orderid='0'"));
    	    $proid = $info['proid'];
			$pro_qty = $info['pro_qty'];
			$stock = mysqli_fetch_array(mysqli_query($conn, "SELECT qty FROM tbl_stock WHERE prid='$proid'"));
    	    $stockqty = floatval($stock['qty']);
	//$pro_dis = $_POST['pro_dis'];

	if($stockqty >= $_POST['qty']){
		
		$qty = $_POST['qty'];
	}else{
		$qty = $pro_qty;
	}		

	$discount = $_POST['dis'];

	$price1 = $_POST['price'];

	//$user_id = $_SESSION['user_id'];

	//$price = $_POST['price'];

	$saletype = $_POST['saletype'];

	//$dis = $pro_dis * $qty;

	$sql12 = "UPDATE mobiletemporarycart SET pro_qty = $qty,sale_type=$saletype,pro_price=$price1  WHERE tempid='$tempid'";

				if ($conn->query($sql12) === TRUE) {

					echo "1";

				}else{

					echo 0;

				}

	$result = mysqli_query($conn, "SELECT * FROM mobiletemporarycart WHERE tempid='$tempid'");

	$rw = mysqli_fetch_array($result);



	//$pro_price = $rw['pro_price'];



	$proid = $rw['proid'];



	$products = mysqli_query($conn, "SELECT * FROM mobileproducts WHERE prid ='$proid'");

	$prinfo = mysqli_fetch_array($products);



	$productprice = $prinfo['sellprice'];

	$costprice = $prinfo['price'];

	if($costprice > $price1){

		//$price1 = $productprice;		

	}

	//$qprice = $amount / $productprice;

	$price = $qty * $productprice;

	$cartitems = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM mobiletemporarycart WHERE tempid ='$tempid'"));



	if($cartitems['proid']==148){

			$sql3 = "UPDATE mobiletemporarycart SET pro_qty = $qty  WHERE tempid='$tempid'";

			if ($conn->query($sql3) === TRUE) {

				echo "1";

			}else{

				echo 0;

			}

	}else{

		if(mysqli_num_rows(mysqli_query($conn, "SELECT * FROM tbl_stock WHERE prid='$proid' AND qty >= '$qty'"))==1){



			if ($productprice != $cartitems['pro_price']) {

				if($costprice > $price){

					$price = $productprice;			

				}

				$sql3 = "UPDATE mobiletemporarycart SET pro_qty = $qty, pro_price = $price  WHERE tempid='$tempid'";

				if ($conn->query($sql3) === TRUE) {

					echo "1";

				}else{

					echo 0;

				}

			}else{

				$sql3 = "UPDATE mobiletemporarycart SET pro_qty = $qty  WHERE tempid='$tempid'";

				if ($conn->query($sql3) === TRUE) {

					echo "1";

				}else{

					echo 0;

				}

			}

	

	

	

		}

	}

	

	$lastchar = substr($discount, -1); 





	if($lastchar=="%"){

		

		$disrate = floatval(substr($discount, 0, -1));

		$dis =  ($productprice/100)*$disrate;

		$pprice = $productprice-$dis;

		$distype = 1;

		$discount = $disrate;

		$pprice = $productprice-$discount;





		$dis =  ($_POST['price']/100)*$disrate;



		$pprice2 = $_POST['price']-$dis;





		

	}else{

		$pprice = $productprice-$discount;

		$pprice2 = $_POST['price']-$discount;

		$distype = 0;

	}

	



	$sql3 = "UPDATE mobiletemporarycart SET pro_discount='$discount',pro_dis_price='$pprice2',discount_type='$distype',pro_price='$price1' WHERE tempid='$tempid'";

	if ($conn->query($sql3) === TRUE) {

		echo "1";

	}else{

		echo 0;

	}













	?>

