<script type="text/javascript">
$(document).ready(function() {
    $(function() {
        $("#theList tr:even").addClass("stripe1");
        $("#theList tr:odd").addClass("stripe2");

        $("#theList tr").hover(
            function() {
                $(this).toggleClass("highlight");
            },
            function() {
                $(this).toggleClass("highlight");
            }
        );
    });
});
</script>

<?php
include '../../inc/inc.koneksi.php';
include '../../inc/fungsi_tanggal.php';

$cari	= $_GET['cari'];
$kodegr	= $_GET['kodegr'];
$status	= $_GET['status'];
$tgllap	= date('Y-m-d');

echo "<table id='theList' width='100%'>
		<tr>
			<th>No.</th>
			<th>Kode</th>
			<th>Nama Barang</th>
			<th>Barcode</th>
			<th>Satuan</th>
			<th>Min. Stok</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'];
			    echo "<th>Stok $skt</th>";
			}
			echo "<th>Total Stok</th>
		</tr>";

		if(!empty($cari))
			$sql= "SELECT * FROM stock WHERE 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='$kodegr' GROUP BY kode_st";
			else if($status=='ada') $sql="SELECT * FROM stock WHERE kode_gr='$kodegr' AND (!(jml_stok=0) OR !(jml_stokb=0)) GROUP BY kode_st";
			else if($status=='minimal') $sql= "SELECT * FROM stock WHERE kode_gr='$kodegr' AND (jml_stok<=min_stok OR jml_stokb<=min_stok)  GROUP BY kode_st";
			else if($status=='expired') $sql= "SELECT * FROM stock WHERE kode_gr='$kodegr' AND expired<='$tgllap' GROUP BY kode_st";
		}else{
			if($status=='seluruh') $sql= "SELECT * FROM stock WHERE 1 GROUP BY kode_st";
			else if($status=='ada') $sql= "SELECT * FROM stock WHERE !(jml_stok=0) OR !(jml_stokb=0) GROUP BY kode_st";
			else if($status=='minimal') $sql= "SELECT * FROM stock WHERE jml_stok<=min_stok OR jml_stokb<=min_stok GROUP BY kode_st";
			else if($status=='expired') $sql= "SELECT * FROM stock WHERE expired<='$tgllap' GROUP BY kode_st";
		}
		$query=mysql_query($sql);
			
		$no=1;
		$tnilai=0;
		$tnil1=0;$tnil2=0;$tnil3=0;$tnil4=0;$tnil5=0;$tnil6=0;$tnil7=0;$tnil8=0;$tnil9=0;$tnil10=0;
		while($r=mysql_fetch_array($query)){
		    $kodest=$r['kode_st'];
		    $jstok=0;
		    
		    $sql1=mysql_query("SELECT * FROM stock WHERE kode_st='$kodest' ORDER BY kode_cab");
		    $n=1;
		    while($r1=mysql_fetch_array($sql1)){
		        if($n==1){
		            $jmlstokb=$r1['jml_stokb'];
		            $jmlstok1=$r1['jml_stok'];
		            $jstok=$jstok+$r1['jml_stok']+$r1['jml_stokb'];
		        }
		        else if($n==2){
		            $jmlstok2=$r1['jml_stok'];
		            $jstok=$jstok+$r1['jml_stok'];
		        }
		        else if($n==3){
		            $jmlstok3=$r1['jml_stok'];
		            $jstok=$jstok+$r1['jml_stok'];
		        }
		        else if($n==4){
		            $jmlstok4=$r1['jml_stok'];
		            $jstok=$jstok+$r1['jml_stok'];
		        } 
		        else if($n==5){
		            $jmlstok5=$r1['jml_stok'];
		            $jstok=$jstok+$r1['jml_stok'];
		        }
		        else if($n==6){
		            $jmlstok6=$r1['jml_stok'];
		            $jstok=$jstok+$r1['jml_stok'];
		        }
		        else if($n==7){
		            $jmlstok7=$r1['jml_stok'];
		            $jstok=$jstok+$r1['jml_stok'];
		        }
		        else if($n==8){
		            $jmlstok8=$r1['jml_stok'];
		            $jstok=$jstok+$r1['jml_stok'];
		        } 
		        else if($n==9){
		            $jmlstok9=$r1['jml_stok'];
		            $jstok=$jstok+$r1['jml_stok'];
		        }
		        else if($n==10){
		            $jmlstok10=$r1['jml_stok'];
		            $jstok=$jstok+$r1['jml_stok'];
		        }
		        $n++;
		    }
			
			echo "<tr>
				<td>$no</td>
				<td>$r[kode_st]</td>
				<td>$r[nama_st]</td>
				<td>$r[barcode]</td>
				<td>$r[satuan]</td>
				<td align='right'>".number_format($r['min_stok'],0)."</td>
				<td align='right'>".number_format($jmlstokb,0)."</td>";
				$n1=1;
				while($n1<$n){
				    if($n1==1) echo "<td align='right'>".number_format($jmlstok1,0)."</td>";
				    else if($n1==2) echo "<td align='right'>".number_format($jmlstok2,0)."</td>";
				    else if($n1==3) echo "<td align='right'>".number_format($jmlstok3,0)."</td>";
				    else if($n1==4) echo "<td align='right'>".number_format($jmlstok4,0)."</td>";
				    else if($n1==5) echo "<td align='right'>".number_format($jmlstok5,0)."</td>";
				    else if($n1==6) echo "<td align='right'>".number_format($jmlstok6,0)."</td>";
				    else if($n1==7) echo "<td align='right'>".number_format($jmlstok7,0)."</td>";
				    else if($n1==8) echo "<td align='right'>".number_format($jmlstok8,0)."</td>";
				    else if($n1==9) echo "<td align='right'>".number_format($jmlstok9,0)."</td>";
                    else if($n1==10) echo "<td align='right'>".number_format($jmlstok10,0)."</td>";
                    $n1++;
				}
				echo "<td align='right'>".number_format($jstok,0)."</td>
			</tr>";
			$no++;
		}
		echo "<tr></tr>
	</table>";

echo "</div>";
?>