<?php
	include_once('../config.php');    
                        
                          
                         
                        $Job2_ncode2 =  json_encode(json_decode(file_get_contents( 'php://input' ), true ));      
                        $stmt=$conn->prepare("insert into UI__CallBack(calBack,Tdate) values(:calBack,:Tdate)"); 
                        $stmt->bindValue(':calBack', $Job2_ncode2 , PDO::PARAM_INT);    
                        $stmt->bindValue(':Tdate',  date("d-M-Y H:i") , PDO::PARAM_STR);     
                        $stmt->execute();    

                        //$Job2_ncode2 = '{"amount":"120.00","customer_virtual_address":"9922758754@ybl","customer_account_name":"SANGITA VIJAY RATHOD"}'; 
                        $reponse = json_decode($Job2_ncode2);    
                        $stmt=$conn->prepare("insert into UI__UTRVefry(UTR,Amt,Tdate,UPI,nm) values(:UTR,:Amt,:Tdate,:UPI,:nm)"); 
                        $stmt->bindValue(':UTR', $reponse->UTR , PDO::PARAM_STR);    
                        $stmt->bindValue(':Amt', $reponse->amount , PDO::PARAM_STR);    
                        $stmt->bindValue(':Tdate', date("d-M-Y H:i") , PDO::PARAM_STR);    
                        $stmt->bindValue(':UPI', $reponse->customer_virtual_address , PDO::PARAM_STR);  
                        $stmt->bindValue(':nm', $reponse->customer_account_name , PDO::PARAM_STR);  
                        $stmt->execute();  
                        
                        $stmt=$conn->prepare("insert into  UI__UTRRec(UTR,Amt,Tdate,UPI,nm) values(:UTR,:Amt,:Tdate,:UPI,:nm)"); 
                        $stmt->bindValue(':UTR', $reponse->UTR , PDO::PARAM_STR);    
                        $stmt->bindValue(':Amt', $reponse->amount , PDO::PARAM_STR);    
                        $stmt->bindValue(':Tdate', date("d-M-Y H:i") , PDO::PARAM_STR);    
                        $stmt->bindValue(':UPI', $reponse->customer_virtual_address , PDO::PARAM_STR);  
                        $stmt->bindValue(':nm', $reponse->customer_account_name , PDO::PARAM_STR);  
                        $stmt->execute();    
                        die; 
                        
                        
                        $stmt=$conn->prepare("select  * from  UI__Rgst where (UPI=:UPI || UPI2=:UPI2 || UPI3=:UPI3|| UPI4=:UPI4|| UPI5=:UPI5)");   
                        $stmt->bindValue(':UPI', $reponse->customer_virtual_address , PDO::PARAM_STR);   
                        $stmt->bindValue(':UPI2', $reponse->customer_virtual_address , PDO::PARAM_STR);  
                        $stmt->bindValue(':UPI3', $reponse->customer_virtual_address , PDO::PARAM_STR);  
                        $stmt->bindValue(':UPI4', $reponse->customer_virtual_address , PDO::PARAM_STR);  
                        $stmt->bindValue(':UPI5', $reponse->customer_virtual_address , PDO::PARAM_STR);  
                        $stmt->execute();      
                        
                        //echo $stmt->rowCount();die;
                        if($stmt->rowCount()>0)   
                        {     
                            $custId = $stmt->fetch(PDO::FETCH_OBJ); 
                            $_POST['fund'] = $reponse->amount; 
                            $_POST['userid'] = trim($custId->cuurentId,""); 
                            $_SESSION['userId'] = $_POST['userid'];
                             
                            $stmt=$conn->prepare("select  cuurentId from   UI__Rgst   where cuurentId=:cuurentId");  
                            $stmt->bindValue(':cuurentId', $_POST['userid'] , PDO::PARAM_STR);
                            $stmt->execute();    
                            if($stmt->rowCount() > 0)
                            {    
                                    $stmt=$conn->prepare("select  amt,availableFund from  UI__Fundtransfer   where userid=:cuurentId order by id desc");  
                                    $stmt->bindValue(':cuurentId', $_POST['userid'] , PDO::PARAM_STR);
                                    $stmt->execute();  
                                    
                                    if($stmt->rowCount() > 0)
                                    {
                                    $AllUsers = $stmt->fetch(PDO::FETCH_OBJ); 
                                    $totalAvaib = $AllUsers->availableFund+$_POST['fund'];  
                                    }else
                                    $totalAvaib =  $_POST['fund'];   
                                    
                                    $stmt=$conn->prepare("insert into  UI__Fundtransfer (userid,Tdate,amt,availableFund) values(:userid,:Tdate,:amt,:availableFund)    "); 
                                    $stmt->bindValue(':userid',$_POST['userid'], PDO::PARAM_STR); 
                                    $stmt->bindValue(':Tdate',date("d-M-Y H:i"), PDO::PARAM_STR); 
                                    $stmt->bindValue(':amt',$_POST['fund'], PDO::PARAM_STR);  
                                    $stmt->bindValue(':availableFund', $totalAvaib , PDO::PARAM_STR);  
                                    $stmt->execute();  
                                    
                                    // top up start     
                                        
                                        $_POST['AMMT'] = $_POST['fund'];
                            			$_POST['cuurentId'] = $_POST['userid'];
                                        $stmt=$conn->prepare("select  custId   from UI__Rgst where cuurentId =:cuurentId ");   
                                        $stmt->bindValue(':cuurentId', $_POST['cuurentId'] , PDO::PARAM_INT);  
                                        $stmt->execute(); 
                                        $FResult = $stmt->fetch(PDO::FETCH_OBJ);  
                                        
                                        $stmt=$conn->prepare("select id from  UI__DChstr where typ=1 and sId=:sId ");    
                                        $stmt->bindValue(':sId', $FResult->custId , PDO::PARAM_STR);  
                                        $stmt->execute(); 
                                        $affected_rows_UI__DChstr = $stmt->rowCount();	    
                                     
                                         
                                        
                                        
                            		    $data['Data'] =  $_POST['AMMT'];                 
                                        $stmt=$conn->prepare("select  availableFund  from  UI__Fundtransfer   where userid=:cuurentId order by id desc");  
                                        $stmt->bindValue(':cuurentId', $_SESSION['userId']  , PDO::PARAM_STR);
                                        $stmt->execute();  
                                        $AllUsers = $stmt->fetch(PDO::FETCH_OBJ); 
                                        $availableFund = 0;
                                         
                                        if($AllUsers->availableFund>=$data['Data']) 
                                        {
                                            $availableFund = 1;    
                                            $totalAvaib = $AllUsers->availableFund;
                                        }    
                                             
                                       
                                        if( ($availableFund > 0)  && ($data['Data']>=100))
                                        {    
                                            
                                            
                                            $Result = $stmt->fetch(PDO::FETCH_OBJ);    
                                            $stmt=$conn->prepare("update  UI__PnMX set exp=1 where id=:id ");    
                                            $stmt->bindValue(':id', $Result->id , PDO::PARAM_STR);  
                                           // $stmt->execute(); 
                                                 
                                            $PlanAMt = $data['Data'] ;    
                                            $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;
                                                    }
                                                }  
                                            }   
                                               
                                            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;
                                            }          
                            				 
                                            $totalAvaib -= $data['Data'];  
                                            $stmt=$conn->prepare("insert into  UI__Fundtransfer (userid,Tdate,amt,availableFund,topUpId,typ) values(:userid,:Tdate,:amt,:availableFund,:topUpId,1)    "); 
                                            $stmt->bindValue(':userid', $_SESSION['userId'] , PDO::PARAM_STR); 
                                            $stmt->bindValue(':Tdate',date("d-M-Y H:i"), PDO::PARAM_STR); 
                                            $stmt->bindValue(':amt',$data['Data'], PDO::PARAM_STR);  
                                            $stmt->bindValue(':availableFund', $totalAvaib , PDO::PARAM_STR);  
                                            $stmt->bindValue(':topUpId', $_POST['cuurentId'] , PDO::PARAM_STR);    
                                            $stmt->execute();  
                                            
                                            $CoinVal = ($data['Data']);    
                                            $stmt=$conn->prepare("insert into  UI__DChstr (typ,amt,Tdate,Rid,sId,percet,coin,byId) values(:typ,:amt,:Tdate,:Rid,:sId,:percet,:coin,:byId) ");   
                                            $stmt->bindValue(':typ', 1, PDO::PARAM_INT); 
                                            $stmt->bindValue(':amt', $data['Data'], PDO::PARAM_INT); 
                                            $stmt->bindValue(':Tdate', date("d-M-Y H:i:s") , PDO::PARAM_STR); 
                                            $stmt->bindValue(':Rid', $_POST['cuurentId']  , PDO::PARAM_STR);
                                            $stmt->bindValue(':sId', $MyCust  , PDO::PARAM_STR);
                                            $stmt->bindValue(':percet', 0  , PDO::PARAM_STR);  
                                            $stmt->bindValue(':coin', 0  , PDO::PARAM_STR);   
                                            $stmt->bindValue(':byId', $_SESSION['userId']  , 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(); 
                            				$Dref = $stmt->fetch(PDO::FETCH_OBJ);   
                            				
                                            if($affected_rows_UI__DChstr < 1)
                                            {
                            				$stmt=$conn->prepare("insert into  UI__DChstr (typ,amt,Tdate,Rid,sId,level) values(:typ,:amt,:Tdate,:Rid,:sId,1) ");   
                            				$stmt->bindValue(':typ', 7, PDO::PARAM_STR); 
                            				$stmt->bindValue(':amt', (($data['Data']*5)/100) , PDO::PARAM_INT); 
                            				$stmt->bindValue(':Tdate', date("d-M-Y H:i:s") , PDO::PARAM_STR); 
                            				$stmt->bindValue(':Rid', $_POST['cuurentId'] , PDO::PARAM_STR);   
                            				$stmt->bindValue(':sId', $Dref->refId, PDO::PARAM_STR);    
                            				$stmt->execute(); 
                                            }
                            				
                            				$stmt=$conn->prepare("select  custId from UI__Rgst where cuurentId =:cuurentId ");   
                                            $stmt->bindValue(':cuurentId', $Dref->refId , PDO::PARAM_INT);  
                                            $stmt->execute(); 
                            				$MdRef = $stmt->fetch(PDO::FETCH_OBJ);    
                            				
                            	     		   
                            				 if(end($ard)==0)
                                             {  
                                                for($j1=0;$j1<(count($ard)-1);$j1++)
                                                {
                                                    if($j1==0)
                                                    {       
                                                            $CoinVal = ($data['Data']*0.2)/100;    
                                                            $stmt=$conn->prepare("select id from UI__DChstr where Rid=:Rid and typ=1 and status=0 ");   
                                                            $stmt->bindValue(':Rid', $ard[$j1] , PDO::PARAM_STR);   
                                                            $stmt->execute();  
                                                            if($stmt->rowCount() > 0) 
                                                            {    
                                                                $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', 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)
                                                    { 
                                                        $CoinVal = ($data['Data']*0.1)/100;    
                                                        $stmt=$conn->prepare("select id from UI__DChstr where Rid=:Rid and typ=1  and status=0 ");   
                                                        $stmt->bindValue(':Rid', $ard[$j1] , PDO::PARAM_STR);   
                                                        $stmt->execute();  
                                                        if($stmt->rowCount() > 0) 
                                                        {
                                                            $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 if($j1==2)
                                                    { 
                                                        $CoinVal = ($data['Data']*(0.05))/100;   
                                                        $stmt=$conn->prepare("select id from UI__DChstr where Rid=:Rid and typ=1  and status=0 ");   
                                                        $stmt->bindValue(':Rid', $ard[$j1] , PDO::PARAM_STR);   
                                                        $stmt->execute();  
                                                        if($stmt->rowCount() > 0) 
                                                        {
                                                            $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', 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)
                                                    { 
                                                        $CoinVal = ($data['Data']*(0.05))/100;       
                                                        $stmt=$conn->prepare("select id from UI__DChstr where Rid=:Rid and typ=1  and status=0 ");   
                                                        $stmt->bindValue(':Rid', $ard[$j1] , PDO::PARAM_STR);   
                                                        $stmt->execute();  
                                                        if($stmt->rowCount() > 0) 
                                                        {
                                                            $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', 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)
                                                    { 
                                                        $CoinVal = ($data['Data']*(0.05))/100;     
                                                        $stmt=$conn->prepare("select id from UI__DChstr where Rid=:Rid and typ=1 and status=0 ");   
                                                        $stmt->bindValue(':Rid', $ard[$j1] , PDO::PARAM_STR);   
                                                        $stmt->execute();  
                                                        if($stmt->rowCount() > 0) 
                                                        {
                                                            $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', 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();     
                                                        }
                                                         
                             					      }
                             					    
                                                   }
                                             }	    
                                            
                                            $Data = (($data['Data']*5)/100);  
                                            $stmt=$conn->prepare("select  refId from UI__Rgst where cuurentId=:cuurentId  "); 
                                            $stmt->bindValue(':cuurentId', $_POST['cuurentId']   , PDO::PARAM_STR);  
                                            $stmt->execute();  
                                            $refId = $stmt->fetch(PDO::FETCH_OBJ);
                                            
                                             
                                            $stmt=$conn->prepare("insert into  UI__Tpm (TopUpId,Amt,Tdate,RefTbl,custId) values(:TopUpId,:Amt,:Tdate,:RefTbl,:custId)"); 
                                            $stmt->bindValue(':TopUpId', $refId->refId , PDO::PARAM_STR); 
                                            $stmt->bindValue(':Amt', $Data , PDO::PARAM_STR); 
                                            $stmt->bindValue(':Tdate', date("d-M-Y") , PDO::PARAM_STR);  
                                            $stmt->bindValue(':RefTbl', $_POST['cuurentId'] , PDO::PARAM_STR);  
                                            $stmt->bindValue(':custId', $_POST['cuurentId'] , PDO::PARAM_STR);    
                                            $stmt->execute();  
                                            
                                            
                                            $Tdt = date("d-M-Y", strtotime($dmt->Tdate));   
                                            $amount = number_format((float)(($data['Data']*2)/180), 2, '.', '');   
                                            $stmt=$conn->prepare("insert into UI__DailyROIGene(custId,amount,Tdate,TopUpdate) values(:custId,:amount,:Tdate,:TopUpdate)"); 
                                            $stmt->bindValue(':custId',  $_POST['cuurentId'] , PDO::PARAM_INT);   
                                            $stmt->bindValue(':amount', $amount , PDO::PARAM_INT);    
                                            $stmt->bindValue(':Tdate',  date("d-M-Y") , PDO::PARAM_STR);   
                                            $stmt->bindValue(':TopUpdate',  date("d-M-Y") , PDO::PARAM_STR);      
                                            $stmt->execute();  
                                            
                                            $stmt=$conn->prepare("insert into UI__TopWithDrw(custId,Tdate,status,type,amt) values(:custId,:Tdate,:status,:type,:amt)"); 
                                            $stmt->bindValue(':custId',  $_POST['cuurentId'] , PDO::PARAM_STR);   
                                            $stmt->bindValue(':Tdate',  date("d-M-Y") , PDO::PARAM_STR);   
                                            $stmt->bindValue(':status', 0 , PDO::PARAM_INT);     
                                            $stmt->bindValue(':type',  1 , PDO::PARAM_INT);
                                            $stmt->bindValue(':amt', $data['Data'] , PDO::PARAM_STR);
                                            $stmt->execute();      
                                             
                                        }
                                        
                                    
                                    //top up end
                                    die;
                           }  
                        } 
                        die;