<script type="text/javascript">
	$(document).ready(function(){
	$("#form_detail").hide();
	
	cari_default();
		
	function cari_default(){
		var no = 1;
		$.ajax({
			type	: "POST",
			url		: "modul/gaji/cari_def.php",
			data	: "no="+no,
			dataType: "json",
			success	: function(data){				
				$("#kwprs").val(data.kwprs);
				$("#tkprs").val(data.tkprs);
				$("#kwkar").val(data.kwkar);
				$("#tkkar").val(data.tkkar);
				$("#ptkpwp").val(data.ptkpwp);
				$("#ptkpis").val(data.ptkpis);
				$("#ptkpan").val(data.ptkpan);
				$("#maxanak").val(data.maxanak);
				$("#prbijab").val(data.prbijab);
				$("#maxbijab").val(data.maxbijab);
				$("#prpph1").val(data.prpph1);
				$("#prpph2").val(data.prpph2);
				$("#prpph3").val(data.prpph3);
				$("#prpph4").val(data.prpph4);
				$("#netgaji1").val(data.netgaji1);
				$("#netgaji2").val(data.netgaji2);
				$("#netgaji3").val(data.netgaji3);
			}
		});
	}
	
	$("#mulai").click(function(){
		$("#form_detail").show();
		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();				
			}
		});
	}
			
	$("#txt_bulan").keyup(function(){
		tampil_data_detail();		
	});
	
	$("#txt_bulan").click(function(){
		tampil_data_detail();		
	});
		
	$("#txt_tahun").keyup(function(){
		var vtahun = $("#txt_tahun").val();
		
		if(vtahun>2100){
			alert('Penulisan tahun tidak terdefinisi.');			
		}
		if(vtahun<2000){
			if(vtahun>999){
				alert('Penulisan tahun tidak terdefinisi.');
			}
		}
		tampil_data_detail();		
	});
	
	function tampil_data_detail(){
		var bulan = $("#txt_bulan").val();
		var tahun = $("#txt_tahun").val();
		
		$.ajax({
			type	: "POST",
			url		: "modul/gaji/cari_hef.php",
			data	: "bulan="+bulan+
					"&tahun="+tahun,
			dataType: "json",
			success	: function(data){
				$("#txt_harief").val(data.harief);				
			}
		});
		$("#data_detail").show();
		$("#data_detail").load('modul/gaji/tampil_data_detail.php?bulan='+bulan+'&tahun='+tahun);
		cari_nomor();
	}
	
	function cari_nomor(){
		var bulan = $("#txt_bulan").val();
		var tahun = $("#txt_tahun").val();
		var nik   = $("#txt_nik").val();
		
		$.ajax({
			type	: "POST",
			url		: "modul/gaji/cari_nomor.php",
			data	: "bulan="+bulan+
					"&tahun="+tahun+
					"&nik="+nik,
			dataType: "json",
			success	: function(data){
				$("#txt_kode").val(data.nomor);				
			}
		});
	}
	
	$('#form_cari_kar').dialog({
		autoOpen: false,
		modal	: true,
		width: 750,
	});
	
	$('#txt_nama').click(function(){
		var harief = $("#txt_harief").val();
		
		if(harief.length=0){
			alert('Maaf, hari efektif harus diisi lebih dahulu.');
			$("#txt_harief").focus();
			return false;
		}
		$('#form_cari_kar').dialog('open');
		$("#info_kar").load('modul/gaji/tampil_data_kar.php');		
		$("#txt_carikar").focus();
		return false;
	});
	
	$('#txt_nama').keyup(function(){
		var harief = $("#txt_harief").val();
		
		if(harief.length=0){
			alert('Maaf, hari efektif harus diisi lebih dahulu.');
			$("#txt_harief").focus();
			return false;
		}
		$('#form_cari_kar').dialog('open');
		$("#info_kar").load('modul/gaji/tampil_data_kar.php');
		$("#txt_carikar").focus();
		return false;
	});
	
	$('#txt_carikar').keyup(function(){
		var harief = $("#txt_harief").val();
		
		if(harief.length=0){
			alert('Maaf, hari efektif harus diisi lebih dahulu.');
			$("#txt_harief").focus();
			return false;
		}
		var cari = $("#txt_carikar").val();
		
		$.ajax({
			type	: "POST",
			url		: "modul/gaji/tampil_data_kar.php",
			data	: "cari="+cari,
			timeout	: 2000,
			beforeSend	: function(){		
				$("#info_kar").html("<img src='loading.gif' />");
			},				  
			success	: function(data){
				$("#info_kar").html(data);
			}
		});
	});

	$("#txt_sakit").keyup(function(){
		hitung_gaji();
	});
	
	$("#txt_ijin").keyup(function(){
		hitung_gaji();
	});
	
	$("#txt_alpa").keyup(function(){
		hitung_gaji();
	});
	
	$("#txt_tunbpjs").keyup(function(){
		hitung_gaji();
	});
	
	$("#txt_potbpjs").keyup(function(){
		hitung_gaji();
	});
	
	$("#txt_potpph").keyup(function(){
		hitung_gaji();
	});
	
	$("#txt_jumlahtl").keyup(function(){	
		hitung_gaji();
	});
	
	$("#txt_umgaji").keyup(function(){
		hitung_gaji();
	});
	
	$("#txt_angsuran").keyup(function(){
		hitung_gaji();		
	});
	
	$("#txt_jumlahpl").keyup(function(){
		hitung_gaji();
	});
		
	function hitung_gaji(){
		var prbijab	= parseFloat($("#prbijab").val());
		var maxbijab= parseInt($("#maxbijab").val());
		var prpph1 	= parseFloat($("#prpph1").val());
		var prpph2 	= parseFloat($("#prpph2").val());
		var prpph3 	= parseFloat($("#prpph3").val());
		var prpph4 	= parseFloat($("#prpph4").val());
		var netgaji1= parseInt($("#netgaji1").val());
		var netgaji2= parseInt($("#netgaji2").val());
		var netgaji3= parseInt($("#netgaji3").val());
		var harief = parseInt($("#txt_harief").val());
		var sakit  = $("#txt_sakit").val();
		var ijin   = $("#txt_ijin").val();
		var alpa   = $("#txt_alpa").val();
		var gapok  = parseInt($("#txt_gapok").val());
		var umak   = $("#umak").val();				
		var ptkp   = (parseInt($("#ptkp").val()))/12;
		var tht    = parseInt($("#tht").val());
		var tunjab = parseInt($("#txt_tunjab").val());
		var umgaji = $("#txt_umgaji").val();
		var angsur = $("#txt_angsuran").val();
		var tunbpjs= $("#txt_tunbpjs").val();
		var tunlain= $("#txt_jumlahtl").val();
		var potbpjs= $("#txt_potbpjs").val();
		var potlain= $("#txt_jumlahpl").val();
		
		if(!(sakit=='')) jsakit=parseFloat(sakit); else jsakit=0;		
		if(!(ijin=='')) jijin=parseFloat(ijin); else jijin=0;
		if(!(alpa=='')) jalpa=parseFloat(alpa); else jalpa=0;
		if(!(umak=='')) umak=parseInt(umak); else umak=0;
		if(!(tunbpjs=='')) tunbpjs=parseInt(tunbpjs); else tunbpjs=0;
		if(!(tunlain=='')) jtunlain=parseInt(tunlain); else jtunlain=0;
		if(!(potbpjs=='')) potbpjs=parseInt(potbpjs); else potbpjs=0;
		if(!(potlain=='')) jpotlain=parseInt(potlain); else jpotlain=0;
		if(!(umgaji=='')) jumgaji=parseInt(umgaji); else jumgaji=0;
		if(!(angsur=='')) jangsur=parseInt(angsur); else jangsur=0;
		
		var hadir = harief-jsakit-jijin-jalpa;
		var tunmak= umak*hadir;
		var totgaji= gapok+tunmak+tunjab+tunbpjs+jtunlain;
		var potabsen= (gapok/harief)*jalpa;
		var potabsen= potabsen.toFixed(0);
		var bijab = ((totgaji-potabsen)*prbijab)/100;
		var bijab = bijab.toFixed(0);
		
		if(bijab>maxbijab) bijab=maxbijab;
		
		var pkp = totgaji-potabsen-bijab-tht-ptkp;
		if(pkp<=netgaji1){
			var potpph = (pkp*prpph1)/100;
		}else if(pkp<=netgaji2){
			var pph1 = (netgaji1*prpph1)/100;
			var pph2 = ((pkp-pph1)*prpph2)/100;
			var potpph = pph1+pph2;
		}else if(pkp<=netgaji3){
			var pph1 = (netgaji1*prpph1)/100;
			var pph2 = ((netgaji2-netgaji1)*prpph2)/100;
			var pph3 = ((pkp-netgaji2)*prpph3)/100;
			var potpph = pph1+pph2+pph3;
		}else{
			var pph1 = (netgaji1*prpph1)/100;
			var pph2 = ((netgaji2-netgaji1)*prpph2)/100;
			var pph3 = ((netgaji3-netgaji2)*prpph3)/100;
			var pph4 = ((pkp-netgaji3)*prpph4)/100;
			var potpph = pph1+pph2+pph3+pph4;
		}
		var potpph = potpph.toFixed(0);
		
		$("#txt_tunmak").val(tunmak);
		$("#txt_totgaji").val(totgaji);
		$("#txt_potabsen").val(potabsen);
		//$("#txt_potpph").val(potpph);
		
		if(potabsen=='') potabsen=0;
		if(potbpjs=='') potbpjs=0;		
		if(jangsur=='') jangsur=0;
		if(jpotlain=='') jpoitlain=0;
		if(potpph=='') potpph=0;
		
		var gaber = totgaji-potabsen-potbpjs-jumgaji-jpotlain;
		var gaber = gaber.toFixed(0);

		$("#txt_gaber").val(gaber);
	}
	
	$("#simpan").click(function(){
		simpan_data();
	});
	
	function simpan_data(){
		var	kode	= $("#txt_kode").val();
		var	tanggal = $("#txt_tanggal").val();
		var	bulan 	= $("#txt_bulan").val();
		var	tahun 	= $("#txt_tahun").val();
		var	harief 	= $("#txt_harief").val();
		var	nik		= $("#txt_nik").val();
		var	nama   	= $("#txt_nama").val();
		var	jabatan	= $("#txt_jabatan").val();
		var sakit   = $("#txt_sakit").val();
		var ijin    = $("#txt_ijin").val();
		var alpa    = $("#txt_alpa").val();
		var gapok   = $("#txt_gapok").val();
		var tunmak  = $("#txt_tunmak").val();
		var tunbpjs = $("#txt_tunbpjs").val();
		var tunjab  = $("#txt_tunjab").val();
		var namatl  = $("#txt_namatl").val();
		var jumlahtl= $("#txt_jumlahtl").val();
		var totgaji = $("#txt_totgaji").val();
		var potabsen= $("#txt_potabsen").val();		
		var umgaji  = $("#txt_umgaji").val();
		var angsur  = $("#txt_angsuran").val();		
		var potbpjs = $("#txt_potbpjs").val();
		var potpph  = $("#txt_potpph").val();
		var namapl  = $("#txt_namapl").val();
		var jumlahpl= $("#txt_jumlahpl").val();
		var	gaber  	= $("#txt_gaber").val();
		
		var error = false;
 		 
		if(harief.length == 0){
           var error = true;
           alert("Maaf, prosentase hadir tidak boleh kosong");
		   $("#txt_namafas").focus();
		   return (false);
        }		
		if(nik.length == 0){
           var error = true;
           alert("Maaf, Kolom NIK masih kosong.");
		   $("#txt_nik").focus();
		   return (false);
        }
		
		if(error == false){
		$.ajax({
			type	: "POST",
			url		: "modul/gaji/simpan.php",
			data	: "&kode="+kode+
					"&tanggal="+tanggal+
					"&bulan="+bulan+
					"&tahun="+tahun+
					"&harief="+harief+
					"&nik="+nik+
					"&nama="+nama+
					"&jabatan="+jabatan+
					"&sakit="+sakit+
					"&ijin="+ijin+
					"&alpa="+alpa+
					"&gapok="+gapok+
					"&tunmak="+tunmak+
					"&tunbpjs="+tunbpjs+
					"&tunjab="+tunjab+
					"&namatl="+namatl+
					"&jumlahtl="+jumlahtl+
					"&totgaji="+totgaji+
					"&potabsen="+potabsen+
					"&umgaji="+umgaji+
					"&angsur="+angsur+
					"&potbpjs="+potbpjs+
					"&potpph="+potpph+
					"&namapl="+namapl+
					"&jumlahpl="+jumlahpl+
					"&gaber="+gaber,

			timeout	: 3000,
			beforeSend	: function(){		
				$("#info").html("<img src='loading.gif' />");			
			},				  
			success	: function(data){
				$("#info").html(data);
				$(".input_detail").val('');
				$("#txt_nama").focus();
				tampil_data_detail();
			}
		});
		}
		return false;
	}
	
	$("#tutup").click(function(){	
		document.location='?module=home';
	});
});
</script>

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

<?php
$tanggal=date('Y-m-d');
$tahun=substr($tanggal,0,4);

echo "<table>
	<tr>
		<input type='text' name='umak' id='umak' class='input_header' hidden/></td>
		<input type='text' name='sex' id='sex' class='input_header' hidden/></td>
		<input type='text' name='kawin' id='kawin' class='input_header' hidden/></td>
		<input type='text' name='anak' id='anak' class='input_header' hidden/></td>
		<input type='text' name='kwprs' id='kwprs' class='input_header' hidden/></td>
		<input type='text' name='kwkar' id='kwkar' class='input_header' hidden/></td>
		<input type='text' name='tkprs' id='tkprs' class='input_header' hidden/></td>
		<input type='text' name='tkkar' id='tkkar' class='input_header' hidden/></td>
		<input type='text' name='tht' id='tht' class='input_header' hidden/></td>
		<input type='text' name='ptkp' id='ptkp' class='input_header' hidden/></td>
		<input type='text' name='ptkpwp' id='ptkpwp' class='input_header' hidden/></td>
		<input type='text' name='ptkpis' id='ptkpis' class='input_header' hidden/></td>
		<input type='text' name='ptkpan' id='ptkpan' class='input_header' hidden/></td>
		<input type='text' name='maxanak' id='maxanak' class='input_header' hidden/></td>
		<input type='text' name='prbijab' id='prbijab' class='input_header' hidden/></td>
		<input type='text' name='maxbijab' id='maxbijab' class='input_header' hidden/></td>
		<input type='text' name='prpph1' id='prpph1' class='input_header' hidden/></td>
		<input type='text' name='prpph2' id='prpph2' class='input_header' hidden/></td>
		<input type='text' name='prpph3' id='prpph3' class='input_header' hidden/></td>
		<input type='text' name='prpph4' id='prpph4' class='input_header' hidden/></td>
		<input type='text' name='netgaji1' id='netgaji1' class='input_header' hidden/></td>
		<input type='text' name='netgaji2' id='netgaji2' class='input_header' hidden/></td>
		<input type='text' name='netgaji3' id='netgaji3' class='input_header' hidden/></td>
				
		<td>Kode Gaji : <input type='text' name='txt_kode' id='txt_kode' size='8' class='input_header' readonly/></td>
		<td>Tanggal : <input type='date' name='txt_tanggal' id='txt_tanggal' size='10' class='input_header' ></td>
		<td>Bulan : <select name='txt_bulan' id='txt_bulan' class='input_header'>
			<option value='' selected>-Pilih-</option>
			<option value='1'> 1 - JANUARI</option>
			<option value='2'> 2 - FEBRUARI</option>
			<option value='3'> 3 - MARET</option>
			<option value='4'> 4 - APRIL</option>
			<option value='5'> 5 - MEI</option>
			<option value='6'> 6 - JUNI</option>
			<option value='7'> 7 - JULI</option>
			<option value='8'> 8 - AGUSTUS</option>
			<option value='9'> 9 - SEPTEMBER</option>
			<option value='10'>10 - OKTOBER</option>
			<option value='11'>11 - NOVEMBER</option>
			<option value='12'>12 - DESEMBER</option>
			<option value='13'>13 - THR</option>
			</select>
		</td>
		<td>Tahun : <input type='number' name='txt_tahun' id='txt_tahun' value='$tahun' class='input_header'></td>
		<td>Heri Efektif : <input type='text' name='txt_harief' id='txt_harief' size='2' class='input_header'></td>
	</tr>
	</table>
	<hr>";
	
echo "<div id='form_detail'>
	<table width='100%'>
		<tr>
			<input type='text' name='txt_jabatan' id='txt_jabatan' class='input_detail' size='20' hidden />
			<td>Nama : <input type='text' name='txt_nama' id='txt_nama' class='input_detail' size='20' class='input_detail'></td>
			<td>NIK : <input type='text' name='txt_nik' id='txt_nik' size='3' class='input_detail' readonly /> hari</td>
			<td>Sakit : <input type='text' name='txt_sakit' id='txt_sakit' size='2' class='input_detail' > hari</td>
			<td>Ijin : <input type='text' name='txt_ijin' id='txt_ijin' size='2' class='input_detail' > hari</td>
			<td>Alpa : <input type='text' name='txt_alpa' id='txt_alpa' size='2' class='input_detail' > hari</td>
		</tr>
	</table>
	<hr>
	
	<table width='100%'>		
		<tr>
			<th>Gaji Pokok</th>
			<th>Tunj. Makan</th>
			<th>Tunj. BPJS</th>
			<th>Tunj. Jabatan</th>
			<th>Tunjangan Lain</th>
			<th></th>
			<th>Total Gaji</th>			
		</tr>		
		<tr>						
			<td align='center'><input type='text' name='txt_gapok' id='txt_gapok' class='input_detail' size='7' readonly /></td>
			<td align='center'><input type='text' name='txt_tunmak' id='txt_tunmak' class='input_detail' size='7' ></td>
			<td align='center'><input type='text' name='txt_tunbpjs' id='txt_tunbpjs' class='input_detail' size='7' /></td>
			<td align='center'><input type='text' name='txt_tunjab' id='txt_tunjab' class='input_detail' size='7' readonly /></td>
			<td align='center'><input type='text' name='txt_namatl' id='txt_namatl' class='input_detail' size='15' ></td>
			<td align='center'><input type='text' name='txt_jumlahtl' id='txt_jumlahtl' class='input_detail' size='7' ></td>			
			<td align='center'><input type='text' name='txt_totgaji' id='txt_totgaji' class='input_detail' size='7' ></td>
		</tr>
	</table>
		
	<table>
		<tr>						
			<th>Pot. Absen</th>
			<th>UM Gaji</th>
			<th>Angsuran</th>
			<th>Pot. BPJS</th>
			<th>Pot. PPh21</th>			
			<th>Potongan Lain</th>
			<th></th>
			<th>Gaji Bersih</th>
			<th></th>
		</tr>
		
		<tr>
			<td align='center'><input type='text' name='txt_potabsen' id='txt_potabsen' class='input_detail' size='7' readonly /></td>
			<td align='center'><input type='text' name='txt_umgaji' id='txt_umgaji' class='input_detail' size='7' ></td>
			<td align='center'><input type='text' name='txt_angsuran' id='txt_angsuran' class='input_detail' size='7' ></td>
			<td align='center'><input type='text' name='txt_potbpjs' id='txt_potbpjs' class='input_detail' size='7' /></td>
			<td align='center'><input type='text' name='txt_potpph' id='txt_potpph' class='input_detail' size='7' /></td>
			<td align='center'><input type='text' name='txt_namapl' id='txt_namapl' class='input_detail' size='14' ></td>
			<td align='center'><input type='text' name='txt_jumlahpl' id='txt_jumlahpl' class='input_detail' size='7' ></td>
			<td align='center'><input type='text' name='txt_gaber' id='txt_gaber' class='input_detail' size='7' readonly /></td>		
			<td align='right'><button name='simpan' id='simpan'>Rekam</button></td>
		</tr>
	</table>
	
	<div id='tampil_detail'></div>
	</div>";

echo "<table align='center'>
		<tr>
			<td><button name='mulai' id='mulai'>Mulai</button> <button name='tutup' id='tutup'>Keluar</button></td>
		</tr>
	</table>";

echo "<div id='form_cari_kar' title='Pencarian Data Karyawan'
		<table width='100%'>
			<td>
				<td width='20%'>Cari Karyawan</td>
				<td width='2%'>:</td>
				<td><input type='text' name='txt_carikar' id='txt_carikar' size='50'</td>
			</td>
		</table>
		<div id='info_kar' align='center'></div>
	</div>";
	
echo "<div id='data_detail'></div>";
?>
