<?php include "layouts/menu.php"; ?>
    <?php $hpl=$_REQUEST['hpl'];?>
    <?php $fid=$_REQUEST['fid'];?>
	<?php /*$vno=$_REQUEST['vno'];*/?>
	 <script> 
function add() 
{ 
var x = document.getElementById("txt1").value; 
var y = document.getElementById("txt2").value; 
var z = document.getElementById("obc").value; 
var w = document.getElementById("jkjk").value;
document.getElementById("tot").value =((x*w)+(y*1)+(z*1)); 
} 
</script>
 <script> 
function add1() 
{ 
var x = document.getElementById("dqw").value; 
var y = document.getElementById("tx2").value; 
document.getElementById("txt2").value =parseInt((x*1)*(y*1)); 

} 
</script>
<script> 
function nnow() 
{ 
var x = document.getElementById("qx").value; 
var y = document.getElementById("jkjk").value; 
document.getElementById("ax").value =parseInt((x*1)+(y*1)); 

} 
</script>
 <script> 
function sum() 
{ 
var z = document.getElementById("tot").value; 
var q = document.getElementById("payment").value; 
document.getElementById("balance").value =((z*1)-(q*1));  
} 
</script>
<script> 
function asal() 
{ 
var z = document.getElementById("asa").value; 
var q = document.getElementById("jkjk").value; 
document.getElementById("asa").value =((z*1)*(q*1)); 
var x = document.getElementById("rno").value; 
var y = document.getElementById("jkjk").value; 
document.getElementById("rno").value =((x*1)*(y*1));
} 
</script>
<script> 
function iasal() 
{ 
var z = document.getElementById("rno").value; 
var q = document.getElementById("jkjk").value; 
document.getElementById("rno").value =((z*1)*(q*1));  
} 
</script>
<script>

    function diff()
{
    var date14 = document.getElementById("dd").value;
    var date15 = document.getElementById("kk").value;
	
	var date11 = date14.split("-").reverse().join("-");
	var date22 = date15.split("-").reverse().join("-");

    var one_day=1000*60*60*24;

    var date1 = new Date(date11);
    var date2 = new Date(date22);

      // Convert both dates to milliseconds
      var date1_ms = date1.getTime();
      var date2_ms = date2.getTime();

      // Calculate the difference in milliseconds
      var difference_ms = date2_ms - date1_ms;

      // Convert back to days and return
document.getElementById("tx2").value =  Math.round(difference_ms/one_day); 

}
</script>
  <!--  page-wrapper -->
         <div class="right_col" role="main">
            <div class="row">
                 <!-- page header -->
                <div class="col-lg-12">
                    <h1 class="page-header  alert alert-info">Pay Due for Vehicle Loans</h1>
                </div>
                <!--end page header -->
            </div>
            <div class="row">
                <div class="col-lg-12">
                    <!-- Form Elements -->
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            Pay Due
                        </div>
                        <div class="panel-body">
                            <div class="row">
  <div class="col-xs-12 col-md-8">
    <form name="form1" class="form-horizontal" method="post" action="" enctype="multipart/form-data">

<?php 


?>
<?php 
$qry5=mysqli_query($conn, "select * from user");
$n5=mysqli_num_rows($qry5); $prno = 0; $rnoo = 0;
						if($n5==0)
						{
							$fid = 0;
						}
						else
					    {
						while($row5=mysqli_fetch_array($qry5))
						{
						$fidd = $row5['fid'];
						$rnoo = $row5['bil'];
						$prno=$row5['fid']+1;
						}
						$fid = $fidd + 1;
						
						}
$qry=mysqli_query($conn, "select * from user where mfin='$mfin' &&  hpl='$hpl'");
						$n=mysqli_num_rows($qry);
						
						if($n==0)
						{
						$qryy=mysqli_query($conn, "select * from customer where mfin='$mfin' &&  hpl='$hpl'");
                          $row2=mysqli_fetch_array($qryy);
							$sss = 0;
						}
						else
					    {
						while($row=mysqli_fetch_array($qry))
						{
						$sss = $row['bal'];
						}
						}
$sql1 = "select sum(paymnt) from user where mfin='$mfin' &&  hpl='$hpl'";
$qt = mysqli_query($conn, $sql1);
$row1 = mysqli_fetch_array($qt);
$sql5=$row1[0];
$sql6=$sql5 + 1;
?> 
 <?php
		$qry=mysqli_query($conn, "select * from customerdetails where mfin='$mfin' &&  hpl='$hpl'");		
	while($row = mysqli_fetch_array($qry)) {
$tyty=$row['duemnt'];
	   $row = str_replace('/', '-', $row['hpdate']);
  $d1 = strtotime($row);
$d2 = strtotime("today");
$min_date = min($d1, $d2);
$max_date = max($d1, $d2);
$i = 0;
while (($min_date = strtotime("+1 DAY", $min_date)) <= $max_date) {
    $i++;
}}if($sql6<$tyty)
{
$qry=mysqli_query($conn, "select * from customerdetails where mfin='$mfin' &&  hpl='$hpl'");			
	while($row = mysqli_fetch_array($qry)) {
	   $duedat = str_replace('/', '-', $row['hpdate']);}
$duedat = date("d-m-Y", strtotime("$duedat + " . $sql6 . " month"));	
	}
	else{$duedat = "Account Clossed";}
$qry=mysqli_query($conn, "select * from customerdetails where mfin='$mfin' &&  hpl='$hpl'");
$row=mysqli_fetch_array($qry);
	$sad = $row['duemnt'];
	$rnoo = $rnoo +1;
	?>
<?php $hpl=$_REQUEST['hpl'];?>
			<?php $fid=$_REQUEST['fid'];?>
			<?php	$qry=mysqli_query($conn, "select * from user where mfin='$mfin' &&  hpl='$hpl' and fid='$fid'");
						$n=mysqli_num_rows($qry);
						$row1=mysqli_fetch_array($qry);
						 ?>
	<div class="alert alert-info">Welcome <?php echo $row['name'];?> Fid <?php echo $fid; ?> Receipt <?php echo $prno; ?></div>
	<div class="form-group">
	<img style="margin-left:150px;margin-bottom:20px;" src="user/<?php echo $row['uimg'];?>" height="100" width="100"/>
	<label for="inputPassword3" class="col-sm-2 control-label">HPL</label>
    <div class="col-sm-4">
	  <input type="hidden" class="form-control" id="inputPassword3" name="fid" value="<?php echo $fid;?>" placeholder="E.g 1008" readonly>
      <input type="text" class="form-control" id="inputPassword3" name="hpl" value="<?php echo $row1['hpl'];?>" placeholder="E.g 1008" readonly>
    </div>
	</div>
   <div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">Vehicle No</label>
    <div class="col-sm-4">
      <input type="text" class="form-control" id="inputPassword3" name="vno" value="<?php echo $row['vno'];?>" placeholder="Vehicle No" readonly>
    </div>
	<label for="inputPassword3" class="col-sm-2 control-label">Receipt No</label>
    <div class="col-sm-4">
	  <input type="text" class="form-control" id="fff" name="bil" value="<?php echo $row1['bil'];?>" placeholder="" readonly>
      <input type="hidden" class="form-control" id="dqw" name="amtr" value="8.34" placeholder="E.g 1008" required>
    </div>
 </div>
  <div class="form-group">
    <label for="inputPassword3" class="col-sm-2 control-label">HP Amount</label>
    <div class="col-sm-4">
      <input type="text" class="form-control" id="inputPassword3" name="hpamount" value="<?php echo $row['hpamount'];?>" placeholder="E.g 1008" readonly>
    </div>
	 <label for="inputPassword3" class="col-sm-2 control-label">HP Date</label>
    <div class="col-sm-4">
      <input type="text" class="form-control" id="inputPassword3" name="hpdate" value="<?php echo $row['hpdate'];?>" placeholder="Vehicle No" readonly>
    </div>
	</div>
 <div class="form-group">
    <label for="inputPassword3" class="col-sm-2 control-label">Name</label>
    <div class="col-sm-4">
      <input type="text" class="form-control" id="inputPassword3" name="name" value="<?php echo $row1['name'];?>" placeholder="" readonly>
    </div>
  	
  
    <label for="inputEmail3" class="col-sm-2 control-label">Due Month</label>
    <div class="col-sm-4">
      <input type="text" class="form-control" id="duemonth" name="no" value="<?php echo $row1['no']; ?>" placeholder="" readonly> 
    </div>
 </div>
 
  <?php
				$qry=mysqli_query($conn, "select * from net where mfin='$mfin' &&  hpl='$hpl'");
	$n=mysqli_num_rows($qry);
						
						if($n==0)
						{?>
 <div class="form-group">
    <label for="inputPassword3" class="col-sm-2 control-label">Due Date</label>
    <div class="col-sm-4">
      <input type="text" class="form-control" id="dd" name="duedate" value="<?php echo $row1['duedate']; ?>" readonly placeholder="Due Date">
    </div>
  <?php }
   else
{?>
<div class="form-group">
    <label for="inputPassword3" class="col-sm-2 control-label">Due Date</label>
    <div class="col-sm-4">
      <input type="text" class="form-control" id="dd" name="duedate" value="<?php echo "Account Clossed";?>"  readonly placeholder="Due Date">
    </div><?php
}   ?>
    
    <label for="inputPassword3" class="col-sm-2 control-label">Due Amount</label>
    <div class="col-sm-4">
      <input type="text" class="form-control" id="txt1" name="dueamount" value="<?php echo $row1['dueamount'];?>" readonly placeholder="Due Amount">
    </div>
</div>
<div class="form-group">
    <label for="inputPassword3" class="col-sm-2 control-label">Interest</label>
    <div class="col-sm-4">
      <input type="text" class="form-control" id="rno" name="rno" value="<?php echo $row1['rno'];?>" placeholder="Recipt Number">
    </div>
	   <label for="inputPassword3" class="col-sm-2 control-label">DueAmount w/t intr</label>
    <div class="col-sm-4">
      <input type="text" class="form-control" id="asa" name="pass" value="<?php echo $row1['pass'];?>" placeholder="" required>
    </div>
  </div>

   <div class="form-group">
    <label for="inputPassword3" class="col-sm-2 control-label">Pay Date</label>
    <div class="col-sm-4">
      <input type="text" class="form-control" id="kk" name="paydate"  value="<?php echo $row1['paydate']; ?>" placeholder="Pay Date" onfocus="insertDate(this,'DD-MM-YYYY')">
    </div>
 
    <label for="inputPassword3" class="col-sm-2 control-label">Pay Months</label>
    <div class="col-sm-4">
      <input type="text" class="form-control" id="jkjk" name="paymnt"  value="<?php echo $row1['paymnt']; ?>" onchange="asal()">
    </div>
  </div>
    <div class="form-group">
    <label for="inputPassword3" class="col-sm-2 control-label">Paid Months Before</label>
    <div class="col-sm-4">
      <input type="text" class="form-control" id="qx" name="pavbd"  value="<?php echo $row1['paybefore']; ?>" placeholder="Dont give any values" >
    </div>
 
    <label for="inputPassword3" class="col-sm-2 control-label">Paid Months Now</label>
    <div class="col-sm-4">
      <input type="text" class="form-control" id="ax" name="payyr"  value="<?php echo $row1['payyr']; ?>" onclick="nnow()">
    </div>
  </div>
  <div class="form-group">
    <label for="inputPassword3" class="col-sm-2 control-label">Old Balance</label>
    <div class="col-sm-4">
      <input type="text" class="form-control" id="obc" name="obc"  value="<?php echo $sss;?>" >
    </div>


  
    <label for="inputPassword3" class="col-sm-2 control-label">D-fault Day</label>
    <div class="col-sm-4">
      <input type="text" class="form-control" id="tx2" name="dfaultdate" value="<?php echo $row1['dfaultdate']; ?>" placeholder="DfaultDay" onclick="diff()">

    </div>
	</div>
   <div class="form-group">
 
    <label for="inputPassword3" class="col-sm-2 control-label">Penalty</label>
    <div class="col-sm-4">
      <input type="text" class="form-control" id="txt2" name="fine" value="<?php echo $row1['fine']; ?>" placeholder="Penalty" onclick="add1()">

    </div>
  
    <label for="inputPassword3" class="col-sm-2 control-label">Payable Amount</label>
    <div class="col-sm-4">
      <input type="text" class="form-control" id="tot" name="amt" value="<?php echo $row1['amt']; ?>" placeholder="Amount"  onclick="add()">
    </div>
 </div>
   <div class="form-group">
    <label for="inputPassword3" class="col-sm-2 control-label">Payment</label>
    <div class="col-sm-4">
      <input type="text" class="form-control" id="payment" name="urname" value="<?php echo $row1['urname']; ?>" placeholder="" required>

	</div>
 
    <label for="inputPassword3" class="col-sm-2 control-label">Balance</label>
    <div class="col-sm-4">
      <input type="text" class="form-control" id="balance"  name="bal" value="<?php echo $row1['bal']; ?>" placeholder=""  onclick="sum()">
    </div>
 
 
  </div>
  <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
      <button type="submit" class="btn btn-warning btn-lg btn-block" name="s1">Register Due</button>
    </div>
  </div>
      <div class="alert alert-success">
	<?php
							if(isset($_POST['s1']))
							{
								extract($_POST);
								$update=mysqli_query($conn, "update user set no='$no',duedate='$duedate',bal='$bal',dueamount='$dueamount',rno='$rno',paydate='$paydate',paymnt='$paymnt',payyr='$payyr',dfaultdate='$dfaultdate',fine='$fine',
								                      name='$name',amt='$amt',urname='$urname',pass='$pass',paybefore='$pavbd',bil='$bil' where mfin='$mfin' &&  fid='$fid'");
								
								if($update)
								{
									
									echo "Updated Successfully";
								$qry5=mysqli_query($conn, "select * from user");
$n5=mysqli_num_rows($qry5);
						if($n5==0)
						{
					
						}
						else
					    {
						
						}	
								header("Location:index2.php?hpl=$hpl&fid=$fid");	
								}
								else
								{
									echo "Failure";
								}
							}
?>
	  </div>
</form>
</div>
 <div class="col-xs-6 col-md-4">
 <div class="alert alert-warning"><?php echo "Total Paid Due  ".$sql5;?></div>
 <!-- <div class="alert alert-success">INSURANCE FC TAX DATES</div>
  <?php
					
						
						$qry=mysqli_query($conn, "select * from customerdetails where mfin='$mfin' &&  hpl='$hpl'");
						$n=mysqli_num_rows($qry);
						if($n==0)
						{
							echo "Nothing To Display";
						}
						else
					{ 
					
	
					while($row=mysqli_fetch_array($qry))
						{ 
						
						$id = $row['idate'];
						$ud = $row['uname'];
						$pd = $row['pwd'];}
						$idd = date("d-m-Y", strtotime("$id + " . 12 . " month"));
						$udd = date("d-m-Y", strtotime("$ud + " . 12 . " month"));
						$pdd = date("d-m-Y", strtotime("$pd + " . 12 . " month"));
			
						if(strtotime($idd) <= strtotime("now"))
						{
						echo "INSURANCE DATE";
						echo '<center><div><font size="6" color="red">'.$id.'</font></div></center>';					
						}
						else
						{
						echo "INSURANCE DATE"; 
						echo '<center><div><font size="6" color="green">'.$id.'</font></div></center>';

						}
							if(strtotime($udd) <= strtotime("now"))
						{
						echo "FC DATE";
						echo '<center><div><font size="6" color="red">'.$ud.'</font></div></center>';						
						}
						else
						{echo "FC DATE";
						echo '<center><div><font size="6" color="green">'.$ud.'</font></div></center>';

						}
							if(strtotime($pdd) <= strtotime("now"))
						{
						echo "TAX DATE";
						echo '<center><div><font size="6" color="red">'.$pd.'</font></div></center>';					
						}
						else
						{
						echo "TAX DATE";
						echo '<center><div><font size="6" color="green">'.$pd.'</font></div></center>';

						}
												
						} ?>-->
						<div class="alert alert-info">Other Due Details</div>
				 <?php
					
						$qry=mysqli_query($conn, "select * from customer where mfin='$mfin' &&  hpl='$hpl'");
						$n=mysqli_num_rows($qry);
						$qry2=mysqli_query($conn, "select * from net2 where mfin='$mfin' &&  hpl='$hpl'");
						$nn=mysqli_num_rows($qry2);
						if($n==0)
						{
							echo "Nothing To Display";
						}
						elseif($nn==0)
					{ 
						?>
					<table table class="table" border="0" align="center" cellspacing="0" cellpadding="10" width="50%" style="table-layout: fixed;">
					<col width="50px">
						<tr class="danger" style="color:purple;">
						<th>Name</th>
						<th></th>
						<th>Loan No</th>
						<th>Amount</th>
					</tr>
					<?php 
					$row=mysqli_fetch_array($qry)
					?>
					<tr class="warning">
					<col width="100px">
						<td><?php echo $row['name'];?></td>
						<td></td>
						<td><?php echo $row['hpl'];?></td>
						<td><?php echo $row['hpamount'];?></td>
						</tr>
					<?php
					}
				else
				{
				echo "Due Closssed";
				}
					?>
					</table>
<div class="alert alert-info">Your Monthly Due Dates</div>
 <p style="color:red;"><?php
					
						
						$qry=mysqli_query($conn, "select * from customerdetails where mfin='$mfin' &&  hpl='$hpl'");			
	while($row = mysqli_fetch_array($qry)) {
	   $duedat = str_replace('/', '-', $row['hpdate']);
						$pp = $row['duemnt'];
						$addr = $row['address'];
						
						}
	
  $d1 = strtotime($duedat);
$d2 = strtotime("today");
$min_date = min($d1, $d2);
$max_date = max($d1, $d2);
$i = 0;
while (($min_date = strtotime("+1 MONTH", $min_date)) <= $max_date) {
    $i++;
}
if($i>$pp)
{ $i=$pp; }
$rc = $i - $sql5 ;
$sql9 = $sql5 + $rc ;
echo $rc;

				
						?></p>
			<table table border="0" align="center" cellspacing="0" cellpadding="10" width="150">
						
					<?php 
					for($y=0;$y<$sql5;$y++)
						{
						$duedat = date("d-m-Y", strtotime("$duedat + " . 1 . " month"));
					?>
					
						<button class="btn btn-info text-center">
                        <i class="fa  fa-pencil fa-1x"></i><b><?php echo $duedat;?></b>
                    </button><?php 
					}
	                for($j=1; $j <= $rc; $j++) {
						$duedat = date("d-m-Y", strtotime("$duedat + " . 1 . " month"));
					?>
					
						<button class="btn btn-danger text-center">
                        <i class="fa  fa-pencil fa-1x"></i><b><?php echo $duedat;?></b>
                    </button>
						<?php
						}
					for($j=$sql9; $j < $sad; $j++) {
						$duedat = date("d-m-Y", strtotime("$duedat + " . 1 . " month"));
					?>
					
						<button class="btn btn-success text-center">
                        <i class="fa  fa-pencil fa-1x"></i><b><?php echo $duedat;?></b>
                    </button>
						<?php
						}
					?>
					</table>
					<div class="alert alert-success">Customer Address</div>
					<font size="3" color="green"><strong><?php echo $addr;?></strong></font>
</div>
</div></div>
</div></div></div> <?php include "layouts/footer.php"; ?> </body></html>