<html lang="en" >
<head>
<meta charset="UTF-8">
<title>CEK WAE</title>  
<!--Data table--> 
<link rel="stylesheet" href="../function/dataTables/media/css/demo_page.css">
<link rel="stylesheet" href="../function/dataTables/media/css/demo_table_jui.css">
<link rel="stylesheet" href="../function/dataTables/media/themes/smoothness/jquery-ui-1.8.4.custom.css">
<script type="text/javascript" src="../function/dataTables/media/js/jquery.js"></script>
<script type="text/javascript" src="../function/dataTables/media/js/jquery.dataTables.js"></script>
<script type="text/javascript" language="javascript">
	$(document).ready( function () {
		var oTable = $('#myTables').dataTable( {
			 "sDom": 'R<"H"lfr>t<"F"ip>',
			 "bJQueryUI": true,
			 "sPaginationType": "full_numbers",
			 "iDisplayLength" :25
		});
	});
	$(document).ready( function () {
		var oTable = $('#myTables2').dataTable( {
			 "sDom": 'R<"H"lfr>t<"F"ip>',
			 "bJQueryUI": true,
			 "sPaginationType": "full_numbers",
			 "iDisplayLength" :25
		});
	});
	$(document).ready( function () {
		var oTable = $('#myTables3').dataTable( {
			 "sDom": 'R<"H"lfr>t<"F"ip>',
			 "bJQueryUI": true,
			 "sPaginationType": "full_numbers",
			 "iDisplayLength" :25
		});
	});
	$(document).ready( function () {
		var oTable = $('#myTables4').dataTable( {
			 "sDom": 'R<"H"lfr>t<"F"ip>',
			 "bJQueryUI": true,
			 "sPaginationType": "full_numbers",
			 "iDisplayLength" :25
		});
	});
	$(document).ready( function () {
		var oTable = $('#myTables5').dataTable( {
			 "sDom": 'R<"H"lfr>t<"F"ip>',
			 "bJQueryUI": true,
			 "sPaginationType": "full_numbers",
			 "iDisplayLength" :25
		});
	});
	$(document).ready( function () {
		var oTable = $('#myTables6').dataTable( {
			 "sDom": 'R<"H"lfr>t<"F"ip>',
			 "bJQueryUI": true,
			 "sPaginationType": "full_numbers",
			 "iDisplayLength" :25
		});
	});
	$(document).ready( function () {
		var oTable = $('#myTables7').dataTable( {
			 "sDom": 'R<"H"lfr>t<"F"ip>',
			 "bJQueryUI": true,
			 "sPaginationType": "full_numbers",
			 "iDisplayLength" :25
		});
	});
	$(document).ready( function () {
		var oTable = $('#myTables8').dataTable( {
			 "sDom": 'R<"H"lfr>t<"F"ip>',
			 "bJQueryUI": true,
			 "sPaginationType": "full_numbers",
			 "iDisplayLength" :25
		});
	});
	function save_click(){
		form.action ='index.php?SIMPAN=SIMPAN';
		form.method = 'post';
		form.submit();		
		return true;
	}	
	function save_click2(){
		form.action ='index.php?SIMPAN2=SIMPAN2';
		form.method = 'post';
		form.submit();		
		return true;
	}
	function save_click5(){
		form.action ='index.php?SIMPAN5=SIMPAN5';
		form.method = 'post';
		form.submit();		
		return true;
	}
	function save_click6(){
		form.action ='index.php?SIMPAN6=SIMPAN6';
		form.method = 'post';
		form.submit();		
		return true;
	}
	function save_click7(){
		form.action ='index.php?SIMPAN7=SIMPAN7';
		form.method = 'post';
		form.submit();		
		return true;
	}
	function search_click(){
		form.action ='index.php?CARI=CARI';
		form.method = 'post';
		form.submit();		
		return true;
	}
</script>
</head>
<body>
<?php
	include "../include/config.php";
	include "../function/inc_function.php";
	$password   = isset($_REQUEST['password'])?$_REQUEST['password'] : '';	
	$start_date = isset($_REQUEST['start_date'])?$_REQUEST['start_date'] : get_startdate();
	$end_date   = isset($_REQUEST['end_date'])?$_REQUEST['end_date'] : get_enddate();
	$prod_code   = isset($_REQUEST['prod_code'])?$_REQUEST['prod_code'] : '';
	$month 		= substr($start_date,5,2);
	$year		= substr($start_date,0,4);
	$year_sto   = substr($end_date,0,4);
	$month_sto  = substr($end_date,5,2);
	if(isset($_REQUEST['SIMPAN'])=="SIMPAN" && $password=="scorp!s"){		
		$idcat   = 43;
		$idpilih = 43;
		$pilih 	 = $_REQUEST['pilih'];
		foreach ( $pilih as $i => $id){	
			$p_startdate 	= $_REQUEST['p_startdate'][$id];
			$p_enddate 		= $_REQUEST['p_enddate'][$id];
			$p_code 		= $_REQUEST['p_code'][$id];
			$p_tahun 		= $_REQUEST['p_tahun'][$id];
			if($p_code=="scorp!s"){
				mysql_query("update periode set p_startdate='$p_startdate', p_enddate='$p_enddate', p_tahun='$p_tahun' where id='$id'");
				#kedua
				if($id==1){
					mysql_query("update transfer_detail set tiheula='$p_startdate', pandeuri='$p_enddate' where id in(86,186,286,386,486)");
				}
			}
		}	
	}elseif(isset($_REQUEST['SIMPAN2'])=="SIMPAN2" && $password=="scorp!s"){		
		$idcat   = 44;
		$idpilih = 44;
		$pilih 	 = $_REQUEST['pilih'];
		foreach ( $pilih as $i => $id){	
			$stock_qty 		= $_REQUEST['stock_qty'][$id];
			$stock_loc 		= $_REQUEST['stock_loc'][$id];
			$stock_status 	= $_REQUEST['stock_status'][$id];
			mysql_query("update stock set stock_qty='$stock_qty', stock_loc='$stock_loc', stock_status='$stock_status' where id='$id'");
		}	
		$pilih2 	 = $_REQUEST['pilih2'];
		foreach ( $pilih2 as $i => $id2){	
			$stod_qtysaldo 	= $_REQUEST['stod_qtysaldo'][$id2];
			mysql_query("update stockopname_detail set stod_qtysaldo='$stod_qtysaldo', stod_qtysto='$stod_qtysaldo' where id='$id2'");
		}
		$start_date = $_REQUEST['start_date'];
		$end_date   = $_REQUEST['end_date'];
		$prod_code  = $_REQUEST['prod_code'];
	}elseif(isset($_REQUEST['SIMPAN5'])=="SIMPAN5" && $password=="scorp!s"){
		$idpilih = isset($_REQUEST['idpilih'])?$_REQUEST['idpilih'] : 10;
		$idcat   = isset($_REQUEST['idcat'])?$_REQUEST['idcat'] : 10;
		$stoh_no = getstono();
		#parameter for closing
		$month_start 	= substr($start_date,5,2);
		$year_start  	= substr($start_date,0,4);	
		$month_end	    = substr($end_date,5,2);
		$year_end	    = substr($end_date,0,4);
		if($month_end==12){
			$month_close = 1;
			$year_close  = $year_end+1;
		}else{
			$month_close = $month_end+1;
			$year_close  = $year_end;
		}
		$tanggal ="$year_close-$month_close-01";
		$year_sto   = substr($end_date,0,4);
		$month_sto  = substr($end_date,5,2);
		if($idcat==1 || $idcat==6 || $idcat==7 || $idcat==8){
			#insert header
			mysql_query("INSERT INTO stockopname SET stoh_no='$stoh_no', stoh_date='$end_date', stoh_cekno='-', 
							  stoh_cekdate='$end_date', stoh_pic='-', stoh_prodcat='$idcat', stoh_inputby='dev',
							  stoh_inputdate=now(), stoh_status=20");
			$data = mysql_fetch_object(mysql_query("SELECT MAX(id) id FROM stockopname"));
			$stoh_id = $data->id;	
			$qitem = mysql_query("SELECT id, prod_code FROM product WHERE prod_cat='$idcat'");
			while($ditem=mysql_fetch_array($qitem)){
				$cstockexbc = get_stockexbc($start_date,$end_date,$ditem['id'],$inv);
				$cstockmtsi = get_stock($start_date,$end_date,$month_start,$year_start,$ditem['id'],$idcat,$inv);
				if($cstockexbc == $cstockmtsi){
					$qd = mysql_query("SELECT b.prodid id, b.doctype, b.docno, b.docdate, b.inqty, b.outqty, (b.inqty-b.outqty) endqty, b.price, b.prod_code, 
									b.prod_name, b.prod_sat, b.curr FROM
									(SELECT a.prodid, a.doctype, a.docno, a.docdate, SUM(a.inqty) inqty, SUM(a.outqty) outqty, a.price, b.prod_code, b.prod_name, b.prod_sat, a.curr  FROM
									(SELECT stock_prodid prodid, ifnull(stock_doctype,0) doctype, ifnull(stock_docno,0) docno, stock_docdate docdate, stock_qty inqty, 0 outqty, (stock_price/stock_qty) price, 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, ifnull(a.inch_doctype,0) doctype , ifnull(a.inch_docno,0) docno, a.inch_docdate docdate, b.incd_qty inqty, 0 outqty, 
									(b.incd_price/b.incd_qty) price, 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, ifnull(a.trfd_doctype,0) doctype , ifnull(a.trfd_docno,0) docno, a.trfd_docdate docdate, 0 inqty, a.trfd_qty outqty, 0 price, '' 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, ifnull(a.trfd_doctype,0) doctype , ifnull(a.trfd_docno,0) docno, a.trfd_docdate docdate, a.trfd_qty inqty, 0 outqty, 0 price, '' 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, ifnull(a.jenis_dokpab_asal,0) doctype, ifnull(a.no_dokpab_asal,0) docno, a.tgl_dokpab_asal docdate, 0 inqty, a.jumlah outqty, 0 price, '' curr 
									FROM outgoing_dokasal a
									INNER JOIN outgoing b ON b.id=a.outh_hid            
									WHERE b.outh_status=20 AND a.log_status=20 AND b.outh_docdate BETWEEN '$start_date' AND '$end_date'
									UNION ALL
									SELECT a.adjd_prodid prodid, a.adjd_doctype doctype , a.adjd_docno docno, a.adjd_docdate docdate, adjd_qty inqty, 0 outqty, 0 price, '' 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
									WHERE b.id='$ditem[id]' GROUP BY a.prodid, a.doctype, a.docno) b
									WHERE (b.inqty-b.outqty) > 0 ORDER BY b.docno, b.prod_code ASC;");	
					while($dt=mysql_fetch_object($qd)){	
						$qty = $dt->endqty;
						$price = $qty * $dt->price;
						$doctype =$dt->doctype;
						$docno =$dt->docno;
						$docdate=$dt->docdate;	
						if($qty>0){
							#insert detail setockopname
							mysql_query("INSERT INTO stockopname_detail SET stod_hid='$stoh_id', stod_prodid='$dt->id', stod_qtysaldo='$qty',stod_qtysto='$qty'");
							#Insert stock log
							mysql_query("INSERT INTO stock SET stock_stoid='$stoh_id', stock_date='$tanggal', stock_prodid='$dt->id', stock_qty='$qty', stock_status=10, stock_price='$price',
							stock_doctype='$doctype', stock_docno='$docno', stock_docdate='$docdate', stock_loc='1', stock_currency='$dt->curr'");
							#display
							echo "Data dengan kode barang : $dt->prod_code, qty :$qty sukses <br>";
						}
					}
				}else{
					#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='$ditem[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='$ditem[id]' ORDER BY a.id DESC LIMIT 0,1"));
					#validasi		 
					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'];}
					if($cstockmtsi>0){
						#insert detail setockopname
						mysql_query("INSERT INTO stockopname_detail SET stod_hid='$stoh_id', stod_prodid='$ditem[id]', stod_qtysaldo='$cstockmtsi',stod_qtysto='$cstockmtsi'");
						#Insert stock log
						mysql_query("INSERT INTO stock SET stock_stoid='$stoh_id', stock_date='$tanggal', stock_prodid='$ditem[id]', stock_qty='$cstockmtsi', stock_status=10, stock_price=0,
						stock_doctype='$doctype', stock_docno='$docno', stock_docdate='$docdate', stock_loc='1', stock_currency=''");
						#display
						echo "Data dengan kode barang : $ditem[prod_code], qty :$cstockmtsi sukses <br>";
					}
				}
			}			
			#Insert History User
			mysql_query("INSERT INTO user_activity SET user_id='dev', user_act='Menambahkan data stock opname No. $stoh_no', user_date=now(), user_time=now()");
			#konfirmasi
			echo "Closing BB & BP sukses";
		}elseif($idcat==2){ 
			#insert header
			mysql_query("INSERT INTO stockopname SET stoh_no='$stoh_no', stoh_date='$end_date', stoh_cekno='-', 
							  stoh_cekdate='$end_date', stoh_pic='-', stoh_prodcat='$idcat', stoh_inputby='dev',
							  stoh_inputdate=now(), stoh_status=20");
			$data = mysql_fetch_object(mysql_query("SELECT MAX(id) id FROM stockopname"));
			$stoh_id = $data->id;				  
			#Query detail	
			$qd =mysql_query("SELECT a.id, a.prod_code, (ifnull(b.qty,0) + ifnull(c.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 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     
				WHERE a.prod_cat IN(2) ORDER BY a.prod_code ASC");
			while($dt=mysql_fetch_object($qd)){	
				$qty = $dt->end_qty;
				#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'];}
				$price =0;
				if($qty>0){
					#insert detail setockopname
					mysql_query("INSERT INTO stockopname_detail SET stod_hid='$stoh_id', stod_prodid='$dt->id', stod_qtysaldo='$qty',stod_qtysto='$qty'");
					#Insert stock log
					mysql_query("INSERT INTO stock SET stock_stoid='$stoh_id', stock_date='$tanggal', stock_prodid='$dt->id', stock_qty='$qty', stock_status=10, stock_price='$price',
					stock_doctype='$doctype', stock_docno='$docno', stock_docdate='$docdate', stock_loc='4'");
					#display
					echo "Data dengan kode barang : $dt->prod_code, qty :$qty sukses <br>";
				}
			}
			#Insert History User
			mysql_query("INSERT INTO user_activity SET user_id='dev', user_act='Menambahkan data stock opname No. $stoh_no', user_date=now(), user_time=now()");
			#konfirmasi
			echo "Closing BM sukses";			
		}elseif($idcat==3){
			#insert header
			mysql_query("INSERT INTO stockopname SET stoh_no='$stoh_no', stoh_date='$end_date', stoh_cekno='-', 
							  stoh_cekdate='$end_date', stoh_pic='-', stoh_prodcat='$idcat', stoh_inputby='dev',
							  stoh_inputdate=now(), stoh_status=20");
			$data = mysql_fetch_object(mysql_query("SELECT MAX(id) id FROM stockopname"));
			$stoh_id = $data->id;				  
			#Query detail	
			$qd =mysql_query("SELECT a.id, a.prod_code, (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)) 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 (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
				WHERE a.prod_cat IN(3,5) ORDER BY a.prod_code ASC");
			while($dt=mysql_fetch_object($qd)){	
				$qty = $dt->end_qty;
				$price =0;
				$doctype ="";
				$docno ="";
				$docdate="";	
				if($qty>0){
					#insert detail setockopname
					mysql_query("INSERT INTO stockopname_detail SET stod_hid='$stoh_id', stod_prodid='$dt->id', stod_qtysaldo='$qty',stod_qtysto='$qty'");
					#Insert stock log
					mysql_query("INSERT INTO stock SET stock_stoid='$stoh_id', stock_date='$tanggal', stock_prodid='$dt->id', stock_qty='$qty', stock_status=10, stock_price='$price',
					stock_doctype='$doctype', stock_docno='$docno', stock_docdate='$docdate', stock_loc='3'");
					#display
					echo "Data dengan kode barang : $dt->prod_code, qty :$qty sukses <br>";
				}
			}
			#Insert History User
			mysql_query("INSERT INTO user_activity SET user_id='dev', user_act='Menambahkan data stock opname No. $stoh_no', user_date=now(), user_time=now()");
			#konfirmasi
			echo "Closing BJ sukses";
		}elseif($idcat==4){
			#insert header
			mysql_query("INSERT INTO stockopname SET stoh_no='$stoh_no', stoh_date='$end_date', stoh_cekno='-', 
							  stoh_cekdate='$end_date', stoh_pic='-', stoh_prodcat='$idcat', stoh_inputby='dev',
							  stoh_inputdate=now(), stoh_status=20");
			$data = mysql_fetch_object(mysql_query("SELECT MAX(id) id FROM stockopname"));
			$stoh_id = $data->id;				  
			#Query detail	
			$qd =mysql_query("SELECT a.id, a.prod_code, (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");
			while($dt=mysql_fetch_object($qd)){	
				$qty = $dt->end_qty;
				$price =0;
				$doctype ="";
				$docno ="";
				$docdate="";	
				if($qty>0){
					#insert detail setockopname
					mysql_query("INSERT INTO stockopname_detail SET stod_hid='$stoh_id', stod_prodid='$dt->id', stod_qtysaldo='$qty',stod_qtysto='$qty'");
					#Insert stock log
					mysql_query("INSERT INTO stock SET stock_stoid='$stoh_id', stock_date='$tanggal', stock_prodid='$dt->id', stock_qty='$qty', stock_status=10, stock_price='$price',
					stock_doctype='$doctype', stock_docno='$docno', stock_docdate='$docdate', stock_loc='3'");
					#display
					echo "Data dengan kode barang : $dt->prod_code, qty :$qty sukses <br>";
				}
			}
			#Insert History User
			mysql_query("INSERT INTO user_activity SET user_id='dev', user_act='Menambahkan data stock opname No. $stoh_no', user_date=now(), user_time=now()");
			#konfirmasi
			echo "Closing BS sukses";
		}elseif($idcat==5){			
			#insert header
			mysql_query("INSERT INTO stockopname SET stoh_no='$stoh_no', stoh_date='$end_date', stoh_cekno='-', 
							  stoh_cekdate='$end_date', stoh_pic='-', stoh_prodcat='$idcat', stoh_inputby='dev',
							  stoh_inputdate=now(), stoh_status=20");
			$data = mysql_fetch_object(mysql_query("SELECT MAX(id) id FROM stockopname"));
			$stoh_id = $data->id;				  
			#Query detail			
			$qd = mysql_query("SELECT z.id, z.end_qty, z.prod_code  FROM
						(SELECT a.id, a.prod_code,
                              ((ifnull(b.stock_qty,0) + ifnull(c.trfgd_qty,0) + ifnull(f.wobj_qty,0) + ifnull(i.rtnqc_qty,0) + ifnull(k.kvp_qty,0) + ifnull(o.kvm2_qty,0)) 
                             - (ifnull(h.trfqc_qty,0) + ifnull(e.wobb_qty,0) + ifnull(l.kvm_qty,0) + ifnull(m.wobbbj_qty,0) + ifnull(n.kvp2_qty,0) + ifnull(d.rtngd_qty,0) + ifnull(p.wonbd_qty,0))) end_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,2,3,4,5,6,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) ) as z
						WHERE z.end_qty > 0");
			while($dd=mysql_fetch_array($qd)){					
				$price  = get_pricewip2($dd['id'],$start_date,$end_date,$inv);
				#insert detail setockopname
				mysql_query("INSERT INTO stockopname_detail SET stod_hid='$stoh_id', stod_prodid='$dd[id]', stod_qtysaldo='$dd[end_qty]',stod_qtysto='$dd[end_qty]'");
				#Insert stock log
				mysql_query("INSERT INTO stock SET stock_stoid='$stoh_id', stock_date='$tanggal', stock_prodid='$dd[id]', stock_qty='$dd[end_qty]', stock_status=10, stock_price='$price',
				stock_doctype='', stock_docno='', stock_docdate='', stock_loc='2'");
				#display
				echo "Data dengan kode barang : $dd[prod_code], qty :$dd[end_qty] sukses <br>";
			}
			#Insert History User
			mysql_query("INSERT INTO user_activity SET user_id='dev', user_act='Menambahkan data stock opname No. $stoh_no', user_date=now(), user_time=now()");
			#konfirmasi
			echo "Closing WIP sukses";
		}
	}elseif(isset($_REQUEST['SIMPAN6'])=="SIMPAN6" && $password=="scorp!s"){
		$idcat   = 46;
		$idpilih = 46;
		$pilih6 	 = $_REQUEST['pilih6'];
		foreach ( $pilih6 as $i => $id6){	
			#hapus stockopname
			mysql_query("update stockopname set stoh_status=100 where id='$id6'");
			#Hapus stock
			mysql_query("update stock set stock_status=100 where stock_stoid='$id6'");
		}	
	}elseif(isset($_REQUEST['SIMPAN7'])=="SIMPAN7" && $password=="scorp!s"){
		$idcat   = 47;
		$idpilih = 47;
		$month_start 	= substr($start_date,5,2);
		$year_start  	= substr($start_date,0,4);	
		$month_end	    = substr($end_date,5,2);
		$year_end	    = substr($end_date,0,4);
		if($month_end==12){
			$month_close = 1;
			$year_close  = $year_end+1;
		}else{
			$month_close = $month_end+1;
			$year_close  = $year_end;
		}
		$tanggal ="$year_close-$month_close-01";
		$year_sto   = substr($end_date,0,4);
		$month_sto  = substr($end_date,5,2);
		#hapus closing
		mysql_query("UPDATE stock_exbc SET stock_status=100 WHERE stock_date='$tanggal' AND stock_flag='CLOSING'");
		#insert
		$q7 = mysql_query("SELECT b.prodid, b.doctype, b.docno, b.docdate, b.begqty, b.inqty, b.outqty, ((b.begqty+b.inqty)-b.outqty) endqty, b.price, b.prod_code, 
			b.prod_name, b.prod_sat, b.curr, b.no_kontrak, b.tgl_kontrak, b.no_aju, b.no_urut FROM
			(SELECT a.prodid, a.doctype, a.docno, a.docdate, SUM(a.begqty) begqty, SUM(a.inqty) inqty, SUM(a.outqty) outqty, a.price, b.prod_code, b.prod_name, b.prod_sat, a.curr, a.no_kontrak, a.tgl_kontrak, a.no_aju, a.no_urut FROM
			(SELECT stock_prodid prodid, stock_doctype doctype, stock_docno docno, stock_docdate docdate, stock_qty begqty, 0 inqty, 0 outqty, stock_price price, stock_currency curr, no_kontrak, tgl_kontrak, stock_noaju no_aju, stock_nourut no_urut 
			FROM stock_exbc WHERE stock_status=10 AND MONTH(stock_date)='$month_start' AND YEAR(stock_date)='$year_start'
			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, CASE WHEN a.valuta='IDR' THEN (b.incd_price/b.incd_qty) ELSE (b.incd_price_usd/b.incd_qty) END AS price, a.valuta curr,
			a.no_kontrak, a.tgl_kontrak, a.no_aju no_aju, b.no_urut  
			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.obcd_prodid prodid, a.obcd_doctype doctype , a.obcd_docno docno, a.obcd_docdate docdate, 0 begqty, 0 inqty, a.obcd_qty outqty, 0 price, '' curr, '' no_kontrak, '' tgl_kontrak, '-' no_aju, 0 no_urut  
			FROM outexbc_detail a
			INNER JOIN outexbc b ON b.id=a.obcd_hid
			WHERE b.obc_status=20 AND b.obc_date BETWEEN '$start_date' AND '$end_date'
			) a
			INNER JOIN product b ON b.id=a.prodid
			WHERE b.prod_cat IN(1,6,7) GROUP BY a.prodid, a.doctype, a.docno, a.docdate) b
			ORDER BY b.docno, b.docdate, b.prod_code ASC");
		while($d7=mysql_fetch_array($q7)){	
			if($d7['endqty']>0){
				mysql_query("INSERT INTO stock_exbc SET stock_prodid='$d7[prodid]', stock_date='$tanggal', stock_qty='$d7[endqty]', stock_status=10, stock_doctype='$d7[doctype]',
				stock_docno='$d7[docno]', stock_docdate='$d7[docdate]', stock_price='$d7[price]', stock_currency='$d7[curr]', stock_noaju='$d7[no_aju]',
				stock_nourut='$d7[no_urut]', no_kontrak='$d7[no_kontrak]', tgl_kontrak='$d7[tgl_kontrak]', stock_flag='CLOSING'");
			}				 
		}	
		#konfirmasi
		echo "Closing EXBC sukses";
	}else{
		$idpilih = isset($_REQUEST['idpilih'])?$_REQUEST['idpilih'] : 10;
		$idcat   = isset($_REQUEST['idcat'])?$_REQUEST['idcat'] : 10;
	}
?>
<form name="form" action="index.php" method="post" enctype="multipart/form-data">
  <table width="100%">
	<tr>
		<td>Pilih</td>
		<td>
		<input type="password" name="password" size="10" value="<?=$password;?>">
		<?php if($password=="scorp!s"){?>
		 <select name="idpilih">
		  <option value="10" selected="selected">PILIH</option> 
			<?php
			  $dataSql = mysql_query("SELECT id, m_desc FROM maintenance WHERE m_status=4");
			  while ($dataRow = mysql_fetch_array($dataSql)) {
				if ($dataRow['id']== $idpilih) {
					$cek = " selected";
				} else { $cek=""; }
				echo "<option value='$dataRow[id]' $cek>$dataRow[m_desc]</option>";
			  }
			  $sqlData ="";
			?>
		 </select>
		 -
		 <select name="idcat">
		  <option value="10" selected="selected">PILIH</option> 
			<?php
			  $dataSql = mysql_query("SELECT id, prod_cat FROM product_category");
			  while ($dataRow = mysql_fetch_array($dataSql)) {
				if ($dataRow['id']== $idcat) {
					$cek = " selected";
				} else { $cek=""; }
				echo "<option value='$dataRow[id]' $cek>$dataRow[prod_cat]</option>";
			  }
			  $sqlData ="";
			?>
		 </select>
		 <input type="text" name="start_date" value='<?=$start_date;?>' size="10"> ~ 
		 <input type="text" name="end_date" value='<?=$end_date;?>' size="10">
		 Kode Barang 
		 <input type="text" name="prod_code" value='<?=$prod_code;?>' size="30">
		 <?php }?>
		 <input type="button" name="cari" value="CARI" onClick="search_click()">
		
		</td>
	</tr>
  </table>
  <?php if($idpilih==40 && $password=="scorp!s"){?>
  <table id="myTables" class="display" cellpadding="0" cellspacing="0">
	<thead>
	<tr>
		<th>No.</th>
		<th>User ID</th>
		<th>Aktivitas</th>
		<th>Tanggal</th>
		<th>Jam</th>
	</tr>
	</thead>
	<tbody>
	<?php		
	  	$no=1;
		$qd = mysql_query("SELECT * FROM user_activity WHERE user_date BETWEEN '$start_date' AND '$end_date' ORDER BY id DESC");
		while($dd=mysql_fetch_object($qd)){		
		?>
		  <tr onMouseOver="setTrColor(this, '#FF9900');"> 
			<td align="center"><?=$no;?>.</td>
			<td align="center"><?=$dd->user_id;?></td>
			<td align="left"><?=$dd->user_act;?></td>
			<td align="center"><?=$dd->user_date;?></td>
			<td align="center"><?=$dd->user_time;?></td>
		  </tr>
      <?php $no++; }  ?>	
	  </tbody>
  </table>
  <?php }else if($idpilih==41 && $password=="scorp!s"){?>
  <table id="myTables2" class="display" cellpadding="0" cellspacing="0">
	<thead>
	<tr>
		<th>No.</th>
		<th>Kode Barang</th>
		<th>Nama Barang</th>
		<th>Tipe/Style</th>
		<th>Size</th>
		<th>Warna</th>
		<th>Satuan</th>
		<th>Kategori</th>
		<th>Status</th>
		<th>ID</th>
	</tr>
	</thead>
	<tbody>
	<?php		
	  	$no=1;
		$qd = mysql_query("SELECT a.*, b.prod_cat FROM product a INNER JOIN product_category b ON b.id=a.prod_cat ORDER BY a.prod_code ASC");
		while($dd=mysql_fetch_object($qd)){	
			if($dd->prod_status==10){
				$status="ACTIVE";
			}else{
				$status="DELETED";
			}
		?> 
		  <tr onMouseOver="setTrColor(this, '#FF9900');">
			<td align="center"><?=$no;?>.</td>
			<td align="left"><?=$dd->prod_code;?></td>
			<td align="left"><?=$dd->prod_name;?></td>
			<td align="left"><?=$dd->prod_type;?></td>
			<td align="left"><?=$dd->prod_spec;?></td>
			<td align="left"><?=$dd->prod_color;?></td>
			<td align="center"><?=$dd->prod_sat;?></td>
			<td align="left"><?=$dd->prod_cat;?></td>
			<td align="center"><?=$status;?></td>	
			<td align="center"><?=$dd->id;?></td>			
		  </tr>
      <?php $no++; }  ?>	
	  </tbody>
  </table>
  <?php }else if($idpilih==42 && $password=="scorp!s"){?>
	<?php if($idcat==1){?>
		  <table id="myTables3" class="display" cellpadding="0" cellspacing="0">
			<thead>	
			  <tr>
				<th>No.</th>
				<th>Kode Barang</th>
				<th>Nama Barang</th>
				<th>Satuan</th>
				<th>Saldo Awal</th>
				<th>Pemasukan</th>
				<th>Pengeluaran</th>
				<th>Penyesuain</th>
				<th>Saldo Akhir</th>
				<th>Stockopname</th>
				<th>Selisih</th>
				<th>Keterangan</th>
			  </tr>
			</thead>
			<tbody>
		  <?php $no=1;
			$val = valstockopname($month_sto,$year_sto,1);
			if(empty($val)){$sto =2;}else{$sto =1;}	
			$qd = mysql_query("CALL spvw_mutasibb('$start_date','$end_date','$month','$year','$month_sto','$year_sto','$sto');");
			while($dd=mysql_fetch_object($qd)){	
				$end_qty = number_format($dd->beg_qty,2,'.','')+number_format($dd->in_qty,2,'.','')-number_format($dd->out_qty,2,'.','')+number_format($dd->adj_qty,2,'.','');
				if($sto==1){
					if((number_format($dd->stock_opname,2,'.','')- number_format($end_qty,2,'.','')) > 0){
						$ket = "Selisih Lebih";
					}elseif((number_format($dd->stock_opname,2,'.','')- number_format($end_qty,2,'.','')) < 0){
						$ket = "Selisih Kurang";
					}else{
						$ket = "Sesuai";
					}	
					$dif_qty = number_format($dd->stock_opname,2,'.','') - number_format($end_qty,2,'.','');
				}elseif($sto==2){
					if(number_format($end_qty,2,'.','') > 0){
						$ket = "Normal";
					}elseif(number_format($end_qty,2,'.','') < 0){
						$ket = "Abnormal";
					}else{
						$ket = "Normal";
					}	
					$dif_qty = 0;
				}
			?> 
			 <tr onMouseOver="setTrColor(this, '#FF9900');">
				<td align="center"><?=$no;?>.</td>
				<td align="left"><?=$dd->prod_code;?></td>
				<td align="left"><?=$dd->prod_name;?></td>
				<td align="center"><?=$dd->prod_sat;?></td>
				<td align="right"><?=number_format($dd->beg_qty,2,'.',',');?></td>
				<td align="right"><?=number_format($dd->in_qty,2,'.',',');?></td>
				<td align="right"><?=number_format($dd->out_qty,2,'.',',');?></td>
				<td align="right"><?=number_format($dd->adj_qty,2,'.',',');?></td>
				<td align="right"><?=number_format($end_qty,2,'.',',');?></td>
				<td align="right"><?=number_format($dd->stock_opname,2,'.',',');?></td>
				<td align="right"><?=number_format($dif_qty,2,'.',',');?></td>
				<td align="left"><?=$ket;?></td>
			  </tr>	  
		  <?php $no++; }  ?> 
		  </tbody> 
		</table>
	<?php }else if($idcat==2){?>	
		<table id="myTables4" class="display" cellpadding="0" cellspacing="0">
			<thead>	
			  <tr>
				<th>No.</th>
				<th>Kode Barang</th>
				<th>Nama Barang</th>
				<th>Satuan</th>
				<th>Saldo Awal</th>
				<th>Pemasukan</th>
				<th>Pengeluaran</th>
				<th>Penyesuain</th>
				<th>Saldo Akhir</th>
				<th>Stockopname</th>
				<th>Selisih</th>
				<th>Keterangan</th>
			  </tr>
			</thead>
			<tbody>
		  <?php $no=1;
			$val = valstockopname($month_sto,$year_sto,1);
			if(empty($val)){$sto =2;}else{$sto =1;}	
			$qd = mysql_query("CALL spvw_mutasibm('$start_date','$end_date','$month','$year','$month_sto','$year_sto','$sto');");
			while($dd=mysql_fetch_object($qd)){		
				$end_qty = number_format($dd->beg_qty,2,'.','')+number_format($dd->in_qty,2,'.','')-number_format($dd->out_qty,2,'.','')+number_format($dd->adj_qty,2,'.','');
				if($sto==1){
					if((number_format($dd->stock_opname,2,'.','')- number_format($end_qty,2,'.','')) > 0){
						$ket = "Selisih Lebih";
					}elseif((number_format($dd->stock_opname,2,'.','')- number_format($end_qty,2,'.','')) < 0){
						$ket = "Selisih Kurang";
					}else{
						$ket = "Sesuai";
					}	
					$dif_qty = number_format($dd->stock_opname,2,'.','') - number_format($end_qty,2,'.','');
				}elseif($sto==2){
					if(number_format($end_qty,2,'.','') > 0){
						$ket = "Normal";
					}elseif(number_format($end_qty,2,'.','') < 0){
						$ket = "Abnormal";
					}else{
						$ket = "Normal";
					}	
					$dif_qty = 0;
				}
			?> 
			 <tr onMouseOver="setTrColor(this, '#FF9900');">
				<td align="center"><?=$no;?>.</td>
				<td align="left"><?=$dd->prod_code;?></td>
				<td align="left"><?=$dd->prod_name;?></td>
				<td align="center"><?=$dd->prod_sat;?></td>
				<td align="right"><?=number_format($dd->beg_qty,2,'.',',');?></td>
				<td align="right"><?=number_format($dd->in_qty,2,'.',',');?></td>
				<td align="right"><?=number_format($dd->out_qty,2,'.',',');?></td>
				<td align="right"><?=number_format($dd->adj_qty,2,'.',',');?></td>
				<td align="right"><?=number_format($end_qty,2,'.',',');?></td>
				<td align="right"><?=number_format($dd->stock_opname,2,'.',',');?></td>
				<td align="right"><?=number_format($dif_qty,2,'.',',');?></td>
				<td align="left"><?=$ket;?></td>
			  </tr>	  
		  <?php $no++; }  ?> 
		  </tbody> 
		</table>
	<?php }else if($idcat==3){?>
		<table id="myTables5" class="display" cellpadding="0" cellspacing="0">
			<thead>	
			  <tr>
				<th>No.</th>
				<th>Kode Barang</th>
				<th>Nama Barang</th>
				<th>Satuan</th>
				<th>Saldo Awal</th>
				<th>Pemasukan</th>
				<th>Pengeluaran</th>
				<th>Penyesuain</th>
				<th>Saldo Akhir</th>
				<th>Stockopname</th>
				<th>Selisih</th>
				<th>Keterangan</th>
			  </tr>
			</thead>
			<tbody>
		  <?php $no=1;
			$val = valstockopname($month_sto,$year_sto,1);
			if(empty($val)){$sto =2;}else{$sto =1;}	
			$qd = mysql_query("CALL spvw_mutasibj('$start_date','$end_date','$month','$year','$month_sto','$year_sto','$sto');");
			while($dd=mysql_fetch_object($qd)){		
				$end_qty = number_format($dd->beg_qty,2,'.','')+number_format($dd->in_qty,2,'.','')-number_format($dd->out_qty,2,'.','')+number_format($dd->adj_qty,2,'.','');
				if($sto==1){
					if((number_format($dd->stock_opname,2,'.','')- number_format($end_qty,2,'.','')) > 0){
						$ket = "Selisih Lebih";
					}elseif((number_format($dd->stock_opname,2,'.','')- number_format($end_qty,2,'.','')) < 0){
						$ket = "Selisih Kurang";
					}else{
						$ket = "Sesuai";
					}	
					$dif_qty = number_format($dd->stock_opname,2,'.','') - number_format($end_qty,2,'.','');
				}elseif($sto==2){
					if(number_format($end_qty,2,'.','') > 0){
						$ket = "Normal";
					}elseif(number_format($end_qty,2,'.','') < 0){
						$ket = "Abnormal";
					}else{
						$ket = "Normal";
					}	
					$dif_qty = 0;
				}
			?> 
			 <tr onMouseOver="setTrColor(this, '#FF9900');">
				<td align="center"><?=$no;?>.</td>
				<td align="left"><?=$dd->prod_code;?></td>
				<td align="left"><?=$dd->prod_name;?></td>
				<td align="center"><?=$dd->prod_sat;?></td>
				<td align="right"><?=number_format($dd->beg_qty,2,'.',',');?></td>
				<td align="right"><?=number_format($dd->in_qty,2,'.',',');?></td>
				<td align="right"><?=number_format($dd->out_qty,2,'.',',');?></td>
				<td align="right"><?=number_format($dd->adj_qty,2,'.',',');?></td>
				<td align="right"><?=number_format($end_qty,2,'.',',');?></td>
				<td align="right"><?=number_format($dd->stock_opname,2,'.',',');?></td>
				<td align="right"><?=number_format($dif_qty,2,'.',',');?></td>
				<td align="left"><?=$ket;?></td>
			  </tr>	  
		  <?php $no++; }  ?> 
		  </tbody> 
		</table>
	<?php }else if($idcat==4){?>
		<table id="myTables6" class="display" cellpadding="0" cellspacing="0">
			<thead>	
			  <tr>
				<th>No.</th>
				<th>Kode Barang</th>
				<th>Nama Barang</th>
				<th>Satuan</th>
				<th>Saldo Awal</th>
				<th>Pemasukan</th>
				<th>Pengeluaran</th>
				<th>Penyesuain</th>
				<th>Saldo Akhir</th>
				<th>Stockopname</th>
				<th>Selisih</th>
				<th>Keterangan</th>
			  </tr>
			</thead>
			<tbody>
		  <?php $no=1;
			$val = valstockopname($month_sto,$year_sto,1);
			if(empty($val)){$sto =2;}else{$sto =1;}	
			$qd = mysql_query("CALL spvw_mutasibs('$start_date','$end_date','$month','$year','$month_sto','$year_sto','$sto');");
			while($dd=mysql_fetch_object($qd)){		
				$end_qty = number_format($dd->beg_qty,2,'.','')+number_format($dd->in_qty,2,'.','')-number_format($dd->out_qty,2,'.','')+number_format($dd->adj_qty,2,'.','');
				if($sto==1){
					if((number_format($dd->stock_opname,2,'.','')- number_format($end_qty,2,'.','')) > 0){
						$ket = "Selisih Lebih";
					}elseif((number_format($dd->stock_opname,2,'.','')- number_format($end_qty,2,'.','')) < 0){
						$ket = "Selisih Kurang";
					}else{
						$ket = "Sesuai";
					}	
					$dif_qty = number_format($dd->stock_opname,2,'.','') - number_format($end_qty,2,'.','');
				}elseif($sto==2){
					if(number_format($end_qty,2,'.','') > 0){
						$ket = "Normal";
					}elseif(number_format($end_qty,2,'.','') < 0){
						$ket = "Abnormal";
					}else{
						$ket = "Normal";
					}	
					$dif_qty = 0;
				}
			?> 
			 <tr onMouseOver="setTrColor(this, '#FF9900');">
				<td align="center"><?=$no;?>.</td>
				<td align="left"><?=$dd->prod_code;?></td>
				<td align="left"><?=$dd->prod_name;?></td>
				<td align="center"><?=$dd->prod_sat;?></td>
				<td align="right"><?=number_format($dd->beg_qty,2,'.',',');?></td>
				<td align="right"><?=number_format($dd->in_qty,2,'.',',');?></td>
				<td align="right"><?=number_format($dd->out_qty,2,'.',',');?></td>
				<td align="right"><?=number_format($dd->adj_qty,2,'.',',');?></td>
				<td align="right"><?=number_format($end_qty,2,'.',',');?></td>
				<td align="right"><?=number_format($dd->stock_opname,2,'.',',');?></td>
				<td align="right"><?=number_format($dif_qty,2,'.',',');?></td>
				<td align="left"><?=$ket;?></td>
			  </tr>	  
		  <?php $no++; }  ?> 
		  </tbody> 
		</table>
	<?php }else if($idcat==5){?>
		<table id="myTables" class="display" cellpadding="0" cellspacing="0">
		  <tr height="30px;">
			<th rowspan="2">No</th>
			<th rowspan="2">Kode Barang</th>
			<th rowspan="2">Nama Barang</th>
			<th rowspan="2">Satuan</th>
			<th colspan="4">Jumlah</th>
			<!--<th rowspan="2">Harga</th>-->
		  </tr>
		  <tr>
			<th>Awal</th>
			<th>Masuk</th>
			<th>Keluar</th>
			<th>Stok</th>
		  </tr>
		  <?php
			$gtprice=0;
			$qh = mysql_query("SELECT * FROM product_category WHERE id IN(1,3,6,4,5) ORDER BY prod_cat ASC");
			while($dh=mysql_fetch_array($qh)){
		  ?>
		  <tr bgcolor="#CCFFFF">
			<td colspan="9">KATEGORI : <?=$dh['prod_cat'];?> </td>
		  </tr>
		  <?php
			$no=1; $price=0;
			$qd = mysql_query("SELECT * FROM product WHERE prod_cat='$dh[id]' ORDER BY prod_code ASC");
			while($dd=mysql_fetch_array($qd)){		
				#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='$start_date' 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, SUM(trfd_price) price 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 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 '$start_date' AND '$end_date' AND b.trf_status=20 AND b.trf_category=3 AND a.trfd_prodid='$dd[id]' 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 '$start_date' AND '$end_date' AND b.wo_status=20 AND a.wod_prodid='$dd[id]' 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 '$start_date' 
						AND '$end_date' 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 '$start_date' AND '$end_date' 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 '$start_date' AND '$end_date' 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 '$start_date' AND '$end_date' AND b.trf_status=20 AND b.trf_category=6 AND a.trfd_prodid='$dd[id]' 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 '$start_date' AND '$end_date' AND b.scrd_prodid='$dd[id]' 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 '$start_date' AND '$end_date' 
						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 '$start_date' AND '$end_date' 	AND a.kvd_prodid='$dd[id]'	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 '$start_date' AND '$end_date' AND b.wo_status=20 AND a.wod_prodid='$dd[id]' 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_pricewip1($dd['id'],$start_date,$end_date,$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_pricewip2($dd['id'],$start_date,$end_date,$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_pricewip2($dd['id'],$start_date,$end_date,$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;
				}
				if($stock !=0){	   
			?> 
			 <tr onMouseOver="setTrColor(this, '#FF9900');">
				<td align="center"><?=$no;?>.</td>
				<td align="left"><?=$dd['prod_code'];?></td>
				<td align="left"><?=$dd['prod_name'];?></td>
				<td align="center"><?=$dd['prod_sat'];?></td>
				<td align="right"><?=number_format($stk,2,'.',',');?></td>
				<td align="right"><?=number_format($in,2,'.',',');?></td>
				<td align="right"><?=number_format($out,2,'.',',');?></td>
				<td align="right"><?=number_format($stock,2,'.',',');?></td>
				<!--<td align="right"><?=number_format($tprice,2,'.',',');?></td>-->
			  </tr>	  
		  <?php $no++; } } } ?> 
		</table>	
	<?php }?>
  <?php }else if($idpilih==43 && $password=="scorp!s"){?>
  <table width="30%">
	<tr>
		<td colspan="3" align="right"><input type="button" name="simpan" value="SIMPAN" onClick="save_click()" /></td>
	</tr>
	<tr>
		<td>Tanggal Awal</td>
		<td>Tanggal Akhir</td>
		<td>Tahun Acct</td>
		<td>Kode</td>
	</tr>	
	<?php $q=mysql_query("SELECT * FROM periode ORDER BY id ASC");
		while($data=mysql_fetch_array($q)){
	?>
	<tr>
		<td>
			<input type="hidden" name="pilih[<?=$data['id'];?>]" value="<?=$data['id'];?>" />
			<input type="text" name="p_startdate[<?=$data['id'];?>]" value="<?=$data['p_startdate'];?>" size="9" />
		</td>
		<td>
			<input type="text" name="p_enddate[<?=$data['id'];?>]" value="<?=$data['p_enddate'];?>" size="9" />
		</td>
		<td>
			<input type="text" name="p_tahun[<?=$data['id'];?>]" value="<?=$data['p_tahun'];?>" size="4" />
		</td>
		<td>
			<input type="text" name="p_code[<?=$data['id'];?>]" value="**********" size="9" />
		</td>
	</tr>
	<?php } ?>
  </table>
   <?php }else if($idpilih==44 && $password=="scorp!s"){?>
  <table width="100%">
	<tr>
		<td colspan="3" align="left">
			<input type="button" name="simpan" value="SIMPAN" onClick="save_click2()" />
		</td>
	</tr>
	<tr>
		<td colspan="3" align="left">
			-- STOCK CLOSING --
		</td>
	</tr>
	<tr>
		<td>Kode Barang</td>
		<td>Nama Barang</td>
		<td>Satuan</td>
		<td>Jumlah</td>
		<td>Lokasi</td>
		<td>Status</td>
	</tr>	
	<?php $q=mysql_query("select a.*, b.prod_code, b.prod_name, b.prod_sat FROM stock a INNER JOIN product b ON b.id=a.stock_prodid
	         WHERE a.stock_date='$start_date' AND a.stock_status=10 AND b.prod_code='$prod_code'");
		while($dh=mysql_fetch_array($q)){
	?>
	<tr>
		<td>
			<input type="hidden" name="pilih[<?=$dh['id'];?>]" value="<?=$dh['id'];?>" />
			<?=$dh['prod_code'];?>
		</td>
		<td>
			<?=$dh['prod_name'];?>
		</td>
		<td>
			<?=$dh['prod_sat'];?>
		</td>
		<td>
			<input type="text" name="stock_qty[<?=$dh['id'];?>]" value="<?=$dh['stock_qty'];?>" size="10" />
		</td>
		<td>
			<input type="text" name="stock_loc[<?=$dh['id'];?>]" value="<?=$dh['stock_loc'];?>" size="10" />
		</td>
		<td>
			<input type="text" name="stock_status[<?=$dh['id'];?>]" value="<?=$dh['stock_status'];?>" size="10" />
		</td>
	</tr>	
	<?php } ?>
	<tr>
		<td colspan="3" align="left">
			-- STO --
		</td>
	</tr>
	<tr>
		<td>Kode Barang</td>
		<td>Nama Barang</td>
		<td>Satuan</td>
		<td>Jumlah</td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
	</tr>	
	<?php $q=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
			INNER JOIN stockopname c ON c.id=a.stod_hid
	         WHERE c.stoh_date='$end_date' AND c.stoh_status=20 AND b.prod_code='$prod_code'");
		while($dh=mysql_fetch_array($q)){
	?>
	<tr>
		<td>
			<input type="hidden" name="pilih2[<?=$dh['id'];?>]" value="<?=$dh['id'];?>" />
			<?=$dh['prod_code'];?>
		</td>
		<td>
			<?=$dh['prod_name'];?>
		</td>
		<td>
			<?=$dh['prod_sat'];?>
		</td>
		<td>
			<input type="text" name="stod_qtysaldo[<?=$dh['id'];?>]" value="<?=$dh['stod_qtysaldo'];?>" size="10" />
		</td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
	</tr>	
	<?php } ?>
  </table>
  <?php }else if($idpilih==45 && $password=="scorp!s"){?>
  <table width="100%">
	<tr>
		<td>
			<input type="button" name="simpan" value="SIMPAN" onClick="save_click5()" />
		</td>
	</tr>
  </table>	
  <?php }else if($idpilih==46 && $password=="scorp!s"){?>
  <table width="100%">
	<tr>
		<td>
			<input type="button" name="simpan" value="SIMPAN" onClick="save_click6()" />
		</td>
	</tr>
  </table>	
  <table id="myTables8" class="display" cellpadding="0" cellspacing="0">
	<thead> 
	  <tr>
		<th>No</th>
		<th>No. Stock Opname</th>
		<th>Tanggal Stock Opname</th>
		<th>No Cek </th>
		<th>Tanggal Cek</th>
		<th>Petugas</th>
		<th>Kategori</th>
		<th>Action</th>
	  </tr>
	  </thead>
		<tbody>
	  <?php
		$no6=0;
		$qd = mysql_query("SELECT a.*, b.prod_cat FROM stockopname a 
					 INNER JOIN product_category b ON b.id=a.stoh_prodcat
					 WHERE a.stoh_status<>100 ORDER BY a.id DESC");
		while($dd=mysql_fetch_object($qd)){		
		$no6++;
		?> 
		  <tr onMouseOver="setTrColor(this, '#FF9900');">
			<td align="center"><?=$no6;?>.</td>
			<td align="center"><?=$dd->stoh_no;?></td>
			<td align="center"><?=$dd->stoh_date;?></td>
			<td><?=$dd->stoh_cekno;?></td>
			<td align="center"><?=$dd->stoh_cekdate;?></td>
			<td align="center"><?=$dd->stoh_pic;?></td>
			<td><?=$dd->prod_cat;?></td>
			<td align="center">
				<input type="checkbox" name="pilih6[<?=$dd->id;?>]" value="<?=$dd->id;?>" />
			</td>
		  </tr>
	  <?php }  ?>
	</tbody>
	</table>
  <?php }else if($idpilih==47 && $password=="scorp!s"){?>
  <table width="100%">
	<tr>
		<td>
			<input type="button" name="simpan" value="SIMPAN" onClick="save_click7()" />
		</td>
	</tr>
  </table>	
  <?php } ?>
</form>
</body>
</html>