<?php
	$loginname = trim($_REQUEST["loginname"]);
	$password = trim($_REQUEST["password"]);
	$userid = $_SESSION["userid"];
	if ($userid=="") {
		if ($loginname=="" || $password=="") {
			ob_clean();
			header("Location: querymoney.html");
			exit;
		}
	}
?>

<?php
include "limitcheck.inc";
?>

<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">
<center>
<?php
	$db=mysql_pconnect(get_cfg_var("SIPSERVER"),get_cfg_var("SIPUSER"),get_cfg_var("SIPPWD"));
	mysql_select_db(get_cfg_var("SIPDB"),$db);
	$bPass=FALSE;
	if ($userid!="") {
		$bPass=TRUE;
	}
	else {
		if ($loginname!="" && $password!="") {
			$bFound=0;
			$rs = mysql_query("select userid,password from users where loginname='".$loginname."' and checked='n'",$db);	//取该用户开号数据
			if ($row = mysql_fetch_array($rs)) {
				if ($password!=$row["password"]) {
					$bFound=1;				//密码错误
				}
				else {
					$userid=$row["userid"];
					$bFound=2;				//密码符合
				}
			}
			mysql_free_result($rs);
			if ($bFound==0) {
				echo("不存在此用户ID！");
			}
			elseif ($bFound==1) {
				echo("用户密码错误！");
			}
			else {
				$bPass=TRUE;
			}
		}
		else {
			echo("必须输入用户ID和密码后才能查询上网清单！");
		}
	}
	if ($bPass) {		//用户身份验证通过
	   $allmoney=0;
   	   $strsql="select users.loginname,users.username,users.type,users.suspendtime,types.name,types.typemode,types.price,types.unit from users,types where users.type=types.typeid and users.userid='".$userid."'";
	   $rs=mysql_query($strsql,$db);
	   if ( ($row = mysql_fetch_array($rs))==FALSE ) {
		  mysql_free_result($rs);
		  echo("您当前无缴费记录！");
	   }
	   else {
		  $loginname=$row[0];
		  $username=$row[1];
		  $usertype=$row[2];
		  $suspendtime=$row[3];
		  $type_name=$row[4];
		  $typemode=$row[5];
		  $price=$row[6];
		  $typeunit=$row[7];
		  mysql_free_result($rs);
		  if (is_null($suspendtime)) {
			  $suspendtime="";
		  }
		  if ($typemode=="0") {
		     echo("您是免费用户，无缴费记录！");
		  }
		  else {
		     $sqlstr="select addtime,length+datalength,otherlength,price,money1,discount,money2 from addtimelist where userid='".$userid."' and money1>0 order by addtime";
             $rs=mysql_query($sqlstr,$db);
             if ( ($row = mysql_fetch_array($rs))==FALSE ) { 
	            mysql_free_result($rs);
                echo("您当前无缴费记录!");
			 }
             else {
                $rs1=mysql_query("select sum(money2) as allmoney from addtimelist where userid='".$userid."'",$db);	//读取总消费金额
				if ($row1 = mysql_fetch_array($rs1)) {
					$allmoney=$row1["allmoney"];
					if (is_null($allmoney)) $allmoney=0;
					$allmoney=round($allmoney,2);	//只保留小数点后2位
				}
			    mysql_free_result($rs1);
			    echo("用户ID:".$loginname."，用户姓名:".$username.",用户类型:".$type_name.",总计缴费:".strval($allmoney)."元。");
                $n=0;
				echo("<table border='1' bordercolor='#008080' cellspacing='0' cellpadding='0'>");
				echo("<tr><td align=center width=40>序号</td><td align=center width=160>时间</td><td align=center width=160>消费数量</td><td align=center width=80>赠送数量</td><td align=center width=80>单价(元)</td><td align=center width=100>应收(元)</td><td align=center width=60>折扣(%)</td><td align=center width=100>实收(元)</td></tr>");
				while ($row) {
					   $n=$n+1;
					   echo("<tr><td align='center'>".strval($n)."</td>");
					   echo("<td align=center width=160>".$row[0]."</td>");
					   echo("<td align=center width=160>".strval($row[1])."</td>");
					   echo("<td align=center width=80>".strval($row[2])."</td>");
					   echo("<td align=center width=80>".strval($row[3])."</td>");
					   echo("<td align=center width=100>".strval($row[4])."</td>");
					   echo("<td align=center width=60>".strval($row[5])."</td>");
					   echo("<td align=center width=100>".strval($row[6])."</td>");
					   echo("</tr>");
					   $row = mysql_fetch_array($rs);
				}
				echo("</table><br>");
				echo("<table align=center><tr><td align=left>");
				echo("<p><font size=2>1.“包年”类型赠送数量是以月为单位；<br>2.“计流量(G)”类型赠送数量是以M为单位；<br>3.其它类型赠送数量的单位和该类型续费数量单位相同；</font></p>");
			    echo("</td></tr></table>");
	            mysql_free_result($rs);
				if ($typemode=="1") {	//先购买后使用类型，显示还剩多少可用
					$unit1="";
					$length1=0;
					$length2=0;
					$n=5;
					$rs = mysql_query("select starttime,length,datalength from usertime where userid='".$userid."' and overed='n'",$db);
					if ( ($row = mysql_fetch_array($rs))==FALSE ) { 		//该用户时间或者流量已经用完
						$n=5;
						mysql_free_result($rs);
					}
					else {
						$starttime=$row["starttime"];
						$length1=$row["length"];
						$length2=$row["datalength"];
						mysql_free_result($rs);
						if ($typeunit==1) {		//按分钟计时
							$unit1="分钟";
							$rs = mysql_query("select sum(facttime) as alltime from history where userid='".$userid."' and starttime>='".$starttime."'",$db);
							if ($row = mysql_fetch_array($rs)) {
								$alltime=$row["alltime"];
								if (is_null($alltime)) {
									$alltime=0;
								}
								$nn=(int)($alltime/60);
								if ($nn*60<$alltime) {
									$alltime=$nn+1;
								} else {
									$alltime=$nn;
								}
								if ($alltime<$length1) {
									$length2=$length1-$alltime;
									$n=6;
								}
							} else { 				//还没有上网记录
								$length2=$length1;
								$n=6;
							}
							mysql_free_result($rs);
						}
						elseif ($typeunit==2) {		//按小时计时
							$unit1="小时";
							$rs = mysql_query("select sum(facttime) as alltime from history where userid='".$userid."' and starttime>='".$starttime."'",$db);
							if ($row = mysql_fetch_array($rs)) {
								$alltime=$row["alltime"];
								if (is_null($alltime)) {
									$alltime=0;
								}
								$nn=(int)($alltime/3600);
								if ($nn*3600<$alltime) {
									$alltime=$nn+1;
								} else {
									$alltime=$nn;
								}
								if ($alltime<$length1) {
									$length2=$length1-$alltime;
									$n=6;
								}
							} else { 				//还没有上网记录
								$length2=$length1;
								$n=6;
							}
							mysql_free_result($rs);
						}
						elseif ($typeunit==3) {		//按天计时
							$unit1="天";
							if (time()<strtotime("+".strval($length1-1)." day",strtotime($starttime))) {			//还没到天数期限
								$length2=DateDiff("d",time(),strtotime("+".strval($length1-1)." day",strtotime($starttime)));
								$n=6;
							}
						}
						elseif ($typeunit==4||$typeunit==5) {		//包月、包年
							$unit1="月";
							if (time()<strtotime("+".strval($length1-1)." month",strtotime($starttime))) {			//还没到月数期限
								$length2=DateDiff("m",time(),strtotime("+".strval($length1-1)." month",strtotime($starttime)));		//还剩下没有使用的月数
								$n=6;
							}
						}
						elseif ($typeunit==6||$typeunit==7) {		//计流量M、G
							$unit1="M";
							$rs = mysql_query("select sum(factdata) as alldata from history where userid='".$userid."' and starttime>='".$starttime."'",$db);
							if ($row = mysql_fetch_array($rs)) {
								$alldata=$row["alldata"];		//上网记录的流量是以K为单位
								if (is_null($alldata)) {
									$alldata=0;
								}
								$nn=(int)($alldata/1024);
								if ($nn*1024<$alldata) {
									$alldata=$nn+1;
								} else {
									$alldata=$nn;
								}
								$length1=$length2;
								if ($alldata<$length1) {		//已经超过购买的流量总数
									$length2=$length1-$alldata;
									$n=6;
								}
							} else { 			//还没有上过网
								$length2=$length1;
								$n=6;
							}
							mysql_free_result($rs);
						} else {
							$length1=0;
							$length2=0;
						}
					}
					if ($n==6) {
						echo("<p align=center>该用户当前还剩余的可使用量为：".strval($length2).$unit1."。</p>");
					} else {
						echo("<p align=center>该用户当前已经没有剩余的可使用量。");
						if ($typeunit==3 || $typeunit==4 || $typeunit==5) {
							echo("下一".$unit1."到来时，将不能再使用！");
						}
						echo("</p>");
					}
				}
             }
			 if ($typemode=="2") {	//先使用后结算类型，显示应交费用
				   $alltime=0;
				   $alllength=0;
				   $money1=0;
				   $createtime="";
				   $limittype="0";
				   $limithour=0;
				   $limitflow=0;
				   $overprice1=0;
				   $overprice2=0;
				   $maxmoney=0;
				   $unit1="";
				   $rs = mysql_query("select limittype,limithour,limitflow,overprice1,overprice2,maxmoney from types where typeid='".$usertype."'",$db);
				   if ($row = mysql_fetch_array($rs)) {
						$limittype=$row["limittype"];
						$limithour=$row["limithour"];
						$limitflow=$row["limitflow"];
						$overprice1=$row["overprice1"];
						$overprice2=$row["overprice2"];
						$maxmoney=$row["maxmoney"];
				   }
					mysql_free_result($rs);
				   $rs = mysql_query("select starttime from usertime where userid='".$userid."'",$db);		//读取该用户最近一次结帐记录
				   if ($row = mysql_fetch_array($rs)) {
					  $createtime=$row["starttime"];
				   }
					mysql_free_result($rs);
				   $rs = mysql_query( "select sum(facttime) as alltime,sum(factdata) as alllength from history where userid='".$userid."' and starttime>'".$createtime."'",$db );	//读取该用户从上一次结帐后以来的上网记录
				   //计算总共时间，总共流量。
				   if ($row = mysql_fetch_array($rs)) {
					  $alltime=$row[0];
					  if (is_null($alltime)) {
				 		 $alltime=0;
					  }
					  $alllength=$row[1];
			 		  if (is_null($alllength)) {
						 $alllength=0;
					  }
					  else {
						 $alllength=round($alllength/1024,1);		//换成成M为单位，只保留小数点后1位
					  }
				   }
					mysql_free_result($rs);
				   if ($typeunit==1) {				//分钟计费
					  $alltime=(int)($alltime/60);		//换成分钟，不保留小数点
					  $money1=$alltime*$price;
					  $unit1="分钟";
				   }
		 		   elseif ($typeunit==2) {			//小时计费
			  		  $alltime=round($alltime/3600,2);		//换成小时，只保留小数点后2位
					  $money1=$alltime*$price;
					  $unit1="小时";
					  if ($maxmoney>0) {	//存在每天费用封顶
						$money1=CalculateTimeMoney($typeunit,$db,$userid,$price,$limithour,$overprice1,$createtime,$maxmoney);
					  }
				   }
				   elseif ($typeunit==3) {			//按天计费
					  if ($suspendtime=="") {		//没有处于欠费状态
						  $alltime=DateDiff("d",strtotime($createtime),time());		//上一次结帐以来到现在的天数
					  } else {							//处于欠费状态，截止到欠费时为止
						  $alltime=DateDiff("d",strtotime($createtime),strtotime($suspendtime));		//上一次结帐以来到欠费时的天数
					  }
					  if ($alltime<1 && $alllength>0) {		//开通当天上过网
						$alltime=1;
					  }
					  $money1=$alltime*$price;
					  $unit1="天";
					  if ($alltime>0 && $limittype=="1" && ($limithour>0 || $limitflow>0)) {	//有超量限制
						$tmpmoney1=0;
						$tmpmoney2=0;
						if ($limithour>0) {
							$tmpmoney1=CalculateTimeMoney($typeunit,$db,$userid,$price,$limithour,$overprice1,$createtime,$maxmoney);
						}
						if ($limitflow>0) {
							$tmpmoney2=CalculateFlowMoney($typeunit,$db,$userid,$price,$limitflow,$overprice2,$createtime,$maxmoney);
						}
						if ($tmpmoney1>=$tmpmoney2) {	//取两者中较大的一个值
							$money1=$tmpmoney1;
						} else {
							$money1=$tmpmoney2;
						}
					  }
				   }
				   elseif ($typeunit==4) {			//按月计费
					  if ($suspendtime=="") {		//没有处于欠费状态
						  $alltime=DateDiff("d",strtotime($createtime),time());		//上一次结帐以来到现在的天数
					  } else {							//处于欠费状态，截止到欠费时为止
						  $alltime=DateDiff("d",strtotime($createtime),strtotime($suspendtime));		//上一次结帐以来到欠费时的天数
					  }
					  if ($alltime<1 && $alllength>0) {		//开通当天上过网
						$alltime=1;
					  }
					  $alltime=round($alltime/31,1);	//换算成月数，每月31天算，保留小数点后1位
					  $money1=$alltime*$price;
					  $unit1="月";
					  if ($alltime>0 && $limittype=="1" && ($limithour>0 || $limitflow>0)) {	//有超量限制
						$tmpmoney1=0;
						$tmpmoney2=0;
						if ($limithour>0) {
							$tmpmoney1=CalculateTimeMoney($typeunit,$db,$userid,$price,$limithour,$overprice1,$createtime,$maxmoney);
						}
						if ($limitflow>0) {
							$tmpmoney2=CalculateFlowMoney($typeunit,$db,$userid,$price,$limitflow,$overprice2,$createtime,$maxmoney);
						}
						if ($tmpmoney1>=$tmpmoney2) {	//取两者中较大的一个值
							$money1=$tmpmoney1;
						} else {
							$money1=$tmpmoney2;
						}
					  }
				   }
				   elseif ($typeunit==5) {			//按年计费
					  if ($suspendtime=="") {		//没有处于欠费状态
						  $alltime=DateDiff("d",strtotime($createtime),time());		//上一次结帐以来到现在的天数
					  } else {							//处于欠费状态，截止到欠费时为止
						  $alltime=DateDiff("d",strtotime($createtime),strtotime($suspendtime));		//上一次结帐以来到欠费时的天数
					  }
					  $alltime=round($alltime/366,3);	//换算成年数，每年366天算，保留小数点后3位
					  $money1=$alltime*$price;
					  $unit1="年";
				   }
				   elseif ($typeunit==6) {			//按流量M计费
					  $money1=$alllength*$price;
					  $alltime=$alllength;
					  $unit1="M";
				   }
				   elseif ($typeunit==7) {			//按流量G计费
					  $alllength=round($alllength/1024,1);		//换成成G为单位，只保留小数点后1位
					  $money1=$alllength*$price;
					  $alltime=$alllength;
					  $unit1="G";
				   }
				   else {
					  $price=0;
					  $money1=0;
				   }
				   $money1=round($money1,2);		            //只保留小数点后2位
				   $money3=0;
				   $rs = mysql_query( "select sum(spare) from cash where userid='".$userid."'",$db );	//剩余押金
				   if ($row = mysql_fetch_array($rs)) {
					  $money3=$row[0];
					  if (is_null($money3)) {
						 $money3=0;
					  } else {
						 $money3=round($money3,2);
					  }
				   }
					mysql_free_result($rs);
				   echo("<p align=center>该用户当前的未结帐使用量为：".strval($alltime).$unit1."，应付费用为：".strval($money1)."元，可用押金为：".strval($money3)."元。</p>");
			 }
          }
       }
	}
   mysql_close($db);
?>
</center>
<hr>
<p align="center">SIP宽带上网计费管理系统</p>
<p align="center">作者：<b>深圳市飞沃软件有限公司</b></p>

</body>

</html>
