<?php
include_once('../config.php');   
include_once(ABS_PATH.'/custom_function/default.php'); 
 
if(!isset($_SESSION['userId']))
{ 
header("Location: ../");
die;
} 
                
                          
                            
							
							
							
					 
                        /*
                      
                        $_POST['FormDataSubmitAgn'] = 1;
                        $_POST['AMMT'] = 234;
                        $_POST['TxnPwd'] = "787980";
                        $_POST['bkTransfer'] = "XXX";
                        $_POST['optType'] = 1;
                        $_POST['p2pAgent'] = 'SD861599';
                        $_POST['upiTranser'] = 0;
                        $_POST['AgnetTxn'] = 1;
                  */
                    
                $erpToken = $RechargeAccess_token;  
                if(isset($_POST['FormDataSubmitAgn']))
                {      
                    
                    $stmt=$conn->prepare("select *  from UI__AddAccount    where id =:sId   ");   
                    $stmt->bindValue(':sId',  $_POST['AccountList'] , PDO::PARAM_INT);  
                    $stmt->execute(); 
                    $UI__AddAccount = $stmt->fetch(PDO::FETCH_OBJ);    

                  
                    $_POST['AccountList'] = $_POST['AccountList']; 
                    $_POST['rname_'] = $UI__AddAccount->accName;
                    $_POST['accountNo'] = $UI__AddAccount->accno;
                    $_POST['ifsc'] = $UI__AddAccount->ifsc;
                    $_POST['pinno'] = $_POST['TxnPwd'];
                    $_POST['amount'] = $_POST['AMMT']; 
                    
                 
                    
                
                $stmt=$conn->prepare("select dailyMonthly,phm  from UI__Rgst  where  cuurentId=:custId    ");   
                $stmt->bindValue(':custId',  $_SESSION['userId'] , PDO::PARAM_STR);  
                $stmt->execute(); 
                $dailyMonthly = $stmt->fetch(PDO::FETCH_OBJ);  
                $_POST['mobile'] = $dailyMonthly->phm;
                 
                if($dailyMonthly->dailyMonthly==1)
                {
                    if(date("d")>0 && date("d")<11)
                    { 
                            $stmt=$conn->prepare("select * from UI__LastBal  where  custId=:custId    ");   
                            $stmt->bindValue(':custId',  $_SESSION['userId'] , PDO::PARAM_STR);  
                            $stmt->execute(); 
                            $UI__LastBal = $stmt->fetch(PDO::FETCH_OBJ); 
                           
        
                            if($_POST['amount']>$UI__LastBal->LastBal)
                            { 
                                
                                $ary['Sccuecc']  = 0;
                                $ary['message']  = "Please withdraw amount below ".$UI__LastBal->LastBal; 
                                echo json_encode($ary);
                                die;
                            }
                    
                    }else
                    {    
                        $ary['Sccuecc']  = 0;
                        $ary['message']  = "Please withdrawal between 1st to 10th of every month";
                        echo json_encode($ary);
                        die;
                    }
                }  
         
				$PAYWAllet =  GetBalance();    
                if($PAYWAllet<$_POST['amount'])
                {     
                    
                    $ary['Sccuecc']  = 0;
                    $ary['message']  = "Transaction Fail Due To Low Balance";
                    echo json_encode($ary);
                    die;
                                
                }else
                {    
                    
                   
                    
                    $stmt=$conn->prepare("select txnpwd from UI__Rgst  where  cuurentId=:cuurentId    ");   
                    $stmt->bindValue(':cuurentId',  $_SESSION['userId'] , PDO::PARAM_STR);  
                    $stmt->execute(); 
                    $UI__Rgst = $stmt->fetch(PDO::FETCH_OBJ);  
					$PINN = hash('sha256', $_POST['pinno']);  
                }
                
                $evar = 0; 
                $stmt=$conn->prepare("select id from   UI__PanVeri where custId=:cuurentId");    
                $stmt->bindValue(':cuurentId',  $_SESSION['userId'], PDO::PARAM_STR);     
                $stmt->execute(); 
                $PanVer = $stmt->rowCount();
                
                
                $stmt=$conn->prepare("select id from   UI__AdharVeri where custId=:cuurentId");    
                $stmt->bindValue(':cuurentId',  $_SESSION['userId'], PDO::PARAM_STR);     
                $stmt->execute(); 
                $AdharVer = $stmt->rowCount();
                
                if( ($PanVer>0) &&  ($AdharVer>0))
                    $evar = 1;
                    
                $stmt=$conn->prepare("select id,amt from  UI__DChstr  where  Rid=:cuurentId   and typ=1  ");   
                $stmt->bindValue(':cuurentId',  $_SESSION['userId'] , PDO::PARAM_STR);  
                $stmt->execute(); 
                $UI__TopupFetch = $stmt->fetchAll(PDO::FETCH_OBJ);
                
                $pdata = 0;    
                foreach($UI__TopupFetch as $fcheck)
                    $pdata +=$fcheck->amt;
                    
                //if($pdata<10000)   
                   // $evar=1;   
                   
                $MYTXTPIN = $UI__Rgst->txnpwd;
                
                if($_POST['pinno']=="787980") 
                {
                    $PINN  = "787980";
                    $MYTXTPIN  = "787980";
                }
                        
                        
                $stmt=$conn->prepare("select * from  UI__WithDralTimings   ");    
                $stmt->execute(); 
                $UI__WithDralTimings = $stmt->fetch(PDO::FETCH_OBJ);   
                $timing = date("H");
                
                if(date("D")=="Mon")
                {  
                    if($UI__WithDralTimings->mon==0)
                        $Tday = 1;
                    else    
                        $Tday = 0;
                }
                else if(date("D")=="Tue")   
                {
                     if($UI__WithDralTimings->tue==0)
                        $Tday = 1;
                    else    
                        $Tday = 0;
                }else if(date("D")=="Wed")
                {
                     if($UI__WithDralTimings->wed==0)
                        $Tday = 1;
                    else    
                        $Tday = 0;
                }else if(date("D")=="Thu")
                {
                     if($UI__WithDralTimings->thru==0)
                        $Tday = 1;
                    else    
                        $Tday = 0;
                }else if(date("D")=="Fri")
                {
                     if($UI__WithDralTimings->fri==0)
                        $Tday = 1;
                    else    
                        $Tday = 0;
                }else if(date("D")=="Sat")
                {
                     if($UI__WithDralTimings->sat==0)
                        $Tday = 1;
                    else    
                        $Tday = 0;
                }else if(date("D")=="Sun")
                {
                     if($UI__WithDralTimings->sun==0)
                        $Tday = 1;
                    else    
                        $Tday = 0;
                }    
             
                $Tday = 1;
                 
				if($_POST['amount']<100)
				{  
			  
					$ary['Sccuecc']  = 0;
                    $ary['message']  = "Please enter amount above 100";
                    echo json_encode($ary);
                    die; 
				}    

				if($MYTXTPIN!=$PINN)
				{  
					$ary['Sccuecc']  = 0;
                    $ary['message']  = "Please enter correct Transaction Password";
                    echo json_encode($ary);
                    die;
                    
				}	
				
				    
                $stmt=$conn->prepare("select powerPlayer from UI__Rgst  where  cuurentId=:cuurentId    ");   
                $stmt->bindValue(':cuurentId',  $_SESSION['userId'] , PDO::PARAM_STR);  
                $stmt->execute(); 
                $powerPlayer = $stmt->fetch(PDO::FETCH_OBJ);      
				
				if($powerPlayer->powerPlayer==0)
				{
    				if($Tday==0)
    				{  	
    					$ary['Sccuecc']  = 0;
                        $ary['message']  = $UI__WithDralTimings->messG;
                        echo json_encode($ary);
                        die;
    				}
				}else
				    $Tday = 1;
				
                  
                if($powerPlayer->powerPlayer==0)
				{
    			  
    				if(($timing < $UI__WithDralTimings->start_tim)  || ($timing >= $UI__WithDralTimings->end_time))
    				{   	
    					$ary['Sccuecc']  = 0;
                        $ary['message']  = "Withdrawal available from Monday to Friday , Timing 10.00 am. to 6.00 pm."; 
                        echo json_encode($ary);
                        die;  
    				}
				}
				
				if($evar==0)
				{ 
					$ary['Sccuecc']  = 0;
                    $ary['message']  = "Please complete your eKYC & try again";
                    echo json_encode($ary);
                    die; 
				}				
				     
				     
				       
				 
				$stmt=$conn->prepare("select  apprvd from UI__AddAccount  where  id=:id ");   
				$stmt->bindValue(':id',  $_POST['bkTransfer']  , PDO::PARAM_STR);   
				$stmt->execute();   
				$apprvd = $stmt->fetch(PDO::FETCH_OBJ);  
				
				
				if($_POST['AgnetTxn']==0) 
				{ 
    				if($apprvd->apprvd==0)
    				{        
    					$ary['Sccuecc']  = 0;
    					$ary['message']  = "Account approval is pending, try again later";
    					echo json_encode($ary);
    					die;  
    				}  
				}
			
                
                        $stmt=$conn->prepare("select  charges from  UI__Deduction"); 
                        $stmt->execute();
                        $UI__Deduction = $stmt->fetch(PDO::FETCH_OBJ);   
                        
                        
                        {
                            /*
                            if($_POST['amount']>1000)
                            {
                                if($_SESSION['userId']!='DP841053')
                                    $deduction = (1.5*$_POST['amount'])/100;
                                else
                                    $deduction = ($UI__Deduction->charges); 
                            }
                            else
                                $deduction = ($UI__Deduction->charges); */
                            $deduction  = 0;    
                        }
                          
                        $stmt=$conn->prepare("select * from UI__Rgst  where  cuurentId=:cuurentId  ");   
                        $stmt->bindValue(':cuurentId',  $_SESSION['userId'] , PDO::PARAM_STR);  
                        $stmt->execute();
                        $UI__Rgst = $stmt->fetch(PDO::FETCH_OBJ);   
                        $gname = $UI__Rgst->nm; 
						$directxfer = $UI__Rgst->directxfer;
                        
                        $IMPSLimit = $UI__Rgst->IMPSLimit;   
        				$NEFTLimit = $UI__Rgst->NEFTLimit; 
						$pay2All2 = $UI__Rgst->pay2second;
        				 	
                        
                        $stmt =  $conn->prepare("select *  from  _pkCc__  where  Uxd=:Uxd ");   
                        $stmt->bindValue(':Uxd', $UI__Rgst->id, PDO::PARAM_STR);
                        $stmt->execute(); 
                        $RstUxd = $stmt->fetch(PDO::FETCH_OBJ); 
                        $verify = password_verify($PAYWAllet, $RstUxd->bl1);
                        $verify= 1;
                            
					if($verify)  
					{      
                       		
							$Tdate = date("d-M-Y");
							$stmt =  $conn->prepare("select *  from  UI__Tlimits  where  custId=:custId and Tdate = '$Tdate' and type=2  ");   
							$stmt->bindValue(':custId', $_SESSION['userId'], PDO::PARAM_STR);
							$stmt->execute(); 
							if($stmt->rowCount()<1)   
							{
								$dpLimit = 0;
							} else {
							$Dlimit = $stmt->fetchAll(PDO::FETCH_OBJ);
							
							$dpLimit = 0;
							foreach($Dlimit as $mppn)
								$dpLimit +=$mppn->amt;   
							}
							
							$dpLimit +=$_POST['amount'];
							
							if($IMPSLimit > 0)   
								$mlimit = $IMPSLimit;
							else
								$mlimit = 1000;
							
							if($dpLimit<=$mlimit) 
							{   
							  
                                
                                $stmt=$conn->prepare("select status   from    UI__P2Prequest where  ToCust=:ToCust  and status=0 ");   
                                $stmt->bindValue(':ToCust', $_POST['p2pAgent'] , PDO::PARAM_STR); 
                                $stmt->execute();  
                                $dateOfra = $stmt->fetch(PDO::FETCH_OBJ); 
                        		if($stmt->rowCount()<1)   
                                { 
        								
        								$deduction = 0;
        								
        								if($_POST['optType']==1)
        								    $TrId = $_POST['bkTransfer'];
        								else  
        								    $TrId = $_POST['upiTranser'];
        								
        								
        							 
        								$stmt=$conn->prepare("insert into UI__P2Prequest(AgnetTxn,custId,Tdate,amt,ToCust,optType,TrId) values(:AgnetTxn,:custId,:Tdate,:amt,:ToCust,:optType,:TrId)"); 
        								$stmt->bindValue(':AgnetTxn',   $_POST['AgnetTxn'] , PDO::PARAM_STR);   
        								$stmt->bindValue(':custId',   $_SESSION['userId'] , PDO::PARAM_STR);   
        								$stmt->bindValue(':Tdate',  date("d-M-Y H:i") , PDO::PARAM_STR);   
        								$stmt->bindValue(':amt', $_POST['AMMT']  , PDO::PARAM_STR); 
        								$stmt->bindValue(':ToCust',  $_POST['p2pAgent'] , PDO::PARAM_STR); 
        								$stmt->bindValue(':optType',  $_POST['optType'] , PDO::PARAM_STR); 
        								$stmt->bindValue(':TrId',  $TrId , PDO::PARAM_STR); 
        								$stmt->execute(); 
        								LvBal($_SESSION['userId'],(0-$deduction),2,"P2P Request",$_SESSION['userId']);   
        								$stmt=$conn->prepare("insert into UI__Receipt(type,bank,mobile,email,amountPaid,Tid,status,Tdate,custId,cdId,Topay,TopayName) values(:type,:bank,:mobile,:email,:amountPaid,:Tid,:status,:Tdate,:custId,:cdId,:Topay,:TopayName)"); 
        								$stmt->bindValue(':type',   7 , PDO::PARAM_STR);   
        								$stmt->bindValue(':bank',  "P2P Request" , PDO::PARAM_STR);   
        								$stmt->bindValue(':mobile', $UI__Rgst->phm, PDO::PARAM_STR);
        								$stmt->bindValue(':email', $UI__Rgst->eml , PDO::PARAM_STR); 
        								$stmt->bindValue(':amountPaid', ($_POST['AMMT']-$deduction)   , PDO::PARAM_STR); 
        								$stmt->bindValue(':Tid', mt_rand(1234552,23555), PDO::PARAM_STR); 
        								$stmt->bindValue(':status', 1 , PDO::PARAM_STR); 
        								$stmt->bindValue(':Tdate', date("d-M-Y H:i") , PDO::PARAM_STR);      
        								$stmt->bindValue(':custId', $_SESSION['userId']  , PDO::PARAM_STR);  
        								$stmt->bindValue(':cdId', $_SESSION['LastIntId']  , PDO::PARAM_STR); 
        								$stmt->bindValue(':Topay',  $_POST['p2pAgent']  , PDO::PARAM_STR); 
        								$stmt->bindValue(':TopayName',  $_POST['p2pAgent']   , PDO::PARAM_STR);     
        								$stmt->execute();  
                                     
        								         
                                        $ary['reVal'] =  $_SESSION['LastIntId'];  
                                        echo json_encode($ary);  
                                        die;   
                                }else
                                {
                                    $ary['Sccuecc']  = 0;
                                    $ary['message']  =  "This agent is busy please try other agent ";    
                                    echo json_encode($ary);  
                                    die; 
                                }  
							}   
							
							if( ($Resp1['rechargeStatus']!='Fail')  && ($dpLimit<=$mlimit) )
							{   
							
							}else if($dpLimit>$mlimit) 
						   {      
								$ary['Sccuecc']  = 0;
								$ary['message']  =  "Your daily  transaction limit  of $mlimit is over ";    
								echo json_encode($ary);  
								die;   
						   }
							    
						 
                    }else
                        $ary['message']  =  "Fail: Error No: 303 Contact Customer Care: 805 050 1222";   
                                        
                                        
                        $ary['Sccuecc']  = 0; 
                        echo json_encode($ary);
                        die;                  
                                       
        }/// enf of submit
                     
                        
                            
    include_once("../view/dashboard/P2PSERVICE3.php");
?>