<!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(){ 
		$("#fromdate").datepicker({
			dateFormat : "yy-mm-dd",
			changeMonth : true,
			changeYear : true,
		});
	});
	$(document).ready(function(){
		$("#todate").datepicker({
			dateFormat : "yy-mm-dd",
			changeMonth : true,
			changeYear : true,
		});
	});
	$(document).ready( function () {
		var oTable = $('#myTables').dataTable( {
			 "sDom": 'R<"H"lfr>t<"F"ip>',
			 "bJQueryUI": true,
			 "sPaginationType": "full_numbers",
			 "iDisplayLength" :10
		});
	});
	function setTrColor(obj, color)
	{
		obj.style.backgroundColor=color;
		obj.onmouseout = function(){
				obj.style.backgroundColor=''; 
		}
	}
	function search_click(){
		form.action ='?page=Data-Stock-BhnBaku&Act=CARI';
		form.method = 'post';
		form.submit();			
		return true;
	}
	function excel_click(){
		form.action ='./transaksi/akuntansi/export_stock_bhnbaku.php';
		form.method = 'post';
		form.submit();			
		return true;
	}
	function pdf_click(){
		form.action ='./transaksi/akuntansi/pdf_stock_bhnbaku.php';
		form.method = 'post';
		form.submit();			
		return true;
	}
</script>
<?php
if(isset($_REQUEST['Act'])=="CARI"){
	 $fromdate	 = isset($_REQUEST['fromdate']) ? $_REQUEST['fromdate'] : $start_date;
	 $todate     = isset($_REQUEST['todate']) ? $_REQUEST['todate'] : $end_date; 	
 }else{
	 $fromdate	 = $start_date;
	 $todate     = $end_date;	
 }
 $month 	= substr($fromdate,5,2);
 $year		= substr($fromdate,0,4);
?>
</head>
<body>
<form action="#" method="post" name="form">
<table width="100%" border="0" cellspacing="1" cellpadding="1">
  <tr>
	<td colspan="12" align="center"><h1><b>DATA STOCK BAHAN BAKU PER DOKUMEN</b></h1></td>
  </tr>
  <tr>
	<td colspan="12">Periode :
	     <!--<select id="bulan" name="bulan">
		   <?php
			$bln = array('','Januari','Februari','Maret','April','Mei','Juni','Juli','Agustus','September','Oktober','November','Desember');
			for($n=1; $n<=12; $n++){
			if ($n == $bulan) {
				$cek = " selected";
			} else { $cek=""; }
		   ?>
		   	<option value="<?=$n;?>" <?=$cek;?>><?=$bln[$n];?></option>
		   <?php } ?>
		  </select>
		  <input type="text" id="tahun" name="tahun" size="4" value="<?=$tahun;?>">-->
		  <input type="text" id="fromdate" name="fromdate" size="9" value="<?=$fromdate;?>" style="border-collapse:inherit"  readonly="true">&nbsp;s/d&nbsp;
	      <input type="text" id="todate" name="todate" size="9" value="<?=$todate;?>" style="border-collapse:inheri" readonly="true">
	      <input type="button" name="cari" value="CARI" style="cursor:pointer" onclick="search_click();">
		  <input type="button" name="export" value="Export to Excel" style="cursor:pointer" onClick="excel_click();" />
		  <!--<input type="button" name="pdf" value="Export to PDF" style="cursor:pointer" onClick="pdf_click();"/>-->
	</td>		
  </tr>
</table>  
<table id="myTables" class="display" cellpadding="0" cellspacing="0">
	<thead>	
  <tr>
	<th>No</th>
	<th>Jenis</th>
	<th>No. Daftar</th>
	<th>Tgl Daftar</th>
	<th>Kode Barang</th>
	<th>Nama Barang</th>
	<th>Satuan</th>
    <th>S.Awal</th>	
	<th>Masuk</th>	
    <th>Keluar</th>
    <th>Stock</th>
	<th>Nilai</th>
  </tr>
  </thead>
	<tbody>
  <?php
        $no=1; $tendqty=0; $tamount=0;
        $qd = 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 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  FROM
					(SELECT stock_prodid prodid, stock_doctype doctype, stock_docno docno, stock_docdate docdate, stock_qty begqty, 0 inqty, 0 outqty, (stock_price/stock_qty) price 
					FROM stock WHERE stock_status=10 AND stock_date='$fromdate' AND stock_loc=1
					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, (b.incd_price/b.incd_qty) price 
					FROM incoming a
					INNER JOIN incoming_detail b ON b.incd_hid=a.id
					WHERE a.inch_status=20 AND a.inch_docdate BETWEEN '$fromdate' AND '$todate'
					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 price 
					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 '$fromdate' AND '$todate'
					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 price 
					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 '$fromdate' AND '$todate'
					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 price 
					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 '$fromdate' AND '$todate') a
					INNER JOIN product b ON b.id=a.prodid
					WHERE b.prod_cat IN(1,5,6) GROUP BY a.prodid, a.doctype, a.docno, a.docdate) b
					ORDER BY b.docno, b.prod_code ASC;");
        while($dd=mysql_fetch_array($qd)){	
			$amount  = $dd['endqty']*$dd['price'];	
			$tendqty = $tendqty+$dd['endqty'];
			$tamount = $tamount+$amount;
        ?> 
         <tr onmouseover="setTrColor(this, '#FF9900');">
            <td align="center"><?=$no;?>.</td>
            <td align="center"><?=$dd['doctype'];?></td>
            <td align="center"><?=$dd['docno'];?></td>
            <td align="center"><?=$dd['docdate'];?></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['begqty'],5,'.',',');?></td>
			<td align="right"><?=number_format($dd['inqty'],5,'.',',');?></td>
            <td align="right"><?=number_format($dd['outqty'],5,'.',',');?></td>
            <td align="right"><?=number_format($dd['endqty'],5,'.',',');?></td>
            <td align="right"><?=number_format($amount,5,'.',',');?></td>
          </tr>	  
      <?php $no++; }  ?> 
	  </tbody>
	  <tbody>
	  	  <tr>
		  	<td colspan="10">TOTAL</td>
			<td align="right"><?=number_format($tendqty,5,'.',',');?></td>
            <td align="right"><?=number_format($tamount,5,'.',',');?></td>
		  </tr>
  </tbody> 
</table>
</form>
</body>
</html>
