-<?php

include('connectdb.inc.php');

//echo addInsentive('SS124391','0');
/* Insentive function Start*/
function addInsentive($parent,$level,$regID,$fixUserCode){
	global $DB;
    $count = 0;
	//echo $level."</br>";
	 $sql = 'SELECT userCode FROM mx_side'.' WHERE refNo="'.$parent.'"';
     $result = mysql_query($sql);
    while ($row = mysql_fetch_array($result))
    {
           $var = str_repeat(' ',$level).$row['userCode']."\n";
		   $level += 1;
           $count += 1 +display_children($row['userCode'],$level,$regID,$fixUserCode);

    }
	$level += 1;
	if($level > 4){
			 $sqlW = 'SELECT * FROM '.$DB->pre.'wallet WHERE userCode="'.$fixUserCode.'" AND atLevel = "'.$level.'"'; 
			 $W = $DB->dbRow($sqlW);
			 if($W['userCode'] == ''){
				 $sqlL = 'SELECT insentive FROM  '.$DB->pre.'wallet	 WHERE userCode="'.$fixUserCode.'" ORDER BY walletID DESC limit 1';
				 $L = $DB->dbRow($sqlL);
				 $sqlI = 'INSERT INTO '.$DB->pre.'wallet SET regID="'.$regID.'",userCode="'.$fixUserCode.'",insentive="'.($L['insentive']==''?'800':($L['insentive']*2)).'",atLevel="'.$level.'"';
				 $DB->dbQuery($sqlI);
			 }
	}
    //return $count.'------'.$level; // it will return all user_id count under parent_id
}
/* Insentive function done*/
die;
	$sql = 'SELECT * FROM '.$DB->pre.'user_reg WHERE status="1" AND isActivate="1"';
		$U = $DB->dbRows($sql);
		foreach($U as $U){
			 checkBinary($U['userCode'],'0','0',$U['regID'],$U['planID']);
			 //addInsentive($U['userCode'],'0',$U['regID'],$U['userCode']);
			$sqlD = 'SELECT * FROM '.$DB->pre.'daily_payout WHERE payDate="'.date('Y-m-d').'" AND userCode="'.$U['userCode'].'"';
			$D = $DB->dbRow($sqlD);
			if($D['userCode'] != ''){
   			 $sqlW = 'SELECT * FROM '.$DB->pre.'wallet WHERE userCode="'.$D['userCode'].'" AND dateAdded = "'.date('Y-m-d').'"'; 
				$W = $DB->dbRow($sqlW);
				if($W['userCode'] == ''){
					$DB->table = $DB->pre."wallet";	
					$DB->data  = array('regID'=>$D['regID'],'userCode'=>$D['userCode'],'dailyPayout'=>$D['amount'],'dateAdded'=>date('Y-m-d'));
					$DB->dbInsert();
					updateBalance($D);
					
				}
			}
		}
		
		
		
		
		function updateBalance($D){
			global $DB;
			$sqlB = 'SELECT balAmt FROM '.$DB->pre.'balance WHERE balanceID=(SELECT max(balanceID) FROM '.$DB->pre.'balance WHERE regID="'.$D['regID'].'" AND userCode="'.$D['userCode'].'")';
			$B = $DB->dbRow($sqlB);
			$bal = (intval($B['balAmt'])+$D['amount']);
			
			$DB->table = $DB->pre."balance";
			$DB->data  = array('regID'=>$D['regID'],'userCode'=>$D['userCode'],'balAmt'=>$bal,'dateAdded'=>date('Y-m-d H:i:s'));
			$DB->dbInsert();
		}
		
		function updateBinaryBalance($userCode,$regID,$direct,$binary){
			global $DB;
			$sqlB = 'SELECT balAmt FROM '.$DB->pre.'balance WHERE balanceID=(SELECT max(balanceID) FROM '.$DB->pre.'balance WHERE regID="'.$regID.'" AND userCode="'.$userCode.'")';
			$B = $DB->dbRow($sqlB);
			$bal = (intval($B['balAmt'])+$direct+$binary);
			
			$DB->table = $DB->pre."balance";
			$DB->data  = array('regID'=>$regID,'userCode'=>$userCode,'balAmt'=>$bal,'dateAdded'=>date('Y-m-d H:i:s'));
			$DB->dbInsert();
		}
	//	echo checkBinary('SS299011','0','0');
		
		function checkBinary($userCode,$level,$cnt,$regID,$planID){ 
	global $DB;
	$levelTwo = '';
	$levelThird = 0;
	$levelFourth = 0;
	echo $query = 'SELECT userCode FROM '.$DB->pre.'user_reg WHERE status="1" AND isActivate="1" AND referenceNo="'.$userCode.'" AND isActivate="1"';
	$D = $DB->dbRows($query);
	$levelTwo = count($D); // level two value
		if($levelTwo == '2'){	
			$sqlTV = 'SELECT regID FROM '.$DB->pre.'wallet WHERE atLevel="2" AND regID="'.$regID.'"';
			$TV = $DB->dbRow($sqlTV);
			if($TV['regID'] == ''){
				$DB->table = $DB->pre."wallet";	
				$sqlP = 'SELECT * FROM '.$DB->pre.'plan WHERE status="1" AND planID="'.$planID.'"';
				$P = $DB->dbRow($sqlP);
				$twoDirect = (($P['charges']/100)*5);
				$twoBinary = (($P['charges']/100)*10);
				$DB->data  = array('regID'=>$regID,'userCode'=>$userCode,'directBinary'=>$twoDirect,'binary'=>($twoBinary),'atLevel'=>'2','dateAdded'=>date('Y-m-d'));
				$DB->dbInsert();
				updateBinaryBalance($userCode,$regID,$twoDirect,$twoBinary);
			}	
		}
		foreach($D as $D){ //level third start
		if($D['userCode']){
			 $queryT = 'SELECT userCode FROM '.$DB->pre.'user_reg WHERE status="1" AND isActivate="1" AND referenceNo="'.$D['userCode'].'" AND isActivate="1"';
			$T = $DB->dbRows($queryT);
			$levelThird += count($T);	// level third value
			if($levelThird == '2'){	
				$sqlThV = 'SELECT regID FROM '.$DB->pre.'wallet WHERE atLevel="3" AND regID="'.$regID.'"';
				$ThV = $DB->dbRow($sqlThV);
				if($ThV['regID'] == ''){
					$DB->table = $DB->pre."wallet";	
					$sqlP = 'SELECT * FROM '.$DB->pre.'plan WHERE status="1" AND planID="'.$planID.'"';
					$P = $DB->dbRow($sqlP);
					$thBinary = (($P['charges']/100)*10);
					$DB->data  = array('regID'=>$regID,'userCode'=>$userCode,'binary'=>($thBinary),'atLevel'=>'3','dateAdded'=>date('Y-m-d'));
					$DB->dbInsert();
					updateBinaryBalance($userCode,$regID,'0',($thBinary));
				}	
			}
			if($levelThird == '4'){	
				$sqlThV = 'SELECT regID FROM '.$DB->pre.'wallet WHERE atLevel="3" AND regID="'.$regID.'"';
				$DB->dbRow($sqlThV);
				if($DB->numRows == '1'){
					$DB->table = $DB->pre."wallet";	
					$sqlP = 'SELECT * FROM '.$DB->pre.'plan WHERE status="1" AND planID="'.$planID.'"';
					$P = $DB->dbRow($sqlP);
					$thBinary = (($P['charges']/100)*10);
					$DB->data  = array('regID'=>$regID,'userCode'=>$userCode,'binary'=>($thBinary),'atLevel'=>'3','dateAdded'=>date('Y-m-d'));
					$DB->dbInsert();
					updateBinaryBalance($userCode,$regID,'0',($thBinary));
				}
			}
			
			
			
						
			foreach($T as $T){ //level four start
				if($T['userCode']){
					$queryF = 'SELECT userCode FROM '.$DB->pre.'user_reg WHERE status="1" AND isActivate="1" AND referenceNo="'.$T['userCode'].'" AND isActivate="1"';
					$F = $DB->dbRows($queryF);
					$levelFourth += count($F); // level four value
					
					if($levelFourth == '2'){	
						$sqlFV = 'SELECT regID FROM '.$DB->pre.'wallet WHERE atLevel="4" AND regID="'.$regID.'"';
						$FV = $DB->dbRow($sqlFV);
						if($FV['regID'] == ''){
							$DB->table = $DB->pre."wallet";	
							$sqlP = 'SELECT * FROM '.$DB->pre.'plan WHERE status="1" AND planID="'.$planID.'"';
							$P = $DB->dbRow($sqlP);
							$fBinary = (($P['charges']/100)*10);
							$DB->data  = array('regID'=>$regID,'userCode'=>$userCode,'binary'=>($fBinary),'atLevel'=>'4','dateAdded'=>date('Y-m-d'));
							$DB->dbInsert();
							updateBinaryBalance($userCode,$regID,'0',($fBinary));
						}	
					}
					if($levelFourth == '4'){	
						$sqlFV = 'SELECT regID FROM '.$DB->pre.'wallet WHERE atLevel="4" AND regID="'.$regID.'"';
						$DB->dbRow($sqlFV);
						if($DB->numRows == '1'){
							$DB->table = $DB->pre."wallet";	
							$sqlP = 'SELECT * FROM '.$DB->pre.'plan WHERE status="1" AND planID="'.$planID.'"';
							$P = $DB->dbRow($sqlP);
							$fBinary = (($P['charges']/100)*10);
							$DB->data  = array('regID'=>$regID,'userCode'=>$userCode,'binary'=>($fBinary),'atLevel'=>'4','dateAdded'=>date('Y-m-d'));
							$DB->dbInsert();
							updateBinaryBalance($userCode,$regID,'0',($fBinary));
						}	
					}
					
					if($levelFourth == '6'){	
						$sqlFV = 'SELECT regID FROM '.$DB->pre.'wallet WHERE atLevel="4" AND regID="'.$regID.'"';
						$DB->dbRows($sqlFV);
						if($DB->numRows == '2'){
							$DB->table = $DB->pre."wallet";	
							$sqlP = 'SELECT * FROM '.$DB->pre.'plan WHERE status="1" AND planID="'.$planID.'"';
							$P = $DB->dbRow($sqlP);
							$fBinary = (($P['charges']/100)*10);
							$DB->data  = array('regID'=>$regID,'userCode'=>$userCode,'binary'=>($fBinary),'atLevel'=>'4','dateAdded'=>date('Y-m-d'));
							$DB->dbInsert();
							updateBinaryBalance($userCode,$regID,'0',($fBinary));
						}	
					}
					if($levelFourth == '8'){	
						$sqlFV = 'SELECT regID FROM '.$DB->pre.'wallet WHERE atLevel="4" AND regID="'.$regID.'"';
						$DB->dbRows($sqlFV);
						if($DB->numRows == '3'){
							$DB->table = $DB->pre."wallet";	
							$sqlP = 'SELECT * FROM '.$DB->pre.'plan WHERE status="1" AND planID="'.$planID.'"';
							$P = $DB->dbRow($sqlP);
							$fBinary = (($P['charges']/100)*10);
							$DB->data  = array('regID'=>$regID,'userCode'=>$userCode,'binary'=>($fBinary),'atLevel'=>'4','dateAdded'=>date('Y-m-d'));
							$DB->dbInsert();
							updateBinaryBalance($userCode,$regID,'0',($fBinary));
						}	
					}
					
					
					
				}
			}
		}

	}
	
   
    //return $levelTwo.'---'.$levelThird.'-----'.$levelFourth; 
}






?>
