<script type="text/javascript">
$(document).ready(function(){
	
	$("#mulai").click(function(){
		$(".input_detail").val('');
		$(".input_header").val('');		
		$("#txt_caribr").val('');
		cari_tgl();
	});
		
	function cari_tgl(){
		var no = 1;
		$.ajax({
			type	: "POST",
			url		: "modul/report/cari_tanggal.php",
			data	: "no="+no,
			dataType: "json",
			success	: function(data){				
				$("#txt_tanggal").val(data.tanggal);
				$("#txt_tanggal").focus();
				cari_nomor();
			}
		});
	}
	
	function cari_nomor(){
		var tanggal = $("#txt_tanggal").val();
		$.ajax({
			type	: "POST",
			url		: "modul/opname/cari_nomor.php",
			data	: "tanggal="+tanggal,
			dataType: "json",
			success	: function(data){
				$("#txt_kode").val(data.nomor);
				cari_nourut();
				tampil_data_detail();
			}
		});
	}
		
	$('#txt_tanggal').keyup(function(){
		cari_nomor();		
		return false;
	});
	
	function tampil_data_detail(){
		var kode = $("#txt_kode").val();
		$("#data_detail").show();
		$("#data_detail").load("modul/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');
		$("#info_kode").load('modul/opname/tampil_data_op.php?');
		return false;
	});
	
	$('#form_cari_barang').dialog({
		autoOpen: false,
		modal	: true,
		width: 600,
	});
		
	$('#txt_namast').keyup(function(){		
		var nourut  = $("#txt_nourut").val();
		
		if(nourut=='') cari_nourut();
		$('#form_cari_barang').dialog('open');
		$("#info_barang").load('modul/opname/tampil_data_barang.php?');
		$("#txt_caribr").focus();
		return false;
	});
	
	function cari_nourut(){
		var kode = $("#txt_kode").val();
				
		$.ajax({
			type	: "POST",
			url		: "modul/opname/cari_nourut.php",
			data	: "kode="+kode,
			dataType: "json",
			success	: function(data){				
				$("#txt_nourut").val(data.nourut);		
			}
		});
	}
	
	$('#txt_namast').click(function(){
		var nourut  = $("#txt_nourut").val();
		
		if(nourut=='') cari_nourut();
		
		$('#form_cari_barang').dialog('open');
		$("#info_barang").load('modul/opname/tampil_data_barang.php?');		
		$("#txt_caribr").focus();
		return false;
	});
	
	$("#txt_caribr").keyup(function(){		
		var cari 	= $("#txt_caribr").val();
		$.ajax({
			type	: "GET",
			url		: "modul/opname/tampil_data_barang.php",
			data	: "cari="+cari,
			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();
		var hbeli = parseFloat(hbeli).toFixed(2);
		var jsls  = parseFloat(jopn)-parseFloat(jstok);
		var jsls  = jsls.toFixed(2);
		var	nilsls= jsls*hbeli;
		var nilsls= nilsls.toFixed(2);
		
		$("#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	nourut  = $("#txt_nourut").val();
		var	kodest	= $("#txt_kodest").val();
		var	namast	= $("#txt_namast").val();
		var	satuan	= $("#txt_satuan").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 SO 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		: "modul/opname/simpan.php",
			data	: "kode="+kode+
					"&tanggal="+tanggal+
					"&nourut="+nourut+
					"&kodest="+kodest+
					"&namast="+namast+
					"&satuan="+satuan+
					"&hgbeli="+hgbeli+
					"&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_caribr").val('');
				$("#txt_namast").focus();
				cari_nourut();
				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;
}
</style>

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

echo "<table>	
	<tr>
		<td>Kode SO : <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>Tanggal : <input type='date' name='txt_tanggal' id='txt_tanggal' size='10' ></td>
	</tr>
	</table>
	<hr>";

echo "<table>
		<tr>
			<th>Nama Barang</th>
			<th>Kode</th>			
			<th>Satuan</th>
			<th>Hrg. Beli</th>
			<th>Jml. Stok</th>
			<th>Jml. Fisik</th>
			<th>Selisih</th>
			<th>Nilai Selisih</th>
			<th></th>
		<tr>
		
		<tr>
			<input type='text' name='txt_nourut' id='txt_nourut' class='input_detail' size='4' hidden/>
			<td align='center'><input type='text' name='txt_namast' id='txt_namast' class='input_detail' size='22'></td>
			<td align='center'><input type='text' name='txt_kodest' id='txt_kodest' class='input_detail' size='6' readonly /<td>
			<td align='center'><input type='text' name='txt_satuan' id='txt_satuan' class='input_detail' size='8' readonly /></td>
			<td align='center'><input type='text' name='txt_hgbeli' id='txt_hgbeli' class='input_detail' size='8' ></td>
			<td align='center'><input type='text' name='txt_jmlstok' id='txt_jmlstok' class='input_detail' size='8' ></td>
			<td align='center'><input type='text' name='txt_jmlopn' id='txt_jmlopn' class='input_detail' size='8' ></td>
			<td align='center'><input type='text' name='txt_jmlsls' id='txt_jmlsls' class='input_detail' size='8' readonly /></td>
			<td align='center'><input type='text' name='txt_nilaisls' id='txt_nilaisls' class='input_detail' size='9' readonly /></td>
			<td align='center'>
				<button name='simpan' id='simpan'>Rekam</button></td>
			</td>
		</tr>
	</table>
	<hr>";

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>";
?>