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

<?php
	$strYear=$_REQUEST["year"];
	$strMonth=$_REQUEST["month"];
	$strDay=$_REQUEST["day"];
	$time1=strftime("%Y-%m-%d 00:00:01");		//统计开始时间
	$time2=strftime("%Y-%m-%d 23:59:59");		//统计结束时间
	if ($strYear!="" && $strMonth!="" && $strDay!="") {
	    $time1=$strYear;
	    if (intval($strMonth)<10) {
	    	$time1=$time1."-0".$strMonth;
		}
	    else {
	    	$time1=$time1."-".$strMonth;
	    }
	    if (intval($strDay)<10) {
	    	$time1=$time1."-0".$strDay." 00:00:01";		//统计开始时间
		}
	    else {
	    	$time1=$time1."-".$strDay." 00:00:01";		//统计开始时间
	    }
	    $time2=$strYear;
	    if (intval($strMonth)<10) {
	    	$time2=$time2."-0".$strMonth;
		}
	    else {
	    	$time2=$time2."-".$strMonth;
	    }
	    if (intval($strDay)<10) {
	    	$time2=$time2."-0".$strDay." 23:59:59";		//统计结束时间
		}
	    else {
	    	$time2=$time2."-".$strDay." 23:59:59";		//统计结束时间
	    }
	}

	$typecount=0;
	$db=mysql_pconnect(get_cfg_var("SIPSERVER"),get_cfg_var("SIPUSER"),get_cfg_var("SIPPWD"));
	mysql_select_db(get_cfg_var("SIPDB"),$db);
	$rs = mysql_query("select count(name) from types",$db);
	$row = mysql_fetch_array($rs);
	$typecount=$row[0];
	mysql_free_result($rs);
	$rs = mysql_query( "select name,orderid from types order by orderid",$db );
	$n=0;
	while ($row = mysql_fetch_array($rs)) {
		$n=$n+1;
		$usertype[$n]=$row["name"];
	}		
	mysql_free_result($rs);
	
	for ($n=1;$n<=$typecount;$n++) {
		$newa[$n]=0;
	}
	$newalla=0;
	$rs = mysql_query( "select type,count(userid) from users where addtime>='".$time1."' and addtime<'".$time2."' group by type",$db );
	while ($row = mysql_fetch_array($rs)) {
		$typeid=$row[0];
		for ($n=1;$n<=$typecount;$n++) {
			if ($usertype[$n]==$typeid) {
				break;
			}
		}
		$newa[$n]=$row[1];
		$newalla=$newalla+$newa[$n];
	}
	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 account where checktime>='".$time1."' and checktime<'".$time2."' group by type",$db );
	while ($row = mysql_fetch_array($rs)) {
		$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];
	}
	if ($checkalla>0) {
		$checkalld=round($checkallc/$checkalla,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 );
	while ($row = mysql_fetch_array($rs)) {
		$typeid=$row[0];
		for ($n=1;$n<=$typecount;$n++) {
			if ($usertype[$n]==$typeid) {
				break;
			}
		}
		$historya[$n]=$row[1];
		$historyb[$n]=$row[2];
		$historyc[$n]=$row[3];
		if ($historya[$n]>0) {
			$historyd[$n]=(int)($historyb[$n]/$historya[$n]);
			$historye[$n]=(int)($historyc[$n]/$historya[$n]);
		} else {
			$historyd[$n]=0;
			$historye[$n]=0;
		}
		$historyalla=$historyalla+$historya[$n];
		$historyallb=$historyallb+$historyb[$n];
		$historyallc=$historyallc+$historyc[$n];
	}
	if ($historyalla>0) {
		$historyalld=(int)($historyallb/$historyalla);
		$historyalle=(int)($historyallc/$historyalla);
	}
	mysql_free_result($rs);

	for ($n=1;$n<=$typecount;$n++) {
		$exista[$n]=0;
	}
	$existalla=0;
	$rs = mysql_query( "select type,count(userid) from users where checked='n' group by type",$db );
	while ($row = mysql_fetch_array($rs)) {
		$typeid=$row[0];
		for ($n=1;$n<=$typecount;$n++) {
			if ($usertype[$n]==$typeid) {
				break;
			}
		}
		$exista[$n]=$row[1];
		$existalla=$existalla+$exista[$n];
	}
	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="2">
        <p align="center">开号情况</td>
    </tr>
    <tr>
      <td width="50%" align="center">类型</td>
      <td width="50%" align="center">用户数</td>
    </tr>
<?php
	for ($n=1;$n<=$typecount;$n++) {
	    echo("<tr>");
    	echo("<td align=center>".$usertype[$n]."</td>");
	    echo("<td align=center>".strval($newa[$n])."</td>");
    	echo("</tr>");
    }
?>
    <tr>
      <td align="center">合计</td>
      <td align="center"><?php echo $newalla; ?></td>
    </tr>
  </table>
  </center>
</div>
<br>
<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 align=center>".$usertype[$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>
<?php
    	echo("<td align=center>".strval($checkallb)."</td>");
    	echo("<td align=center>".strval($checkallc)."</td>");
    	echo("<td align=center>".strval($checkalld)."</td>");
?>
    </tr>
  </table>
  </center>
</div>
<br>
<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 align=center>".$usertype[$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>
<br>
<div align="center">
  <center>
  <table border="1" cellpadding="0" cellspacing="0" bordercolor="#008080" width="100%">
    <tr>
      <td colspan="2" align="center">
        <p align="center">当前可上网人数</td>
    </tr>
    <tr>
      <td width="50%" align="center">类型</td>
      <td width="50%" align="center">用户数</td>
    </tr>
<?php
	for ($n=1;$n<=$typecount;$n++) {
	    echo("<tr>");
    	echo("<td align=center>".$usertype[$n]."</td>");
	    echo("<td align=center>".strval($exista[$n])."</td>");
    	echo("</tr>");
    }
?>
    <tr>
      <td align="center">合计</td>
      <td align="center"><?php echo $existalla; ?></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>
