<?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 !";
	    $toMobile = "8410930786";
        $wmsg    = "$OpName Is Deactive For $ApiName From LLP Portal !";
        require'../wmsg.php';
    }
	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,Debit,OpName,OpCode,IP,UsrBal)
		VALUES ('$date','$time','$userid','$partnerId','$mnum','$amount','$DebitAmount','$OpName','$OpCode','$IP','$usrNewBal')";
		if(mysqli_query($con,$insertRech))
		{
		    $newrechid=mysqli_insert_id($con);
	        
	        if(!empty($apiHitId))
	        {
	            $updateApiPartnerHit="UPDATE api_hit SET TxnId='$newrechid' WHERE Id='$apiHitId'";
	            mysqli_query($con,$updateApiPartnerHit);
	        }
	        
		    $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' AND Status='ACTIVE' ORDER BY Id ASC";
			    }
			    else
			    {
			        $quSetApi="SELECT * FROM set_api WHERE Userid='$G_Parent' AND OpCode='$OpCode' AND Status='ACTIVE' ORDER BY Id ASC";
			    }
			    $reSetApi    = mysqli_query($con,$quSetApi);
			    $countSetApi = mysqli_num_rows($reSetApi);
			    
			        $apiLoop    = "STOP";
			        $maxLoop    = 0;
			        $api        = "";
			        $ApiOpCode  = "";
			        $R_offer    = "";
			        $STV        = "";
			        $Circle     = "";
			        $AlwAmt     = "";
			        $BlkAmt     = "";
			        $LapuGroup  = "";
			        $ReqRoffer  = "";
			        
			    for($x=1; $x<=$countSetApi; $x++)
			    {
			        $maxLoop    = $maxLoop+1;
			        $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";
			            require '../LinkFile/'.$apiFile;
			        }
			        else
			        {
		    	        $SeperateAlwAmt = explode(',',$AlwAmt);
			            
						if(empty($AlwAmt) || $AlwAmt=="0" || in_array($amount, $SeperateAlwAmt))
						{
							$SeperateBlkAmt = explode(',',$BlkAmt);
						
							if(in_array($amount, $SeperateBlkAmt)) 
							{
								if($countSetApi>$x)
								{
									$apiLoop="CONTINUE";
								}
								else
								{
									$rechCode_errorPass=1;
									$msg = "This Recharge Amount Is Not Allowed";
									require'../LinkFile/rechFailed.php';
								}
							} 
							else 
							{
								$apiFile    = "ApiFile_".$api.".php";
								require '../LinkFile/'.$apiFile;
							}
						} 
						else 
						{
							$apiFile    = "ApiFile_".$api.".php";
							require '../LinkFile/'.$apiFile;
						}
			        }
			        
			        if($apiLoop=="STOP" || $maxLoop==3)
			        {
			            break;
			        }
			    }
			}
		}
	}
	
	if($rechCode_errorPass==1)
	{
	    $Printmsg=$msg;
	    if($usrType=="API")
	    {
	        $rechStatus="FAILED";
	        include'../LinkFile/respoApi.php';
	    }
	    else
	    {
	        $respoStatus="FAILED";
	        include'../LinkFile/respoPanel.php';
	    }
	}
?>