<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");
	}
	function pilih(id)
	{
		//alert(teste);
		form.action ='?page=Proses-Stock-Opname&Act=ADD&stoh_prodcat='+id;
		form.method = 'post';
		form.submit();			
		return true;
	}
	function save_click(){
		if(form.stoh_cekno.value==""){
			alert("Silakan isi nomor surat.");
			form.stoh_cekno.focus();
			return false;
		}else if((form.stoh_pic.value=="") ){
			alert("Silakan isi pic.");
			form.stoh_pic.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-Stock-Opname&Act=SAVE';
				form.method = 'post';
				form.submit();			
				return true;
			}
		}
	}
	function close_click(){
		form.action ='?page=Proses-Stock-Opname&Act=CLOSE'
		form.method = 'post';
		form.submit();		
		return true;
	}
</script>
</head>
<form action="?page=Proses-Stock-Opname" 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 STOK OPNAME</th>
	</tr>
	<tr>
	  <td width="15%">No Stok Opname</td>
	  <td width="35%">: <input type="hidden" name="stoh_id" value="<?php echo $stoh_id; ?>" size="50" />
	                       <input type="text" name="stoh_no" value="<?php echo $stoh_no; ?>" size="45" readonly style="background-color:#CCFFFF " /></td>
	  <td width="15%">&nbsp;</td>
	  <td width="35%">&nbsp;</td>
	</tr>
	<tr>
		<td>Tanggal Stok Opname</td>
		<td>: <input type="text" name="stoh_date" value="<?php if(empty($stoh_date)){$stoh_date=$end_date;} echo $stoh_date; ?>" size="10" id="spbdate" 
				  readonly="true" style="background-color:#CCFFFF " /> </td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
	</tr>
	<tr>
		<td>No Surat Tugas</td>
		<td>: <input type="text" name="stoh_cekno" value="<?php echo $stoh_cekno; ?>" size="45"></td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
	</tr>
	<tr>
		<td>Tanggal Surat Tugas</td>
		<td>: <input type="text" name="stoh_cekdate" value="<?php if(empty($stoh_cekdate)){$stoh_cekdate=$end_date;} echo $stoh_cekdate; ?>" 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="stoh_deptid">
							<?php
							  $dataSql = mysql_query("SELECT id,dept_name FROM departement ORDER BY id ASC");
							  while ($dataRow = mysql_fetch_array($dataSql)) {
								if ($dataRow['id']== $stoh_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="stoh_pic" value="<?php echo $stoh_pic; ?>" size="45">
		</td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
	</tr>
	<?php if(empty($stoh_id)){ ?>
	<tr>	
		<td>Kategori</td>
		<td>: <select name="stoh_prodcat" onChange="pilih(this.value);">
				  <option value="BLANK">PILIH</option> 
					<?php
					  $dataSql = mysql_query("SELECT * FROM product_category ORDER BY  id ASC");
					  while ($dataRow = mysql_fetch_array($dataSql)) {
						if ($dataRow['id']== $stoh_prodcat) {
							$cek = " selected";
						} else { $cek=""; }
						echo "<option value='$dataRow[id]' $cek>$dataRow[prod_cat]</option>";
					  }
					  $sqlData ="";
					?>
				 </select>
		</td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
	</tr>
	<?php }else{?>
	<tr>	
		<td>Kategori</td>
		<td>: <select name="stoh_prodcat" onChange="pilih(this.value);" disabled>
				  <option value="BLANK">PILIH</option> 
					<?php
					  $dataSql = mysql_query("SELECT * FROM product_category ORDER BY  id ASC");
					  while ($dataRow = mysql_fetch_array($dataSql)) {
						if ($dataRow['id']== $stoh_prodcat) {
							$cek = " selected";
						} else { $cek=""; }
						echo "<option value='$dataRow[id]' $cek>$dataRow[prod_cat]</option>";
					  }
					  $sqlData ="";
					?>
				 </select>
		</td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
	</tr>
	<?php } ?>
	<tr>
		<td colspan="4">&nbsp;</td>
	</tr>
	<tr>
		<th colspan="4">DETAIL STOK OPNAME</th>
	</tr>
	<tr>
		<td colspan="4">&nbsp;</td>
	</tr>
	<tr>
		<td colspan="4">
		<?php if(empty($stoh_id)){?>
			<table width="100%">
				<tr>
					<th>No.</th>
					<th>Nama Barang</th>
					<th>Unit</th>
					<th>Saldo</th>
					<th>Stock Opname</th>
				</tr>
				<?php	
					$no=1; $tqty=0; 
					if($stoh_prodcat==1 || $stoh_prodcat==2 || $stoh_prodcat==3 || $stoh_prodcat==4 || $stoh_prodcat==6 || $stoh_prodcat==7 || $stoh_prodcat==8){  	
						if($stoh_prodcat==1 || $stoh_prodcat==6 || $stoh_prodcat==7 || $stoh_prodcat==8){
							#$qd = mysql_query("CALL spvw_stobb('$start_date','$end_date','$month_start','$year_start','$stoh_prodcat','$month_sto','$year_sto');");	
							$qd = mysql_query("SELECT b.prodid id, b.doctype, b.docno, b.docdate, b.begqty, b.inqty, b.outqty, b.adjqty, ((b.begqty+b.inqty)-b.outqty+b.adjqty) endqty,  b.price, b.prod_code, 
							b.prod_name, b.prod_sat, b.whid, b.mw_name, b.curr FROM
							(SELECT a.prodid, a.doctype, a.docno, a.docdate, a.whid, c.mw_name, SUM(a.begqty) begqty, SUM(a.inqty) inqty, SUM(a.outqty) outqty, SUM(a.adj_qty) adjqty, a.price, b.prod_code, b.prod_name, b.prod_sat, a.curr  FROM
							(SELECT stock_prodid prodid, stock_doctype doctype, stock_docno docno, stock_docdate docdate, stock_qty begqty, 0 inqty, 0 outqty, 0 adj_qty, (stock_price/stock_qty) price, stock_whid as whid, stock_currency curr 
							FROM stock WHERE stock_status=10 AND stock_date='$start_date' AND stock_loc IN(1,4)
							UNION ALL
							SELECT b.incd_prodid prodid, a.inch_doctype doctype , a.inch_docno docno, a.inch_docdate docdate, 0 begqty, b.incd_qty inqty, 0 outqty, 0 adj_qty, (b.incd_price/b.incd_qty) price, b.incd_whid as whid, b.incd_currency curr 
							FROM incoming a
							INNER JOIN incoming_detail b ON b.incd_hid=a.id
							WHERE a.inch_status=20 AND a.inch_docdate BETWEEN '$start_date' AND '$end_date'
							UNION ALL
							SELECT a.trfd_prodid prodid, a.trfd_doctype doctype , a.trfd_docno docno, a.trfd_docdate docdate, 0 begqty, 0 inqty, a.trfd_qty outqty, 0 adj_qty, 0 price, a.trfd_whid as whid, 0 curr
							FROM transfer_detailexbc a
							INNER JOIN transfer b ON b.id=a.trfd_hid
							WHERE b.trf_status=20 AND a.trfd_status=10 AND b.trf_category=1 AND b.trf_spbdate BETWEEN '$start_date' AND '$end_date'
							UNION ALL
							SELECT a.trfd_prodid prodid, a.trfd_doctype doctype , a.trfd_docno docno, a.trfd_docdate docdate, 0 begqty, a.trfd_qty inqty, 0 outqty, 0 adj_qty, 0 price, a.trfd_whid as whid, 0 curr
							FROM transfer_detailexbc a
							INNER JOIN transfer b ON b.id=a.trfd_hid
							WHERE b.trf_status=20 AND a.trfd_status=10 AND b.trf_category=3 AND b.trf_spbdate BETWEEN '$start_date' AND '$end_date'
							UNION ALL
							SELECT a.prodid, a.jenis_dokpab_asal doctype , a.no_dokpab_asal docno, a.tgl_dokpab_asal docdate, 0 begqty, 0 inqty, a.jumlah outqty, 0 adj_qty, 0 price, a.outd_whid as whid, 0 curr 
							FROM outgoing_dokasal a
							INNER JOIN outgoing b ON b.id=a.outh_hid
							INNER JOIN product c ON c.id=a.prodid AND prod_cat IN(1,6,7)
							INNER JOIN outgoing_detail d ON d.outd_prodid=a.prodid AND d.outd_hid=b.id
							WHERE b.outh_status=20 AND a.log_status=20 AND b.outh_docdate BETWEEN '$start_date' AND '$end_date'
							UNION ALL
							SELECT trfd_prodid prodid, trfd_doctype doctype , trfd_docno docno, trfd_docdate docdate, 0 begqty, 0 inqty, trfd_qty outqty, 0 adj_qty, 0 price, trfd_whid as whid, 0 curr
							FROM transfer_temp WHERE trfd_category=1 AND (trfd_inputby<>'' AND trfd_inputby is not null)
							UNION ALL
							SELECT a.adjd_prodid prodid, a.adjd_doctype doctype , a.adjd_docno docno, a.adjd_docdate docdate, 0 begqty, adjd_qty inqty, 0 outqty, 0 adj_qty, 0 price, 0 as whid, 0 curr 
							FROM adjusment_detail a
							INNER JOIN adjusment b ON b.id=a.adjd_hid            
							WHERE b.adjh_status=20 AND b.adjh_date BETWEEN '$start_date' AND '$end_date') a
							INNER JOIN product b ON b.id=a.prodid
							LEFT JOIN master_warehouse c ON c.id=a.whid
							WHERE b.prod_cat='$stoh_prodcat' GROUP BY a.prodid, a.doctype, a.docno, a.docdate, a.whid) b
							WHERE ((b.begqty+b.inqty)-b.outqty+b.adjqty) > 0 ORDER BY b.docno, b.prod_code ASC;");								
							$loc = 1;	
						}elseif($stoh_prodcat==2){
							#$qd = mysql_query("CALL spvw_stobm('$start_date','$end_date','$month_start','$year_start','$stoh_prodcat','$month_sto','$year_sto');");
							$qd =mysql_query("SELECT a.id, a.prod_code, a.prod_name, a.prod_sat, (ifnull(b.qty,0) + ifnull(c.qty,0) + ifnull(h.qty,0) + ifnull(i.qty,0) - ifnull(d.qty,0) - + ifnull(g.qty,0) + ifnull(e.qty,0)) as end_qty
									FROM product a             
									LEFT JOIN (SELECT stock_prodid, SUM(stock_qty) as qty FROM stock WHERE stock_date='$start_date' AND stock_status=10 AND stock_loc IN(1,4) GROUP BY stock_prodid) AS b
											  ON b.stock_prodid = a.id
									LEFT JOIN (SELECT a.incd_prodid, sum(a.incd_qty) as qty from incoming_detail a 
											 inner join incoming b on b.id = a.incd_hid
											 where b.inch_status=20 and inch_docdate between '$start_date' and '$end_date' 
											 group by incd_prodid) AS c ON c.incd_prodid=a.id
									LEFT JOIN (SELECT a.outd_prodid, sum(a.outd_qty) as qty from outgoing_detail a 
											 inner join outgoing b on b.id = a.outd_hid
											 where b.outh_status=20 and outh_docdate between '$start_date' and '$end_date'
											 group by outd_prodid) AS d ON d.outd_prodid=a.id     
									LEFT JOIN (select a.adjd_prodid, sum(adjd_qty) as qty from adjusment_detail a
											   inner join adjusment b on b.id=a.adjd_hid
											   where b.adjh_status=20 and adjh_date between '$start_date' and '$end_date'
											   group by adjd_prodid) AS e ON e.adjd_prodid=a.id
									LEFT JOIN (select a.stod_prodid, max(b.stoh_date), a.stod_qtysaldo as qty from stockopname_detail a 
											   inner join stockopname b on b.id=a.stod_hid
											   where b.stoh_status=20 and month(b.stoh_date)='$month_sto' and year(b.stoh_date)='$year_sto'
											   group by a.stod_prodid) AS f  ON f.stod_prodid=a.id   									
									LEFT JOIN (select a.trfd_prodid, sum(a.trfd_qty) as qty from transfer_detailexbc a 
										   inner join transfer b on b.id = a.trfd_hid
										   where b.trf_status=20 and b.trf_category IN (1,2) and trf_spbdate between '$start_date' and '$end_date'
										   group by trfd_prodid) AS g ON g.trfd_prodid=a.id
									LEFT JOIN (select a.trfd_prodid, sum(a.trfd_qty) as qty from transfer_detailexbc a 
										   inner join transfer b on b.id = a.trfd_hid
										   where b.trf_status=20 and b.trf_category IN (3,5) and trf_spbdate between '$start_date' and '$end_date'
										   group by trfd_prodid) AS h ON h.trfd_prodid=a.id
									LEFT JOIN (select a.trfd_prodid, sum(a.trfd_qty) as qty from transfer_detail a 
										   inner join transfer b on b.id = a.trfd_hid
										   where b.trf_status=20 and b.trf_category IN (4) and trf_spbdate between '$start_date' and '$end_date'
										   group by trfd_prodid) AS i ON i.trfd_prodid=a.id	
									WHERE a.prod_cat IN(2) ORDER BY a.prod_code ASC");
							$loc = 4;
						}elseif($stoh_prodcat==3){
							#$qd = mysql_query("CALL spvw_stobj('$start_date','$end_date','$month_start','$year_start','$stoh_prodcat','$month_sto','$year_sto');");
							$qd = mysql_query("SELECT a.id, a.prod_code, a.prod_name, a.prod_sat, 
								    (ifnull(b.qty,0) + (ifnull(c.qty,0) + ifnull(h.qty,0) + ifnull(i.qty,0) - ifnull(g.qty,0)) - ifnull(d.qty,0) + ifnull(e.qty,0) - ifnull(j.qty,0)) as end_qty
									FROM product a             
									LEFT JOIN (SELECT stock_prodid, stock_qty as qty FROM stock WHERE stock_date='$start_date' AND stock_status=10 AND stock_loc=3) AS b
											  ON b.stock_prodid = a.id
									LEFT JOIN (select a.trfd_prodid, sum(a.trfd_qty) as qty from transfer_detail a 
											 inner join transfer b on b.id = a.trfd_hid
											 where b.trf_status=20 and b.trf_category IN (4) and trf_spbdate between '$start_date' and '$end_date' 
											 group by trfd_prodid) AS c ON c.trfd_prodid=a.id                                       
									LEFT JOIN (SELECT a.outd_prodid, sum(a.outd_qty) as qty from outgoing_detail a 
											 inner join outgoing b on b.id = a.outd_hid
											 where b.outh_status=20 and outh_docdate between '$start_date' and '$end_date'
											 group by outd_prodid) AS d ON d.outd_prodid=a.id							    
									LEFT JOIN (select a.adjd_prodid, sum(adjd_qty) as qty from adjusment_detail a
											   inner join adjusment b on b.id=a.adjd_hid
											   where b.adjh_status=20 and adjh_date between '$start_date' and '$end_date'
											   group by adjd_prodid) AS e ON e.adjd_prodid=a.id
									LEFT JOIN (select a.stod_prodid, max(b.stoh_date), a.stod_qtysaldo as qty from stockopname_detail a 
											   inner join stockopname b on b.id=a.stod_hid
											   where b.stoh_status=20 and month(b.stoh_date)='$month_sto' and year(b.stoh_date)='$year_sto' 
											   group by a.stod_prodid) AS f  ON f.stod_prodid=a.id  
									LEFT JOIN (select a.trfd_prodid, sum(a.trfd_qty) as qty from transfer_detail a 
											 inner join transfer b on b.id = a.trfd_hid
											 where b.trf_status=20 and b.trf_category IN (5,6) and trf_spbdate between '$start_date' and '$end_date'
											 group by trfd_prodid) AS g ON g.trfd_prodid=a.id
									LEFT JOIN (SELECT a.incd_prodid, sum(a.incd_qty) as qty from incoming_detail a 
											 inner join incoming b on b.id = a.incd_hid
											 where b.inch_status=20 and inch_docdate between '$start_date' and '$end_date' 
											 group by incd_prodid) AS h ON h.incd_prodid=a.id	
									LEFT JOIN (SELECT kv_prodid, sum(kv_qty) as qty from konversi_external where kv_status=20 and kv_date between '$start_date' and '$end_date' 
											group by kv_prodid) AS i ON i.kv_prodid=a.id
									LEFT JOIN (SELECT a.kvd_prodid, sum(a.kvd_qty) as qty from konversi_external_detail a INNER JOIN konversi_external b ON b.id=a.kvd_hid  
												where b.kv_status=20 and b.kv_date between '$start_date' and '$end_date' group by a.kvd_prodid) AS j ON j.kvd_prodid=a.id		
									WHERE a.prod_cat IN(3,5) ORDER BY a.prod_code ASC");
							$loc = 3;
						}elseif($stoh_prodcat==4){
							#$qd = mysql_query("CALL spvw_stobs('$start_date','$end_date','$month_start','$year_start','$stoh_prodcat','$month_sto','$year_sto');");
							$qd =mysql_query("SELECT a.id, a.prod_code, a.prod_name, a.prod_sat, (ifnull(b.qty,0) + (ifnull(c.qty,0) + ifnull(h.qty,0) - ifnull(g.qty,0)) - ifnull(d.qty,0) + ifnull(e.qty,0)) as end_qty
								FROM product a             
								LEFT JOIN (SELECT stock_prodid, SUM(stock_qty) as qty FROM stock WHERE stock_date='$start_date' AND stock_status=10 AND stock_loc=3 GROUP BY stock_prodid) AS b
										  ON b.stock_prodid = a.id
								LEFT JOIN (select a.trfd_prodid, sum(a.trfd_qty) as qty from transfer_detail a 
										 inner join transfer b on b.id = a.trfd_hid
										 where b.trf_status=20 and b.trf_category IN (4) and trf_spbdate between '$start_date' and '$end_date' 
										 group by trfd_prodid) AS c ON c.trfd_prodid=a.id                                       
								LEFT JOIN (SELECT a.outd_prodid, sum(a.outd_qty) as qty from outgoing_detail a 
										 inner join outgoing b on b.id = a.outd_hid
										 where b.outh_status=20 and outh_docdate between '$start_date' and '$end_date'
										 group by outd_prodid) AS d ON d.outd_prodid=a.id							    
								LEFT JOIN (select a.adjd_prodid, sum(adjd_qty) as qty from adjusment_detail a
										   inner join adjusment b on b.id=a.adjd_hid
										   where b.adjh_status=20 and adjh_date between '$start_date' and '$end_date'
										   group by adjd_prodid) AS e ON e.adjd_prodid=a.id
								LEFT JOIN (select a.stod_prodid, max(b.stoh_date), a.stod_qtysaldo as qty from stockopname_detail a 
										   inner join stockopname b on b.id=a.stod_hid
										   where b.stoh_status=20 and month(b.stoh_date)='$month_sto' and year(b.stoh_date)='$year_sto' 
										   group by a.stod_prodid) AS f  ON f.stod_prodid=a.id  
								LEFT JOIN (select a.trfd_prodid, sum(a.trfd_qty) as qty from transfer_detail a 
										 inner join transfer b on b.id = a.trfd_hid
										 where b.trf_status=20 and b.trf_category IN (6) and trf_spbdate between '$start_date' and '$end_date'
										 group by trfd_prodid) AS g ON g.trfd_prodid=a.id
								LEFT JOIN (SELECT a.incd_prodid, sum(a.incd_qty) as qty from incoming_detail a 
										 inner join incoming b on b.id = a.incd_hid
										 where b.inch_status=20 and inch_docdate between '$start_date' and '$end_date' 
										 group by incd_prodid) AS h ON h.incd_prodid=a.id		 		      
								WHERE a.prod_cat=4 ORDER BY a.prod_code ASC");
							$loc = 3;
						}
						while($dt=mysql_fetch_object($qd)){	
							if($stoh_prodcat==1 || $stoh_prodcat==6 || $stoh_prodcat==7 || $stoh_prodcat==8){  	
								$qty = $dt->endqty;
								$price = $qty * $dt->price;
								$doctype =$dt->doctype;
								$docno =$dt->docno;
								$docdate=$dt->docdate;
								$curr   = $dt->curr;
								$whid   = $dt->whid;
							}elseif($stoh_prodcat==2){  
								#cek dari stock
								$exbc1 = mysql_fetch_array(mysql_query("SELECT stock_doctype, stock_docno, stock_docdate FROM stock WHERE stock_status=10 AND stock_prodid='$dt->id' AND stock_date='$start_date'
										 AND stock_docno <>'' AND stock_doctype<>'' AND (stock_docdate <>'' OR stock_docdate<>'0000-00-00') ORDER BY id DESC LIMIT 0,1"));
								#cek dari incoming
								$exbc2 = mysql_fetch_array(mysql_query("SELECT a.inch_doctype, a.inch_docno, a.inch_docdate FROM incoming a INNER JOIN incoming_detail b ON b.incd_hid=a.id 
										 WHERE a.inch_status=20 AND a.inch_docdate BETWEEN '$start_date' AND '$end_date' AND b.incd_prodid='$dt->id' ORDER BY a.id DESC LIMIT 0,1"));
								if(!empty($exbc1['stock_doctype'])){$doctype=$exbc1['stock_doctype'];}else{$doctype=$exbc2['inch_doctype'];}	
								if(!empty($exbc1['stock_docno'])){$docno=$exbc1['stock_docno'];}else{$docno=$exbc2['inch_docno'];}	
								if(!empty($exbc1['stock_docdate']) && $exbc1['stock_docdate'] != "0000-00-00"){$docdate=$exbc1['stock_docdate'];}else{$docdate=$exbc2['inch_docdate'];}
								$qty   = $dt->end_qty;
								$price = 0;
								$curr  = "";
								$whid  = 0;
							}else{
								$qty = $dt->end_qty;
								$price   = 0;
								$doctype ="";
								$docno   ="";
								$docdate ="";
								$curr    = "";
								$whid    = 0;
							}
							$tqty     = $tqty + $qty; 
					if($qty>0){
				?> 
				   <tr>
					<td align="center"><?=$no;?>.<input type="hidden" name="id_det[<?=$no;?>]" value="<?=$no;?>"></td>
					<td><input type="text" value="<?php echo "$dt->prod_code - $dt->prod_name";?>" size="100" readonly></td>
					<td><input type="text" value="<?=$dt->prod_sat;?>" size="10" readonly style="text-align:center"></td>
					<td align="left"><input type="text" name="stod_qtysaldo[<?=$no;?>]" value="<?=$qty;?>" style="text-align:right "></td>
					<td align="left">
                    	<input type="text" name="stod_qtysto[<?=$no;?>]" value="<?=$qty;?>" style="text-align:right ">
                        <input type="hidden" name="stock_price[<?=$no;?>]" value="<?=$price;?>">
                        <input type="hidden" name="stock_doctype[<?=$no;?>]" value="<?=$doctype;?>">
                        <input type="hidden" name="stock_docno[<?=$no;?>]" value="<?=$docno;?>">
                        <input type="hidden" name="stock_docdate[<?=$no;?>]" value="<?=$docdate;?>">
						<input type="hidden" name="stock_currency[<?=$no;?>]" value="<?=$curr;?>">
                        <input type="hidden" name="stock_loc[<?=$no;?>]" value="<?=$loc;?>">
						<input type="hidden" name="whid[<?=$no;?>]" value="<?=$whid;?>">
                        <input type="hidden" name="id[<?=$no?>]" value="<?=$dt->id;?>">
                    </td>
				</tr>
				<?php $no++; } } ?>	
				<input type="hidden" name="tqty" value="<?=$tqty;?>">		
				<?php }elseif($stoh_prodcat==5){
					$no=1; $tqty=0; 
					$qd = mysql_query("SELECT a.id, a.prod_code, a.prod_name, a.prod_sat, a.prod_cat, b.stock_qty, c.trfgd_qty, d.rtngd_qty, e.wobb_qty, f.wobj_qty, g.fgd_qty, h.trfqc_qty, i.rtnqc_qty,
											  j.scr_qty, k.kvp_qty, l.kvm_qty, m.wobbbj_qty, n.kvp2_qty, o.kvm2_qty, p.wonbd_qty
										FROM product a
										LEFT JOIN (SELECT stock_prodid, SUM(stock_qty) stock_qty FROM stock WHERE stock_date='$start_date' AND stock_status=10 
														   AND stock_loc=2 GROUP BY stock_prodid) as b ON b.stock_prodid=a.id
										LEFT JOIN (SELECT a.trfd_prodid, SUM(a.trfd_qty) trfgd_qty FROM transfer_detailexbc a INNER JOIN transfer b ON b.id=a.trfd_hid
													   WHERE b.trf_spbdate BETWEEN '$start_date' AND '$end_date' AND b.trf_status=20 AND b.trf_category=1 GROUP BY a.trfd_prodid) as c ON c.trfd_prodid=a.id
										LEFT JOIN (SELECT a.trfd_prodid, SUM(a.trfd_qty) rtngd_qty FROM transfer_detailexbc a INNER JOIN transfer b ON b.id=a.trfd_hid
													   WHERE b.trf_spbdate BETWEEN '$start_date' AND '$end_date' AND b.trf_status=20 AND b.trf_category=3 GROUP BY a.trfd_prodid) as d ON d.trfd_prodid=a.id  
										LEFT JOIN (SELECT a.wod_prodid, SUM(a.wod_qty) wobb_qty FROM workorder_detail a INNER JOIN workorder b ON b.id=a.wod_hid
													   WHERE b.wo_date BETWEEN '$start_date' AND '$end_date' AND b.wo_status=20 AND b.wo_level<>'Level FG' GROUP BY a.wod_prodid) as e ON e.wod_prodid=a.id           
										LEFT JOIN (SELECT wo_prodid, SUM(wo_qty) wobj_qty FROM workorder WHERE wo_date BETWEEN '$start_date' AND '$end_date'
												   AND wo_status=20 AND wo_level<>'Level FG' GROUP BY wo_prodid) as f ON f.wo_prodid=a.id           
										LEFT JOIN (SELECT c.id, SUM(b.fgd_qty) fgd_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 '$start_date' AND '$end_date' GROUP BY c.id) as g ON g.id=a.id
										LEFT JOIN (SELECT a.trfd_prodid, SUM(a.trfd_qty) trfqc_qty FROM transfer_detail a INNER JOIN transfer b ON b.id=a.trfd_hid
													   WHERE b.trf_spbdate BETWEEN '$start_date' AND '$end_date' AND b.trf_status=20 AND b.trf_category=4 GROUP BY a.trfd_prodid) as h ON h.trfd_prodid=a.id
										LEFT JOIN (SELECT a.trfd_prodid, SUM(a.trfd_qty) rtnqc_qty FROM transfer_detail a INNER JOIN transfer b ON b.id=a.trfd_hid
													   WHERE b.trf_spbdate BETWEEN '$start_date' AND '$end_date' AND b.trf_status=20 AND b.trf_category=6 GROUP BY a.trfd_prodid) as i ON i.trfd_prodid=a.id
										LEFT JOIN (SELECT c.id, SUM(b.scrd_qty) scr_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 '$start_date' AND '$end_date' GROUP BY c.id) as j ON j.id=a.id
										LEFT JOIN (SELECT kv_prodid, SUM(kv_qty) kvp_qty FROM konversi WHERE kv_status=20 AND kv_date BETWEEN '$start_date' AND '$end_date' GROUP BY kv_prodid) as k ON k.kv_prodid=a.id
										LEFT JOIN (SELECT a.kvd_prodid, SUM(a.kvd_qty) kvm_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 '$start_date' AND '$end_date'	GROUP BY a.kvd_prodid) as l ON l.kvd_prodid=a.id
										LEFT JOIN (SELECT a.wod_prodid, SUM(a.wod_qty) wobbbj_qty FROM workorder_detail a INNER JOIN workorder b ON b.id=a.wod_hid
														 WHERE b.wo_date BETWEEN '$start_date' AND '$end_date' AND b.wo_status=20 AND b.wo_level='Level FG' GROUP BY a.wod_prodid) as m ON m.wod_prodid=a.id
										LEFT JOIN (SELECT kv_prodid, SUM(kv_qty) kvp2_qty FROM konversi2 WHERE kv_status=20 AND kv_date BETWEEN '$start_date' AND '$end_date' GROUP BY kv_prodid) as n ON n.kv_prodid=a.id
										LEFT JOIN (SELECT a.kvd_prodid, SUM(a.kvd_qty) kvm2_qty FROM konversi_detail2 a INNER JOIN konversi2 b ON b.id=a.kvd_hid 
														   WHERE b.kv_status=20 AND b.kv_date BETWEEN '$start_date' AND '$end_date'	GROUP BY a.kvd_prodid) as o ON o.kvd_prodid=a.id   
										LEFT JOIN (SELECT a.wonbd_prodid, SUM(a.wonbd_qty) wonbd_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 '$start_date' AND '$end_date'	GROUP BY a.wonbd_prodid) as p ON p.wonbd_prodid=a.id                           
										WHERE a.prod_cat IN(1,3,6,4,5,7) AND (b.stock_qty > 0 OR c.trfgd_qty > 0 OR d.rtngd_qty > 0 OR e.wobb_qty > 0 OR f.wobj_qty > 0 OR g.fgd_qty > 0 OR h.trfqc_qty > 0 OR i.rtnqc_qty > 0 OR j.scr_qty > 0 OR
											  k.kvp_qty > 0 OR l.kvm_qty > 0 OR m.wobbbj_qty > 0 OR n.kvp2_qty > 0 OR o.kvm2_qty > 0 OR p.wonbd_qty > 0) 
										ORDER BY a.prod_code ASC");
					while($dd=mysql_fetch_array($qd)){		
						#Summary	   
						if($dd['prod_cat']==1 || $dd['prod_cat']==6 || $dd['prod_cat']==7 ){			
							$stk    = $dd['stock_qty']; 
							$in     = $dd['trfgd_qty']+$dd['kvp_qty']+$dd['kvm2_qty']; 
							$out    = $dd['rtngd_qty']+$dd['wobb_qty']+$dd['kvm_qty']+$dd['wobbbj_qty']+$dd['kvp2_qty']+$dd['wonbd_qty'];	
							$stock  = ($stk+$in)-$out;
							$price  = get_unitprice_bb($start_date_m2year,$end_date,$dd['id']); 
							$tprice = $stock*$price;
						}elseif($dd['prod_cat']==3){
							$stk    = $dd['stock_qty'];
							$in     = $dd['trfgd_qty']+$dd['wobj_qty']+$dd['rtnqc_qty']+$dd['fgd_qty']+$dd['kvp_qty']+$dd['kvm2_qty'];
							$out    = $dd['trfqc_qty']+$dd['wobb_qty']+$dd['kvm_qty']+$dd['wobbbj_qty']+$dd['kvp2_qty']+$dd['wonbd_qty'];	
							$stock  = ($stk+$in)-$out;
							$price  = get_unitprice_bj($start_date_m2year,$end_date,$dd['id']); 
							$tprice = $stock*$price;
						}elseif($dd['prod_cat']==5){
							$stk    = $dd['stock_qty'];
							$in     = $dd['trfgd_qty']+$dd['wobj_qty']+$dd['rtnqc_qty']+$dd['kvp_qty']+$dd['kvm2_qty'];
							$out    = $dd['trfqc_qty']+$dd['wobb_qty']+$dd['kvm_qty']+$dd['wobbbj_qty']+$dd['kvp2_qty']+$dd['wonbd_qty'];	
							$stock  = ($stk+$in)-$out;
							$price  = get_unitprice_wip($start_date_m2year,$end_date,$dd['id']); 
							$tprice = $stock*$price;
						}else{
							$stk    = $dd['stock_qty'];
							$in     = $dd['scr_qty']+$dd['wobj_qty']+$dd['rtnqc_qty']+$dd['kvp_qty']+$dd['kvm2_qty'];
							$out    = $dd['trfqc_qty']+$dd['kvm_qty']+$dd['kvp2_qty']+$dd['wonbd_qty'];	
							$stock  = ($stk+$in)-$out;
							$price  = 0;
							$tprice = $stock*$price;
						}
					$tqty=$tqty+$stock;
					if($stock>0){	
						?>
					<tr>
                        <td align="center"><?=$no;?>.<input type="hidden" name="id_det[<?=$no;?>]" value="<?=$no;?>"></td>
                        <td><input type="text" value="<?php echo "$dd[prod_code] - $dd[prod_name]";?>" size="100" readonly></td>
                        <td><input type="text" value="<?=$dd['prod_sat'];?>" size="10" readonly style="text-align:center"></td>
                        <td align="left"><input type="text" name="stod_qtysaldo[<?=$no;?>]" value="<?=$stock;?>" style="text-align:right "></td>
                        <td align="left">
                            <input type="text" name="stod_qtysto[<?=$no;?>]" value="<?=$stock;?>" style="text-align:right ">
                            <input type="hidden" name="stock_price[<?=$no;?>]" value="<?=$price;?>">
                            <input type="hidden" name="stock_doctype[<?=$no;?>]" value="">
                            <input type="hidden" name="stock_docno[<?=$no;?>]" value="">
                            <input type="hidden" name="stock_docdate[<?=$no;?>]" value="">
							<input type="hidden" name="stock_currency[<?=$no;?>]" value="">
                            <input type="hidden" name="stock_loc[<?=$no;?>]" value="2">
							<input type="hidden" name="whid[<?=$no;?>]" value="0">
                            <input type="hidden" name="id[<?=$no?>]" value="<?=$dd['id'];?>">
                        </td>
				  </tr>	
				<?php $no++;} } ?>
				<input type="hidden" name="tqty" value="<?=$tqty;?>">
				<?php } ?>    	
			</table>
			<?php }else{ ?>
			<table width="100%">
				<tr>
					<th>No.</th>
					<th>Nama Barang</th>
					<th>Unit</th>
					<th>Saldo</th>
					<th>Stock Opname</th>
				</tr>
				<?php	
					if($stoh_prodcat==1){$loc=1;}
					elseif($stoh_prodcat==2){$loc=4;}
					elseif($stoh_prodcat==3){$loc=3;}
					elseif($stoh_prodcat==4){$loc=5;}
					elseif($stoh_prodcat==5){$loc=2;}
					elseif($stoh_prodcat==6){$loc=1;}
					elseif($stoh_prodcat==7){$loc=1;}
					$no=0; $tqty=0;
					$qd = mysql_query(" SELECT a.*,b.prod_code, b.prod_name, b.prod_sat FROM stockopname_detail a
										INNER JOIN product b ON b.id=a.stod_prodid
										WHERE a.stod_hid='$stoh_id'
										ORDER BY b.prod_code ASC");
					while($dt=mysql_fetch_object($qd)){	   
				    $no++;
					$tqty     = $tqty + $dt->stod_qtysaldo; 
				?> 
				  <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="100" readonly></td>
					<td><input type="text" value="<?=$dt->prod_sat;?>" size="10" readonly style="text-align:center"></td>
					<td align="left"><input type="text" name="stod_qtysaldo[<?=$dt->id;?>]" value="<?=$dt->stod_qtysaldo;?>" style="text-align:right "></td>
					<td align="left"><input type="text" name="stod_qtysto[<?=$dt->id;?>]" value="<?=$dt->stod_qtysto;?>" style="text-align:right "></td>
				</tr>
				<?php } ?>	
				<input type="hidden" name="tqty" value="<?=$tqty;?>">			
			</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> 
