<?php
	session_start();
	require_once 'db_connection.php';
	$dis = 0; 
	$tempid = $_POST['tempid'];
	//$pro_dis = $_POST['pro_dis'];
	$qty = $_POST['qty'];
	$discount = $_POST['dis'];
	$price1 = $_POST['price'];
	$distype1 = $_POST['distype'];
	
	//$user_id = $_SESSION['user_id'];
	//$price = $_POST['price'];

	//$dis = $pro_dis * $qty;
	$sql8 = "UPDATE mobiletemporarycart SET pro_qty = $qty  WHERE tempid='$tempid'";
				if ($conn->query($sql8) === TRUE) {}
	$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 = intval(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='$distype1',pro_price='$price1' WHERE tempid='$tempid'";
	if ($conn->query($sql3) === TRUE) {
		echo "1";
	}else{
		echo 0;
	}






	?>
