<?PHP
	include'session.php';
	include'connection.php';
	include'../Retailers/functions.php';
	
?>
<html>
<head>
	<title>Payment Approve</title>
	<!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    
	<link rel="icon" href="images/logo.png" type="image/gif" sizes="16x16">
	
	<!-- Admin CSS File Link -->
	<link rel="stylesheet" href="css/style.css">
	
	<!-- User JAVA SCRIPT File Link -->
	<script type="text/javascript" src="js/script.js"></script>

	
	<!-- Font Awesome Icone -->
	<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
	
	<style>
		.div1, .div3{
			background:#f7f7f7;
			width:50%;
			height:auto;
			float:left;
			text-align:center;
			padding:10px;
		}
		.div1 table,.div3 table{
			width:94%;
			text-align:center;
			font-size:13px;
			border-collapse:collapse;
			margin:10px;
			padding:5px;
		}
		.div1 hr, div3 hr{
			margin:5px 10px;
			padding:0 10px;
		}
		table th{
			background:#6b5127;
			color:white;
			height:40px;
			border:solid white 1px;
			padding:4px 8px;
		}
		table td{
			background:white;
			height:40px;
			border:solid black 1px;
			padding:4px 8px;
		}
		.div1 hr, .div3 hr{width:94%;}
		.div1 input,select, textarea{
			width:90%;
			height:45px;
			margin:5px 10px;
			padding:2px 10px;
			border-radius:5px;
			border:1px solid #6b5127;
			font-size:18px;
		}
		
		
		textarea{
			height:100px;
			padding:5px 10px;
		}
		input[type=submit]{
			background:#6b5127;
			color:white;
			font-size:30px;
			transition:1s;
		}
		input[type=submit]:hover{
			background:white;
			color:#6b5127;
			border:1px solid #6b5127;
			cursor:pointer;
			transition:1s;
		}
		.div3{width:96%; text-align:left;}
		.div3 input, .div3 select{
			width:120px;
			height:35px;
			margin:10px 5px;
			padding:2px 5px;
			border-radius:5px;
			border:1px solid #6b5127;
			font-size:13px;
		}
		@media(max-width:768px)
		{
			.div1{
				width:100%;
				height:auto;
			}
			.nav,.container{
				height:auto;
			}
		}
	</style>
	<link rel="stylesheet" href="css/mediaStyle.css">
</head>
<body>
	<!--Header Divison-->
	<?php
		require'header.php';
	?>
	
	<!--Container Division-->
	<div class="container">
	    <?php include'menu.php'; ?>
		<div class="content">
			<div class="head">
				<div class="title"><p>Payment Approve :-</p></div>
				<div class="msg"><marquee><?php include'msg.php'; ?></marquee></msg>
			</div><hr>
			
<?php
	ini_set('memory_limit', '-1');

	$reqId=$_GET['reqId'];
	
	if(empty($reqId))
	{
	    echo "<h3 align='center' style='color:red;'>Request Id Not Found !</h3>";
	}
	
	$query="SELECT * FROM paymentreq WHERE Id='$reqId'";
	$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=$reqId";
			$reGetDupReq=mysqli_query($con,$quGetDupReq);
			$dataGetDupReq=mysqli_fetch_array($reGetDupReq);
			$DupReqStatus=$dataGetDupReq['Active'];
			
			if($DupReqStatus==0)
			{
			    if($reqType2!=$reqType)
			    {
			        $msg= "Request Type Not Matched !";
			        echo "<div class='msg-div'>
			                <form method='post'>
				                <h3>".$msg."</h3><br>
				                <i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
				                <button type='submit' class='btnPopup'>OK</button><br><br>
			                </form>
		                </div>";
			    }
			    elseif($reqType=="BANKING")
			    {
			        if($amount>$bankAmount || $amount<$bankAmount)
			        {
			            $msg= "Request Amount Not Matched !";
			            echo "<div class='msg-div'>
			                    <form method='post'>
				                    <h3>".$msg."</h3><br>
				                    <i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
				                    <button type='submit' class='btnPopup'>OK</button><br><br>
			                    </form>
		                    </div>";
			        }  
				    elseif($BankName2!==$MyBankName)
				    {
					    $msg= "Bank Name Not Matched !";
			            echo "<div class='msg-div'>
			                    <form method='post'>
				                    <h3>".$msg."</h3><br>
				                    <i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
				                    <button type='submit' class='btnPopup'>OK</button><br><br>
			                    </form>
		                    </div>";
				    }  
				    elseif($Txnnumber!==$bankTxnNo)
				    {
					    $msg= "Transaction Number Not Matched !";
			            echo "<div class='msg-div'>
			                    <form method='post'>
				                    <h3>".$msg."</h3><br>
				                    <i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
				                    <button type='submit' class='btnPopup'>OK</button><br><br>
			                    </form>
		                    </div>";
				    }
				    else
				    {
				        $flag=1;
				    }
			    }
			    else
			    {
			        if($amount>$bankAmount || $amount<$bankAmount)
			        {
			            $msg= "Request Amount Not Matched !";
			            echo "<div class='msg-div'>
			                    <form method='post'>
				                    <h3>".$msg."</h3><br>
				                    <i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
				                    <button type='submit' class='btnPopup'>OK</button><br><br>
			                    </form>
		                    </div>";
			        }
			        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='$userid' WHERE Id=$reqId";
					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','$reqId','PAYMENT_APPROVED','$date','$time','$userid')";
							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','$reqId','Approved By Admin','$date','$time','$userid')";
								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','$reqId','Payment_Approved','$uid')";
				                            if(mysqli_query($con,$InsertTxn))
				                            {
				                                $SettleUpdate="UPDATE paymentreq SET SettleStatus='SETTELED', SettleDate='$date' WHERE Id=$reqId";
				                                if(mysqli_query($con,$SettleUpdate))
				                                {
                                                    if(mysqli_query($con,"COMMIT"))
                                                	{
                                                	    $extraMailMsg="Your Payment Request [ ".$reqId." ] has been Approved.";
    													$Mmsg=$extraMailMsg.", ".$msg;
	    												$subject="Payment_Approved";
		    											$to=$UsrMailId;
			    										include'../mail.php';
			    										
                                                	    echo "<div class='msg-div'>
							                                    <form method='post'>
							                                        <h3>Payment Approved Successfully !</h3><br>
							                                        <i class='fa fa-check-circle' aria-hidden='true' id='okIcon'></i><br><br>
							                                        <a href='topup_reports.php' class='btnPopup'>OK</a><br><br>
							                                    </form>
						                                    </div>";  
                                                    }
                                                    else
                                                    {
                                                        if(mysqli_query($con,"ROLLBACK"))
												        {
				                                            $msg= "Rollback For Not Commit !";
			                                                echo "<div class='msg-div'>
			                                                        <form method='post'>
				                                                        <h3>".$msg."</h3><br>
				                                                        <i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
				                                                        <button type='submit' class='btnPopup'>OK</button><br><br>
			                                                        </form>
		                                                        </div>";
											            }
                                                    }
				                                }
				                                else
				                                {
				                                    if(mysqli_query($con,"ROLLBACK"))
												    {
				                                        $msg= "Rollback For Settlement Query !";
			                                            echo "<div class='msg-div'>
			                                                    <form method='post'>
				                                                    <h3>".$msg."</h3><br>
				                                                    <i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
				                                                    <button type='submit' class='btnPopup'>OK</button><br><br>
			                                                    </form>
		                                                    </div>";
											        }
				                                }
				                            }
				                            else
				                            {
				                                if(mysqli_query($con,"ROLLBACK"))
												{
				                                        $msg= "Rollback For Bank Txn !";
			                                            echo "<div class='msg-div'>
			                                                    <form method='post'>
				                                                    <h3>".$msg."</h3><br>
				                                                    <i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
				                                                    <button type='submit' class='btnPopup'>OK</button><br><br>
			                                                    </form>
		                                                    </div>";
											    }
				                            }
				                        }
				                        else
				                        {
				                            if(mysqli_query($con,"COMMIT"))
                                            {
                                                $extraMailMsg="Your Payment Request [ ".$reqId." ] has been Approved.";
    											$Mmsg=$extraMailMsg.", ".$msg;
	    										$subject="Payment_Approved";
		    									$to=$UsrMailId;
			    								include'../mail.php';
                                                
                                                echo "<div class='msg-div'>
							                            <form method='post'>
							                                <h3>Payment Approved Successfully !</h3><br>
							                                <i class='fa fa-check-circle' aria-hidden='true' id='okIcon'></i><br><br>
							                                <button type='submit' class='btnPopup'>OK</button><br><br>
							                            </form>
						                            </div>";
                                            }
												        
				                        }
				                    }
				                    else
				                    {
										if(mysqli_query($con,"ROLLBACK"))
										{
										                $msg= "Rollback For Error In Sum Query !";
			                                            echo "<div class='msg-div'>
			                                                    <form method='post'>
				                                                    <h3>".$msg."</h3><br>
				                                                    <i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
				                                                    <button type='submit' class='btnPopup'>OK</button><br><br>
			                                                    </form>
		                                                    </div>";
										    
									    }
				                    }
								}
								else
								{
									if(mysqli_query($con,"ROLLBACK"))
									{
									    $msg= "Something Error !";
			                            echo "<div class='msg-div'>
			                                    <form method='post'>
				                                    <h3>".$msg."</h3><br>
				                                    <i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
				                                    <button type='submit' class='btnPopup'>OK</button><br><br>
			                                    </form>
		                                      </div>";
									}
								}
							}
						}
					}
			    }
			}
			else
			{
				$msg= "This Request Proccesed Already !";
			                            echo "<div class='msg-div'>
			                                    <form method='post'>
				                                    <h3>".$msg."</h3><br>
				                                    <i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
				                                    <button type='submit' class='btnPopup'>OK</button><br><br>
			                                    </form>
		                                      </div>";
			}
		}
		else
		{
			$msg= "Insufficiant Balance !";
			                            echo "<div class='msg-div'>
			                                    <form method='post'>
				                                    <h3>".$msg."</h3><br>
				                                    <i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
				                                    <button type='submit' class='btnPopup'>OK</button><br><br>
			                                    </form>
		                                      </div>";
		}
		
	}
?>
			<div class="data-content">
				<div class="div1">
				    <form method="post">
					    <input type="number" placeholder="Enter Bank Charge..." name="bc">
				        <select name="reqType" id="inp">
		                    <option>--Select Type--</option>
		                    <option>BANKING</option>
		                    <option>ADVANCE</option>
		                    <option>EXCHANGE</option>
		                </select>
		                <?php
		                    if($reqType=="BANKING" )
		                    {
		                        echo "<select name='BankName' id='inp'>
			                            <option>--Select Bank--</option>";
			                
			                            $GetBank="SELECT * FROM bank WHERE Admin='SHOW'";
			                            $reGetbank=mysqli_query($con,$GetBank);
			                            while($dataGetbank=mysqli_fetch_array($reGetbank))
			                            {
			                                echo "<option>".$dataGetbank['Bankname']."</option>"; 
			                            }
			       
			                    echo "</select>
			                    <input type='text' placeholder='Enter Txn No...' name='txn'>";
		   
		                    }
		                ?>
		                <input type="number" placeholder="Enter Amount..." name="amont" >
		                <input type="submit" name="submit" value="Approve">
				    </form>
				</div>
			</div>
		</div>
	</div>
	<?php
		require'footer.php';
	?>
</body>
</html>