<html>
<head>
<script type="text/javascript">
	function setTrColor(obj, color)
	{
		obj.style.backgroundColor=color;
		obj.onmouseout = function(){
				obj.style.backgroundColor=''; 
		}
	}
	$(document).ready(function(){
		$("#start_date").datepicker({
			dateFormat : "yy-mm-dd",
			changeMonth : true,
			changeYear : true,
		});
	});
	$(document).ready(function(){
		$("#end_date").datepicker({
			dateFormat : "yy-mm-dd",
			changeMonth : true,
			changeYear : true,
		});
	});		
	function cancel_click(){
		form.action ='?page=Proses-Prod-Out&Act=CLOSE';
		form.method = 'post';
		form.submit();			
		return true;
	}	
	function process_click(){
		form.action ='?page=Proses-Prod-Out&Act=PROCESS';
		form.method = 'post';
		form.submit();			
		return true;
	}
	function inventory_click(){
		form.action ='?page=Proses-Prod-Out&Act=INV';
		form.method = 'post';
		form.submit();			
		return true;
	}	
	function inventory_clickng(){
		form.action ='?page=Proses-Prod-Out&Act=INVNG';
		form.method = 'post';
		form.submit();			
		return true;
	}
	function delete_click(){
		form.action ='?page=Proses-Prod-Out&Act=DEL_TEMP';
		form.method = 'post';
		form.submit();			
		return true;
	}
	function deldettemp_click(id){
		form.action ='?page=Proses-Prod-Out&Act=DELDET_TEMP&id_temp='+id;
		form.method = 'post';
		form.submit();			
		return true;
	}
	function checkAll(){
		var inputs = document.getElementsByTagName('input');
		for (var i = 0; i< inputs.length; i++) {
			if (inputs[i].type == 'checkbox') {
				inputs[i].checked =true;
			}
		}
	}
	function uncheckAll(){
		var inputs = document.getElementsByTagName('input');
		for (var i = 0; i< inputs.length; i++) {
			if (inputs[i].type == 'checkbox') {
				inputs[i].checked =false;
			}
		}
	}
	function currency_click(currency,no){
		 qty=0; price=0; rate=0; usd=0, idr=0;
		 qty   = document.getElementById("trfd_qty2["+no+"]").value;
		 price = document.getElementById("trfd_price_satuan2["+no+"]").value;
		 rate  = document.getElementById("trfd_price_rate2["+no+"]").value;
		 qty = parseFloat(qty); price = parseFloat(price); rate = parseFloat(rate);
		 if(currency=="IDR"){
		 	idr = qty*price;
			usd = 0;
		 }else{
		 	idr = (qty*price)*rate;
			usd =  qty*price;
		 }
		 document.getElementById("trfd_price_usd2["+no+"]").value=usd;
		 document.getElementById("trfd_price2["+no+"]").value=idr;
	}
	function search_click(){
		form.action ='?page=Proses-Prod-Out&Act=CARI';
		form.method = 'post';
		form.submit();			
		return true;
	}
</script>
</head>
<?php
$start_date2 = get_startdate2($inv);
$end_date2   = get_enddate2($inv);
$bulan2    	 = substr($start_date2,5,2);
$tahun2  	 = substr($start_date2,0,4);
$fromdate	 = $start_date;
$todate      = $end_date;
$idprodcat	= isset($_REQUEST['idprodcat']) ? $_REQUEST['idprodcat'] : 1000; 
?>
<form action="#" method="post" name="form" target="_self" enctype="multipart/form-data">
<table>
	<tr>
		<th colspan="2">TRANSFER BARANG JADI/WIP/SCRAP KE GUDANG FINISHGOODS/SCRAP</th>
	</tr>	
	<tr>
		<td colspan="2">
			<!--<input name="userfile" type="file">
			<input type="button" name="process" value="IMPORT" style="cursor:pointer" onClick="process_click();">-->
			<input type="button" name="inventory" value="TRANSFER GOOD" style="cursor:pointer" onClick="inventory_click();">
			<input type="button" name="inventoryng" value="TRANSFER NG" style="cursor:pointer" onClick="inventory_clickng();">
			<select name="idprodcat" class="form_select">
				<option value="BLANK" selected="selected">PILIH</option>
				<?php
				  $dataSql = mysql_query("SELECT * FROM product_category WHERE id IN(2,3,5) ORDER BY id ASC");
				  while ($dataRow = mysql_fetch_array($dataSql)) {
					if ($dataRow['id']== $idprodcat) {
						$cek = " selected";
					} else { $cek=""; }
					echo "<option value='$dataRow[id]' $cek>$dataRow[prod_cat]</option>";
				  }
				  $sqlData ="";
				?>				
		    </select>
			<input type="button" name="cari" value="CARI" style="cursor:pointer" onclick="search_click();">
			<!--<input type="button" name="delete" value="DELETE" style="cursor:pointer" onClick="delete_click();">-->
			<input type="button" name="batal" value="CLOSE" style="cursor:pointer" onClick="cancel_click();" />
		</td>
	</tr>
</table>
<?php if(($Act=="ADD") || ($Act=="UPL") || ($Act=="PROCESS") || ($Act=="DEL_TEMP") || ($Act=="DELDET_TEMP") || ($Act=="CARI")){ ?>
	<div class="scroll">
	<table width="100%">
		<tr bgcolor="#CCCCCC">
			<td align="center" rowspan="2">No</td>
			<td align="center" rowspan="2">Pilih<br>
				<input type="radio" name="check" onClick="checkAll();">C
			    <input type="radio" name="check" onClick="uncheckAll();">U
			</td>
			<td align="center" rowspan="2">Tgl Trf.</td>				
			<td align="center" rowspan="2">Kode</td>
			<td align="center" rowspan="2">Nama Barang</td>
			<td align="center" rowspan="2">Satuan</td>
			<td align="center" rowspan="2">Stock</td>
            <td align="center" colspan="2">Transfer</td>
            <td align="center" rowspan="2">Harga</td>
			<!--<td align="center" rowspan="2">Gudang</td>-->
		</tr>	
		<tr bgcolor="#CCCCCC">
			<td align="center">Good</td>
			<td align="center">No Good</td>
		</tr>	
	<?php
	$no=1; $tprice=0;
	$qd = mysql_query("SELECT * FROM product WHERE prod_cat='$idprodcat' ORDER BY prod_code ASC");
	while($dd=mysql_fetch_array($qd)){		
		#Stock
		$stk = mysql_fetch_array(mysql_query("SELECT stock_prodid, SUM(stock_qty) qty, SUM(stock_price/stock_qty) price FROM stock WHERE stock_date='$fromdate' AND stock_status=10 AND stock_loc=2 AND stock_prodid='$dd[id]' 
			   GROUP BY stock_prodid"));
		#Transfer BB
		$trf = 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=1 AND a.trfd_prodid='$dd[id]' 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[id]' GROUP BY a.trfd_prodid"));
		#Work Order BJ BOM
		$wprod = mysql_fetch_array(mysql_query("SELECT wo_prodid, SUM(wo_qty) qty, SUM(wo_price/wo_qty) price FROM workorder WHERE wo_date BETWEEN '$fromdate' AND '$todate' 
		AND wo_status=20 AND wo_prodid='$dd[id]' 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[id]'
		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[id]' 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[id]' GROUP BY a.trfd_prodid"));      	   
		#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[id]'	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[id]'	GROUP BY a.kvd_prodid"));
		#wo detail
		$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[id]' AND b.wo_level<>'Level FG' GROUP BY a.wod_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[id]' AND b.wo_level='Level FG' 
			   GROUP BY a.wod_prodid"));
		#Work Order Non BOM
		$wonb = mysql_fetch_array(mysql_query("SELECT a.wonbd_prodid, SUM(a.wonbd_qty) qty FROM workorder_nonbom_detail a INNER JOIN workorder_nonbom b ON b.id=a.wonbd_hid 
				WHERE b.wonb_status=20 AND b.wonb_date BETWEEN '$fromdate' AND '$todate' AND a.wonbd_prodid='$dd[id]'	GROUP BY a.wonbd_prodid"));
		#Summary	   
		/*if($kvp['qty']>0){
			$rmn = ($stk['qty']+$trf['qty']+$wprod['qty']+$rtnprod['qty']+$mlprod['qty']+$kvp['qty'])-($rtn['qty']+$trfprod['qty']+$wo['qty']+$wofg['qty']);	
		}else{
			$rmn = ($stk['qty']+$trf['qty']+$wprod['qty']+$rtnprod['qty']+$mlprod['qty'])-($rtn['qty']+$trfprod['qty']+$kvm['qty']+$wo['qty']+$wofg['qty']);	
		}	
		$price = get_pricewip2($dd['id'],$fromdate,$todate,$inv);
		if($rmn > 0){*/
		#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']+$wonb['qty'];	
			$stock  = ($stk+$in)-$out;
			$price  = 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']+$wonb['qty'];	
			$stock  = ($stk+$in)-$out;
			$price  = 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']+$wonb['qty'];	
			$stock  = ($stk+$in)-$out;
			$price  = get_pricewip2($dd['id'],$fromdate,$todate,$inv);
			$tprice = $stock*$price;
		}else{
			$stk    = $stk['qty'];
			$in     = $rtnprod['qty']+$kvp['qty'];
			$out    = $trfprod['qty']+$kvm['qty']+$wonb['qty'];	
			$stock  = ($stk+$in)-$out;
			$price  = 0;
			$tprice = $stock*$price;
		}
		if($stock > 0){
	?>	
		<tr onMouseOver="setTrColor(this, '#FF9900');">
			<td align="center"><?=$no;?>.</td>	
			<td align="center">
				<input type="checkbox" id="trf_id2[<?=$no;?>]" name="trf_id2[<?=$no;?>]" value="<?=$no;?>">
			</td>	
			<td align="center">
				<input type="text" name="trf_spbdate2[<?=$no;?>]" value="<?=date("Y-m-d");?>" size="8" maxlength="10" style="text-align:center">
				<input type="hidden" name="trfd_prodid2[<?=$no;?>]" value="<?=$dd['id'];?>" size="8" style="text-align:center">
				<input type="hidden" name="rmn_qty2[<?=$no;?>]" value="<?=$stock;?>" size="8" style="text-align:center">
                <input type="hidden" name="trfd_price_satuan2[<?=$no;?>]" value="<?=$price;?>" size="8" style="text-align:center">
			</td>		
			<td><?=$dd['prod_code'];?></td>
			<td><?=$dd['prod_name'];?></td>
			<td><?=$dd['prod_sat'];?></td>
			<td align="right"><?=number_format($stock,2,'.',',');?></td>
			<td align="center"><input type="text" name="trfd_qty2[<?=$no;?>]"  id="trfd_qty2[<?=$no;?>]" size="8" value="0" style="text-align:center"></td>	
			<td align="center"><input type="text" name="trfd_qty3[<?=$no;?>]"  id="trfd_qty3[<?=$no;?>]" size="8" value="0" style="text-align:center"></td>		
            <td align="right"><?=number_format($price,2,'.',',');?></td>
			<!--<td align="right">
						<select name="trfd_whid2[<?=$no;?>]">
                           <option value="BLANK" 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>-->
		</tr>		
	<?php $no++; } } ?>
		<input type="hidden" name="rows" id="rows" value="<?=$no;?>">
	</table>
	</div>
<?php } ?>
</form>
</html> 
