<?php
	include '../../inc/inc.koneksi.php';	
	
	$cari = $_GET[cari];
	$jml = 0;
	$tgllap =date('d-m-Y');
	if (!empty($cari)){
		$grup='KELOMPOK : '.$cari;
	}else{
		$grup='KESELURUHAN';
	}
		
	$content = "<b><li>DAFTAR PERSEDIAAN BARANG DAGANGAN</li>
		<li>$grup</li>
		<li>PER TANGGAL $tgllap</li></b>
		<br>
	
		<table width='390' border='1' style='border-collapse:collapse'>
		<tr>
			<th>No.</th>
			<th>Kode</th>
			<th>Nama Barang</th>
			<th>Satuan</th>
			<th>Harga Jual</th>
			<th>HB Rata-2</th>
			<th>HB Terakhir</th>
			<th>Jumlah Stok</th>
			<th>Nilai Stok</th>
		</tr>";
	
		if (!empty($cari)){			
			$query2	= "SELECT * FROM st_group WHERE (nama_gr LIKE '%$cari%' or kode_gr LIKE '%$cari%') ORDER BY kode_gr";
		}
		else{
			$query2	= "SELECT * FROM st_group ORDER BY kode_gr";
		}		
		$data2	= mysql_query($query2);
		
		$tnilai=0;
		while($r_data2=mysql_fetch_array($data2)){
			$kodegr=$r_data2[kode_gr];
		
		    $content .= "<tr>	
				<td></td>
				<td><b>$r_data2[kode_gr]</b></td>				
				<td><b>$r_data2[nama_gr]</b></td>
			</tr>";
		
		$sql = "SELECT * FROM stock WHERE kode_gr='$kodegr' ORDER BY kode_st ";		
		$query = mysql_query($sql);
		
		$no=1;
		$snilai=0;
		while($r_data=mysql_fetch_array($query)){
			$nilai=$r_data[jml_stok]*$r_data[hg_beli];
			
		$content .= "<tr>	
				<td>$no</td>
				<td>$r_data[kode_st]</td>
				<td>$r_data[nama_st]</td>
				<td>$r_data[satuan]</td>
				<td align='right'>$r_data[hg_jual]</td>
				<td align='right'>$r_data[hg_beli]</td>
				<td align='right'>$r_data[hb_akhir]</td>
				<td align='right'>$r_data[jml_stok]</td>
				<td align='right'>$nilai</td>
			</tr>";
			$snilai=$snilai+$nilai;
			$no++;
			$jml++;
		}
		$content .= "<tr>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>				
				<td align='right'>Jumlah</td>
				<td align='right'>$snilai</td>
			</tr>";
			$tnilai=$tnilai+$snilai;	
		}
		
		$content .= "<tr>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td align='right'>Total</td>
				<td align='right'>$tnilai</td>
			</tr>";			
		$content .= "</table>";
		
		$content .= "<table>
			<tr>
				<td>Jumlah Data : $jml</td>
			</tr>
			</table>";
				
		header("Content-type: application/x-msdownload");
		header("Content-Disposition: attachment; filename=daftar_stok.xls");
		header("Pragma: no-cache");
		header("Expires: 0");
		echo $content;
?>
