<?php
session_start();
require_once 'db_connection.php';
date_default_timezone_set('Asia/Colombo');
$currentDate = date('Y-m-d H:i:s');

$searchbarcode = $_POST['searchbarcode'];
$searchQty = $_POST['searchQty'];
$user_id = $_SESSION['user_id'];
$branch_id = $_SESSION['branch_id'];
$sql1 = "SELECT * FROM mobileproducts WHERE prid='$searchbarcode' AND status=1";
$result1 = $conn->query($sql1);
$r = mysqli_fetch_assoc($result1);
$expiredate = $r['expdate'];
$today = date('Y-m-d');
//$expiredate = new date($expiredate);

if ($expiredate <= $today && $expiredate != "0000-00-00") {
	
	echo "Sorry, product has expired on ".$expiredate;
	
}else{


    $productdetails = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM mobileproducts WHERE prid='$searchbarcode' AND status=1"));
    
    $prid = $productdetails['prid'];
    $price = $productdetails['sellprice'];
    $pro_dis = 0;
    //$searchQty = $productdetails['prid'];
    //$user_id = $productdetails['prid'];
   // $currentDate = $productdetails['prid'];
   // $branch_id = $productdetails['prid'];
   




    $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) {
        echo "1";
    }else{
        echo "0";
    }




/*$sql = "SELECT * FROM mobileproducts WHERE prid='$searchbarcode' AND status=1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {

	while ($row = $result->fetch_assoc()) {
		$prid = $row["prid"];
		$proname = $row["proname"];
		$cover = $row["cover"];
		$pro_dis = $row["discount"];
		$price = $row["sellprice"];
		$qty = $row["qty"];
		$mqty = $row["mqty"];

		$user_qury_02 = mysqli_query($conn, "SELECT * FROM tbl_user_branch WHERE user_id='" . $_SESSION['user_id'] . "'");
		$user_res = mysqli_fetch_assoc($user_qury_02);
		$user_branch = $user_res['branch_id'];

		//$user_qury_03 = mysqli_query($conn, "SELECT * FROM tbl_stock WHERE prid = '$prid' AND branch_id='$user_branch'");
		$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){

			
			//check product is added
			$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($proname=="General Item"){
				$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) {
				echo 1;
			} else {
				echo "Error: " . $sqll . "<br>" . $conn->error;
			}
			
		}else{
			echo "Error: this product does not have a quantity";
		}

	}
} else {
	echo "Not Found Any Products..";
}
*/
}
?>