<?php
include "../include/config.php";
include "../function/inc_function.php";
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="laporan_jmldok_pemasukan_'.date('Y-m-d H:i:s').'.xls"');
header('Cache-Control: max-age=0');

$start_date = $_REQUEST['fromdate'];
$end_date   = $_REQUEST['todate'];
$doc_type   = $_REQUEST['doc_type'];

$month 		= substr($start_date,5,2);
$year		= substr($start_date,0,4);
$cek        = $month-1;
if($cek==0){
	$bln = 12;
	$thn = $year-1;
}else{
	$bln = $cek;
	$thn = $year;
}
$prof 	= mysql_fetch_array(mysql_query("SELECT prof_name FROM profil"));
?>
<table width="100%" border="0">
    <tr>
  		<td colspan="6" align="center">JUMLAH DOKUMEN PEMASUKAN</td>
    </tr>
	<tr>
  		<td colspan="6" align="center">PERIODE : <?=$start_date;?> S/D <?=$end_date;?> </td>
    </tr>
</table>	
<table width="100%" border="1">	
  <tr>
    <th align="center" bgcolor="#D1D1D1" rowspan="2">No</th>
    <th align="center" bgcolor="#D1D1D1" rowspan="2">Jenis Dokumen</th>
	<th align="center" bgcolor="#D1D1D1" rowspan="2">Jumlah Dokumen</th>
	<th align="center" bgcolor="#D1D1D1" rowspan="2">Berat</th>
	<th align="center" bgcolor="#D1D1D1" colspan="2">Nilai</th>
  </tr>  
  <tr>
	<th align="center" bgcolor="#D1D1D1">IDR</th>
    <th align="center" bgcolor="#D1D1D1">NON IDR</th>
  </tr>
	<?php		
		$qd = mysql_query("SELECT id,doc_type FROM document_type WHERE doc_status=1 ORDER BY doc_type ASC");
		$no = 1;
		while($dd=mysql_fetch_object($qd)){	
			#jumlah dokumen
			$cq = mysql_fetch_array(mysql_query("SELECT a.inch_doctype, COUNT(a.inch_docno) cdoc FROM (SELECT inch_doctype, inch_docno FROM incoming WHERE inch_docdate BETWEEN '$start_date' AND '$end_date' 
			AND inch_status=20 AND inch_doctype='$dd->doc_type' GROUP BY inch_doctype, inch_docno ) a GROUP BY a.inch_doctype"));
			#Berat 
			$br = mysql_fetch_array(mysql_query("SELECT SUM(a.incd_weight) berat FROM incoming_detail a INNER JOIN incoming b ON b.id=a.incd_hid
			WHERE inch_docdate BETWEEN '$start_date' AND '$end_date' AND inch_status=20 AND inch_doctype='$dd->doc_type'"));
			#nilai-IDR
			$amtidr = mysql_fetch_array(mysql_query("SELECT SUM(a.incd_price) amt_idr FROM incoming_detail a INNER JOIN incoming b ON b.id=a.incd_hid
			WHERE inch_docdate BETWEEN '$start_date' AND '$end_date' AND inch_status=20 AND inch_doctype='$dd->doc_type' AND a.incd_currency='IDR'"));
			#nilai-NonIDR
			$amtnidr = mysql_fetch_array(mysql_query("SELECT SUM(a.incd_price_usd) amt_nidr FROM incoming_detail a INNER JOIN incoming b ON b.id=a.incd_hid
			WHERE inch_docdate BETWEEN '$start_date' AND '$end_date' AND inch_status=20 AND inch_doctype='$dd->doc_type' AND a.incd_currency<>'IDR'"));
		?> 
		  <tr>
			<td align="center"><?=$no;?></td>
			<td align="center"><?=$dd->doc_type;?></td>
			<td align="center"><?=$cq['cdoc'];?></td>
			<td align="right"><?=number_format($br['berat'],2,'.','');?></td>
			<td align="right"><?=number_format($amtidr['amt_idr'],2,'.','');?></td>
			<td align="right"><?=number_format($amtnidr['amt_nidr'],2,'.','');?></td>
		  </tr>
      <?php $no++; }  ?>	
</table>