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

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

<?php
	$userid="";
	$addtime="";
	$usertype="";
	$checktime="";
	$suspendtime="";
	$prepay=0;
	$price=0;
	$alltime=0;
	$alllength=0;
	$money1=0;			//结算费用
	$money2=0;			//实际收取费用
	$money3=0;
	$allcount=0;
	$loginname = trim($_REQUEST["loginname"]);
	if ($loginname!="") {
		$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,addtime,userid,suspendtime,prepay from users where loginname='".$loginname."' and checked='n'",$db);	//取该用户开号数据
		if ($row = mysql_fetch_array($rs)) {
			$usertype=$row[0];
			$addtime=$row[1];
			$userid=$row[2];
			$suspendtime=$row[3];
			$prepay=$row[4];
			mysql_free_result($rs);
			if (is_null($suspendtime)) {
				$suspendtime="";
			}
			$ipaddr="";
			$rs = mysql_query("select ipaddr from login where userid='".$userid."'",$db);	//检查该客人是否还在上网
			if ($row = mysql_fetch_array($rs)) {
				$ipaddr=$row["ipaddr"];
			}
			mysql_free_result($rs);
			if ($ipaddr!="") {	//该客人还在上网
				mysql_close($db);
				ob_clean();
				header("Location: shutdown1.php?ipaddr=".$ipaddr);
				exit;
			}
			$rs = mysql_query("select price from types where name='".$usertype."'",$db);	//取用户类型对应的单价
			if ($row = mysql_fetch_array($rs)) {
				$price=$row[0];
				mysql_free_result($rs);
				if ($usertype=="天") {		//天类型，检查是否特殊房间（备有电脑）
					$rs = mysql_query("select price from rooms where loginname='".$loginname."'",$db);
					if ($row = mysql_fetch_array($rs)) {
						$price=$row[0];		//取该房间的特殊单价
					}
					mysql_free_result($rs);
				}
				$rs = mysql_query("select count(userid) as allcount,sum(usetime) as alltime,sum(datalength) as alllength from history where userid='".$userid."'",$db);	//读取该用户上网记录
				//计算总共时间，总共流量。
				if ($row = mysql_fetch_array($rs)) {			//有记录
					$allcount=$row[0];
					$alltime=$row[1];
					$alllength=round($row[2]/1024,2);	//换算成M为单位，只保留小数点后2位
				}
				mysql_free_result($rs);

				$money1=CheckMoney($db,$userid,$usertype,$price,$addtime,$alltime,$alllength,$suspendtime);
				$money2=$money1;
				$money3=$money2-$prepay;
			} else {
				mysql_free_result($rs);
				mysql_close($db);
				echo("<p align=center>该用户ID的结算类型不存在，不能结帐！</p>");
				exit;
			}
		} else {
			mysql_free_result($rs);
			mysql_close($db);
			echo("<p align=center>该用户ID不存在，或者已经结帐销号！</p>");
			exit;
		}
		mysql_close($db);
	} else {
		echo("<p align=center>必须输入结帐用户ID！</p>");
		exit;
	}
?>

<html>

<head>

<SCRIPT LANGUAGE="javascript">
function setvalue()
{
	var a=0.0;
	var b=1;
	b=parseInt(document.FrontPage_Form1.discount.value)*parseFloat(document.FrontPage_Form1.money1.value)*100/100;
	b=parseInt(b);
	a=b/100;
	document.FrontPage_Form1.money2.value=a.toString();
	a=a-parseInt(document.FrontPage_Form1.prepay.value);
	document.FrontPage_Form1.money3.value=a.toString();
	document.all.money33.innerText=a.toString();
}
</SCRIPT>

<title>上网费用结算</title>
</head>

<body background="bkg.bmp">

<p align="center"><font size="5"><b>上网费用结算表</b></font></p>
<hr color="#999966">

<!--webbot BOT="GeneratedScript" PREVIEW=" " startspan --><script Language="JavaScript"><!--
function FrontPage_Form1_Validator(theForm)
{

  var checkOK = "0123456789-";
  var checkStr = theForm.discount.value;
  var allValid = true;
  var decPoints = 0;
  var allNum = "";
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
    allNum += ch;
  }
  if (!allValid)
  {
    alert("优惠率只能输入数字");
    theForm.discount.focus();
    return (false);
  }

  var chkVal = allNum;
  var prsVal = parseInt(allNum);
  if (chkVal != "" && !(prsVal >= "0" && prsVal <= "100"))
  {
    alert("优惠率必须>=0并且<=100");
    theForm.discount.focus();
    return (false);
  }
  return (true);
}
//--></script><!--webbot BOT="GeneratedScript" endspan --><FORM name=FrontPage_Form1 METHOD="POST" ACTION="check_result2.php" onsubmit="return FrontPage_Form1_Validator(this)">

<div align="center">
  <center>
  <table border="1" width="632" bordercolor="#008080" cellspacing="0" cellpadding="0" height="308">
    <tr>
      <td width="148" height="30">用户ID</td>
      <td width="468" height="30" align="center"><?php echo $loginname; ?></td>
    </tr>
    <tr>
      <td width="148" height="29">开号时间</td>
      <td width="468" height="29" align="center"><?php echo $addtime; ?></td>
    </tr>
    <tr>
      <td width="148" height="27">计费类型</td>
      <td width="468" height="27" align="center"><?php echo $usertype; ?></td>
    </tr>
    <tr>
      <td width="148" height="29">结帐时间</td>
      <td width="468" height="29" align="center"><?php echo strftime("%Y-%m-%d %H:%M:%S"); ?></td>
    </tr>
    <tr>
      <td width="148" height="28">结算总数量</td>
      <td width="468" height="28" align="center">
      <?php
      		$flowunit="M";
      		if ($usertype=="流量(G)") {
      			$flowunit="G";
      		}
      		if ($usertype=="天") {
      			echo(strval($alltime)."天");
      		} elseif ($usertype=="小时") {
      			echo(strval($alltime)."小时");
      		} elseif ($usertype=="包月") {
      			echo(strval($alltime)."月");
      		} elseif ($usertype=="分钟") {
      			echo(strval($alltime)."分钟");
      		} elseif ($usertype=="年") {
      			echo(strval($alltime)."年");
      		} elseif ($usertype=="流量(M)") {
      			echo(strval($alllength)."M");
      		} elseif ($usertype=="流量(G)") {
      			echo(strval($alllength)."G");
      		} else {		//免费
      			echo("0");
      		}
      ?>
      </td>
    </tr>
    <tr>
      <td width="148" height="28">上网总流量</td>
      <td width="468" height="28" align="center"><?php echo strval($alllength); ?><?php echo $flowunit; ?></td>
    </tr>
    <tr>
      <td width="148" height="28">单价</td>
      <td width="468" height="28" align="center"><?php echo strval($price); ?>元</td>
    </tr>
    <tr>
      <td width="148" height="29">结算总费用</td>
      <td width="468" height="29" align="center"><?php echo strval($money1); ?>元</td>
    </tr>
    <tr>
      <td width="148" height="29">优惠率</td>
      <td width="468" height="29" align="center"><!--webbot bot="Validation"
        S-Data-Type="Integer" S-Number-Separators="x"
        S-Validation-Constraint="Greater than or equal to"
        S-Validation-Value="0" S-Validation-Constraint="Less than or equal to"
        S-Validation-Value="100" --><input type="text" name="discount" size="3" value="100" onkeyup="setvalue();">%</td>
    </tr>
    <tr>
      <td width="148" height="28">应收费用</td>
      <td width="468" height="28" align="center"><input type="text" name="money2" size="10" value="<?php echo strval($money2); ?>">元</td>
    </tr>
    <tr>
      <td width="148" height="29">预付押金</td>
      <td width="468" height="29" align="center"><?php echo strval($prepay); ?>元</td>
    </tr>
    <tr>
      <td width="148" height="29"><font color=#FFFF00>实际收取</font></td>
      <td width="468" height="28" align="center"><font color=#FFFF00><span id=money33><?php echo strval($money3); ?></span>元</font></td>
    </tr>
    <tr>
      <td width="148" height="29">备注</td>
      <td width="468" height="29" align="center"><input type="text" name="comment" size="50"></td>
    </tr>
    <tr>
      <td width="616" height="31" colspan="2" align="center"><input type="submit" value="确认" name="B1"></td>
    </tr>
  </table>
  </center>
</div>
<input type=hidden name=userid value=<?php echo $userid; ?>>
<input type=hidden name=alltime value=<?php echo strval($alltime); ?>>
<input type=hidden name=alllength value=<?php echo strval($alllength); ?>>
<input type=hidden name=money1 value=<?php echo strval($money1); ?>>
<input type=hidden name=price value=<?php echo strval($price); ?>>
<input type=hidden name=prepay value=<?php echo strval($prepay); ?>>
<input type=hidden name=money3 value=<?php echo strval($money3); ?>>
</FORM>
<p align="center">
<a href="query1.php?userid=<?php echo $userid; ?>&pageno=1" target="_blank">上网清单查询</a>
</p>
<hr>
<p align="center">SIP酒店上网计费管理系统</p>
<p align="center">作者：<b>深圳市飞沃软件有限公司</b></p>

</body>

</html>
