<?php
	include '../../inc/inc.koneksi.php';	
	
	$cari	= $_GET['cari'];
	$kodegr	= $_GET['kodegr'];
	$namagr	= $_GET['namagr'];
	$status	= $_GET['status'];
	$kodecab= $_GET['kodecab'];
    $tgllap = date('d-m-Y');
	$jml    = 0;

	$query2 = mysql_query("SELECT * FROM cabang WHERE kode_cab='$kodecab'");
	$r2 = mysql_fetch_assoc($query2);
	$namacab= $r2['nama_cab'];
	
	$query2 = mysql_query("SELECT * FROM company");
	$r2=mysql_fetch_assoc($query2);
	$kop1 = $r2['nama1'];
	$kop2 = $r2['nama2'];
	$kop3 = $r2['alamat'];
	
	$content = "<li><b>$kop1</b></li>
		<li>$kop2</li>
		<li>$kop3</li>
		<br>
		<b><li>DAFTAR PERSEDIAAN BARANG DAGANGAN</li>
		<li>CABANG : $namacab</li>";
		if(!$cari=='') $content .="<li>PENCARIAN : $cari</li>";
		
		if(!$kodegr==''){
			$content .="<li>KELOMPOK : $kodegr - $namagr</li>";			
		}
		if(!$kodesp==''){
			$content .="<li>SUPPLIER : $kodesp - $namasp</li>";
		}
		$content .="<li>STATUS STOK : $status</li>
		<li>PER TANGGAL $tgllap</li></b>
		<br>
	
	<table width='100%' border='1' style='border-collapse:collapse:font:12px arial'>
		<tr>
			<th>No.</th>
			<th>Kode</th>
			<th>Nama Barang</th>
			<th>Barcode</th>
			<th>Satuan</th>
			<th>Min. Stok</th>
			<th>Jumlah Stok</th>
			<th>Nilai Stok</th>
		</tr>";
	
		if(!empty($cari))
			$sql1= "SELECT * FROM stock WHERE kode_cab='$kodecab' AND nama_st LIKE '%$cari%' OR kode_st LIKE '%$cari%' OR barcode LIKE '%$cari%' GROUP BY kode_gr";
		else{
            if(!($kodegr=='')){
			    if($status=='seluruh') $sql1="SELECT * FROM stock WHERE kode_cab='$kodecab' AND kode_gr='$kodegr' GROUP BY kode_gr";
			    else if($status=='ada') $sql1="SELECT * FROM stock WHERE kode_cab='$kodecab' AND kode_gr='$kodegr' AND !(jml_stok=0) GROUP BY kode_gr";
			    else if($status=='minimal') $sql1= "SELECT * FROM stock WHERE kode_cab='$kodecab' AND kode_gr='$kodegr' AND jml_stok<=min_stok GROUP BY kode_gr";
			    else if($status=='expired') $sql1= "SELECT * FROM stock WHERE kode_cab='$kodecab' AND kode_gr='$kodegr' AND expired<='$tgllap' GROUP BY kode_gr";
		    }
		    else{
		        if(!($kodesp=='')){
			        if($status=='seluruh') $sql1= "SELECT * FROM stock WHERE kode_cab='$kodecab' AND kode_sp='$kodesp' GROUP BY kode_gr";
			        else if($status=='ada') $sql1= "SELECT * FROM stock WHERE kode_cab='$kodecab' AND kode_sp='$kodesp' AND (!(jml_stok=0) OR !(jml_stokb=0)) GROUP BY kode_gr";
			        else if($status=='minimal') $sql1= "SELECT * FROM stock WHERE kode_cab='$kodecab' AND kode_sp='$kodesp' AND (jml_stok<=min_stok OR jml_stokb<=min_stok) GROUP BY kode_gr";
			        else if($status=='expired') $sql1= "SELECT * FROM stock WHERE kode_cab='$kodecab' AND kode_sp='$kodesp' expired<='$tgllap' GROUP BY kode_gr";
		        }
		        else{
			        if($status=='seluruh') $sql1= "SELECT * FROM stock WHERE kode_cab='$kodecab' GROUP BY kode_gr";
			        else if($status=='ada') $sql1= "SELECT * FROM stock WHERE kode_cab='$kodecab' AND !(jml_stok=0) OR !(jml_stokb=0) GROUP BY kode_gr";
			        else if($status=='minimal') $sql1= "SELECT * FROM stock WHERE kode_cab='$kodecab' AND jml_stok<=min_stok OR jml_stokb<=min_stok GROUP BY kode_gr";
			        else if($status=='expired') $sql1= "SELECT * FROM stock WHERE kode_cab='$kodecab' AND expired<='$tgllap' GROUP BY kode_gr";
		        }
		    }
		}
		$query1=mysql_query($sql1);
		
		$jml=0;
		$tnilai=0;
		while($r1=mysql_fetch_array($query1)){
			$kodegr1=$r1['kode_gr'];
		
			$group=mysql_query("SELECT * FROM st_group WHERE kode_gr='$kodegr1'");
			$datagr=mysql_fetch_assoc($group);
			$namagr=$datagr['nama_gr'];
			
			$content .= "<tr>	
				<td></td>
				<td><b>'$r1[kode_gr]</b></td>				
				<td><b>$namagr</b></td>
			</tr>";
		
			if(!empty($cari))
		    	$sql= "SELECT * FROM stock WHERE kode_cab='$kodecab' AND kode_gr='$kodegr1' AND (nama_st LIKE '%$cari%' OR kode_st LIKE '%$cari%' OR barcode LIKE '%$cari%') GROUP BY kode_st";
		    else{
		        if(!($kodegr=='')){
			        if($status=='seluruh') $sql="SELECT * FROM stock WHERE kode_cab='$kodecab' AND kode_gr='$kodegr1' GROUP BY kode_st";
			        else if($status=='ada') $sql="SELECT * FROM stock WHERE kode_cab='$kodecab' AND kode_gr='$kodegr1' AND (!(jml_stok=0) OR !(jml_stokb=0)) GROUP BY kode_st";
			        else if($status=='minimal') $sql= "SELECT * FROM stock WHERE kode_cab='$kodecab' AND kode_gr='$kodegr1' AND (jml_stok<=min_stok OR jml_stokb<=min_stok)  GROUP BY kode_st";
			        else if($status=='expired') $sql= "SELECT * FROM stock WHERE kode_cab='$kodecab' AND kode_gr='$kodegr1' AND expired<='$tgllap' GROUP BY kode_st";
		        }
		        else{
		            if(!($kodesp=='')){
			            if($status=='seluruh') $sql= "SELECT * FROM stock WHERE kode_cab='$kodecab' AND kode_gr='$kodegr1' AND kode_sp='$kodesp' GROUP BY kode_st";
			            else if($status=='ada') $sql= "SELECT * FROM stock WHERE kode_cab='$kodecab' AND kode_gr='$kodegr1' AND kode_sp='$kodesp' AND (!(jml_stok=0) OR !(jml_stokb=0)) GROUP BY kode_st";
			            else if($status=='minimal') $sql= "SELECT * FROM stock WHERE kode_cab='$kodecab' AND kode_gr='$kodegr1' AND kode_sp='$kodesp' AND (jml_stok<=min_stok OR jml_stokb<=min_stok) GROUP BY kode_st";
			            else if($status=='expired') $sql= "SELECT * FROM stock WHERE kode_cab='$kodecab' AND kode_gr='$kodegr1' AND kode_sp='$kodesp' expired<='$tgllap' GROUP BY kode_st";
		            }
		            else{
    			        if($status=='seluruh') $sql= "SELECT * FROM stock WHERE kode_cab='$kodecab' AND kode_gr='$kodegr1' GROUP BY kode_st";
	    		        else if($status=='ada') $sql= "SELECT * FROM stock WHERE kode_cab='$kodecab' AND kode_gr='$kodegr1' AND (!(jml_stok=0) OR !(jml_stokb=0)) GROUP BY kode_st";
		    	        else if($status=='minimal') $sql= "SELECT * FROM stock WHERE kode_cab='$kodecab' AND kode_gr='$kodegr1' AND (jml_stok<=min_stok OR jml_stokb<=min_stok) GROUP BY kode_st";
			            else if($status=='expired') $sql= "SELECT * FROM stock WHERE kode_cab='$kodecab' AND kode_gr='$kodegr1' AND expired<='$tgllap' GROUP BY kode_st";
		            }
		        }
		    }
		    $query= mysql_query($sql);
			
			$no=1;
    		$snilai=0;
		    while($r=mysql_fetch_array($query)){
		        $nilai=$r['jml_stok']*$r['hg_beli'];
			
			    $content .= "<tr>
				    <td>$no</td>
				    <td>'$r[kode_st]</td>
				    <td>$r[nama_st]</td>
				    <td>'$r[barcode]</td>
				    <td>$r[nama_sp]</td>
				    <td>$r[satuan]</td>
				    <td align='right'>".number_format($r['hg_beli'],2)."</td>
				    <td align='right'>".number_format($r['hb_akhir'],2)."</td>
				    <td align='right'>".number_format($r['min_stok'],0)."</td>
				    <td align='right'>".number_format($r['jml_stok'],0)."</td>
				    <td align='right'>".number_format($nilai,2)."</td>
			    </tr>";
			    $snilai=$snilai+$nilai;
			    $no++;
		    }
		
		    $content .= "<tr>
			    <td></td>
			    <td></td>
			    <td></td>
			    <td></td>
			    <td></td>
			    <td></td>
			    <td></td>
			    <td></td>
			    <td></td>
			    <td>Sub Total</td>
			    <td align='right'>".number_format($snilai,2)."</td>
		    </tr>";
		    $tnilai=$tnilai+$snilai;
		}
	    $content .= "<tr></tr>
	    <tr>
		    <td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td>Total</td>
			<td align='right'>".number_format($tnilai,2)."</td>
		</tr>
	</table>
	<table>
		<tr>
			<td>Jumlah data : $jml</td>
		</tr>
	</table>";
				
	header("Content-type: application/x-msdownload");
	header("Content-Disposition: attachment; filename=daftar_st$kodecab.xls");
	header("Pragma: no-cache");
	header("Expires: 0");
	echo $content;
?>