<?php
	$acl=$_SESSION["acl"];
	if (strlen($acl)<1 || !strpos($acl,"<17>")) {	//没有权限
		ob_clean();
		header("Location: accesserror.html");
		exit;
	}
?>

<?php
	$strYear=$_REQUEST["year"];
	$strMonth=$_REQUEST["month"];
	$time1=strftime("%Y-%m-01 00:00:01");		//统计开始时间，本月开始
	$time2=strftime("%Y-%m-01 00:00:01",strtotime("+1 month",time()));		//统计结束时间，下一个月开始
	if ($strYear!="" && $strMonth!="") {
	    $time1=$strYear;
	    if (intval($strMonth)<10) {
	    	$time1=$time1."-0".$strMonth."-01 00:00:01";		//统计开始时间
		}
	    else {
	    	$time1=$time1."-".$strMonth."-01 00:00:01";		//统计开始时间
	    }
	    $time2=$strYear;
	    if (intval($strMonth)<10) {
	    	$time2=$time2."-0".$strMonth."-01 00:00:01";
		}
	    else {
	    	$time2=$time2."-".$strMonth."-01 00:00:01";
	    }
		$time2=strftime("%Y-%m-01 00:00:01",strtotime("+1 month",strtotime($time2)));		//统计结束时间，下一个月开始
	}
	$db=mysql_pconnect(get_cfg_var("SIPSERVER"),get_cfg_var("SIPUSER"),get_cfg_var("SIPPWD"));
	mysql_select_db(get_cfg_var("SIPDB"),$db);
	$n=0;
	$rs = mysql_query( "select userdept,count(userid) from users group by userdept",$db );
	while ($row = mysql_fetch_array($rs)) {
		$n=$n+1;
		$dept[$n]=$row[0];
		$count1[$n]=$row[1];
		$count2[$n]=0;
		$alltime[$n]=0;
		$alldata[$n]=0;
	}
	mysql_free_result($rs);
	if ($n>0) {		//有部门
		$rs = mysql_query( "select users.userdept,count(history.userid),sum(history.usetime),sum(history.datalength) from users,history where users.userid=history.userid and starttime>='".$time1."' and starttime<'".$time2."' group by users.userdept",$db );
		while ($row = mysql_fetch_array($rs)) {
			$str=$row[0];
			for ($i=1;$i<=$n;$i++) {
				if ($str==$dept[$i]) {
					break;
				}
			}
			if ($i<=$n) {	//有对应部门
				$count2[$i]=$row[1];
				$alltime[$i]=$row[2];
				$alldata[$i]=$row[3];
			}
			$row = mysql_fetch_array($rs);
		}
		mysql_free_result($rs);
	}
	mysql_close($db);	
?>


<html>

<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>统计月报表</title>
</head>

<body background="bkg.bmp">

<p align="center"><font size="5"><b>统计月报表</b></font></p>
<hr color="#999966">

<div align="center">
<center>
<table border="1" cellpadding="0" cellspacing="0" bordercolor="#008080" width="100%">
  <tr>
    <td align="center">用户部门</td>
    <td align="center">用户数</td>
    <td align="center">总上网次数</td>
    <td align="center">总上网<br>时间(小时)</td>
    <td align="center">总上网<br>流量(M)</td>
  </tr>
<?php
	if ($n>0) {
		$mycount1=0;
		$mycount2=0;
		$mytime=0;
		$mydata=0;
		for ($i=1;$i<=$n;$i++) {
			echo("<tr>");
			echo("<td align=center>".$dept[$i]."</td>");
			echo("<td align=center>".strval($count1[$i])."</td>");
			echo("<td align=center>".strval($count2[$i])."</td>");
			echo("<td align=center>");
			echo( strval(round($alltime[$i]/3600,2)) );
			echo("</td>");
			echo("<td align=center>");
			echo( strval(round($alldata[$i]/1024,2)) );
			echo("</td>");
			echo("</tr>");
			$mycount1=$mycount1+$count1[$i];
			$mycount2=$mycount2+$count2[$i];
			$mytime=$mytime+$alltime[$i];
			$mydata=$mydata+$alldata[$i];
		}
		echo("<tr>");
		echo("<td align=center>合计</td>");
		echo("<td align=center>".strval($mycount1)."</td>");
		echo("<td align=center>".strval($mycount2)."</td>");
		echo("<td align=center>");
		echo( strval(round($mytime/3600,2)) );
		echo("</td>");
		echo("<td align=center>");
		echo( strval(round($mydata/1024,2)) );
		echo("</td>");
		echo("</tr>");
	}
?>
</table>

  </center>
</div>

<p align="center">　</p>

<p align="center">统计时间范围：从 <?php echo $time1; ?> 起，到 <?php echo $time2; ?> 止。</p> 
<hr>
<p align="center">SIP企业上网管理系统</p>
<p align="center">作者：<b>深圳市飞沃软件有限公司</b></p>

</body>

</html>
