<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];
$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>Stok Awal</th>
			<th>Pembelian</th>
			<th>Retur Penjualan</th>
			<th>Stok Opname</th>
			<th>Total Masuk</th>
			<th>Penjualan</th>
			<th>Retur Pembelian</th>
			<th>Total Keluar</th>
			<th>Stok Akhir</th>
		</tr>";
		
		if (!empty($cari)){
			$sql= "SELECT * FROM stock WHERE nama_st LIKE '%$cari%' OR kode_st LIKE '%$cari%' ORDER BY kode_st";
		}
		else{
			$sql= "SELECT * FROM stock ORDER BY kode_st ";
		}
		$query = mysql_query($sql);
		
		$no=1;
		while($r_data=mysql_fetch_array($query)){
			$kodest=$r_data[kode_st];
			$jmlstok=$r_data[jml_stok];
		
			$sql1 	= "SELECT kode_st,sum(masuk) as jmasuk,sum(keluar) as jkeluar FROM rekap_mbr WHERE tanggal>='$tglawal' AND kode_st='$kodest' AND batal='' GROUP BY kode_st ";
			$query1 = mysql_query($sql1);
			while($r1=mysql_fetch_array($query1)){			
				$jmasuk=$r1[jmasuk];
				$jkeluar=$r1[jkeluar];
			}
			$stawal=$jmlstok-$jmasuk+$jkeluar;
			
			$sql3 = "SELECT * FROM rekap_mbr WHERE tanggal BETWEEN '$tglawal' AND '$tglakhir' AND kode_st='$kodest' AND batal='' ";
			$query3= mysql_query($sql3);
			
			$jual=0;
			$retjual=0;
			$beli=0;			
			$retbeli=0;
			$opname=0;
			
			while($r=mysql_fetch_array($query3)){
				if($r[jurnal]=='PJL')
					$jual=$jual+$r[keluar];
				else if($r[jurnal]=='PBL')
					$beli=$beli+$r[masuk];
				else if($r[jurnal]=='RPB')
					$retbeli=$retbeli+$r[keluar];
				else if($r[jurnal]=='RPJ')
					$retjual=$retjual+$r[masuk];
				else if($r[jurnal]=='SO')
					$opname=$opname+$r[masuk];
			}
			$tmasuk=$beli+$retjual+$opname;
			$tkeluar=$jual+$retbeli;
			$stakhir=$stawal+$tmasuk-$tkeluar;
				
			echo "<tr>
					<td>$no</td>
					<td>$r_data[kode_st]</td>
					<td>$r_data[nama_st]</td>
					<td align='right'>".number_format($stawal,2)."</td>
					<td align='right'>".number_format($beli,2)."</td>
					<td align='right'>".number_format($retjual,2)."</td>
					<td align='right'>".number_format($opname,2)."</td>
					<td align='right'>".number_format($tmasuk,2)."</td>
					<td align='right'>".number_format($jual,2)."</td>
					<td align='right'>".number_format($retbeli,2)."</td>
					<td align='right'>".number_format($tkeluar,2)."</td>
					<td align='right'>".number_format($stakhir,2)."</td>
				</tr>";
			$no++;
		}
	echo "</table>";	
echo "</div>";
?>