<script type="text/javascript">
$(document).ready(function(){
	$("#mulai").focus();

	$("#mulai").click(function(){
		var	kode = $("#txt_kode").val();
		var	total= $("#txt_total").val();
		
		if(!(total.length==0)){
			alert("Maaf, input pemakaian bahan "+kode+" belum direkam.");
			$("#rekam_psb").focus();
			return (false);
		}
		$(".input_detail").val('');
		$(".input_header").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_kodetk").val(data.kodetk);
				cari_nomor();
				$("#txt_tanggal").focus();
			}
		});
	}
	
	function cari_nomor(){
		var tanggal = $("#txt_tanggal").val();
		var kodetk  = $("#txt_kodetk").val();
		
		$.ajax({
			type	: "POST",
			url		: "modul/ubahstatus/cari_nomor.php",
			data	: "tanggal="+tanggal+
					"&kodetk="+kodetk,
			dataType: "json",
			success	: function(data){
				$("#txt_kode").val(data.nomor);
				$('#data_detail').show();
				cari_nourut();
				tampil_data_detail();
			}
		});
	}
	
	function cari_nourut(){
		var kode = $("#txt_kode").val();
		var kodetk = $("#txt_kodetk").val();
				
		$.ajax({
			type	: "POST",
			url		: "modul/ubahstatus/cari_nourut.php",
			data	: "kode="+kode+
					"&kodetk="+kodetk,
			dataType: "json",
			success	: function(data){
				$("#txt_nourut").val(data.nourut);
			}
		});
	}
	
	function tampil_data_detail(){
		var kode = $("#txt_kode").val();
		var kodetk  = $("#txt_kodetk").val();
		
		$("#data_detail").show();
		$("#data_detail").load("modul/ubahstatus/tampil_data_detail.php?kode="+kode+'&kodetk='+kodetk);
		hitung_total();
	}
	
	function hitung_total(){
		var kode   = $("#txt_kode").val();
		var kodetk = $("#txt_kodetk").val();
		var jumlahasl = $("#txt_jumlahasl").val();
		
		$.ajax({
			type	: "POST",
			url		: "modul/ubahstatus/cari_total.php",
			data	: "kode="+kode+
					"&kodetk="+kodetk+
					"&jumlahasl="+jumlahasl,
			dataType: "json",
			success	: function(data){
				$("#txt_total").val(data.total);
				$("#txt_selisih").val(data.selisih);
			}
		});
	}
	
	$("#txt_tanggal").keyup(function(){
		cari_nomor();
		return(false);
	});
	
	$('#form_cari_br').dialog({
		autoOpen: false,
		modal	: true,
		width: 700,
	});
	
	$('#txt_namast').keyup(function(){
		var kodetk = $("#txt_kodetk").val();
		
		$('#form_cari_br').dialog('open');
		$("#info_br").load('modul/ubahstatus/tampil_data_br.php?kodetk='+kodetk);
		$("#txt_caribr").focus();
		return false;
	});
	
	$('#txt_namast').click(function(){
		var kodetk = $("#txt_kodetk").val();
		
		$('#form_cari_br').dialog('open');
		$("#info_br").load('modul/ubahstatus/tampil_data_br.php?kodetk='+kodetk);
		$("#txt_caribr").focus();
		return false;
	});
	
	$("#txt_caribr").keyup(function(){
		var cari= $("#txt_caribr").val();
		var kodetk = $("#txt_kodetk").val();
		
		$.ajax({
			type	: "GET",
			url		: "modul/ubahstatus/tampil_data_br.php",
			data	: "cari="+cari+
					"&kodetk="+kodetk,
			timeout	: 2000,
			beforeSend	: function(){	
				$("#info_br").html("<img src='loading.gif' />");
			},				  
			success	: function(data){				
				$("#info_br").html(data);
			}
		});
	});
		
	$("#txt_jmlunit").keyup(function(){
		var jmlunit = $("#txt_jmlunit").val();
		var harga   = $("#txt_harga").val();
		var jmlstok = $("#txt_jmlstok").val();
		var total   = $("#txt_total").val();
		var nharga  = parseFloat(harga);
		var nunit	= parseFloat(jmlunit);
		var nstok	= parseFloat(jmlstok);
		if(total=='')
			var ntotal=0;
		else
			var ntotal	= parseFloat(total);
		
		if(nstok<nunit){
			alert('Maaf, Jumlah diubah lebih besar dari stok '+jmlstok+'.');
			return (false);
		}
		var jumlahasl = nunit*nharga;
		var selisih = ntotal-jumlahasl;
		$("#txt_jumlahasl").val(jumlahasl);
		$("#txt_selisih").val(selisih);
	});
	
	$('#form_cari_br1').dialog({
		autoOpen: false,
		modal	: true,
		width: 700,
	});

	$('#txt_namast1').keyup(function(){
		var namast1 = $('#txt_namast1').val();
		
		if(namast1=='Q'){
			$("#rekam_psb").focus();
			return false;
		}else if(namast1=='q'){
			$("#rekam_psb").focus();
			return false;
		}else{
			
			$('#form_cari_br1').dialog('open');
			$("#info_br1").load('modul/ubahstatus/tampil_data_br1.php');
			$("#txt_caribr1").focus();
			return false;
		}
	});
	
	$("#txt_caribr1").keyup(function(){
		var cari= $("#txt_caribr1").val();
		var kodetk = $("#txt_kodetk").val();
		
		$.ajax({
			type	: "GET",
			url		: "modul/ubahstatus/tampil_data_br1.php",
			data	: "cari="+cari+
					"&kodetk="+kodetk,
			timeout	: 2000,
			beforeSend	: function(){	
				$("#info_br1").html("<img src='loading.gif' />");
			},				  
			success	: function(data){				
				$("#info_br1").html(data);
			}
		});
	});
	
	$("#txt_jmlunit1").keyup(function(){
		var junit1 = $("#txt_jmlunit1").val();
		var nunit1 = parseFloat(junit1);
		var jharga = $("#txt_harga1").val();
		var nharga1= parseFloat(jharga);
		var njumlah= nunit1*nharga1;
		var njumlah= njumlah.toFixed(2);
		$("#txt_jumlah").val(njumlah);
	});
	
	$("#simpan").click(function(){
		simpan_data();
	});
		
	function simpan_data(){
		var	kode    = $("#txt_kode").val();
		var	tanggal = $("#txt_tanggal").val();
		var	kodetk  = $("#txt_kodetk").val();
		var	kodest	= $("#txt_kodest").val();
		var	namast	= $("#txt_namast").val();
		var	jmlunit	= $("#txt_jmlunit").val();
		var	satuan	= $("#txt_satuan").val();
		var	harga 	= $("#txt_harga").val();
		var	jumlahasl= $("#txt_jumlahasl").val();
		var	nourut	= $("#txt_nourut").val();
		var	kodest1 = $("#txt_kodest1").val();
		var	namast1	= $("#txt_namast1").val();		
		var	satuan1	= $("#txt_satuan1").val();
		var	jmlunit1 = $("#txt_jmlunit1").val();
		var	harga1	= $("#txt_harga1").val();
		var	jumlah  = $("#txt_jumlah").val();
		var total   
		
		var error = false;
		
		if(kode.length == 0){
			var error = true;
			alert("Maaf, Kode transaksi masih kosong. Klik tombol Input untuk mulai.");
			$("#mulai").focus();
			return (false);
        }
		
		if(jmlunit.length == 0){
			alert("Maaf, Jumlah diubah belum diisi.");
			$("#txt_jmlunit").focus();
			return (false);
        }
		
		if(kodest1.length == 0){
			var error = true;
			alert("Maaf, Kode stok hasil masih kosong.");
			$("#txt_kodest1").focus();
			return (false);
        }
		
		if(error == false){
		$.ajax({
			type	: "POST",
			url		: "modul/ubahstatus/simpan.php",
			data	: "kode="+kode+
					"&tanggal="+tanggal+
					"&kodetk="+kodetk+
					"&kodest="+kodest+
					"&namast="+namast+
					"&jmlunit="+jmlunit+
					"&satuan="+satuan+
					"&harga="+harga+
					"&jumlahasl="+jumlahasl+
					"&nourut="+nourut+
					"&kodest1="+kodest1+
					"&namast1="+namast1+
					"&satuan1="+satuan1+
					"&jmlunit1="+jmlunit1+
					"&harga1="+harga1+
					"&jumlah="+jumlah,
			timeout	: 2000,
			beforeSend	: function(){		
				$("#info").html("<img src='loading.gif' />");
			},				  
			success	: function(data){
				$("#info").html(data);
				$(".input_detail").val('');
				cari_nourut();
				tampil_data_detail();
			}
		});
		}
		return false;
	}
	
	$("#rekam_psb").click(function(){
		rekam_psb();
	});
		
	function rekam_psb(){
		var	kode    = $("#txt_kode").val();
		var	tanggal = $("#txt_tanggal").val();
		var	kodetk  = $("#txt_kodetk").val();
		var	kodest  = $("#txt_kodest").val();
		var	jmlunit = $("#txt_jmlunit").val();
		var	satuan  = $("#txt_satuan").val();
		var	harga   = $("#txt_harga").val();
		var	jumlahasl= $("#txt_jumlahasl").val();
		var	kodest1 = $("#txt_kodest1").val();
	
		var error = false;
		
		if(kode.length == 0){
			var error = true;
			alert("Maaf, Data input perubahan status masih kosong.");
			return (false);
        }
				
		if(error == false){
		$.ajax({
			type	: "POST",
			url		: "modul/ubahstatus/simpan_psb.php",
			data	: "kode="+kode+
					"&tanggal="+tanggal+
					"&kodetk="+kodetk+
					"&kodest="+kodest+
					"&jmlunit="+jmlunit+
					"&satuan="+satuan+
					"&harga="+harga+
					"&jumlahasl="+jumlahasl,
					
			timeout	: 2000,
			beforeSend	: function(){		
				$("#info").html("<img src='loading.gif' />");
			},				  
			success	: function(data){
				$("#info").html(data);
				$(".input_detail").val('');
				$(".input_header").val('');
				$("#mulai").focus();
				$("#data_detail").hide();
			}
		});
		}
		return false;
	}
		
	$("#tutup").click(function(){	
		document.location='?module=home';
	});
	
	$("#batal_psb").click(function(){
		var kode = $("#txt_kode").val();
		$.ajax({
			type	: "POST",
			url		: "modul/ubahstatus/batal.php",
			data	: "kode="+kode,
			success	: function(data){
				$("#info").html(data);
				$(".input_detail").val('');
				$(".input_header").val('');
				$("#data_detail").hide();
			}
		});
	});
});
</script>

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

<?php
	
echo "<table width='100%'>
	<tr>
		<input type='text' name='txt_kodetk' id='txt_kodetk' class='input_header' hidden/>
		<input type='text' name='txt_total' id='txt_total' class='input_header' hidden />
		<td>Kode Transaksi : <input type='text' name='txt_kode' id='txt_kode' class='input_header' size='15' readonly />
		<td>Tanggal : <input type='date' name='txt_tanggal' id='txt_tanggal' class='input_header'></td>
		<td align='right'>Nilai : <input type='text' name='txt_jumlahasl' id='txt_jumlahasl' class='input_header' size='10' readonly/>
	</tr>
	</table>
	
	<table width='100%'>
	<tr>
		<input type='text' name='txt_harga' id='txt_harga' class='input_header' hidden/ >
		<td width='14%'>Nama Barang</td>
		<td>: <input type='text' name='txt_namast' id='txt_namast' class='input_header' size='20'>
			<input type='text' name='txt_kodest' id='txt_kodest' class='input_header' size='6' readonly />
		</td>
		<td>Jumlah</td>
		<td>: <input type='text' name='txt_jmlunit' id='txt_jmlunit' class='input_header' size='9' >
			<input type='text' name='txt_satuan' id='txt_satuan' class='input_header' size='5' readonly/>
		</td>
		<td>Selisih</td>
		<td>: <input type='text' name='txt_selisih' id='txt_selisih' class='input_header' size='10'>
	</tr>
	</table>
	<hr>";

echo "<div id='form_cari_br' title='Pencarian Data Barang'>
		<table width='100%'>
			<tr>
				<td width='22%'>Cari Data Barang</td>
				<td width='1%'>:</td>
				<td><input type='text' name='txt_caribr' id='txt_caribr' size='40' class='input_header' ></td>
			</tr>
		</table>
		<div id='info_br' align='center'></div>
	</div>";
	
echo "<div id='form_cari_br1' title='Pencarian Data Barang'>
		<table width='100%'>
			<tr>
				<td width='22%'>Cari Data Barang</td>
				<td width='1%'>:</td>
				<td><input type='text' name='txt_caribr1' id='txt_caribr1' size='40' class='input_header' ></td>
			</tr>
		</table>
		<div id='info_br1' align='center'></div>
	</div>";

echo "<table align='center'>
		<tr>			
			<th>Nama Barang</th>
			<th>Kode</th>
			<th>Jumlah Hasil</th>
			<th></th>
			<th>Satuan</th>
			<th>Harga Pokok</th>
			<th>Nilai Hasil</th>
			<th>Selisih</th>
		</tr>
		
		<tr>			
			<input type='text' name='txt_nourut' id='txt_nourut' class='input_header' hidden />
			<input type='text' name='t_jmlunit' id='t_jmlunit' class='input_header' hidden />
			<input type='text' name='txt_jmlstok' id='txt_jmlstok' class='input_header' hidden />			
			<td align='center'><input type='text' name='txt_namast1' id='txt_namast1' class='input_detail' size='20' ></td>
			<td align='center'><input type='text' name='txt_kodest1' id='txt_kodest1' class='input_detail' size='6' readonly />
			<td align='center'><input type='text' name='txt_jmlunit1' id='txt_jmlunit1' class='input_detail' size='9' ></td>
			<td><button name='simpan' id='simpan'>Rekam</button></td>
			<td align='center'><input type='text' name='txt_satuan1' id='txt_satuan1' class='input_detail' size='7' readonly /></td>
			<td align='center'><input type='text' name='txt_harga1' id='txt_harga1' class='input_detail' size='9' readonly /></td>
			<td align='center'><input type='text' name='txt_jumlah' id='txt_jumlah' class='input_detail' size='10' readonly /></td>
			<td align='center'><input type='text' name='txt_selisih' id='txt_selisih' class='input_detail' size='10' readonly /></td>
		</tr>
	</table>
		
	<table width='100%'>
	<tr>
		<td align='left' style='color:red'><b>Tekan Q pada Nama Brg untuk Rekam Perubahan !</b></td>
		<td align='right'>
			<button name='rekam_psb' id='rekam_psb'>Rekam Perubahan</button>
			<button name='mulai' id='mulai'>Input</button>
			<button name='batal_psb' id='batal_psb'>Batal</button>
			<button name='tutup' id='tutup'>Tutup</button>
		</td>
	</tr>	
	</table>";
	
echo "<div id='data_detail'></div>";
?>