<?php
	$acl=$_SESSION["acl"];
	if (strlen($acl)<1 || !strpos($acl,"<2003>")) {	//没有权限
		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 6.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>
<p align="center">
<?php
	$timestr=strftime("%Y-%m-%d %H:%M:%S");
	$userid=$_REQUEST["userid"];
	$buylength=$_REQUEST["buylength"];
	$otherlength=$_REQUEST["otherlength"];
	$discount=$_REQUEST["discount"];
	$money1=$_REQUEST["money1"];
	$money2=$_REQUEST["money2"];
	if ($buylength=="") {
		$buylength="0";
	}
	if ($otherlength=="") {
		$otherlength="0";
	}
	if ($discount=="") {
		$discount="100";
	}
	if ($userid!="") {						//必须输入用户ID
		$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 type,gpid from users where userid='".$userid."' and checked='n'",$db);		//检查是否存在该用户ID
		if ( ($row = mysql_fetch_array($rs))==FALSE ) {										//没有找到，还没有该用户注册过
			mysql_free_result($rs);
			echo("错误，不存在此用户ID");
		}
		else {
			$usertype=$row["type"];
			$gpid=$row["gpid"];
			mysql_free_result($rs);
			$rs = mysql_query("select price,typemode,unit from types where typeid='".$usertype."'",$db);
			$row = mysql_fetch_array($rs);
			$price=$row["price"];
			$typemode=$row["typemode"];
			$typeunit=$row["unit"];
			mysql_free_result($rs);
			if ($typemode=="1") {
				$alllength=intval($buylength)+intval($otherlength);
				if ($typeunit==5) {			//包年
					$alllength=intval($buylength)*12;			//实际控制按照月来控制
					if ($otherlength!="") {	//有优惠
						$alllength=$alllength+intval($otherlength);		//优惠单位是月
					}
				}
				else {
					if ($typeunit==7) {			//计流量(G)
						$alllength=intval($buylength)*1024;			//实际控制按照M来控制	
						if ($otherlength!="") {	//有优惠
							$alllength=$alllength+intval($otherlength);		//优惠单位是M
						}
					}
				}
				$moretime=0;
				$moredata=0;
				if ($typeunit<6) {		//续时间类型
					if ($typeunit==1 || $typeunit==2) {		//计分钟和计小时
						$rs = mysql_query("select overed from usertime where userid='".$userid."'",$db);
						$row = mysql_fetch_array($rs);
						$overed=$row["overed"];
						mysql_free_result($rs);
						if ($overed=="y") {		//已经用完，重新开始
							mysql_query( "update usertime set starttime='".$timestr."',length=".strval($alllength).",overed='n' where userid='".$userid."'",$db );
						}
						else {					//还未用完，在原来的基础上增加
							mysql_query( "update usertime set length=length+".strval($alllength).",overed='n' where userid='".$userid."'",$db );
						}
						if ($typeunit==1) $moretime=$alllength*60;
						else $moretime=$alllength*3600;
					}
					else {
						$rs = mysql_query("select starttime,length from usertime where userid='".$userid."'",$db);
						$row = mysql_fetch_array($rs);
						$starttime=$row["starttime"];
						$length=$row["length"];
						mysql_free_result($rs);
						$overed="y";
						if ($typeunit==3) {			//计天数
							//if (Now<DateAdd("d",length,starttime)) {
							if ( time() < strtotime("+".strval($length)." day",strtotime($starttime)) ) {
								$overed="n";
							}
							$moretime=$alllength*24*3600;
						}
						elseif ($typeunit==4) {			//包月
							//if (Now<DateAdd("m",length,starttime)) {
							if ( time() < strtotime("+".strval($length)." month",strtotime($starttime)) ) {
								$overed="n";
							}
							//$moretime=$alllength*30*24*3600;//不在线增加时间
						}
						else {						//包年
							//if (Now<DateAdd("m",length,starttime)) {	
							if ( time() < strtotime("+".strval($length)." month",strtotime($starttime)) ) {
								$overed="n";
							}
							//$moretime=$alllength*30*24*3600;//不在线增加时间
						}
						if ($overed=="y") {		//已经用完，重新开始
							mysql_query( "update usertime set starttime='".$timestr."',length=".strval($alllength).",overed='n' where userid='".$userid."'",$db );
						}
						else {					//还未用完，在原来的基础上增加
							mysql_query( "update usertime set length=length+".strval($alllength).",overed='n' where userid='".$userid."'",$db );
						}
					}
					mysql_query( "insert into addtimelist values('".$userid."','".$timestr."','".$usertype."',".$buylength.",0,".$otherlength.",".strval($price).",".strval($money1).",".$discount.",".strval($money2).",'2')",$db );
					mysql_query( "insert into operation values('".$_SESSION["operator"]."','".$timestr."','用户续费，用户ID：".$userid."')",$db );
				}
				elseif ($typeunit==6 || $typeunit==7) {		//计流量
					$rs = mysql_query("select overed from usertime where userid='".$userid."'",$db);
					$row = mysql_fetch_array($rs);
					$overed=$row["overed"];
					mysql_free_result($rs);
					if ($overed=="y") {		//已经用完，重新开始
						mysql_query( "update usertime set starttime='".$timestr."',datalength=".strval($alllength).",overed='n' where userid='".$userid."'",$db );
					}
					else {					//还未用完，在原来的基础上增加
						mysql_query( "update usertime set datalength=datalength+".strval($alllength).",overed='n' where userid='".$userid."'",$db );
					}
					mysql_query( "insert into addtimelist values('".$userid."','".$timestr."','".$usertype."',0,".$buylength.",".$otherlength.",".strval($price).",".strval($money1).",".$discount.",".strval($money2).",'2')",$db );
					mysql_query( "insert into operation values('".$_SESSION["operator"]."','".$timestr."','用户续费，用户ID：".$userid."')",$db );
					$moredata=$alllength*1024;
				}
				else {					//其它类型不用续费
				}
				if ($money2!="0" && $money2!="") {	//有购买款资金流
					mysql_query( "insert into cash values('".$userid."','".$timestr."',".$money2.",0,'1','".$_SESSION["operator"]."')" ,$db);
				}
				if ($moretime>0||$moredata>0) {
					$rs = mysql_query("select ipaddr from login where userid='".$userid."'",$db);
					if ($row = mysql_fetch_array($rs)) {		//正在上网，增加可用时间
						$rs1 = mysql_query("select userid from allowtimelist where userid='".$userid."' or userid='gp_".strval($gpid)."'",$db);
						if ( ($row1 = mysql_fetch_array($rs1))==FALSE ) {		//而且不限制每天上网时间段
							mysql_query("insert into addusertime values('".$userid."',".strval($moretime).",".strval($moredata).")",$db);
						}
						mysql_free_result($rs1);
					}
					mysql_free_result($rs);
				}
				echo("续费成功!");
			}
			else {
				echo("不是先购买后使用计费类型，不需要续费!");
			}
		}
		mysql_close($db);
	}
	else {
		echo("必须输入用户ID");
	}
?>
</p>
<hr>
<p align="center">SIP宽带上网计费管理系统</p>
<p align="center">作者：<b>深圳市飞沃软件有限公司</b></p>

</body>

</html>
