<?php
	include_once('../config.php');    
      
                	 	
                	
                function Refcall($ref)
                {
                    global $conn;
                    $stmt=$conn->prepare("select cuurentId   from UI__Rgst where refId =:cuurentId ");   
                    $stmt->bindValue(':cuurentId', $ref , PDO::PARAM_INT);  
                    $stmt->execute(); 
                    $Result = $stmt->fetchAll(PDO::FETCH_OBJ);   
                    return $Result;
                }          
			
				
                    die;
                
                $_POST['cuurentId']  = "EJN225271";
                /*$amount = ($dmmt->amt*0.25)/100;  
                $stmt=$conn->prepare("insert into UI__DailyROIGene(custId,amount,Tdate) values(:custId,:amount,:Tdate)"); 
                $stmt->bindValue(':custId', $dmmt->Rid , PDO::PARAM_STR);   
                $stmt->bindValue(':amount', $amount , PDO::PARAM_STR);   
                $stmt->bindValue(':Tdate',  date("d-M-Y", strtotime('-1 day')) , PDO::PARAM_STR);   
                //$stmt->execute();
                */
                
                $stmt=$conn->prepare("select refId,custId   from UI__Rgst where cuurentId =:cuurentId ");   
                $stmt->bindValue(':cuurentId', $_POST['cuurentId'] , PDO::PARAM_INT);  
                $stmt->execute(); 
                 
                 
                if($stmt->rowCount() > 0)
                {     
                    $Result = $stmt->fetch(PDO::FETCH_OBJ);   
                    $CC = $Result->refId;
                    $MyCust =  $Result->custId;
                    $ard[] = $CC;
                    for($i=0;$i<=9;$i++)
                    {
                        $stmt=$conn->prepare("select refId from UI__Rgst where cuurentId =:cuurentId ");   
                        $stmt->bindValue(':cuurentId',$CC, PDO::PARAM_INT);  
                        $stmt->execute();  
                        if($stmt->rowCount() > 0)
                        { 
                            $Result = $stmt->fetch(PDO::FETCH_OBJ);   
                            $CC = $Result->refId;
                            $ard[] = $CC;
                        }
                    }  
                }   

                
				 if(end($ard)==0)
                 {  
                    for($j1=0;$j1<(count($ard)-1);$j1++)
                    {
                        if($j1==0)
                        {    
                             
                            $stmt=$conn->prepare("select id from UI__DChstr where Rid=:Rid and typ=1 ");   
                            $stmt->bindValue(':Rid',  $ard[$j1]  , PDO::PARAM_STR);  
                            $stmt->execute(); 
                            $Result1 = $stmt->fetch(PDO::FETCH_OBJ);   
                            
                            if($stmt->rowCount() > 0) 
                            {    
                                $stmt=$conn->prepare("select  id,cuurentId from UI__Rgst where refId=:custId ");   
                                $stmt->bindValue(':custId', $ard[$j1]  , PDO::PARAM_STR);  
                                $stmt->execute();  
                                $mtCt = 0;
                                if($stmt->rowCount() >=2 )
                                {
                                    $Result = $stmt->fetchAll(PDO::FETCH_OBJ);  
                                    foreach($Result as $dmtt)
                                    {
                                        $stmt=$conn->prepare("select id from UI__DChstr where Rid=:Rid and typ=1 ");   
                                        $stmt->bindValue(':Rid',  $dmtt->cuurentId  , PDO::PARAM_STR);  
                                        $stmt->execute();  
                                        if($stmt->rowCount() > 0)
                                            $mtCt++;
                                    } 
                                }   
                                
                                if($mtCt >= 2) 
    							{    
    							    $CoinVal = 4;
    								$stmt=$conn->prepare("insert into  UI__Bns (Rid,Brid,Tdate,Tamt,BAmt,level) values(:Rid,:Brid,:Tdate,:Tamt,:BAmt,:level) ");   
    								$stmt->bindValue(':Rid', $ard[$j1] , PDO::PARAM_STR); 
    								$stmt->bindValue(':Brid', $_POST['cuurentId'], PDO::PARAM_STR); 
    								$stmt->bindValue(':Tdate', date("d-M-Y") , PDO::PARAM_STR); 
    								$stmt->bindValue(':Tamt', $CoinVal , PDO::PARAM_STR);   
    								$stmt->bindValue(':BAmt',  399  , PDO::PARAM_STR);      
    								$stmt->bindValue(':level', 1 , PDO::PARAM_STR);        
    								$stmt->execute();   
    								
    								$stmt=$conn->prepare("select  * from  UI__Rgst where cuurentId=:cuurentId  ");   
    								$stmt->bindValue(':cuurentId',  $ard[$j1] , PDO::PARAM_STR);   
    								$stmt->execute();  
    								$custId = $stmt->fetch(PDO::FETCH_OBJ);
    								
    								$stmt=$conn->prepare("update  UI__Mnt set Mnnnt = Mnnnt + $CoinVal  where sId=:sId");    
    								$stmt->bindValue(':sId', $custId->custId, PDO::PARAM_STR);  
    								$stmt->execute();     
    							}   
                            } 
                        }
                        else if($j1==1)
                        {  
                            $stmt=$conn->prepare("select id from UI__DChstr where Rid=:Rid and typ=1 ");   
                            $stmt->bindValue(':Rid',  $ard[$j1]  , PDO::PARAM_STR);  
                            $stmt->execute(); 
                            $Result1 = $stmt->fetch(PDO::FETCH_OBJ);   
                            
                             
                            if($stmt->rowCount() > 0) 
                            {    
                                $stmt=$conn->prepare("select  id,cuurentId from UI__Rgst where refId=:custId ");   
                                $stmt->bindValue(':custId', $ard[$j1]  , PDO::PARAM_STR);  
                                $stmt->execute(); 
                                
                                $mtCt = 0;
                                if($stmt->rowCount() >=4 )
                                {
                                    
                                    $Result = $stmt->fetchAll(PDO::FETCH_OBJ);  
                                    foreach($Result as $dmtt)
                                    {
                                        $stmt=$conn->prepare("select id from UI__DChstr where Rid=:Rid and typ=1 ");   
                                        $stmt->bindValue(':Rid',  $dmtt->cuurentId  , PDO::PARAM_STR);  
                                        $stmt->execute();  
                                        if($stmt->rowCount() > 0)
                                            $mtCt++;
                                    } 
                                }   
                                
                                if($mtCt >= 4)  
    							{    
    							    $CoinVal = 3;
    								$stmt=$conn->prepare("insert into  UI__Bns (Rid,Brid,Tdate,Tamt,BAmt,level) values(:Rid,:Brid,:Tdate,:Tamt,:BAmt,:level) ");   
    								$stmt->bindValue(':Rid', $ard[$j1] , PDO::PARAM_STR); 
    								$stmt->bindValue(':Brid', $_POST['cuurentId'], PDO::PARAM_STR); 
    								$stmt->bindValue(':Tdate', date("d-M-Y") , PDO::PARAM_STR); 
    								$stmt->bindValue(':Tamt', $CoinVal , PDO::PARAM_STR);   
    								$stmt->bindValue(':BAmt',  399  , PDO::PARAM_STR);      
    								$stmt->bindValue(':level', 2 , PDO::PARAM_STR);        
    								$stmt->execute();   
    								
    								$stmt=$conn->prepare("select  * from  UI__Rgst where cuurentId=:cuurentId  ");   
    								$stmt->bindValue(':cuurentId',  $ard[$j1] , PDO::PARAM_STR);   
    								$stmt->execute();  
    								$custId = $stmt->fetch(PDO::FETCH_OBJ);
    								
    								$stmt=$conn->prepare("update  UI__Mnt set Mnnnt = Mnnnt + $CoinVal  where sId=:sId");    
    								$stmt->bindValue(':sId', $custId->custId, PDO::PARAM_STR);  
    								$stmt->execute();     
    							 }   
                              }   
                          }else if($j1==2)
                        { 
                         
                            $stmt=$conn->prepare("select id from UI__DChstr where Rid=:Rid and typ=1 ");   
                            $stmt->bindValue(':Rid',  $ard[$j1]  , PDO::PARAM_STR);  
                            $stmt->execute(); 
                            $Result1 = $stmt->fetch(PDO::FETCH_OBJ);   
                            
                            if($stmt->rowCount() > 0) 
                            {    
                                $stmt=$conn->prepare("select  id,cuurentId from UI__Rgst where refId=:custId ");   
                                $stmt->bindValue(':custId', $ard[$j1]  , PDO::PARAM_STR);  
                                $stmt->execute();  
                                
                                $mtCt = 0;
                                if($stmt->rowCount() >=6 )
                                {
                                    $Result = $stmt->fetchAll(PDO::FETCH_OBJ);  
                                    foreach($Result as $dmtt)
                                    {
                                        $stmt=$conn->prepare("select id from UI__DChstr where Rid=:Rid and typ=1 ");   
                                        $stmt->bindValue(':Rid',  $dmtt->cuurentId  , PDO::PARAM_STR);  
                                        $stmt->execute();  
                                        if($stmt->rowCount() > 0)
                                            $mtCt++;
                                    } 
                                }   
                                
                                if($mtCt >= 6) 
    							{    
    							    $CoinVal = 3;
    								$stmt=$conn->prepare("insert into  UI__Bns (Rid,Brid,Tdate,Tamt,BAmt,level) values(:Rid,:Brid,:Tdate,:Tamt,:BAmt,:level) ");   
    								$stmt->bindValue(':Rid', $ard[$j1] , PDO::PARAM_STR); 
    								$stmt->bindValue(':Brid', $_POST['cuurentId'], PDO::PARAM_STR); 
    								$stmt->bindValue(':Tdate', date("d-M-Y") , PDO::PARAM_STR); 
    								$stmt->bindValue(':Tamt', $CoinVal , PDO::PARAM_STR);   
    								$stmt->bindValue(':BAmt',  399  , PDO::PARAM_STR);      
    								$stmt->bindValue(':level', 3 , PDO::PARAM_STR);        
    								$stmt->execute();   
    								
    								$stmt=$conn->prepare("select  * from  UI__Rgst where cuurentId=:cuurentId  ");   
    								$stmt->bindValue(':cuurentId',  $ard[$j1] , PDO::PARAM_STR);   
    								$stmt->execute();  
    								$custId = $stmt->fetch(PDO::FETCH_OBJ);
    								
    								$stmt=$conn->prepare("update  UI__Mnt set Mnnnt = Mnnnt + $CoinVal  where sId=:sId");    
    								$stmt->bindValue(':sId', $custId->custId, PDO::PARAM_STR);  
    								$stmt->execute();     
    							 }   
                              }   
                          }else if($j1==3)
                        { 
                         
                            $stmt=$conn->prepare("select id from UI__DChstr where Rid=:Rid and typ=1 ");   
                            $stmt->bindValue(':Rid',  $ard[$j1]  , PDO::PARAM_STR);  
                            $stmt->execute(); 
                            $Result1 = $stmt->fetch(PDO::FETCH_OBJ);   
                            
                            if($stmt->rowCount() > 0) 
                            {    
                                $stmt=$conn->prepare("select  id,cuurentId from UI__Rgst where refId=:custId ");   
                                $stmt->bindValue(':custId', $ard[$j1]  , PDO::PARAM_STR);  
                                $stmt->execute(); 
                                
                                $mtCt = 0;
                                if($stmt->rowCount() >=8 )
                                {
                                    $Result = $stmt->fetchAll(PDO::FETCH_OBJ);  
                                    foreach($Result as $dmtt)
                                    {
                                        $stmt=$conn->prepare("select id from UI__DChstr where Rid=:Rid and typ=1 ");   
                                        $stmt->bindValue(':Rid',  $dmtt->cuurentId  , PDO::PARAM_STR);  
                                        $stmt->execute();  
                                        if($stmt->rowCount() > 0)
                                            $mtCt++;
                                    } 
                                }   
                                
                                if($mtCt >= 8)  
    							{    
    							    $CoinVal = 2;
    								$stmt=$conn->prepare("insert into  UI__Bns (Rid,Brid,Tdate,Tamt,BAmt,level) values(:Rid,:Brid,:Tdate,:Tamt,:BAmt,:level) ");   
    								$stmt->bindValue(':Rid', $ard[$j1] , PDO::PARAM_STR); 
    								$stmt->bindValue(':Brid', $_POST['cuurentId'], PDO::PARAM_STR); 
    								$stmt->bindValue(':Tdate', date("d-M-Y") , PDO::PARAM_STR); 
    								$stmt->bindValue(':Tamt', $CoinVal , PDO::PARAM_STR);   
    								$stmt->bindValue(':BAmt',  399  , PDO::PARAM_STR);      
    								$stmt->bindValue(':level', 4 , PDO::PARAM_STR);        
    								$stmt->execute();   
    								
    								$stmt=$conn->prepare("select  * from  UI__Rgst where cuurentId=:cuurentId  ");   
    								$stmt->bindValue(':cuurentId',  $ard[$j1] , PDO::PARAM_STR);   
    								$stmt->execute();  
    								$custId = $stmt->fetch(PDO::FETCH_OBJ);
    								
    								$stmt=$conn->prepare("update  UI__Mnt set Mnnnt = Mnnnt + $CoinVal  where sId=:sId");    
    								$stmt->bindValue(':sId', $custId->custId, PDO::PARAM_STR);  
    								$stmt->execute();     
    							 }   
                              }   
                          }else if($j1==4)
                        { 
                         
                            $stmt=$conn->prepare("select id from UI__DChstr where Rid=:Rid and typ=1 ");   
                            $stmt->bindValue(':Rid',  $ard[$j1]  , PDO::PARAM_STR);  
                            $stmt->execute(); 
                            $Result1 = $stmt->fetch(PDO::FETCH_OBJ);   
                            
                            if($stmt->rowCount() > 0) 
                            {    
                                $stmt=$conn->prepare("select  id,cuurentId from UI__Rgst where refId=:custId ");   
                                $stmt->bindValue(':custId', $ard[$j1]  , PDO::PARAM_STR);  
                                $stmt->execute(); 
                                
                                $mtCt = 0;
                                if($stmt->rowCount() >=10 )
                                {
                                    $Result = $stmt->fetchAll(PDO::FETCH_OBJ);  
                                    foreach($Result as $dmtt)
                                    {
                                        $stmt=$conn->prepare("select id from UI__DChstr where Rid=:Rid and typ=1 ");   
                                        $stmt->bindValue(':Rid',  $dmtt->cuurentId  , PDO::PARAM_STR);  
                                        $stmt->execute();  
                                        if($stmt->rowCount() > 0)
                                            $mtCt++;
                                    } 
                                }   
                                
                                if($mtCt >= 10)  
    							{    
    							    $CoinVal = 2;
    								$stmt=$conn->prepare("insert into  UI__Bns (Rid,Brid,Tdate,Tamt,BAmt,level) values(:Rid,:Brid,:Tdate,:Tamt,:BAmt,:level) ");   
    								$stmt->bindValue(':Rid', $ard[$j1] , PDO::PARAM_STR); 
    								$stmt->bindValue(':Brid', $_POST['cuurentId'], PDO::PARAM_STR); 
    								$stmt->bindValue(':Tdate', date("d-M-Y") , PDO::PARAM_STR); 
    								$stmt->bindValue(':Tamt', $CoinVal , PDO::PARAM_STR);   
    								$stmt->bindValue(':BAmt',  399  , PDO::PARAM_STR);      
    								$stmt->bindValue(':level', 5 , PDO::PARAM_STR);        
    								$stmt->execute();   
    								
    								$stmt=$conn->prepare("select  * from  UI__Rgst where cuurentId=:cuurentId  ");   
    								$stmt->bindValue(':cuurentId',  $ard[$j1] , PDO::PARAM_STR);   
    								$stmt->execute();  
    								$custId = $stmt->fetch(PDO::FETCH_OBJ);
    								
    								$stmt=$conn->prepare("update  UI__Mnt set Mnnnt = Mnnnt + $CoinVal  where sId=:sId");    
    								$stmt->bindValue(':sId', $custId->custId, PDO::PARAM_STR);  
    								$stmt->execute();     
    							 }   
                              }   
                          }else if($j1==5)
                        { 
                         
                            $stmt=$conn->prepare("select id from UI__DChstr where Rid=:Rid and typ=1 ");   
                            $stmt->bindValue(':Rid',  $ard[$j1]  , PDO::PARAM_STR);  
                            $stmt->execute(); 
                            $Result1 = $stmt->fetch(PDO::FETCH_OBJ);   
                            
                            if($stmt->rowCount() > 0) 
                            {    
                                $stmt=$conn->prepare("select  id,cuurentId from UI__Rgst where refId=:custId ");   
                                $stmt->bindValue(':custId', $ard[$j1]  , PDO::PARAM_STR);  
                                $stmt->execute(); 
                                
                                $mtCt = 0;
                                if($stmt->rowCount() >=12 )
                                {
                                    $Result = $stmt->fetchAll(PDO::FETCH_OBJ);  
                                    foreach($Result as $dmtt)
                                    {
                                        $stmt=$conn->prepare("select id from UI__DChstr where Rid=:Rid and typ=1 ");   
                                        $stmt->bindValue(':Rid',  $dmtt->cuurentId  , PDO::PARAM_STR);  
                                        $stmt->execute();  
                                        if($stmt->rowCount() > 0)
                                            $mtCt++;
                                    } 
                                }   
                                
                                if($mtCt >=12)  
    							{    
    							    $CoinVal = 1;
    								$stmt=$conn->prepare("insert into  UI__Bns (Rid,Brid,Tdate,Tamt,BAmt,level) values(:Rid,:Brid,:Tdate,:Tamt,:BAmt,:level) ");   
    								$stmt->bindValue(':Rid', $ard[$j1] , PDO::PARAM_STR); 
    								$stmt->bindValue(':Brid', $_POST['cuurentId'], PDO::PARAM_STR); 
    								$stmt->bindValue(':Tdate', date("d-M-Y") , PDO::PARAM_STR); 
    								$stmt->bindValue(':Tamt', $CoinVal , PDO::PARAM_STR);   
    								$stmt->bindValue(':BAmt',  399  , PDO::PARAM_STR);      
    								$stmt->bindValue(':level', 6 , PDO::PARAM_STR);        
    								$stmt->execute();   
    								
    								$stmt=$conn->prepare("select  * from  UI__Rgst where cuurentId=:cuurentId  ");   
    								$stmt->bindValue(':cuurentId',  $ard[$j1] , PDO::PARAM_STR);   
    								$stmt->execute();  
    								$custId = $stmt->fetch(PDO::FETCH_OBJ);
    								
    								$stmt=$conn->prepare("update  UI__Mnt set Mnnnt = Mnnnt + $CoinVal  where sId=:sId");    
    								$stmt->bindValue(':sId', $custId->custId, PDO::PARAM_STR);  
    								$stmt->execute();     
    							 }   
                              }   
                          } 
                        
                       } 
                    }	     
                      