<?php 

    function getExtension($str){
        $i=strpos($str,'.');
        if(!$i){
            return "";
        }
        $l=strlen($str)-$i;
        $ext=substr($str,$i+1,$l);
        return $ext;
    }
    
    function getmember($userid){
        $sql="SELECT * FROM mlmmembers WHERE userid=$userid";
        $result=mysql_query($sql) or die(mysql_error());
        $rows=mysql_fetch_array($result);
        return $username =$rows['username'];
    }

    function getsponsor($sponsorid){
        $sql="SELECT * FROM mlmmembers WHERE profileid=$sponsorid";
        $result=mysql_query($sql) or die(mysql_error());
        $rows=mysql_fetch_array($result);
        return $username =$rows['username'];
    }
	

    function directmembers($profileid){
		$sql="SELECT * FROM mlmmembers WHERE sponsorid='$profileid'";
		$result=mysql_query($sql) or die(mysql_error());
		//if($result){
	   return $count = mysql_num_rows($result);       
	}

	function referralbonus($sponsorid,$userid,$bonustype='Referral'){
		$sql="SELECT * FROM mlmmembers WHERE profileid='$sponsorid'";
        $result=mysql_query($sql) or die(mysql_error());
        if($result){ 
            $rows=mysql_fetch_array($result);
            $sponsoruserid=$rows['userid'];
            
            //$sql="SELECT * FROM mlmbonus WHERE bonustype='$bonustype";
            //$result
            $bonusamount=3;

            $transdescription='Referral bonus received on  ' .  getmember($userid);       
            
            $sql="INSERT INTO mlmmemberledger(userid, transdate,transdescription,creditamount,type) VALUES ($sponsoruserid,CURDATE(),'$transdescription',$bonusamount,'Referral Bonus')";
            $result=mysql_query($sql) or die(mysql_error());
            $transrefid=mysql_insert_id();

            //debit company ledger
            $debitamount=$bonusamount;
            $transdescription='Referral bonus paid to ' . getmember($sponsoruserid) . ' on ' . getmember($userid);
            $sql="INSERT INTO mlmledger(beneficiaryid,transdate, debitamount, transdescription, transrefid,type) VALUES ($sponsoruserid, CURDATE(), $debitamount, '$transdescription', $transrefid,'Referral Bonus')";
            $result=mysql_query($sql) or die(mysql_error());

        }
	}

	function currentstage($profileid){
		$sql="SELECT * FROM mlmmembers WHERE profileid='$profileid'";
		$result=mysql_query($sql) or die(mysql_error());
		$rows=mysql_fetch_array($result);
		$stage=$rows['currentstage'];
		return $stage;
	}

	function getbonus ($bonustype,$bonusstage){ 
		$sql="SELECT * FROM mlmbonus WHERE bonustype='$bonustype' AND bonusstage='$bonusstage'";
		$result=mysql_query($sql) or die(mysql_error());
		if($result){ 
			$rows=mysql_fetch_array($result);
			return $rows['bonusamount'];
		}
    }
    
    function memberwallet($userid){
        $sql="SELECT SUM(creditamount) as balance FROM mlmmemberledger WHERE userid=$userid";
        $result=mysql_query($sql) or die(mysql_error());
        
        if($result){
            $rows=mysql_fetch_array($result);
            return $rows[0];
        }else{
            return 0;
        }
                
    }

    function referralbalance($userid){
        $sql="SELECT SUM(creditamount) as balance FROM mlmmemberledger WHERE userid=$userid AND type='Referral Bonus'";
        $result=mysql_query($sql) or die(mysql_error());
        
        if($result){
            $rows=mysql_fetch_array($result);
            return $rows[0];
        }else{
            return 0;
        }
                
    }

    function matrixbonus($userid){
        $sql="SELECT SUM(creditamount) as balance FROM mlmmemberledger WHERE userid=$userid AND type='Matrix Bonus'";
        $result=mysql_query($sql) or die(mysql_error());
        
        if($result){
            $rows=mysql_fetch_array($result);
            return $rows[0];
        }else{
            return 0;
        }
                
    }
    
    function stepoutbonus($userid){
        $sql="SELECT SUM(creditamount) as balance FROM mlmmemberledger WHERE userid=$userid AND type='StepOut Bonus'";
        $result=mysql_query($sql) or die(mysql_error());
        
        if($result){
            $rows=mysql_fetch_array($result);
            return $rows[0];
        }else{
            return 0;
        }
                
    }

    function totaldebit($userid){
        $sql="SELECT SUM(debitamount) as balance FROM mlmmemberledger WHERE userid=$userid";
        $result=mysql_query($sql) or die(mysql_error());
        
        if($result){
            $rows=mysql_fetch_array($result);
            return $rows[0];
        }else{
            return 0;
        }
                
    }

    function totalreceived($userid){
        $sql="SELECT SUM(creditamount) as balance FROM mlmmemberledger WHERE userid=$userid AND type='Received'";
        $result=mysql_query($sql) or die(mysql_error());
        
        if($result){
            $rows=mysql_fetch_array($result);
            return $rows[0];
        }else{
            return 0;
        }
                
    }
    

    function gettickets($recipientemail){
        $sql="SELECT * FROM mlmtickets WHERE recipientemail='$recipientemail'";
        $result=mysql_query($sql) or die(mysql_error());
        $count=mysql_num_rows($result);
        return $count;
    }
    
    function getpinbalance($username){
        $sql="SELECT * FROM mlmepins WHERE createdby='$username' AND status='NOT USED'";
        $result=mysql_query($sql) or die(mysql_error());
        $no=mysql_num_rows($result);
        return $no;        
    }

    function newmatrixbonus($parentid){
        $sql="SELECT * FROM mlmmembers WHERE parentid=$parentid";
        $result=mysql_query($sql) or die(mysql_error());
        $rows=mysql_fetch_array($result);
        $parentprofileid=$rows['profileid'];
        $parentuserid=$rows['userid'];
        $parentcurrentstage=$rows['currentstage'];
        $numberofchildren=mysql_num_rows($result);

        if(($numberofchildren==2) && $parentcurrentstage=='Pawn'){
            $transdescription='Matrix bonus earn From Pawn to stage 1 - Rook Star' ;                      
            $creditamount=5;
            $sql="INSERT INTO mlmmemberledger(userid, transdate,transdescription,creditamount,type) VALUES ($parentid, CURDATE(), '$transdescription',$creditamount,'Matrix Bonus')"; 
            $result=mysql_query($sql) or die(mysql_error());

            //2-debit company-2//
            $transrefid=mysql_insert_id(); 
            $debitamount=5;
            $transdescription='Matrix bonus paid to ' . getmember($parentid);
            $sql="INSERT INTO mlmledger(beneficiaryid,transdate, debitamount, transdescription, transrefid,type) VALUES ($parentid, CURDATE(), $debitamount, '$transdescription', $transrefid,'Matrix Bonus')";
            $result=mysql_query($sql) or die(mysql_error());

            //3-update level-3//
            $result=mysql_query("UPDATE mlmmembers SET currentstage='Rook Star', level='Level 1' WHERE userid='$parentid'");

            $parentid=getparentuserid($parentid);
            pawnstepout($parentid);

        }elseif ($parentcurrentstage=='Rook Star') {
            # code...
            #count the number of children with whose currentstage Pawn

            $numofrook=countrook($parentid);

            if($numofrook==14){

                $transdescription='Matrix bonus earn From Rook Star to stage 2 - Knight Star' ;                      
                $creditamount=10;
                $sql="INSERT INTO mlmmemberledger(userid, transdate,transdescription,creditamount,type) VALUES ($parentid, CURDATE(), '$transdescription',$creditamount,'Matrix Bonus')"; 
                $result=mysql_query($sql) or die(mysql_error());

                //2-debit company-2//
                $transrefid=mysql_insert_id(); 
                $debitamount=10;
                $transdescription='Matrix bonus paid to ' . getmember($parentid);
                $sql="INSERT INTO mlmledger(beneficiaryid,transdate, debitamount, transdescription, transrefid,type) VALUES ($parentid, CURDATE(), $debitamount, '$transdescription', $transrefid,'Matrix Bonus')";
                $result=mysql_query($sql) or die(mysql_error());

                //3-update level-3//
                $result=mysql_query("UPDATE mlmmembers SET currentstage='Knight Star', level='Level 2' WHERE userid='$parentid'");

                $parentid=getparentuserid($parentid);
                rookstepout($parentid);

            }


        }elseif ($parentcurrentstage=="Knight Star") {
            # code...
            $numofknight=countknight($parentid);

            if($numofknight==14){

                $transdescription='Matrix bonus earn From Knight Star to stage 3 - Bishop Star' ;                      
                $creditamount=100;
                $sql="INSERT INTO mlmmemberledger(userid, transdate,transdescription,creditamount,type) VALUES ($parentid, CURDATE(), '$transdescription',$creditamount,'Matrix Bonus')"; 
                $result=mysql_query($sql) or die(mysql_error());

                //2-debit company-2//
                $transrefid=mysql_insert_id(); 
                $debitamount=100;
                $transdescription='Matrix bonus paid to ' . getmember($parentid);
                $sql="INSERT INTO mlmledger(beneficiaryid,transdate, debitamount, transdescription, transrefid,type) VALUES ($parentid, CURDATE(), $debitamount, '$transdescription', $transrefid,'Matrix Bonus')";
                $result=mysql_query($sql) or die(mysql_error());

                //3-update level-3//
                $result=mysql_query("UPDATE mlmmembers SET currentstage='Bishop Star', level='Level 3' WHERE userid='$parentid'");

                $parentid=getparentuserid($parentid);
                knightstepout($parentid);
            }

        }elseif ($parentcurrentstage=='Bishop Star') {
            # code...
            $numofbishop=countbishop($parentid);

            if($numofbishop==14){

                $transdescription='Matrix bonus earn From Bishop Star to stage 4 - King Star' ;                      
                $creditamount=300;
                $sql="INSERT INTO mlmmemberledger(userid, transdate,transdescription,creditamount,type) VALUES ($parentid, CURDATE(), '$transdescription',$creditamount,'Matrix Bonus')"; 
                $result=mysql_query($sql) or die(mysql_error());

                //2-debit company-2//
                $transrefid=mysql_insert_id(); 
                $debitamount=300;
                $transdescription='Matrix bonus paid to ' . getmember($parentid);
                $sql="INSERT INTO mlmledger(beneficiaryid,transdate, debitamount, transdescription, transrefid,type) VALUES ($parentid, CURDATE(), $debitamount, '$transdescription', $transrefid,' Matrix Bonus')";
                $result=mysql_query($sql) or die(mysql_error());

                //3-update level-3//
                $result=mysql_query("UPDATE mlmmembers SET currentstage='King Star', level='Level 4' WHERE userid='$parentid'");

                $parentid=getparentuserid($parentid);
                bishopstepout($parentid);

            }
        }elseif ($parentcurrentstage=='Queen Star') {
            # code...
            $numofqueen=countqueen($parentid);

            if($numofqueen==6){

                $transdescription='Matrix bonus earn From King Star to stage 4 - Queen Star' ;                      
                $creditamount=5000;
                $sql="INSERT INTO mlmmemberledger(userid, transdate,transdescription,creditamount,type) VALUES ($parentid, CURDATE(), '$transdescription',$creditamount,'Matrix Bonus')"; 
                $result=mysql_query($sql) or die(mysql_error());

                //2-debit company-2//
                $transrefid=mysql_insert_id(); 
                $debitamount=5000;
                $transdescription='Matrix bonus paid to ' . getmember($parentid);
                $sql="INSERT INTO mlmledger(beneficiaryid,transdate, debitamount, transdescription, transrefid,type) VALUES ($parentid, CURDATE(), $debitamount, '$transdescription', $transrefid,'Matrix Bonus')";
                $result=mysql_query($sql) or die(mysql_error());

                //3-update level-3//
                $result=mysql_query("UPDATE mlmmembers SET currentstage='Queen Star', level='Level 5' WHERE userid='$parentid'");

                $parentid=getparentuserid($parentid);
                queenstepout($parentid);

            }
        }

    }

    function countrook($id){

        global $count;
        echo $sql="SELECT * FROM mlmmembers WHERE parentid=$id AND currentstage='Rook Star'";
        $result=mysql_query($sql) or die(mysql_error());
        $count+=mysql_num_rows($result);

        while($rows=mysql_fetch_array($result)){
            $id=$rows['userid'];
            countrook($id);
        }

        return $count;
    }

    function countknight($id){

        global $count;
        $sql="SELECT * FROM mlmmembers WHERE parentid=$id AND currentstage='Knight Star'";
        $result=mysql_query($sql) or die(mysql_error());
        $count+=mysql_num_rows($result);

        while($rows=mysql_fetch_array($result)){
            $id=$rows['userid'];
            countknight($id);
        }

        return $count;
    }
    

    function pawnstepout1($parentid){
        global $num;
        $sql="SELECT * FROM mlmmembers WHERE userid=$parentid";
        $result=mysql_query($sql)  or die(mysql_error());

        if(mysql_num_rows($result)==1){
            if($parentid!=0){
                //if(mysql_num_rows($result)==1){
                    $transdescription='StepOut Bonus' ;                      
                    $creditamount=5;
                    $sql="INSERT INTO mlmmemberledger(userid, transdate,transdescription,creditamount,type) VALUES ($parentid, CURDATE(), '$transdescription',$creditamount,'StepOut Bonus')"; 
                    $result=mysql_query($sql) or die(mysql_error());

                    //2-debit company-2//
                    $transrefid=mysql_insert_id(); 
                    $debitamount=5;
                    $transdescription='StepOut bonus paid to ' . $parentid;
                    $sql="INSERT INTO mlmledger(beneficiaryid,transdate, debitamount, transdescription, transrefid,type) VALUES ($parentid, CURDATE(), $debitamount, '$transdescription', $transrefid,'StepOut Bonus')";
                    $result=mysql_query($sql) or die(mysql_error());
                //} 
            }
        }
        
        while($rows=mysql_num_rows($result)){
            $parentid=$rows['parentid'];
            pawnstepout($parentid);
        }
    }

    function pawnstepout($parentid){
        global $num;
        $sql="SELECT * FROM mlmmembers WHERE userid=$parentid";
        $result=mysql_query($sql)  or die(mysql_error());

        if(mysql_num_rows($result)==1){
            if($parentid!=0){
                //if(mysql_num_rows($result)==1){
                    $transdescription='StepOut Bonus' ;                      
                    $creditamount=5;
                    $sql="INSERT INTO mlmmemberledger(userid, transdate,transdescription,creditamount,type) VALUES ($parentid, CURDATE(), '$transdescription',$creditamount,'StepOut Bonus')"; 
                    $result=mysql_query($sql) or die(mysql_error());

                    //2-debit company-2//
                    $transrefid=mysql_insert_id(); 
                    $debitamount=5;
                    $transdescription='StepOut bonus paid to ' . $parentid;
                    $sql="INSERT INTO mlmledger(beneficiaryid,transdate, debitamount, transdescription, transrefid,type) VALUES ($parentid, CURDATE(), $debitamount, '$transdescription', $transrefid,'StepOut Bonus')";
                    $result=mysql_query($sql) or die(mysql_error());
                //} 
            }
        }
        
        while($rows=mysql_fetch_array($result)){
            $parentid=$rows['parentid'];
            pawnstepout($parentid);
        }
    }

    function rookstepout($parentid){
        global $num;
        $sql="SELECT * FROM mlmmembers WHERE userid=$parentid";
        $result=mysql_query($sql)  or die(mysql_error());
        
        if(mysql_num_rows($result)==1){
            $transdescription='StepOut Bonus' ;                      
            $creditamount=10;
            $sql="INSERT INTO mlmmemberledger(userid, transdate,transdescription,creditamount,type) VALUES ($parentid, CURDATE(), '$transdescription',$creditamount,'StepOut Bonus')"; 
            $result=mysql_query($sql) or die(mysql_error());

            //2-debit company-2//
            $transrefid=mysql_insert_id(); 
            $debitamount=10;
            $transdescription='StepOut bonus paid to ' . getmember($parentid);
            $sql="INSERT INTO mlmledger(beneficiaryid,transdate, debitamount, transdescription, transrefid,type) VALUES ($parentid, CURDATE(), $debitamount, '$transdescription', $transrefid,'StepOut Bonus')";
            $result=mysql_query($sql) or die(mysql_error());
        } 

        while($rows=mysql_num_rows($result)){
            $parentid=$rows['parentid'];
            rookstepout($parentid);
        }           
    }

    function knightstepout($parentid){
        global $num;
        $sql="SELECT * FROM mlmmembers WHERE userid=$parentid";
        $result=mysql_query($sql)  or die(mysql_error());
        
        if(mysql_num_rows($result)==1){
            $transdescription='StepOut Bonus' ;                      
            $creditamount=20;
            $sql="INSERT INTO mlmmemberledger(userid, transdate,transdescription,creditamount,type) VALUES ($parentid, CURDATE(), '$transdescription',$creditamount,'StepOut Bonus')"; 
            $result=mysql_query($sql) or die(mysql_error());

            //2-debit company-2//
            $transrefid=mysql_insert_id(); 
            $debitamount=20;
            $transdescription='StepOut bonus paid to ' . getmember($parentid);
            $sql="INSERT INTO mlmledger(beneficiaryid,transdate, debitamount, transdescription, transrefid,type) VALUES ($parentid, CURDATE(), $debitamount, '$transdescription', $transrefid,'StepOut Bonus')";
            $result=mysql_query($sql) or die(mysql_error());
        }  
        while($rows=mysql_num_rows($result)){
            $parentid=$rows['parentid'];
            knightstepout($parentid);
        }

    }

    function bishopstepout($parentid){
        global $num;
        $sql="SELECT * FROM mlmmembers WHERE userid=$parentid";
        $result=mysql_query($sql)  or die(mysql_error());
        
        if(mysql_num_rows($result)==1){
            $transdescription='StepOut Bonus' ;                      
            $creditamount=20;
            $sql="INSERT INTO mlmmemberledger(userid, transdate,transdescription,creditamount,type) VALUES ($parentid, CURDATE(), '$transdescription',$creditamount,'StepOut Bonus')"; 
            $result=mysql_query($sql) or die(mysql_error());

            //2-debit company-2//
            $transrefid=mysql_insert_id(); 
            $debitamount=20;
            $transdescription='StepOut bonus paid to ' . getmember($parentid);
            $sql="INSERT INTO mlmledger(beneficiaryid,transdate, debitamount, transdescription, transrefid,type) VALUES ($parentid, CURDATE(), $debitamount, '$transdescription', $transrefid,'StepOut Bonus')";
            $result=mysql_query($sql) or die(mysql_error());
        }  
        while($rows=mysql_num_rows($result)){
            $parentid=$rows['parentid'];
            bishopstepout($parentid);
        }           
    }

    function kingstepout($parentid){
        global $num;
        $sql="SELECT * FROM mlmmembers WHERE userid=$parentid";
        $result=mysql_query($sql)  or die(mysql_error());
        
        if(mysql_num_rows($result)==1){
            $transdescription='StepOut Bonus' ;                      
            $creditamount=50;
            $sql="INSERT INTO mlmmemberledger(userid, transdate,transdescription,creditamount,type) VALUES ($parentid, CURDATE(), '$transdescription',$creditamount,'StepOut Bonus')"; 
            $result=mysql_query($sql) or die(mysql_error());

            //2-debit company-2//
            $transrefid=mysql_insert_id(); 
            $debitamount=50;
            $transdescription='StepOut bonus paid to ' . getmember($parentid);
            $sql="INSERT INTO mlmledger(beneficiaryid,transdate, debitamount, transdescription, transrefid,type) VALUES ($parentid, CURDATE(), $debitamount, '$transdescription', $transrefid,'StepOut Bonus')";
            $result=mysql_query($sql) or die(mysql_error());
        }  
        while($rows=mysql_num_rows($result)){
            $parentid=$rows['parentid'];
            kingstepout($parentid);
        }           
    }

    function queenstepout($parentid){
        global $num;
        $sql="SELECT * FROM mlmmembers WHERE userid=$parentid";
        $result=mysql_query($sql)  or die(mysql_error());
        
        if(mysql_num_rows($result)==1){
            $transdescription='StepOut Bonus' ;                      
            $creditamount=300;
            $sql="INSERT INTO mlmmemberledger(userid, transdate,transdescription,creditamount,type) VALUES ($parentid, CURDATE(), '$transdescription',$creditamount,'StepOut Bonus')"; 
            $result=mysql_query($sql) or die(mysql_error());

            //2-debit company-2//
            $transrefid=mysql_insert_id(); 
            $debitamount=300;
            $transdescription='StepOut bonus paid to ' . getmember($parentid);
            $sql="INSERT INTO mlmledger(beneficiaryid,transdate, debitamount, transdescription, transrefid,type) VALUES ($parentid, CURDATE(), $debitamount, '$transdescription', $transrefid,'StepOut Bonus')";
            $result=mysql_query($sql) or die(mysql_error());
        }   
        while($rows=mysql_num_rows($result)){
            $parentid=$rows['parentid'];
            queenstepout($parentid);
        }          
    }

    function computematrixbonus($profileid){
    	//if($profileid!='RL20180100001'){
            //$profileid='RL20180100001';
            $sql="SELECT * FROM mlmmembers WHERE sponsorid='$profileid'";
            $result=mysql_query($sql) or die(mysql_error());
            
            $count=mysql_num_rows($result);

            $sql2="SELECT * FROM mlmmembers WHERE profileid='$profileid'";
            $result2=mysql_query($sql2) or die(mysql_error());
            $rows2=mysql_fetch_array($result2);
            $currentstage=$rows2['currentstage'];
            $userid=$rows2['userid'];

            switch ($currentstage){
                case 'Pawn':
                    if($count==2){
                        //-credit member-
                       $transdescription='Matrix bonus earn From Pawn to stage 1 - Rook Star' ;                      
                        $creditamount=5;
                        $sql="INSERT INTO mlmmemberledger(userid, transdate,transdescription,creditamount) VALUES ($userid, CURDATE(), '$transdescription',$creditamount)"; 
                        $result=mysql_query($sql) or die(mysql_error());

                        //-debit company-
                        $transrefid=mysql_insert_id(); 
                        $debitamount=5;
                        $transdescription='Matrix bonus paid to ' . $profileid;
                        $sql="INSERT INTO mlmledger(beneficiaryid,transdate, debitamount, transdescription, transrefid) VALUES ($userid, CURDATE(), $debitamount, '$transdescription', $transrefid)";
                        $result=mysql_query($sql) or die(mysql_error());

                        //update level
                        $result=mysql_query("UPDATE mlmmembers SET currentstage='Rook Star', level='Level 1' WHERE profileid='$profileid'");
                    }

                    //check for stepout

                    break;
                case 'Rook Star':
                    if($count==14){

                    }
                
                default:
                    # code...
                    break;
            }
        //}
    }

    function computestepout($parentid,$stage='Pawn'){
        $sql="SELECT * FROM mlmbonus WHERE bonusstage='$stage'";
        $result=mysql_query($sql) or die(mysql_errno());
        if($result){
            $rows=mysql_fetch_array($result);
            $bonusamount=$rows['bonusamount'];
        }
        $sql2="SELECT * FROM mlmmembers WHERE userid=$parentid";
        $result2=mysql_query($sql2) or die(mysql_error());
        if($result2){
            $transdescription='Stepout bonus earn ' ;  
            $userid=$parentid;                    ;
            $creditamount=$bonusamount;
            $sql3="INSERT INTO mlmmemberledger(userid, transdate,transdescription,creditamount,type) VALUES ($userid, CURDATE(), '$transdescription',$creditamount,'Bonus')"; 
            $result3=mysql_query($sql) or die(mysql_error());

            $transrefid=mysql_insert_id(); 
            $debitamount=$bonusamount;
            $transdescription='Stepout bonus paid to ' . $parentid;
            $sql4="INSERT INTO mlmledger(beneficiaryid,transdate, debitamount, transdescription, transrefid) VALUES ($userid, CURDATE(), $debitamount, '$transdescription', $transrefid)";
            $result4=mysql_query($sql4) or die(mysql_error());


        }

        while($rows2=mysql_fetch_array($result2)){
            $userid2=$rows2['userid'];
            computestepout($userid2);
        }

    }

    function setparentid($profileid,$userid){
        
        $sql="SELECT * FROM mlmmembers WHERE sponsorid='$profileid'";

        $result=mysql_query($sql) or die(mysql_error());
        $rows=mysql_fetch_array($result);
        $myuserid=$rows['userid'];
        $count=mysql_num_rows($result);

        if($count>2){
            $sql="SELECT * FROM mlmmembers WHERE sponsorid='$profileid'";
            $result=mysql_query($sql) or die(mysql_error());

            while ($rows=mysql_fetch_array($result)) {
                setparentid($rows['profileid'],$userid);
            }
        }else{
            $parentid=$myuserid;
        }
        
        $sqlupdate="UPDATE mlmmembers SET parentid='$parentid' WHERE userid=" . $userid;
        $rstUpdate=mysql_query($sqlupdate) or die(mysql_error());    

        return $parentid;    
    }


    function setparentid_old($profileid,$userid){
        
        $sponsoruserid=getuserid($profileid);

        $sql="SELECT * FROM mlmmembers WHERE parentid=$sponsoruserid ORDER BY userid ASC";

        $result=mysql_query($sql) or die(mysql_error());
        $count=mysql_num_rows($result);
        //echo "sponsoruserid= " . $sponsoruserid . " and No. of record: " . $k . "<br/>";

        if($count==2){
                        
            while ($rows=mysql_fetch_array($result)) {
                $profileid=$rows['profileid'];
                setparentid_old($profileid,$userid);              
            }
        }else{
            $parentid=$sponsoruserid;
            $sqlupdate="UPDATE mlmmembers SET parentid='$parentid' WHERE userid=" . $userid;
            $rstUpdate=mysql_query($sqlupdate) or die(mysql_error());            
        }  
        //update leg values
        $parentid=$sponsoruserid;
        $sql="SELECT * FROM mlmmembers WHERE parentid=$parentid";
        $result=mysql_query($sql) or die(mysql_error());
        $count=mysql_num_rows($result);

        if($count==1){
            $sqlupdate="UPDATE mlmmembers SET l_member=1 WHERE userid=$userid";
        }elseif($count==2){
            $sqlupdate="UPDATE mlmmembers SET r_member=1 WHERE userid=$userid";
        }
        
        $rstUpdate=mysql_query($sqlupdate) or die(mysql_error());

        //computermatrixbonus
        newmatrixbonus($parentid);
    }

    function get_all_children($parentid,$grand_child=false){
        $child_result=mysql_query("SELECT * FROM mlmmembers WHERE parentid=$parentid");

        if(mysql_num_rows($child_result)>0){
            ?>
                <ul>
                    <?php
                        while($rows=mysql_fetch_array($child_result)){
                            ?>
                            <li><a href="#"><?php echo $rows['username'];?></a>
                            <?php get_all_children($rows['userid'],true);?> 
                        </li>
                        <?php
                        }
                    ?>
                </ul>
            <?php
        }
    }

    function registrationcalculation($userid,$bonustype='Welcome'){
        
        $sql="SELECT * FROM mlmmembers WHERE userid=$userid";
        $result=mysql_query($sql) or die(mysql_error());
        if($result){
            $rows=mysql_fetch_array($result);
            $currentstage=$rows['currentstage'];
            $profileid=$rows['profileid'];
            $sponsorid=$rows['sponsorid'];

            $sql="SELECT * FROM mlmbonus WHERE bonustype='$bonustype'";
            $res=mysql_query($sql) or die(mysql_error());
            $rows=mysql_fetch_array($res);
            $bonusamount=$rows['bonusamount'];

            $transdescription='Membership Welcome bonus';
            $sql="INSERT INTO mlmmemberledger(userid,transdate, creditamount, transdescription,type) VALUES ($userid,CURDATE(),$bonusamount,'$transdescription','Bonus')";
            $result=mysql_query($sql) or die(mysql_error() . " - " . mysql_errno());
            $transrefid=mysql_insert_id();

            $debitamount=$bonusamount;
            $transdescription='Welcome bonus paid to ' . getmember($userid);
            $sql="INSERT INTO mlmledger(beneficiaryid,transdate, debitamount, transdescription, transrefid,type) VALUES ($userid, CURDATE(), $debitamount, '$transdescription', $transrefid,'Bonus')";
            $result=mysql_query($sql) or die(mysql_error());

            //credit company ledger
            $creditamount=30;
            $transdescription='Membership registration fees for ' . getmember($userid);
            $sql="INSERT INTO mlmledger(beneficiaryid,transdate, creditamount, transdescription, transrefid,type) VALUES ($userid, CURDATE(), $creditamount, '$transdescription', $transrefid,'Registration')";
            $result=mysql_query($sql) or die(mysql_error());
        }
    }

    function downlinemembers2($userid){
        global $count;
        $sql="SELECT * FROM mlmmembers WHERE parentid=$userid";
        $result=mysql_query($sql) or die(mysql_error());        
                
        $count=+mysql_num_rows($result);
        while($rows=mysql_fetch_array($result)){ 
            //$count+=1;     
            $id=$rows['userid'];                   
            //downlinemembers($rows['userid']);
            $sql="SELECT * FROM mlmmembers WHERE parentid=$userid";
            $result=mysql_query($sql) or die(mysql_error());        
                    
            $count=+mysql_num_rows($result);

        }
        return $count;
    }

    function countall($id){
        global $count;
        $sql="SELECT * FROM mlmmembers WHERE parentid=$id";
        $result=mysql_query($sql) or die(mysql_error());
        $count+=mysql_num_rows($result);

        while($rows=mysql_fetch_array($result)){
            $id=$rows['userid'];
            countall($id);
        }

        return $count;
    }

    function getparentid($profileid){
        $sql="SELECT * FROM mlmmembers WHERE profileid='$profileid'";
        $result=mysql_query($sql) or die(mysql_error());

        if($result){
            $rows=mysql_fetch_array($result);
            return $rows['parentid'];
        }
    }

    function getparentuserid($userid){
        $sql="SELECT * FROM mlmmembers WHERE userid='$userid'";
        $result=mysql_query($sql) or die(mysql_error());

        if($result){
            $rows=mysql_fetch_array($result);
            return $rows['parentid'];
        }
    }

    function getuserid($profileid){
        $sql="SELECT * FROM mlmmembers WHERE profileid='$profileid'";
        $result=mysql_query($sql) or die(mysql_error());

        if($result){
            $rows=mysql_fetch_array($result);
            return $rows['userid'];
        }
    }

    function setparentid3($parentid,$userid){
        $sql="SELECT * FROM mlmmembers WHERE parentid=$parentid";
        $child_result=mysql_query($sql) or die(mysql_error());      

        if(mysql_num_rows($child_result)<2){
            $rows=mysql_fetch_array($child_result);
            $myuserid=$rows['userid'];
            return $parentid=$myuserid;            
        }else{
            $sql="SELECT * mlmmembers WHERE parentid=$parentid";
            $result=mysql_query($sql) or die(mysql_error());
            while($rows=mysql_fetch_array($result)){
                
                setparentid3($rows['userid'],$userid);
            }
        }

        /*$sqlupdate="UPDATE mlmmembers SET parentid='$parentid' WHERE userid=" . $userid;
        $rstUpdate=mysql_query($sqlupdate) or die(mysql_error());*/
    }

    function setsponsorid($sponsorid,$userid){
        $sql="UPDATE mlmmembers SET sponsorid='$sponsorid' WHERE userid=$userid";
        $result=mysql_query($sql) or die(mysql_error());

        
    }

    function leftcount($id)   //Function to calculate all left children count
    {
        $sql = "SELECT userid,username,sponsoridid,parentid,l_member,r_member FROM mlmmembers WHERE userid = '$id'";
        $execsql = mysql_query($sql);
        $array = mysql_fetch_array($execsql);
        (array_count_values($array));
        $count = 0;
        if(!empty($array['l_member']))
        {
            $count += allcount($array['l_member']) +1;
        }
        return $count;
    }
    function rightcount($id)   //Function to calculate all right children count
    {
        $sql = "SELECT userid,username,sponsoridid,parentid,l_member,r_member FROM mlmmembers WHERE userid = '$id'";
        $execsql = mysql_query($sql);
        $array = mysql_fetch_array($execsql);
        (array_count_values($array));
        $count = 0;
        if(!empty($array['r_member']))
        {
            $count += allcount($array['r_member']) +1;
        }
        return $count;
    }

    function allcount($id)   //Function to calculate all children count
    {
        $sql = "SELECT userid,username,sponsoridid,parentid,l_member,r_member FROM mlmmembers WHERE userid = '$id'";
        $execsql = mysql_query($sql);
        $array = mysql_fetch_array($execsql);
        (array_count_values($array));
        $count = 0;
        if(!empty($array['l_member']))
        {
            $count += allcount($array['l_member']) +1;
        }
        if(!empty($array['r_member']))
        {
            $count += allcount($array['r_member']) +1;
        }
        return $count;
    }


    function sendmail ($to,$msg,$subject){
        require_once "mail.php";
        $from = 'MLMPro <admin@mlmpro.com>';
        $to = $to;
        $subject = $subject;
        $body = $msg;

        $headers = array(
            'From' => $from,
            'To' => $to,
            'Subject' => $subject
        );

        $smtp = Mail::factory('smtp', array(
                'host' => 'ssl://smtp.gmail.com',
                'port' => '465',
                'auth' => true,
                'username' => '#',
                'password' => '#'
            ));

        $mail = $smtp->send($to, $headers, $body);

        if (PEAR::isError($mail)) {
            echo('<p>' . $mail->getMessage() . '</p>');
        } else {
            echo('<p>Message successfully sent!</p>');
        }
    }

?>