<?php
	session_start();
	if($_SESSION['adm'])
	{
		$userid= $_SESSION['adm'];
	}
	else
	{
		header('location:../login.php');
	}
?>
<?php
	include'connection.php';
		
		$query8="select * from wallet where Userid='$userid' order by Id desc";
		$result8=mysqli_query($con,$query8);
		$data8=mysqli_fetch_array($result8);
		$currentbal= $data8['Balance'];
?>
<?php
	include'connection.php';
	$id=$_GET['id'];
	$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'];
	
	        date_default_timezone_set("asia/Kolkata");
			$datetime= Date("Y-m-d - h:i:s A");
			$time= Date("h:i:s A");
			$date= Date("Y-m-d");
			
	$status="APPROVED";
	
	if(isset($_POST['submit']))
	{
		$respmsg="Approved By Admin";
		$bankAmount=$_POST['amont'];
		$bankTxnNo=$_POST['txn'];
		$bankcharge=$_POST['bc'];
		if(empty($bankcharge))
		{
			$bankcharge=0;
		}
			$actualamount=$amount-$bankcharge;
		if($actualamount<=$currentbal)
		{
			$quGetDupReq="select * from paymentreq where Id=$id";
			$reGetDupReq=mysqli_query($con,$quGetDupReq);
			$dataGetDupReq=mysqli_fetch_array($reGetDupReq);
			$DupReqStatus=$dataGetDupReq['Active'];
			if($DupReqStatus==0)
			{
			    if($amount!==$bankAmount)
			    {
			        echo "<center><h3 style='color:red;'>Request Amount Not Matched !</h3></center>";
			    }   
				elseif($Txnnumber!==$bankTxnNo)
				{
					echo "<center><h3 style='color:red;'>Transaction Number Not Matched !</h3></center>";
				}
				else
				{
					$query1="update paymentreq set Responsemsg='$respmsg', Bankcharge='$bankcharge', Status='$status', Active='1' where Id=$id";
					if(mysqli_query($con,$query1))
					{
						$query2="insert into transaction (Fromid, Toid, Credit)values('$userid', '$uid', '$actualamount')";
						if(mysqli_query($con,$query2))
						{
							$querytxn="select * from transaction order by Id desc";
							$resulttxn=mysqli_query($con,$querytxn);
							$datatxn=mysqli_fetch_array($resulttxn);
							$lasttxnid=$datatxn['Id'];
							
							$query3="select * from wallet where Userid='$userid' order by Id desc";
							$result3=mysqli_query($con,$query3);
							$data3=mysqli_fetch_array($result3);
							$fidoldbal=$data3['Balance'];
							$finlebal=$fidoldbal-$actualamount;
							
							$query4="insert into wallet (Userid,Openingbalance,Debit,Remarks,Balance,Date,Time,Paymentto,Transactiontype,Paynrechid)
							values('$userid','$fidoldbal','$actualamount','$respmsg','$finlebal','$date','$time','$uid','Payment_Transfer','$lasttxnid')";
							if(mysqli_query($con,$query4))
							{
								$query5="select * from wallet where Userid='$uid' order by Id desc";
								$result5=mysqli_query($con,$query5);
								$data5=mysqli_fetch_array($result5);
								$fidoldbal5=$data5['Balance'];
								$finlebal5=$fidoldbal5+$actualamount;
								
								$query6="insert into wallet (Userid,Openingbalance,Credit,Remarks,Balance,Date,Time,Paymentfrom,Transactiontype,Paynrechid)
								values('$uid','$fidoldbal5','$actualamount','$respmsg','$finlebal5','$date','$time','$userid','Payment_Transfer','$lasttxnid')";
								if(mysqli_query($con,$query6))
								{
									$sub=substr($uid,"0","2");
									if($sub==50)
									{
										$query12="update retailer set Balance='$finlebal5' 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'];
										
										
									}
									elseif($sub==20)
									{
										$query12="update distributer set Balance='$finlebal5' 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'];
									}
									elseif($sub==30)
									{
										$query12="update masterdis set Balance='$finlebal5' where Userid='$uid'";
										mysqli_query($con,$query12);
										
										$quGetUser="select * from masterdis where Userid='$uid'";
										$reGetUser=mysqli_query($con,$quGetUser);
										$data=mysqli_fetch_array($reGetUser);
										$Mobile=$data['Mobile'];
									}
									echo "<center><h3 style='color:green;'>Update Payment Successfully !</h3></center>";
														$msg="Rs.".$actualamount." Credited In Your LLP Wallet On ".$date." ".$time.". Now Your Balance Is Rs. ".$finlebal5." Call For Any Query- 9897095971";
														$curl = curl_init();
														curl_setopt_array($curl, array(
														CURLOPT_URL => "http://sms2.kwikapi.com/sendSMS?username=sameermansuri111&message=".urlencode($msg)."&sendername=LLPRCH&smstype=TRANS&numbers=$Mobile&apikey=dc086ecd-1a22-4928-9e4e-69703230fd4e",
														CURLOPT_RETURNTRANSFER => true,
														CURLOPT_ENCODING => "",
														CURLOPT_MAXREDIRS => 10,
														CURLOPT_TIMEOUT => 0,
														CURLOPT_FOLLOWLOCATION => false,
														CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
														CURLOPT_CUSTOMREQUEST => "GET",
														));
		
														$response = curl_exec($curl);
														$err = curl_error($curl);
		
														curl_close($curl);
		
														if ($err)
														{
															echo "cURL Error #:" . $err;
														}
														else
														{
															$response;
															$data2=json_decode($response);
													
														}
								}
								else
								{
									echo "error";
								}
							}
						}
					}
				}
			}
			else
			{
				echo "<center><h3 style='color:red;'>This Request Proccesed Already !</h3></center>";
			}
		}
		else
		{
			echo "<center><h3 style='color:red;'>Insufficiant Balance !</h3></center>";
		}
		
	}
?>
<html>
<head>

</head>
<body>
	<form method="post">
	<table border="0" cellpadding="10">
		<tr>
			<td>Bank Charge : </td><td><input type="text" name="bc"></td>
		</tr>
		<tr>
			<td>Request Amount : </td><td>*******</td>
		</tr>
		<tr>
			<td>Enter Amount : </td><td><input type="number" name="amont"></td>
		</tr>
		<tr>
			<td>Transaction No. : </td><td>************</td>
		</tr>
		<tr>
			<td>Enter Txn No. : </td><td><input type="text" name="txn"></td>
		</tr>
		<tr>
			<td></td><td><input type="submit" name="submit" value="Balance Success"></td>
		</tr>
	</table>
	</form>
</body>
</html>