<?php ob_start(); ?>
	<!DOCTYPE html>
	<html>
<?php
error_reporting(0);
 date_default_timezone_set('Asia/Kolkata');
 session_start('ERP');
		$connect= mysqli_connect("localhost","motorfin_cron","infossel123.")or mysqli_connect("localhost","motorfin_cron","infossel123.");
		
		$select=mysqli_select_db("motorfin_info",$connect) or die("database does not exist");
	?>

		<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		
		<!-- Meta, title, CSS, favicons, etc. -->
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta http-equiv="X-UA-Compatible" content="chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1">

		<title>INFOSSEL SOFT SOLUTIONS </title>

	
        <style type="text/css">
		/* when @font-face is defined(it is in font.css) you can add the font to any rule by using font-family */
		hh1 {
			font-family: 'BAUHS93';
		}
		</style>
		<!-- Custom Theme Style -->
		<link href="build/css/custom.min.css" rel="stylesheet">
		
	<script type="text/javascript"
	  src="http://ajax.googleapis.com/ajax/libs/chrome-frame/1/CFInstall.min.js"></script>

	  
	  </head>

	  <body class="nav-md">
<div class="right_col" role="main">
            <div class="row">
                 <!-- page header -->
                <div class="col-lg-12">
                    <h1 class="page-header">Schedule SMS</h1>
                </div>
                <!--end page header -->
            </div>
            <div class="row">
                <div class="col-lg-12">
                    <!-- Form Elements -->
					 <!-- Form Elements -->
                    <div class="panel panel-default">
                       
                        <div class="panel-body">
                            <div class="row">

 
    <?php
							
  $sts2 ="Active";
					  $ii= date("d"); 	  $ddd= date("d");
  $stime=$stime;
  $ffff = 35815;
					  $iii=23000;
  $qry2 = mysqli_query($conn, "select * from pending ORDER BY id ASC");
while($row2 = mysqli_fetch_array($qry2)) {
	$ddd55 = $row2['ddd'];
	if($ddd55 != $ii) { echo $ii;
	$x = $row2['hpl'];
	$mfin = $row2['mfin'];
	$tottt = 0; $caa = 0; $fine55 = 0; $hlbal = 0;
	$spdd= 0; $spdue = 0; $spff = 0; $spfine = 0;
	$sppp = 0; $less = 0; $sppdue = 0;
	$spfine = 0; $spddd = 0;
$spdd = 0; $spdue = 0;
$spff = 0;
$sphl = 0;
		$sql666 = mysqli_query($conn, "select * from customerdetails where hpl='$x' && mfin='$mfin'");
		$row = mysqli_fetch_array($sql666);

	$x = $row['hpl'];
	
	$tottt = 0; $caa = 0; $fine55 = 0; $hlbal = 0;
	$spdd= 0; $spdue = 0; $spff = 0; $spfine = 0;
	$sppp = 0; $less = 0; $sppdue = 0;
	$spfine = 0; $spddd = 0;
$spdd = 0; $spdue = 0;
$spff = 0;
$sphl = 0;
		
		$hpl = $row['hpl'];
		$hpdate = $row['hpdate'];
			$vno = $row['vno'];
			$dd = $row['dd'];
			$pno = $row['pno'];
			$vname = $row['vname']; 		$duemnt55 = $row['duemnt'];
			$model = $row['model'];
			$area = $row['area'];
			$name = $row['name'];
				$eno  = $row['eno'];
	$cno  = $row['cno'];
	$bname  = $row['bname'];
	$color  = $row['color'];
	$hpamt  = $row['hpamount'];
	$lat  = $row['lat'];
	$lang  = $row['lang'];
	$uimg  = $row['uimg'];
	$img  = $row['img'];
	$gi1  = $row['g1'];
	$bi1  = $row['b1'];
	$profession  = $row['profession'];
	$rdate  = $row['rdate'];
	$remarks  = $row['remarks'];
	$cobname  = $row['cobname'];
			
  $rrno2 = 0;
  $mfin = $mfin;
  $aaa = mysqli_query($conn, "select * from bad where hpl='$hpl' && mfin='$mfin'");
  $an = mysqli_num_rows($aaa);
  $aaa1 = mysqli_query($conn, "select * from sizzing where hpl='$hpl' && mfin='$mfin'");
  $an1 = mysqli_num_rows($aaa1);
  if(($an==0)&&($an1==0)) {  
$rrow44=0;$rrow45=0;
										
									$sql = "SELECT SUM(hpamount) FROM customerdetails where mfin='$mfin' &&  hpl='$hpl'";
                                    $q = mysqli_query($conn, $sql);
                                    $row52 = mysqli_fetch_array($q);	
                                    $rrow44 = $row52[0] + $rrow44;								
									
									$sql = "SELECT SUM(pass) FROM user where mfin='$mfin' &&  hpl='$hpl'";
                                    $q = mysqli_query($conn, $sql);
                                    $row53 = mysqli_fetch_array($q);	
                                    $rrow45 = $row53[0] + $rrow45;																			
									
									
									 $rrow445= $rrow44 - $rrow45;  $rrno2 = 0;
$qry5=mysqli_query($conn, "select * from user ORDER BY id DESC");
$n5=mysqli_num_rows($qry5);
$row5=mysqli_fetch_array($qry5);
											
$qry=mysqli_query($conn, "select * from user where mfin='$mfin' && hpl='$hpl' ORDER BY fid DESC");
						$n=mysqli_num_rows($qry);
						
						if($n==0)
						{
							$sss = 0;
						}
						else
					    {
						$row=mysqli_fetch_array($qry);
						$lddate = $row['paydate'];
						$sss = $row['bal'];
						$newsss = $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;
$nuserpay = $sql5;
$hlbal20 = 0;
$sql155 = "select sum(actamt2) from hloan where mfin='$mfin' && type!='COLLECTION' && hpl='$hpl'";
$qt55 = mysqli_query($conn, $sql155);
$row155 = mysqli_fetch_array($qt55); 
	$hlbal20= $row155[0]; 
$sql155 = "select sum(postamt2) from otherentry where mfin='$mfin' &&  hpl='$hpl'";
$qt55 = mysqli_query($conn, $sql155);
$row155 = mysqli_fetch_array($qt55);
$hlbal2=$row155[0];

$sql1551 = "select sum(hlamt) from customerdetails where mfin='$mfin' &&  hpl='$hpl'";
$qt551 = mysqli_query($conn, $sql1551);
$row1551 = mysqli_fetch_array($qt551);
$hlbal1=$row1551[0];

$sql1552 = "select sum(hloan) from user where mfin='$mfin' &&  hpl='$hpl'";
$qt552 = mysqli_query($conn, $sql1552);
$row1552 = mysqli_fetch_array($qt552);
$hlbal3=$row1552[0];
$hlbal = $hlbal20 + $hlbal2 - $hlbal1 - $hlbal3;

 $noo = 0; $less = 0; $spdue= 0 ; $spfine = 0; $spdd = 0;
			$ttp = 0;	$tti = 0; $ttdue = 0;	$ttdd = 0;	$ttfine = 0;	$ttpf = 0;	$tthl = 0;	
				$tttot = 0;	
				$qdue = 0; $qdue25 = 0;
			$ttp = 0;	$tti  = 0; $ttdue = 0;	$ttdd = 0;	$ttfine = 0;	$ttpf = 0;	$tthl = 0;	
				$tttot = 0;	$dmm = 0; $sts = 0;
						$qry=mysqli_query($conn, "select * from customerdetails where mfin='$mfin' &&  hpl='$hpl'");			
$row = mysqli_fetch_array($qry);
	   $duedat = str_replace('/', '-', $row['hpdate']);
						$pp = $row['duemnt'];
						$addr = $row['address'];
			$damt = $row['total'];		
$intt = $row['inter'];
$intt2 = $row['interest'];
$prin = $row['dueamt'];			
$pno = $row['pno'];	
$cobpno = $row['cobpno'];	
	$defamt2 = $row['defamt'];	
	$defamt = (($damt*$defamt2)/100)/30;	
  $d1 = strtotime($duedat);
$d2 = strtotime("today");
$min_date = min($d1, $d2);
$max_date = max($d1, $d2);

if($iii>$pp)
{ $iii=$pp; }
$rc = $iii - $sql5 ;
$sql9 = $sql5 + $rc ; $vv = 1; $ii = 0;
 
$qry5=mysqli_query($conn, "select * from user where mfin='$mfin' && hpl='$hpl' order by paydate2 asc");			
while($row5 = mysqli_fetch_array($qry5)) { $rr = 0;
$duemnt = $row5['paymnt'];
$rcptno = $row5['roll'];
$fidd = $row5['fid'];
$gi = 0;
$rno1 = $row5['rno'];
$rno = $rno1/$duemnt;
$pass1 = $row5['pass'];
$pday = $row5['paydate'];
$pass = $pass1/$duemnt;
$urname1 = $row5['urname'];
$hloan = $row5['hloan'];
$fine1 = $row5['fine'];
$urname = ($urname1 - $hloan - $fine1) /$duemnt;
$dmm = $urname1 - $hloan - $fine1;
$ff55 = 0;
while($dmm>0) { $gi++; $rr++;
if($ff55 == 0){
	$fine18 = $fine1;
	$hloan8 = $hloan;
}
else {
	$fine18 = 0;
	$hloan8 = 0;
}
$ff55++;
if($sts==0) {
if($dmm>$damt){
	$sts = 1; $less = 0;
	$pamt = $damt;
	$noo++;
}
elseif($dmm==$damt){
	$sts = 0; $less = 0;
	$pamt = $damt;
	$noo++;
}
else{
	$sts = 2;
	$pamt = $dmm;
	$less = $damt - $dmm;
}
$ii++;
$selected_date=$duedat;
		  	$selected_date=strtotime( $selected_date );
			$mysqldate = date( 'd-m-Y', $selected_date );
			$month = date( 'm', $selected_date );
			$day = date( 'd', $selected_date );
			$yyyy = date( 'Y', $selected_date );
if((($day==30)&&($month==01)) || (($day==29)&&($month==01)) || (($day==31)&&($month==01)))	
{
$duedat = "28-02-".$yyyy;
}
else {	
$duedat = date("d-m-Y", strtotime("$duedat + " . 1 . " month"));
}
$pday = $row5['paydate'];


  $d11 = strtotime($duedat);
$d22 = strtotime($pday);
$min_date1 = min($d11, $d22);
$max_date1 = max($d11, $d22);
$i6 = 0;
if($d11<=$d22) {
while (($min_date1 = strtotime("+1 DAY", $min_date1)) <= $max_date1) {
    $i6++;
}
}
if($i6 <= 3){
    
$fine2 = 0;
} else {
$fine2 = (($defamt/$damt)*($i6 * $pamt)); }
if($fine2 < 0) {
	$fine2 = 0;
}
 $dmm = $dmm - $pamt;
$spfine = $spfine + $fine2;
$spdd = $spdd + $i6; $spdue = $spdue + $pamt;
$spff = $spff + $fine18;
$sphl = $sphl + $hloan8;
 } 

elseif($sts == 1) {
	if($dmm>$damt){
	$sts = 1; $less = 0;
	$pamt = $damt;
	$noo++;
}
elseif($dmm==$damt){
	$sts = 0; $less = 0;
	$pamt = $damt;
	$noo++;
}
else{
	$sts = 2;
	$pamt = $dmm;
	$less = $damt - $dmm;
} $ii++; 
$selected_date=$duedat;
		  	$selected_date=strtotime( $selected_date );
			$mysqldate = date( 'd-m-Y', $selected_date );
			$month = date( 'm', $selected_date );
			$day = date( 'd', $selected_date );
			$yyyy = date( 'Y', $selected_date );
if((($day==30)&&($month==01)) || (($day==29)&&($month==01)) || (($day==31)&&($month==01)))	
{
$duedat = "28-02-".$yyyy;
}
else {	
$duedat = date("d-m-Y", strtotime("$duedat + " . 1 . " month"));
}



  $d11 = strtotime($duedat);
$d22 = strtotime($pday);
$min_date1 = min($d11, $d22);
$max_date1 = max($d11, $d22);
$i6 = 0;
if($d11<=$d22) {
while (($min_date1 = strtotime("+1 DAY", $min_date1)) <= $max_date1) {
    $i6++;
}
}
if($i6 <= 3){
    
$fine2 = 0;
} else {
$fine2 = (($defamt/$damt)*($i6 * $pamt)); }
if($fine2 < 0) {
	$fine2 = 0;
}  $dmm = $dmm - $pamt;
$spfine = $spfine + $fine2;
$spdd = $spdd + $i6; $spdue = $spdue + $pamt;
$spff = $spff + $fine18;
$sphl = $sphl + $hloan8;
 } 
else { 
  $d11 = strtotime($duedat);
$d22 = strtotime($pday);
$min_date1 = min($d11, $d22);
$max_date1 = max($d11, $d22);
$i6 = 0;
if($d11<=$d22) {
while (($min_date1 = strtotime("+1 DAY", $min_date1)) <= $max_date1) {
    $i6++;
}
}
if($i6 < 3){
    
$fine2 = 0;
} else {
$fine2 = (($defamt/$damt)*($i6 * $less));
 }
if($fine2 < 0) {
	$fine2 = 0;
}
if($less>$dmm){
$dmm2 = $less - $dmm; $noo++;
$less2 = $less  - $dmm2; 
$spdue = $spdue + $less;
$dmm = 0;
if($dmm>$damt){
	$sts = 1;
	$less = 0;
	$pamt = $damt;
	$noo++;
}
elseif($dmm==$damt){
	$sts = 0; $less = 0;
	$pamt = $damt;
	$noo++;
}
else{
	
	$sts = 2;
	$pamt = $less2;
	$less = $dmm2;
}
$pamt = $less2;	
}
else{
$dmm = $dmm - $less; $noo++;
$spdue = $spdue + $less;
if($dmm>$damt){
	$sts = 1;
	$less = 0;
	$pamt = $damt;
	$noo++;
}
elseif($dmm==$damt){
	$sts = 0; $less = 0;
	$pamt = $damt;
	$noo++;
}
else{
	
	$sts = 0;
	$pamt = $dmm;
	$less = 0;
}
$pamt = $dmm; }
 $dmm = $dmm;
$spfine = $spfine + $fine2;
$spdd = $spdd + $i6; $spff = $spff + $fine18;  
$sphl = $sphl + $hloan8;
 }  
$ttp = $ttp + $pass; $tti = $tti + $rno; $ttdue = $ttdue + $urname;	$ttdd = $ttdd + $i6;	
$ttfine = $ttfine + $fine2;	
$ttpf = $fine1 + $ttpf;	$tthl = $tthl + $hloan;	$tttot = $tttot + $urname1;
				
} } 

$qry51=mysqli_query($conn, "select * from net where mfin='$mfin' &&  hpl='$hpl'");
					$n51=mysqli_num_rows($qry51);
					$row51 = mysqli_fetch_array($qry51);
					if($n51>0){  $ttt = $row51['tot'];  $spdue = $spdue + $ttt;	}
else { 
if($less > 0){
	$tday = date('d-m-Y');
  $d11 = strtotime($duedat);
  $duedatles = $duedat;
$d22 = strtotime($tday);
$min_date1 = min($d11, $d22);
$max_date1 = max($d11, $d22);
$i6 = 0;
if($d11<=$d22) {
while (($min_date1 = strtotime("+1 DAY", $min_date1)) <= $max_date1) {
    $i6++;
}
}
if($i6 < 3){
    
$fine2 = 0;
} else {
$fine2 = (($defamt/$damt)*($i6 * $less));
 }
if($fine2 < 0) {
	$fine2 = 0;
}

$dmm = 0;	
$spfine = $spfine + $fine2;
$spdd = $spdd + $i6;
$sppp = $sppp + $less;
}
$iiii = $ii;
$ii++; $ni = 0;
while($ii<=$pp) {
	 $duedat = date("d-m-Y", strtotime("$duedat + " . 1 . " month")); 
  $d11 = strtotime($duedat);
$d22 = strtotime("today");
$min_date1 = min($d11, $d22);
$max_date1 = max($d11, $d22);
$i6 = 0;
if($d11<=$d22) {
while (($min_date1 = strtotime("+1 DAY", $min_date1)) <= $max_date1) {
    $i6++;
} }

if($i6 <= 3){
    
$fine2 = 0;
} else {
$fine2 = $defamt * $i6; }
if($fine2 < 0) {
	$fine2 = 0;
} $ii++; 
$ttdd = $ttdd + $i6;	
$ttfine = $ttfine + $fine2;	
$spfine = $spfine + $fine2;
$spdd = $spdd + $i6;
if($i6>0){
	$ni++;
	$sppdue = $sppdue + $damt;
}
} }
$fine55 = $spfine - $spff;
  $d119 = strtotime($duedatles);
$d229 = strtotime("today");
if($d119>=$d229){
	$spddd = $sppdue;
}
else {
$spddd = $less + $sppdue;
if($less>0){
$iiii = $iiii - 1;
$ni = $ni + 1;
    
}
}
	 $tottt = 0;
 $caa = round($fine55,0); 
 $tottt = ($spddd *1) + ($caa * 1) + ($hlbal * 1); 
  $d111 = strtotime($duedat);
$d222 = strtotime("today");
if($d111<=$d222){
	$star = "*";
}
else{
	$star = NULL;
}
echo $tottt;
 if($tottt>0){
	 echo "88888888";
     $sql44 = mysqli_query($conn, "select * from pending where hpl='$hpl' && mfin='$mfin'");
     $n447 = mysqli_num_rows($sql44);
     $row44 = mysqli_fetch_array($sql44);
  $delete2=mysqli_query($conn, "delete from pending where mfin='$mfin' && hpl='$hpl'");
         	$insert=mysqli_query($conn, "insert into pending(eno,cno,bname,color,hpamt,lat,lang,uimg,img,gi1,bi1,profession,rdate,remarks,cobname,mfin,hpl,vno,vname,model,lddate,hpdate,duemnt,paymnt,dueamt,hlbal,penamt,pendmnt,hloan,fname,area,defamt,pno,cobpno,dd,star,ddd)
											 values('$eno','$cno','$bname','$color','$hpamt','$lat','$lang','$uimg','$img','$gi1','$bi1','$profession','$rdate','$remarks','$cobname','$mfin','$hpl','$vno','$vname','$model','$lddate','$hpdate','$duemnt55','$iiii','$damt','$hlbal','$tottt','$ni','$hlbal','$name','$area','$fine55','$pno','$cobpno','$dd','$star','$ddd')");
											  if($insert) {
	 echo "success";
 }
 else {
	 echo "failed"; 
 }
}
else {
	$delete2=mysqli_query($conn, "delete from pending where mfin='$mfin' && hpl='$hpl'");
}								
         

 ?>
	
								<?php  }  }
}

 ?>
			
				</tbody></table>
 </div></div>
 
 
</div>
</div></div>
</div><?php include "layouts/footer.php"; ?></body></html>