<script type="text/javascript">
    $(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';
$cari		= $_GET['cari'];
$gudang		= $_GET['gudang'];
$tglawal 	= $_GET['tglawal'];
$tglakhir	= $_GET['tglakhir'];

echo "<div id='info'>
	<table id='theList' width='100%'>
		<tr>
			<th>No.</th>
			<th>Kode</th>
			<th>Nama Barang</th>
			<th>Barcode</th>
			<th>Stok Awal</th>
			<th>Masuk</th>
			<th>Keluar</th>
			<th>Stok Akhir</th>
		</tr>";
		
		if(!empty($cari)){
		    if($gudang=='Gudang')
			    $sql= "SELECT * FROM stock WHERE kode_cab='01' AND (nama_st LIKE '%$cari%' OR kode_st LIKE '%$cari%') ORDER BY kode_st";
			else
			    $sql= "SELECT * FROM stock WHERE kode_cab='$gudang' AND (nama_st LIKE '%$cari%' OR kode_st LIKE '%$cari%') ORDER BY kode_st";
		}
		else{
		    if($gudang=='Gudang')
		        $sql= "SELECT * FROM stock WHERE kode_cab='01' ORDER BY kode_st ";
		    else
		        $sql= "SELECT * FROM stock WHERE kode_cab='$gudang' ORDER BY kode_st ";
		}
		$query = mysql_query($sql);
		
		$no=1;
		while($r=mysql_fetch_array($query)){
			$kodest=$r['kode_st'];
			if($gudang=='Gudang')
				$jmlstok=$r['jml_stokb'];
			else
				$jmlstok=$r['jml_stok'];
			
			$query1 = mysql_query("SELECT kode_st,sum(masuk) as jmasuk,sum(keluar) as jkeluar FROM rekap_mbr WHERE tanggal>='$tglawal' AND kode_st='$kodest' AND gudang='$gudang' GROUP BY kode_st ");			
			$row1=mysql_num_rows($query1);
			if($row1>0){
				$r1=mysql_fetch_assoc($query1);
				$jmasuk=$r1['jmasuk'];
				$jkeluar=$r1['jkeluar'];
			}else{
				$jmasuk=0;
				$jkeluar=0;
			}
			$stawal=$jmlstok-$jmasuk+$jkeluar;
			
			$query3= mysql_query("SELECT kode_st,sum(masuk) as tmasuk,sum(keluar) as tkeluar FROM rekap_mbr WHERE tanggal BETWEEN '$tglawal' AND '$tglakhir' AND kode_st='$kodest' AND gudang='$gudang'");
			$r3=mysql_fetch_assoc($query3);
			
			$tmasuk=$r3['tmasuk'];
			$tkeluar=$r3['tkeluar'];
			$stakhir=$stawal+$tmasuk-$tkeluar;
				
			echo "<tr>
				<td align='center'>$no</td>
				<td>$r[kode_st]</td>
				<td>$r[nama_st]</td>
				<td>$r[barcode]</td>
				<td align='right'>".number_format($stawal,2)."</td>
				<td align='right'>".number_format($tmasuk,2)."</td>
				<td align='right'>".number_format($tkeluar,2)."</td>
				<td align='right'>".number_format($stakhir,2)."</td>
			</tr>";
			$no++;
		}
	echo "</table>";	
echo "</div>";
?>