<script type="text/javascript">
	$(document).ready(function(){
	$("#input_bon").hide();
	
	$("#mulai").focus();
	
	$('#mulai').click(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_tanggal").val(data.tanggal);
				$("#txt_tanggal").focus();
				cari_nomor();
				tampil_data_detail();
			}
		});
	}
	
	function cari_nomor(){
		var tanggal	= $("#txt_tanggal").val();
		
		$.ajax({
			type	: "POST",
			url		: "modul/tebus_do/cari_nomor.php",
			data	: "tanggal="+tanggal,
			dataType: "json",
			success	: function(data){
				$("#txt_kode").val(data.nomor);
				$("#txt_hgbeli").val(data.hgbeli);
			}
		});
	}
	
	function tampil_data_detail(){
		var tanggal = $("#txt_tanggal").val();
		$("#data_detail").load('modul/tebus_do/tampil_data_detail.php?tanggal='+tanggal);
	}
	
	$('#txt_tanggal').keyup(function(){
		cari_nomor();
	});
	
	$('#form_cari_bank').dialog({
		autoOpen: false,
		modal	: true,
		width: 600,
	});
		
	$('#txt_namabank').keyup(function(){
		cari_nomor();
		$('#form_cari_bank').dialog('open');
		$("#txt_caribk").focus();
	});
	
	$('#txt_namabank').click(function(){
		$('#form_cari_bank').dialog('open');
		$("#txt_caribk").focus();
	});
	
	$("#txt_caribk").keyup(function(){
		var cari	= $("#txt_caribk").val();
		
		var no=1;
		$.ajax({
			type	: "POST",
			url	: "modul/tebus_do/tampil_data_bank.php",
			data	: "cari="+cari,
			timeout	: 3000,
			beforeSend	: function(){		
				$("#info_bank").html("<img src='loading.gif' />");			
			},				  
			success	: function(data){
				$("#info_bank").html(data);				
			}
		});
	});
		
	$('#txt_jmlunit').keyup(function(){
		hitung_jumlah();
	});
	
	$('#txt_hgbeli').keyup(function(){
		hitung_jumlah();
	});
	
	function hitung_jumlah(){
		var hgbeli	= $("#txt_hgbeli").val();
		var jmlunit	= $("#txt_jmlunit").val();
		
		if(hgbeli.length==0) var hgbeli=0;
		else var hgbeli=parseInt(hgbeli);
		
		if(jmlunit.length==0) var jmlunit=0;
		else var jmlunit=parseInt(jmlunit);
		
		var jumlah  = jmlunit*hgbeli;
		$("#txt_jumlah").val(jumlah);
	}
	
	$('#txt_setor').keyup(function(){
		var jumlah	= $("#txt_jumlah").val();
		var setor 	= $("#txt_setor").val();
		
		if(setor.length==0) var setor=0;
		else var setor=parseInt(setor);
		
		if(jumlah.length==0) var jumlah=0;
		else var jumlah=parseInt(jumlah);
		
		var bulat = setor-jumlah;
		$("#txt_bulat").val(bulat);
	});
		
	$('#simpan').click(function(){
		simpan_data();
	});
		
	function simpan_data(){
		var kode	= $("#txt_kode").val();
		var tanggal	= $("#txt_tanggal").val();
		var nomorsa	= $("#txt_nomorsa").val();
		var nobank 	= $("#txt_nobank").val();
		var namabank= $("#txt_namabank").val();
		var kodesp	= $("#txt_kodesp").val();
		var namasp	= $("#txt_namasp").val();
		var jmlunit	= $("#txt_jmlunit").val();
		var hgbeli 	= $("#txt_hgbeli").val();
		var jumlah	= $("#txt_jumlah").val();
		var setor	= $("#txt_setor").val();		
		var bulat	= $("#txt_bulat").val();
		
		var error = false;
		
		if(kode.length == 0){
           var error = true;
           alert("Maaf, Form input belum diisi secara lengkap.");
		   $("#txt_tanggal").focus();
		   return (false);
        }
		
		if(jmlunit.length == 0){
           var error = true;
           alert("Maaf, Jumlah unit masih kosong.");
		   $("#txt_jmlunit").focus();
		   return (false);
        }
		
		if(hgbeli.length == 0){
           var error = true;
           alert("Maaf, Harga beli masih kosong.");
		   $("#txt_hgbeli").focus();
		   return (false);
        }
		 
		if(error == false){
		$.ajax({
			type	: "POST",
			url	    : "modul/tebus_do/rekam.php",
			data	: "kode="+kode+
					"&tanggal="+tanggal+
					"&nomorsa="+nomorsa+
					"&nobank="+nobank+
					"&namabank="+namabank+
					"&kodesp="+kodesp+
					"&namasp="+namasp+
					"&jmlunit="+jmlunit+
					"&hgbeli="+hgbeli+
					"&jumlah="+jumlah+
					"&setor="+setor+
					"&bulat="+bulat,
			timeout	: 2000,
			beforeSend	: function(){		
				$("#info").html("<img src='loading.gif' />");			
			},				  
			success	: function(data){
				$("#info").html(data);
				$(".input").val('');
				$("#txt_tanggal").focus();
				tampil_data_detail();
			}
		});
		}
	}
	
	$("#tutup").click(function(){
		document.location='?module=home';
	});
});
</script>

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

<?php
include "../../inc/inc.koneksi.php";

echo "<table width='100%'>	
	<tr>
		<td>Kode Transaksi</td>
		<td>: <input type='text' name='txt_kode' id='txt_kode' size='12' readonly class='input' ></td>
		<td>Tanggal</td>
		<td>: <input type='date' name='txt_tanggal' id='txt_tanggal' size='10' ></td>
		<td>Nomor SA</td>
		<td>: <input type='text' name='txt_nomorsa' id='txt_nomorsa' size='15' class='input'></td>
		<td>Rekening Bank</td>
		<td>: <select id='txt_nobank' class='input_header'>";
			$sql = mysql_query("SELECT * FROM akun_bb WHERE noakun BETWEEN '11111' AND '11119' ORDER BY noakun");
			while($r=mysql_fetch_array($sql)){
				$noakun = $r[noakun];
				echo "<option value='$noakun'>$noakun - $r[naakun]</option>";
			}
			echo "</select>
		</td>
	</tr>
	<tr>
		<td>SPBE</td>
		<td>: <select name='txt_kodesp' id='txt_kodesp' class='input' >";
			$sql = mysql_query("SELECT * FROM supplier ORDER BY kode_sp");
			
			while($r=mysql_fetch_array($sql)){
				$kodesp = $r[kode_sp];
				echo "<option value='$kodesp'>$kodesp - $r[nama]</option>";
			}
			echo "</select>
		</td>
		<td>Jumlah Unit</td>
		<td>: <input type='text' name='txt_jmlunit' id='txt_jmlunit' size='6' class='input' ></td>
		<td>Harga Beli</td>
		<td>: <input type='text' name='txt_hgbeli' id='txt_hgbeli' size='9' class='input'></td>
		<td>Jumlah Harga</td>
		<td>: <input type='text' name='txt_jumlah' id='txt_jumlah' size='12' class='input' readonly /></td>
	</tr>
	<tr>
		<td>Jumlah Setor</td>
		<td>: <input type='text' name='txt_setor' id='txt_setor' size='12' class='input' ></td>
		<td>Pembulatan</td>
		<td>: <input type='text' name='txt_bulat' id='txt_bulat' size='9' class='input' readonly /></td>
		<td></td>
		<td>
			<button name='simpan' id='simpan'>Rekam</button>	
			<button name='mulai' id='mulai'>Mulai</button>
			<button name='tutup' id='tutup'>Tutup</button>
		</td>
		<td></td>
		<td></td>
	</tr>
	</table>";

echo "<div id='form_cari_bank' title='Pencarian Akun bank'>
		<table width='100%'>
			<tr>
				<td>Cari Akun bank : <input type='text' name='txt_caribk' id='txt_caribk' size='40'></td>
			</tr>
		</table>
		<div id='info_bank' align='center'></div>
	</div>";

echo "<div id='data_detail' align='center'><div>";
?>
