<script type="text/javascript">
$(document).ready(function(){
	$("#mulai_op").focus();
	
	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){
				$("#op_tanggal").val(data.tanggal);
				$("#op_tgljualaw").val(data.tanggal);
				$("#op_tgljualak").val(data.tanggal);
			}
		});
	}
	
	$("#mulai_op").click(function(){
		$(".input_detail").val('');
		$(".input_header").val('');
		$("#op_carisp").val('');
		$("#op_caribr").val('');
		$("#op_tanggal").focus();
		cari_nomor();
	});
	
	function cari_nomor(){
		var tanggal = $("#op_tanggal").val();
		
		$.ajax({
			type	: "POST",
			url		: "spv/order_pb/cari_nomor.php",
			data	: "tanggal="+tanggal,
			dataType: "json",
			success	: function(data){
				$("#op_kode").val(data.nomor);
				cari_nourut();
				tampil_data_detail();
			}
		});
	}
	
	function cari_nourut(){
		var kode = $("#op_kode").val();

		$.ajax({
			type	: "POST",
			url		: "spv/order_pb/cari_nourut.php",
			data	: "kode="+kode,
			dataType: "json",
			success	: function(data){
				$("#op_nourut").val(data.nourut);
			}
		});
	}
	
	function tampil_data_detail(){
		var kode = $("#op_kode").val();
		$("#data_detail").show();
		$("#data_detail").load("spv/order_pb/tampil_data_detail.php?kode="+kode);
		cari_total();
	}
	
	function cari_total(){
		var kode=$("#op_kode").val();
		
		$.ajax({
			type	: "POST",
			url		: "spv/order_pb/cari_total.php",
			data	: "kode="+kode,
			dataType: "json",
			success	: function(data){
				$("#op_total").val(data.total);
				format_total();
			}
		});
	}
	
	function format_total(){
		var angka 	= $("#op_total").val();
		var jhuruf	= angka.length;
		
		$.ajax({
			type	: "POST",
			url		: "kasir/penjualan/format_rupiah.php",
			data	: "angka="+angka+
					"&jhuruf="+jhuruf,
			dataType: "json",
			success	: function(data){				
				$("#op_ctotal").val(data.huruf);
				var ctotal=$("#op_ctotal").val();
			}
		});
	}
	
	$('#form_cari_sp').dialog({
		autoOpen: false,
		modal	: true,
		width: 700,
	});
		
	$('#op_namasp').keyup(function(){
		cari_nomor();
		var cari= $("#op_carisp").val();
		$('#form_cari_sp').dialog('open');
		$("#info_supplier").load('spv/order_pb/tampil_data_sp.php?cari='+cari);
		$("#op_carisp").focus();
	});
	
	$("#op_carisp").keyup(function(){
		$("#info_supplier").show();
		var cari= $("#op_carisp").val();
		
		$.ajax({
			type	: "POST",
			url		: "spv/order_pb/tampil_data_sp.php",
			data	: "cari="+cari,
			success	: function(data){
				$("#info_supplier").html(data);
			}
		});
	});
	
	$('#form_cari_brg').dialog({
		autoOpen: false,
		modal	: true,
		width: 700,
	});
	
	$('#op_namast').keyup(function(){
		$('#form_cari_brg').dialog('open');
		$("#op_caribr").focus();
	});
	
	$("#op_caribr").keyup(function(){
		var cari 	= $("#op_caribr").val();
		var kodesp	= $("#op_kodesp").val();
				
		$.ajax({
			type	: "GET",
			url		: "spv/order_pb/tampil_data_barang.php",
			data	: "cari="+cari+
					"&kodesp="+kodesp,
			success	: function(data){
			    $("#info_barang").show();
				$("#info_barang").html(data);
			}
		});
	});
	
	$('#form_cari_sopb').dialog({
		autoOpen: false,
		modal	: true,
		width: 800,
	});
		
	$('#cari_sop').click(function(){
		$('#form_cari_sopb').dialog('open');
		$("#info_opb").load("spv/order_pb/tampil_nosopb.php");
		$("#op_carikode").focus();
	});
	
	$("#op_carikode").keyup(function(){
		var cari = $("#op_carikode").val();
		
		$.ajax({
			type	: "POST",
			url		: "spv/order_pb/tampil_nosopb.php",
			data	: "cari="+cari,
			success	: function(data){
				$("#info_opb").html(data);				
			}
		});
	});

	$("#op_jmlunit").keyup(function(){		
		hitung_jumlah();
	});

	$("#op_harga").keyup(function(){
		hitung_jumlah();
	});

	function hitung_jumlah(){
		var unit	= $("#op_jmlunit").val();
		var harga	= $("#op_harga").val();
		
		if(unit=='') var junit=0;
		else var junit=parseInt(unit);
		
		if(harga=='') var jharga=0;
		else var jharga=parseInt(harga);
		
		var njumlah= junit*jharga;
		$("#op_jumlah").val(njumlah);
	}
	
	$("#simpan").click(function(){
		var kode		= $("#op_kode").val();
		var tanggal		= $("#op_tanggal").val();
		var kodecab	    = $("#op_kodecab").val();
		var kodesp		= $("#op_kodesp").val();
		var namasp		= $("#op_namasp").val();
		var nourut 		= $("#op_nourut").val();
		var kodest		= $("#op_kodest").val();
		var namast		= $("#op_namast").val();
		var zataktif	= $("#op_zataktif").val();
		var bentuk  	= $("#op_bentuk").val();
		var tgljualaw	= $("#op_tgljualaw").val();
		var tgljualak	= $("#op_tgljualak").val();
		var jmljual  	= $("#op_jmljual").val();
		var jmlstok  	= $("#op_jmlstok").val();
		var satuan		= $("#op_satuan").val();
		var jmlunit		= $("#op_jmlunit").val();
		var harga		= $("#op_harga").val();
		var jumlah		= $("#op_jumlah").val();
		var keter 		= $("#op_keter").val();
		var total  		= $("#op_total").val();
		var nharga		= parseInt(harga);		
		var njumlah		= parseInt(jumlah);
		
		if(kode.length == 0){
			alert("Maaf, Data penjualan belum ada, klik Buat SOPB untuk mulai.");
			return (false);
        }
		if(tanggal.length == 0){
			alert("Maaf, Tanggal SOPB tidak boleh kosong.");
			return (false);
        }
		if(kodesp.length == 0){
			alert("Maaf, Kode supplier tidak boleh kosong.");
			$("#op_kodesp").focus();
			return (false);
        }
		if(jmlunit.length == 0){
           alert("Maaf, Jumlah barang tidak boleh kosong.");
		   $("#op_jmlunit").focus();
		   return (false);
        }
		
		$.ajax({
			type	: "POST",
			url		: "spv/order_pb/simpan.php",
			data	: "kode="+kode+
					"&tanggal="+tanggal+
					"&kodecab="+kodecab+
					"&kodesp="+kodesp+
					"&namasp="+namasp+
					"&nourut="+nourut+
					"&kodest="+kodest+
					"&namast="+namast+
					"&zataktif="+zataktif+
					"&bentuk="+bentuk+
					"&tgljualaw="+tgljualaw+
					"&tgljualak="+tgljualak+
					"&jmljual="+jmljual+
					"&jmlstok="+jmlstok+
					"&satuan="+satuan+
					"&harga="+harga+
					"&jmlunit="+jmlunit+
					"&jumlah="+jumlah+
					"&keter="+keter,
			timeout	: 2000,
			beforeSend	: function(){
				$("#info").html("<img src='loading.gif' />");			
			},
			success	: function(data){
				$("#info").html(data);
				$(".input_detail").val('');
				$("#op_caribr").val('');
				$("#op_tgljualaw").focus();
				cari_nourut();
				tampil_data_detail();
			}
		});
	});
	
	$("#cetak_sopreg").click(function(){
		var kode	= $("#op_kode").val();
		var kodesp	= $("#op_kodesp").val();
		var namasp	= $("#op_namasp").val();
		var tgl		= $("#op_tanggal").val();
		var kodecab = $("#op_kodecab").val();
		window.location.href='modul/laporan/order_pbreg.php?kode='+kode+'&kodesp='+kodesp+'&namasp='+namasp+'&tgl='+tgl+'&kodecab='+kodecab;
	});
	
	$("#cetak_sopoot").click(function(){
		var kode	= $("#op_kode").val();
		var kodesp	= $("#op_kodesp").val();
		var namasp	= $("#op_namasp").val();
		var tgl		= $("#op_tanggal").val();
		var kodecab = $("#op_kodecab").val();
		window.location.href='modul/laporan/order_pboot.php?kode='+kode+'&kodesp='+kodesp+'&namasp='+namasp+'&tgl='+tgl+'&kodecab='+kodecab;
	});
	
	$("#cetak_soppre").click(function(){
		var kode	= $("#op_kode").val();
		var kodesp	= $("#op_kodesp").val();
		var namasp	= $("#op_namasp").val();
		var tgl		= $("#op_tanggal").val();
		var kodecab = $("#op_kodecab").val();
		window.location.href='modul/laporan/order_pbpre.php?kode='+kode+'&kodesp='+kodesp+'&namasp='+namasp+'&tgl='+tgl+'&kodecab='+kodecab;
	});
	
	$("#cetak_soppsi").click(function(){
		var kode	= $("#op_kode").val();
		var kodesp	= $("#op_kodesp").val();
		var namasp	= $("#op_namasp").val();
		var tgl		= $("#op_tanggal").val();
		var kodecab = $("#op_kodecab").val();
		window.location.href='modul/laporan/order_pbpsi.php?kode='+kode+'&kodesp='+kodesp+'&namasp='+namasp+'&tgl='+tgl+'&kodecab='+kodecab;
	});
	
	$("#tutup_op").click(function(){
		document.location='?module=home';
	});
});
</script>

<style type="text/css">
#op_kode {
	background:#CCC;
}
#op_ctotal{
	font-size:20px;
	background:#CCC;
}
</style>

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

echo "<table width='100%'>
	<tr>		
		<input type='text' id='op_total' class='input_header' hidden />
		<td>Nomor SOPB : <input type='text' id='op_kode' size='12' readonly/>
			<img src='images/view.png' width='18' height='18' title='Cari SOP' id='cari_sop'>
		</td>
		<td>Tanggal : <input type='date' id='op_tanggal' size='10'></td>
		<td>Apotek : <select id='op_kodecab'>";
				$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>Supplier : <input type='text' id='op_namasp' size='25' class='input_header'>
			<input type='text' id='op_kodesp' size='4' class='input_header' readonly/>
		</td>
	</tr>
</table>
<hr>";

echo "<table>
		<tr>
			<th>Periode Penjualan</th>
			<th>Nama Barang</th>
			<th>Kode</th>
			<th>Zat Aktif</th>
			<th>Bentuk Sediaan</th>
			<th>Satuan</th>
			<th>Jml.Pesan</th>
			<th>Keterangan</th>
			<th></th>
		</tr>
		<tr>
			<input type='text' id='op_nourut' class='input_detail' hidden/>
			<td align='center'><input type='date' id='op_tgljualaw'> - <input type='date' id='op_tgljualak'></td>
			<td align='center'><input type='text' id='op_namast' class='input_detail' size='25'></td>
			<td align='center'><input type='text' id='op_kodest' class='input_detail' size='7' readonly/></td>
			<td align='center'><input type='text' id='op_zataktif' class='input_detail' size='20'></td>
			<td align='center'><input type='text' id='op_bentuk' class='input_detail' size='12'></td>
			<td align='center'><input type='text' id='op_satuan' class='input_detail' size='9'></td>
			<td align='center'><input type='text' id='op_jmlunit' class='input_detail' size='8'></td>
			<td align='center'><input type='text' id='op_keter' class='input_detail' size='25'></td>
			<td align='center'><button id='simpan'>Rekam</button></td>
		</tr>
		<tr>
		    <th>Terjual - Jml. Stok</th>
			<th>Harga Sat.</th>
			<th>Jml. Harga</th>
			<th>TOTAL</th>
		</tr>
		<tr>
		    <td align='center'><input type='text' id='op_jmljual' class='input_detail' size='10' readonly/> - <input type='text' id='op_jmlstok' class='input_detail' size='10' readonly/></td>
		    <td align='center'><input type='text' id='op_harga' class='input_detail' size='10'></td>
			<td align='center'><input type='text' id='op_jumlah' class='input_detail' size='11' readonly/></td>
		    <td align='center'><input style='color:maroon' align='right' type='text' id='op_ctotal' class='input_bayar' size='8' readonly /></td>
		</tr>
	</table>
	
	<table width='100%'>
		<tr>
			<td align='center'>
				<button id='cetak_sopreg'>Cetak SP Reguler</button>
				<button id='cetak_sopoot'>Cetak SP OOT</button>
				<button id='cetak_soppre'>Cetak SP Prekursor</button>
				<button id='cetak_soppsi'>Cetak SP Psikotropika</button>
			</td>
		</tr>
	</table>";

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

echo "<div id='form_cari_sp' title='Pencarian Data Supplier'>
		<table width='100%'>
			<tr>
				<td>Cari Supplier : <input type='text' id='op_carisp' size='40'></td>
			</tr>
		</table>
		<div id='info_supplier' align='center'></div>
	</div>";

echo "<div id='form_cari_brg' title='Pencarian Barang'>
		<table width='100%'>
			<tr>
				<td>Cari Barang : <input type='text' name='op_caribr' id='op_caribr' size='40'></td>
			</tr>
		</table>
		<div id='info_barang' align='center'></div>
	</div>";

echo "<table width='100%'>
	<tr>
		<td colspan='3' align='center'>
		<button name='mulai_op' id='mulai_op'>Buat SOP</button>
		<button name='tutup_op' id='tutup_op'>Tutup</button>
		</td>
	</tr>
</table>";

echo "<div id='form_cari_sopb' title='Pencarian data Order Pembelian'>
		<table width='100%'>
			<tr>
				<td>Cari Nomor SOP : <input type='text' name='op_carikode' id='op_carikode' size='15'></td>
			</tr>
		</table>

		<div id='info_opb' align='center'></div>
	</div>";
?>