<script type="text/javascript">	
	window.print();
</script>

<?php
    include '../../inc/inc.koneksi.php';
    include '../../inc/fungsi_tanggal.php';
		
    $kodepl     = $_GET['kodepl'];
    $namapl     = $_GET['namapl'];
    $tglawal    = $_GET['tglawal'];
    $tglakhir   = $_GET['tglakhir'];

    $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;
    if($kodepl=='')
	    $kel='KESELURUHAN';
    else
	    $kel=$namapl;

    $sql=mysql_query("SELECT * FROM company");
	$r	=mysql_fetch_assoc($sql);	
	$kop1 = $r[nama1];
	$kop2 = $r[nama2];
	$kop3 = $r[alamat];
	
	echo "<table style='font:11px arial'>
		<tr>
			<td>$kop1</td>
		</tr>
		<tr>
			<td>$kop2</td>
		</tr>
		<tr>
			<td>$kop3</td>
		</tr>
	</table>
		
	<table width='800'>
		<tr>
			<td align='center' style='font:14px arial'><b>DAFTAR PIUTANG DAGANG</b></td>
		</tr>
		<tr>
			<td align='center' style='font:14px arial'><b>TANGGAL PENJUALAN ".jin_date_str($tglawal)." S/D ".jin_date_str($tglakhir)."</b></td>
		</tr>
		<tr>
			<td align='center' style='font:14px arial'><b>Pelanggan : $kel</b></td>
		</tr>
	</table>
	<br>
	
	<table width='800' border='1' style='border-collapse:collapse;font:12px arial'>
	<tr>
		<th>No.</th>
		<th>Nomor Faktur</th>
		<th>Tanggal</th>
		<th>No.Polisi</th>
		<th>Jml.Liter</th>
		<th>Saldo Rp</th>
		<th>Jatuh Tempo</th>
		<th>Umur (hari)</th>
	</tr>";
		
	if($kodepl=='')
	    $query1	= "SELECT kode_pl,nama FROM piutang WHERE tanggal BETWEEN '$tglawal' AND '$tglakhir' AND !jumlah='0' GROUP BY kode_pl";
	else
		$query1	= "SELECT kode_pl,nama FROM piutang WHERE tanggal BETWEEN '$tglawal' AND '$tglakhir' AND !jumlah='0' AND kode_pl='$kodepl' GROUP BY kode_pl";
	
	$data1 = mysql_query($query1);
	$tjumlah=0;
	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>";

		$query = mysql_query("SELECT * FROM piutang WHERE tanggal BETWEEN '$tglawal' AND '$tglakhir' AND kode_pl='$kodepl' AND !jumlah='0'");
		
		$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[jml_liter],2)."</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></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></td>
			<td align='right'>Total </td>
			<td align='right'>".number_format($tjumlah)."</td>
		</tr>
	</table>
	<table width='100%'>
		<tr>
			<td>Jumlah data : $jmldata</td>				
		</tr>
	</table>";
?>
<script type="text/javascript">
	history.back();
</script>