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

<?php
	$strYear=$_REQUEST["year"];
	$time1=strftime("%Y-01-01 00:00:01");
	$time2=strftime("%Y-12-31 23:59:59");
	if ($strYear!="") {
	    $time1=$strYear."-01-01 00:00:01";
	    $time2=$strYear."-12-31 23:59:59";
	}
	$db=mysql_pconnect(get_cfg_var("SIPSERVER"),get_cfg_var("SIPUSER"),get_cfg_var("SIPPWD"));
	mysql_select_db(get_cfg_var("SIPDB"),$db);
	$typecount=0;
	$rs = mysql_query( "select count(typeid) from types",$db );
	if ($row = mysql_fetch_array($rs)) {
		$typecount=$row[0];
	}
	mysql_free_result($rs);
	$rs = mysql_query( "select typeid,name from types order by typeid",$db );
	$n=1;
	while ($row = mysql_fetch_array($rs)) {
		$usertype[$n]=$row["typeid"];
		$usertypename[$n]=$row["name"];
		$n=$n+1;
	}		
	mysql_free_result($rs);
	
	for($n=1;$n<=$typecount;$n++) {
		$newa[$n]=0;
		$newb[$n]=0;
		$newc[$n]=0;
		$newd[$n]=0;
	}
	$newalla=0;
	$newallb=0;
	$newallc=0;
	$newalld=0;
	$rs = mysql_query( "select type,count(userid),sum(money1),sum(money2) from addtimelist where flag='1' and addtime>='".$time1."' and addtime<'".$time2."' group by type",$db );
	if ($row = mysql_fetch_array($rs)) {
		while ($row) {
			$typeid=$row[0];
			for($n=1;$n<=$typecount;$n++) {
				if ($usertype[$n]==$typeid) {
					break;
				}
			}
			$newa[$n]=$row[1];
			$newb[$n]=round($row[2],2);		//只保留小数点后2位
			$newc[$n]=round($row[3],2);
			if ($newa[$n]>0) {
				$newd[$n]=round($newc[$n]/$newa[$n],2);		//只保留小数点后2位
			}
			else {
				$newd[$n]=0;
			}
			$newalla=$newalla+$newa[$n];
			$newallb=$newallb+$newb[$n];
			$newallc=$newallc+$newc[$n];
			$row = mysql_fetch_array($rs);
		}
		$newalld=round($newallc/$newalla,2);		//只保留小数点后2位
	}
	mysql_free_result($rs);
	
	for($n=1;$n<=$typecount;$n++) {
		$checka[$n]=0;
		$checkb[$n]=0;
		$checkc[$n]=0;
		$checkd[$n]=0;
	}
	$checkalla=0;
	$checkallb=0;
	$checkallc=0;
	$checkalld=0;
	$rs = mysql_query( "select type,count(userid),sum(money1),sum(money2) from addtimelist where flag='3' and addtime>='".$time1."' and addtime<'".$time2."' group by type",$db );
	if ($row = mysql_fetch_array($rs)) {
		while ($row) {
			$typeid=$row[0];
			for($n=1;$n<=$typecount;$n++) {
				if ($usertype[$n]==$typeid) {
					break;
				}
			}
			$checka[$n]=$row[1];
			$checkb[$n]=round($row[2],2);
			$checkc[$n]=round($row[3],2);
			if ($checka[$n]>0) {
				$checkd[$n]=round($checkc[$n]/$checka[$n],2);		//只保留小数点后2位
			}
			else {
				$checkd[$n]=0;
			}
			$checkalla=$checkalla+$checka[$n];
			$checkallb=$checkallb+$checkb[$n];
			$checkallc=$checkallc+$checkc[$n];
			$row = mysql_fetch_array($rs);
		}
		$checkalld=round($checkallc/$checkalla,2);		//只保留小数点后2位
	}
	mysql_free_result($rs);
	
	for($n=1;$n<=$typecount;$n++) {
		$adda[$n]=0;
		$addb[$n]=0;
		$addc[$n]=0;
		$addd[$n]=0;
	}
	$addalla=0;
	$addallb=0;
	$addallc=0;
	$addalld=0;
	$rs = mysql_query( "select type,count(userid),sum(money1),sum(money2) from addtimelist where flag='2' and addtime>='".$time1."' and addtime<'".$time2."' group by type",$db );
	if ($row = mysql_fetch_array($rs)) {
		while ($row) {
			$typeid=$row[0];
			for($n=1;$n<=$typecount;$n++) {
				if ($usertype[$n]==$typeid) {
					break;
				}
			}
			$adda[$n]=$row[1];
			$addb[$n]=round($row[2],2);
			$addc[$n]=round($row[3],2);
			if ($adda[$n]>0) {
				$addd[$n]=round($addc[$n]/$adda[$n],2);		//只保留小数点后2位
			}
			else {
				$addd[$n]=0;
			}
			$addalla=$addalla+$adda[$n];
			$addallb=$addallb+$addb[$n];
			$addallc=$addallc+$addc[$n];
			$row = mysql_fetch_array($rs);
		}
		$addalld=round($addallc/$addalla,2);		//只保留小数点后2位
	}
	mysql_free_result($rs);

	for($n=1;$n<=$typecount;$n++) {
		$historya[$n]=0;
		$historyb[$n]=0;
		$historyc[$n]=0;
		$historyd[$n]=0;
		$historye[$n]=0;
	}
	$historyalla=0;
	$historyallb=0;
	$historyallc=0;
	$historyalld=0;
	$historyalle=0;
	$rs = mysql_query( "select type,count(userid),sum(usetime),sum(datalength) from history where starttime>='".$time1."' and starttime<'".$time2."' group by type",$db );
	if ($row = mysql_fetch_array($rs)) {
		while ($row) {
			$typeid=$row[0];
			for($n=1;$n<=$typecount;$n++) {
				if ($usertype[$n]==$typeid) {
					break;
				}
			}
			$historya[$n]=$row[1];
			$historyb[$n]=round($row[2],2);
			$historyc[$n]=round($row[3],2);
			if ($historya[$n]>0) {
				$historyd[$n]=round($historyb[$n]/$historya[$n],2);
				$historye[$n]=round($historyc[$n]/$historya[$n],2);
			}
			else {
				$historyd[$n]=0;
				$historye[$n]=0;
			}
			$historyalla=$historyalla+$historya[$n];
			$historyallb=$historyallb+$historyb[$n];
			$historyallc=$historyallc+$historyc[$n];
			$row = mysql_fetch_array($rs);
		}
		$historyalld=round($historyallb/$historyalla,2);
		$historyalle=round($historyallc/$historyalla,2);
	}
	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 colspan="5" width="721">
        <p align="center">注册情况</td>
    </tr>
    <tr>
      <td width="20%" align="center">类型</td>
      <td width="20%" align="center">用户数</td>
      <td width="20%" align="center">应收金额(元)</td>
      <td width="20%" align="center">实收金额(元)</td>
      <td width="20%" align="center">平均实收金额(元)</td>
    </tr>
<?php
	for($n=1;$n<=$typecount;$n++) {
	    echo("<tr>");
    	echo("<td>".$usertypename[$n]."</td>");
	    echo("<td align=center>".strval($newa[$n])."</td>");
    	echo("<td align=center>".strval($newb[$n])."</td>");
    	echo("<td align=center>".strval($newc[$n])."</td>");
    	echo("<td align=center>".strval($newd[$n])."</td>");
    	echo("</tr>");
    }
?>
    <tr>
      <td align="center">合计</td>
      <td align="center"><?php echo $newalla; ?></td>
      <td align="center"><?php echo $newallb; ?></td>
      <td align="center"><?php echo $newallc; ?></td>
      <td align="center"><?php echo $newalld; ?></td>
    </tr>
  </table>
  </center>
</div>
<div align="center">
  <center>
  <table border="1" cellpadding="0" cellspacing="0" bordercolor="#008080" width="100%">
    <tr>
      <td colspan="5">
        <p align="center">结帐情况</td>
    </tr>
    <tr>
      <td width="20%" align="center">类型</td>
      <td width="20%" align="center">用户数</td>
      <td width="20%" align="center">应收金额(元)</td>
      <td width="20%" align="center">实收金额(元)</td>
      <td width="20%" align="center">平均实收金额(元)</td>
    </tr>
<?php
	for($n=1;$n<=$typecount;$n++) {
	    echo("<tr>");
    	echo("<td>".$usertypename[$n]."</td>");
	    echo("<td align=center>".strval($checka[$n])."</td>");
    	echo("<td align=center>".strval($checkb[$n])."</td>");
    	echo("<td align=center>".strval($checkc[$n])."</td>");
    	echo("<td align=center>".strval($checkd[$n])."</td>");
    	echo("</tr>");
    }
?>
    <tr>
      <td align="center">合计</td>
      <td align="center"><?php echo $checkalla; ?></td>
      <td align="center"><?php echo $checkallb; ?></td>
      <td align="center"><?php echo $checkallc; ?></td>
      <td align="center"><?php echo $checkalld; ?></td>
    </tr>
  </table>
  </center>
</div>
<div align="center">
  <center>
  <table border="1" cellpadding="0" cellspacing="0" bordercolor="#008080" width="100%">
    <tr>
      <td colspan="5" width="659" align="center">
        <p align="center">续费情况</td>
    </tr>
    <tr>
      <td width="20%" align="center">类型</td>
      <td width="20%" align="center">用户数</td>
      <td width="20%" align="center">应收金额(元)</td>
      <td width="20%" align="center">实收金额(元)</td>
      <td width="20%" align="center">平均实收金额(元)</td>
    </tr>
<?php
	for($n=1;$n<=$typecount;$n++) {
	    echo("<tr>");
    	echo("<td>".$usertypename[$n]."</td>");
	    echo("<td align=center>".strval($adda[$n])."</td>");
    	echo("<td align=center>".strval($addb[$n])."</td>");
    	echo("<td align=center>".strval($addc[$n])."</td>");
    	echo("<td align=center>".strval($addd[$n])."</td>");
    	echo("</tr>");
    }
?>
    <tr>
      <td align="center">合计</td>
      <td align="center"><?php echo $addalla; ?></td>
      <td align="center"><?php echo $addallb; ?></td>
      <td align="center"><?php echo $addallc; ?></td>
      <td align="center"><?php echo $addalld; ?></td>
    </tr>
  </table>
  </center>
</div>
<div align="center">
  <center>
<table border="1" cellpadding="0" cellspacing="0" bordercolor="#008080" width="100%">
  <tr>
    <td colspan="6" align="center">
      <p align="center">上网情况</td>
  </tr>
  <tr>
    <td width="20%" align="center">类型</td>
    <td width="16%" align="center">总上网次数</td>
    <td width="16%" align="center">总上网时间(秒)</td>
    <td width="16%" align="center">总上网流量(K)</td>
    <td width="16%" align="center">平均每次上网时间(秒)</td>
    <td width="16%" align="center">平均每次上网流量(K)</td>
  </tr>
<?php
	for($n=1;$n<=$typecount;$n++) {
	    echo("<tr>");
    	echo("<td>".$usertypename[$n]."</td>");
	    echo("<td align=center>".strval($historya[$n])."</td>");
    	echo("<td align=center>".strval($historyb[$n])."</td>");
    	echo("<td align=center>".strval($historyc[$n])."</td>");
    	echo("<td align=center>".strval($historyd[$n])."</td>");
    	echo("<td align=center>".strval($historye[$n])."</td>");
    	echo("</tr>");
    }
?>
  <tr>
    <td align="center">合计</td>
    <td align="center"><?php echo $historyalla; ?></td>
    <td align="center"><?php echo $historyallb; ?></td>
    <td align="center"><?php echo $historyallc; ?></td>
    <td align="center"><?php echo $historyalld; ?></td>
    <td align="center"><?php echo $historyalle; ?></td>
  </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>
