<?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;
	
	$content = "<b><li>DAFTAR PENERIMAAN PIUTANG</li>
		<li>TANGGAL : ".jin_date_str($tgl_awal)." S/D ".jin_date_str($tgl_akhir)."</li></b>
		<li>PELANGGAN : $pelanggan</li></b>
		<br>
	
		<table width='390' border='1' style='border-collapse:collapse'>
		<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];
		
			$content .= "<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];
				
				$content .= "<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++;			
			}
			$content .= "<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;
		}
		
		$content .= "<tr>				
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td align='right'>Total</td>
				<td align='right'>".number_format($tbayar,0)."</td>
			</tr>";
		$content .= "</table>";
		
		$content .= "<li>Jumlah data : $jml</li>";
				
		header("Content-type: application/x-msdownload");
		header("Content-Disposition: attachment; filename=daftar_ppi.xls");
		header("Pragma: no-cache");
		header("Expires: 0");
		echo $content;
?>