<?php
	session_start();
	require_once 'db_connection.php'; ?>
    <?php
	date_default_timezone_set('Asia/Colombo');
	$currentDate = date('Y-m-d H:i:s');
	$searchbarcode = $_POST['searchbarcode'];
	$user_id = $_SESSION['user_id'];
$branch_id = $_SESSION['branch_id'];
$searchQty = 1;

	$sql1 = "SELECT * FROM mobileproducts WHERE itemcode='$searchbarcode' AND status=1 LIMIT 1";
	$result1 = $conn->query($sql1);
	if ($result1->num_rows > 0) {
		$productinfo = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM mobileproducts WHERE itemcode='$searchbarcode' AND status=1 LIMIT 1"));
		//$prid = $productinfo['prid'];
		//echo "<script>addNewProductById(" . $prid . ")</script>";
	//	$prid = $row["prid"];
		$prid = $productinfo["prid"];
		$proname = $productinfo["proname"];
		$cover = $productinfo["cover"];
		$pro_dis = $productinfo["discount1"];
		$price = $productinfo["sellprice"];
		$qty = $productinfo["qty"];
		$mqty = $productinfo["mqty"];
		$discount = $productinfo["discount1"];
		$discount_type = $productinfo["discount_type1"];
		$costprice = $productinfo["price"];

		if($discount_type==1){
			$dis = $discount."%";
		}else{
			$dis = $discount;
		}



		$user_qury_03 = mysqli_query($conn, "SELECT * FROM tbl_stock WHERE prid = '$prid'");
		$user_res1 = mysqli_fetch_assoc($user_qury_03);
		$sqty = $user_res1['qty'];
		
		//if($sqty > 0){
			$sql33 = "SELECT * FROM mobiletemporarycart WHERE proid='$prid' AND pro_dis='$pro_dis' AND  user_id='$user_id' AND orderid=0 AND hold=0 ";
			$result33 = $conn->query($sql33);
			
			if($prid==148){
				
				$sqll = "INSERT INTO `mobiletemporarycart`(`tempid`, `orderid`, `proid`, `pro_price`, `pro_dis`, `pro_qty`, `user_id`, `return_qty`, `return_date`, `return_user`, `hold`, `branch_id`) VALUES (null,0,'$prid','$price','$pro_dis','$searchQty','$user_id',0,'$currentDate',0,0,'$branch_id')";

				
			}else{

			

			if ($result33->num_rows > 0) {
				$sqll = "UPDATE mobiletemporarycart SET pro_qty = pro_qty+1  WHERE proid='$prid' AND pro_dis='$pro_dis' AND  user_id='$user_id' AND orderid=0";
			} else {
				// $sqll = "INSERT INTO mobiletemporarycart (proid, pro_price,  pro_dis, pro_qty, user_id)
				// VALUES ('$prid', '$price', '$pro_dis', '$searchQty', '$user_id' )";
				$sqll = "INSERT INTO `mobiletemporarycart`(`tempid`, `orderid`, `proid`, `pro_price`, `pro_dis`, `pro_qty`, `user_id`, `return_qty`, `return_date`, `return_user`, `hold`, `branch_id`) VALUES (null,0,'$prid','$price','$pro_dis','$searchQty','$user_id',0,'$currentDate',0,0,'$branch_id')";
			}

			if ($conn->query($sqll) === TRUE) {
				$last_id = $conn->insert_id;
				$lastdata = array($last_id, $sqty, $proname, $price,$prid, $dis, $costprice);
				echo json_encode($lastdata);

			}else{
				echo "0";
			}

		}
	//} 
    
}
    
    
    ?>
