<?PHP
	include'session.php';
	include'../connection.php';
	include'../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;
		}
		.selectHide{display:none;}
	</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 order_req WHERE Id='$id'";
	$result  = mysqli_query($con,$query);
	$data    = mysqli_fetch_array($result);
	$reqAmt  = $data['Amount'];
	$TxnNo   = $data['TxnNo'];
	$uid     = $data['Userid'];
	$ReqType = $data['ReqType'];
			
	$status="APPROVED";
?>

	<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>
		<script>
		</script>
		<div class="data-content">
				<div class="div1">
				    <form method="post">
					    <select name="Txn" id="txntype" onchange="showHideSelect()">
					        <option>BANKING</option>
					        <option>EXCHANGE</option>
					        <option>CASHPAID</option>
					    </select>
	    			    <select name="BankId" id="bank">
		    	            <?php
			                    $rowBank   = 0;
			                    $quBank    = "SELECT * FROM bank WHERE Status='ACTIVE'";
    			                $rebank    = mysqli_query($con,$quBank);
    			                $countBank = mysqli_num_rows($rebank);
	    		                while($databank = mysqli_fetch_array($rebank))
    		    	            {
	    	    	                $rowBank = $rowBank+1;
		        	                if($countBank==1)
		        	                {
			                            echo "<option value='".$databank['Id']."'>".$databank['BankName']." - ".$databank['AccName']."</option>";
		    	                    }
		    	                    else
		    	                    {
		    	                        if($rowBank==1)
		    	                        {
		    	                            echo "<option>--Select Bank--</option>";
		    	                        }
    		    	                    echo "<option value='".$databank['Id']."'>".$databank['BankName']." - ".$databank['AccName']."</option>";
	    	    	                }
		    	                }
			                ?>
    			        </select>
	    			    <select name="seller" id="SellerId2" onchange="getFirmBySellerId('--SELECT FIRM--')" class="selectHide">
					        <option>--SELECT SELLER--</option>
		    	            <?php
			                    $quSeller = "SELECT * FROM Seller WHERE Status='ACTIVE' ORDER BY Name";
    			                $reSeller = mysqli_query($con,$quSeller);
	    		                while($dataSeller=mysqli_fetch_array($reSeller))
    		    	            {
	    	    	                echo "<option value='".$dataSeller['Id']."'>".$dataSeller['Name']." - ".$dataSeller['City']."</option>";
		    	                }
			                ?>
    			        </select>
					    <select name="firm" id="FirmPlace2" class="selectHide">
					        <option>--SELECT FIRM--</option>
					    </select>
		                <input type="number" placeholder="Enter Amount..." name="amount" >
		                <input type="text" id="txnInp" placeholder="Enter Txn No..." name="txnNo">
		                <select name="TxnMode" id="mode">
		                    <option>NEFT</option>
		                    <option>IMPS</option>
		                    <option>RTGS</option>
		                    <option>Cash Deposit</option>
		                </select>
		                <input type="password" placeholder="Enter Password..." name="pwd" onfocusout="showBtn()" id="pswd">
		                <textarea name="remarks"></textarea>
		                <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']))
	{
		
		$TxnType = $_POST['Txn'];
		$BankId  = $_POST['BankId'];
		$seller  = $_POST['seller'];
		$firm    = $_POST['firm'];
		$amount  = $_POST['amount'];
		$txnNo   = $_POST['txnNo'];
		$TxnMode = $_POST['TxnMode'];
		$pwd     = $_POST['pwd'];
		$remarks = $_POST['remarks'];
		
		$adminPwd = "Sameer@7255";
		$respmsg  = "Approved By Admin";
		$AdminOpBal=getBal($userid);
		$flag=0;
		if($actualamount<=$AdminOpBal)
		{
			$quGetDupReq   = "SELECT * FROM order_req WHERE Id=$id";
			$reGetDupReq   = mysqli_query($con,$quGetDupReq);
			$dataGetDupReq = mysqli_fetch_array($reGetDupReq);
			$DupReqStatus  = $dataGetDupReq['S_Status'];
			
			if($DupReqStatus!="SETTELED")
			{
			    if($amount>$reqAmt || $amount<$reqAmt)
			    {
			        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)
			    {
				    $UsrOpBal=getBal($uid);
	                
				    mysqli_query($con,"START TRANSACTION");
				        				
					if($ReqType=="ADVANCE")
				    {
				        if($TxnType=="CASHPAID")
				        {
				                $SettleUpdate="UPDATE order_req SET S_Status='SETTELED', S_Date='$date', S_Time='$time', S_Type='$TxnType', TxnNo='Cash In Hand', Mode='CASH', Remarks='$remarks' 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($TxnType=="BANKING")
				        {
	    			        $BankOpBal = getBankBal($BankId);               
				            $BankClBal = $BankOpBal+$amount;               
				            $InsertTxn="INSERT INTO bank_wallet(BankId,Date,Time,OpBal,Txn,ClBal,TxnId,TxnType,TxnWith,BankRefNo,Remarks)
				            VALUES ('$BankId','$date','$time','$BankOpBal','$amount','$BankClBal','$id','Payment_Setteled','$uid','$txnNo','Payment_Setteled')";
    				        if(mysqli_query($con,$InsertTxn))
	    			        {
		    		            $SettleUpdate="UPDATE order_req SET BankId='$BankId', S_Status='SETTELED', S_Date='$date', S_Time='$time', S_Type='$TxnType', TxnNo='$txnNo', Mode='$TxnMode', Remarks='$remarks' 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($TxnType=="EXCHANGE")
						{
						    $InsertSellerTxn = "INSERT INTO seller_txn(Date,Time,SellerId,FirmId,Amount,TxnType,OrderReqId)
				            VALUES ('$date','$time','$seller','$firm','$amount','$TxnType','$id')";
    				        if(mysqli_query($con,$InsertSellerTxn))
	    			        {
	    			            $newSellerTxnId = mysqli_insert_id($con);
		    		            $SettleUpdate="UPDATE order_req SET SellerTxnId='$newSellerTxnId', S_Status='SETTELED', S_Date='$date', S_Time='$time', S_Type='$TxnType', Mode='TOPUP', Remarks='$remarks'  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
			{
				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>