<html>
<head>
<script language="javascript">
	$(document).ready(function(){ 
		$("#spbdate").datepicker({
			dateFormat : "yy-mm-dd",
			changeMonth : true,
			changeYear : true,
		});
	});
	$(document).ready(function(){
		$("#reqdate").datepicker({
			dateFormat : "yy-mm-dd",
			changeMonth : true,
			changeYear : true,
		});
	});
	$(document).ready(function(){
		$("#docdate").datepicker({
			dateFormat : "yy-mm-dd",
			changeMonth : true,
			changeYear : true,
		});
	});
	function openwindow1(id)
	{
		window.open("popup/popup_barang.php?loc="+id,"mywindow","menubar=1,scrollbars=1,resizable=1,width=650,height=300");
	}
	$(document).ready(function(){
		var temp = {
			source: "transaksi/manual/json_tprodout.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(temp);		
	});
	function save_click(){
		if((form.trf_typeid.value=="0") ){
			alert("Silakan pilih jenis barang.");
			form.trf_typeid.focus();
			return false;
		}else if((form.trf_pic.value=="") ){
			alert("Silakan isi PIC.");
			form.trf_pic.focus();
			return false;
		}else if((form.trf_desc.value=="") ){
			alert("Silakan isi keterangan.");
			form.trf_desc.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-Prod-Inc&Act=SAVE';
				form.method = 'post';
				form.submit();			
				return true;
			}
		}
	}
	function add_det_click(){
		var qtystock = $("#prodstock").val();
		var qtytrf	 = $("#trfdqty").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.prodstock.value=="") ){
			alert("Stock tidak boleh kosong.");
			form.prodstock.focus();
			return false;*/
		}else if((form.trfdqty.value=="") ){
			alert("Silakan isi jumlah barang.");
			form.trfdqty.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-Prod-Inc&Act_Det=ADD_DET';
			form.method = 'post';
			form.submit();			
			return true;
		}
	}
	function delete_detail_click(hid,id){
		form.action ='?page=Proses-Prod-Inc&Act_Det=DELETE_DET&trf_id='+hid+'&id_deth='+id;
		form.method = 'post';
		form.submit();			
		return true;
	}
	function close_click(){
		form.action ='?page=Proses-Prod-Inc&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="#" 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 PENGEMBALIAN BAHAN BAKU</th>
	</tr>
	<tr>
	  <td width="15%">No SPB</td>
	  <td width="35%">: <input type="hidden" name="trf_id" value="<?php echo $trf_id; ?>" size="50" />
	                       <input type="text" name="trf_spbno" value="<?php echo $trf_spbno; ?>" size="45" readonly="true" style="background-color:#CCFFFF " /></td>
	  <td width="15%">&nbsp;</td>
	  <td width="35%">&nbsp;</td>
	</tr>
	<tr>
		<td>Tanggal SPB</td>
		<td>: <input type="text" name="trf_spbdate" value="<?php if(empty($trf_spbdate)){$trf_spbdate=date('Y-m-d');} echo $trf_spbdate; ?>" size="10" id="spbdate" 
				  readonly="true" style="background-color:#CCFFFF " /> </td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
	</tr>
	<!--<tr>
		<td>No. Permintaan</td>
		<td>: <input type="text" name="trf_reqno" value="<?php echo $trf_reqno; ?>" size="45"></td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
	</tr>
	<tr>
		<td>Tanggal Permintaan</td>
		<td>: <input type="text" name="trf_reqdate" value="<?php if(empty($trf_reqdate)){$trf_reqdate=date('Y-m-d');} echo $trf_reqdate; ?>" size="10" id="reqdate" 
				  readonly="true" style="background-color:#CCFFFF " /> 
		</td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
	</tr>-->
	<tr>
		<td>Departemen</td>
		<td>: <select name="trf_deptid">
							<?php
							  $dataSql = mysql_query("SELECT id,dept_name FROM departement ORDER BY id ASC");
							  while ($dataRow = mysql_fetch_array($dataSql)) {
								if ($dataRow['id']== $trf_deptid) {
									$cek = " selected";
								} else { $cek=""; }
								echo "<option value='$dataRow[id]' $cek>$dataRow[dept_name]</option>";
							  }
							  $sqlData =""; 
							?>
						 </select>
		</td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
	</tr>
	<tr>
		<td>Jenis Barang</td>
		<td>: <select name="trf_typeid">
				<?php if($trf_typeid==10){ ?>
				<option value="0">Pilih</option>
				<option value="10" selected="selected">Barang OK</option>
				<option value="20">Barang NG</option>	
				<?php }else if($trf_typeid==20){ ?>
				<option value="0">Pilih</option>
				<option value="10">Barang OK</option>
				<option value="20" selected="selected">Barang NG</option>
				<?php }else{ ?>
				<option value="0" selected="selected">Pilih</option>
				<option value="10">Barang OK</option>
				<option value="20">Barang NG</option>
				<?php } ?>		
			  </select>
		</td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
	</tr>
	<tr>
		<td>PIC</td>
		<td>: <input type="text" name="trf_pic" value="<?php echo $trf_pic; ?>" size="45">
		</td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
	</tr>
	<tr>
		<td>Keterangan</td>
		<td>: <textarea name="trf_desc" cols="35"><?php echo $trf_desc; ?></textarea>
		</td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
	</tr>
	<tr>
		<td colspan="4">&nbsp;</td>
	</tr>
	<tr>
		<th colspan="4">DETAIL PENGEMBALIAN BARANG BAHAN BAKU</th>
	</tr>
	<tr>
		<td colspan="4">
			<table width="100%">
				<tr>
					<td style="width:250px;">Kode/Nama Barang</td>
					<td style="width:50px;">Satuan</td>
					<!--<td style="width:100px;">Stok</td>-->
					<td style="width:100px;">Jumlah</td>
					<td style="width:100px;">Jenis</td>
					<td style="width:100px;">Nomor</td>
					<td style="width:100px;">Tanggal</td>
					<td style="width:100px;">Gudang</td>
					<td style="width:150px;">Keterangan</td>
					<td style="width:170px;">Action</td>
				</tr>
				<tr>
					<td><input type="hidden" name="prodid" id="prodid" style="width:300px;">
					    <input type="text" name="prodcode" id="prodcode" style="width:250px; 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">
						<input type="hidden" name="prodstock" id="prodstock" style="width:100px; text-align:right; background-color:#CCFFFF" readonly="true">
					</td>
					<!--<td><input type="text" name="prodstock" id="prodstock" style="width:100px; text-align:right; background-color:#CCFFFF" readonly="true"></td>-->
					<td><input type="text" name="trfdqty" id="trfdqty" style="width:100px; text-align:right"></td>
					<td>
						<select name="trfddoctype">
							<?php
							  $dataSql = mysql_query("SELECT doc_type FROM document_type WHERE doc_status=1 ORDER BY  id ASC");
							  while ($dataRow = mysql_fetch_array($dataSql)) {								
								echo "<option value='$dataRow[doc_type]' $cek>$dataRow[doc_type]</option>";
							  }	
							  $sqlData ="";
							?>
						 </select>
					</td>
					<td><input type="text" name="trfddocno" maxlength="6" style="width:100px; text-align:center"></td>
					<td>
						<input type="text" name="trfddocdate" value="<?php if(empty($trfddocdate)){$trfddocdate=date('Y-m-d');} echo $trfddocdate; ?>" size="10" id="docdate" 
				  readonly="true" style="background-color:#CCFFFF " />
					</td>
					<td align="right">
						<select name="trfdwhid">
                           <option value="0" selected="selected">PILIH</option> 
                            <?php
                              $dataSql = mysql_query("SELECT id, mw_name FROM master_warehouse WHERE mw_status=10 ORDER BY id ASC");
                              while ($dataRow = mysql_fetch_array($dataSql)){
                                echo "<option value='$dataRow[id]'>$dataRow[mw_name]</option>";
                              }
                              $sqlData ="";
                            ?>
                        </select>	
					</td>
					<td><input type="text" name="trfddesc" style="width:100px; text-align:left"></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($trf_id)){  ?>			
			<table width="100%">
				<tr>
					<th style="width:20px;">No.</th>
					<th style="width:300px;">Nama Barang</th>
					<th style="width:150px;">Unit</th>
					<th style="width:150px;">Jumlah</th>
					<th style="width:150px;">Jenis</th>
					<th style="width:150px;">Nomor</th>
					<th style="width:150px;">Tanggal</th>
					<th style="width:150px;">gudang</th>
					<th style="width:150px;">Ket</th>
					<th style="width:170px;">Action</th>
				</tr>
				<?php
					$no=0; $tqty=0;
					$qd = mysql_query("SELECT a.*,b.prod_code,b.prod_name,b.prod_sat, c.mw_name
									   FROM transfer_temp a
									   INNER JOIN product b ON b.id=a.trfd_prodid
									   LEFT JOIN master_warehouse c ON c.id=a.trfd_whid
									   WHERE a.trfd_inputby='$user_id' AND a.trfd_category=3 ORDER BY a.id ASC");
					while($dt=mysql_fetch_object($qd)){	   
				    $no++;
					$tqty     = $tqty + $dt->trfd_qty; 
				?> 
				  <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->trfd_qty,2,',','.');?></td>
					<td style="width:150px;" align="center"><?=$dt->trfd_doctype;?></td>
					<td style="width:150px;" align="center"><?=$dt->trfd_docno;?></td>
					<td style="width:150px;" align="center"><?=$dt->trfd_docdate;?></td>
					<td style="width:150px;" align="center"><?=$dt->mw_name;?></td>
					<td style="width:150px;" align="left"><?=$dt->trfd_desc;?></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('<?=$trf_id;?>','<?=$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>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
				</tr>
			</table>
			<?php }else{ ?>
			<table width="100%">
				<tr>
					<th>No.</th>
					<th>Nama Barang</th>
					<th>Unit</th>
					<th>Jumlah</th>
					<th>jenis</th>
					<th>Nomor</th>
					<th>Tanggal</th>
					<th>Gudang</th>
					<th>Keterangan</th>
					<th>Action</th>
				</tr>
				<?php
				    $no=0; $tqty=0;
					$qd = mysql_query("SELECT a.*,b.prod_code,b.prod_name,b.prod_sat
									   FROM transfer_detailexbc a
									   INNER JOIN product b ON b.id=a.trfd_prodid
									   WHERE a.trfd_hid='$trf_id'");
					while($dt=mysql_fetch_object($qd)){	   
				    $no++;
					$tqty     = $tqty + $dt->trfd_qty; 
				?> 
				  <tr>
					<td align="center"><?=$no;?>
						<input type="hidden" name="id_det[<?=$dt->id;?>]" value="<?=$dt->id;?>">
						<input type="hidden" name="trfd_prodid[<?=$dt->id;?>]" value="<?=$dt->trfd_prodid;?>">
					</td>
					<td><input type="text" value="<?php echo "$dt->prod_code - $dt->prod_name";?>" size="30" readonly="true" style="background-color:#CCFFFF "></td>
					<td><input type="text" value="<?=$dt->prod_sat;?>" size="5" readonly="true" style="background-color:#CCFFFF "></td>
					<td align="left"><input type="text" name="trfd_qty[<?=$dt->id;?>]" value="<?=$dt->trfd_qty;?>" size="7" style="text-align:right "></td>
					<td align="left">
						<select name="trfd_doctype[<?=$dt->id;?>]" >
							<?php
							  $dataSql = mysql_query("SELECT doc_type FROM document_type WHERE doc_status=1 ORDER BY  id ASC");
							  while ($dataRow = mysql_fetch_array($dataSql)) {
								if ($dataRow['doc_type']== $dt->trfd_doctype) {
									$cek = " selected";
								} else { $cek=""; }
								echo "<option value='$dataRow[doc_type]' $cek>$dataRow[doc_type]</option>";
							  }
							  $sqlData ="";
							?>
						 </select>
					</td>
					<td align="left"><input type="text" name="trfd_docno[<?=$dt->id;?>]" value="<?=$dt->trfd_docno;?>" size="5" maxlength="6" style="text-align:center "></td>
					<td align="left">
						<input type="text" name="trfd_docdate[<?=$dt->id;?>]" value="<?=$dt->trfd_docdate;?>" size="7" id="docdate" style="text-align:center">
					</td>
					<td align="left">
						<select name="trfd_whid[<?=$dt->id;?>]" >
							<?php
							  $dataSql = mysql_query("SELECT id, mw_name FROM master_warehouse WHERE mw_status=10 ORDER BY id ASC");
							  while ($dataRow = mysql_fetch_array($dataSql)) {
								if ($dataRow['id']== $dt->trfd_whid) {
									$cek = " selected";
								} else { $cek=""; }
								echo "<option value='$dataRow[id]' $cek>$dataRow[mw_name]</option>";
							  }
							  $sqlData ="";
							?>
						 </select>
					</td>
					<td align="left"><input type="text" name="trfd_desc[<?=$dt->id;?>]" value="<?=$dt->trfd_desc;?>" size="30"></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('<?=$trf_id;?>','<?=$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>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</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> 