<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 edit_cab(ID){
		var kode    = ID;
	    var kodecab = $("#txt_kodecab").val();
	    
	    $.ajax({
			type	: "POST",
			url		: "modul/inventory/edit_cb.php",
			data	: "kode="+kode+
			        "&kodecab="+kodecab,
			dataType: "json",
			success	: function(data){
				$("#cb_kodest").val(kode);
				$("#cb_kodegr").val(data.kodegr);
				$("#cb_namast").val(data.namast);
				$("#cb_satuan").val(data.satuan);
				$("#cb_hgresep").val(data.hgresep);
				$("#cb_hgparam").val(data.hgparam);
				$("#cb_hgbeli").val(data.hgbeli);
				$("#cb_hgjual").val(data.hgjual);
				$("#cb_minstok").val(data.minstok);
			}
		});
	}
</script>

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

$cari	= $_GET['cari'];
$kodegr	= $_GET['kodegr'];
$kodesp	= $_GET['kodesp'];
$kodecab= $_GET['kodecab'];
$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>Supplier</th>
			<th>Satuan</th>
			<th>Harga Beli</th>
			<th>HB Terakhir</th>
			<th>HJ. Umum</th>
			<th>HJ. Resep</th>
			<th>HJ. Grosir</th>
			<th>Min. Stok</th>
			<th>Jumlah Stok</th>
			<th>Nilai Stok</th>
			<th>Edit</th>
		</tr>";

		if(!empty($cari))
			$sql= "SELECT * FROM stock WHERE kode_cab='$kodecab' 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_cab='$kodecab' AND kode_gr='$kodegr' GROUP BY kode_st";
			    else if($status=='ada') $sql="SELECT * FROM stock WHERE kode_cab='$kodecab' AND kode_gr='$kodegr' AND !(jml_stok=0) GROUP BY kode_st";
			    else if($status=='minimal') $sql= "SELECT * FROM stock WHERE kode_cab='$kodecab' AND kode_gr='$kodegr' AND jml_stok<=min_stok GROUP BY kode_st";
			    else if($status=='expired') $sql= "SELECT * FROM stock WHERE kode_cab='$kodecab' AND kode_gr='$kodegr' AND expired<='$tgllap' GROUP BY kode_st";
		    }
		    else{
		        if(!($kodesp=='')){
			        if($status=='seluruh') $sql= "SELECT * FROM stock WHERE kode_cab='$kodecab' AND kode_sp='$kodesp' GROUP BY kode_st";
    			    else if($status=='ada') $sql= "SELECT * FROM stock WHERE kode_cab='$kodecab' AND kode_sp='$kodesp' AND !(jml_stok=0) GROUP BY kode_st";
	    		    else if($status=='minimal') $sql= "SELECT * FROM stock WHERE kode_cab='$kodecab' AND kode_sp='$kodesp' AND jml_stok<=min_stok GROUP BY kode_st";
		    	    else if($status=='expired') $sql= "SELECT * FROM stock WHERE kode_cab='$kodecab' AND kode_sp='$kodesp' expired<='$tgllap' GROUP BY kode_st";
		        }
		        else{
			        if($status=='seluruh') $sql= "SELECT * FROM stock WHERE kode_cab='$kodecab' GROUP BY kode_st";
			        else if($status=='ada') $sql= "SELECT * FROM stock WHERE kode_cab='$kodecab' AND !(jml_stok=0) GROUP BY kode_st";
			        else if($status=='minimal') $sql= "SELECT * FROM stock WHERE kode_cab='$kodecab' AND jml_stok<=min_stok GROUP BY kode_st";
			        else if($status=='expired') $sql= "SELECT * FROM stock WHERE kode_cab='$kodecab' AND expired<='$tgllap' GROUP BY kode_st";
		        }
		    }
		}
		$query=mysql_query($sql);
			
		$no=1;
		$tnilai=0;
		while($r=mysql_fetch_array($query)){
			$nilai=$r['jml_stok']*$r['hg_beli'];
			
			echo "<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['hg_jual'],0)."</td>
				<td align='right'>".number_format($r['hg_resep'],0)."</td>
				<td align='right'>".number_format($r['hg_param'],0)."</td>
				<td align='right'>".number_format($r['min_stok'],0)."</td>
				<td align='right'>".number_format($r['jml_stok'],0)."</td>
				<td align='right'>".number_format($nilai,2)."</td>
				<td align='center'>
					<a href='javascript:void(0)' onClick=\"edit_cab('$r[kode_st]')\">
						<img src='icon/thumb_up.png' border='0' id='edit' title='Edit' width='12' height='12' >
					</a>
				</td>
			</tr>";
			$tnilai=$tnilai+$nilai;
			$no++;
		}
		
		echo "<tr>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td>Total</td>
			<td align='right'>".number_format($tnilai,2)."</td>
		</tr>
	</table>";

echo "</div>";
?>