<script type="text/javascript">
	$(document).ready(function(){
	$("#data_detail").hide();
	
	$("#mulai").focus();
	
	$("#mulai").click(function(){
		var	kode= $("#txt_kode").val();
		var	total = $("#txt_total").val();
		
		if(!(total.length==0)){
			alert("Maaf, input retur pembelian "+kode+" belum direkam.");
			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_tanggal").focus();
				cari_nomor();
			}
		});
	}
	
	function cari_nomor(){
		var tanggal = $("#txt_tanggal").val();
		
		$.ajax({
			type	: "POST",
			url		: "modul/returbeli/cari_nomor.php",
			data	: "tanggal="+tanggal,
			dataType: "json",
			success	: function(data){
				$("#txt_kode").val(data.nomor);				
				$('#input_rb').show();
				tampil_data_detail();
			}
		});
	}
	
	function tampil_data_detail(){
		var kode = $("#txt_kode").val();
		$("#data_detail").show();
		$("#data_detail").load("modul/returbeli/tampil_data_detail.php?kode="+kode);
		hitung_total();
	}
	
	function hitung_total(){
		var	kode= $("#txt_kode").val();
		
		$.ajax({
			type	: "POST",
			url		: "modul/returbeli/cari_total.php",
			data	: "kode="+kode,
			dataType: "json",
			success	: function(data){
				$("#txt_total").val(data.total);
			}
		});
	}
	
	$("#txt_tanggal").keyup(function(){
		cari_nomor();
		return(false);
	});
	
	$('#form_cari_sup').dialog({
		autoOpen: false,
		modal	: true,
		width: 600,
	});
	
	$('#txt_namasup').keyup(function(){
		$('#form_cari_sup').dialog('open');
		$("#info_supplier").load('modul/pembelian/tampil_data_sup.php');
		$("#txt_carisup").focus();
		return false;
	});
	
	$("#txt_carisup").keyup(function(){
		$("#info_supplier").show();
		var cari = $("#txt_carisup").val();
		
		$.ajax({
			type	: "POST",
			url		: "modul/pembelian/tampil_data_sup.php",
			data	: "cari="+cari,
			timeout	: 2000,
			beforeSend	: function(){		
				$("#info_supplier").html("<img src='loading.gif' />");			
			},				  
			success	: function(data){
				$("#info_supplier").html(data);
			}
		});
	});
	
	$("#txt_nomorfk").keyup(function() {
		var  nomorfk 	= $("#txt_nomorfk").val()
		
		$.ajax({
			type	: "POST",
			url	: "modul/returbeli/cari_nofk.php",
			data	: "nomorfk="+nomorfk,
			dataType	: "json",
			success	: function(data){
				$("#t_nomorfk").val(data.nofkt);
				var nofk = $("#t_nomorfk").val();
				
				if(!(nofk.length==0)){
					alert('Nomor faktur '+nomorfk+' sudah ada.')
					return (false);
				}	
			}
		});
	});
	
	$('#form_cari_faktur').dialog({
		autoOpen: false,
		modal	: true,
		width: 600,
	});
		
	$('#cari_faktur').click(function(){		
		$('#form_cari_faktur').dialog('open');		
		var kodesup	= $("#txt_kodesup").val();
		
		$.ajax({
			type	: "POST",
			url	: "modul/returbeli/tampil_data_faktur.php",
			data	: "kodesup="+kodesup,
			timeout	: 2000,
			beforeSend	: function(){		
				$("#info_faktur").html("<img src='loading.gif' />");			
			},				  
			success	: function(data){
				$("#info_faktur").html(data);
			}
		});
	});
		
	$('#form_cari_barang').dialog({
		autoOpen: false,
		modal	: true,
		width: 700,
	});
	
	$('#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();
		
		$.ajax({
			type	: "POST",
			url		: "modul/returbeli/tampil_data_barang.php",
			data	: "cari="+cari,
			timeout	: 1000,
			beforeSend: function(){		
				$("#info_barang").html("<img src='loading.gif' />");			
			},				  
			success	: function(data){
				$("#info_barang").html(data);				
			}
		});
	});
	
	$("#txt_jmlretur").keyup(function(){
		var hgbeli	= $("#txt_hgbeli").val();
		var jmlretur= $("#txt_jmlretur").val();
		var jretur	= parseFloat(jmlretur);
		
		var tjumlah	= jretur*parseFloat(hgbeli);
		$("#txt_jumlah").val(tjumlah);		
	});
	
	$("#simpan").click(function(){
		simpan_data();
	});
		
	function simpan_data(){
		var kode			= $("#txt_kode").val();
		var tanggal 	= $("#txt_tanggal").val();
		var kodesup 	= $("#txt_kodesup").val();
		var namasup 	= $("#txt_namasup").val();
		var nomorfk 	= $("#txt_nomorfk").val();
		var tanggalfk	= $("#txt_tanggalfk").val();
		var kodest		= $("#txt_kodest").val();
		var namast		= $("#txt_namast").val();
		var satuan		= $("#txt_satuan").val();
		var jmlretur		= $("#txt_jmlretur").val();
		var hgbeli  		= $("#txt_hgbeli").val();
		var jumlah		= $("#txt_jumlah").val();
		var ket			= $("#txt_ket").val();
		var caraby 		= $(".radio_cb:checked").val();
		var cek_cb		= $(".radio_cb:checked");
		
		var error = false;

		if(tanggal.length == 0){
           var error = true;
           alert("Maaf, Tanggal faktur tidak boleh kosong.");
		   return (false);
        }
		
		if(kodesup.length == 0){
           var error = true;
           alert("Maaf, Kode supplier tidak boleh kosong.");		   
		   return (false);
        }
		
		if(cek_cb.length == 0){
			var error = true;
			alert("Maaf, Anda belum menentukan cara pengembalian.");
			$("#radio_cb").focus();
			return (false);
        }
				
		if(nomorfk.length == 0){
			if(caraby=='Kredit'){
				var error = true;
				alert("Maaf, untuk retur kredit Nomor faktur harus diisi.");
				$("#txt_nomorfk").focus();
				return (false);
			}
        }
		
		if(kodest.length == 0){
           var error = true;
           alert("Maaf, Kode Barang tidak boleh kosong");
		   $("#txt_kodest").focus();
		   return (false);
        }
		
		if(jmlretur.length == 0){
           var error = true;
           alert("Maaf, Jumlah retur barang tidak boleh kosong");
		   $("#txt_jmlunit").focus();
		   return (false);
        }
		 
		if(error == false){
		$.ajax({
			type		: "POST",
			url		: "modul/returbeli/simpan.php",
			data	: "kode="+kode+
					"&tanggal="+tanggal+
					"&kodesup="+kodesup+
					"&namasup="+namasup+
					"&nomorfk="+nomorfk+
					"&tanggalfk="+tanggalfk+
					"&caraby="+caraby+
					"&kodest="+kodest+
					"&namast="+namast+
					"&satuan="+satuan+
					"&jmlretur="+jmlretur+
					"&hgbeli="+hgbeli+
					"&jumlah="+jumlah+
					"&ket="+ket,
			timeout	: 1000,
			beforeSend	: function(){		
				$("#info").html("<img src='loading.gif' />");			
			},				  
			success	: function(data){
				$("#info").html(data);
				$(".input_detail").val('');
				$("#txt_namast").focus();
				tampil_data_detail();
			}
		});
		}
	}
	
	$("#rekam_rb").click(function(){	
		rekam_rpb();
	});
	
	function rekam_rpb(){
		var kode 		= $("#txt_kode").val();
		var tanggal 	= $("#txt_tanggal").val();		
		var kodesup	= $("#txt_kodesup").val();
		var namasup	= $("#txt_namasup").val();
		var nomorfk  	= $("#txt_nomorfk").val();		
		var total	 		= $("#txt_total").val();
		var caraby 		= $(".radio_cb:checked").val();
		
		var error = false;

		if(kode.length == 0){
           var error = true;
           alert("Maaf, Kode retur masih kosong. Klik tombol Buat Retur untuk mulai.");
		   return (false);
        }
		
		if(total.length == 0){
			var error = true;
			alert("Maaf, Total nilai retur pembelian masih kosong.");
			$("#txt_kodest").focus();
			return (false);
        }
				
		if(error == false){
		$.ajax({
			type		: "POST",
			url		: "modul/returbeli/simpan_rb.php",
			data	: "kode="+kode+
				"&tanggal="+tanggal+
				"&kodesup="+kodesup+
				"&namasup="+namasup+
				"&namasup="+namasup+
				"&total="+total+
				"&caraby="+caraby,
					
			timeout	: 3000,
			beforeSend	: function(){		
				$("#info").html("<img src='loading.gif' />");			
			},				  
			success	: function(data){
				$("#info").html(data);
				$(".input_detail").val('');
				$(".input_header").val('');
				$('#input_rb').hide();
				$('#data_detail').hide();
			}
		});
		}
		return false;
	}
	
	$("#batal_rb").click(function(){
		batal_rpb();
	});
	
	function batal_rpb(){
		var	kode	= $("#txt_kode").val();
		var	total	 	= $("#txt_total").val();
		
		if(kode.length == 0){
           var error = true;
           alert("Maaf, Kode retur pembelian  masih kosong.");
		   return (false);
        }
		
		if(total.length == 0){
			var error = true;
			alert("Maaf, Total nilai retur pembelian masih kosong");
			$("#txt_kodest").focus();
			return (false);
        }
		
		var pilih = confirm('Apakah Anda sungguh-sungguh ingin membatalkan input '+kode+' ?');
		if (pilih==true) {
			var	kode	= $("#txt_kode").val();			
			$.ajax({
				type	: "POST",
				url	: "modul/returbeli/batal_input.php",
				data	: "kode="+kode,
				success	: function(data){
					window.location.reload();
				}
			});
		}
	}
		
	$("#tutup").click(function(){
		var	total	 = $("#txt_total").val();
		
		if(!(total.length == 0)){
			var error = true;
			alert("Maaf, data retur pembelian belum direkam.");			
			return (false);
        }
		document.location='?module=home';
	});
});
</script>

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

<?php
echo "<table>	
	<tr>
		<td>Kode Retur : <input type='text' name='txt_kode' id='txt_kode' size='11' class='input_header' readonly></td>
		<td>Tanggal : <input type='date' name='txt_tanggal' id='txt_tanggal' size='10' class='input_header'></td>
		<td>Supplier : <input type='text' name='txt_namasup' id='txt_namasup' size='21' class='input_header'>
			<input type='text' name='txt_kodesup' id='txt_kodesup' size='3' class='input_header' readonly />
		</td>
		<td>No.Faktur : <input type='text' name='txt_nomorfk' id='txt_nomorfk' size='10' class='input_header' readonly/>
			<img src='images/view.png' width='18' height='18' title='Cari Faktur' id='cari_faktur'>
		</td>
		<td>Tgl.Faktur : <input type='date' name='txt_tanggalfk' id='txt_tanggalfk' size='10' class='input_header' readonly/></td>
	</tr>
	</table>
	<table>
	<tr>
		<td>Cara Pengembaliaan :<td>
		<td><input type='radio' name='txt_caraby' id='txt_caraby' value='Tunai' class='radio_cb'>Tunai</td>
		<td><input type='radio' name='txt_caraby' id='txt_caraby' value='Kredit' class='radio_cb'>Kredit</td>
	</tr>
	</table>
	<hr>";

echo "<div id='form_cari_sup' title='Pencarian Data Supplier'>
		<table width='100%'>
			<tr>
				<td width='18%'>Cari Supplier</td>
				<td width='2%'>:</td>
				<td><input type='text' name='txt_carisup' id='txt_carisup' size='40'</td>
			</tr>
		</table>
		<div id='info_supplier' align='center' style='overflow-x: auto;height: 400px;scroll-behavior: smooth'></div>
	</div>";

echo "<table align='center'>
		<tr>
			<th>Nama Barang</th>
			<th>Kode</th>
			<th>Jml.Retur</th>
			<th>Satuan</th>
			<th>Harga Beli</th>
			<th>Nilai Retur</th>
			<th>Keterangan</th>
			<th></th>
		<tr>
		
		<tr>
			<input type='text' name='txt_total' id='txt_total' class='input_detail' size='10' hidden/>
			<td align='center'><input type='text' name='txt_namast' id='txt_namast' class='input_detail' size='20'></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_jmlretur' id='txt_jmlretur' class='input_detail' size='6'></td>
			<td align='center'><input type='text' name='txt_satuan' id='txt_satuan' class='input_detail' size='6' readonly/></td>
			<td align='center'><input type='text' name='txt_hgbeli' id='txt_hgbeli' class='input_detail' size='10' 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_ket' id='txt_ket' class='input_detail' size='20' maxlength='30'></td>
			<td align='center'><button name='simpan' id='simpan'>Rekam</button></td>
		</tr>
	</table>";

echo "<div id='input_rb'>
	<table width='100%'>
	<tr>
		
	</tr>
	</table>
	</div>";

echo "<div id='data_detail' style='overflow-x: auto;height: 390px;scroll-behavior: smooth'></div>";

echo "<div id='form_cari_faktur' title='Pencarian Faktur Pembelian'>		
		<div id='info_faktur' align='center' style='overflow-x: auto;height: 400px;scroll-behavior: smooth'></div>
	</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' style='overflow-x: auto;height: 400px;scroll-behavior: smooth'></div>
	</div>";
	
echo "<table width='100%'>
	<tr>
		<td colspan='3' align='center'>
		<button name='mulai' id='mulai'>Buat Retur</button>
		<button name='rekam_rb' id='rekam_rb'>Rekam Retur</button>
		<button name='batal_rb' id='batal_rb'>Batal</button>
		<button name='tutup' id='tutup'>Tutup</button>
		</td>
	</tr>
	</table>";
?>
