<?php
	include '../../inc/inc.koneksi.php';	
	
	$cari	= $_GET['cari'];
	$kodegr	= $_GET['kodegr'];
	$namagr	= $_GET['namagr'];
	$kodesp	= $_GET['kodesp'];
	$namasp	= $_GET['namasp'];
	$status	= $_GET['status'];

	$jml = 0;
	$tgllap =date('d-m-Y');
	
	$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>";
		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'>
		<tr>
			<th>No.</th>
			<th>Kode</th>
			<th>Nama Barang</th>
			<th>Barcode</th>
			<th>Supplier</th>
			<th>Satuan</th>
			<th>Harga Beli</th>
			<th>HB Terakhir</th>
			<th>Stok Min.Gd</th>
			<th>Stok Gudang</th>";
			
			$sql1=mysql_query("SELECT * FROM cabang WHERE 1 ORDER BY kode_cab");
			
			while($r1=mysql_fetch_array($sql1)){
			    $skt=$r1['singkatan'];
			    $content .= "<th>Stok $skt</th>";
			}
			$content .= "<th>Total Stok</th>
			<th>Nilai Stok</th>
		</tr>";
	
		if(!empty($cari))
			$sql1= "SELECT * FROM stock WHERE 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_gr='$kodegr' GROUP BY kode_gr";
			else if($status=='ada') $sql1="SELECT * FROM stock WHERE kode_gr='$kodegr' AND (!(jml_stok=0) OR !(jml_stokb=0)) GROUP BY kode_gr";
			else if($status=='minimal') $sql1= "SELECT * FROM stock WHERE kode_gr='$kodegr' AND jml_stokb<=min_stokb  GROUP BY kode_gr";
			else if($status=='expired') $sql1= "SELECT * FROM stock WHERE kode_gr='$kodegr' AND expired<='$tgllap' GROUP BY kode_gr";
		}else if(!($kodesp=='')){
			if($status=='seluruh') $sql1= "SELECT * FROM stock WHERE kode_sp='$kodesp' GROUP BY kode_gr";
			else if($status=='ada') $sql1= "SELECT * FROM stock WHERE 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_sp='$kodesp' AND jml_stokb<=min_stokb GROUP BY kode_gr";
			else if($status=='expired') $sql1= "SELECT * FROM stock WHERE kode_sp='$kodesp' expired<='$tgllap' GROUP BY kode_gr";
		}else{
			if($status=='seluruh') $sql1= "SELECT * FROM stock WHERE 1 GROUP BY kode_gr";
			else if($status=='ada') $sql1= "SELECT * FROM stock WHERE !(jml_stok=0) OR !(jml_stokb=0) GROUP BY kode_gr";
			else if($status=='minimal') $sql1= "SELECT * FROM stock WHERE jml_stokb<=min_stokb GROUP BY kode_gr";
			else if($status=='expired') $sql1= "SELECT * FROM stock WHERE expired<='$tgllap' GROUP BY kode_gr";
		}
		$query1=mysql_query($sql1);
		
		$jml=0;
		$tnilai=0;
		$tnilgd=0;$tnil1=0;$tnil2=0;$tnil3=0;$tnil4=0;$tnil5=0;$tnil6=0;$tnil7=0;$tnil8=0;$tnil9=0;$tnil10=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_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_gr='$kodegr1' GROUP BY kode_st";
			    else if($status=='ada') $sql="SELECT * FROM stock WHERE 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_gr='$kodegr1' AND jml_stokb<=min_stokb  GROUP BY kode_st";
			    else if($status=='expired') $sql= "SELECT * FROM stock WHERE kode_gr='$kodegr1' AND expired<='$tgllap' GROUP BY kode_st";
		    }else if(!($kodesp=='')){
			    if($status=='seluruh') $sql= "SELECT * FROM stock WHERE kode_gr='$kodegr1' AND kode_sp='$kodesp' GROUP BY kode_st";
			    else if($status=='ada') $sql= "SELECT * FROM stock WHERE 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_gr='$kodegr1' AND kode_sp='$kodesp' AND jml_stokb<=min_stokb GROUP BY kode_st";
			    else if($status=='expired') $sql= "SELECT * FROM stock WHERE kode_gr='$kodegr1' AND kode_sp='$kodesp' expired<='$tgllap' GROUP BY kode_st";
		    }else{
    			if($status=='seluruh') $sql= "SELECT * FROM stock WHERE kode_gr='$kodegr1' GROUP BY kode_st";
	    		else if($status=='ada') $sql= "SELECT * FROM stock WHERE 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_gr='$kodegr1' AND jml_stokb<=min_stokb GROUP BY kode_st";
			    else if($status=='expired') $sql= "SELECT * FROM stock WHERE kode_gr='$kodegr1' AND expired<='$tgllap' GROUP BY kode_st";
		    }
		    $query= mysql_query($sql);
			
			$no=1;
    		$snilai=0;
	    	$snilgd=0;$snil1=0;$snil2=0;$snil3=0;$snil4=0;$snil5=0;$snil6=0;$snil7=0;$snil8=0;$snil9=0;$snil10=0;
		    while($r=mysql_fetch_array($query)){
		        $kodest =$r['kode_st'];
		        $hgbeli =$r['hg_beli'];
		        $jstok  =$r['jml_stokb'];
		        
		        $sql2=mysql_query("SELECT * FROM stock WHERE kode_st='$kodest'");
		        $n=1;
		        
		        while($r2=mysql_fetch_array($sql2)){
		            if($n==1){
		                $jmlstok1=$r2['jml_stok'];
		                $snil1=$snil1+($r2['jml_stok']*$hgbeli);
		                $snilgd=$snilgd+($r2['jml_stokb']*$hgbeli);
		                $jstok=$jstok+$r2['jml_stok'];
		            }
		            else if($n==2){
		                $jmlstok2=$r2['jml_stok'];
		                $snil2=$snil2+($r2['jml_stok']*$hgbeli);
		                $jstok=$jstok+$r2['jml_stok'];
    		        }
	    	        else if($n==3){
		                $jmlstok3=$r2['jml_stok'];
		                $snil3=$snil3+($r2['jml_stok']*$hgbeli);
		                $jstok=$jstok+$r2['jml_stok'];
		            }
		            else if($n==4){
		                $jmlstok4=$r2['jml_stok'];
		                $snil4=$snil4+($r2['jml_stok']*$hgbeli);
		                $jstok=$jstok+$r2['jml_stok'];
		            } 
		            else if($n==5){
		                $jmlstok5=$r2['jml_stok'];
		                $snil5=$snil5+($r2['jml_stok']*$hgbeli);
		                $jstok=$jstok+$r2['jml_stok'];
		            }
		            else if($n==6){
		                $jmlstok6=$r2['jml_stok'];
		                $snil6=$snil6+($r2['jml_stok']*$hgbeli);
		                $jstok=$jstok+$r2['jml_stok'];
		            }
		            else if($n==7){
		                $jmlstok7=$r2['jml_stok'];
		                $snil7=$snil7+($r2['jml_stok']*$hgbeli);
		                $jstok=$jstok+$r2['jml_stok'];
		            }
		            else if($n==8){
		                $jmlstok8=$r2['jml_stok'];
		                $snil8=$snil8+($r2['jml_stok']*$hgbeli);
		                $jstok=$jstok+$r2['jml_stok'];
		            }
		            else if($n==9){
		                $jmlstok9=$r2['jml_stok'];
		                $snil9=$snil9+($r2['jml_stok']*$hgbeli);
		                $jstok=$jstok+$r2['jml_stok'];
		            }
		            else if($n==10){
		                $jmlstok10=$r2['jml_stok'];
		                $snil10=$snil10+($r2['jml_stok']*$hgbeli);
		                $jstok=$jstok+$r2['jml_stok'];
		            }
		            $n++;
		        }
			    $nilai=$jstok*$hgbeli;
			
			    $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_stokb'],0)."</td>
				    <td align='right'>".number_format($r['jml_stokb'],0)."</td>";
				    $n1=1;
				    while($n1<$n){
				        if($n1==1) $content .= "<td align='right'>".number_format($jmlstok1,0)."</td>";
				        else if($n1==2) $content .= "<td align='right'>".number_format($jmlstok2,0)."</td>";
				        else if($n1==3) $content .= "<td align='right'>".number_format($jmlstok3,0)."</td>";
				        else if($n1==4) $content .= "<td align='right'>".number_format($jmlstok4,0)."</td>";
				        else if($n1==5) $content .= "<td align='right'>".number_format($jmlstok5,0)."</td>";
				        else if($n1==6) $content .= "<td align='right'>".number_format($jmlstok6,0)."</td>";
				        else if($n1==7) $content .= "<td align='right'>".number_format($jmlstok7,0)."</td>";
				        else if($n1==8) $content .= "<td align='right'>".number_format($jmlstok8,0)."</td>";
				        else if($n1==9) $content .= "<td align='right'>".number_format($jmlstok9,0)."</td>";
                        else if($n1==10) $content .= "<td align='right'>".number_format($jmlstok10,0)."</td>";
                        $n1++;
                        $jml++;
				    }
				    $content .= "<td align='right'>".number_format($jstok,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></td>";
			    $n1=1;
			    while($n1<$n){
			        $content .= "<td></td>";
			        $n1++;
			    }
			    $content .= "<td>Sub Total</td>
			    <td align='right'>".number_format($snilai,2)."</td>
		    </tr>
		    <tr>
			    <td></td>
			    <td></td>
			    <td></td>
			    <td></td>
			    <td></td>
			    <td></td>
			    <td></td>
			    <td></td>
			    <td></td>
			    <td></td>";
			    $n1=1;
			    while($n1<$n){
			        $content .= "<td></td>";
			        $n1++;
			    }
			    $content .= "<td>Sub Total Gudang</td>
			    <td align='right'>".number_format($snilgd,2)."</td>
		    </tr>";
		
		    $sql2=mysql_query("SELECT * FROM cabang WHERE 1 ORDER BY kode_cab");
		    $n2=1;
		    while($r2=mysql_fetch_array($sql2)){
		        $skt=$r2['singkatan'];
		        $content .= "<tr>
		            <td></td>
			        <td></td>
			        <td></td>
			        <td></td>
			        <td></td>
			        <td></td>
			        <td></td>
			        <td></td>
			        <td></td>
			        <td></td>";
			        $n1=1;
			        while($n1<$n){
			            $content .= "<td></td>";
			            $n1++;
			        }
			        $content .= "<td>Sub Total $skt</td>";
			    
			        if($n2==1) $content .= "<td align='right'>".number_format($snil1,2)."</td>";
			        else if($n2==2) $content .= "<td align='right'>".number_format($snil2,2)."</td>";
			        else if($n2==3) $content .= "<td align='right'>".number_format($snil3,2)."</td>";
			        else if($n2==4) $content .= "<td align='right'>".number_format($snil4,2)."</td>";
			        else if($n2==5) $content .= "<td align='right'>".number_format($snil5,2)."</td>";
			        else if($n2==6) $content .= "<td align='right'>".number_format($snil6,2)."</td>";
			        else if($n2==7) $content .= "<td align='right'>".number_format($snil7,2)."</td>";
			        else if($n2==8) $content .= "<td align='right'>".number_format($snil8,2)."</td>";
			        else if($n2==9) $content .= "<td align='right'>".number_format($snil9,2)."</td>";
			        else if($n2==10) $content .= "<td align='right'>".number_format($snil10,2)."</td>";
		        $content .= "</tr>";
		    }
		    $tnilai=$tnilai+$snilai;
		    $tnilgd=$tnilgd+$snilgd;
		    $tnil1=$tnil1+$snil1;
		    $tnil2=$tnil2+$snil2;
		    $tnil3=$tnil3+$snil3;
		    $tnil4=$tnil4+$snil4;
		    $tnil5=$tnil5+$snil5;
		    $tnil6=$tnil6+$snil6;
		    $tnil7=$tnil7+$snil7;
		    $tnil8=$tnil8+$snil8;
		    $tnil9=$tnil9+$snil9;
		    $tnil10=$tnil10+$snil10;
		}
	    $content .= "<tr>
		    <td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>";
			$n1=1;
			while($n1<$n){
			    $content .= "<td></td>";
			    $n1++;
			}
			$content .= "<td>Total</td>
			<td align='right'>".number_format($tnilai,2)."</td>
		</tr>
		<tr>
		    <td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>";
			$n1=1;
			while($n1<$n){
			    $content .= "<td></td>";
			    $n1++;
			}
			$content .= "<td>Total Gudang</td>
			<td align='right'>".number_format($tnilgd,2)."</td>
		</tr>";
		
		$sql2=mysql_query("SELECT * FROM cabang WHERE 1 ORDER BY kode_cab");
		$n2=1;
		while($r2=mysql_fetch_array($sql2)){
		    $skt=$r2['singkatan'];
		    $content .= "<tr>
		        <td></td>
		        <td></td>
			    <td></td>
			    <td></td>
			    <td></td>
			    <td></td>
			    <td></td>
			    <td></td>
			    <td></td>
			    <td></td>";
			    $n1=1;
			    while($n1<$n){
			        $content .= "<td></td>";
			        $n1++;
			    }
			    $content .= "<td>Total $skt</td>";
			    
			    if($n2==1) $content .= "<td align='right'>".number_format($tnil1,2)."</td>";
			    else if($n2==2) $content .= "<td align='right'>".number_format($tnil2,2)."</td>";
			    else if($n2==3) $content .= "<td align='right'>".number_format($tnil3,2)."</td>";
			    else if($n2==4) $content .= "<td align='right'>".number_format($tnil4,2)."</td>";
			    else if($n2==5) $content .= "<td align='right'>".number_format($tnil5,2)."</td>";
			    else if($n2==6) $content .= "<td align='right'>".number_format($tnil6,2)."</td>";
			    else if($n2==7) $content .= "<td align='right'>".number_format($tnil7,2)."</td>";
			    else if($n2==8) $content .= "<td align='right'>".number_format($tnil8,2)."</td>";
			    else if($n2==9) $content .= "<td align='right'>".number_format($tnil9,2)."</td>";
			    else if($n2==10) $content .= "<td align='right'>".number_format($tnil10,2)."</td>";
		    $content .= "</tr>";
		}
	$content .= "<tr></tr>
	</table>
	<table>
		<tr>
			<td>Jumlah data : $jml</td>
		</tr>
	</table>";
				
	header("Content-type: application/x-msdownload");
	header("Content-Disposition: attachment; filename=daftar_sthb.xls");
	header("Pragma: no-cache");
	header("Expires: 0");
	echo $content;
?>