<html>
<head>
<script type="text/javascript">
	$(document).ready(function(){
		$("#sjdate").datepicker({
			dateFormat : "yy-mm-dd",
			changeMonth : true,
			changeYear : true,
		});
	});
	$(document).ready(function(){
		$("#invdate").datepicker({
			dateFormat : "yy-mm-dd",
			changeMonth : true,
			changeYear : true,
		});
	});
	$(document).ready(function(){
		$("#docdate").datepicker({
			dateFormat : "yy-mm-dd",
			changeMonth : true,
			changeYear : true,
		});
	});
	$(document).ready(function(){
		$("#ajudate").datepicker({
			dateFormat : "yy-mm-dd",
			changeMonth : true,
			changeYear : true,
		});
	});
	$(document).ready(function(){
		$("#npedate").datepicker({
			dateFormat : "yy-mm-dd",
			changeMonth : true,
			changeYear : true,
		});
	});
	function openwindow()
	{
		window.open("popup/popup_cust.php?","mywindow","menubar=1,scrollbars=1,resizable=1,width=600,height=250");
	}
	function openwindow1()
	{
		window.open("popup/popup_barang.php?","mywindow","menubar=1,scrollbars=1,resizable=1,width=600,height=250");
	}
	function pilih(id)
	{
		var y  = document.getElementById("outh_purpid");		
		$.ajax({type: "POST",
			url: "transaksi/manual/load_purpose.php", 
			data: "docid="+id,
			success: function(result){			
			y.innerHTML="<option value='0'>PILIH</option>";
			arr_data = result.split("XX");
			if(id==7){ /*BC 2.5 */
				var option = document.createElement("option");
					option.text = arr_data[1];
					option.value = arr_data[0];
					y.add(option);					
			}else if(id==8){ /*BC 2.6.1 */
				var option = document.createElement("option");
					option.text = arr_data[1];
					option.value = arr_data[0];
					y.add(option);
				var option = document.createElement("option");
					option.text = arr_data[3];
					option.value = arr_data[2];
					y.add(option);						
			}else if(id==9){ /*BC 2.7 */
				var option = document.createElement("option");
					option.text = arr_data[1];
					option.value = arr_data[0];
					y.add(option);					
			}else if(id==10){ /*BC 3.0 */
				var option = document.createElement("option");
					option.text = arr_data[1];
					option.value = arr_data[0];
					y.add(option);
				var option = document.createElement("option");
					option.text = arr_data[3];
					option.value = arr_data[2];
					y.add(option);
				var option = document.createElement("option");
					option.text = arr_data[5];
					option.value = arr_data[4];
					y.add(option);					
			}else if(id==12){ /*BC 4.1 */
				var option = document.createElement("option");
					option.text = arr_data[1];
					option.value = arr_data[0];
					y.add(option);
				var option = document.createElement("option");
					option.text = arr_data[3];
					option.value = arr_data[2];
					y.add(option);						
			}else if(id==13){ /*OTHERS */
				var option = document.createElement("option");
					option.text = arr_data[1];
					option.value = arr_data[0];
					y.add(option);
				var option = document.createElement("option");
					option.text = arr_data[3];
					option.value = arr_data[2];
					y.add(option);						
			}
			
		}});
	}
	$(document).ready(function(){
		var out = {
			source: "transaksi/manual/json_prodout.php",					
			select: function(event, ui){
				$("#prodcode").val(ui.item.prod_code); 
				$("#prodid").val(ui.item.prod_id); 
				$("#prodsat").val(ui.item.prod_sat); 
				$("#prodstock").val(ui.item.prod_stock);
			},
			minLength: 1
		};
		$("#prodcode").autocomplete(out);		
	});	
	function currency_click(){
		var fdate    = $("#docdate").val();
		var currency = $("#outdcurrency").val();
		var qty		 = $("#outdqty").val();
		var price	 = $("#outdprice").val();
		var total 	 = eval(qty)*eval(price);
		$("#outdamount").val(total);		
	 }
	 function save_click(){
		if(form.cust_name.value==""){
			alert("Silakan isi nama penerima.");
			form.cust_name.focus();
			return false;
		}else if((form.outh_invno.value=="") ){
			alert("Silakan isi nomor invoice.");
			form.outh_invno.focus();
			return false;
		}else if((form.outh_dono.value=="") ){
			alert("Silakan isi nomor DO.");
			form.outh_dono.focus();
			return false;
		}else if((form.outh_docid.value=="BLANK") ){
			alert("Silakan pilih jenis dokumen.");
			form.outh_docid.focus();
			return false;
		}else if((form.outh_purpid.value=="BLANK") ){
			alert("Silakan pilih tujuan dokumen.");
			form.outh_purpid.focus();
			return false;
		}else if((form.outh_docno.value=="") ){
			alert("Silakan isi nomor dokumen.");
			form.outh_docno.focus();
			return false;
		}else if((form.no_aju.value=="") ){
			alert("Silakan isi nomor aju.");
			form.no_aju.focus();
			return false;
		}else if((form.no_npe.value=="") ){
			alert("Silakan isi nomor dokumen.");
			form.no_npe.focus();
			return false;
		}else if((form.tqty.value==0) ){
			alert("Silakan isi detail barang.");
			form.tqty.focus();
			return false;
		}else{
			var a = confirm("Apakah anda yakin akan menyimpan data?");
			if (a == true){
				form.action ='?page=Proses-Pengeluaran&Act=SAVE';
				form.method = 'post';
				form.submit();			
				return true;
			}
		}
	}
	function add_det_click(){
	    var qtystock = $("#prodstock").val();
		var qtytrf	 = $("#outdqty").val();
		var qtys     = eval(qtystock);
		var qtyt     = eval(qtytrf);
		if(form.prodid.value==""){
			alert("Silakan isi nama barang.");
			form.prodname.focus();
			return false;
		}else if((form.outdqty.value=="") ){
			alert("Silakan isi jumlah barang.");
			form.outdqty.focus();
			return false; 
		}else if((form.outdprice.value=="") ){
			alert("Silakan isi harga barang.");
			form.outdprice.focus();
			return false;
		}else if((form.outdcurrency.value=="") ){
			alert("Silakan pilih mata uang.");
			form.outdcurrency.focus();
			return false;
		}else if((form.outdamount.value=="") ){
			alert("Silakan nilai barang.");
			form.outdamount.focus();
			return false;
		}else if((form.outdweight.value=="") ){
			alert("Silakan isi berat barang.");
			form.outdweight.focus();
			return false;
		}else if(qtys < qtyt ){
			alert("Jumlah stock lebih kecil dari jumlah yang dikeluarkan.");
			form.trfdqty.focus();
			return false; 
		}else{
			form.action ='?page=Proses-Pengeluaran&Act_Det=ADD_DET';
			form.method = 'post';
			form.submit();			
			return true;
		}
	}
	function delete_detail_click(id){
		form.action ='?page=Proses-Pengeluaran&Act_Det=DELETE_DET&id_deth='+id;
		form.method = 'post';
		form.submit();			
		return true;
	}
	function close_click(){
		form.action ='?page=Proses-Pengeluaran&Act=CLOSE'
		form.method = 'post';
		form.submit();		
		return true;
	}
	function popup_item(){
		window.open("popup/popup_item.php?","mywindow","menubar=1,scrollbars=1,resizable=1,width=650,height=250");
	}
</script>
</head>
<form action="?page=Proses-Pengeluaran" method="post" name="form" target="_self" enctype="multipart/form-data">
<table width="100%" cellpadding="2" cellspacing="1" class="table-list" style="margin-top:0px;">
	<tr>
	  <th colspan="4">HEADER PENGELUARAN</th>
	</tr>
	<tr>
	  <td width="15%">No Keluar</td>
	  <td width="35%">: <input type="hidden" name="outh_id" value="<?php echo $outh_id; ?>" size="50" />
	                       <input type="text" name="outh_sjno" value="<?php echo $outh_sjno; ?>" size="20" readonly="true" style="background-color:#CCFFFF " /></td>
	  <td width="15%">No DO</td>
	  <td width="35%">: <input type="text" name="outh_dono" value="<?php echo $outh_dono; ?>" size="20"></td>
	</tr>
	<tr>
		<td>Tanggal Keluar</td>
		<td>: <input type="text" name="outh_sjdate" value="<?php if(empty($outh_sjdate)){$outh_sjdate=date('Y-m-d');} echo $outh_sjdate; ?>" size="12" id="sjdate" 
				  readonly="true" style="background-color:#CCFFFF " /> </td>
		<td>Jenis Dokumen</td>
		<td>: <select name="outh_docid" onChange="pilih(this.value);">
				  <option value="BLANK">PILIH</option> 
					<?php
					  $dataSql = mysql_query("SELECT id,doc_type FROM document_type WHERE doc_status=2 ORDER BY  id ASC");
					  while ($dataRow = mysql_fetch_array($dataSql)) {
						if ($dataRow['id']== $outh_docid) {
							$cek = " selected";
						} else { $cek=""; }
						echo "<option value='$dataRow[id]' $cek>$dataRow[doc_type]</option>";
					  }
					  $sqlData ="";
					?>
				 </select>
		</td>
	</tr>
	<tr>
		<td>Penerima</td>
		<td>: <input type="hidden" name="cust_id" value="<?php echo $cust_id; ?>" size="10" />
				 <input type="text" name="cust_name" value="<?php echo $cust_name; ?>" size="50" readonly="true" style="background-color:#CCFFFF " />		
				 <a href="javascript: openwindow('');"><img src="images/popup.png" height="15" width="15" title="Tambah" border="0" />
			 </td>
		<td>Tujuan</td>
		<td>: 
			<?php if((!empty($outh_id)) || (!empty($outh_docid)) ){ ?>
			<select name="outh_purpid" id="outh_purpid">
			  <option value="BLANK">PILIH</option> 
				<?php
				  $dataSql = mysql_query("SELECT id, purp_desc FROM purpose WHERE purp_docid='$outh_docid' ORDER BY id ASC");
				  while ($dataRow = mysql_fetch_array($dataSql)) {
					if ($dataRow['id']== $outh_purpid) {
						$cek = " selected";
					} else { $cek=""; }
					echo "<option value='$dataRow[id]' $cek>$dataRow[purp_desc]</option>";
				  }
				  $sqlData =""; 
				?>
			 </select> 
			 <?php } else { ?> 
			<select name="outh_purpid" id="outh_purpid">
				<option value="BLANK">PILIH</option>
			</select>
			<?php } ?>
		</td>
	</tr>
	<tr>
		<td>No Invoice</td>
		<td>: <input type="text" name="outh_invno" value="<?=$outh_invno;?>"></td>
		<td>No Dokumen</td>
		<td>: <input type="text" name="outh_docno" size="6" maxlength="6" value="<?=$outh_docno;?>"></td>
	</tr>
	<tr>
		<td>Tanggal Invoice</td>
		<td>: <input type="text" name="outh_invdate" value="<?php if(empty($outh_invdate)){$outh_invdate=date('Y-m-d');} echo $outh_invdate; ?>" size="12" id="invdate" 
				  readonly="true" style="background-color:#CCFFFF " />
		</td>
		<td>Tanggal Dokumen</td>
		<td>: <input type="text" name="outh_docdate" value="<?php if(empty($outh_docdate)){$outh_docdate=date('Y-m-d');} echo $outh_docdate; ?>" size="12" id="docdate" 
				  readonly="true" style="background-color:#CCFFFF " /></td>
	</tr>
	<tr>
		<td>No Aju</td>
		<td>: <input type="text" name="no_aju" size="6" maxlength="6" value="<?=$no_aju;?>"></td>
		<td>Tanggal Aju</td>
		<td>: <input type="text" name="tgl_aju" value="<?php if(empty($tgl_aju)){$tgl_aju=date('Y-m-d');} echo $tgl_aju; ?>" size="12" id="ajudate" 
				  readonly="true" style="background-color:#CCFFFF " /></td>
	</tr>
	<tr>
		<td>No NPE</td>
		<td>: <input type="text" name="no_npe" size="6" maxlength="6" value="<?=$no_npe;?>"></td>
		<td>Tanggal NPE</td>
		<td>: <input type="text" name="tgl_npe" value="<?php if(empty($tgl_npe)){$tgl_npe=date('Y-m-d');} echo $tgl_npe; ?>" size="12" id="npedate" 
				  readonly="true" style="background-color:#CCFFFF " /></td>
	</tr>
	<tr>
		<td colspan="4">&nbsp;</td>
	</tr>
	<tr>
		<th colspan="4">DETAIL PENGELUARAN</th>
	</tr>
	<tr>
		<td colspan="4">
			<table width="100%">
				<tr>
					<td style="width:250px;">Kode Barang</td>
					<td style="width:50px;">Satuan</td>
					<td style="width:50px;">Stok</td>
					<td style="width:50px;">Jumlah</td>
					<td style="width:180px;">Harga</td>
					<td style="width:120px;">Nilai</td>
					<td style="width:150px;">Berat</td>
					<td style="width:150px;">Action</td>
				</tr>
				<tr>
					<td><input type="hidden" name="prodid" id="prodid" style="width:300px;">
						<input type="text" name="prodcode" id="prodcode" style="width:235px; background-color:#CCFFFF" >
						<img src="images/popup.png" height="15" width="15" title="Popup Item Master" style="cursor:pointer" border="0" onClick="popup_item();" />
					</td>
					<td><input type="text" name="prodsat" id="prodsat" style="width:50px; text-align:left; background-color:#CCFFFF" readonly="true"></td>
					<td><input type="text" name="prodstock" id="prodstock" style="width:50px; text-align:right; background-color:#CCFFFF" readonly="true"></td>
					<td><input type="text" id="outdqty" name="outdqty" style="width:50px; text-align:right"></td>
					<td>
						<input type="text" id="outdprice" name="outdprice" style="width:100px; text-align:right">
						<select name="outdcurrency" id="outdcurrency" onChange="currency_click();">
							<option value="BLANK" selected>PILIH</option>
							<?php
							  $dataSql = mysql_query("SELECT currency FROM currency ORDER BY id ASC");
							  while ($dataRow = mysql_fetch_array($dataSql)) {
								echo "<option value='$dataRow[currency]' $cek>$dataRow[currency]</option>";
							  }
							  $sqlData =""; 
							?>
						 </select>
					</td>
					<td>
						<input type="text" id="outdamount" name="outdamount" style="width:100px; text-align:right;">&nbsp;
					</td>
					<td><input type="text" name="outdweight" style="width:100px; text-align:right ">&nbsp;Kg</td>
					<td><input type="button" name="Add_Detail" value="Tambah" style="cursor:pointer" onClick="add_det_click();"></td>
				</tr>
			</table>
		</td>
	</tr>
	<tr>
		<td colspan="4">&nbsp;</td>
	</tr>
	<tr>
		<td colspan="4">
		  <?php if(empty($outh_id)){  ?>			
			<table width="100%">
				<tr>
					<td style="width:20px;">No.</td>
					<td style="width:300px;">Nama Barang</td>
					<td style="width:150px;">Unit</td>
					<td style="width:150px;">Jumlah</td>
					<td style="width:150px;">Nilai</td>
					<td style="width:150px;">Berat</td>
					<td style="width:170px;">Action</td>
				</tr>
				<?php
				    $no=0; $tqty=0; $tprice=0; $tweight=0; 
					$qd = mysql_query("SELECT a.id,a.outd_qty,a.outd_price,a.outd_weight,a.outd_wunit,a.outd_currency,
									   b.prod_code,b.prod_name,b.prod_sat
									   FROM outgoing_temp a
									   INNER JOIN product b ON b.id=a.outd_prodid
									   WHERE a.outd_inputby='$user_id'");
					while($dt=mysql_fetch_object($qd)){	   
				    $no++;
					$tqty     = $tqty + $dt->outd_qty; 
					$tprice   = $tprice + $dt->outd_price;
					$tweight  = $tweight + $dt->outd_weight;
				?> 
				  <tr>
					<td style="width:20px;" align="center"><?=$no;?></td>
					<td style="width:300px;"><?php echo "$dt->prod_code - $dt->prod_name";?></td>
					<td style="width:150px;" align="center"><?=$dt->prod_sat;?></td>
					<td style="width:150px;" align="right"><?=number_format($dt->outd_qty,2,',','.');?></td>
					<td style="width:150px;" align="right"><?=number_format($dt->outd_price,2,',','.');?>&nbsp;<?=$dt->outd_currency;?></td>
					<td style="width:150px;" align="right"><?=number_format($dt->outd_weight,2,',','.');?>&nbsp;<?=$dt->outd_wunit;?></td>
					<td style="width:170px;" align="center">
						<img src="images/btn_delete.png" width="20" height="20"  border="0"  title="Hapus" style="cursor:pointer" onClick="delete_detail_click('<?=$dt->id;?>');"/>
					</td>
				</tr>
				<?php } ?>
				<tr>
					<td colspan="3" align="right">Total</td>
					<td align="right"><?=number_format($tqty,2,',','.');?><input type="hidden" name="tqty" value="<?=$tqty;?>"></td>
					<td align="right"><?=number_format($tprice,2,',','.');?></td>
					<td align="right"><?=number_format($tweight,2,',','.');?></td>
					<td>&nbsp;</td>
				</tr>
			</table>
			<?php }else{ ?>
			<table width="100%">
				<tr>
					<td align="center">No.</td>
					<td align="center">Nama Barang</td>
					<td align="center">Unit</td>
					<td align="center">Jumlah</td>
					<td align="center">Nilai</td>
					<td align="center">Berat</td>
					<td align="center">Action</td>
				</tr>
				<?php
				    $no=0; $tqty=0; $tprice=0; $tweight=0; 
					$qd = mysql_query("SELECT a.id,a.outd_qty,a.outd_price,a.outd_weight,a.outd_wunit,a.outd_currency,
									   b.prod_code,b.prod_name,b.prod_sat
									   FROM outgoing_detail a
									   INNER JOIN product b ON b.id=a.outd_prodid
						   			   WHERE a.outd_hid='$outh_id'");
					while($dt=mysql_fetch_object($qd)){	   
				    $no++;
					$tqty     = $tqty + $dt->outd_qty; 
					$tprice   = $tprice + $dt->outd_price;
					$tweight  = $tweight + $dt->outd_weight;
				?> 
				  <tr>
					<td align="center"><?=$no;?><input type="hidden" name="id_det[<?=$dt->id;?>]" value="<?=$dt->id;?>"></td>
					<td><input type="text" value="<?php echo "$dt->prod_code - $dt->prod_name";?>" size="60" readonly="true"></td>
					<td align="center"><input type="text" value="<?=$dt->prod_sat;?>" size="8" readonly="true" style="text-align:center"></td>
					<td align="right"><input type="text" name="outd_qty[<?=$dt->id;?>]" value="<?=$dt->outd_qty;?>" size="15" style="text-align:right"></td>
					<td align="right"><input type="text" name="outd_price[<?=$dt->id;?>]" value="<?=$dt->outd_price;?>" size="15" style="text-align:right">&nbsp;<?=$dt->outd_currency;?></td>
					<td align="right"><input type="text" name="outd_weight[<?=$dt->id;?>]" value="<?=$dt->outd_weight;?>" size="15" style="text-align:right">&nbsp;<?=$dt->outd_wunit;?></td>
					<td align="center">
						<img src="images/btn_delete.png" width="20" height="20"  border="0"  title="Hapus" style="cursor:pointer" onClick="delete_detail_click('<?=$dt->id;?>');"/>
					</td>
				</tr>
				<?php } ?>
				<tr>
					<td colspan="3" align="right">Total</td>
					<td align="right"><?=number_format($tqty,2,',','.');?><input type="hidden" name="tqty" value="<?=$tqty;?>"></td>
					<td align="right"><?=number_format($tprice,2,',','.');?></td>
					<td align="right"><?=number_format($tweight,2,',','.');?></td>
					<td>&nbsp;</td>
				</tr>
			</table>
			<?php } ?>
		</td>
	</tr>
	<tr>
		<td colspan="4">&nbsp;</td>
	</tr>
	<tr>
		<td colspan="4" align="center">
			<input type="button" name="btnSave" value="SIMPAN" style="cursor:pointer;" onClick="save_click();">
			<input type="button" value="BATAL" name="batal" style="cursor:pointer"  onClick="close_click();" />
		</td>
    </tr>
</table>
</form>
</html> 
