<script type="text/javascript">
    $(function() {
        $("#theList tr:even").addClass("stripe1");
        $("#theList tr:odd").addClass("stripe2");

        $("#theList tr").hover(
            function() {
                $(this).toggleClass("highlight");
            },
            function() {
                $(this).toggleClass("highlight");
            }
        );
    });
</script>

<?php
include '../../inc/inc.koneksi.php';
include '../../inc/fungsi_tanggal.php';

$cari 	= $_GET['cari'];
$tgljt1	= $_GET['tgljt1'];
$tgljt2	= $_GET['tgljt2'];
$tglnow	= date('Y-m-d');

$th_now = (int) substr($tglnow, 0, 4);
$bl_now = (int) substr($tglnow, 5, 2);
$tg_now = (int) substr($tglnow, 8, 2);
$kb_now = $th_now;

while($kb_now>4){
	$kb_now = $kb_now-4;
}

$kab=0;
$jh_now=0;
while($th_now>0){
	$jh_now=$jh_now+365;
	$kab=$kab+1;
	if($kab==4){
		$jh_now=$jh_now+1;
		$kab=0;
	}
	$th_now=$th_now-1;
}

$bln=1;
while($bln<($bl_now+1)){
	if($bl_now==1)
		$jh_now=$jh_now+31;
	else if($bln==2){
		if($kb_now==0)
			$jh_now=$jh_now+29;
		else
			$jh_now=$jh_now+28;
	}
	else if($bln==3)
		$jh_now=$jh_now+31;
	else if($bln==4)
		$jh_now=$jh_now+30;
	else if($bln==5)
		$jh_now=$jh_now+31;
	else if($bln==6)
		$jh_now=$jh_now+30;
	else if($bln==7)
		$jh_now=$jh_now+31;
	else if($bln==8)
		$jh_now=$jh_now+31;
	else if($bln==9)
		$jh_now=$jh_now+30;
	else if($bln==10)
		$jh_now=$jh_now+31;
	else if($bln==11)
		$jh_now=$jh_now+30;
	else if($bln==12)
		$jh_now=$jh_now+31;
		
	$bln=$bln+1;
}
$jh_now=$jh_now+$tg_now;

$jmldata= 0;

echo "<div id='info'>	
	<table id='theList' width='100%'>
	<tr>
		<th>No.</th>
		<th>Nomor Faktur</th>
		<th>Tanggal</th>
		<th>No.Polisi</th>
		<th>Saldo Rp</th>
		<th>Jatuh Tempo</th>
		<th>Umur (hari)</th>
	</tr>";
		
	if(!empty($cari))
		$query1	= "SELECT kode_pl,nama FROM piutang WHERE tanggal_jt BETWEEN '$tgljt1' AND '$tgljt2' AND !jumlah=0 AND (kode_pl LIKE '%$cari%' OR nama LIKE '%$cari%') GROUP BY kode_pl";
	else
		$query1	= "SELECT kode_pl,nama FROM piutang WHERE tanggal_jt BETWEEN '$tgljt1' AND '$tgljt2'  AND !jumlah=0 GROUP BY kode_pl";
	
	$data1 = mysql_query($query1);
	while($r_data1=mysql_fetch_array($data1)){
		$kodepl=$r_data1[kode_pl];

		echo "<tr>				
				<td></td>
				<td><b>Pelanggan : $r_data1[kode_pl]</b></td>
				<td><b>$r_data1[nama]</b></td>
			</tr>";
			
		$sql   = "SELECT * FROM piutang WHERE kode_pl='$kodepl' AND tanggal_jt BETWEEN '$tgljt1' AND '$tgljt2' ";
		$query = mysql_query($sql);
		
		$no=1;
		$sjumlah=0;
		while($r_data=mysql_fetch_array($query)){
			$th_tr = (int) substr($r_data[tanggal], 0, 4);
			$bl_tr = (int) substr($r_data[tanggal], 5, 2);
			$tg_tr = (int) substr($r_data[tanggal], 8, 2);
			$kb_tr = $th_tr;

			while($kb_tr>4){
				$kb_tr = $kb_tr-4;
			}

			$kab=0;
			$jh_tr=0;
			while($th_tr>0){
				$jh_tr=$jh_tr+365;
				$kab=$kab+1;
				if($kab==4){
					$jh_tr=$jh_tr+1;
					$kab=0;
				}
				$th_tr=$th_tr-1;
			}

			$bln=1;
			while($bln<($bl_tr+1)){
				if($bl_tr==1)
					$jh_tr=$jh_tr+31;
				else if($bln==2){
					if($kb_tr==0)
						$jh_tr=$jh_tr+29;
					else
						$jh_tr=$jh_tr+28;
					}
				else if($bln==3)
					$jh_tr=$jh_tr+31;
				else if($bln==4)
					$jh_tr=$jh_tr+30;
				else if($bln==5)
					$jh_tr=$jh_tr+31;
				else if($bln==6)
					$jh_tr=$jh_tr+30;
				else if($bln==7)
					$jh_tr=$jh_tr+31;
				else if($bln==8)
					$jh_tr=$jh_tr+31;
				else if($bln==9)
					$jh_tr=$jh_tr+30;
				else if($bln==10)
					$jh_tr=$jh_tr+31;
				else if($bln==11)
					$jh_tr=$jh_tr+30;
				else if($bln==12)
					$jh_tr=$jh_tr+31;
		
				$bln=$bln+1;
			}
			$jh_tr=$jh_tr+$tg_tr;
			$umur=$jh_now-$jh_tr;
										
			echo "<tr>
				<td align='center'>$no</td>
				<td align='center'>$r_data[nofak]</td>
				<td align='center'>".jin_date_str($r_data[tanggal])."</td>
				<td align='center'>$r_data[nopol]</td>
				<td align='right'>".number_format($r_data[jumlah])."</td>
				<td align='center'>".jin_date_str($r_data[tanggal_jt])."</td>
				<td align='center'>$umur</td>
				</tr>";
			$sjumlah=$sjumlah+$r_data[jumlah];
			$no++;
			$jmldata++;
		}
		echo "<tr>								
				<td></td>
				<td></td>
				<td></td>
				<td align='right'>Jumlah</td>
				<td align='right'>".number_format($sjumlah)."</td>
			</tr>";
			$tjumlah=$tjumlah+$sjumlah;
	}
	echo "<tr>			
			<td></td>
			<td></td>
			<td></td>
			<td align='right'>Total </td>
			<td align='right'>".number_format($tjumlah)."</td>
		</tr>";
	
	echo "</table>";
	echo "<table width='100%'>
		<tr>
			<td>Jumlah data : $jmldata</td>				
		</tr>
		</table>";
echo "</div>";
?>
