<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");
            }
        );
    });
});
	
	function pilih(ID){
		var kode    = ID;
		var tglawal = $("#ks_tglawal").val();
		var tglakhir= $("#ks_tglakhir").val();
		var gudang  = $("#ks_tanki").val();

		if(tglawal.length == 0){			
			alert("Maaf, Tanggal awal pencarian belum ditentukan.");
			$("#ks_tglawal").focus();
			return (false);
        }
		 
		if(tglakhir.length == 0){
			alert("Maaf, Tanggal akhir pencarian belum ditentukan.");
			$("#ks_tglakhir").focus();
		   return (false);
        }
		 
		$.ajax({
			type	: "POST",
			url		: "modul/inventory/cari_st.php",
			data	: "kode="+kode+				
				"&gudang="+gudang,
			dataType: "json",
			success	: function(data){
				$("#t_kodest").val(kode);
				$("#t_namast").val(data.namast);
				$("#t_jmlstok").val(data.jmlstok);
				$("#t_satuan").val(data.tsatuan);
				$("#pencarian_ks").hide();
				$("#tampil_data_ks").hide();
				$("#info_ks").show();
				$("#tampil_detail_ks").show();
				$("#tampil_detail_ks").load("modul/inventory/tampil_detail_ks.php?kode="+kode+"&gudang="+gudang+"&tglawal="+tglawal+"&tglakhir="+tglakhir);
			}
		});		
	}
</script>

<?php
include '../../inc/inc.koneksi.php';
$cari	= $_GET[cari];
$tglawal = $_GET[tglawal];
$tglakhir= $_GET[tglakhir];
$gudang = $_GET[gudang];

echo "<table id='theList' width='100%'>
		<tr>
			<th>No.</th>
			<th>Kode</th>
			<th>Nama Barang</th>			
			<th>Jumlah Stok</th>
			<th>Satuan</th>
			<th>Pilih</th>
		</tr>";
		
		if (!empty($cari)){
			$sql= "SELECT * FROM stock WHERE nama_st LIKE '%$cari%' or kode_st LIKE '%$cari%'";
		}
		else{
			$sql= "SELECT * FROM stock ORDER BY kode_st";
		}
		$query = mysql_query($sql);
			
		$no=1;
		while($r_data=mysql_fetch_array($query)){			
			if($gudang=='1')
				$jmlstok=$r_data[jml_gd1];
			else if($gudang=='2')
				$jmlstok=$r_data[jml_gd2];
			else
				$jmlstok=$r_data[jml_gd3];
			
			$sql1 = "SELECT kode_st,gudang,masuk,keluar FROM rekap_mbr WHERE tanggal>'$tglakhir' AND kode_st='$kodest' AND gudang='$gudang' AND batal='' ORDER BY kode_st AND tanggal ";
			$query1= mysql_query($sql1);
			$jmasuk=0;
			$jkeluar=0;
			
			while($r1=mysql_fetch_array($query1)){			
				$jmasuk=$jmasuk+$r1[masuk];
				$jkeluar=$jkeluar+$r1[keluar];
			}
			$jmlstok=$jmlstok-$jmasuk+$jkeluar;
			
			echo "<tr>
				<td>$no</td>
				<td>$r_data[kode_st]</td>
				<td>$r_data[nama_st]</td>					
				<td align='right'>".number_format($jmlstok,2)."</td>
				<td>$r_data[satuan]</td>
				<td align='center'>
					<a href='javascript:void(0)' onClick=\"pilih('$r_data[kode_st]')\">
					<img src='images/view.png' border='0' id='edit' title='Lihat Kartu Stok' width='18' height='18'></a>						
				</td>
			</tr>";
			$no++;
		}
	
	echo "</table>";
echo "</div>";
?>
