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

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

	$tgl_awal= $_GET['tgl_awal'];
	$tgl_akhir= $_GET['tgl_akhir'];
	$cari = $_GET['cari'];
	
	if($cari=='')
		$pelanggan='Keseluruhan';
	else
		$pelanggan='$cari';
	$jml = 0;
	
	$query2	= mysql_query("SELECT * FROM company");
	$r2		= mysql_fetch_assoc($query2);
	$kop1 	= $r2[nama1];
	$kop2 	= $r2[nama2];
	$kop3 	= $r2[nama3];
	
	echo "<table width='100%'>
		<tr>
			<td style='font:11px arial'><b>$kop1</b></td>
		</tr>
		<tr>
			<td style='font:11px arial'>$kop2</td>
		</tr>
		<tr>
			<td style='font:11px arial'>$kop3</td>
		</tr>
	</table>
		
	<table width='100%'>
		<tr>
			<td align='center' style='font:14px arial'><b>DAFTAR PENERIMAAN PIUTANG</b></td>
		</tr>
		<tr>
			<td align='center' style='font:14px arial'><b>TANGGAL : ".jin_date_str($tgl_awal)." S/D ".jin_date_str($tgl_akhir)."</b></td>
		</tr>
		<tr>
			<td align='center' style='font:14px arial'><b>PELANGGAN : $pelanggan</b></td>
		</tr>
	</table>
	<br>
	
	<table width='100%' border='1' style='border-collapse:collapse;font:12px arial'>
		<tr>
			<th>No.</th>
			<th>No. Faktur</th>
			<th>Tgl. Faktur</th>
			<th>No. Polisi</th>
			<th>Nominal</th>
			<th>Dibayar</th>
			<th>Sisa</th>
		</tr>";
		
		if(!empty($cari))
			$query1 = "SELECT kode_ppi,tanggal,kode_pl,nama_pl,tanggal_aw,tanggal_ak FROM jurppi WHERE tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' AND (kode_pl LIKE '%$cari%' OR nama_pl LIKE '%$cari%') AND batal='' GROUP BY kode_ppi ORDER BY tanggal";
		else
			$query1 = "SELECT kode_ppi,tanggal,kode_pl,nama_pl,tanggal_aw,tanggal_ak FROM jurppi WHERE tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' AND batal='' GROUP BY kode_ppi ORDER BY tanggal";
		$data1	= mysql_query($query1);
		
		$tbayar=0;
		$jdata=1;
		while($r_data1=mysql_fetch_array($data1)){
			$kodeppi = $r_data1[kode_ppi];
		
			echo "<tr>	
				<td></td>
				<td>No.PPI : $r_data1[kode_km]</td>
				<td>".jin_date_str($r_data1[tanggal])."</td>
				<td>$r_data1[kode_pl] - $r_data1[nama_pl]</td>
				<td>Periode : $r_data1[tanggal_aw]</td>
				<td>s/d $r_data1[tanggal_ak]</td>
			</tr>";
			
			$sql = "SELECT kode_ppi,nofak,tgl_fak,nopol,jml_piut,dibayar,sisa FROM jurppi WHERE kode_ppi='$kodeppi' ";
			$query = mysql_query($sql);
		
			$no=1;
			$sbayar=0;
			while($r_data=mysql_fetch_array($query)){
				$sbayar=$sbatar+$r_data[dibayar];
				
				echo "<tr>
					<td align='center'>$no</td>
					<td align='center'>$r_data[nofak]</td>
					<td align='center'>".jin_date_str($r_data[tgl_fak])."</td>
					<td align='center'>$r_data[nopol]</td>
					<td align='right'>".number_format($r_data[jml_piut],0)."</td>
					<td align='right'>".number_format($r_data[dibayar],0)."</td>
					<td align='right'>".number_format($r_data[sisa],0)."</td>
				</tr>";
			
			    $no++;
			    $jml++;			
			}
			echo "<tr>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td align='right'>Jumlah</td>
				<td align='right'>".number_format($sbayar,0)."</td>
			</tr>";
			$tbayar=$tbayar+$sbayar;
		}
		
		echo "<tr>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td align='right'>Total</td>
			<td align='right'>".number_format($tbayar,0)."</td>
		</tr>
	</table>
	<table style='font:12px arial'>
        <tr>
            <td>Jumlah data : $jml</td>
        </tr>
    </table>";
?>
<script type="text/javascript">	
	history.back();
</script>