<?php
	session_start();
	
	ob_start();
	if($_SESSION['did'])
	{
		$userid= $_SESSION['did'];
	}
	else
	{
		header('location:../login.php');
	}
?>
<?php
	include'connection.php';
	
	$quGetEwalletId="select * from RetBal where Userid='$userid'";
    $reGetEwalletId=mysqli_query($con,$quGetEwalletId);
    $dataGetEwalletId=mysqli_fetch_array($reGetEwalletId);
    $EwalletId=$dataGetEwalletId['Walletid'];
    
	$queryNewBal="select * from wallet where Userid='$userid' and Id>=$EwalletId";
	$resultNewBal=mysqli_query($con,$queryNewBal);
	$newbal=0;
	while($dataNewBal=mysqli_fetch_array($resultNewBal))
	{
	    $Retbal= $dataNewBal['Txn'];
	    $newbal=$newbal+$Retbal;
	}
	
	$quDisname="select * from distributer where Userid='$userid'";
	$reDisName=mysqli_query($con,$quDisname);
	$dataDisName=mysqli_fetch_array($reDisName);
	$disName=$dataDisName['Name'];
?>
<html>
<title>Credit</title>
<head>
<style>
body{background:url('bg.jpg'); background-size:cover;
width:100%; height:700px; margin:0px;}

.head{background:; width:1350px; height:100px; margin:0px;}
#p1{font-size:34px; float:left; Margin:10 0 0 45px; color:#996420; font-weight:bold;}
#p2{font-size:20px; float:left; margin:28 0 0 -8px; color:#eaa040; font-family:Courier;}
#p3{font-size:15px; font-family:Comic Sans MS; float:left; color:#996420; margin-top:48px; margin-left:-153px;}
.h2{background:white; width:400px; height:70px; margin:10 200px;}
#link1{float:right; margin:10 30 0 0px; font-size:20px; text-decoration:none;
 color:white; background:red; padding:5px; border-radius:4px;}
 #link2{float:right; margin:10 30 0 0px; font-size:20px; background:white;color:Green; padding:5px; border-radius:4px;}
#link1:hover{ padding:5px; border-radius:4px; color:black;}
	
.menu{background:#996420; height:40px; width:1300px; margin:0 0 0 20px; padding:0;}
.menu ul{padding:0; margin:0; position:relative; display:inline-block;}
.menu ul li{list-style:none; float:left; width:170px; line-height:40px;
	text-align:center; font-size:15px; margin:0; padding:0; color:white;}
.menu ul li ul {display:none;}
.menu ul li:hover{background:#d1994a; cursor:pointer;}
.menu ul li:hover ul {display:block; z-index:2;}
.menu ul li:hover ul li{background:#d1994a; border-top:solid white 0.1px;}
.menu ul li ul li:hover{background:#996420; color:white;}
.menu ul li ul li:hover a{ color:white;}
.menu ul li a{ text-decoration:none; color:white;}
 #a1{border:none; color:white;}
 .menu{letter-spacing:1px;}
 #bal:hover{background:#996420; cursor:text;}
 
 
.content{background:; height:;}
 .content h3{margin:10 20px; font-size:20px;}
 .content #tb1{margin:10 0 0 150px;}
 .content #tb1 #tbp{float:right; margin:10 20px;}
 .viewstate{background:#996420; width:97%; height:40px; margin:10 0 0 20px;}
 .viewstate h3{line-height:40px; margin-left:20px; color:white;}
 #tb1{width:450px; margin:10 20px; border-collapse:collapse; font-size:18px}
 #td1{background:#fef6ec;}
 #td2{background:#e8edfd; padding:0 0 0 10px;}
 #th1{background:green; color:white; font-size:25px; font-family:sans-serif;}
 #th2{background:green; color:white; font-size:25px; font-family:sans-serif;}
 #inp{width:100px;}
 #btn{margin:10 0 0 520px; padding:5px; width:80px; background:green; color:white; cursor:pointer;}
 #actionmsg{border:2px solid #4779b6; width:30%; padding:10 20px; font-size:18px; font-weight:bold; color:#4779b6; font-family:sans-serif; background:white; margin-left:50px; border-radius:5px;}
 #actionmsg2{border:2px solid #4779b6; width:30%; padding:10 20px; font-size:18px; font-weight:bold; color:red; font-family:sans-serif; background:white; margin-left:50px; border-radius:5px;}

#loaddiv{ width:1580px; height:100%; z-index:3; display:none; position:absolute; margin:0px; padding:0; float:right;}
#loader{position:absolute; top:50%; left:50%; transform:translate(-50%,-50%); width:300px;  display:none; z-index:4; float:right;}
 
.foot{background:#996420; width:100%; height:50px; float:left;
text-align:center; margin-top:10px;}
 
 
</style>
</head>
<body>
    <div id="loaddiv">
	     <img src="../loader2.gif" id="loader"></img>
	</div>
	 <script>
	    window.on(load, hide())
	    function show()
	    {
	        var d = document.getElementById("loaddiv");
	        d.style.display = "block";
	        var i = document.getElementById("loader");
	        i.style.display="block";
	    }
	    
	    function hide()
	    {
	        var d = document.getElementById("loaddiv");
	        d.style.display = "none";
	        var i = document.getElementById("loader");
	        i.style.display="none";
	    }
	 </script>
	<div class="head">
		<p id="p1">LLP</p>
		<p id="p2">Recharge</p>
		<p id="p3">The Complete Solution</p>
		<a id="link1" href="logout.php">Log Out</a>
	</div>
	<div class="menu">
	    <?php include'menu.php'; ?>
	</div>
	<div class="content">
		<?php
			include'connection.php';
			$uid=$_GET['id'];
	
        	$quGetEwalletIdU="select * from RetBal where Userid='$uid'";
            $reGetEwalletIdU=mysqli_query($con,$quGetEwalletIdU);
            $dataGetEwalletIdU=mysqli_fetch_array($reGetEwalletIdU);
            $EwalletIdU=$dataGetEwalletIdU['Walletid'];
    
			$resultTo=$pdoCon->query("SELECT * FROM retailer WHERE Userid='$uid'");
			$dataTo=$resultTo->fetch();
			$retaName=$dataTo['Name'];
			
			date_default_timezone_set("asia/kolkata");
			$date=date("Y-m-d");
			$time=date("h:i:s A");
			if(isset($_POST['submit']))
			{
				$inputbal=$_POST['addbal'];
				$rmark=$_POST['rmark'];
				$moduleBal=$inputbal%500;
				
				    $quDisOpBal="select * from wallet where Userid='$userid' and Id>=$EwalletId";
	                $reDisOpBal=mysqli_query($con,$quDisOpBal);
	                $OldDisBal=0;
	                while($dataDisOpBal=mysqli_fetch_array($reDisOpBal))
	                {
	                    $OldDisTxn= $dataDisOpBal['Txn'];
	                    $OldDisBal=$OldDisBal+$OldDisTxn;
	                }
	                
	                $quRetOpBal="select * from wallet where Userid='$uid' and Id>=$EwalletIdU";
	                $reRetOpBal=mysqli_query($con,$quRetOpBal);
	                $OldRetBal=0;
	                while($dataRetOpBal=mysqli_fetch_array($reRetOpBal))
	                {
	                    $OldRetTxn= $dataRetOpBal['Txn'];
	                    $OldRetBal=$OldRetBal+$OldRetTxn;
	                }
				    $BothOldBal=$OldDisBal+$OldRetBal;
				
				if($inputbal>$newbal)
				{
					echo "<p id='actionmsg2'>Insufficiant Balance !</p>";
				}
				else
				{	
				    
				    mysqli_query($con,"START TRANSACTION");
				    
				    $sql1="INSERT INTO transaction(Fromid,Toid,Credit,Debit) VALUES(?,?,?,?)";
					if($pdoCon->prepare($sql1)->execute([$userid,$uid,$inputbal,$inputbal]))
					{
							$paymentid=$pdoCon->lastInsertId();
							
	                    	$sqlDis="insert into wallet(Userid,Txn,Debit,Date,Time,Paymentto,Paytoname,Transactiontype,Paynrechid,Remarks)
							VALUES('$userid','-$inputbal','$inputbal','$date','$time','$uid','$retaName','Payment_Transfer','$paymentid','$rmark')";
							if(mysqli_query($con,$sqlDis))
							{	
								$DisEwalletId = mysqli_insert_id($con);
								$queryNewBal="select * from wallet where Userid='$userid' and Id>=$EwalletId";
	                            $resultNewBal=mysqli_query($con,$queryNewBal);
	                            $newbal=0;
	                            while($dataNewBal=mysqli_fetch_array($resultNewBal))
	                            {
	                                $Retbal= $dataNewBal['Txn'];
	                                $newbal=$newbal+$Retbal;
	                            }
	                            $OpRetBal=$newbal+$inputbal;
	                            $queryW="update wallet set Openingbalance='$OpRetBal', Balance='$newbal' where Id='$DisEwalletId'";
								mysqli_query($con,$queryW);
								$quUpDis="update distributer set Balance='$newbal' where Userid='$userid'";
								mysqli_query($con,$quUpDis);
								
										$sql6="insert into wallet(Userid,Txn,Credit,Date,Time,Paymentfrom,Payfromname,Transactiontype,Paynrechid)
										VALUES('$uid','$inputbal','$inputbal','$date','$time','$userid','$disName','Payment_Received','$paymentid')";
										if(mysqli_query($con,$sql6))
										{
										    $ewalletid=mysqli_insert_id($con);
										    
										    $query8="select * from wallet where Userid='$uid' and Id>=$EwalletIdU";
	                                        $result8=mysqli_query($con,$query8);
	                                        $newbal2=0;
	                                        while($data8=mysqli_fetch_array($result8))
	                                        {
	                                            $OldTxn= $data8['Txn'];
	                                            $newbal2=$newbal2+$OldTxn;
	                                        }
	                                        
	                                        $queryW="update wallet set Balance='$newbal2' where Id='$ewalletid'";
											mysqli_query($con,$queryW);
										    $sql7 ="update retailer set Balance='$newbal2' where Userid='$uid'";
											if(mysqli_query($con,$sql7))
											{
												$quDisClBal="select * from wallet where Userid='$userid' and Id>=$EwalletId";
                            	                $reDisClBal=mysqli_query($con,$quDisClBal);
	                                            $NewDisBal=0;
	                                            while($dataDisClBal=mysqli_fetch_array($reDisClBal))
	                                            {
	                                                $NewDisTxn= $dataDisClBal['Txn'];
	                                                $NewDisBal=$NewDisBal+$NewDisTxn;
	                                            }
	                
	                                            $quRetClBal="select * from wallet where Userid='$uid' and Id>=$EwalletIdU";
	                                            $reDisClBal=mysqli_query($con,$quRetClBal);
	                                            $NewRetBal=0;
	                                            while($dataRetClBal=mysqli_fetch_array($reDisClBal))
	                                            {
	                                                $NewRetTxn= $dataRetClBal['Txn'];
	                                                $NewRetBal=$NewRetBal+$NewRetTxn;
	                                            }
				                                $BothNewBal=$NewDisBal+$NewRetBal;
				                                if(floor($BothOldBal)==floor($BothNewBal))
				                                {
												    echo "<p id='actionmsg'>Balance Update Successfully !</p>";
                                                	mysqli_query($con,"COMMIT");
                                                	header( "refresh:5; url=credit.php?id=$uid" );
				                                }
				                                else
				                                {
				                                    echo "<error query Sum>";
												    mysqli_query($con,"ROLLBACK"); 
				                                }
											}
											else
											{
												echo "<error query 7>";
												mysqli_query($con,"ROLLBACK");
											}
										}
										else
										{
											echo "<error query 6>";
											mysqli_query($con,"ROLLBACK");
										}
									}
									else
									{
										echo "error in query 5";
										mysqli_query($con,"ROLLBACK");
									}
						}
						else
						{
							echo "<error query 2>";
							mysqli_query($con,"ROLLBACK");
						}
				}
				mysqli_close($con);
			}
		?>

		<h3>Add Balance</h3><hr>
	<form method="post">
<?php
	include'connection.php';
    $querygetname="select * from retailer where Userid='$uid'";
	$resultgetname=mysqli_query($con,$querygetname);
	$datagetname=mysqli_fetch_array($resultgetname);
	$username=$datagetname['Name'];
	$address=$datagetname['Address'];
	
	echo"<table border='1' cellpadding='5' id='tb1'>";
		echo "<tr>";
		    echo "<th id='th1'><p id='tbp'>My Balance : </p></td>";
			echo "<th id='th2'>".floor($newbal)." Rs.</td>";
		echo "</tr>";
		echo "<tr>";
		 echo "<td id='td1'><p id='tbp'>Client Balance :</p></td>";
		
			$query8="select * from wallet where Userid='$uid' and Id>=$EwalletIdU";
	        $result8=mysqli_query($con,$query8);
	        $newbal2=0;
	        while($data8=mysqli_fetch_array($result8))
	        {
	            $OldTxn= $data8['Txn'];
	            $newbal2=$newbal2+$OldTxn;
	        }
		
		 echo "<td id='td2'>".floor($newbal2)." Rs.</td>";
		echo "</tr>";
		echo "<tr>";
			echo "<td id='td1'><p id='tbp'>User Id : </p></td>";
			echo "<td id='td2'>".$uid."</td>";
		echo "</tr>";
		echo "<tr>";
			echo "<td id='td1'><p id='tbp'>User Name : </p></td>";
			echo "<td id='td2'>".$username."</td>";
		echo "</tr>";
		echo "<tr>";
			echo "<td id='td1'><p id='tbp'>Address : </p></td>";
			echo "<td id='td2'>".$address."</td>";
		echo "</tr>";
		echo "<tr>";
			echo "<td id='td1'><p id='tbp'>Add Amount: </p></td>";
			echo "<td id='td2'><input type='text' name='addbal' id='inp' required></td>";
		echo "</tr>";
		echo "<tr>";
			echo "<td id='td1'><p id='tbp'>Add Remark: </p></td>";
			echo "<td id='td2'><input type='text' name='rmark'></td>";
		echo "</tr>";
echo "</table>";

ob_end_flush();
?>
			<input type="submit" name="submit" id="btn" onclick="show()">
		</form>

	</div>
<div class="footer">
		<p style="text-align:center;">Copyright &copy; 2018 -2019 Long Life Product Pvt. Ltd.</p>
</div>
</body>
</html>