<?php 
    function newmatrixbonus($parentid){
        $sql="SELECT * FROM mlmmembers WHERE parentid=$parentid";
        $result=mysql_query($sql) or die(mysql_error());
        
        $numberofchildren=mysql_num_rows($result);
        //$numberofchildren=mysql_num_rows($result);

        $sql1="SELECT * FROM mlmmembers WHERE userid=$parentid";
        $result1=mysql_query($sql1) or die(mysql_error());
        if($result1){
            $rows1=mysql_fetch_array($result1);
            $currentstage=$rows1['currentstage'];
        }

        if(($numberofchildren==2) && $currentstage=='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);

        }
    }

    function newrookmatrix(){
        //echo "B</br>";
        global $count;
        $sql0="SELECT * FROM mlmmembers";
        $result0=mysql_query($sql0) or die(mysql_error());
        if($result0){
            $i=1;
            while ($rows0=mysql_fetch_array($result0)) {
                # code...
                
                $userid=$rows0['userid'];
                $currentstage=currentstage($userid);
                $k=$count=0;
                $k=countallrook($userid);

                if(($k==14) && ($currentstage=='Rook Star')){
                    $transdescription='Matrix bonus earn From Rook Star to stage 2 - Knight Star' ;                      
                    $creditamount=10;
                    $sql1="INSERT INTO mlmmemberledger(userid, transdate,transdescription,creditamount,type) VALUES ($userid, CURDATE(), '$transdescription',$creditamount,'Matrix Bonus')"; 
                    $result1=mysql_query($sql1) or die(mysql_error());

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

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

                    $parentid=getparentuserid($userid);
                    rookstepout($parentid);
                    $count=0;
                    $k=0;
                }

                $i+=1;
            }
        }
    }


    function newknightmatrix(){
        //echo "p</br>";
        global $count;
        $sql0="SELECT * FROM mlmmembers";
        $result0=mysql_query($sql0) or die(mysql_error());
        if($result0){
            while ($rows0=mysql_fetch_array($result0)) {
                # code...
                $userid=$rows0['userid'];
                $currentstage=currentstage($userid);
                $k=$count=0;
                $k=countallknight($userid);
                if(($k==14) && ($currentstage=='Knight Star')){
                    $transdescription='Matrix bonus earn From Knight Star to stage 3 - Bishop Star' ;                      
                    $creditamount=100;
                    $sql1="INSERT INTO mlmmemberledger(userid, transdate,transdescription,creditamount,type) VALUES ($userid, CURDATE(), '$transdescription',$creditamount,'Matrix Bonus')"; 
                    $result1=mysql_query($sql1) or die(mysql_error());

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

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

                    $parentid=getparentuserid($userid);
                    knightstepout($parentid);
                    $k=$count=0;
                }

            }
        }
    }

    function newbishopmatrix(){
        //echo "S</br>";
        global $count;
        $sql0="SELECT * FROM mlmmembers";
        $result0=mysql_query($sql0) or die(mysql_error());
        if($result0){
            while ($rows0=mysql_fetch_array($result0)) {
                # code...
                $userid=$rows0['userid'];
                $currentstage=currentstage($userid);
                $k=$count=0;
                $k=countallbishop($userid);
                if(($k==14) && ($currentstage=='Bishop Star')){
                    $transdescription='Matrix bonus earn From Bishop Star to stage 4 - King Star' ;                      
                    $creditamount=300;
                    $sql1="INSERT INTO mlmmemberledger(userid, transdate,transdescription,creditamount,type) VALUES ($userid, CURDATE(), '$transdescription',$creditamount,'Matrix Bonus')"; 
                    $result1=mysql_query($sql1) or die(mysql_error());

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

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

                    $parentid=getparentuserid($userid);
                    bishopstepout($parentid);
                    $k=$count=0;
                }

            }
        }
    }


    function newkingmatrix(){
        //echo "M</br>";
        global $count;
        $sql0="SELECT * FROM mlmmembers";
        $result0=mysql_query($sql0) or die(mysql_error());
        if($result0){
            while ($rows0=mysql_fetch_array($result0)) {
                # code...
                $userid=$rows0['userid'];
                $currentstage=currentstage($userid);
                $c=$currentstage;
                //echo getmember($userid) . " - " .  $c .  'K: ';
                $k=$count=0;
                $k=countallking($userid);
                //echo $k . "</br>";
                if(($k==14) && ($currentstage=='King Star')){
                    $transdescription='Matrix bonus earn From King Star to stage 5 - Queen Star' ;                      
                    $creditamount=1500;
                    $sql1="INSERT INTO mlmmemberledger(userid, transdate,transdescription,creditamount,type) VALUES ($userid, CURDATE(), '$transdescription',$creditamount,'Matrix Bonus')"; 
                    $result1=mysql_query($sql1) or die(mysql_error());

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

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

                    $parentid=getparentuserid($userid);
                    kingstepout($parentid);
                    $k=$count=0;
                }
            }
        }
    }

    function newqueenmatrix() {
        //echo "I</br>";
        global $count;
        $sql0="SELECT * FROM mlmmembers";
        $result0=mysql_query($sql0) or die(mysql_error());

        while ($rows0=mysql_fetch_array($result0)) {
            # code...
            $userid=$rows0['userid'];
            $currentstage=currentstage($userid);
            $k=$count=0;
            $k=countallqueen($userid);
            if(($k==14) && ($currentstage=='Queen Star')){
                $transdescription='Matrix bonus earn From Queen Star to stage 6 - VIP' ;                      
                $creditamount=5000;
                $sql1="INSERT INTO mlmmemberledger(userid, transdate,transdescription,creditamount,type) VALUES ($userid, CURDATE(), '$transdescription',$creditamount,'Matrix Bonus')"; 
                $result1=mysql_query($sql1) or die(mysql_error());

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

                //3-update level/
                $result3=mysql_query("UPDATE mlmmembers SET currentstage='Rep', level='Level 6' WHERE userid='$userid'");

                $parentid=getparentuserid($userid);
                queenstepout($parentid);
                $k=$count=0;
            }
        }
    }     

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

    function getfullname($userid){
        $sql="SELECT * FROM mlmmembers WHERE userid=$userid";
        $result=mysql_query($sql) or die(mysql_error());
        $rows=mysql_fetch_array($result);
        return $fullname=$rows['firstname'] . ', ' . $rows['lastname'];
    }
    function get_gravatar( $email, $s = 80, $d = 'mm', $r = 'g', $img = false, $atts = array() ) {
        $url = 'https://www.gravatar.com/avatar/';
        $url .= md5( strtolower( trim( $email ) ) );
        $url .= "?s=$s&d=$d&r=$r";
        if ( $img ) {
            $url = '<img src="' . $url . '"';
            foreach ( $atts as $key => $val )
                $url .= ' ' . $key . '="' . $val . '"';
            $url .= ' />';
        }
        return $url;
    }
    /*function mychildren($id){       

        global $array;
        $sql="SELECT * FROM mlmmembers WHERE parentid=$id ORDER BY userid ASC";
        $result=mysql_query($sql) or die(mysql_error());

        while($rows=mysql_fetch_assoc($result)){
            $array[]=$rows;
            $userid=$rows['userid'];
            mychildren($userid);
        }

        return $array;
    }*/



    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 getprofileid($userid){
        $sql="SELECT * FROM mlmmembers WHERE userid=$userid";
        $result=mysql_query($sql) or die(mysql_error());
        $rows=mysql_fetch_array($result);
        return $profileid =$rows['profileid'];
    }

    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($userid){
		$sql="SELECT * FROM mlmmembers WHERE userid=$userid";
		$result=mysql_query($sql) or die(mysql_error());
		$rows=mysql_fetch_array($result);
		$stage=$rows['currentstage'];
		return $stage;
	}

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


	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 credit, sum(debitamount) as debit FROM mlmmemberledger WHERE userid=$userid";
        $result=mysql_query($sql) or die(mysql_error());
        
        if($result){
            $rows=mysql_fetch_array($result);
            $credit=$rows['credit'];
            $debit=$rows['debit'];
            return number_format(($credit-$debit),2);
        }else{
            return 0;
        }
                
    }

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

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

    function totalsales(){
        $sql="SELECT SUM(creditamount) as balance FROM mlmledger WHERE type='Sales'";
        $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_2($parentid){
        $sql="SELECT * FROM mlmmembers WHERE parentid=$parentid";
        $result=mysql_query($sql) or die(mysql_error());
        
        $numberofchildren=mysql_num_rows($result);
        //$numberofchildren=mysql_num_rows($result);

        $sql1="SELECT * FROM mlmmembers WHERE userid=$parentid";
        $result1=mysql_query($sql1) or die(mysql_error());
        if($result1){
            $rows1=mysql_fetch_array($result1);
            $currentstage=$rows1['currentstage'];
        }

        if(($numberofchildren==2) && $currentstage=='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 ($currentstage=='Rook Star') {
            # code...
            #count the number of children with whose currentstage Pawn

            //$numofrook=countrook($parentid);

            $rook=countrook($parentid);
            $k=$rook[0];
            $memberid=$rook[1];

            if($k==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 ($memberid, 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 ($memberid, 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='$memberid'");

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

            }


        }elseif ($currentstage=="Knight Star") {
            # code...
            //$numofknight=countknight($parentid);
            $rook=countknight($parentid);
            $k=$rook[0];
            $memberid=$rook[1];

            if($k==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 ($memberid, 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 ($memberid, 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='$memberid'");

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

        }elseif ($currentstage=='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 ($currentstage=='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);

            }
        }elseif ($currentstage=='King Star') {
            # code...
            $numofking=countking($parentid);

            if($numofking==14){
                $transdescription='Matrix bonus earn From King Star to stage 5 - Queen Star' ;                      
                $creditamount=1500;
                $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=1500;
                $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/
                $result=mysql_query("UPDATE mlmmembers SET currentstage='Queen Star', level='Level 5' WHERE userid='$parentid'");

                $parentid=getparentuserid($parentid);
                kingstepout($parentid);

            }
        }

    }

    function newmatrixbonus22($parentid){     
        
        //call matrixes function
        pawnmatrix($parentid);
        rookmatrix($parentid);
        knightmatrix($parentid);
        bishopmatrix($parentid);
        kingmatrix($parentid);
        queenmatrix($parentid);

    }

    function pawnmatrix($parentid){
        $sql="SELECT * FROM mlmmembers WHERE parentid=$parentid";
        $result=mysql_query($sql) or die(mysql_error());
        
        $sql1="SELECT * FROM mlmmembers WHERE userid=$parentid";
        $result1=mysql_query($sql1) or die(mysql_error());
        if($result1){
            $rows1=mysql_fetch_array($result1);
            $currentstage=$rows1['currentstage'];
        }
        $numberofchildren=mysql_num_rows($result);

        if(($numberofchildren==2) && ($currentstage=="Pawn")){
            //echo "inside matrix";die();
            $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);

        }
    }

    function rookmatrix($parentid){
        global $count;
        $sql4="SELECT * FROM mlmmembers WHERE userid=$parentid";
        $result4=mysql_query($sql4) or die(mysql_error());

        //echo "Userid: " . $parentid . "No of Rook: " . $countrook2=countrook($parentid) . "</br>";
        $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);
            https://server217.web-hosting.com/cpanel epout($parentid);

        }

        $count=0;

        while($rows4=mysql_fetch_array($result4)){
            unset($count);
            rookmatrix($rows4['parentid']);
        }
    }

    function knightmatrix($parentid){
        $sql1="SELECT * FROM mlmmembers WHERE userid=$parentid";
        $result1=mysql_query($sql1) or die(mysql_error());
        if($result1){
            $rows1=mysql_fetch_array($result1);
            $currentstage=$rows1['currentstage'];
        }        $sql4="SELECT * FROM mlmmembers WHERE userid=$parentid";
        $result4=mysql_query($sql4) or die(mysql_error());

        //echo "Userid: " . $parentid . "No of Rook: " . $countrook2=countknight($parentid) . "</br>";
        $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/
            $result=mysql_query("UPDATE mlmmembers SET currentstage='Bishop Star', level='Level 3' WHERE userid='$parentid'");

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

        }

        $count=0;

        while($rows4=mysql_fetch_array($result4)){
            unset($count);
            knightmatrix($rows4['parentid']);
        }
    }


    function bishopmatrix($parentid){
        global $count;
        $sql4="SELECT * FROM mlmmembers WHERE userid=$parentid";
        $result4=mysql_query($sql4) or die(mysql_error());

        //echo "Userid: " . $parentid . "No of Rook: " . $countrook2=countrook($parentid) . "</br>";
        $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/
            $result=mysql_query("UPDATE mlmmembers SET currentstage='King Star', level='Level 4' WHERE userid='$parentid'");

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

        }

        $count=0;

        while($rows4=mysql_fetch_array($result4)){
            unset($count);
            bishopmatrix($rows4['parentid']);
        }
    }

    function kingmatrix($parentid){
        //global $count;
        $sql4="SELECT * FROM mlmmembers WHERE userid=$parentid";
        $result4=mysql_query($sql4) or die(mysql_error());

        //echo "Userid: " . $parentid . "No of Rook: " . $countrook2=countrook($parentid) . "</br>";
        $numofking=countking($parentid);

        if($numofking==14){
            $transdescription='Matrix bonus earn From King Star to stage 5 - Queen Star' ;                      
            $creditamount=1500;
            $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=1500;
            $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/
            $result=mysql_query("UPDATE mlmmembers SET currentstage='Queen Star', level='Level 5' WHERE userid='$parentid'");

            $parentid=getparentuserid($parentid);
            kingstepout($parentid);

        }

        $count=0;

        while($rows4=mysql_fetch_array($result4)){
            unset($count);
            kingmatrix($rows4['parentid']);
        }
    }    

    function queenmatrix($parentid){
        //global $count;
        $sql4="SELECT * FROM mlmmembers WHERE userid=$parentid";
        $result4=mysql_query($sql4) or die(mysql_error());

        //echo "Userid: " . $parentid . "No of Rook: " . $countrook2=countrook($parentid) . "</br>";
        $numofqueen=countqueen($parentid);

        if($numofqueen==6){
            $transdescription='Matrix bonus earn From Queen Star to stage 6 - V I P' ;                      
            $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/
            $result=mysql_query("UPDATE mlmmembers SET currentstage='Rep', level='Level 6' WHERE userid='$parentid'");

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

        }

        $count=0;

        while($rows4=mysql_fetch_array($result4)){
            unset($count);
            kingmatrix($rows4['parentid']);
        }
    }    

    function countknight2($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 pawnstepout($parentid){
        //global $num;
        //$parentid=$parentid;
        /*$immediateparentid=$parentid;
        echo "Immediate Parent ID: " . $immediateparentid;
        echo "</br> Parent ID: ". $parentid;die();*/
        $sql1="SELECT * FROM mlmmembers WHERE userid=$parentid AND currentstage='Rook Star'";
        $result1=mysql_query($sql1)  or die(mysql_error());

        if(mysql_num_rows($result1)==1){
            if($parentid!=0){
                    $transdescription='StepOut Bonus earn on Rook Member' ;                      
                    $creditamount=5;
                    $sql2="INSERT INTO mlmmemberledger(userid, transdate,transdescription,creditamount,type) VALUES ($parentid, CURDATE(), '$transdescription',$creditamount,'StepOut Bonus')"; 
                    $result2=mysql_query($sql2) or die(mysql_error());

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

    function countallrook($userid){
        global $count;
        $sql="SELECT * FROM mlmmembers WHERE parentid=$userid AND currentstage='Rook Star'";
        $result=mysql_query($sql) or die(mysql_error());

        while($rows=mysql_fetch_array($result)){
            //$rows['currentstage'];
            if($rows['currentstage']='Rook Star'){
                $count+=1;
            }
            countallrook($rows['userid']);
        }

        return $count;
        $count=0;
    }

    function countallpawn($userid){
        global $count;
        $sql="SELECT * FROM mlmmembers WHERE parentid=$userid AND currentstage='Pawn'";
        $result=mysql_query($sql) or die(mysql_error());

        while($rows=mysql_fetch_array($result)){
            //$rows['currentstage'];
            if($rows['currentstage']='Pawn'){
                $count+=1;
            }
            countallpawn($rows['userid']);
        }

        return $count;
        $count=0;
    }
    function countallknight($userid){
        global $count;
        $sql="SELECT * FROM mlmmembers WHERE parentid=$userid AND currentstage='Knight Star'";
        $result=mysql_query($sql) or die(mysql_error());

        while($rows=mysql_fetch_array($result)){
            //$rows['currentstage'];
            if($rows['currentstage']='Knight Star'){
                $count+=1;
            }
            countallknight($rows['userid']);
        }

        return $count;
        $count=0;
    }

    function countallking($userid){
        global $count;
        $sql="SELECT * FROM mlmmembers WHERE parentid=$userid AND currentstage='King Star'";
        $result=mysql_query($sql) or die(mysql_error());

        while($rows=mysql_fetch_array($result)){
            //$rows['currentstage'];
            if($rows['currentstage']='King Star'){
                $count+=1;
            }
            countallking($rows['userid']);
        }

        return $count;
        $count=0;
    }


    function countallbishop($userid){
        global $count;
        $sql="SELECT * FROM mlmmembers WHERE parentid=$userid AND currentstage='Bishop Star'";
        $result=mysql_query($sql) or die(mysql_error());

        while($rows=mysql_fetch_array($result)){
            //$rows['currentstage'];
            if($rows['currentstage']='Bishop Star'){
                $count+=1;
            }
            countallbishop($rows['userid']);
        }

        return $count;
        $count=0;
    }

    function countallqueen($userid){
        global $count;
        $sql="SELECT * FROM mlmmembers WHERE parentid=$userid AND currentstage='Queen Star'";
        $result=mysql_query($sql) or die(mysql_error());

        while($rows=mysql_fetch_array($result)){
            //$rows['currentstage'];
            if($rows['currentstage']='Bishop Star'){
                $count+=1;
            }
            countallqueen($rows['userid']);
        }

        return $count;
        $count=0;
    }


    function countrook($id){

        global $count,$members;
        //$counter=array();
        //$siblinguserid=$id;
        $sqlrook="SELECT * FROM mlmmembers WHERE userid=$id AND currentstage = 'Rook Star'";
        $resultrook=mysql_query($sqlrook) or die(mysql_error());
        $count+=mysql_num_rows($resultrook);

        //echo getmember($id) . ": " . currentstage($id) . "</br>";

        $members[]=$id;
        

        while($rowsrook=mysql_fetch_array($resultrook)){
            $parentuserid=getparentuserid($id);
            $uid=$rowsrook['userid'];
            $id2=findsibling($parentuserid,$uid);
            //echo getmember($id2) . ": " . currentstage($id2) ."</br>";
            $sql2="SELECT * FROM mlmmembers WHERE userid=$id2 AND currentstage = 'Rook Star'";
            $result2=mysql_query($sql2) or die(mysql_error());

            $count+=mysql_num_rows($result2);
            $members[]=$id2;

            $sql3="SELECT * FROM mlmmembers WHERE parentid=$id2";
            $result3=mysql_query($sql3) or die(mysql_error());

            if($result3){
                while($rows3=mysql_fetch_array($result3)){
                    $userid3=$rows3['userid'];
                    //echo getmember($userid3) . ": " . currentstage($userid3) . "</br>";
                    $sql4="SELECT * FROM mlmmembers WHERE userid=$userid3 AND currentstage='Rook Star'";
                    //$currentstage=currentstage($userid3);
                    //if($currentstage='Rook Star'){
                    //    $count++;
                    //}
                    $result4=mysql_query($sql4) or die(mysql_error());

                    if(mysql_num_rows($result4)==1){
                         $count++;
                         $members[]=$userid3;
                    }
                }
            }
        
            
            $id=$rowsrook['parentid'];
            countrook($id);
        }

        sort($members);
        $memberid=$members[1];
        //if($count==14){
            $counter[0]=$count;
            $counter[1]=$memberid;
        //}

        //echo "<pre>";
            //print_r($members);
        //echo "</pre>";
        return $counter;
    }
     function countknight($id){

        global $count,$members;
        //$counter=array();
        //$siblinguserid=$id;
        $sqlrook="SELECT * FROM mlmmembers WHERE userid=$id AND currentstage = 'Knight Star'";
        $resultrook=mysql_query($sqlrook) or die(mysql_error());
        $count+=mysql_num_rows($resultrook);

        //echo getmember($id) . ": " . currentstage($id) . "</br>";

        $members[]=$id;
        

        while($rowsrook=mysql_fetch_array($resultrook)){
            $parentuserid=getparentuserid($id);
            $uid=$rowsrook['userid'];
            $id2=findsibling($parentuserid,$uid);
            //echo getmember($id2) . ": " . currentstage($id2) ."</br>";
            $sql2="SELECT * FROM mlmmembers WHERE userid=$id2 AND currentstage = 'Knight Star'";
            $result2=mysql_query($sql2) or die(mysql_error());

            $count+=mysql_num_rows($result2);
            $members[]=$id2;

            $sql3="SELECT * FROM mlmmembers WHERE parentid=$id2";
            $result3=mysql_query($sql3) or die(mysql_error());

            if($result3){
                while($rows3=mysql_fetch_array($result3)){
                    $userid3=$rows3['userid'];
                    //echo getmember($userid3) . ": " . currentstage($userid3) . "</br>";
                    $sql4="SELECT * FROM mlmmembers WHERE userid=$userid3 AND currentstage='Knight Star'";
                    //$currentstage=currentstage($userid3);
                    //if($currentstage='Rook Star'){
                    //    $count++;
                    //}
                    $result4=mysql_query($sql4) or die(mysql_error());

                    if(mysql_num_rows($result4)==1){
                         $count++;
                         $members[]=$userid3;
                    }
                }
            }
        
            
            $id=$rowsrook['parentid'];
            countknight($id);
        }

        sort($members);
        $memberid=$members[1];
        //if($count==14){
            $counter[0]=$count;
            $counter[1]=$memberid;
        //}

        //echo "<pre>";
            //print_r($members);
        //echo "</pre>";
        return $counter;
    }

    function findsibling($parentid,$siblinguserid){
        $sql1="SELECT * FROM mlmmembers WHERE parentid=$parentid AND userid <> $siblinguserid";
        $result1=mysql_query($sql1) or die(mysql_error());
        if($result1){
            $rows=mysql_fetch_array($result1);
            return $rows['userid'];
        }

    }
    function countqueen($id){

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

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

        return $count;
    }

        
    function rookstepout($parentid){
        //global $num;
        $sql0="SELECT * FROM mlmmembers WHERE userid=$parentid AND currentstage='Knight Star'";
        $result0=mysql_query($sql0)  or die(mysql_error());
        
        if(mysql_num_rows($result0)==1){
            if($parentid!=0){
                $transdescription='StepOut Bonus on Knight Star Member';
                $creditamount=10;
                $sql1="INSERT INTO mlmmemberledger(userid, transdate,transdescription,creditamount,type) VALUES ($parentid, CURDATE(), '$transdescription',$creditamount,'StepOut Bonus')"; 
                $result1=mysql_query($sql1) or die(mysql_error());

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

        while($rows=mysql_fetch_array($result0)){
            $parentid=$rows['parentid'];
            rookstepout($parentid);
        }           
    }

    function knightstepout($parentid){
        //global $num;
        $sql1="SELECT * FROM mlmmembers WHERE userid=$parentid AND currentstage='Bishop Star'";
        $result1=mysql_query($sql1)  or die(mysql_error());
        
        if(mysql_num_rows($result1)==1){
            $transdescription='StepOut Bonus Earn on Bishop Star Member' ;                      
            $creditamount=20;
            $sql2="INSERT INTO mlmmemberledger(userid, transdate,transdescription,creditamount,type) VALUES ($parentid, CURDATE(), '$transdescription',$creditamount,'StepOut Bonus')"; 
            $result2=mysql_query($sql2) or die(mysql_error());

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

    }

    function bishopstepout($parentid){
        //global $num;
        $sql1="SELECT * FROM mlmmembers WHERE userid=$parentid AND currentstage='King Star'";
        $result1=mysql_query($sql1)  or die(mysql_error());
        
        if(mysql_num_rows($result1)==1){
            $transdescription='StepOut Bonus Earn on King Star Member' ;                      
            $creditamount=50;
            $sql2="INSERT INTO mlmmemberledger(userid, transdate,transdescription,creditamount,type) VALUES ($parentid, CURDATE(), '$transdescription',$creditamount,'StepOut Bonus')"; 
            $result2=mysql_query($sql2) or die(mysql_error());

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

    function kingstepout($parentid){
        //global $num;
        $sql1="SELECT * FROM mlmmembers WHERE userid=$parentid='Queen Star'";
        $result1=mysql_query($sql1)  or die(mysql_error());
        
        if(mysql_num_rows($result1)==1){
            $transdescription='StepOut Bonus Earn on Queen Member' ;                      
            $creditamount=300;
            $sql2="INSERT INTO mlmmemberledger(userid, transdate,transdescription,creditamount,type) VALUES ($parentid, CURDATE(), '$transdescription',$creditamount,'StepOut Bonus')"; 
            $result2=mysql_query($sql2) or die(mysql_error());

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

    function queenstepout($parentid){
        //global $num;
        $sql1="SELECT * FROM mlmmembers WHERE userid=$parentid";
        $result1=mysql_query($sql1)  or die(mysql_error());
        
        if(mysql_num_rows($result1)==1){
            $transdescription='StepOut Bonus Earn on Rep Member';                      
            $creditamount=300;
            $sql2="INSERT INTO mlmmemberledger(userid, transdate,transdescription,creditamount,type) VALUES ($parentid, CURDATE(), '$transdescription',$creditamount,'StepOut Bonus')"; 
            $result2=mysql_query($sql2) or die(mysql_error());

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

    function mychildren($id){       

        global $array;
        $sql="SELECT userid, username, profileid, passport, currentstage, l_member, r_member, firstname, lastname, emailaddress, parentid FROM mlmmembers WHERE parentid=$id";
        $result=mysql_query($sql) or die(mysql_error());

        while($rows=mysql_fetch_assoc($result)){
            $array[]=$rows;
            $userid=$rows['userid'];
            mychildren($userid);
        }
        return $array;       
    }

    function mychildren2($id){       

        global $array;
        $sql="SELECT * FROM mlmmembers WHERE userid=$id ORDER BY userid ASC";
        $result=mysql_query($sql) or die(mysql_error());

        while($rows=mysql_fetch_assoc($result)){
            $array[]=$rows;
            $userid=$rows['userid'];
            mychildren2($userid);
        }
        return $array; 
    }

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

        $sql="SELECT * FROM mlmmembers WHERE parentid=$sponsoruserid";

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

        if($count==2){                        
            //while ($rows=mysql_fetch_array($result)) {                    
            //    $profileid=$rows['profileid'];
            //   setparentid_old($profileid,$userid);              
            //}
            $mynewarray=array();
            $mynewarray=mychildren($sponsoruserid);
            foreach ($mynewarray as $key => $row)
            {
                $array_depth[$key] = $row['depth'];
                $array_userid[$key] = $row['userid'];
            }
            array_multisort($array_depth, SORT_ASC, $array_depth, SORT_ASC, $mynewarray); 
            //echo "<pre>";
            //print_r($mynewarray)   ;
            //echo "</pre>";
            foreach ($mynewarray as $key => $row) {
                $count=0;            
                $childuserid=$row['userid'];
                $sql="SELECT * FROM mlmmembers WHERE parentid=$childuserid";
                $result=mysql_query($sql) or die(mysql_error());
                $count=mysql_num_rows($result);
                if($count<2){
                    $sql="UPDATE mlmmembers SET parentid=$childuserid WHERE userid=$newuserid";
                    $rst=mysql_query($sql) or die(mysql_error());
                    $parentid=$childuserid;
                    break;
                }

            }
        }else{
            $parentid=$sponsoruserid;
            $sqlupdate="UPDATE mlmmembers SET parentid='$parentid' WHERE userid=" . $newuserid;

            $rstUpdate=mysql_query($sqlupdate) or die(mysql_error()); 
            $parentid=$sponsoruserid;           
        } 

        //set dept
        $depth=depth($newuserid);
        $sql="UPDATE mlmmembers SET depth=$depth WHERE userid=$newuserid";
        $result=mysql_query($sql) or die(mysql_error());;
        unset($depth);

        //update leg valuesd
        //$parentid=$parentid;
        $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=$newuserid";
        }elseif($count==2){
            $sqlupdate="UPDATE mlmmembers SET r_member=1 WHERE userid=$newuserid";
        }
        
        $rstUpdate=mysql_query($sqlupdate) or die(mysql_error());
             
        $sql20="SELECT parentid FROM mlmmembers WHERE userid=$newuserid";
        $result20=mysql_query($sql20) or die(mysql_error());
        $rows20=mysql_fetch_array($result20);
        return $rows20['parentid']; 
    }

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

        if($result && mysql_num_rows($result)>0){
            
            while($rows=mysql_fetch_array($result)){
                $d++;
                $parentid=$rows['parentid'];
                depth($parentid);
            }
        }

        return $d;      

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

        $sql="SELECT * FROM mlmmembers WHERE parentid=$sponsoruserid";

        $result=mysql_query($sql) or die(mysql_error());
        $count=mysql_num_rows($result);
        //echo "sponsoruserid= " . $sponsoruserid . " and No. of record: " . $count . "<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 valuesd

            //die('Stopped by  user');
        $parentid2=$sponsoruserid;
        $sql="SELECT * FROM mlmmembers WHERE parentid=$parentid2";
        $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
        //return $parentid2;      
        $sql20="SELECT parentid FROM mlmmembers WHERE userid=$userid";
        $result20=mysql_query($sql20) or die(mysql_error());
        $rows20=mysql_fetch_array($result20);
        return $rows20['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 class="collapsed"><?php echo $rows['username'];?>
                            <?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());
        $count=mysql_num_rows($result);
        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 sendmail2 ($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' => '#',
                'port' => '#',
                'auth' => true,
                'username' => '#',
                'password' => '#'
            ));

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

       if (PEAR::isError($mail)) {
            return 0;
            //echo('<p>' . $mail->getMessage() . '</p>');
        } else {
            return 1;
            //echo "<p>" . $statusmsg . "</p>";
        }
    }

    function sendmail ($to,$msg,$subject,$statusmsg='Your message has successfully been sent'){        
        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' => '#',
                'port' => '#',
                'auth' => true,
                'username' => '#',
                'password' => '#'
            ));

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

       if (PEAR::isError($mail)) {
            //return 0;
            echo('<p>' . $mail->getMessage() . '</p>');
        } else {
            //return 1;
            echo "<p>" . $statusmsg . "</p>";
        }
    }

    

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

        if($count<2){
            $string.="<option value='" . $id . "'><" . $id . "</option>";
        }

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

        echo $string;
    }

    function totalmembers(){
        $sql="SELECT * FROM mlmmembers";
        $result=mysql_query($sql) or die(mysql_error());
        return mysql_num_rows(($result));
    }

    function registrationincome (){
        $sql="SELECT SUM(creditamount) as registration FROM mlmledger WHERE type='Registration'";
        $result=mysql_query($sql) or die(mysql_error());
        
        if($result){
            $rows=mysql_fetch_array($result);
            return number_format($rows[0],2);
        }else{
            return 0;
        }
    }

    function unusedepins(){
        $sql="SELECT * FROM mlmepins WHERE status='NOT USED'";
        $result=mysql_query($sql) or die(mysql_error());

        if($result){
            return mysql_num_rows($result);
        }else{
            return 0;
        }
    }

    function matrixbonuspaid(){
        $sql="SELECT SUM(creditamount) as matrixbonuspaid FROM mlmmemberledger WHERE 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 stepoutbonuspaid(){
        $sql="SELECT SUM(creditamount) as stepoutbonuspaid FROM mlmmemberledger WHERE 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 referralbonuspaid(){
        $sql="SELECT SUM(creditamount) as referralbonuspaid FROM mlmmemberledger WHERE 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 welcomebonuspaid(){
        $sql="SELECT SUM(creditamount) as welcomebonuspaid FROM mlmmemberledger WHERE type='Bonus'";
        $result=mysql_query($sql) or die(mysql_error());
        
        if($result){
            $rows=mysql_fetch_array($result);
            return $rows[0];
        }else{
            return 0;
        }
                
    }

    function profit(){

        $sql="SELECT SUM(creditamount) registration FROM mlmledger";
        $result=mysql_query($sql) or die(mysql_error());
        
        if($result){
            $rows=mysql_fetch_array($result);
             $totalincome= $rows[0];
        }else{
            $totalincome= 0;
        }

       $sql2="SELECT SUM(creditamount) credit FROM mlmmemberledger";
        $result2=mysql_query($sql2) or die(mysql_error());
        
        if($result2){
            $rows2=mysql_fetch_array($result2);
            $payout=$rows2[0];
        }else{
            $payout=0;
        }
        $ans=$totalincome-$payout;
       return $ans;
                
    }

    function unreadmails(){
        $sql="SELECT * FROM mlmtickets WHERE status !='Read'";
        $result=mysql_query($sql) or die(mysql_error());

        if($result){
            echo mysql_num_rows($result);
        }
    }

    function newwithdrawals(){
         $sql="SELECT * FROM mlmwithdrawals WHERE status ='PENDING'";
        $result=mysql_query($sql) or die(mysql_error());

        if($result){
            echo mysql_num_rows($result);
        }
    }


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

        $rows=mysql_fetch_assoc($result);
        echo $rows['emailaddress'];
    }
?>