<?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;
                } 
                
                $stmt=$conn->prepare("select  *   from  UI__DChstr where typ=1  and status=0 ");   
                $stmt->execute();  
                $UI__DailyROIGeneCt = $stmt->fetchAll(PDO::FETCH_OBJ);   
                  
                foreach($UI__DailyROIGeneCt as $dmt)
                {    
                    $originalDate = $dmt->Tdate;
                    $OldD = date("d-m-Y", strtotime($originalDate)); 
                    
                    $newDate = date("d-m-Y");
                    $date1 = new DateTime($OldD);
                    $date2 = new DateTime($newDate);
                    $interval = $date1->diff($date2); 
                     
                    if( (date("D")!="Sat")  && (date("D")!="Sun") )
                    {
                        if($interval->d <=200)
                        {
                                $amount = ($dmt->amt*1)/100; 
                                $stmt=$conn->prepare("insert into UI__DailyROIGene(custId,amount,Tdate) values(:custId,:amount,:Tdate)"); 
                                $stmt->bindValue(':custId', $dmt->Rid , PDO::PARAM_INT);   
                                $stmt->bindValue(':amount', $amount , PDO::PARAM_INT);   
                                $stmt->bindValue(':Tdate', date("d-M-Y"), PDO::PARAM_INT);   
                                $stmt->execute();   
                                
                                 
                                
                                // start
                                
                                    
                    				$_POST['cuurentId'] = $dmt->Rid;		
                    				$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)
                                    {     
                                        $ard = null;
                                        $ard = [];
                    					$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;
                                            }
                                        }  
                                    }  
                                     
                    				echo $_POST['cuurentId'].'<bR>';
                    				
                    				if(end($ard)==0)
                                     {  
                                        for($j1=0;$j1<(count($ard)-1);$j1++)
                                        {
                                            if($j1==0)
                                            {      
                                                 
                                                $stmt=$conn->prepare("select id,amt  from UI__DChstr where Rid=:Rid and typ=1 ");   
                                                $stmt->bindValue(':Rid', $_POST['cuurentId'] , PDO::PARAM_STR);   
                                                $stmt->execute();     
                                                $custIdResult = $stmt->fetch(PDO::FETCH_OBJ);     
                                                
                                                $CoinValT = $amount;//($custIdResult->amt*1)/100;  
                                                $CoinVal = ($CoinValT*20)/100;  
                                                
                                                $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', $custIdResult->amt  , 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 custId   from UI__Rgst where cuurentId =:cuurentId ");   
                                                $stmt->bindValue(':cuurentId',  $ard[$j1]  , PDO::PARAM_INT);  
                                                $stmt->execute(); 
                                                $Result1 = $stmt->fetch(PDO::FETCH_OBJ);  
                                                   
                                                $stmt=$conn->prepare("select cuurentId from UI__Rgst where custId =:custId ");   
                                                $stmt->bindValue(':custId', $Result1->custId   , PDO::PARAM_STR);  
                                                $stmt->execute(); 
                                                $Result = $stmt->fetch(PDO::FETCH_OBJ);
                                                
                                                  
                                                $myRefId = $Result->cuurentId;   
                                                $stmt=$conn->prepare("select cuurentId   from UI__Rgst where refId =:cuurentId ");   
                                                $stmt->bindValue(':cuurentId', $Result->cuurentId  , PDO::PARAM_INT);  
                                                $stmt->execute();
                                                
                                                $myTT = 0;
                                                if($stmt->rowCount()>0)
                                                { 
                                                    $Result = $stmt->fetchAll(PDO::FETCH_OBJ);     
                                                    foreach($Result as $dtm) 
                                                        $stepCt['step1']['id'][] = $dtm->cuurentId;
                                                        
                                                        
                                                    $myTT +=count($stepCt['step1']['id']);       
                                                    foreach($Result as $dtm) 
                                                        $stepCt['step2']['id'][] =  Refcall($dtm->cuurentId); 
                                                    
                                                } 
                                                
                                                $NBusinessAmt = 0; 
                                                foreach($stepCt['step2']['id'] as $dtm) 
                                                {         
                                                    foreach($dtm as $dtmx) 
                                                    {    
                                                        $stmt=$conn->prepare("select *  from UI__DChstr where Rid=:Rid and typ=1");   
                                                        $stmt->bindValue(':Rid', $dtmx->cuurentId , PDO::PARAM_STR);  
                                                        $stmt->execute(); 
                                                        $Resultx = $stmt->fetch(PDO::FETCH_OBJ);  
                                                        $NBusinessAmt +=$Resultx->amt; 
                                                    } 
                                                }
                                                
                                                    if($NBusinessAmt>=300000) 
                                                    {    
                                                        $stmt=$conn->prepare("select  * from  UI__Bns where Rid=:Rid and level=2  ");   
                                                        $stmt->bindValue(':Rid',  $ard[$j1] , PDO::PARAM_STR);   
                                                        $stmt->execute();  
                                                        if($stmt->rowCount()<1)
                                                        {     
                                                            
                                                            $CoinValT = ($NBusinessAmt*1)/100;  
                                                            $CoinVal = ($CoinValT*10)/100;  
                    
                                                            $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', $data['Data']  , 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
                                                        {    
                                                             
                                                            $CoinValT = ($amount*1)/100;  
                                                            $CoinVal = ($CoinValT*10)/100;  
                                                            
                                                            $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', $data['Data']  , 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();        
                                                        }     
                                                    }
                    					        } 
                                            }
                                     } 
                                // end   
                        }
                        else
                        {     
                            $stmt=$conn->prepare("update UI__DChstr set status=1 where id=:id"); 
                            $stmt->bindValue(':custId', $dmt->id , PDO::PARAM_STR);    
                            //$stmt->execute();   
                        }
                    }  
                }    
                die;