<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,
		});
	});
	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.wo_lotno.value=="") ){
			alert("Silakan isi nomor lot.");
			form.wo_lotno.focus();
			return false;
		}else if((form.wo_pic.value=="") ){
			alert("Silakan isi PIC.");
			form.wo_pic.focus();
			return false;
		}else if((form.wo_prodid.value=="BLANK") ){
			alert("Silakan pilih barang jadi.");
			form.wo_prodid.focus();
			return false;
		}else if((form.wo_qty.value=="") ){
			alert("Silakan isi jumlah barang jadi.");
			form.wo_qty.focus();
			return false;
		}else if((form.wo_desc.value=="") ){
			alert("Silakan isi keterangan.");
			form.wo_desc.focus();
			return false;
		}else{
			var a = confirm("Apakah anda yakin akan menyimpan data?");
			if (a == true){
				form.action ='?page=Proses-Work-Order2&Act=SAVE2';
				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-Work-Order2&Act_Det=ADD_DET';
			form.method = 'post';
			form.submit();			
			return true;
		}
	}
	function delete_detail_click(id){
		form.action ='?page=Proses-Work-Order2&Act_Det=DELETE_DET&id_deth='+id;
		form.method = 'post';
		form.submit();			
		return true;
	}
	function close_click(){
		form.action ='?page=Proses-Work-Order2&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"); 
	}
	function bom_click(){
		var wo_prodid =$("#prodidh").val(); 
		form.action ='?page=Proses-Work-Order2&Act=GET_BOM2&wo_prodid='+wo_prodid;
		form.method = 'post';
		form.submit();		
		return true;
	}
	function woqty_click(wo_prodid){
		form.action ='?page=Proses-Work-Order2&Act=GET_BOM2&wo_prodid='+wo_prodid;
		form.method = 'post';
		form.submit();		
		return true;
	}
	function itemh_click(){
		$(document).ready(function(){
			var temp = {
				source: "transaksi/manual/json_wo_hbom_scrap.php",					
				select: function(event, ui){
					$("#prodcodeh").val(ui.item.prod_code); 
					$("#prodidh").val(ui.item.prod_id); 
					$("#prodsath").val(ui.item.prod_sat);
				},
				minLength: 1
			};
			$("#prodcodeh").autocomplete(temp);		
		});
	}
</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">WORK ORDER BOM SCRAP</th> 
	</tr>
	<tr>
	  <td width="15%">No Work Order</td>
	  <td width="35%">: <input type="hidden" name="wo_id" value="<?php echo $wo_id; ?>" size="50" />
      					<input type="hidden" name="bom_id" value="<?php echo $bom_id; ?>" size="50" />
	                    <?php if(!empty($wo_id)){?>
						<input type="text" name="wo_no" value="<?php echo $wo_no; ?>" size="20" style="background-color:#CCFFFF " />
						<?php }else{ ?>
                        <input type="text" name="wo_no" value="" size="20" style="background-color:#CCFFFF " />
                        <?php } ?>
	  </td>
	  <td width="15%">&nbsp;</td>
	  <td width="35%">&nbsp;</td>
	</tr>
	<tr>
		<td>Tanggal WO</td>
		<td>: <input type="text" name="wo_date" value="<?php if(empty($wo_date)){$wo_date=date('Y-m-d');} echo $wo_date; ?>" size="10" id="spbdate" 
				  readonly="true" style="background-color:#CCFFFF " /> </td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
	</tr>
	<tr>
		<td>No. Lot</td>
		<td>: <input type="text" name="wo_lotno" value="<?php echo $wo_lotno; ?>" 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="wo_deptid">
				<?php
                  $dataSql = mysql_query("SELECT id,dept_name FROM departement WHERE id=4 ORDER BY id ASC");
                  while ($dataRow = mysql_fetch_array($dataSql)) {
                    if ($dataRow['id']== $wo_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>PIC</td>
		<td>: <input type="text" name="wo_pic" value="<?php echo $wo_pic; ?>" size="45">
		</td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
	</tr>
    <tr>
		<td>Kode/Nama Barang Scrap</td>
		<td colspan="3">:
        	<input type="hidden" name="wo_prodid" id="prodidh" value="<?=$wo_prodid;?>" style="width:300px;">
			<?php if(empty($wo_id)){?>
			<input type="text" name="wo_prodname" id="prodcodeh" value="<?=$wo_prodname;?>" style="width:500px; background-color:#CCFFFF" onKeyPress="itemh_click();" onBlur="bom_click();">
			<?php }else{?> 
			<input type="text" name="wo_prodname" id="prodcodeh" value="<?=$wo_prodname;?>" style="width:500px; background-color:#CCFFFF" readonly="true">
			<?php }?> 
        	<!--<?php if(empty($wo_id)){?> 
        	 <select name="wo_prodid" onChange="bom_click(this.value);">
        		<option value="0" selected="true">PILIH</option>
				<?php
                  $dataSql = mysql_query("SELECT a.bomh_prodid, b.prod_code, b.prod_name FROM bom a INNER JOIN product b ON b.id=a.bomh_prodid 
				  WHERE bomh_status=10 AND b.prod_cat=3 GROUP BY b.prod_code ORDER BY b.prod_code");
                  while ($dataRow = mysql_fetch_array($dataSql)) {
                    if ($dataRow['bomh_prodid']== $wo_prodid) {
                        $cek = " selected";
                    } else { $cek=""; }
                    echo "<option value='$dataRow[bomh_prodid]' $cek>$dataRow[prod_code] - $dataRow[prod_name]</option>";
                  }
                  $sqlData =""; 
                ?>
             </select>
             <?php }else{?> 
             <select name="wo_prodid" onChange="bom_click(this_value);" disabled>
        		<option value="0" selected="true">PILIH</option>
				<?php
                  $dataSql = mysql_query("SELECT a.bomh_prodid, b.prod_code, b.prod_name FROM bom a INNER JOIN product b ON b.id=a.bomh_prodid 
				  WHERE bomh_status=10 AND b.prod_cat=3 GROUP BY b.prod_code ORDER BY b.prod_code");
                  while ($dataRow = mysql_fetch_array($dataSql)) {
                    if ($dataRow['bomh_prodid']== $wo_prodid) {
                        $cek = " selected";
                    } else { $cek=""; }
                    echo "<option value='$dataRow[bomh_prodid]' $cek>$dataRow[prod_code] - $dataRow[prod_name]</option>";
                  }
                  $sqlData =""; 
                ?>
             </select>
             <?php } ?>--> 
		</td>
	</tr>
	<tr>
		<td>Jumlah</td>
		<td>: 
			<input type="text" name="wo_qty2" value="<?php echo $wo_qty2; ?>" size="10" style="text-align:right" onBlur="woqty_click(<?=$wo_prodid;?>);">&nbsp;
			Nilai Konversinya adalah : &nbsp;<input type="text" name="wo_qty" value="<?php echo $wo_qty; ?>" size="10" style="text-align:right; background-color:#CCFFFF" readonly>		
		</td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
	</tr>	
	<tr>
		<td>Keterangan</td>
		<td>: <textarea name="wo_desc" cols="35"><?php echo $wo_desc; ?></textarea>
		</td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
	</tr>
	<tr>
		<td colspan="4">&nbsp;</td>
	</tr>
	<tr>
		<th colspan="4">DETAIL BAHAN BAKU & PENOLONG</th>
	</tr>	
	<tr>
		<td colspan="4">&nbsp;</td>
	</tr>
	<tr>
		<td colspan="4">
			<table width="100%">
				<tr>
					<th style="width:20px;">No.</th>
					<th style="width:300px;">Nama Barang</th>					
					<th style="width:150px;">Stock</th>
                    <th style="width:150px;">Jumlah</th>
					<th style="width:150px;">Harga</th>
					<th style="width:150px;">Total</th>
					<th style="width:150px;">Unit</th>
                    <th style="width:150px;">Status</th>
				</tr>
				<?php
					$no=0; $tqty=0; $ttprice=0; 
					$qd = mysql_query("SELECT a.*,b.prod_code,b.prod_name,b.prod_sat, b.prod_cat
									   FROM bom_detail a
									   INNER JOIN product b ON b.id=a.bomd_prodid
									   WHERE a.bomd_hid='$bom_id' AND a.bomd_status=10"); 
					while($dd=mysql_fetch_array($qd)){	 
						$no++;						
						#Bahan Baku & penolong
						$stk = mysql_fetch_array(mysql_query("SELECT stock_prodid, SUM(stock_qty) qty, SUM(stock_price) price FROM stock WHERE stock_date='$fromdate' 
						AND stock_status=10 AND stock_loc=2 AND stock_prodid='$dd[bomd_prodid]' GROUP BY stock_prodid"));
						#Transfer BB
						$trf = mysql_fetch_array(mysql_query("SELECT a.trfd_prodid, SUM(a.trfd_qty) qty, SUM(trfd_price) price FROM transfer_detailexbc a 
						INNER JOIN transfer b ON b.id=a.trfd_hid  WHERE b.trf_spbdate BETWEEN '$fromdate' AND '$todate' AND b.trf_status=20 AND b.trf_category=1 
						AND a.trfd_prodid='$dd[bomd_prodid]' GROUP BY a.trfd_prodid"));
						#Return BB
						$rtn = mysql_fetch_array(mysql_query("SELECT a.trfd_prodid, SUM(a.trfd_qty) qty FROM transfer_detailexbc a INNER JOIN transfer b ON b.id=a.trfd_hid
						WHERE b.trf_spbdate BETWEEN '$fromdate' AND '$todate' AND b.trf_status=20 AND b.trf_category=3 AND a.trfd_prodid='$dd[bomd_prodid]' 
						GROUP BY a.trfd_prodid"));
						#Work Order BB
						$wo = mysql_fetch_array(mysql_query("SELECT a.wod_prodid, SUM(a.wod_qty) qty FROM workorder_detail a INNER JOIN workorder b ON b.id=a.wod_hid
							   WHERE b.wo_date BETWEEN '$fromdate' AND '$todate' AND b.wo_status=20 AND a.wod_prodid='$dd[bomd_prodid]' AND b.wo_level<>'Level FG' 
							   GROUP BY a.wod_prodid"));	
						#Barang Jadi & Scrap
						#Work Order BJ BOM
						$wprod = mysql_fetch_array(mysql_query("SELECT wo_prodid, SUM(wo_qty) qty, SUM(wo_price) price FROM workorder WHERE wo_date BETWEEN '$fromdate' 
						AND '$todate' AND wo_status=20 AND wo_prodid='$dd[bomd_prodid]' AND wo_level<>'Level FG' GROUP BY wo_prodid"));	
						#Work Order BJ Manual
						$mlprod = mysql_fetch_array(mysql_query("SELECT c.id, SUM(b.fgd_qty) qty FROM finishgoods a INNER JOIN finishgoods_detail b ON b.fgd_hid=a.id
						INNER JOIN product c ON c.id=b.fgd_prodid WHERE a.fgh_status=20 AND a.fgh_lpdate BETWEEN '$fromdate' AND '$todate' AND b.fgd_prodid='$dd[bomd_prodid]'
						GROUP BY c.id"));	
						#Transfer BJ
						$trfprod = mysql_fetch_array(mysql_query("SELECT a.trfd_prodid, SUM(a.trfd_qty) qty FROM transfer_detail a INNER JOIN transfer b ON b.id=a.trfd_hid
							   WHERE b.trf_spbdate BETWEEN '$fromdate' AND '$todate' AND b.trf_status=20 AND b.trf_category=4 AND a.trfd_prodid='$dd[bomd_prodid]' 
							   GROUP BY a.trfd_prodid"));
						#Return BJ
						$rtnprod = mysql_fetch_array(mysql_query("SELECT a.trfd_prodid, SUM(a.trfd_qty) qty FROM transfer_detail a INNER JOIN transfer b ON b.id=a.trfd_hid
							   WHERE b.trf_spbdate BETWEEN '$fromdate' AND '$todate' AND b.trf_status=20 AND b.trf_category=6 AND a.trfd_prodid='$dd[bomd_prodid]' 
							   GROUP BY a.trfd_prodid"));      	   
						#Laporan Scrap
						$scrp = mysql_fetch_array(mysql_query("SELECT c.id, SUM(b.scrd_qty) qty FROM scrap a INNER JOIN scrap_detail b ON b.scrd_hid=a.id
						INNER JOIN product c ON c.id=b.scrd_prodid WHERE a.scrh_status=20 AND a.scrh_lpdate BETWEEN '$fromdate' AND '$todate' AND b.scrd_prodid='$dd[bomd_prodid]'
						GROUP BY c.id"));
						#Konversi +
						$kvp = mysql_fetch_array(mysql_query("SELECT kv_prodid, SUM(kv_qty) qty FROM konversi WHERE kv_status=20 AND kv_date BETWEEN '$fromdate' AND '$todate' 
						AND kv_prodid='$dd[bomd_prodid]'	GROUP BY kv_prodid"));
						#Konversi -
						$kvm = mysql_fetch_array(mysql_query("SELECT a.kvd_prodid, SUM(a.kvd_qty) qty FROM konversi_detail a INNER JOIN konversi b ON b.id=a.kvd_hid 
						WHERE b.kv_status=20 AND b.kv_date BETWEEN '$fromdate' AND '$todate' 	AND a.kvd_prodid='$dd[bomd_prodid]'	GROUP BY a.kvd_prodid"));
						#Work Order BB-FG
						$wofg = mysql_fetch_array(mysql_query("SELECT a.wod_prodid, SUM(a.wod_qty) qty FROM workorder_detail a INNER JOIN workorder b ON b.id=a.wod_hid
							   WHERE b.wo_date BETWEEN '$fromdate' AND '$todate' AND b.wo_status=20 AND a.wod_prodid='$dd[bomd_prodid]' AND b.wo_level='Level FG' 
							   GROUP BY a.wod_prodid"));
						#Summary	   
						if($dd['prod_cat']==1 || $dd['prod_cat']==6 ){
							$stk    = $stk['qty'];
							$in     = $trf['qty']+$kvp['qty'];
							$out    = $rtn['qty']+$wo['qty']+$kvm['qty']+$wofg['qty'];	
							$stock  = ($stk+$in)-$out;
							$price  = get_unitprice_bb($start_date_m2year,$end_date,$dd['id']); #get_pricewip1($dd['id'],$fromdate,$todate,$inv);
							$tprice = $stock*$price;
						}elseif($dd['prod_cat']==3){
							$stk    = $stk['qty'];
							$in     = $trf['qty']+$wprod['qty']+$rtnprod['qty']+$mlprod['qty']+$kvp['qty'];
							$out    = $trfprod['qty']+$wo['qty']+$kvm['qty']+$wofg['qty'];	
							$stock  = ($stk+$in)-$out;
							$price  = get_unitprice_bj($start_date_m2year,$end_date,$dd['id']); #get_pricewip2($dd['id'],$fromdate,$todate,$inv);
							$tprice = $stock*$price;
						}elseif($dd['prod_cat']==5){
							$stk    = $stk['qty'];
							$in     = $trf['qty']+$wprod['qty']+$rtnprod['qty']+$kvp['qty'];
							$out    = $trfprod['qty']+$wo['qty']+$kvm['qty']+$wofg['qty'];	
							$stock  = ($stk+$in)-$out;
							$price  = get_unitprice_wip($start_date_m2year,$end_date,$dd['id']); #get_pricewip2($dd['id'],$fromdate,$todate,$inv);
							$tprice = $stock*$price;
						}else{
							$stk    = $stk['qty'];
							$in     = $scrp['qty']+$rtnprod['qty']+$kvp['qty'];
							$out    = $trfprod['qty']+$kvm['qty'];	
							$stock  = ($stk+$in)-$out;
							$price  = 0;
							$tprice = $stock*$price;
						}
						#$calc = $wo_qty*$dd['bomd_qty'];
						$calc = $wo_qty2*1;
						#display
						/*if($stock < 0){
							$stock=0;
						}else{
							$stock=$stock;
						}*/
						
						#remain
						$remain = number_format($stock,2,'.','')-number_format($calc,2,'.',''); #$stock-$calc; 	
						$tprice = $calc*$price;
						$ttprice  = $ttprice+$tprice;
						if($remain >= 0){
							$status = "OK";	
							$color  = "white";
						}else{
							$status = "KURANG";	
							$color  = "red";
						}
				?> 
				  <tr>
					<td style="width:20px;" align="center"><?=$no;?></td>
					<td style="width:300px;"><?php echo "$dd[prod_code] - $dd[prod_name]";?></td>					
					<td style="width:150px;" align="right"><?=number_format($stock,5,'.',',');?></td>
                    <td style="width:150px;" align="right"><?=number_format($calc,5,'.',',');?></td>
					<td style="width:150px;" align="right"><?=number_format($price,5,'.',',');?></td>
					<td style="width:150px;" align="right"><?=number_format($tprice,5,'.',',');?></td>
                    <td style="width:150px;" align="center">
						<?=$dd['prod_sat'];?>
						<input type="hidden" name="id_det[<?=$dd['id'];?>]" value="<?=$dd['id'];?>"> 
                        <input type="hidden" name="wod_prodid[<?=$dd['id'];?>]" value="<?=$dd['bomd_prodid'];?>">  
                        <input type="hidden" name="wod_stock[<?=$dd['id'];?>]" value="<?=$stock;?>"> 
                        <input type="hidden" name="wod_qty[<?=$dd['id'];?>]" value="<?=$calc;?>">   
                        <input type="hidden" name="wod_status[<?=$dd['id'];?>]" value="<?=$status;?>">   
                        <input type="hidden" name="prod_code[<?=$dd['id'];?>]" value="<?=$dd['prod_code'];?>">   
						<input type="hidden" name="wod_price_satuan[<?=$dd['id'];?>]" value="<?=$price;?>">             
                    </td>					
                    <td style="width:150px;" align="center" bgcolor="<?=$color;?>"><?=$status;?></td>
				</tr>
				<?php } ?>
				<input type="hidden" name="wo_price" value="<?=$ttprice;?>">
			</table>			
		</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> 