<?php 
ob_start();
include "../include/config.php"; 
include "../function/inc_function.php"; 

$start_date = $_REQUEST['fromdate'];
$end_date   = $_REQUEST['todate'];
$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        = $month-1;
if($cek==0){
	$bln = 12;
	$thn = $year-1;
}else{
	$bln = $cek;
	$thn = $year;
}
$thnbln = "$year$month";
$sqlShow 	= mysql_query("SELECT prof_name FROM profil");
$dataShow 	= mysql_fetch_array($sqlShow);
#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)";
	}
?>
<html>
<head>
<link rel='stylesheet' type='text/css' href="../include/css/printdoc_styles.css" />
</head>
<body>
<table class="tables_inout1">
    <tr>
  		<td colspan="12">LAPORAN PERTANGGUNGJAWABAN MUTASI MESIN DAN PERALATAN</td>
    </tr>
    <tr>
  		<td colspan="12">KAWASAN BERIKAT <?=strtoupper($dataShow['prof_name']);?></td>
    </tr>
    <tr>
  		<td colspan="12">PERIODE <?=DateToIndo($start_date);?> S/D <?=DateToIndo($end_date);?> </td>
    </tr>
    <tr>
  		<td colspan="12">&nbsp;</td>
    </tr>
</table>	
<table style="width:1300px;" class="tables_inout">	
  <tr>
		<th bgcolor="#D1D1D1" rowspan="2" style="width:30px;">No.</th>
		<th bgcolor="#D1D1D1" rowspan="2" style="width:80px;">Kode Barang</th>
		<th bgcolor="#D1D1D1" rowspan="2" style="width:240px;">Nama Barang</th>
		<th bgcolor="#D1D1D1" rowspan="2" style="width:50px;">Satuan</th>
		<th bgcolor="#D1D1D1" style="width:80px;">Saldo Awal</th>
		<th bgcolor="#D1D1D1" rowspan="2" style="width:80px;">Pemasukan</th>
		<th bgcolor="#D1D1D1" rowspan="2" style="width:80px;">Pengeluaran</th>
		<th bgcolor="#D1D1D1" rowspan="2" style="width:80px;">Penyesuaian</th>
		<th bgcolor="#D1D1D1" style="width:80px;">Saldo Buku</th>
		<th bgcolor="#D1D1D1" rowspan="2" style="width:60px;">Stock Opname</th>
		<th bgcolor="#D1D1D1" rowspan="2" style="width:60px;">Selisih</th>
		<th bgcolor="#D1D1D1" rowspan="2" style="width:80px;">Keterangan</th>
	</tr>
	<tr>
		<th align="center" bgcolor="#D1D1D1"><?=DateToIndo($start_date);?></th>
		<th align="center" bgcolor="#D1D1D1"><?=DateToIndo($end_date);?></th>
	</tr>
	<?php		
		$no=0;
		$val = valstockopname($month_sto,$year_sto,2);
		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)){		
			$no++;
			#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($end_qty > 0){
		?> 
		  <tr>
			<td align="center"><?=$no;?></td>
			<td align="left"><?=$dd->prod_code;?></td>
			<td align="left">
			<?php 
				if(strlen($dd->prod_name)<=40){
					echo substr($dd->prod_name,0,40)."<br>";
				}else{
					echo substr($dd->prod_name,0,40)."<br>";
					echo substr($dd->prod_name,40,40)."<br>";
					echo substr($dd->prod_name,80,40)."<br>";
				}
			
			?>
			</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 }  ?>		
</table>
<?php  
	 #PDF
	 $filename="laporan_mutasi_brgmodal.pdf";   
	 $content = ob_get_clean();  
	 //$content = '<page style="font-family: Arial;">'.nl2br($content).'</page>';  
	 require_once('../function/html2pdf/html2pdf.class.php');  
	 try  
	 {  
	  //$html2pdf = new HTML2PDF('P',array(240,280),'en', false, 'ISO-8859-15',array(0, 0, 0, 0));  
	  $html2pdf = new HTML2PDF('L','A4','en', false, 'ISO-8859-15',array(0, 0, 0, 0));
	  $html2pdf->setDefaultFont('Arial');  
	  $html2pdf->writeHTML($content, isset($_GET['vuehtml']));  
	  $html2pdf->Output($filename);  
	 }  
	 catch(HTML2PDF_exception $e) { echo $e; }
?> 