<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;
}

$jmldata= 0;

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

		echo "<tr>				
				<td></td>
				<td><b>Supplier : $r_data1[kode_sp]</b></td>
				<td><b>$r_data1[nama]</b></td>
			</tr>";
			
		$sql   = "SELECT * FROM hutang WHERE kode_sp='$kodesp' 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);
			$slsth = $th_now-$th_tr-1;
			$kb_tr = $th_tr;
			
			while($kb_tr>4){
				$kb_tr = $kb_tr-4;
			}
			
			if($bl_tr==1)
				$umur=31-$tg_tr+$tg_now;
			else if($bl_tr==2){
				if($kb_tr==0)
					$umur=29-$tg_tr+$tg_now;
				else
					$umur=28-$tg_tr+$tg_now;					
			}
			else if($bl_tr==3)
				$umur=31-$tg_tr+$tg_now;
			else if($bl_tr==4)
				$umur=30-$tg_tr+$tg_now;
			else if($bl_tr==5)
				$umur=31-$tg_tr+$tg_now;
			else if($bl_tr==6)
				$umur=30-$tg_tr+$tg_now;
			else if($bl_tr==7)
				$umur=31-$tg_tr+$tg_now;
			else if($bl_tr==8)
				$umur=31-$tg_tr+$tg_now;
			else if($bl_tr==9)
				$umur=30-$tg_tr+$tg_now;				
			else if($bl_tr==10)
				$umur=31-$tg_tr+$tg_now;				
			else if($bl_tr==11)
				$umur=30-$tg_tr+$tg_now;				
			else
				$umur=31-$tg_tr+$tg_now;
			
			if(!($bl_tr==12)){
				$bln=$bl_tr+1;
				while($bln<13){
					if($bln==2){
						if($kb_tr==0)
							$umur=$umur+29;
						else
							$umur=$umur+28;
					}
					else if($bln==3)
						$umur=$umur+31;
					else if($bln==4)
						$umur=$umur+30;
					else if($bln==5)
						$umur=$umur+31;
					else if($bln==6)
						$umur=$umur+30;
					else if($bln==7)
						$umur=$umur+31;
					else if($bln==8)
						$umur=$umur+31;
					else if($bln==9)
						$umur=$umur+30;
					else if($bln==10)
						$umur=$umur+31;
					else if($bln==11)
						$umur=$umur+30;
					else if($bln==12)
						$umur=$umur+31;
					
					$bln=$bln+1;
				}
			}
			
			if(!($bl_now==1)){
				$bln=$bl_now-1;
				while($bln>0){
					if($bln==2){
						if($kb_now==0)
							$umur=$umur+29;
						else
							$umur=$umur+28;
					}
					else if($bln==3)
						$umur=$umur+31;
					else if($bln==4)
						$umur=$umur+30;
					else if($bln==5)
						$umur=$umur+31;
					else if($bln==6)
						$umur=$umur+30;
					else if($bln==7)
						$umur=$umur+31;
					else if($bln==8)
						$umur=$umur+31;
					else if($bln==9)
						$umur=$umur+30;
					else if($bln==10)
						$umur=$umur+31;
					else if($bln==11)
						$umur=$umur+30;
					else if($bln==12)
						$umur=$umur+31;
					$bln=$bln-1;
				}
			}
			
			if($slsth>0){
				$thn=$th_tr+1;
				while($slsth>0){
					$kbthn=$thn;
					while($kbthn>4){
						$kbthn = $kbthn-4;
					}
					
					if($kbthn==0)
						$umur=$umur+366;
					else
						$umur=$umur+365;
					
					$thn=$thn-1;
				}
			}
										
			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='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 align='right'>Jumlah</td>
				<td align='right'>".number_format($sjumlah)."</td>
			</tr>";
			$tjumlah=$tjumlah+$sjumlah;
	}
	echo "<tr>			
			<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>";
?>
