<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<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.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" :10
		});
	});
	$(document).ready(function(){ 
		$("#fromdate").datepicker({
			dateFormat : "yy-mm-dd",
			changeMonth : true,
			changeYear : true,
		});
	});
	$(document).ready(function(){
		$("#todate").datepicker({
			dateFormat : "yy-mm-dd",
			changeMonth : true,
			changeYear : true,
		});
	});
	function setTrColor(obj, color)
	{
		obj.style.backgroundColor=color;
		obj.onmouseout = function(){
				obj.style.backgroundColor=''; 
		}
	}
	$(document).ready(function() {
      $("#search").click(function(event){	  
	     $.post("laporan/grid_mutasi.php?action=mutasi_brgmodal",
             { name: "Data" },
             function(data) {
				$('#list').load("laporan/grid_mutasi.php?action=mutasi_brgmodal&fromdate="+$("#fromdate").val()+"&todate="+$("#todate").val()); 
             }
          );  
      });
   });
   function export_data()
	{
		form.action ='./laporan/export_mutasi_brgmodal.php';
		form.method = 'post';
		form.submit();			
		return true;
	}
	function pdf_data()
	{
		form.action ='./laporan/pdf_mutasi_brgmodal.php';
		form.method = 'post';
		form.submit();			
		return true;
	}
	/*----------------*/
   function showDiv(divID,show) {
	  var w=document.getElementById(divID);
	  w.style.visibility=(show==1)?'visible':'hidden';
	}
	function search_click(){
		form.action ='?page=Mutasi-Barang-Modal&cari=CARI';
		form.method = 'post';
		form.submit();			
		return true;
	}
</script>
</head>
<body>
<?php
	#range date
	if(isset($_REQUEST['cari'])=="CARI"){
		 $start_date   = $_REQUEST['fromdate'];
		 $end_date     = $_REQUEST['todate']; 	
	}else{
		 $start_date   = $start_date;
		 $end_date     = $end_date;	
	}
	$days 		= substr($start_date,8,2);
	$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);
	#cek closing
	$cekclg = mysql_fetch_array(mysql_query("SELECT stock_date FROM stock WHERE stock_date='$start_date' AND stock_status=10 AND stock_loc<>'' GROUP BY stock_date"));
	if(empty($cekclg['stock_date'])){
		$sdate = mysql_fetch_array(mysql_query("SELECT MAX(stock_date) stock_date FROM stock WHERE stock_date<='$start_date' AND stock_status=10 AND stock_loc<>''"));
		$start_date_sa = $sdate['stock_date'];
		$end_date_sa   = date('Y-m-d', strtotime('-1 days', strtotime( $start_date )));
		$bulan 	  	   = substr($start_date_sa,5,2);
		$tahun	  	   = substr($start_date_sa,0,4);
	}
	$prod_cat		= isset($_REQUEST['prod_cat']) ? $_REQUEST['prod_cat'] : 1000;
	if($prod_cat==1000){
		$prodcat = "WHERE a.prod_cat IN(2,7,8)";
	}else{
		$prodcat = "WHERE a.prod_cat IN($prod_cat)";
	}
?>
<h1>LAPORAN PERTANGGUNGJAWABAN MUTASI MESIN DAN PERALATAN</h1>
<form name="form" enctype="multipart/form-data">
<table width="100%" border="0"  class="table-list">
	<tr>
	  <td colspan="3" bgcolor="#CCCCCC"><div align="left">PERIODE TANGGAL</div></td>
	</tr>
	<tr>
	  <td width="90">Periode</td>
	  <td width="5">:</td>
	  <td width="815">
	      <input type="text" id="fromdate" name="fromdate" size="9" value="<?=$start_date;?>" style="border-collapse:inherit"  readonly="true">&nbsp;s/d&nbsp;
	      <input type="text" id="todate" name="todate" size="9" value="<?=$end_date;?>" style="border-collapse:inheri" readonly="true"> 
		  <select name="prod_cat" class="form_select">	
			<option value="1000" selected="true">SEMUA</option>
				<?php
				  $dataSql = mysql_query("SELECT * FROM product_category WHERE id IN(2,7,8) ORDER BY id ASC");
				  while ($dataRow = mysql_fetch_array($dataSql)) {
					if ($dataRow['id']== $prod_cat) {
						$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="export" value="EXCEL" onClick="export_data();" style="cursor:pointer" />
		  <input type="button" name="pdf" value="PDF" onClick="pdf_data();" style="cursor:pointer" />
	  </td>
	</tr>
</table>
<table id="myTables" 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 Buku</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');");
	$qd = mysql_query("SELECT a.id, a.prod_code, a.prod_name, a.prod_type, a.prod_sat, a.prod_cat, b.qty as beg_qty, (ifnull(c.qty,0)+ifnull(h.qty,0)+ifnull(i.qty,0)) as in_qty, (ifnull(d.qty,0)+ifnull(g.qty,0)) as out_qty,
			   e.qty as adj_qty, (ifnull(b.qty,0) + ifnull(c.qty,0) - ifnull(d.qty,0) + ifnull(e.qty,0)) as end_qty,
			   f.qty as stock_opname
		FROM product a            
		LEFT JOIN (SELECT stock_prodid, SUM(stock_qty) as qty FROM stock WHERE MONTH(stock_date)='$month' AND YEAR(stock_date)='$year' 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, SUM(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 b.stoh_date='$end_date' AND b.stoh_prodcat IN(2,7,8)
			   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	
		$prodcat ORDER BY a.prod_code ASC;");
	while($dd=mysql_fetch_object($qd)){		
		#tanggal stock awal
		if(!empty($cekclg['stock_date'])){
			$beg_qty  = $dd->beg_qty;
		}else{
			$beg_qty  = get_stock($start_date_sa,$end_date_sa,$bulan,$tahun,$dd->id,$dd->prod_cat,$inv);
		}
		$end_qty = number_format($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($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>
</form>
</body>
</html>