<script type="text/javascript">
$(document).ready(function(){
	$("#input_pj").hide();
	
	cari_user();
	
	function cari_user(){
		var no = 1;
		$.ajax({
			type	: "POST",
			url		: "modul/ubah_status/cari_user.php",
			data	: "no="+no,
			dataType: "json",
			success : function(data){				
				$("#txt_user").val(data.user);
				$("#mulai").focus();
			}
		});
	}
	
	$("#mulai").click(function(){
		$(".input_detail").val('');
		$(".input_header").val('');
		$(".input_bayar").val('');
		$("#txt_caribr").val('');
		$("#input_pj").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/ubah_status/cari_nomor.php",
			data	: "tanggal="+tanggal,					
			dataType: "json",
			success : function(data){
				$("#txt_kode").val(data.nomor);
				$("#input_pj").show();
				cari_nourut();
				tampil_data_kl();
				tampil_data_ms();
			}
		});
	}
	
    function cari_nourut(){
		var kode = $("#txt_kode").val();
		
		$.ajax({
			type	: "POST",
			url		: "modul/ubah_status/cari_nourut.php",
			data	: "kode="+kode,
			dataType: "json",
			success : function(data){
				$("#txt_nourut").val(data.nourut);
			}
		});
	}
	
	function tampil_data_kl(){
		var kode = $("#txt_kode").val();
		$("#data_detail_kl").show();
		$("#data_detail_kl").load("modul/ubah_status/tampil_data_kl.php?kode="+kode);
		cari_total_kl();
	}
	
	function cari_total_kl(){
		var kode = $("#txt_kode").val();
		
		$.ajax({
			type	: "POST",
			url		: "modul/ubah_status/hitung_totkl.php",
			data	: "kode="+kode,
			dataType: "json",
			success	: function(data){
				$("#txt_tjumlah1").val(data.tjumlah1);
				format_asal();
			}
		});
	}
	
	function format_asal(){
		var angka = $("#txt_tjumlah1").val();
		var jhuruf= angka.length;
		
		$.ajax({
			type	: "POST",
			url		: "modul/ubah_status/format_rupiah.php",
			data	: "angka="+angka+
					"&jhuruf="+jhuruf,
			dataType: "json",
			success	: function(data){
				$("#txt_ctotasal").val(data.huruf);
			}
		});
	}
	
	function tampil_data_ms(){
		var kode = $("#txt_kode").val();
		$("#data_detail_ms").show();
		$("#data_detail_ms").load("modul/ubah_status/tampil_data_ms.php?kode="+kode);
		cari_total_ms();
	}
	
	function cari_total_ms(){
		var kode = $("#txt_kode").val();
		
		$.ajax({
			type	: "POST",
			url		: "modul/ubah_status/hitung_totms.php",
			data	: "kode="+kode,
			dataType: "json",
			success	: function(data){
				$("#txt_tjumlah2").val(data.tjumlah2);
				$("#txt_tselisih").val(data.tselisih);
				format_hasil();
				format_selisih();
			}
		});
	}
	
	function format_hasil(){
		var angka = $("#txt_tjumlah2").val();
		var jhuruf= angka.length;
		
		$.ajax({
			type	: "POST",
			url		: "modul/ubah_status/format_rupiah.php",
			data	: "angka="+angka+
					"&jhuruf="+jhuruf,
			dataType: "json",
			success	: function(data){
				$("#txt_ctothasil").val(data.huruf);
			}
		});
	}
	
	function format_selisih(){
		var angka 	= $("#txt_tselisih").val();
		var jhuruf	= angka.length;
		
		$.ajax({
			type	: "POST",
			url		: "modul/ubah_status/format_rupiah.php",
			data	: "angka="+angka+
					"&jhuruf="+jhuruf,
			dataType: "json",
			success	: function(data){
				$("#txt_cselisih").val(data.huruf);
			}
		});
	}
	
	$('#txt_tanggal').keyup(function(){
		cari_nomor();
	});
	
	$('#form_cari_kodepsb').dialog({
		autoOpen: false,
		modal	: true,
		width: 700,
	});
		
	$('#cari_kodepsb').click(function(){
		$('#form_cari_kodepsb').dialog('open');
		$("#info_psb").show();	
		$("#txt_carikode").focus();		
	});
		
	$("#txt_carikode").keyup(function(){
		var cari = $("#txt_carikode").val();
		
		$.ajax({
			type	: "POST",
			url		: "modul/ubah_status/tampil_kodepsb.php",
			data	: "cari="+cari,
			success	: function(data){
				$("#info_psb").html(data);				
			}
		});
	});
	
	$('#form_cari_barang1').dialog({
		autoOpen: false,
		modal	: true,
		width: 750,
	});
	
	$('#txt_namast1').click(function(){
		$('#form_cari_barang1').dialog('open');
		$("#txt_caribr1").focus();
	});
	
	$('#txt_namast1').keyup(function(){
		$('#form_cari_barang1').dialog('open');
		$("#txt_caribr1").focus();
	});
	
	$("#txt_caribr1").keyup(function(){
		var cari   = $("#txt_caribr1").val();
		var gudang = $("#txt_gudang").val();
		
		$.ajax({
			type	: "GET",
			url		: "modul/ubah_status/tampil_data_barang1.php",
			data	: "cari="+cari+
					"&gudang="+gudang,
			success	: function(data){
				$("#info_barang1").html(data);
			}
		});
	});
	
	$('#form_cari_barang2').dialog({
		autoOpen: false,
		modal	: true,
		width: 750,
	});
	
	$('#txt_namast2').click(function(){
		$('#form_cari_barang2').dialog('open');
		$("#txt_caribr2").focus();
	});
	
	$('#txt_namast2').keyup(function(){
		$('#form_cari_barang2').dialog('open');
		$("#txt_caribr2").focus();
	});
	
	$("#txt_caribr2").keyup(function(){
		var cari = $("#txt_caribr2").val();
		var gudang = $("#txt_gudang").val();
		
		$.ajax({
			type	: "GET",
			url		: "modul/ubah_status/tampil_data_barang2.php",
			data	: "cari="+cari+
					"&gudang="+gudang,
			success	: function(data){
				$("#info_barang2").html(data);
			}
		});
	});
		
	$("#txt_jmlunit1").keyup(function(){
		hitung_jumlah1();
	});
	
	$("#txt_hgbeli1").keyup(function(){
		hitung_jumlah1();
	});
	
	$("#txt_jmlunit2").keyup(function(){
		hitung_jumlah2();
	});
	
	$("#txt_hgbeli2").keyup(function(){
		hitung_hgbeli();
	});
	
	function hitung_jumlah1(){
		var jmlunit	= $("#txt_jmlunit1").val();
		var hgbeli	= $("#txt_hgbeli1").val();
				
		if(jmlunit=='') var njmlunit=0;
		else var njmlunit=parseInt(jmlunit);
		
		if(hgbeli=='') var nhgbeli=0;
		else var nhgbeli=parseFloat(hgbeli);
		
		var jumlah  = njmlunit*nhgbeli;
		var jumlah	= jumlah.toFixed(0);
		$("#txt_jumlah1").val(jumlah);
	}
	
	function hitung_jumlah2(){
		var jmlunit	= $("#txt_jmlunit2").val();
		var hgbeli	= $("#txt_hgbeli2").val();
		var tjumlah	= $("#txt_tjumlah1").val();
		
		if(jmlunit=='') var njmlunit=0;
		else	var njmlunit=parseInt(jmlunit);
		
		if(tjumlah=='') var ntjumlah=0;
		else var ntjumlah=parseInt(tjumlah);
		
		if(hgbeli=='') var nhgbeli=0;
		else var nhgbeli=parseFloat(hgbeli);
				
		if(nhgbeli==0){
			var jumlah2=ntjumlah;			
			if(njmlunit>0) var nhgbeli=jumlah2/njmlunit;
		}else var jumlah2=njmlunit*nhgbeli;
		
		$("#txt_hgbeli2").val(nhgbeli);
		$("#txt_jumlah2").val(jumlah2);
	}
	
	function hitung_hgbeli(){
		var hgbeli 	= $("#txt_hgbeli2").val();
		var jmlunit	= $("#txt_jmlunit2").val();
		
		if(hgbeli=='') var nhgbeli=0;
		else var nhgbeli=parseFloat(hgbeli);
		
		if(jmlunit=='') var njmlunit=0;
		else var njmlunit=parseInt(jmlunit);
		
		var jumlah2  = njmlunit*nhgbeli;
		$("#txt_hgbeli2").val(nhgbeli);
		$("#txt_jumlah2").val(jumlah2);
	}
	
	$("#simpan_kl").click(function(){
		var kode	= $("#txt_kode").val();
		var tanggal	= $("#txt_tanggal").val();
		var gudang 	= $("#txt_gudang").val();
		var keter  	= $("#txt_keter").val();
		var nourut 	= $("#txt_nourut").val();
		var kodest1	= $("#txt_kodest1").val();
		var namast1	= $("#txt_namast1").val();
		var satuan1	= $("#txt_satuan1").val();
		var jmlunit1= $("#txt_jmlunit1").val();
		var hgbeli1	= $("#txt_hgbeli1").val();		
		var jumlah1	= $("#txt_jumlah1").val();
		var user    	= $("#txt_user").val();
		
		if(kode.length == 0){
           alert("Maaf, Nomor PSB masih kosong, klik Input PSB untuk buat nomor baru.");
		   return (false);
        }
		if(kodest1.length == 0){        
           alert("Maaf, Kode barang asal masih kosong.");
		   $("#txt_namast1").focus();
		   return (false);
        }		
		if(jmlunit1.length == 0){
           alert("Maaf, kolom jumlah unit asal masih kosong.");
		   $("#txt_jmlunit1").focus();
		   return (false);
        }

		$.ajax({
			type	: "POST",
			url		: "modul/ubah_status/simpan_kl.php",
			data	: "kode="+kode+
					"&tanggal="+tanggal+
					"&gudang="+gudang+
					"&keter="+keter+
					"&nourut="+nourut+
					"&user="+user+
					"&kodest1="+kodest1+
					"&namast1="+namast1+
					"&satuan1="+satuan1+
					"&jmlunit1="+jmlunit1+
					"&hgbeli1="+hgbeli1+
					"&jumlah1="+jumlah1,
			timeout	: 2000,
			beforeSend	: function(){		
				$("#info").html("<img src='loading.gif' />");			
			},				  
			success	: function(data){
				$("#info").html(data);
				$(".input_detail").val('');
				$("#txt_namast1").focus();
				cari_nourut();
				tampil_data_kl();
				tampil_data_ms();
			}
		});
	});
	
	$("#simpan_ms").click(function(){
		var kode	= $("#txt_kode").val();
		var tanggal	= $("#txt_tanggal").val();
		var gudang 	= $("#txt_gudang").val();
		var keter  	= $("#txt_keter").val();
		var nourut 	= $("#txt_nourut").val();
		var kodest2	= $("#txt_kodest2").val();
		var namast2	= $("#txt_namast2").val();
		var satuan2	= $("#txt_satuan2").val();
		var hgbeli2	= $("#txt_hgbeli2").val();
		var jmlunit2= $("#txt_jmlunit2").val();
		var jumlah2 = $("#txt_jumlah2").val();		
		var user    = $("#txt_user").val();
		
		if(kode.length == 0){
           alert("Maaf, Nomor faktur masih kosong, klik Buat Faktur untuk buat nomor baru.");
		   return (false);
        }		
		if(kodest2.length == 0){
           alert("Maaf, Kode barang hasil masih kosong.");
		   $("#txt_namast2").focus();
		   return (false);
        }
		if(jmlunit2.length == 0){
           alert("Maaf, kolom jumlah unit hasil masih kosong.");
		   $("#txt_jmlunit2").focus();
		   return (false);
        }
		if(hgbeli2.length == 0){
           alert("Maaf, kolom harga beli hasil masih kosong.");
		   $("#txt_hgbeli2").focus();
		   return (false);
        }
		  
		$.ajax({
			type	: "POST",
			url		: "modul/ubah_status/simpan_ms.php",
			data	: "kode="+kode+
					"&tanggal="+tanggal+
					"&gudang="+gudang+
					"&keter="+keter+
					"&nourut="+nourut+
					"&user="+user+
					"&kodest2="+kodest2+
					"&namast2="+namast2+
					"&satuan2="+satuan2+
					"&jmlunit2="+jmlunit2+
					"&jumlah2="+jumlah2+
					"&hgbeli2="+hgbeli2,
			timeout	: 2000,
			beforeSend	: function(){		
				$("#info").html("<img src='loading.gif' />");			
			},				  
			success	: function(data){
				$("#info").html(data);
				$(".input_detail").val('');
				$("#txt_namast2").focus();
				cari_nourut();
				tampil_data_kl();
				tampil_data_ms();
			}
		});
	});
	
	$("#simpan_psb").click(function(){
		var kode	= $("#txt_kode").val();
		var tanggal	= $("#txt_tanggal").val();
		var gudang 	= $("#txt_gudang").val();
		var tjumlah1= $("#txt_tjumlah1").val();
		var tjumlah2= $("#txt_tjumlah2").val();		
		var user    = $("#txt_user").val();
		
		if(kode.length == 0){
           alert("Maaf, Nomor PSB masih kosong, klik Input PSB untuk buat nomor baru.");
		   return (false);
        }
		if(tjumlah1.length == 0){			
			alert("Maaf, data barang asal masih kosong.");
			$("#txt_namast1").focus();
			return (false);
        }

		$.ajax({
			type	: "POST",
			url		: "modul/ubah_status/simpan_psb.php",
			data	: "kode="+kode+
					"&tanggal="+tanggal+
					"&gudang="+gudang+
					"&user="+user+
					"&tjumlah1="+tjumlah1+
					"&tjumlah2="+tjumlah2+
					"&user="+user,
			timeout	: 2000,
			beforeSend	: function(){		
				$("#info").html("<img src='loading.gif' />");			
			},				  
			success	: function(data){
				$("#info").html(data);
				$("#data_detail_kl").hide();
				$("#data_detail_ms").hide();
				$("#mulai").focus();
			}
		});
	});
	
	$("#batal_psb").click(function(){
		var kode	= $("#txt_kode").val();
		
		if(kode.length == 0){
           alert("Maaf, Nomor PSB masih kosong.");
		   return (false);
        }
				
		var pilih = confirm('Apakah Anda sungguh-sungguh ingin membatalkan input perubahan status '+kode+' ?');
		if(pilih==true) {
			$.ajax({
				type	: "POST",
				url		: "modul/ubah_status/batal_input.php",
				data	: "kode="+kode,
				success	: function(data){
					$("#info").html(data);
					$(".input_detail").val('');
					$(".input_header").val('');					
					$("#txt_caribr1").val('');
					$("#txt_caribr2").val('');
					$("#input_by").hide();
					$("#data_detail_kl").hide();
					$("#data_detail_ms").hide();
				}
			});
		}
	});

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

<style type="text/css">
#txt_kode {
	background:#CCC;
}
#txt_ctotasal{
	font-size:20px;
	background:#CCC;
}
#txt_ctothasil{
	font-size:20px;
	background:#CCC;
}
#txt_cselisih{
	font-size:20px;
	background:#CCC;
}
</style>

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

echo "<table width='100%'>	
	<tr>
		<input type='text' id='txt_user' hidden/>
		<input type='text' id='txt_nourut' class='input_detail' hidden />		
		<td>Nomor PSB : <input type='text' id='txt_kode' size='14' class='input_header' readonly />
			<img src='images/view.png' width='18' height='18' title='Cari kode transaksi' id='cari_kodepsb'>
		</td>
		<td>Tanggal : <input type='date' name='txt_tanggal' id='txt_tanggal' size='10' class='input_header'></td>
		<td>Gudang : <select id='txt_gudang'>
				<option value='Gudang'>Gudang Pusat</option>";
				$sql=mysql_query("SELECT * FROM cabang WHERE 1 ORDER BY kode_cab");
				while($r=mysql_fetch_array($sql)){
				    $kodecab=$r[kode_cab];
				    echo "<option value='$kodecab'>$r[nama_cab]</option>";
				}
			echo "</select>
		</td>
		<td>Keterangan : <input type='text' id='txt_keter' size='40' class='input_header'></td>
		<td>
			<button id='mulai'>Input PSB</button>
			<button id='tutup'>Tutup</button>
		</td>
	</tr>
	</table>
	<hr>";

echo "<div id='form_input'>	
	<table>
		<tr>
			<td style='color:red'><b>Barang Asal :</b></td>
		</tr>
	</table>
	
	<table>
		<tr>
			<th>Nama Barang</th>
			<th>Kode</th>
			<th>Satuan</th>
			<th>Jml. Unit</th>
			<th>Harga Pokok</th>
			<th>Jumlah Harga</th>
			<th></th>
		</tr>
		<tr>			
			<input type='text' id='txt_tjumlah1' hidden />
			<input type='text' id='txt_jmlstok' hidden />
			<td align='center'><input type='text' id='txt_namast1' class='input_detail' size='35'></td>
			<td align='center'><input type='text' id='txt_kodest1' class='input_detail' size='8' readonly></td>
			<td align='center'><input type='text' id='txt_satuan1' class='input_detail' size='12' readonly/></td>
			<td align='center'><input type='text' id='txt_jmlunit1' class='input_detail' size='4'></td>
			<td align='center'><input type='text' id='txt_hgbeli1' class='input_detail' size='10'></td>
			<td align='center'><input type='text' id='txt_jumlah1' class='input_detail' size='12' readonly/></td>
			<td><button id='simpan_kl'>Rekam</button></td>
		</tr>
	</table>
	<hr>
	
	<table>
		<tr>
			<td style='color:red'><b>Barang Hasil :</b></td>
		</tr>
	</table>
	
	<table>
		<tr>
			<th>Nama Barang</th>
			<th>Kode</th>
			<th>Satuan</th>
			<th>Jml. Unit</th>
			<th>Harga Pokok</th>
			<th>Jumlah Harga</th>
			<th></th>
		</tr>
		<tr>
			<input type='text' id='txt_tjumlah2' hidden />
			<td align='center'><input type='text' id='txt_namast2' class='input_detail' size='35'></td>
			<td align='center'><input type='text' id='txt_kodest2' class='input_detail' size='8' readonly></td>
			<td align='center'><input type='text' id='txt_satuan2' class='input_detail' size='12' readonly/></td>
			<td align='center'><input type='text' id='txt_jmlunit2' class='input_detail' size='4'></td>
			<td align='center'><input type='text' id='txt_hgbeli2' class='input_detail' size='10'></td>
			<td align='center'><input type='text' id='txt_jumlah2' class='input_detail' size='12' readonly/></td>
			<td><button name='simpan_ms' id='simpan_ms'>Rekam</button></td>
		</tr>
	</table>
	<hr>
	</div>";

echo "<div id='form_total'>
	<table width='100%'>
		<tr>
			<input type='text' id='txt_tselisih' class='input_total' hidden />
			<td>Total Asal : <input align='right' type='text' id='txt_ctotasal' style='color:maroon' class='input_total' size='10' readonly /></td>
			<td>Total Hasil : <input align='right' type='text' id='txt_ctothasil' style='color:maroon' class='input_total' size='10' readonly/></td>
			<td>Selisih : <input align='right' type='text' id='txt_cselisih' style='color:maroon' class='input_total' size='10' readonly/></td>
			<td><button id='simpan_psb'>Rekam PSB</button>
				<button id='batal_psb'>Batal PSB</button>
			</td>
		</tr>
	</table>
	</div>";
	
echo "<div id='data_detail_kl'></div><br>";

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

echo "<div id='form_cari_kodepsb' title='Pencarian Kode Transaksi'>
		<table width='100%'>
			<tr>
				<td>Cari kode transaksi : <input type='text' id='txt_carikode' size='20'></td>
			</tr>
		</table>

		<div id='info_psb' align='center'></div>
	</div>";
	
echo "<div id='form_cari_barang1' title='Pencarian Barang'>
		<table width='100%'>
			<tr>
				<td>Cari kode/nama barang : <input type='text' id='txt_caribr1' size='40'></td>
			</tr>
		</table>
		<div id='info_barang1' align='center' style='overflow-x: auto;height: 400px;scroll-behavior: smooth'></div>
	</div>";

echo "<div id='form_cari_barang2' title='Pencarian Barang'>
		<table width='100%'>
			<tr>
				<td>Cari kode/nama barang : <input type='text' id='txt_caribr2' size='40'></td>
			</tr>
		</table>
		<div id='info_barang2' align='center' style='overflow-x: auto;height: 400px;scroll-behavior: smooth'></div>
	</div>";
?>