<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'];
	$kodepl  	= $_GET['kodepl'];
	$namapl	= $_GET['namapl'];
	
	$tjumlah=0;
	$tunit=0;
	$tbayar=0;
	$tkurang=0;
	$tpotong=0;
	
	$sql2	= mysql_query("SELECT * FROM company");
	$r2		= mysql_fetch_assoc($sql2);
	$kop1 = $r2['nama1'];
	$kop2 = $r2['nama2'];
	$kop3 = $r2['alamat'];
	
	echo "<table width='100%' style='font:11px arial'><b>
		<tr>
			<td>$kop1</td>
		</tr>
		<tr>
			<td>$kop2</td>
		</tr>
		<tr>
			<td>$kop3</td>
		</tr>
	</table>
		
	<table width='100%'>
		<tr>
			<td align='center' style='font:14px arial'><b>DAFTAR PENERIMAN 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>";
		if(!$kodepl==''){
			echo "<tr>
				<td align='center' style='font:14px arial'><b>PELANGGAN : $namapl ('.kodepl.')'</b></td>
			</tr>";
		}		
	echo "</table>
	<br>
	
<table width='100%' border='1' style='border-collapse:collapse;font:11px arial'>
	<tr>
			<th>No.</th>
			<th>No.Faktur</th>
			<th>Kode Cabang</th>
			<th>Tgl.Faktur</th>
			<th>Saldo</th>
			<th>Bayar Tunai</th>
			<th>Potongan</th>
			<th>Sisa</th>
		</tr>";
		
		if(!empty($kodepl))
			$query1 = "SELECT kode_ppi,tanggal,kode_pl,nama_pl FROM jurppi WHERE tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' AND (kode_pl LIKE '%$kodepl%' OR nama_pl LIKE '%$kodepl%') AND posting='P' AND batal='' GROUP BY kode_ppi ORDER BY tanggal,kode_ppi";
		else
			$query1 = "SELECT kode_ppi,tanggal,kode_pl,nama_pl FROM jurppi WHERE tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' AND posting='P' AND batal='' GROUP BY kode_ppi ORDER BY tanggal,kode_ppi";
		$data1	= mysql_query($query1);
		
		$tpiut=0;
		$ttunai=0;		
		$tpot=0;
		$tsisa=0;
		$jdata=0;
		while($r1=mysql_fetch_array($data1)){
			$kodeppi = $r1['kode_ppi'];
			
			echo "<tr>
				<td></td>
				<td>No.PPI : $r1[kode_ppi]</td>
				<td>".jin_date_str($r1['tanggal'])."</td>
				<td>$r1[kode_pl] - $r1[nama_pl]</td>				
			</tr>";
			$query = mysql_query("SELECT * FROM jurppi WHERE kode_ppi='$kodeppi' ");
		
			$no=1;
			$spiut=0;
			$stunai=0;
			$spot=0;
			$ssisa=0;
			while($r=mysql_fetch_array($query)){
				
				echo "<tr>					
					<td align='center'>$no</td>
					<td align='center'>$r[nofak]</td>
					<td align='center'>$r[kode_cab]</td>
					<td align='center'>".jin_date_str($r['tgl_fak'])."</td>
					<td align='right'>".number_format($r['jml_piut'],0)."</td>
					<td align='right'>".number_format($r['tunai'],0)."</td>
					<td align='right'>".number_format($r['potongan'],0)."</td>
					<td align='right'>".number_format($r['sisa'],0)."</td>
				</tr>";
				$spiut=$spiut+$r['jml_piut'];
				$stunai=$stunai+$r['tunai'];
				$spot=$spot+$r['potongan'];
				$ssisa=$ssisa+$r['sisa'];
				$no++;
				$jdata++;
			}
			
			echo "<tr>
				<td></td>
				<td></td>
				<td></td>
				<td align='right'>Jumlah</td>
				<td align='right'>".number_format($spiut,0)."</td>
				<td align='right'>".number_format($stunai,0)."</td>				
				<td align='right'>".number_format($spot,0)."</td>
				<td align='right'>".number_format($ssisa,0)."</td>
			</tr>";
			$tpiut=$tpiut+$spiut;
			$ttunai=$ttunai+$stunai;			
			$tpot=$tpot+$spot;
			$tsisa=$tsisa+$ssisa;
		}
		echo "<tr></tr>
		<tr>
			<td></td>
			<td></td>
			<td></td>
			<td align='right'>Total</td>
			<td align='right'>".number_format($tpiut,0)."</td>
			<td align='right'>".number_format($ttunai,0)."</td>
			<td align='right'>".number_format($tpot,0)."</td>
			<td align='right'>".number_format($tsisa,0)."</td>
		</tr>
	</table>
	<table width='100%'>
		<tr>
			<td>Jumlah data : $jdata</td>
		</tr>
	</table>";
?>