<?php
	include_once('../config.php');      
    include_once(ABS_PATH.'/custom_function/default.php');  

    if(!isset($_SESSION['userId']))
    { 
         header("Location: ../");
         die;
    }   
    
     
                      $PAYWAllet  = GetBalance();
    if(isset($_POST['submit'])) 
    {              
        
                
					$stmt=$conn->prepare("select *  from UI__DailyROIGene    where custId=:sId     ");   
					$stmt->bindValue(':sId', $_SESSION['userId'] , PDO::PARAM_INT);  
					$stmt->execute(); 
					$XUI__DailyROIGene = $stmt->fetchAll(PDO::FETCH_OBJ);  

					$Tamnt = 0;
					foreach($XUI__DailyROIGene as $xx)
					{
					$Tamnt +=$xx->amount;
					}      


					$stmt=$conn->prepare("select *  from UI__Bns    where Rid =:sId   ");   
					$stmt->bindValue(':sId',  $_SESSION['userId'] , PDO::PARAM_INT);  
					$stmt->execute(); 
					$UI__Bns = $stmt->fetchAll(PDO::FETCH_OBJ); 

					$LevelI = 0;
					foreach($UI__Bns as $dddm)
					{
					$LevelI +=$dddm->Tamt;
					}


					$stmt=$conn->prepare("select Amt   from UI__Tpm where TopUpId=:refId   ");   
					$stmt->bindValue(':refId',  $_SESSION['userId'] , PDO::PARAM_INT);  
					$stmt->execute(); 
					$MdirectB = $stmt->fetchAll(PDO::FETCH_OBJ);  

					$directIncome = 0;
					foreach($MdirectB as $dmm)
					$directIncome += $dmm->Amt;                




					$stmt=$conn->prepare("select *  from UI__WithDraw    where userid=:sId and (accept=1 ||accept=2)  ");   
					$stmt->bindValue(':sId',  $_SESSION['userId'] , PDO::PARAM_INT);  
					$stmt->execute(); 
					$UI__WithDraw = $stmt->fetchAll(PDO::FETCH_OBJ); 


					$totalWithdrwa = 0;
					foreach($UI__WithDraw as $tl)
					{
					$totalWithdrwa +=$tl->amt;
					}  


					$stmt=$conn->prepare("select availableFund from UI__Fundtransfer  where  userid=:userid  order by id desc  ");   
					$stmt->bindValue(':userid',  $_SESSION['userId'] , PDO::PARAM_INT);  
					$stmt->execute(); 
					$UI__Fundtransfer = $stmt->fetch(PDO::FETCH_OBJ);     
					$PAYWAllet =  ($Tamnt+$LevelI+$directIncome+$UI__Fundtransfer->availableFund)-($totalWithdrwa);        

                    
                    if($PAYWAllet<$_POST['amount'])
                    {
                        $_SESSION['Regis'] = "Transaction Fail Due To Low Balance";
                        header("Location: Wllat_transf.php");
                        die; 
                    }
                    
                
                $stmt=$conn->prepare("select * from  UI__Voucher where unique_provider_id=:unique_provider_id");    
                $stmt->bindValue(':unique_provider_id',  $_POST['Operator'], PDO::PARAM_STR);     
                $stmt->execute(); 
                $UI_ = $stmt->fetch(PDO::FETCH_OBJ);
                
                
                
                if($_POST['amount']>=$UI_->min_price && $_POST['amount']<=$UI_->max_price) 
                { 
                 
                    $stmt=$conn->prepare("select eml,nm from  UI__Rgst where cuurentId=:cuurentId");    
                    $stmt->bindValue(':cuurentId',  $_SESSION['userId'], PDO::PARAM_STR);     
                    $stmt->execute(); 
                    $sendertmt = $stmt->fetch(PDO::FETCH_OBJ);    


                    
                    $stmt=$conn->prepare("select  eml,nm,phm from  UI__Rgst where cuurentId=:cuurentId");    
                    $stmt->bindValue(':cuurentId',  $_POST['mobile'], PDO::PARAM_STR);     
                    $stmt->execute(); 
                    $Rec = $stmt->fetch(PDO::FETCH_OBJ); 
                    
                     

                    $senderEmail = $sendertmt->eml ;    
                    $senderName =  $sendertmt->nm;;    
                    $ReceptName = $Rec->nm;    
                    $ReceptEmail = $Rec->eml ;    
                    $mobile = $Rec->phm;
                    
                  //  echo '<pre>'; print_r($_POST);
                 
                    $txnid = mt_rand(234553,343553);
                    $amount= $_POST['amount'].".00";
                    $apisecret = "yZe6SQavyVPe4kVfMjebHnbfKTNPyFz98PJvKT6qgeRR4sNr"; 
                    $api_user_id= "z3dz5LVEYBw34AUXAcaUr7V8jNATpPcfbJpMfZbd48tJNzNA";
                    $concat = $amount."|".$api_user_id."|".$txnid."|".$apisecret; 
                    $hash = hash("sha512",$concat);    
                     
                    $Operator = $_POST['Operator']; 
                    $url = 'https://api.komparify.com/api/v2/recharge?search_type=voucher&number='.$mobile.'&operator='.$Operator.'&txnid='.$txnid.'&securehash='.$hash.'&decimal_value='.$amount."&method=GET&api_user_id=z3dz5LVEYBw34AUXAcaUr7V8jNATpPcfbJpMfZbd48tJNzNA".'&amp;param_1='.urlencode($ReceptName)."&amp;param_2=".urlencode($ReceptEmail)."&amp;param_3=".urlencode($senderName)."&amp;param_4=".urlencode($senderEmail);
                             
                    $crl = curl_init();
                    curl_setopt($crl, CURLOPT_URL, $url);
                    curl_setopt($crl, CURLOPT_FRESH_CONNECT, true);
                    curl_setopt($crl, CURLOPT_RETURNTRANSFER, true); 
                    $response = curl_exec($crl);   
                    curl_close($crl);
                    
                    $stmt =  $conn->prepare("insert into test (name) values(:name)");            
                    $stmt->bindValue(':name', $response, PDO::PARAM_STR);
                    $stmt->execute();  
                     
                    $Data = (array)json_decode($response);   
                    if($Data['error']=='false') 
                    {
                        $stmt =  $conn->prepare("insert into UI__Gifts (custId,Tdate,operator,orderId,txnid,amount,toId) values(:custId,:Tdate,:operator,:orderId,:txnid,:amount,:toId)");            
                        $stmt->bindValue(':custId', $_SESSION['userId'], PDO::PARAM_STR);
                        $stmt->bindValue(':Tdate', date("d-M-Y H:i"), PDO::PARAM_STR);   
                        $stmt->bindValue(':operator', $Data['operator'], PDO::PARAM_STR);
                        $stmt->bindValue(':orderId', $Data['orderid'], PDO::PARAM_STR); 
                        $stmt->bindValue(':txnid', $Data['txnid'], PDO::PARAM_STR);
                        $stmt->bindValue(':amount', $Data['decimal_amount'], PDO::PARAM_STR); 
                        $stmt->bindValue(':toId', $_POST['mobile']  , PDO::PARAM_STR);     
                        $stmt->execute();
                          
    					$stmt=$conn->prepare("insert into UI__WithDraw(userid,Tdate,amt,orgAmt,order_id,bank_remark,status,accept) values(:userid,:Tdate,:amt,:orgAmt,:order_id,:bank_remark,:status,:accept)"); 
                        $stmt->bindValue(':userid',   $_SESSION['userId']  , PDO::PARAM_STR);   
                        $stmt->bindValue(':Tdate',  date("d-M-Y H:i") , PDO::PARAM_STR);   
                        $stmt->bindValue(':amt', $_POST['amount'] , PDO::PARAM_STR);
                        $stmt->bindValue(':orgAmt', $_POST['amount'] , PDO::PARAM_STR); 
                        $stmt->bindValue(':order_id',  $Data['orderid'] , PDO::PARAM_STR); 
                        $stmt->bindValue(':bank_remark', "NA", PDO::PARAM_STR); 
                        $stmt->bindValue(':status', "NA" , PDO::PARAM_STR); 
                        $stmt->bindValue(':accept', 1 , PDO::PARAM_STR);      
                        $stmt->execute();         
                                
                        $stmt=$conn->prepare("insert into  UI__DebitCredit (custId,addDel,availableBal,Tdate,type,Intype) values(:custId,:addDel,:availableBal,:Tdate,:type,'Gift Transfer')    "); 
                        $stmt->bindValue(':custId',$_SESSION['userId'], PDO::PARAM_STR); 
                        $stmt->bindValue(':addDel', $_POST['amount'], PDO::PARAM_STR); 
                        $stmt->bindValue(':availableBal',$PAYWAllet - $_POST['amount'], PDO::PARAM_STR);  
                        $stmt->bindValue(':Tdate', date("d-M-Y H:i") , PDO::PARAM_STR);  
                        $stmt->bindValue(':type', 2 , PDO::PARAM_STR);  
                        $stmt->execute();    
                         
                        $_SESSION['Regis'] = "Transaction successful";
                        $_SESSION['topSuc'] = 1;
                        
                        
                        $subject = "Gift Vourcher ".$SITE_NAME;  
                        $template_type = 1;
                        $attachment = '';
                        $name = $Data['operator'];
                        $Amt = $_POST['amount'];
                        
                        $message="
                        <tr>
                        <td align='left' valign='top'>
                        <h4 style='font-weight:bold; padding:2px 2px; margin:3px; text-align:left; color:#002368;'>Dear user,</h4>
                        <h5 style='font-weight:bold; padding:2px 2px; margin:0px; text-align:center; color:#002368;'>Thanks for Generating Gifts With Us. Your Details are as follows </h5> 
                        <h5 style='font-weight:bold; padding:2px 2px; margin:0px; text-align:center; color:#002368;'> To: $name , Amount Rs : $Amt </h5>
                        
                        </td>
                        </tr>"; 
                        
                        SendMail($ReceptEmail,$subject,$message,$template_type,$attachment); 

                        
                    }else
                        $_SESSION['Regis'] = "Transaction Fail";  
                }
                else
                    $_SESSION['Regis'] = "Amount should be between ".$UI_->min_price." and ".$UI_->max_price." "; 
                header("Location: Gifts_transf.php");
                die; 
    }
    
    $stmt=$conn->prepare("select * from  UI__Rgst where cuurentId=:cuurentId");    
    $stmt->bindValue(':cuurentId',  $_SESSION['userId'], PDO::PARAM_STR);     
    $stmt->execute(); 
    $Result = $stmt->fetch(PDO::FETCH_OBJ);    
    
    
    $stmt=$conn->prepare("select * from  UI__Voucher where category=:cuurentId");    
    $stmt->bindValue(':cuurentId',  $_REQUEST['cat'], PDO::PARAM_STR);     
    $stmt->execute(); 
    $UI__Voucher = $stmt->fetchAll(PDO::FETCH_OBJ);   
    
    

include_once("../view/dashboard/Gifts_transf1.php");
?>