<script type="text/javascript">	
	window.print();
</script>
<?php
	include '../../inc/inc.koneksi.php';	
	include '../../inc/fungsi_tanggal.php';

	$tglawal = $_GET['tglawal'];
	$tglakhir= $_GET['tglakhir'];
	$kodesp  = $_GET['kodesp'];
	$namasp  = $_GET['namasp'];
	$cari    = $_GET['cari'];
	
	$query2= mysql_query("SELECT * FROM company");
	$r2		=mysql_fetch_assoc($query2);
	$kop1 = $r2['nama1'];
	$kop2 = $r2['nama2'];
	$kop3 = $r2['alamat'];
	
	echo "<table width='100%'>
		<tr>
			<td style='font:11px arial;line-height:80%'><b>$kop1</b></td>
		</tr>
		<tr>
			<td style='font:10px arial;line-height:80%'>$kop2</td>
		</tr>
		<tr>
			<td style='font:10px arial;line-height:80%'>$kop3</td>
		</tr>
	</table>

	<table width='100%'>
		<tr>
			<td style='font:13px arial;line-height:75%' align='center'><b>DAFTAR PEMBELIAN BARANG DAGANGAN</td>
		</tr>
		<tr>
			<td style='font:13px arial;line-height:75%' align='center'><b>TANGGAL : ".jin_date_str($tglawal)." S/D ".jin_date_str($tglakhir)."</b></td>
		</tr>";
		if(!empty($kodesp)){
			echo "<tr>
				<td style='font:13px arial;line-height:75%' align='center'><b>SUPPLIER : $namasp ($kodesp)</b></td>
			</tr>";
		}
		if($cari==''){
			echo "<tr>
				<td style='font:13px arial;line-height:75%' align='center'><b>PENCARIAN : $cari</td>
			</tr>";
		}
	echo "</table>
	<br>
	
<table width='100%' border='1' style='border-collapse:collapse; font:11px arial'>
	<tr>
		<th>No.</th>
		<th>Kode</th>
		<th>Nama Barang</th>
		<th>Barcode</th>
		<th>Satuan</th>
		<th>Jml.Unit</th>
		<th>Harga Bruto</th>
		<th>Diskon</th>
		<th>PPN</th>
		<th>Harga Netto</th>
		<th>Jumlah Harga</th>
		<th>HJ.Umum</th>
		<th>Margin</th>
		<th>Expired</th>
	</tr>";
		
	If(!($cari==''))
		$query2	= "SELECT kode_pb,tanggal,nomor_fk,kode_sup,nama_sup,kode_opb,tanggal_jt,count(kode_st) as totitem FROM faktur_pb WHERE tanggal BETWEEN '$tglawal' AND '$tglakhir' AND (nama_st LIKE '%$cari%' OR kode_st LIKE '%$cari%') AND posting='P' AND batal='' GROUP BY kode_pb";
	else{
		if(!empty($kodesp))
			$query2= "SELECT kode_pb,tanggal,nomor_fk,kode_sup,nama_sup,kode_opb,tanggal_jt,count(kode_st) as totitem FROM faktur_pb WHERE tanggal BETWEEN '$tglawal' AND '$tglakhir' AND kode_sup='$kodesp' AND posting='P' AND batal='' GROUP BY kode_pb";
		else
			$query2= "SELECT kode_pb,tanggal,nomor_fk,kode_sup,nama_sup,kode_opb,tanggal_jt,count(kode_st) as totitem FROM faktur_pb WHERE tanggal BETWEEN '$tglawal' AND '$tglakhir' AND posting='P' AND batal='' GROUP BY kode_pb";
	}
	$data2	= mysql_query($query2);
	$tjumlah=0;
	$tbayar=0;
	$tkurang=0;
	$tpotong=0;
	$jml==0;
	while($r2=mysql_fetch_array($data2)){
		$kodepb=$r2['kode_pb'];
        $jml++;
        
		echo "<tr>
			<td></td>
			<td><b>Kode Trans.</b></td>
			<td><b>: $r2[kode_pb] - No.Faktur : $r2[nomor_fk]</b></td>
			<td><b>Tgl. :".jin_date_str($r2['tanggal'])."</b></td>
			<td><b>Sup. : $r2[kode_sup]</td>
			<td><b>$r2[nama_sup]</b></td>
			<td><b>Pesanan : $r2[kode_opb]</td>
			<td><b>Jatuh Tempo :</td>
			<td><b>".jin_date_str($r2['tanggal_jt'])."</b></td>
		</tr>";
		If(!($cari==''))
			$query = mysql_query("SELECT * FROM faktur_pb WHERE kode_pb='$kodepb' AND (nama_st LIKE '%$cari%' OR kode_st LIKE '%$cari%') ORDER BY no_urut");
		else
			$query = mysql_query("SELECT * FROM faktur_pb WHERE kode_pb='$kodepb' ORDER BY no_urut");
		
		$no=1;
		$sjumlah=0;
		$sbayar=0;
		$skurang=0;
		$spotong=0;
		while($r=mysql_fetch_array($query)){
			
			echo "<tr>
				<td align='center'>$no</td>
				<td>$r[kode_st]</td>
				<td>$r[nama_st]</td>
				<td>$r[barcode]</td>
				<td>$r[satuan]</td>
				<td align='center'>".number_format($r['jml_unit'],0)."</td>
				<td align='right'>".number_format($r['hg_bruto'],2)."</td>
				<td align='right'>".number_format($r['diskon'],2)." %</td>
				<td align='right'>".number_format($r['ppn'],0)."</td>
				<td align='right'>".number_format($r['hg_beli'],2)."</td>
				<td align='right'>".number_format($r['jumlah'],2)."</td>
				<td align='right'>".number_format($r['hg_jual'],0)."</td>
				<td align='right'>".number_format($r['margin'],2)." %</td>
				<td>".jin_date_str($r['expired'])."</td>
			</tr>";
			$sjumlah=$sjumlah+$r['jumlah'];
			$sbayar=$sbayar+$r['bayar'];
			$skurang=$skurang+$r['kurang'];
			$spotong=$spotong+$r['potongan'];
			$no++;
		}
		echo "<tr>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td align='right'>Jumlah</td>
			<td align='right'>".number_format($sjumlah,2)."</td>
		</tr>
		<tr>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td align='right'>Potongan</td>
			<td align='right'>".number_format($spotong,2)."</td>
		</tr>
		<tr>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td align='right'>Dibayar</td>
			<td align='right'>".number_format($sbayar,2)."</td>			
		</tr>
		<tr>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td align='right'>Kurang</td>
			<td align='right'>".number_format($skurang,2)."</td>
		</tr>";
		$tjumlah=$tjumlah+$sjumlah;
		$tbayar=$tbayar+$sbayar;
		$tkurang=$tkurang+$skurang;
		$tpotong=$tpotong+$spotong;
	}
	echo "<tr>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td align='right'>Total</td>
		<td align='right'>".number_format($tjumlah,2)."</td>
	</tr>
	<tr>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td align='right'>Potongan</td>
		<td align='right'>".number_format($tpotong,2)."</td>
	</tr>
	<tr>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td align='right'>Dibayar</td>
		<td align='right'>".number_format($tbayar,2)."</td>
	</tr>
	<tr>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td align='right'>Kurang</td>
		<td align='right'>".number_format($tkurang,2)."</td>
	</tr>
</table>
<table width='100%'>
	<tr>
		<td>Jumlah faktur : $jml</td>			
	</tr>
</table>";
?>