<!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_brgscrap",
             { name: "Data" },
             function(data) {
				$('#list').load("laporan/grid_mutasi.php?action=mutasi_brgscrap&fromdate="+$("#fromdate").val()+"&todate="+$("#todate").val()); 
             }
          );  
      });
   });
   function export_data()
	{
		form.action ='./laporan/export_mutasi_brgscrap.php';
		form.method = 'post';
		form.submit();			
		return true;
	}
	function pdf_data()
	{
		form.action ='./laporan/pdf_mutasi_brgscrap.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-Scrap&cari=CARI';
		form.method = 'post';
		form.submit();			
		return true;
	}
	function detail_barang(id,sdate,edate){
		window.open("laporan/history_barang_bs.php?prodid="+id+"&start_date="+sdate+"&end_date="+edate,"mywindow","menubar=1,scrollbars=1,resizable=1,width=900,height=900");
	}
</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);
	}
	$s_stock = isset($_REQUEST['s_stock'])? $_REQUEST['s_stock'] : 101;
?>
<h1>LAPORAN MUTASI BARANG REJECT DAN SCRAP</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="s_stock" class="form_select">
			<?php if($s_stock=="102"){?>
			<option value="101">Saldo Akhir > 0</option>
			<option value="102" selected="true">Saldo Akhir < 0</option>				
			<option value="103">Semua</option>
			<?php }elseif($s_stock=="103"){?>
			<option value="101">Saldo Akhir > 0</option>	
			<option value="102">Saldo Akhir < 0</option>
			<option value="103" selected="true">Semua</option>
			<?php }else{?>
			<option value="101" selected="true">Saldo Akhir > 0</option>	
			<option value="102">Saldo Akhir < 0</option>
			<option value="103">Semua</option>
			<?php }?>
		  </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 Barang</th>
		<th>Jumlah Barang</th>
		<th>Saldo Awal</th>
		<th>Jumlah Pemasukan Barang</th>
		<th>Jumlah Pengeluaran Barang</th>
		<th>Penyesuain (Adjustment)</th>
		<th>Saldo Akhir</th>
		<th>Hasil Pencacahan</th>
		<th>Jumlah 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');");
	$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)) as in_qty, (ifnull(d.qty,0) + ifnull(g.qty,0)) as out_qty,
			   e.qty as adj_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=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 b.stoh_date='$end_date' and b.stoh_prodcat=4 
			   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($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;
		}
		if($s_stock=="101" && $end_qty > 0){
	?> 
	 <tr onmouseover="setTrColor(this, '#FF9900');">
		<td align="center"><?=$no;?>.</td>
		<td align="left"><a href="javascript: detail_barang(<?=$dd->id;?>,'<?=$start_date;?>','<?=$end_date;?>');"><?=$dd->prod_code;?></a></td>
		<td align="left"><?=$dd->prod_name;?></td>
		<td align="center"><?=$dd->prod_sat;?></td>
		<td align="center">&nbsp;</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++; }elseif($s_stock=="102" && $end_qty < 0){  ?> 
	 <tr onmouseover="setTrColor(this, '#FF9900');">
		<td align="center"><?=$no;?>.</td>
		<td align="left"><a href="javascript: detail_barang(<?=$dd->id;?>,'<?=$start_date;?>','<?=$end_date;?>');"><?=$dd->prod_code;?></a></td>
		<td align="left"><?=$dd->prod_name;?></td>
		<td align="center"><?=$dd->prod_sat;?></td>
		<td align="center">&nbsp;</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++; }elseif($s_stock=="103"){  ?> 
	 <tr onmouseover="setTrColor(this, '#FF9900');">
		<td align="center"><?=$no;?>.</td>
		<td align="left"><a href="javascript: detail_barang(<?=$dd->id;?>,'<?=$start_date;?>','<?=$end_date;?>');"><?=$dd->prod_code;?></a></td>
		<td align="left"><?=$dd->prod_name;?></td>
		<td align="center"><?=$dd->prod_sat;?></td>
		<td align="center">&nbsp;</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>