<html>
<head>
<script language="javascript">
	$(document).ready(function(){ 
		$("#bomdate").datepicker({
			dateFormat : "yy-mm-dd",
			changeMonth : true,
			changeYear : true,
		});
	});
	$(document).ready(function(){
		var temp = {
			source: "transaksi/manual/json_hkv.php",					
			select: function(event, ui){
				$("#kv_itemid").val(ui.item.prod_id); 
				$("#kv_name").val(ui.item.prod_name); 
				$("#kv_satuan").val(ui.item.prod_sat); 
			},
			minLength: 1
		};
		$("#kv_itemcode").autocomplete(temp);		
	});
	$(document).ready(function(){
		var temp = {
			source: "transaksi/manual/json_prodinc.php",					
			select: function(event, ui){
				/*$("#prodcode").val(ui.item.prod_code); */
				$("#prodid").val(ui.item.prod_id); 
				$("#prodsat").val(ui.item.prod_sat); 				
			},
			minLength: 1
		};
		$("#prodcode").autocomplete(temp);		
	});
	function save_click(){
		if((form.kv_pic.value=="") ){
			alert("Silakan isi pic.");
			form.kv_pic.focus();
			return false;
		}else if((form.kv_desc.value=="") ){
			alert("Silakan isi keterangan");
			form.kv_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-Konversi-External2&Act=SAVE';
				form.method = 'post';
				form.submit();			
				return true;
			}
		}
	}
	function add_det_click(){
		var qtystock = $("#prodstock").val();
		var qtytrf	 = $("#kvd_qty").val();
		var qtys     = eval(qtystock);
		var qtyt     = eval(qtytrf);
		if(form.prodid.value==""){
			alert("Silakan isi nama barang.");
			form.prodcode.focus();
			return false;
		}else if((form.kvd_qty.value=="") ){
			alert("Silakan isi jumlah barang.");
			form.kvd_qty.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-Konversi-External2&Act_Det=ADD_DET';
			form.method = 'post';
			form.submit();			
			return true;
		}
	}
	function delete_detail_click(id){
		form.action ='?page=Proses-Konversi-External2&Act_Det=DELETE_DET&id_deth='+id;
		form.method = 'post';
		form.submit();			
		return true;
	}
	function close_click(){
		form.action ='?page=Proses-Konversi-External2&Act=CLOSE'
		form.method = 'post';
		form.submit();		
		return true;
	}
</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">KONVERSI BARANG PEB KE SUBKON</th>
	</tr>
	<tr>
	  <td width="15%">Kode Konversi</td>
	  <td width="35%">: <input type="hidden" name="kv_id" value="<?php echo $kv_id; ?>" size="50" />
	  					<input type="text" name="kv_no" value="<?php echo $kv_no; ?>" size="20" readonly style="background-color:#CCFFFF " />	  
	  					</td>
	  <td width="15%">&nbsp;</td>
	  <td width="35%">&nbsp;</td>
	</tr>
	<tr>
		<td>Tanggal</td>
		<td>: <input type="text" name="kv_date" value="<?php if(empty($kv_date)){$kv_date=$date;} echo $kv_date; ?>" size="10" id="bomdate" 
				  readonly="true" style="background-color:#CCFFFF " /> </td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
	</tr>
	<tr>
		<td>PIC</td>
		<td>: <input type="text" name="kv_pic" value="<?php echo $kv_pic; ?>" size="30"></td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
	</tr>
    <tr>
		<td>Keterangan</td>
		<td>: <textarea name="kv_desc" cols="30"><?php echo $kv_desc; ?></textarea></td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
	</tr>
	<tr>
		<td colspan="4">&nbsp;</td>
	</tr>
	<tr>
		<th colspan="4">KONVERSI DETAIL</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;">Jumlah</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">
						<!--<a href="javascript: openwindow1('1');"><img src="images/popup.png" height="15" width="15" title="Tambah" border="0" /></a>-->
					</td>
					<td><input type="text" name="prodsat" id="prodsat" style="width:50px; text-align:left; background-color:#CCFFFF" readonly></td>
					<td><input type="text" name="kvd_qty" id="kvd_qty" style="width:100px; text-align:right"></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($kv_id)){  ?>			
			<table width="100%">
				<tr>
					<th>No.</th>
					<th>Kode/Nama Barang</th>
					<th>Unit</th>
					<th>Jumlah</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 konversi_external_temp a
									   INNER JOIN product b ON b.id=a.kvd_prodid
									   WHERE a.kvd_inputby='$user_id' AND a.kvd_type='KONVERSI2'");
					while($dt=mysql_fetch_object($qd)){	   
				    $no++;
					$tqty     = $tqty + $dt->kvd_qty; 
				?> 
				  <tr>
					<td align="center"><?=$no;?></td>
					<td><?php echo "$dt->prod_code - $dt->prod_name";?></td>
					<td align="center"><?=$dt->prod_sat;?></td>
					<td align="right"><?=number_format($dt->kvd_qty,2,',','.');?></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>&nbsp;</td>
					<td>&nbsp;</td>
				</tr>
			</table>
			<?php }else{ ?>
			<table width="100%">
				<tr>
					<th>No.</th>
					<th>Kode/Nama Barang</th>
					<th>Unit</th>
					<th>Jumlah</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 konversi_external_detail a
									   INNER JOIN product b ON b.id=a.kvd_prodid
									   WHERE a.kvd_hid='$kv_id'");
					while($dt=mysql_fetch_object($qd)){	   
				    $no++;
					$tqty     = $tqty + $dt->kvd_qty; 
				?>
				  <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="70" readonly></td>
					<td><input type="text" value="<?=$dt->prod_sat;?>" size="10" readonly></td>
					<td align="left"><input type="text" name="kvdqty[<?=$dt->id;?>]" value="<?=$dt->kvd_qty;?>" style="text-align:right "></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>&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> 
