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

<?php
	$timestr=strftime("%Y-%m-%d %H:%M:%S");
	$bOk=FALSE;
	$deluser=trim($_REQUEST["deluser"]);
	$userid=trim($_REQUEST["userid"]);
	$discount=intval(trim($_REQUEST["discount"]));
	$money1=$_REQUEST["money1"];
	$money2=$_REQUEST["money2"];
	$money3=$_REQUEST["money3"];
	$money4=$_REQUEST["money4"];
	$money5=$_REQUEST["money5"];
	$money6=$_REQUEST["money6"];
	$alltime=$_REQUEST["alltime"];
	$alllength=$_REQUEST["alllength"];
	if ($userid!="") {
		$usertype="";
		$createtime="";
		$suspend="";
		$price=0;
		$username="";
		$usertypename="";
		$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 users.userid,users.type,users.username,users.createtime,users.suspend,types.name,types.price,types.typemode from users,types where users.type=types.typeid and users.checked='n' and users.userid='".$userid."'",$db);		//读取结帐类型列表
		if ($row = mysql_fetch_array($rs)) {
			$userid=$row[0];
			$usertype=$row[1];
			$username=$row[2];
			$createtime=$row[3];
			$suspend=$row[4];
			if (is_null($suspend)) {
				$suspend="";
			}
			$usertypename=$row[5];
			$price=$row[6];
			$typemode=$row[7];
		}
		mysql_free_result($rs);
		if ($typemode=="2") {
		    mysql_query( "insert into addtimelist values('".$userid."','".$timestr."','".$usertype."',".$alltime.",".$alllength.",0,".strval($price).",".$money1.",".strval($discount).",".$money2.",'3')",$db );
			mysql_query( "update usertime set starttime='".$timestr."' where userid='".$userid."'",$db );
			mysql_query( "insert into operation values('".$_SESSION["operator"]."','".$timestr."','用户结帐，用户ID：".$userid."')",$db );
			if ($money4!="0" && $money4!="") {	//有结帐款资金流
				mysql_query( "insert into cash values('".$userid."','".$timestr."',".$money4.",0,'3','".$_SESSION["operator"]."')" ,$db);
			}
			if ($money3!="0" && $money3!="") {	//有预付押金
				if ($money2!="0" && $money2!="") {	//有结帐款，有押金转结帐款资金流产生
					if (floatval($money3)>=floatval($money2)) {	//押金足够，从押金中扣除结帐款
						mysql_query( "insert into cash values('".$userid."','".$timestr."',".$money2.",0,'4','".$_SESSION["operator"]."')" ,$db);
					} else {	//押金不够，全部押金转结帐款
						mysql_query( "insert into cash values('".$userid."','".$timestr."',".$money3.",0,'4','".$_SESSION["operator"]."')" ,$db);
					}
				}
			}
			if ($suspend=="y") {		//取消欠费状态
				mysql_query( "update users set suspend='',suspendtime='' where userid='".$userid."'",$db );
				if ($money5!="0" && $money5!="") {	//还有剩余押金，需要退还
					mysql_query( "update cash set spare=0 where userid='".$userid."'" ,$db);
					mysql_query( "insert into cash values('".$userid."','".$timestr."',".$money5.",0,'6','".$_SESSION["operator"]."')" ,$db);	//退还剩余押金
				}
			}
			if ($deluser=="y") {		//注销用户
				mysql_query( "update users set checked='y',deletetime='".$timestr."' where userid='".$userid."'",$db );
				mysql_query( "insert into operation values('".$_SESSION["operator"]."','".$timestr."','注销用户，用户ID：".$userid."')",$db );
			}
			else {
				if ($money3!="0" && $money3!="") {	//有预付押金
					if ($money5!="0" && $money5!="") {	//设置剩余押金
						mysql_query( "update cash set spare=".$money5." where userid='".$userid."' and spare>0" ,$db);
					} else {	//押金已经用完
						mysql_query( "update cash set spare=0 where userid='".$userid."'" ,$db);
					}
				}
				if ($money6!="0" && $money6!="") {	//增加押金资金流
					mysql_query( "insert into cash values('".$userid."','".$timestr."',".$money6.",".$money6.",'2','".$_SESSION["operator"]."')" ,$db);	//增加押金
				}
			}
			$bOk=TRUE;
		}
		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 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 color="#999966">

<p align="center">

<?php
	if (bOk) {
		echo("结帐成功");
	}
	else {
		echo("结帐失败");
	}
?>

</p>

<hr>
<p align="center">SIP宽带上网计费管理系统</p>
<p align="center">作者：<b>深圳市飞沃软件有限公司</b></p>

</body>

</html>
