<?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=='')
		$supplier='Keseluruhan';
	else
		$supplier='$cari';
	$jml = 0;
	
	$content = "<b><li>DAFTAR PEMBAYARAN HUTANG</li>
		<li>TANGGAL : ".jin_date_str($tgl_awal)." S/D ".jin_date_str($tgl_akhir)."</li></b>
		<li>SUPPLIER : $supplier</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>Nominal</th>
			<th>Dibayar</th>
			<th>Sisa</th>
		</tr>";
		
		if(!empty($cari))
			$query1 = "SELECT kode_pht,tanggal,kode_sp,nama_sp FROM jurpht WHERE tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' AND (kode_sp LIKE '%$cari%' OR nama_sp LIKE '%$cari%') AND batal='' GROUP BY kode_pht ORDER BY tanggal";
		else
			$query1 = "SELECT kode_pht,tanggal,kode_sp,nama_sp FROM jurpht WHERE tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' AND batal='' GROUP BY kode_pht ORDER BY tanggal";
		$data1	= mysql_query($query1);
		
		$tbayar=0;
		$jdata=1;
		while($r_data1=mysql_fetch_array($data1)){
			$kodepht = $r_data1[kode_pht];
		
			$content .= "<tr>	
				<td></td>
				<td>No.Bukti : $r_data1[kode_pht]</td>
				<td>".jin_date_str($r_data1[tanggal])."</td>
				<td>$r_data1[kode_sp] - $r_data1[nama_sp]</td>
			</tr>";
			
			$sql = "SELECT kode_pht,nofak,tgl_fak,jml_utang,dibayar,sisa FROM jurpht WHERE kode_pht='$kodepht' ";
			$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='right'>".number_format($r_data[jml_utang],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 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 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_pht.xls");
		header("Pragma: no-cache");
		header("Expires: 0");
		echo $content;
?>