<script type="text/javascript">
	$(document).ready(function(){
		
	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_tglkwit").val(data.tanggal);
				$("#txt_tglawkw").val(data.tanggal);
				$("#txt_tglakkw").val(data.tanggal);				
			}
		});
	}
	
	$("#txt_tglkwit").focus();
	
	$('#form_cariplg').dialog({
		autoOpen: false,
		modal	: true,
		width: 600,
	});
		
	$('#txt_namaplkw').keyup(function(){
		cari_nomor();
		$('#form_cariplg').dialog('open');
		$("#txt_cariplkw").focus();
		return false;
	});
	
	$('#txt_namaplkw').click(function(){
		cari_nomor();
		$('#form_cariplg').dialog('open');
		$("#txt_cariplkw").focus();
		return false;
	});
	
	function cari_nomor(){
		var tanggal = $("#txt_tglkwit").val();
		
		$.ajax({
			type	: "POST",
			url		: "modul/pener_piut/cari_nokwit.php",
			data	: "tanggal="+tanggal,
			dataType: "json",
			success	: function(data){
				$("#txt_kodekw").val(data.nomor);
				$('#txt_tglkwit').focus();
				$("#txt_cariplkw").focus();
			}
		});
	}
	
	$("#txt_cariplkw").keyup(function(){
		var cari= $("#txt_cariplkw").val();
		
		var no=1;
		$.ajax({
			type	: "POST",
			url		: "modul/pener_piut/tampil_dataplkw.php",
			data	: "cari="+cari,
			timeout	: 3000,
			beforeSend	: function(){		
				$("#info_plkw").html("<img src='loading.gif' />");			
			},				  
			success	: function(data){
				$("#info_plkw").html(data);
			}
		});
	});
	
	$("#cari_pikw").click(function(){
		var kode  = $("#txt_kodekw").val();
		var	tanggal	= $("#txt_tglkwit").val();
		var kodepl= $("#txt_kodeplkw").val();
		var	namapl 	= $("#txt_namaplkw").val();		
		var tglaw = $("#txt_tglawkw").val();
		var tglak = $("#txt_tglakkw").val();
				
		$.ajax({
			type	: "POST",
			url		: "modul/pener_piut/rekam_data_tmp.php",
			data	: "kode="+kode+
				"&tanggal="+tanggal+
				"&kodepl="+kodepl+
				"&namapl="+namapl+
				"&tglaw="+tglaw+
				"&tglak="+tglak,
			dataType: "json",
			timeout	: 3000,
			beforeSend	: function(){		
				$("#info").html("<img src='loading.gif' />");			
			},				  
			success	: function(data){
				$("#info").html(data);
				$("#txt_tagihan").val(data.tbayar)
				tampil_detail();
				terbilang();
			}
		});
	});
	
	function tampil_detail(){
		$("#data_detailkw").show();
		$("#data_detailkw").load("modul/pener_piut/tampil_data_kwt.php?");
	}
	
	function terbilang(){
		var total = $("#txt_tagihan").val();
		
		if(total.length==1)
			konversi1();
		else if(total.length==2)
			konversi2();
		else if(total.length==3)
			konversi3();
		else if(total.length==4)
			konversi4();
		else if(total.length==5)
			konversi5();
		else if(total.length==6)
			konversi6();
		else if(total.length==7)
			konversi7();
		else if(total.length==8)
			konversi8();
		else if(total.length==9)
			konversi9();
	}
	
	function konversi1(){
		var	total= $("#txt_tagihan").val();
		
		$.ajax({
			type	: "POST",	
			url		: "modul/konversi/konversi1.php",
			data	: "total="+total,
			dataType: "json",
			success	: function(data){				
				$("#txt_terbilang").val(data.terbilang);				
			}
		});
	}
	
	function konversi2(){
		var	total = $("#txt_tagihan").val();
		
		$.ajax({
			type	: "POST",
			url	: "modul/konversi/konversi2.php",
			data	: "total="+total,
			dataType	: "json",
			success	: function(data){				
				$("#txt_terbilang").val(data.terbilang);
			}
		});
	}
	
	function konversi3(){
		var	total = $("#txt_tagihan").val();
		
		$.ajax({
			type	: "POST",
			url	: "modul/konversi/konversi3.php",
			data	: "total="+total,
			dataType	: "json",
			success	: function(data){
				$("#txt_terbilang").val(data.terbilang);				
			}
		});		
	}
	
	function konversi4(){
		var	total = $("#txt_tagihan").val();
		
		$.ajax({
			type	: "POST",
			url		: "modul/konversi/konversi4.php",
			data	: "total="+total,
			dataType: "json",
			success	: function(data){				
				$("#txt_terbilang").val(data.terbilang);				
			}
		});
	}
	
	function konversi5(){
		var	total = $("#txt_tagihan").val();
		
		$.ajax({
			type	: "POST",
			url		: "modul/konversi/konversi5.php",
			data	: "total="+total,
			dataType: "json",
			success	: function(data){				
				$("#txt_terbilang").val(data.terbilang);				
			}
		});
	}
	
	function konversi6(){
		var	total = $("#txt_tagihan").val();
		
		$.ajax({
			type	: "POST",
			url		: "modul/konversi/konversi6.php",
			data	: "total="+total,
			dataType: "json",
			success	: function(data){
				$("#txt_terbilang").val(data.terbilang);
			}
		});		
	}
	
	function konversi7(){
		var	total = $("#txt_tagihan").val();
		
		$.ajax({
			type	: "POST",
			url		: "modul/konversi/konversi7.php",
			data	: "total="+total,
			dataType: "json",
			success	: function(data){				
				$("#txt_terbilang").val(data.terbilang);				
			}
		});
	}
	
	function konversi8(){
		var	total = $("#txt_tagihan").val();
				
		$.ajax({
			type	: "POST",
			url		: "modul/konversi/konversi8.php",
			data	: "total="+total,
			dataType: "json",
			success	: function(data){				
				$("#txt_terbilang").val(data.terbilang);				
			}
		});
	}
	
	function konversi9(){
		var	total = $("#txt_tagihan").val();
		
		$.ajax({
			type	: "POST",
			url		: "modul/konversi/konversi9.php",
			data	: "total="+total,
			dataType: "json",
			success	: function(data){				
				$("#txt_terbilang").val(data.terbilang);				
			}
		});		
	}
	
	$('#form_caribatal').dialog({
		autoOpen: false,
		modal	: true,
		width: 600,
	});
	
	$("#txt_nofakkw").keyup(function(){
		$('#form_caribatal').dialog('open');
		var cari='';
		
		var no=1;
		$.ajax({
			type	: "POST",
			url		: "modul/pener_piut/tampil_batal_tmp.php",
			data	: "cari="+cari,
			timeout	: 3000,
			beforeSend	: function(){		
				$("#info_batalkw").html("<img src='loading.gif' />");
			},				 
			success	: function(data){
				$("#info_batalkw").html(data);
			}
		});
	});
	
	$("#txt_nofakkw").click(function(){
		$('#form_caribatal').dialog('open');
		var cari='';
				
		var no=1;
		$.ajax({
			type	: "POST",
			url		: "modul/pener_piut/tampil_batal_tmp.php",
			data	: "cari="+cari,
			timeout	: 3000,
			beforeSend	: function(){		
				$("#info_batalkw").html("<img src='loading.gif' />");
			},				  
			success	: function(data){
				$("#info_batalkw").html(data);
			}
		});
	});

	$("#txt_dibayarkw").keyup(function(){
		var jmlpiut = $("#txt_jmlpiutkw").val();
		var dibayar = $("#txt_dibayarkw").val();
		var jpiut	= parseInt(jmlpiut);
		var jbayar	= parseInt(dibayar);
		var jsisa	= jpiut-jbayar;
		
		if(jbayar>jpiut){
			alert('Maaf, jumlah dibayar tidak boleh melebihi nominal.');
			return(false);
		}
		$("#txt_sisakw").val(jsisa);
	});
	
	$("#simpan_kw").click(function(){
		simpan_tmp();
	});
			
	function simpan_tmp(){
		var	kode	= $("#txt_kodekw").val();
		var	tanggal	= $("#txt_tglkwit").val();
		var	kodepl  = $("#txt_kodeplkw").val();		
		var	namapl 	= $("#txt_namaplkw").val();
		var	tglaw 	= $("#txt_tglawkw").val();
		var	tglak  	= $("#txt_tglakkw").val();
		var	nofak  	= $("#txt_nofakkw").val();
		var	tglfak 	= $("#txt_tglfakkw").val();
		var	nopol  	= $("#txt_nopolkw").val();
		var	jmlliter= $("#txt_jmlliterkw").val();
		var	jmlpiut	= $("#txt_jmlpiutkw").val();
		var	dibayar	= $("#txt_dibayarkw").val();
		var	sisa  	= $("#txt_sisakw").val();
		
		var error = false;

		if(tanggal.length == 0){
           var error = true;
           alert("Maaf, Tanggal penerimaan tidak boleh kosong.");
		   return (false);
        }
		
		if(kodepl.length == 0){
			var error = true;
			alert("Maaf, Kolom pelanggan harus diisi.");
			$("#txt_namaplkw").focus();
			return (false);
        }
		
		if(nofak.length == 0){
           alert("Maaf, Kolom nomor faktur masih kosong. Kolom ini untuk memanggil data yang sudah dihapus.");
		   $("#txt_nofakkw").focus();
		   return (false);
        }
		
		if(dibayar.length == 0){
			alert("Maaf, Kolom dibayar harus diiisi.");
			$("#txt_dibayarkw").focus();
			return (false);
        }
		 
		if(error == false){
		$.ajax({
			type	: "POST",
			url		: "modul/pener_piut/simpan.php",
			data	: "kode="+kode+
					"&tanggal="+tanggal+
					"&kodepl="+kodepl+
					"&namapl="+namapl+
					"&tglaw="+tglaw+
					"&tglak="+tglak+
					"&nofak="+nofak+
					"&tglfak="+tglfak+
					"&nopol="+nopol+
					"&jmlliter="+jmlliter+
					"&jmlpiut="+jmlpiut+
					"&dibayar="+dibayar+
					"&sisa="+sisa,
			dataType: "json",
			timeout	: 2000,
			beforeSend	: function(){		
				$("#info").html("<img src='loading.gif' />");			
			},				  
			success	: function(data){
				$("#info").html(data);
				$(".input_detail").val('');
				$("#txt_tagihan").val(data.tbayar);
				$("#txt_nofakkw").focus();
				tampil_detail();
				terbilang();
			}
		});
		}
		return false;
	}
	
	$("#cetak_kw").click(function(){
		cetak_kwit();
		tambah_nomor();
	});
	
	function cetak_kwit(){
		var	kode	 = $("#txt_kodekw").val();
		var	tanggal	 = $("#txt_tglkwit").val();
		var	kodepl   = $("#txt_kodeplkw").val();		
		var	namapl 	 = $("#txt_namaplkw").val();
		var	tglaw 	 = $("#txt_tglawkw").val();
		var	tglak  	 = $("#txt_tglakkw").val();
		var	tagihan	 = $("#txt_tagihan").val();
		var	terbilang= $("#txt_terbilang").val();
		
		var error = false;

		if(tanggal.length == 0){
           var error = true;
           alert("Maaf, Tanggal penerimaan tidak boleh kosong.");
		   return (false);
        }
		
		if(kodepl.length == 0){
			var error = true;
			alert("Maaf, Kolom pelanggan harus diisi.");
			$("#txt_namaplkw").focus();
			return (false);
        }
		
		if(tagihan.length == 0){
           alert("Maaf, Total tagihan masih kosong. Tentukan tanggal jual, lalu klik Cari data.");
		   $("#txt_tglawkw").focus();
		   return (false);
        }
		window.location.href='modul/laporan/kwitansi.php?kode='+kode+'&tanggal='+tanggal+'&kodepl='+kodepl+'&namapl='+namapl+'&tglaw='+tglaw+'&tglak='+tglak+'&tagihan='+tagihan+'&terbilang='+terbilang;		
	}
	
	function tambah_nomor(){
		var	kode = $("#txt_kodekw").val();
		$.ajax({
			type	: "POST",
			url		: "modul/pener_piut/tambah_nokwit.php",
			data	: "kode="+kode,
			dataType: "json",			
			timeout	: 2000,
			beforeSend	: function(){		
				$("#info").html("<img src='loading.gif' />");			
			},				  
			success	: function(data){
				$("#info").html(data);				
			}
		});
	}
	
	$("#refresh").click(function(){
		$(".input_header").val('');
		$("#data_detailkw").hide();
		$("#txt_tglkwit").focus();
	});
	
	$("#tutup_kw").click(function(){		
		document.location='?module=home';
	});
});
</script>

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

<?php
echo "<table width='100%'>	
	<tr>
		<td width='9%'>No.Kwitansi</td>
		<td>: <input type='text' name='txt_kodekw' id='txt_kodekw' size='10' class='input_header' readonly /></td>
		<td>Tanggal : <input type='date' name='txt_tglkwit' id='txt_tglkwit'></td>
		<td>Pelanggan : <input type='text' name='txt_namaplkw' id='txt_namaplkw' size='20' class='input_header'>
			<input type='text' name='txt_kodeplkw' id='txt_kodeplkw' size='4' class='input_header' readonly />
		</td>
		<td>Tanggal Jual : <input type='date' name='txt_tglawkw' id='txt_tglawkw'> s/d <input type='date' name='txt_tglakkw' id='txt_tglakkw'></td>
	</tr>
	</table>
	<table width='100%'>
	<tr>
		<td width='9%'>Total Tagihan</td>
		<td>: <input type='text' name='txt_tagihan' id='txt_tagihan' size='10' class='input_header' readonly /></td>
		<td>Terbilang : <input type='text' name='txt_terbilang' id='txt_terbilang' size='70'></td>
		<td>
		    <button name='cari_pikw' id='cari_pikw'>Cari Data</button>
			<button name='cetak_kw' id='cetak_kw'>Cetak</button>
			<button name='refresh' id='refresh'>Refresh</button>
			<button name='tutup_kw' id='tutup_kw'>Tutup</button>
		</td>
	</tr>
	</table>
	<hr>";

echo "<table>
		<tr>
			<th>No. Faktur</th> 
			<th>Tanggal</th>
			<th>No. Polisi</th>
			<th>Jml.Liter</th>
			<th>Nominal</th>
			<th>Dibayar</th>
			<th>Sisa</th>
		</tr>		
		<tr>
			<td align='center'><input type='text' name='txt_nofakkw' id='txt_nofakkw' class='input_detail' size='12'></td>
			<td align='center'><input type='date' name='txt_tglfakkw' id='txt_tglfakkw' class='input_detail' readonly /></td>
			<td align='center'><input type='text' name='txt_nopolkw' id='txt_nopolkw' class='input_detail' size='12' ></td>
			<td align='center'><input type='text' name='txt_jmlliterkw' id='txt_jmlliterkw' class='input_detail' size='9' readonly /></td>
			<td align='center'><input type='text' name='txt_jmlpiutkw' id='txt_jmlpiutkw' class='input_detail' size='12' readonly /></td>
			<td align='center'><input type='text' name='txt_dibayarkw' id='txt_dibayarkw' class='input_bayar' size='12'></td>
			<td align='center'><input type='text' name='txt_sisakw' id='txt_sisakw' class='input_detail' size='12' ></td>
			<td>
				<button name='simpan_kw' id='simpan_kw'>Rekam</button>
			</td>
		</tr>
	</table>";

echo "<div id='data_detailkw'></div>";

echo "<div id='form_cariplg' title='Pencarian Data Pelanggan'>
		<table width='100%'>
			<tr>
				<td width='20%'>Cari Pelanggan</td>
				<td width='1%'>:</td>
				<td><input type='text' name='txt_cariplkw' id='txt_cariplkw' size='40'</td>
			</tr>
		</table>
		<div id='info_plkw' align='center'></div>
	</div>";
	
echo "<div id='form_caribatal' title='Pencarian Data Piutang'>		
		<div id='info_batalkw' align='center'></div>
	</div>";
?>
