<?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{





$searchQtyDefault = 1;



$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["discount1"];

		$price = $row["sellprice"];

		$qty = $row["qty"];

		$mqty = $row["mqty"];

		$discount = $row["discount1"];

		$discount_type = $row["discount_type1"];

		$costprice = $row["price"];

		$itemcode = $row["itemcode"];
		 $whole_sale_price = $row["whole_sale_price"];
		  $loose_price = $row["loose_price"];
		if($discount_type==1){

			$dis = $discount."%";

		}else{

			$dis = $discount;

		}





		$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' AND branch_id='$user_branch'");

		$user_res1 = mysqli_fetch_assoc($user_qury_03);

		$sqty = $user_res1['qty'];

		$temp = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM mobiletemporarycart WHERE proid='$prid' AND orderid='0'"));
    	$pro_qty = $temp['pro_qty'];
		$sqty = $sqty - $pro_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 branch_id='$branch_id' AND orderid=0 AND hold=0 ";

			$result33 = $conn->query($sql33);

			

			if($proname=="genaral items"){

				

				$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 branch_id='$branch_id' AND user_id='$user_id' AND orderid=0   ";

				$info = mysqli_fetch_array(mysqli_query($conn, "SELECT proid,pro_qty FROM mobiletemporarycart WHERE proid ='$prid' AND orderid='0'"));
				$pro_qty2 = $info['pro_qty'];
				$pro_qty1 = floatval($info['pro_qty'])+1;
				$stock = mysqli_fetch_array(mysqli_query($conn, "SELECT qty FROM tbl_stock WHERE prid='$prid'"));
    	    	$stockqty = floatval($stock['qty']);
				if($stockqty >= $pro_qty1){
					$sqll = "UPDATE mobiletemporarycart SET pro_qty = pro_qty+1  WHERE proid='$prid' AND pro_dis='$pro_dis' AND branch_id='$branch_id' AND user_id='$user_id' AND orderid=0";
				}else{
					$sqll = "UPDATE mobiletemporarycart SET pro_qty = '$pro_qty2'  WHERE proid='$prid' AND pro_dis='$pro_dis' AND branch_id='$branch_id' 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,$itemcode, $searchQtyDefault,$whole_sale_price,$loose_price);

				echo json_encode($lastdata);

			} else {

				echo "Error: " . $sqll . "<br>" . $conn->error;

			}

			

		}else{

			echo "0";

		}



	}

} else {

	echo "Not Found Any Products..";

}



}

?>