<?php
	session_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'];
    
	$query8="select * from wallet where Userid='$userid' order by Id desc";
	$result8=mysqli_query($con,$query8);
	$data8=mysqli_fetch_array($result8);
	$currentbal= $data8['Balance'];
	
	$query2="select * from distributer where Userid='$userid'";
	$result2=mysqli_query($con,$query2);
	$data=mysqli_fetch_array($result2);
	$username=$data['Name'];
	$grandparentid=$data['Parentid'];
?>
<?php
	include'connection.php';
	$query="select * from state";
	$result=mysqli_query($con,$query);
	
?>
<script>
function Showmnum()
{
		var umnum = document.getElementById("umnum").value;
	
		var xmlhttp = new XMLHttpRequest();
		xmlhttp.open("GET","userMnum.php?mnum="+umnum,false);
			xmlhttp.send(null);
			
		document.getElementById("usrdetail").innerHTML= xmlhttp.responseText;
}
</script>
<html>
<title>Add Retailer</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:800px; margin:0; padding:0;}
.content #conp1{margin:10px; font-size:19px; font-family:calibri; font-weight:bold;}
.addagent{background:#996420; height:40px; width:600px; margin:0px; border:1px solid black; line-height:40px; padding:0px;}
.addagent h3{margin:0 0 0 10px; padding:0; color:white; font-size:20px;}
.form{background:; margin:10 0 0 100px; width:400px; float:left;}
.form td{padding:5px 0 0 20px; height:20px;}

#t1{ width:602px; height:800px;text-align:center; border-collapse:collapse; border-color:black; margin:-1 0 0 0px; padding:5px;}
#t1 th{font-family:calibri; background:#f6f0e8; width:200px; text-align:right; margin-right:25px; font-size:20px;}
#t1 td{font-family:calibri; background:#d9e0fb;}
#t1 input{padding-left:8px; width:280px; height:35px; font-size:20px; border:1px solid black;}
#t1 select{padding-left:8px; width:280px; height:35px; border:1px solid black;}
#t1 textarea{padding-left:8px; width:280px; border:1px solid black;}
 #t2{ width:500px; text-align:center; border-collapse:collapse; border-color:black; margin:0px; padding:10px; background:white;}
#star{color:red; text-align:right; margin-right:10px;}
#btntd{background:none; height:60px;}
#subbtn{ background:green; color:white; font-size:17px; border-radius:5px; border:1px solid white; height:40px; width:200px;
margin:30 0 0 400px;}
#subbtn:hover{background:#d1994a; cursor:pointer;}

.foot{background:#996420; width:100%; height:50px; float:left;text-align:center;
	margin-top:50px;}
 
</style>
</head>
<body>
	<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>
		<p id="link2"><?php echo"Balance : ". $currentbal;  ?></p>
	</div>
	<div class="menu">
	    <?php include'menu.php'; ?>
	</div>
	<div class="content">
		<p id="conp1">Personal Information</p><hr>
	<div class="form">
	<div class="addagent">
		<h3>Fill Details For Add New Retailer</h3>
	</div>
		<?php

			include'connection.php';
			date_default_timezone_set("asia/Kolkata");
			$time= Date("h:i:s A");
			$date= Date("Y-m-d");
			if(isset($_POST['submit']))
			{	
				$querygetmaxid="select max(Userid) from retailer";
				$resultgetmaxid=mysqli_query($con,$querygetmaxid);
				$row=mysqli_fetch_array($resultgetmaxid);
				$maxid=$row[0];
				$newuserid=$maxid+1;
				
				$uname=$_POST['uname'];
				$address=$_POST['address'];
				$state=$_POST['state'];
				
				$mnum=$_POST['mnum'];
				$pwd=rand(100000,999999);
				$biztype=$_POST['biztype'];
				$pannum=$_POST['pannum'];
				$pincode=$_POST['pincode'];
				$city=$_POST['city'];
				$email=$_POST['email'];
				$bizname=$_POST['biztype'];
				$opbal=$_POST['opbal'];
				if(empty($opbal))
				{
					$opbal=0;
				}
				
				$quGetDupId="select * from retailer where Mobile='$mnum'";
				$reGetDupId=mysqli_query($con,$quGetDupId);
				$DupId=mysqli_num_rows($reGetDupId);
				
				$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;
	            }
				
				if($opbal>$OldDisBal)
				{
	            	echo "<h1 align='center'><font color='#ff0000'> &#x2716; Insufficiant Balance !</font></h1><br>";
				}
				elseif($DupId>=1)
				{
				    echo "<h1 align='center'><font color='#ff0000'> &#x2716; This Mobile Number Already Exist !</font></h1><br>";
				}
				else
				{
					$queryinsert="insert into retailer(JoinDate,Userid,Password,Name,Balance,Address,State,Mobile,Businessname,Pan,Parentid,Grandparentid,Pincode,City,Email,Businesstype)
					values('$date','$newuserid','$pwd','$uname','$opbal','$address','$state','$mnum','$bizname','$pannum','$userid','$grandparentid','$pincode','$city','$email','$biztype')";
					
					if(mysqli_query($con,$queryinsert))
					{
						$quDeductBal="insert into transaction(Fromid, Toid, Credit, Debit) values('$userid','$newuserid','$opbal','$opbal')";
						if(mysqli_query($con,$quDeductBal))
						{
						    $paymentId=mysqli_insert_id($con);
							
							$quInsertBal="insert into wallet(Userid,Txn,Debit,Remarks,Date,Time,Paymentto,Paytoname,Transactiontype,Paynrechid)
							value('$userid','-$opbal','$opbal','Opening_Balance_To_New_Retailer','$date','$time','$newuserid','$uname','Transfer_Opening_Balance','$paymentId')";
							
							if(mysqli_query($con,$quInsertBal))
							{
							    $DisEwalletId=mysqli_insert_id($con);
							    
							    $quDisOpBal2="select * from wallet where Userid='$userid' and Id>=234084";
                	            $reDisOpBal2=mysqli_query($con,$quDisOpBal2);
	                            $OldDisBal2=0;
	                            while($dataDisOpBal2=mysqli_fetch_array($reDisOpBal2))
	                            {
	                                $OldDisTxn2= $dataDisOpBal2['Txn'];
	                                $OldDisBal2=$OldDisBal2+$OldDisTxn2;
	                            }
	                            $OpRetBal=$OldDisBal2+$opbal;
	                            $queryW="update wallet set Openingbalance='$OpRetBal', Balance='$OldDisBal2' where Id='$DisEwalletId'";
								mysqli_query($con,$queryW);
							    
								$queryinsertwallet="insert into wallet(Userid,Openingbalance,Txn,Credit,Balance,Remarks,Date,Time,Paymentfrom,Payfromname,Transactiontype,Paynrechid)
								value('$newuserid','$opbal','$opbal','$opbal','$opbal','Add_Opening_Balance','$date','$time','$userid','$username','Recieved_Opening_Balance','$paymentId')";
								if(mysqli_query($con,$queryinsertwallet))
								{   
								    $EwalletId=mysqli_insert_id($con);
								    $quRetBal="insert into RetBal(Userid, Walletid, Balance)values('$newuserid','$EwalletId','$opbal')";
								    mysqli_query($con,$quRetBal);
								    
									$queryinsertcomm="insert into commision(Userid)values('$newuserid')";
									if(mysqli_query($con,$queryinsertcomm))
									{
									    $updateComm="select * from operator";
									    $reUpdateComm=mysqli_query($con,$updateComm);
									    while($dataUpdateComm=mysqli_fetch_array($reUpdateComm))
									    {
									        $Columname=$dataUpdateComm['Commision_Column'];
									        $RetComm=$dataUpdateComm['RetComm'];
									        
									        $quUpdateComm="update commision set $Columname='$RetComm' where Userid='$newuserid'";
									        mysqli_query($con,$quUpdateComm);
									    }
										$quUpdate="update retailer set Balance='$opbal' where Userid='$newuserid'";
										if(mysqli_query($con,$quUpdate))
										{
											$quUpdate1="update distributer set Balance='$OldDisBal2' where Userid='$userid'";
											if(mysqli_query($con,$quUpdate1))
											{
											    echo "<h1 align='center'><font color='#008000'> &#x2714; Registration Successfull !</font></h1><br>";
											    
												$query="select * from retailer where Userid='$newuserid'";
												$result=mysqli_query($con,$query);
												$data=mysqli_fetch_array($result);
												echo	"<table border='0' cellpadding='10' id='t2'>
												        <tr>
															<th>User Id : </th>
															<td> ".$data['Userid']."</td>
														</tr>
														<tr>
															<th>Password : </th>
															<td>". $data['Password']."</td>
														</tr>
														<tr>
															<th>Agent Name : </th>
															<td>".$data['Name']."</td>
														</tr>
														<tr>
															<th></th>
															<td><a href='recharge_home.php.php'>OK</a></td>
														</tr>
													</table><br><br><br>";
													
													$msg="Welcome To LLP Recharge Dear ".$uname." Your Userid = ".$newuserid." and Password Is = ".$data['Password']." . Thanks To Join LLP Recharge ! Log On: www.llprecharge.in";
		    
		                                             $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=$mnum&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)
                                                     {
                                                         "cURL Error #:" . $err;
                                                     }
                                                  	else
                                        			{
                                    			     $response;
	                                        			$data2=json_decode($response);
                                        			}
											
		                                    	
											
											}
											else
											{
												echo "Registration error !";
											}
										}
										else
										{
											echo "Registration error !";
										}
									}
									else
									{
										echo "error 5"; 
									}
									
								}
								else
								{
									echo "error 4";
								}
							}
							else
							{
								echo "error 3";
							}
							
							
						}
						
					}
					else
					{
						echo "error";
					}
				}
				mysqli_close($con);
			}
		?>
			<form method="post">
				<table border="1" cellpadding="10" id="t1">
					<tr>
						<th>Agent Name : <span style="color:red;"> * </span></th>
						<td><input type="text" name="uname" required></td>
					</tr>
					<tr>
						<th>Mobile No. : <span style="color:red;">*</span></th>
						<td><input type="text" name="mnum" id="umnum" onblur="Showmnum()" required><br><br>
						<div id="usrdetail" style="color:red; font-size:19px;"></div>
						</td>
					</tr>
					<tr>
						<th>E-Mail : </th>
						<td><input type="email" name="email" placeholder="example@gmail.com"></td>
					</tr>
					<tr>
						<th>Postal Address : <span style="color:red;">*</span></th>
						<td><textarea rows="4" cols="22" name="address" required></textarea></td>
					</tr>
					<tr>
						<th>State : <span style="color:red;">*</span></th>
						<td><input type="text" name="state"></td>
					</tr>
					<tr>
						<th>City/District : <span style="color:red;">*</span></th>
						<td><input type="text" name="city"></td>
					</tr>
					<tr>
						<th>Pin Code : </th>
						<td><input type="text" name="pincode"></td>
					</tr>
					<tr>
						<th>Agent Business Type : <span style="color:red;">*</span></th>
						<td>
							<select name="biztype" required>
								<option>--Select Business Type--</option>
								<?php
									include'connection.php';
									$querybt="select * from retailertype";
									$resultbt=mysqli_query($con,$querybt);
									while($row=mysqli_fetch_array($resultbt))
									{
										echo "<option>".$row['Businessname']."</option>";
									}
								?>
							</select>
						</td>
					</tr>
					<tr>
						<th>Pan No. : </th>
						<td><input type="text" name="pannum"></td>
					</tr>
					<tr>
						<th>Business name : </th>
						<td><input type="text" name="bizname"></td>
					</tr>
					<tr>
						<th>Opening Balance : </th>
						<td><input type="text" name="opbal" placeholder="Rs."></td>
					</tr>
				</table>
						
						<input type="submit" name="submit" value="Submit" id="subbtn"><br><br>
						<input type="submit" name="cancel" value="Cancel" id="subbtn">
			</form>
	</div>
</div>
	<div class="foot">
		<p style="text-align:center;">Copyright &copy; 2018 -2019 Long Life Product Pvt. Ltd.</p>
	</div>
</body>
</html>