<?php
    
    function getName($usrId)
    {   
	    include'connection.php';
	    $usrDigit=substr($usrId,0,2);
	    
	    if(empty($usrId))
	    {
	        $retName="";
	        return($retName);
	    }
	    else
	    {
	        $quGetName="SELECT * FROM MLM_Users WHERE Userid='$usrId'";
	        if($reGetName=mysqli_query($con,$quGetName))
	        {
                $dataGetName=mysqli_fetch_array($reGetName);
                $retName=$dataGetName['Name'];
                return($retName);
	        }
	        else
	        {
	            $retName="";
                return($retName);
	        }
	    }
    }
    
    function getUser($usrId)
    {   
	    include'connection.php';
	    $usrDigit=substr($usrId,0,2);
	    
	    if(empty($usrDigit))
	    {
	        $retName=" ";
	        return($retName);
	    }
	    elseif($usrDigit==11 || $usrDigit==22)
	    {
	        $retName="ADMIN";
	        return($retName);
	    }
	    else
	    {
	        
	        $quGetName="SELECT * FROM MLM_Users WHERE Userid='$usrId'";
	        $reGetName=mysqli_query($con,$quGetName);
            $dataGetName=mysqli_fetch_array($reGetName);
            return($dataGetName);
	    }
    }

    function getBal($usrId)
    {
        include'connection.php';
        $quUserBal="SELECT * FROM MLM_Wallet WHERE Userid='$usrId'";
        $reUserBal=mysqli_query($con,$quUserBal);
	    $Bal=0;
	    while($dataDisOpBal=mysqli_fetch_array($reUserBal))
	    {
	       $Txn= $dataDisOpBal['Txn'];
	       $Bal=$Bal+$Txn;
	    }
	    return($Bal);
    }
    
    function checkChild($parentId, $childId)
    {
        include'connection.php';
        $flag=0;
        $tmp_ChildId=$childId;
        while($flag==0)
        {
            $qu="SELECT * FROM MLM_Users WHERE Userid='$tmp_ChildId'";
            $re=mysqli_query($con,$qu);
            $count=mysqli_num_rows($re);
            if($count==0)
            {
                $flag=1;
                $result="Invalid Userid !"; 
            }
            else
            {
                $data=mysqli_fetch_array($re);
                if($data['Parent_Id']==$parentId)
                {
                    $flag=1;
                    $result="true";
                }
                elseif(empty($data['Parent_Id']))
                {
                    $flag=1;
                    $result="User Is Not In Your Downline !";
                }
                else
                {
                    $tmp_ChildId=$data['Parent_Id'];
                }
            }
        }
        return($result);
    }
    
    function checkPlace($parentId)
    {
        include'connection.php';
        $quTree="SELECT * FROM MLM_Tree WHERE Userid='$parentId'";
        $reTree=mysqli_query($con,$quTree);
        $dataTree=mysqli_fetch_array($reTree);
        $rightSide=$dataTree['RightSide'];
        $leftSide=$dataTree['LeftSide'];
        
        $countParent=mysqli_num_rows($reTree);
        if($countParent<1)
        {
            return("Parent User Not Found");
        }
        elseif(empty($rightSide))
        {
            return("Right");
        }
        elseif(empty($leftSide))
        {
            return("Left");
        }
        else
        {
            return("PNA");
        }
        
    }
    
    
    function getOp($opName)
    {
        include'connection.php';
        $quGetOp="SELECT * FROM operator WHERE Operator='$opName'";
        $reGetOp=mysqli_query($con,$quGetOp);
        $dataOp=mysqli_fetch_array($reGetOp);
        
        return($dataOp);
    }
    
    function getDupRech($mobileNo,$amount,$opName,$date)
    {
        include'connection.php';
        $quDupRech="SELECT * FROM recharge WHERE Mobile='$mobileNo' AND Amount='$amount' AND Companyname='$opName' AND Rechargedate='$date' AND Status='SUCCESS' OR Mobile='$mobileNo' AND Amount='$amount' AND Companyname='$opName' AND Rechargedate='$date' AND Status='PENDING'";
	    $reDupRech=mysqli_query($con,$quDupRech);
	    $countDupRech=mysqli_num_rows($reDupRech);
	    return($countDupRech);
    }
    
    function getComm($usrId,$opName)
    {
        include'connection.php';
        $quComm="SELECT * FROM CommSlab WHERE Userid='$usrId' AND OpName='$opName'";
	    $reComm=mysqli_query($con,$quComm);
	    $dataComm=mysqli_fetch_array($reComm);
	    $usrComm=$dataComm['Comm'];
	    return($usrComm);
    }
    
    
    function chk_msg($msg_string, $chk_string)
    {
        include'connection.php';
        $msg_upper_string=strtoupper($msg_string);
        $chk_upper_string=strtoupper($chk_string);
        $result=similar_text($msg_upper_string, $chk_upper_string);
        
        return($result);
    }
    
    function getAFS()
    {
        include'connection.php';
        $quGetAutoFailedStatus="SELECT * FROM Auto_Failed WHERE Id='1'";
		$reGetAutoFailedStatus=mysqli_query($con,$quGetAutoFailedStatus);
		$dataGetAutoFailedStatus=mysqli_fetch_array($reGetAutoFailedStatus);
		$AutoFailedStatus=$dataGetAutoFailedStatus['Status'];
		return($AutoFailedStatus);
    }
    
    function getApiNo($newrechid)
    {
        include'connection.php';
        $quGetApiNo="SELECT API_NO FROM recharge WHERE Id='$newrechid'";
        $reGetApiNo=mysqli_query($con,$quGetApiNo);
        $dataApiNo=mysqli_fetch_array($reGetApiNo);
        $apiNo=$dataApiNo['API_NO'];
        
        return($apiNo);
    }
    
?>