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


<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">
<center>
<p align="center"><font size="5"><b>现金收支列表</b></font></p>
<hr color="#999966">
<?php
		$operator=$_REQUEST["operator"];
		$cashflag=$_REQUEST["cashflag"];
		$stryear1=$_REQUEST["year1"];
		$strmonth1=$_REQUEST["month1"];
		$strday1=$_REQUEST["day1"];
		$stryear2=$_REQUEST["year2"];
		$strmonth2=$_REQUEST["month2"];
		$strday2=$_REQUEST["day2"];
		$str=$_REQUEST["pageno"];
		if ($str=="") {
			$str="1";
		}
		$page_no=intval($str);			//当前显示页号，因为可能需要多页显示
		$pagecount=20;		//每页显示多少条记录
		$count=0;
		$allpage=0;
		if ($stryear1!="" && $strmonth1!="" && $strday1!="") {
           $time1=$stryear1;
           if (intval($strmonth1)<10) {
        	  $time1=$time1."-0".$strmonth1;
           } else {
        	  $time1=$time1."-".$strmonth1;
           }
           if (intval($strday1)<10) {
	          $time1=$time1."-0".$strday1." 00:00:01";				//统计起始时间
	       } else {
	          $time1=$time1."-".$strday1." 00:00:01";				//统计起始时间
	       }
	    } else {
	       $time1="2004-01-01 00:00:01";				//统计起始时间
	    }
	    if ($stryear2!="" && $strmonth2!="" && $strday2!="") {
           $time2=$stryear2;
           if (intval($strmonth2)<10) {
        	  $time2=$time2."-0".$strmonth2;
           } else {
           	  $time2=$time2."-".$strmonth2;
           }
           if (intval($strday2)<10) {
	          $time2=$time2."-0".$strday2." 23:59:59";				//统计结束时间
	       } else {
	          $time2=$time2."-".$strday2." 23:59:59";				//统计结束时间
	       }
	    } else {
			$time2=strftime("%Y-%m-%d %H:%M:%S");		//当前时间
   	    }
		$db=mysql_pconnect(get_cfg_var("SIPSERVER"),get_cfg_var("SIPUSER"),get_cfg_var("SIPPWD"));
		mysql_select_db(get_cfg_var("SIPDB"),$db);
		//根据查询条件生成sql语句
		$sqlstr="select count(userid) from cash";
	    $sqlstr=$sqlstr." where transtime>='".$time1."'";
	    $sqlstr=$sqlstr." and  transtime<'".$time2."'";
	   	if ($operator!="") {
	       $sqlstr=$sqlstr." and operator='".$operator."'";
	    }
	   	if ($cashflag!="") {
	       $sqlstr=$sqlstr." and flag='".$cashflag."'";
	    }
	    $rs=mysql_query( $sqlstr ,$db);
		//计算总记录数，计算共有多少页
		if ($row = mysql_fetch_array($rs)) {			//有记录
			$tmpcount=$row[0];
			if (!is_null($tmpcount)) {
				$count=$tmpcount;
			}
		}
		mysql_free_result($rs);
        if ($count<1) { 
           echo("<p align='center'>查询范围内没有现金收支记录！</p>");
        } else {
			$sqlstr="select flag,sum(money) from cash";
			$sqlstr=$sqlstr." where transtime>='".$time1."'";
			$sqlstr=$sqlstr." and  transtime<'".$time2."'";
	   		if ($operator!="") {
			   $sqlstr=$sqlstr." and operator='".$operator."'";
			}
			$sqlstr=$sqlstr." group by flag";
			for ($i=1;$i<=6;$i++) {
				$money[$i]=0;
			}
			$rs=mysql_query( $sqlstr ,$db);
			if ($row = mysql_fetch_array($rs)) {
				while ($row) {
					$tmpmoney=$row[1];
					if (is_null($tmpmoney)) {
						$tmpmoney=0;
					}
					$money[intval($row[0])]=round($tmpmoney,2);
					$row = mysql_fetch_array($rs);
				}
			}
			mysql_free_result($rs);
			$money7=$money[1]+$money[2]+$money[3];	//收
			$money8=$money[5]+$money[6];			//支
			$allpage=(int)($count/$pagecount);
			if ($allpage*$pagecount<$count) {		//不是整除
				$allpage=$allpage+1;
			}
			if ($page_no>$allpage) {
				echo("页号超出记录范围!");
			} else {
				//根据查询条件生成sql语句
				$sqlstr="select cash.transtime,cash.userid,cash.money,cash.flag,cash.operator,users.loginname from cash,users where cash.userid=users.userid";
	    		$sqlstr=$sqlstr." and transtime>='".$time1."'";
	 	   		$sqlstr=$sqlstr." and  transtime<'".$time2."'";
		   		if ($operator!="") {
		       		$sqlstr=$sqlstr." and operator='".$operator."'";
			    }
	   			if ($cashflag!="") {
				   $sqlstr=$sqlstr." and flag='".$cashflag."'";
				}
			    $sqlstr=$sqlstr." order by cash.transtime";
				$rs=mysql_query( $sqlstr ,$db);
				if ($row = mysql_fetch_array($rs)) {			//有记录
					$n=1;
					while ( $row && $n<=($page_no-1)*$pagecount	) {	//移动行号直到当前页对应起始行号位置
						$row = mysql_fetch_array($rs);
						$n=$n+1;
					}
					if ($row) {
							 echo("<table border='1' bordercolor='#008080' cellspacing='0' cellpadding='0'>");
							 echo("<tr><td align=center>序号</td><td align=center>操作时间</td><td align=center>用户id</td><td align=center>金额(元)</td><td align=center>资金类型</td><td align=center>操作员</td></tr>");
							 while ( $row && $n<=$page_no*$pagecount ) {	//显示当前页记录
								   echo("<tr>");
								   echo("<td>".strval($n)."</td>");
								   echo("<td>".$row[0]."</td>");
								   echo("<td><a href=queryuser_content.php?userid=".$row[1]." target=_blank>".$row[5]."</a></td>");
								   echo("<td>".strval($row[2])."</td>");
								   $str=$row[3];
								   if ($str=="1") {
										$str="购买款";
								   } elseif ($str=="2") {
										$str="押金款";
								   } elseif ($str=="3") {
										$str="结帐款";
								   } elseif ($str=="4") {
										$str="押金转结帐款";
								   } elseif ($str=="5") {
										$str="退购买款";
								   } elseif ($str=="6") {
										$str="退押金款";
								   } else {
										$str="&nbsp;";
								   }
								   echo("<td>".$str."</td>");
								   echo("<td>".$row[4]."</td>");
								   echo("</tr>");
								   $row = mysql_fetch_array($rs);         
								   $n=$n+1;
							 }
							 echo("</table><br>");
							 echo("<form method=get action=querycash_result.php><input type=hidden name=year1 value=".$stryear1."><input type=hidden name=year2 value=".$stryear2."><input type=hidden name=month1 value=".$strmonth1."><input type=hidden name=month2 value=".$strmonth2."><input type=hidden name=day1 value=".$strday1."><input type=hidden name=day2 value=".$strday2."><input type=hidden name=operator value=".$operator."><input type=hidden name=cashflag value=".$cashflag.">");
							 echo("第 ".strval($page_no)." 页，共 ".strval($allpage)." 页。");
							 if ($page_no>1) {			//有前一页
								echo("<a href='querycash_result.php?pageno=".strval($page_no-1)."&year1=".$stryear1."&year2=".$stryear2."&month1=".$strmonth1."&month2=".$strmonth2."&day1=".$strday1."&day2=".$strday2."&operator=".$operator."&cashflag=".$cashflag."'>前一页</a>");
							 }
							 if ($page_no<$allpage) {	//有后一页
								echo("&nbsp;<a href='querycash_result.php?pageno=".strval($page_no+1)."&year1=".$stryear1."&year2=".$stryear2."&month1=".$strmonth1."&month2=".$strmonth2."&day1=".$strday1."&day2=".$strday2."&operator=".$operator."&cashflag=".$cashflag."'>后一页</a>");
							 }
							if ($allpage>2) {	//超过2页
								echo(" 到<input type=text size=3 name=pageno maxlength=3>页");
							}
							echo("</form>");
							echo("<br><br>");
							echo("<table border=1 cellpadding=0 cellspacing=0 bordercolor=#008080>");
							echo("<tr><td rowspan=2>合计(元)</td><td>购买款</td><td>押金款</td><td>结帐款</td><td>押金转结帐款</td><td>退购买款</td><td>退押金款</td><td>收取</td><td>支出</td></tr>");
							echo("<tr><td>".strval($money[1])."</td><td>".strval($money[2])."</td><td>".strval($money[3])."</td><td>".strval($money[4])."</td><td>".strval($money[5])."</td><td>".strval($money[6])."</td><td>".strval($money7)."</td><td>".strval($money8)."</td>");
							echo("</tr></table>");
					} else {
						echo("页号已经超出记录范围!");
					}
				} else {
					echo("查询范围内没有现金收支记录!");
				}
				mysql_free_result($rs);
            }
        }
		mysql_close($db);
?>
<p align="center">统计时间范围：从 <?php echo $time1; ?> 起，到 <?php echo $time2; ?> 止。</p>
<hr>
<p align="center">SIP宽带上网计费管理系统</p>
<p align="center">作者：<b>深圳市飞沃软件有限公司</b></p>
</center>
</body>

</html>
