<script type="text/javascript">
$(document).ready(function(){
	$("#input_pj").hide();
	$("#data_detail").hide();
	$("#lihat_detail").hide();
	
	cari_user();
	
	function cari_user(){
		var no = 1;
		$.ajax({
			type	: "POST",
			url	    : "modul/jual_kd/cari_user.php",
			data	: "no="+no,
			dataType: "json",
			success	: function(data){
				$("#txt_user").val(data.user);
				$("#txt_hgbeli").val(data.hgbeli);
				$("#txt_hgjual").val(data.hgjual);
				cek_pending();
			}
		});
	}
	
	function cek_pending(){
		var no = 1;
		
		$.ajax({
			type	: "POST",
			url		: "modul/jual_kd/cek_data_pending.php",
			data	: "no="+no,
			dataType: "json",
			success	: function(data){
				$("#txt_kode").val(data.kode);
				$("#txt_tanggal").val(data.tanggal);
				$("#txt_kodesp").val(data.kodesp);
				$("#txt_namasp").val(data.namasp);
				$("#txt_kodedv").val(data.kodedv);
				$("#txt_namadv").val(data.namadv);
				$("#txt_nopol").val(data.nopol);
				var kode = $("#txt_kode").val();
				
				if(!kode==''){
					$('#input_pj').show();
					$("#txt_namapl").focus();
					cari_nourut();
					tampil_data_detail();
				}
				else $("#mulai").focus();
			}
		});
	}
	
	$("#mulai").click(function(){
		$(".input_detail").val('');
		$(".input_header").val('');
		$(".input_bayar").val('');
		$("#input_pj").show();
		$("#data_detail").show();
		cari_tanggal();
	});
	
	function cari_tanggal(){
		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/jual_kd/cari_nomor.php",
			data	: "tanggal="+tanggal,
			dataType: "json",
			success	: function(data){
				$("#txt_kode").val(data.nomor);
				$("#input_pj").show();
				cari_nourut();
				tampil_data_detail();
			}
		});
	}
	
	function tampil_data_detail(){
		var kode = $("#txt_kode").val();
		$("#data_detail").show();
		$("#data_detail").load("modul/jual_kd/tampil_data_detail.php?kode="+kode);
		cari_total();
	}
	
	function cari_total(){
		var kode=$("#txt_kode").val();
		
		$.ajax({
			type	: "POST",
			url	    : "modul/jual_kd/cari_total.php",
			data	: "kode="+kode,
			dataType: "json",
			success	: function(data){
				$("#txt_tunit").val(data.tunit);
				$("#txt_total").val(data.total);
				$("#txt_ctotal").val(data.total);
				format_total();
			}
		});
	}
	
	function format_total(){
		var angka 	= $("#txt_total").val();
		var jhuruf	= angka.length;
				
		$.ajax({
			type	: "POST",
			url	    : "modul/jual_kd/format_rupiah.php",
			data	: "angka="+angka+
					"&jhuruf="+jhuruf,
			dataType: "json",
			success	: function(data){				
				$("#txt_ctotal").val(data.huruf);
				var ctotal=$("#txt_ctotal").val();				
			}
		});
	}
	
	$('#txt_tanggal').keyup(function(){
		cari_nomor();
	});
	
	function cari_nourut(){
		var kode = $("#txt_kode").val();
				
		$.ajax({
			type	: "POST",
			url	    : "modul/jual_kd/cari_nourut.php",
			data	: "kode="+kode,
			dataType: "json",
			success	: function(data){
				$("#txt_nourut").val(data.nourut);
			}
		});
	}

	$('#form_cari_plg').dialog({
		autoOpen: false,
		modal	: true,
		width: 600,
	});
		
	$('#txt_namapl').keyup(function(){
		var cari= $("#txt_cariplg").val();
		$('#form_cari_plg').dialog('open');
		$("#info_pelanggan").load('modul/jual_kd/tampil_data_plg.php?cari='+cari);
		$("#txt_cariplg").focus();
	});
	
	$("#txt_cariplg").keyup(function(){
		$("#info_pelanggan").show();
		var cari	= $("#txt_cariplg").val();
		
		$.ajax({
			type	: "POST",
			url		: "modul/jual_kd/tampil_data_plg.php",
			data	: "cari="+cari,
			timeout	: 3000,
			beforeSend	: function(){
				$("#info_pelanggan").html("<img src='loading.gif' />");			
			},				  
			success	: function(data){
				$("#info_pelanggan").html(data);
			}
		});
	});
	
	$("#txt_jmlunit").keyup(function(){
		hitung_jumlah();
	});
	
	$("#txt_hgjual").keyup(function(){
		hitung_jumlah();
	});
		
	function hitung_jumlah(){
		var unit	=$("#txt_jmlunit").val();
		var hgjual	=$("#txt_hgjual").val();
		var hgbeli	=$("#txt_hgbeli").val();
		
		if(unit=='') var unit=0;
		else var unit	=parseInt(unit);
		
		if(hgjual=='')  var hgjual=0
		else var hgjual =parseInt(hgjual);
		
		var jumlah	=unit*parseInt(hgjual);
		var hpp		=unit*parseInt(hgbeli);
		$("#txt_jumlah").val(jumlah);
		$("#txt_hpp").val(hpp);
	}
	
	$("#simpan").click(function(){
		simpan_data();
	});
		
	function simpan_data(){
		var kode	= $("#txt_kode").val();
		var tanggal	= $("#txt_tanggal").val();
		var kodesp 	= $("#txt_kodesp").val();
		var kodedv 	= $("#txt_kodedv").val();
		var nopol  	= $("#txt_nopol").val();
		var nourut	= $("#txt_nourut").val();
		var kodepl	= $("#txt_kodepl").val();
		var namapl	= $("#txt_namapl").val();
		var jmlunit	= $("#txt_jmlunit").val();
		var hgjual	= $("#txt_hgjual").val();
		var jumlah	= $("#txt_jumlah").val();
		var hpp   	= $("#txt_hpp").val();
		var nobank 	= $("#txt_nobank").val();
		var nabank 	= $("#txt_nabank").val();
		var user	= $("#txt_user").val();
		
		var error = false;
		
		if(kode.length == 0){
           var error = true;
           alert("Maaf, Nomor surat jalan masih kosong, klik Input Penjualan untuk buat nomor baru.");
		   $("#mulai").focus();
		   return (false);
        }

		if(kodepl.length == 0){
           var error = true;
           alert("Maaf, Kode pelanggan tidak boleh kosong.");
		   $("#txt_namapl").focus();
		   return (false);
        }

		if(jmlunit.length == 0){
           var error = true;
           alert("Maaf, Jumlah unit tidak boleh kosong.");
		   $("#txt_jmlunit").focus();
		   return (false);
        }
		
		if(hgjual.leagth == 0){
			alert("Maaf, harga jual tidak boleh kosong.");
			$("#txt_hgjual").focus();
		   return (false);
		}

		if(error == false){
        
		$.ajax({
			type	: "POST",
			url	    : "modul/jual_kd/simpan.php",
			data	: "kode="+kode+
					"&tanggal="+tanggal+
					"&kodesp="+kodesp+
					"&kodedv="+kodedv+
					"&nopol="+nopol+
					"&nourut="+nourut+
					"&kodepl="+kodepl+
					"&namapl="+namapl+
					"&jmlunit="+jmlunit+
					"&hgjual="+hgjual+
					"&jumlah="+jumlah+
					"&hpp="+hpp+
					"&nobank="+nobank+
					"&nabank="+nabank+
					"&user="+user,
			timeout	: 3000,
			beforeSend	: function(){
				$("#info").html("<img src='loading.gif' />");
			},
			success	: function(data){
				$("#info").html(data);
				$(".input_detail").val('');
				$("#txt_cariplg").val('');
				$("#txt_namapl").focus();
				cari_nourut();
				tampil_data_detail();
			}
		});
		}
	}
	
	$('#form_lihat_detail').dialog({
		autoOpen: false,
		modal	: true,
		width: 700,
	});
	
	$("#detail").click(function(){
		$('#form_lihat_detail').dialog('open');
		var kode = $("#txt_kode").val();
		$("#lihat_detail").show();
		$("#lihat_detail").load("modul/jual_kd/tampil_detail.php?kode="+kode);
	});

	$("#rekam_pj").click(function(){
		var kode	= $("#txt_kode").val();
		var tanggal = $("#txt_tanggal").val();
		var kodesp  = $("#txt_kodesp").val();
		var namasp  = $("#txt_namasp").val();
		var tunit	= $("#txt_tunit").val();
		var total	= $("#txt_total").val();
		var user    = $("#txt_user").val();
		
		var error = false;

		if(kode.length == 0){
           var error = true;
           alert("Maaf, Nomor surat jalan masih kosong, klik Buat Surat Jalan untuk buat nomor baru.");
		   return (false);
        }

		if(total.length == 0){
			var error = true;
			alert("Maaf, data input surat jalan masih kosong.");
			$("#txt_kodest").focus();
			return (false);
        }

		if(error == false){
			$.ajax({
				type	: "POST",
				url	    : "modul/jual_kd/simpan_pj.php",
				data	: "kode="+kode+
					"&tanggal="+tanggal+
					"&kodesp="+kodesp+
					"&namasp="+namasp+
					"&total="+total+
					"&tunit="+tunit+
					"&user="+user,
				timeout	: 1500,
				beforeSend	: function(){
					$("#info").html("<img src='loading.gif'/>");
				},
				success	: function(data){
					$("#info").html(data);
					$("#data_detail").hide();
					$("#txt_posting").val('P');
				}
			});
		}
	});

	$("#batal_pj").click(function(){
		var kode= $("#txt_kode").val();
		
		if(kode.length == 0){
           var error = true;
           alert("Maaf, Nomor faktur  masih kosong.");
		   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/jual_kd/batal_input.php",
				data	: "kode="+kode,
				success	: function(data){
					$("#info").html(data);
					$(".input_detail").val('');
					$(".input_header").val('');
					$("#txt_caribr").val('');
					$("#input_pj").hide();
					$("#data_detail").hide();
					$("#mulai").focus();
				}
			});
		}
	});

	$("#tutup").click(function(){
		document.location='?module=home';
	});
});
</script>

<style type="text/css">
#txt_kode {
	background:#CCC;
}
#txt_ctotal{
	font-size:25px;
	background:#CCC;
}
.data_detail{
	font-size:14px;
}
.input_detail readonly {
	background:#CCC;
}
.button {
	color:blue;
}
</style>

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

echo "<table width='100%'>	
	<tr>
		<input type='text' name='txt_user' id='txt_user' hidden/>
		<input type='text' name='txt_posting' id='txt_posting' class='input_header' hidden />
		<input type='text' name='txt_hgbeli' id='txt_hgbeli' hidden />
		<td>No.Surat Jalan : <input type='text' name='txt_kode' id='txt_kode' size='14' class='input_header' readonly/></td>
		<td>Tanggal : <input type='date' name='txt_tanggal' id='txt_tanggal' size='10' class='input_header' ></td>
		<td>SPBE : <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>Pengemudi : <select id='txt_kodedv' class='input_header'>";
			$sql = mysql_query("SELECT * FROM driver ORDER BY kode_dv");
			while($r=mysql_fetch_array($sql)){
				$kodedv = $r[kode_dv];
				echo "<option value='$kodedv'>$kodedv - $r[nama_dv]</option>";
			}
			echo "</select>
		</td>
		<td>Nomor Polisi : <select id='txt_nopol' class='input_header'>";
			$sql = mysql_query("SELECT * FROM truk ORDER BY nopol");
			while($r=mysql_fetch_array($sql)){
				$nopol = $r[nopol];
				echo "<option value='$nopol'>$nopol - $r[nama_kd]</option>";
			}
			echo "</select>
		</td>
	</tr>
	</table>
	<hr>";

echo "<div id='form_input'>
	<table>
		<tr>
			<th>Nama Pangkalan</th>
			<th>Kode</th>
			<th>Kode Bank</th>
			<th>Nama Bank</th>
			<th>Jumlah Unit</th>
			<th>Harga Satuan</th>
			<th></th>
			<th>Jumlah Harga</th>
		</tr>
		<tr>
			<input type='text' name='txt_nourut' id='txt_nourut' class='input_detail' size='6' hidden/>
			<input type='text' name='txt_hpp' id='txt_hpp' class='input_detail' hidden/>
			<td><input type='text' name='txt_namapl' id='txt_namapl' class='input_detail' size='30' ></td>
			<td align='center'><input type='text' name='txt_kodepl' id='txt_kodepl' class='input_detail' size='4' readonly/></td>
			<td><input type='text' id='txt_nobank' size='10' class='input_header'></td>
			<td><input type='text' id='txt_nabank' size='25' class='input_header'></td>
			<td align='center'><input type='text' name='txt_jmlunit' id='txt_jmlunit' class='input_detail' size='10' maxlength='10'></td>
			<td align='center'><input type='text' name='txt_hgjual' id='txt_hgjual' size='10' maxlength='10'></td>
			<td><button name='simpan' id='simpan'>Rekam</button></td>
			<td align='center'><input type='text' name='txt_jumlah' id='txt_jumlah' class='input_detail' size='12' maxlength='12'></td>			
		</tr>
	</table>
	<hr>
	</div>";

echo "<div id='input_pj'>
	<table width='100%'>
	<tr>
		<input align='right' type='text' name='txt_tunit' id='txt_tunit' class='input_header' hidden />
		<input align='right' type='text' name='txt_total' id='txt_total' class='input_header' hidden />
		<td>Total : <input style='color:maroon' align='right' type='text' name='txt_ctotal' id='txt_ctotal' class='input_bayar' size='9' readonly /></td>
		<td><button name='rekam_pj' id='rekam_pj'>Rekam Penjualan</button>
			<button name='batal_pj' id='batal_pj'>Batal</button>
		</td>
		<td align='right'>
			<button name='detail' id='detail'>Lihat Detail</button>
		</td>
	</tr>
	</table>
	</div>";

echo "<div id='form_cari_plg' title='Pencarian Data Pangkalan'>
		<table width='100%'>
			<tr>
				<td>Cari Pangkalan : <input type='text' name='txt_cariplg' id='txt_cariplg' size='40'></td>
			</tr>
		</table>
		<div id='info_pelanggan' align='center' style='overflow-x: auto;height: 350px;scroll-behavior: smooth'></div>
	</div>";

echo "<div id='data_detail' style='overflow-x: auto;height: 300px;scroll-behavior: smooth'></div>";
	
echo "<div id='form_lihat_detail' title='Detail nota'>
		<div id='lihat_detail' style='overflow-x: auto;height: 350px;scroll-behavior: smooth'></div>
	</div>";
		
echo "<table width='100%'>
	<tr>
		<td colspan='3' align='center'>
			<button name='mulai' id='mulai'>Input Penjualan</button>
			<button name='tutup' id='tutup'>Tutup</button>
		</td>
	</tr>
	</table>";
?>