<?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>HB Rata-2</th>
			<th>HB Terakhir</th>
			<th>Supplier</th>
			<th>Nama Supplier</th>
			<th>Harga Jual</th>
			<th>Stok Min.</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'>".number_format($r_data[hg_beli],2)."</td>
					<td align='right'>".number_format($r_data[hb_akhir],2)."</td>
					<td align='center'>$r_data[kode_sp]</td>
					<td>$r_data[nama_sp]</td>
					<td align='right'>".number_format($r_data[hg_jual],0)."</td>
					<td align='right'>$r_data[min_stok]</td>
					<td align='right'>".number_format($r_data[jml_stok],2)."</td>
					<td align='right'>".number_format($nilai,2)."</td>
				</tr>";
				$snilai=$snilai+$nilai;
				$no++;
				$jml++;
			}
			$content .= "<tr>
				<td></td>
				<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($snilai,0)."</td>
			</tr>";
			$tnilai=$tnilai+$snilai;	
		}
		
		$content .= "<tr>
			<td></td>
			<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($snilai,0)."</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;
?>