<?php
    
	$quCommSlab   = "SELECT Comm, OpName, Status FROM comm_slab WHERE Userid='$userid' AND OpCode='$OpCode'";
	$reCommSlab   = mysqli_query($con,$quCommSlab);
	$dataCommSlab = mysqli_fetch_array($reCommSlab);
	$usrComm      = $dataCommSlab['Comm'];
	$OpName	      = $dataCommSlab['OpName'];
	$OpStatus     = $dataCommSlab['Status'];
	    
	$rechCode_errorPass=1;
	if($amount<10)
	{
	    $msg="Amount Is Less Than 10 !";
	}
	elseif($OpStatus!="ACTIVE")
	{
	    $msg="Inactive Operator !";
    }
	elseif($amount>$usrBal)
	{
	   $msg="Insufficiant Wallet Balance !";
    }
	elseif(0<getDupRech($mnum,$amount,$OpCode,$date))
	{
	    $msg="Same Recharge Not Allowed At Same Day !";
	}
	else
	{
	    $rechCode_errorPass=0;
	    
	    $UserCommAmt=$amount*$usrComm/100;
	    $DebitAmount=$amount-$UserCommAmt;
	    $usrNewBal=$usrBal-$DebitAmount;
	    
	    $insertRech="INSERT INTO recharge(Date,Time,Userid,PartnerId,Mobile,Amount,OpName,OpCode,IP,UsrBal)
		VALUES ('$date','$time','$userid','$partnerId','$mnum','$amount','$OpName','$OpCode','$IP','$usrNewBal')";
		if(mysqli_query($con,$insertRech))
		{
		    $newrechid=mysqli_insert_id($con);
	    
		    $insertWallet="INSERT INTO wallet (Date,Time,Userid,OpBal,Txn,ClBal,TxnId,TxnType,TxnWith)
			VALUES ('$date','$time','$userid','$usrBal','-$DebitAmount','$usrNewBal','$newrechid','RECHARGE','$IP')";
			if(mysqli_query($con,$insertWallet))
			{   
			    
			    if($usrType=="API")
			    {
			        $quSetApi="SELECT * FROM set_api WHERE Userid='$userid' AND OpCode='$OpCode'";
			    }
			    else
			    {
			        $quSetApi="SELECT * FROM set_api WHERE Userid='$G_Parent' AND OpCode='$OpCode'";
			    }
			    $reSetApi    = mysqli_query($con,$quSetApi);
			    $countSetApi = mysqli_num_rows($reSetApi);
			    
			    $apiLoop="STOP";
			    
			    for($x=1; $x<=$countSetApi; $x++)
			    {
			        $dataSetApi = mysqli_fetch_array($reSetApi);
			        $api        = $dataSetApi['UseApi'];
			        $ApiOpCode  = $dataSetApi['ApiOpCode'];
			        $R_offer    = $dataSetApi['R_offer'];
			        $STV        = $dataSetApi['STV'];
			        $Circle     = $dataSetApi['Circle'];
			        $AlwAmt     = $dataSetApi['AlwAmt'];
			        $BlkAmt     = $dataSetApi['BlkAmt'];
			        $LapuGroup  = $dataSetApi['LapuGroup'];
			        $ReqRoffer  = $dataSetApi['ReqRoffer'];
			        
			        if(empty($AlwAmt) && empty($BlkAmt) || $AlwAmt=="0" && $BlkAmt=="0")
			        {
			            $apiFile    = "ApiFile_".$api.".php";
			            include '../LinkFile/'.$apiFile;
			        }
			        else
			        {
		    	        $SeperateAlwAmt = explode(',',$AlwAmt);
			            $SeperateBlkAmt = explode(',',$BlkAmt);
			        
                        if(in_array($amount, $SeperateBlkAmt) || !in_array($amount, $SeperateAlwAmt)) 
                        {
                            if($countSetApi>$x)
		                    {
		                        $apiLoop="CONTINUE";
		                    }
		                    else
		                    {
		                        $rechCode_errorPass=1;
                                $msg = "This Recharge Amount Is Not Allowed";
                                include'../LinkFile/rechFailed.php';
		                    }
                        } 
                        else 
                        {
                            $apiFile    = "ApiFile_".$api.".php";
			                include '../LinkFile/'.$apiFile;
                        }
			        }
			        
			        if($apiLoop=="STOP")
			        {
			            break;
			        }
			    }
			}
		}
	}
	
	if($rechCode_errorPass==1)
	{
	    $Printmsg=$msg;
	    if($usrType=="API")
	    {
	        $rechStatus="FAILED";
	        include'../LinkFile/respoApi.php';
	    }
	    else
	    {
	        $respoStatus="FAILED";
	        include'../LinkFile/respoPanel.php';
	    }
	}
?>