<?php
	session_start();
	if($_SESSION['subadm'])
	{
		$userid=1111;
		if($_SESSION['subadm']==1111)
		{
			$APBU=$userid;
		}
		else
		{
			$APBU=$_SESSION['subadm'];
		}
		
	}
	else
	{
		header('location:../login.php');
	}
?>
<?php
	include'connection.php';
	include'time.php';
	include'../Retailers/functions.php';
	
	ini_set('memory_limit', '-1');

	$id=$_GET['id'];
	
	if(empty($id))
	{
	    echo "<h3 align='center' style='color:red;'>Request Id Not Found !</h3>";
	}
	
	$query="SELECT * FROM paymentreq WHERE Id='$id'";
	$result=mysqli_query($con,$query);
	$data=mysqli_fetch_array($result);
	$amount=$data['Amount'];
	$Txnnumber=$data['Txnnumber'];
	$uid=$data['Userid'];
	$BankName=$data['Bankname'];
	$reqType=$data['reqType'];
	
	if($BankName=="llprecharge@uboi")
	{
	    $BankName2="UNION BANK OF INDIA";
	}
	else
	{
	    $BankName2=$BankName;
	}
	
			
	$status="APPROVED";
	
	if(isset($_POST['submit']))
	{
		$respmsg="Approved By Admin";
		$bankAmount=$_POST['amont'];
		$bankTxnNo=$_POST['txn'];
		$bankcharge=$_POST['bc'];
		$MyBankName=$_POST['BankName'];
		$reqType2=$_POST['reqType'];
		
		if(empty($bankcharge))
		{
			$bankcharge=0;
		}
		
		$actualamount=$amount-$bankcharge;
		
		$Admin_Op_Bal=getBal($userid);
		$flag=0;
		if($actualamount<=$Admin_Op_Bal)
		{
			$quGetDupReq="SELECT * FROM paymentreq WHERE Id=$id";
			$reGetDupReq=mysqli_query($con,$quGetDupReq);
			$dataGetDupReq=mysqli_fetch_array($reGetDupReq);
			$DupReqStatus=$dataGetDupReq['Active'];
			
			if($DupReqStatus==0)
			{
			    if($reqType2!=$reqType)
			    {
			        echo "<center><h3 style='color:red;'>Request Type Not Matched !</h3></center>";
			    }
			    elseif($reqType=="BANKING")
			    {
			        if($amount>$bankAmount || $amount<$bankAmount)
			        {
			            echo "<center><h3 style='color:red;'>Request Amount Not Matched !</h3></center>";
			        }  
				    elseif($BankName2!==$MyBankName)
				    {
					    echo "<center><h3 style='color:red;'>Bank Name Not Matched !</h3></center>";
				    }  
				    elseif($Txnnumber!==$bankTxnNo)
				    {
					    echo "<center><h3 style='color:red;'>Transaction Number Not Matched !</h3></center>";
				    }
				    else
				    {
				        $flag=1;
				    }
			    }
			    else
			    {
			        if($amount>$bankAmount || $amount<$bankAmount)
			        {
			            echo "<center><h3 style='color:red;'>Request Amount Not Matched !</h3></center>";
			        }
			        else
			        {
			            $flag=1;
			        }
			    }
			    
			    if($flag==1)
			    {
				    $User_Op_Bal=getBal($uid);
	                
				    $BothOldBal=$Admin_Op_Bal+$User_Op_Bal;
				    $Admin_Cl_Bal=$Admin_Op_Bal-$actualamount;
				        
				    mysqli_query($con,"START TRANSACTION");
				    
					$query1="UPDATE paymentreq SET Responsemsg='$respmsg', RespTime='$time', Bankcharge='$bankcharge', Status='$status', Active='1', APBU='$APBU' WHERE Id=$id";
					if(mysqli_query($con,$query1))
					{
					    $query2="INSERT INTO transaction (Fromid, Toid, Credit)values('$userid', '$uid', '$actualamount')";
						if(mysqli_query($con,$query2))
						{
						    $query4="INSERT INTO wallet (Userid,OpBal,Txn,ClBal,TxnType,TxnWith,TxnId,Remarks,Date,Time,APBU)
							values('$userid','$Admin_Op_Bal','-$actualamount','$Admin_Cl_Bal','PAYMENT_APPROVED','$uid','$id','PAYMENT_APPROVED','$date','$time','$APBU')";
							if(mysqli_query($con,$query4))
							{	
							    $AdminEwalletId=mysqli_insert_id($con);
							        
								$query12="UPDATE admin SET Balance='$Admin_Cl_Bal' WHERE Userid='$userid'";
								mysqli_query($con,$query12);
							    
							    $clintOpBal=getBal($uid);
							    $clintClBal=$clintOpBal+$actualamount;
							    
								$query6="INSERT INTO wallet (Userid,OpBal,Txn,ClBal,TxnType,TxnWith,TxnId,Remarks,Date,Time,APBU)
								values('$uid','$clintOpBal','$actualamount','$clintClBal','PAYMENT_APPROVED','$userid','$id','Approved By Admin','$date','$time','$APBU')";
								if(mysqli_query($con,$query6))
								{
								    $ewalletid=mysqli_insert_id($con);
								    
									$sub=substr($uid,"0","2");
									if($sub==50)
									{
									    
										$query12="UPDATE retailer SET Balance='$clintClBal' WHERE Userid='$uid'";
										mysqli_query($con,$query12);
										
										$quGetUser="SELECT * FROM retailer WHERE Userid='$uid'";
										$reGetUser=mysqli_query($con,$quGetUser);
										$data=mysqli_fetch_array($reGetUser);
										$Mobile=$data['Mobile'];
										$UsrMailId=$data['Email'];
									}
									elseif($sub==20)
									{	
										$query12="UPDATE distributer SET Balance='$clintClBal' WHERE Userid='$uid'";
										mysqli_query($con,$query12);
										
										$quGetUser="SELECT * FROM distributer WHERE Userid='$uid'";
										$reGetUser=mysqli_query($con,$quGetUser);
										$data=mysqli_fetch_array($reGetUser);
										$Mobile=$data['Mobile'];
										$UsrMailId=$data['Email'];
									}
									elseif($sub==30)
									{
									    echo "proggramme Not Found For Master Distributer";
									    exit();
									}
									        
														
									$msg="Rs.".$actualamount." Credited In Your LLP Wallet On ".$date." ".$time.". Now Your Balance Is Rs. ".$newbal." Call For Any Query- 9897095971";
									
									$Admin_New_Bal=getBal($userid);
									$Clint_New_Bal=getBal($uid);
													
									$BothNewBal=$Admin_New_Bal+$Clint_New_Bal;
									
				                    if(floor($BothOldBal)==floor($BothNewBal))
				                    {
				                        if($reqType=="BANKING")
				                        {
				                            $BankId="select Id from bank where Bankname='$BankName2'";
				                            $reBankId=mysqli_query($con,$BankId);
				                            $dataBankId=mysqli_fetch_array($reBankId);
				                            $BankId=$dataBankId['Id'];
				                            
				                            $InsertTxn="INSERT INTO Bank_Account_Txn(Date,Time,Bank_Id,Bank_Name,Txn,Txn_Id,Disc,Txn_For)
				                            VALUES ('$date','$time','$BankId','$BankName2','$amount','$id','Payment_Approved','$uid')";
				                            if(mysqli_query($con,$InsertTxn))
				                            {
				                                $SettleUpdate="UPDATE paymentreq SET SettleStatus='SETTELED' WHERE Id=$id";
				                                if(mysqli_query($con,$SettleUpdate))
				                                {
                                                    if(mysqli_query($con,"COMMIT"))
                                                	{
                                                	    $extraMailMsg="Your Payment Request [ ".$id." ] has been Approved.";
    													$Mmsg=$extraMailMsg.", ".$msg;
	    												$subject="Payment_Approved";
		    											$to=$UsrMailId;
			    										include'../mail.php';
                                                	    echo "<h3 align='center' style='color:green; font-weight:bold;'>Balance Update Successfully !</h3>";  
                                                    }
                                                    else
                                                    {
                                                        if(mysqli_query($con,"ROLLBACK"))
												        {
				                                            echo "<h3 align='center' style='color:red; font-weight:bold;'>Rollback For Not Commit !</h3>";
											            }
                                                    }
				                                }
				                                else
				                                {
				                                    if(mysqli_query($con,"ROLLBACK"))
												    {
				                                        echo "<h3 align='center' style='color:red; font-weight:bold;'>Rollback For Settlement Query !</h3>";
											        }
				                                }
				                            }
				                            else
				                            {
				                                if(mysqli_query($con,"ROLLBACK"))
												{
				                                    echo "<h3 align='center' style='color:red; font-weight:bold;'>Rollback For Bank Txn !</h3>";
											    }
				                            }
				                        }
				                        else
				                        {
				                            if(mysqli_query($con,"COMMIT"))
                                            {
                                                $extraMailMsg="Your Payment Request [ ".$id." ] has been Approved.";
    											$Mmsg=$extraMailMsg.", ".$msg;
	    										$subject="Payment_Approved";
		    									$to=$UsrMailId;
			    								include'../mail.php';
                                                echo "<h3 align='center' style='color:green; font-weight:bold;'>Balance Update Successfully !</h3>";  
                                            }
												        
				                        }
				                    }
				                    else
				                    {
										if(mysqli_query($con,"ROLLBACK"))
										{
										    echo "<h3 align='center' style='color:red; font-weight:bold;'>Rollback For Error In Sum Query !</h3>";
									    }
				                    }
								}
								else
								{
									echo "error";
									mysqli_query($con,"ROLLBACK");
								}
							}
						}
					}
			    }
			}
			else
			{
				echo "<center><h3 align='center' style='color:red;'>This Request Proccesed Already !</h3></center>";
			}
		}
		else
		{
			echo "<center><h3 align='center' style='color:red;'>Insufficiant Balance !</h3></center>";
		}
		
	}
?>
<html>
<head>
<style>
    #inp{margin:15px; width:300px; height:50px; font-size:20px; padding:5px; border-radius:5px;}
</style>
</head>
<body>
	<form method="post">
	<table border="1" cellpadding="5" align="center" style="border-collapse:collapse;">
		<tr>
			<td><input type="text" placeholder="Enter Bank Charge..." name="bc" id="inp"></td>
		</tr>
		<tr>
		    <td>
		        <select name="reqType" id="inp">
		            <option>--Select Type--</option>
		            <option>BANKING</option>
		            <option>ADVANCE</option>
		            <option>EXCHANGE</option>
		        </select>
		    </td>
		 </tr>
		<?php
		if($reqType=="BANKING" )
		{
		    echo "<tr>
			        <td>
			            <select name='BankName' id='inp'>
			                <option>--Select Bank--</option>";
			                
			                $GetBank="SELECT * FROM bank WHERE Status='Active'";
			                $reGetbank=mysqli_query($con,$GetBank);
			                while($dataGetbank=mysqli_fetch_array($reGetbank))
			                {
			                    echo "<option>".$dataGetbank['Bankname']."</option>"; 
			                }
			       
			        echo "</select>
			    </td>
		    </tr>
		    <tr>
			    <td><input type='text' placeholder='Enter Txn No...' name='txn' id='inp'></td>
		    </tr>";
		}
		?>
		<tr>
			<td><input type="number" placeholder="Enter Amount..." name="amont" id="inp"></td>
		</tr>
		<tr>
			<td><input type="submit" name="submit" value="Balance Success" id="inp"></td>
		</tr>
	</table>
	</form>
</body>
</html>