<script type="text/javascript">
$(document).ready(function(){
	$("#txt_tanggal").val('');
	
	$("#mulai").click(function(){
		$(".input_detail").val('');
		$(".input_header").val('');		
		$("#txt_caribr").val('');
		cari_nomor();
	});
	
	function cari_nomor(){
		var no = 1;
		$.ajax({
			type	: "POST",
			url		: "spv/st_opname/cari_nomor.php",
			data	: "no="+no,
			dataType: "json",
			success	: function(data){
				$("#txt_kode").val(data.nomor);
				$("#txt_tanggal").val(data.tanggal);
				$("#txt_tanggal").focus();
				tampil_data_detail();
			}
		});
	}
	
	function tampil_data_detail(){
		var kode = $("#txt_kode").val();
		$("#data_detail").show();
		$("#data_detail").load("spv/st_opname/tampil_data_detail.php?kode="+kode);
		cari_total();
	}
	
	$('#form_cari_kode').dialog({
		autoOpen: false,
		modal	: true,
		width: 600,
	});
		
	$('#cari_kode').click(function(){
		$('#form_cari_kode').dialog('open');		
		no=1
		
		$.ajax({
			type	: "POST",
			url		: "spv/st_opname/tampil_data_op.php",
			data	: "no="+no,
			timeout	: 3000,
			beforeSend	: function(){
				$("#info_kode").html("<img src='loading.gif' />");			
			},				  
			success	: function(data){				
				$("#info_kode").html(data);
				tampil_data_detail();
			}
		});
	});
	
	$('#form_cari_barang').dialog({
		autoOpen: false,
		modal	: true,
		width: 600,
	});
		
	$('#txt_namast').click(function(){
		$('#form_cari_barang').dialog('open');
		$("#txt_caribr").focus();
		return false;
	});
	
	$('#txt_namast').keyup(function(){
		$('#form_cari_barang').dialog('open');
		$("#txt_caribr").focus();
		return false;
	});
	
	$("#txt_caribr").keyup(function(){		
		var cari	= $("#txt_caribr").val();
		var gudang	= $("#txt_gudang").val();
		$.ajax({
			type	: "GET",
			url		: "spv/st_opname/tampil_data_barang.php",
			data	: "cari="+cari+
				"&gudang="+gudang,
			timeout	: 3000,
			beforeSend	: function(){		
				$("#info_barang").html("<img src='loading.gif' />");			
			},				  
			success	: function(data){
				$("#info_barang").html(data);
			}
		});
	});
	
	$("#txt_jmlopn").keyup(function(){
		var jopn  = $("#txt_jmlopn").val();
		var jstok = $("#txt_jmlstok").val();
		var	hbeli = $("#txt_hgbeli").val();
		if(jstok=='')
			var jsls= parseInt(jopn)
		else
			var jsls= parseInt(jopn)-parseInt(jstok);
		var	nilsls = jsls*parseInt(hbeli);
		
		$("#txt_jmlsls").val(jsls);
		$("#txt_nilaisls").val(nilsls);
	});
	
	$("#simpan").click(function(){
		simpan_data();		
	});
		
	function simpan_data(){
		var	kode	= $("#txt_kode").val();		
		var	tanggal	= $("#txt_tanggal").val();		
		var	kodest	= $("#txt_kodest").val();
		var	namast	= $("#txt_namast").val();
		var	satuan	= $("#txt_satuan").val();
		var	gudang	= $("#txt_gudang").val();
		var	jmlstok	= $("#txt_jmlstok").val();		
		var	hgbeli	= $("#txt_hgbeli").val();
		var	jmlopn	= $("#txt_jmlopn").val();
		var	jmlsls	= $("#txt_jmlsls").val();
		var	nilsls	= $("#txt_nilaisls").val();
		
		var error = false;
		
		if(tanggal.length == 0){
           var error = true;
           alert("Maaf, Tanggal opname tidak boleh kosong.");
		   return (false);
        }
		
		if(kode.length == 0){
           var error = true;
           alert("Maaf, Kode faktur masih kosong, klik Input Stok Opname untuk buat nomor baru.");
		   return (false);
        }
				
		if(kodest.length == 0){
           var error = true;
           alert("Maaf, Kode Barang tidak boleh kosong");
		   $("#txt_kodest").focus();
		   return (false);
        }
		
		if(jmlopn.length == 0){
			var error = true;
			alert("Maaf, Jumlah stok fisik belum diisi.");
			$("#txt_jmlopn").focus();
			return (false);
         }
		  
		if(error == false){
		
		$.ajax({
			type	: "POST",
			url		: "spv/st_opname/simpan.php",
			data	: "kode="+kode+
					"&tanggal="+tanggal+					
					"&kodest="+kodest+
					"&namast="+namast+
					"&satuan="+satuan+
					"&hgbeli="+hgbeli+
					"&gudang="+gudang+
					"&jmlstok="+jmlstok+
					"&jmlopn="+jmlopn+
					"&jmlsls="+jmlsls+
					"&nilsls="+nilsls,
								
			timeout	: 3000,
			beforeSend	: function(){		
				$("#info").html("<img src='loading.gif' />");			
			},				  
			success	: function(data){
				$("#info").html(data);
				$(".input_detail").val('');
				$("#txt_kodest").focus();
				tampil_data_detail();
			}
		});
		}
		return false;
	}
		
	$("#tutup").click(function(){
		var	total	 = $("#txt_total").val();		
		document.location='?module=home';
	});
});
</script>

<style type="text/css">
#txt_kode {
	background:#CCC;
}
#cari_produk{
	cursor:pointer;
}
#cari_buyer{
	cursor:pointer;
}
.input_detail readonly {
	background:#666;
}
</style>

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

echo "<table>
	<tr>
		<td>Kode Opname</td>
		<td>:</td>
		<td>
			<input type='text' name='txt_kode' id='txt_kode' size='9' class='input_header' readonly />
			<img src='images/view.png' width='18' height='18' title='Cari Kode Opname' id='cari_kode'>
		</td>
		<td></td>
		<td>Tanggal</td>
		<td>:</td>
		<td><input type='date' name='txt_tanggal' id='txt_tanggal' size='10' ></td>
		<td>Tanki</td>
		<td>:</td>
		<td><select name='txt_gudang' id='txt_gudang' class='input_header' >
			<option>1</option>
			<option>2</option>
			<option>3</option></select>
		</td>
	</tr>
	</table>
	<hr>";

echo "<table width='100%' align='center'>
		<tr>
			<th>Nama Barang</th>
			<th>Kode</th>			
			<th>Satuan</th>
			<th>Hrg. Pokok</th>
			<th>Jml. Stok</th>
			<th>Jml. Fisik</th>
			<th>Selisih</th>
			<th>Nilai Selisih</th>
		</tr>		
		<tr>
			<td><input type='text' name='txt_namast' id='txt_namast' class='input_detail' size='22'></td>
			<td><input type='text' name='txt_kodest' id='txt_kodest' class='input_detail' size='5' readonly/></td>
			<td><input type='text' name='txt_satuan' id='txt_satuan' class='input_detail' size='8' readonly/></td>
			<td><input type='text' name='txt_hgbeli' id='txt_hgbeli' class='input_detail' size='8' readonly/></td>
			<td><input type='text' name='txt_jmlstok' id='txt_jmlstok' class='input_detail' size='8' readonly/></td>
			<td><input type='text' name='txt_jmlopn' id='txt_jmlopn' class='input_detail' size='8' ></td>
			<td><input type='text' name='txt_jmlsls' id='txt_jmlsls' class='input_detail' size='8' readonly /></td>
			<td><input type='text' name='txt_nilaisls' id='txt_nilaisls' class='input_detail' size='9' readonly /></td>
		</tr>
		<tr>
			<td colspan='12' align='center'>
				<button name='simpan' id='simpan'>Rekam</button></td>
			</td>
		</tr>
	</table>";

echo "<div id='form_cari_kode' title='Pencarian Kode Opname'>
		<div id='info_kode' align='center'></div>
	</div>";

echo "<div id='data_detail'></div>";
	
echo "<div id='form_cari_barang' title='Pencarian Barang'>
		<table width='100%'>
			<tr>
				<td width='15%'>Cari Barang</td>
				<td width='2%'>:</td>
				<td><input type='text' name='txt_caribr' id='txt_caribr' size='40'</td>
			</tr>
		</table>
		<div id='info_barang' align='center'></div>
	</div>";

echo "<table width='100%'>
	<tr>
		<td colspan='3' align='center'>
		<button name='mulai' id='mulai'>Input Stok Opname</button>
		<button name='tutup' id='tutup'>Tutup</button>
		</td>
	</tr>
	</table>";
?>