<?PHP
	include'session.php';
	include'connection.php';
	include'../Retailers/functions.php';
	
?>
<html>
<head>
	<title>Payment Settle</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;
		}
		#sbmt{
			background:#6b5127;
			color:white;
			width:150px;
			float:right;
			margin-right:30px;
			font-size:20px;
			transition:1s;
			display:none;
		}
		input[type=submit]:hover{
			background:white;
			color:#6b5127;
			border:1px solid #6b5127;
			cursor:pointer;
			transition:1s;
		}
	</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';
		?>
				<?php
		$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'];
			
	$status="APPROVED";
	//$reqType="ADVANCE"
		?>

	<div class="content">
			<div class="head">
				<div class="title"><p>Payment Settle List :-</p></div>
				<div class="msg"><marquee><?php include'msg.php'; ?></marquee></msg>
			</div><hr>
		
		<div class="data-content">
				<div class="div1">
				    <form method="post">
					    <input type="number" placeholder="Enter Bank Charge..." name="bc">
		                <?php
		                    if($reqType=="ADVANCE")
		                    {
		                        echo "<select name='Bank' id='inp'>
			                            <option>--Select Bank--</option>";
			                            echo "<option>CASH IN HAND</option>";
			                
			                            $GetBank="SELECT * FROM bank WHERE Admin='SHOW'";
			                            $reGetbank=mysqli_query($con,$GetBank);
			                            while($dataGetbank=mysqli_fetch_array($reGetbank))
			                            {
			                                echo "<option value='".$dataGetbank['Id']."'>".$dataGetbank['Bankname']." - ".$dataGetbank['Accname']."</option>"; 
			                            }
			       
			                    echo "</select>
			                    <input type='text' placeholder='Enter Txn No...' name='txn'>";
		   
		                    }
		                    elseif($reqType=="EXCHANGE")
		                    {   
				                echo "<select name='PayTo'>
				                        <option>--Select Seller--</option>";
				                        $quGetSeller="SELECT * FROM Seller WHERE Status='Active' ORDER BY Name ASC";
				                        $reGetSeller=mysqli_query($con,$quGetSeller);
				                        while($dataGetSeller=mysqli_fetch_array($reGetSeller))
				                        {
				                            echo "<option>".$dataGetSeller['Name']."</option>";
				                        }
				                        echo "<option>Other</option>
				                    </select>
				                    
				                    <select name='TxnService'>
				                        <option>--Select Txn Service--</option>
				                        <option>Manage Differences</option>
				                        <option>Rech_API</option>
				                        <option>AIRTEL</option>
				                        <option>IDEA</option>
				                        <option>JIO</option>
				                        <option>VODAFONE</option>
				                        <option>Quick_API</option>
				                        <option>Kwik_API</option>
				                        <option>Unique_API</option>
				                        <option>DiziPay_API</option>
				                        <option>Other</option>
				                    </select>";
		                    }
		                ?>
		                <input type="number" placeholder="Enter Amount..." name="amount" >
		                <input type="password" placeholder="Enter Password..." name="pwd" onfocusout="showBtn()" id="pswd">
		                <input type="submit" name="submit" value="Settle" id="sbmt">
				    </form>
				</div>
			</div>
		</div>
	</div>
	<script>
	    function showBtn()
	    {
	        var pswd = document.getElementById("pswd").value;
	        if(pswd=="Sameer@7255")
	        {
	            document.getElementById("sbmt").style.display = "inline";
	        }
	        else
	        {
	            alert("Invalid Password");
	        }
	    }
	</script>
	<?php
	ini_set('memory_limit', '-1');

	
	
	if(isset($_POST['submit']))
	{
		$respmsg="Approved By Admin";
		$bankAmount=$_POST['amount'];
		$bankTxnNo=$_POST['txn'];
		$bankcharge=$_POST['bc'];
		$BankId=$_POST['Bank'];
	
		$PayTo=$_POST['PayTo'];
		$TxnService=$_POST['TxnService'];
		$pwd=$_POST['pwd'];
		
		$adminPwd="Sameer@7255";
		
		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['SettleStatus'];
			
			if($DupReqStatus!="SETTELED")
			{
			    if($amount>$bankAmount || $amount<$bankAmount)
			    {
			        echo "<center><h3 style='color:red;'>Request Amount Not Matched !</h3></center>";
			    }
			    elseif($pwd!=$adminPwd)
			    {
			        echo "<center><h3 style='color:red;'>Invalid Password !</h3></center>";
			    }
			    else
			    {
			        $flag=1;
			    }
			    
			    if($flag==1)
			    {
				    $User_Op_Bal=getBal($uid);
	                
				    mysqli_query($con,"START TRANSACTION");
				        				
					if($reqType=="ADVANCE")
				    {
				        if($BankName=="CASH IN HAND")
				        {
				                $SettleUpdate="UPDATE paymentreq SET SettleStatus='SETTELED', SettleDate='$date', Txnnumber='$bankTxnNo', Bankname='$BankName' WHERE Id=$id";
			    	            if(mysqli_query($con,$SettleUpdate))
				                {
                                    if(mysqli_query($con,"COMMIT"))
                                    {
                                        echo "<div class='msg-div'>
							                <form method='post'>
							                    <h3>Advance Payment Settled Successfully !</h3><br>
							                    <i class='fa fa-check-circle' aria-hidden='true' id='okIcon'></i><br><br>
							                    <a href='Payment_Settle_List.php'  class='btnPopup'>OK</a><br><br>
							                </form>
						                    </div>";  
                                    }
                                    else
                                    {
                                        if(mysqli_query($con,"ROLLBACK"))
				    					{
				                            echo "<div class='msg-div'>
			                                        <form method='post'>
				                                        <h3>Rollback For Not Commit !</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"))
								    {
				                        echo "<div class='msg-div'>
			                                    <form method='post'>
				                                    <h3>Rollback For Settlement Query !</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
								    {
									    echo "error";
									    mysqli_query($con,"ROLLBACK");
								    }
							    }
				        }
				        else
				        {
	    			        $quBank="SELECT * FROM bank WHERE Id='$BankId'";
		    		        $reBank=mysqli_query($con,$quBank);
			    	        $dataBank=mysqli_fetch_array($reBank);
				            $BankName=$dataBank['Bankname'];
				                            
				            $InsertTxn="INSERT INTO Bank_Account_Txn(Date,Time,Bank_Id,Bank_Name,Txn,Txn_Id,BankRefNo,Disc,Txn_For)
				            VALUES ('$date','$time','$BankId','$BankName','$amount','$id','$bankTxnNo','Payment_Setteled','$uid')";
    				        if(mysqli_query($con,$InsertTxn))
	    			        {
		    		            $SettleUpdate="UPDATE paymentreq SET SettleStatus='SETTELED', SettleDate='$date', Txnnumber='$bankTxnNo', BankId='$BankId', Bankname='$BankName' WHERE Id=$id";
			    	            if(mysqli_query($con,$SettleUpdate))
				                {
                                    if(mysqli_query($con,"COMMIT"))
                                    {
                                        echo "<div class='msg-div'>
							                <form method='post'>
							                    <h3>Advance Payment Settled Successfully !</h3><br>
							                    <i class='fa fa-check-circle' aria-hidden='true' id='okIcon'></i><br><br>
							                    <a href='Payment_Settle_List.php'  class='btnPopup'>OK</a><br><br>
							                </form>
						                    </div>";  
                                    }
                                    else
                                    {
                                        if(mysqli_query($con,"ROLLBACK"))
				    					{
				                            echo "<div class='msg-div'>
			                                        <form method='post'>
				                                        <h3>Rollback For Not Commit !</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"))
								    {
				                        echo "<div class='msg-div'>
			                                    <form method='post'>
				                                    <h3>Rollback For Settlement Query !</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
								    {
									    echo "error";
									    mysqli_query($con,"ROLLBACK");
								    }
							    }
	    			        }
						}
					}
					elseif($reqType=="EXCHANGE")
					{
					    include'connection.php';
					    
					    $quGetTxnId="SELECT * FROM wallet WHERE Userid='$userid' AND TxnType='Generate_Amount' ORDER BY Id DESC";
	                    $reGetTxnId=mysqli_query($con,$quGetTxnId);
	                    $dataGetTxnId=mysqli_fetch_array($reGetTxnId);
	                    $OldTxnId=$dataGetTxnId['TxnId'];
	                    $NewTxnId=$OldTxnId+1;
	
					    $Admin_Op_Bal=getBal($userid);
				        $Admin_Cl_Bal=$Admin_Op_Bal+$amount;
				        
				        $BankName="Pay LLP Fund";
				        $rmark="EXCHANGE LAPU BALANCE";
				        
				        $query="INSERT INTO wallet(Userid,OpBal,Txn,ClBal,TxnType,TxnWith,TxnId,TxnBy,TxnFor,Remarks,Date,Time)
				        VALUES ('$userid','$Admin_Op_Bal','$amount','$Admin_Cl_Bal','Generate_Amount','$PayTo','$NewTxnId','$BankName','$TxnService','$rmark','$date','$time')";
				
    				    if(mysqli_query($con,$query))
    				    {
    				        $query12="UPDATE admin SET Balance='$Admin_Cl_Bal' WHERE Userid='$userid'";
    				        
							$SettleUpdate="UPDATE paymentreq SET SettleStatus='SETTELED', SettleDate='$date', Txnnumber='$PayTo', Bankname='$rmark' WHERE Id=$id";
				            if(mysqli_query($con,$SettleUpdate))
				            {
				                if(mysqli_query($con,"COMMIT"))
				                {
    				                echo "<div class='msg-div'>
							                <form method='post'>
							                    <h3>Exchange Payment Settled Successfully !</h3><br>
							                    <i class='fa fa-check-circle' aria-hidden='true' id='okIcon'></i><br><br>
							                    <a href='Payment_Settle_List.php'  class='btnPopup'>OK</a><br><br>
							                </form>
						                </div>";
				                }
                                else
                                {
                                    if(mysqli_query($con,"ROLLBACK"))
									{
				                        echo "<div class='msg-div'>
			                                    <form method='post'>
				                                    <h3>Rollback For Not Commit !</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
			{
				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>";
		}
		
	}
?>
	<?php
		require'footer.php';
	?>
</body>
</html>