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

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

<?php
    $errno=1;
	$userid="";
	$usertype="";
	$price=0;
	$username="";
	$usertypename="";
	$alltime=0;
	$alllength=0;
	$money1=0;
	$money2=0;
	$createtime="";
	$unit1="秒";
	$unit2="M";
	$money3=0;
	$money4=0;
	$money5=0;
	$money6=0;
	$money7=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 users.userid,users.type,users.username,users.createtime,users.suspendtime,types.name,types.price,types.typemode,types.unit from users,types where users.type=types.typeid and users.checked='n' and users.loginname='".$loginname."'",$db);		//读取结帐类型列表
		if ($row = mysql_fetch_array($rs)) {
			$userid=$row[0];
			$usertype=$row[1];
			$username=$row[2];
			if ($username=="") {
			   $username="&nbsp;";
			}
			$createtime=$row[3];
			$suspendtime=$row[4];
			if (is_null($suspendtime)) {
				$suspendtime="";
			}
			$usertypename=$row[5];
			$price=$row[6];
			$typemode=$row[7];
			$typeunit=$row[8];
		    mysql_free_result($rs);
		    $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;
		    }
		    if ($typemode=="2") {
			   $limittype="0";
			   $limithour=0;
			   $limitflow=0;
			   $overprice1=0;
			   $overprice2=0;
			   $maxmoney=0;
			   $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 viptype,discount from vipuser where userid='".$userid."'",$db);	//读取该用户优惠等级记录
			   if ($row = mysql_fetch_array($rs)) {
				   $viptype=$row["viptype"];
			   	   $discount=$row["discount"];
		       } else {
		           $viptype="";
		           $discount=100;
		   	   }
			   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)) {			//有记录
			      $tmpcount=$row[0];
				  if (!is_null($tmpcount)) {
				 	 $alltime=$tmpcount;
				  }
				  $tmpcount=$row[1];
			 	  if (!is_null($tmpcount)) {
					 $alllength=$tmpcount;
					 $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));		//上一次结帐以来到欠费时的天数
				  }
				  $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));		//上一次结帐以来到欠费时的天数
				  }
				  $alltime=round($alltime/31,3);	//换算成月数，每月31天算，保留小数点后3位
				  $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;
			   } elseif ($typeunit==7) {			//按流量g计费
				  $alllength=round($alllength/1024,3);		//换成成g为单位，只保留小数点后3位
				  $money1=$alllength*$price;
				  $unit2="G";
			   } else {
				  $price=0;
				  $money1=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);
			   $money1=round($money1,2);		            //只保留小数点后2位
			   $money2=round($money1*$discount/100,2);		//只保留小数点后2位
			   if ($money3>$money2) {	//押金足够
				  $money4=0;
				  $money5=$money3-$money2;
			   } else {
			      $money4=$money2-$money3;
				  $money5=0;
			   }
			   $money7=$money4;
		   }
		   else {
		      $errno=4;
		   }
		}
		else {
		   $errno=2;
		}
		mysql_close($db);
	}
    else {
	    $errno=3;
	}
?>

<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">

<SCRIPT LANGUAGE="javascript">
function setmoney2()
{	
    cancelBubble=true;
	var money2=Math.round(parseInt(document.form1.discount.value,"10")*parseFloat(document.form1.money1.value))/100;
	var money3=parseFloat(document.form1.money3.value);
	var money4=0;
	var money5=0;
	if (money3>money2) {
		money4=0;
		money5=Math.round((money3-money2)*100)/100;
	}
	else {
		money4=Math.round((money2-money3)*100)/100;
		money5=0;
	}
	money7=Math.round( (parseFloat(document.form1.money6.value)+money4)*100 )/100;
	document.all.money21.innerText=money2.toString()+"元";
	document.form1.money2.value=money2.toString();
	document.all.money41.innerText=money4.toString()+"元";
	document.form1.money4.value=money4.toString();
	document.all.money51.innerText=money5.toString()+"元";
	document.form1.money5.value=money5.toString();
	document.all.money71.innerText=money7.toString()+"元";
}

function IsInt(obj)
{   
    var str=obj.value;
    var tmpstr=parseInt(str,"10");
    if (tmpstr!=str)
        return(false);
    else
       {
        obj.value=tmpstr;
        return(true);
       }
}

function checkData()
{
    obj=document.form1.discount;
    cansub=IsInt(obj);
    if (cansub&&obj.value>0)
        document.form1.submit();
    else
       { 
        alert("折扣只能是整数且大于0");
        document.form1.discount.focus();
        document.form1.discount.select();
       }
}

function setdelete(bDelete)
{
	if (bDelete==1)
    {
        document.all.deposit.innerText="需要退还押金";
        document.all.money61.style.display="none";
		document.form1.money6.value="0";
		document.all.money71.innerText=document.form1.money4.value+"元";
    }
    else
    {
        document.all.deposit.innerText="本次结帐后还剩余押金";
        document.all.money61.style.display="";
		document.form1.money6.value="0";
    }
}
</SCRIPT>


<title>用户上网结帐</title>
</head>

<body background="bkg.bmp">

<p align="center"><font size="5"><b>用户上网结帐</b></font></p>
<hr color="#999966">
<center>
<div align="center">
<?php if ($errno==1) { ?>
  <form name=form1 method="POST" action="check_result.php">
  <input type="hidden" name="userid" value="<?php echo $userid; ?>">
  <input type="hidden" name="usertype" value="<?php echo $usertype; ?>">
  <table border="1" width="514" height="183" bordercolor="#008080" cellspacing="0" cellpadding="0">
    <tr>
      <td width="173" align="center" height="50">用户ID</td>
      <td width="327" align="left" height="50">&nbsp&nbsp<?php echo $loginname; ?>
      </td>
    </tr>
    <tr>
      <td width="173" align="center" height="50">用户名称</td>
      <td width="327" align="left" height="50">&nbsp&nbsp<?php echo $username; ?>
      </td>
    </tr>
    <tr>
      <td width="173" align="center" height="50">用户类型</td>
      <td width="327" align="left" height="50">&nbsp&nbsp<?php echo $usertypename; ?>
      </td>
    </tr>
    <tr>
      <td width="173" align="center" height="50">单价</td>
      <td width="327" align="left" height="50">&nbsp&nbsp<?php echo strval($price); ?>元
      </td>
    </tr>
    <tr>
      <td width="173" align="center" height="50">上网总时间</td>
      <td width="327" align="left" height="50">&nbsp&nbsp<?php echo strval($alltime); ?><?php echo $unit1; ?>
          <input type="hidden" name="alltime" value="<?php echo strval($alltime); ?>">
      </td>
    </tr>
    <tr>
      <td width="173" align="center" height="50">上网总流量</td>
      <td width="327" align="left" height="50">&nbsp&nbsp<?php echo strval($alllength); ?><?php echo $unit2; ?>
          <input type="hidden" name="alllength" value="<?php echo strval($alllength); ?>">
      </td>
    </tr>
    <tr>
      <td width="173" align="center" height="50">合计费用</td>
      <td width="327" align="left" height="50">&nbsp&nbsp<span><?php echo strval($money1); ?>元</span>
          <input type="hidden" name="money1" value="<?php echo strval($money1); ?>">
      </td>
    </tr>
    <tr>
      <td width="173" align="center" height="50">优惠折扣率 </td>
      <td width="327" align="left" height="50">&nbsp
          <input type="text" name="discount" size="3" value="<?php echo strval($discount); ?>" onkeyup="setmoney2();">%                      
          <?php if ($viptype!="") {?>
          该用户优惠等级为：<?php echo $viptype; ?>,折扣<?php echo strval($discount); ?>%                       
          <?php } ?>
      </td>
    </tr>
    <tr>
      <td width="173" align="center" height="50">实收费用</td>
      <td width="327" align="left" height="50">&nbsp
          <span id="money21"><?php echo strval($money2); ?>元</span>
          <input type="hidden" name="money2" value="<?php echo strval($money2); ?>">
        </td>
    </tr>
    <tr>
      <td width="173" align="center" height="50">当前可用押金</td>
      <td width="327" align="left" height="50">&nbsp
          <span id="money31"><?php echo strval($money3); ?>元</span>
          <input type="hidden" name="money3" value="<?php echo strval($money3); ?>">
        </td>
    </tr>
    <tr>
      <td width="173" align="center" height="50"><span id="deposit">本次结帐后还剩余押金</span></td>
      <td width="327" align="left" height="50">&nbsp
          <span id="money51"><?php echo strval($money5); ?>元</span>
          <input type="hidden" name="money5" value="<?php echo strval($money5); ?>">
        </td>
    </tr>
    <tr>
      <td width="173" align="center" height="50">扣除押金后还需要支付</td>
      <td width="327" align="left" height="50">&nbsp
          <span id="money41"><?php echo strval($money4); ?>元</span>
          <input type="hidden" name="money4" value="<?php echo strval($money4); ?>">
        </td>
    </tr>
    <tr id="money61">
      <td width="173" align="center" height="50">增加预付押金</td>
      <td width="327" align="left" height="50">&nbsp
          <input type="text" name="money6" size="5" value="0" onkeyup="setmoney2();">元
      </td>
    </tr>
    <tr>
      <td width="173" align="center" height="50"><b>本次共需要收款</b></td>
      <td width="327" align="left" height="50">&nbsp
          <font color="#FFFF00"><b><span id="money71"><?php echo strval($money7); ?>元</span></b></font>
        </td>
    </tr>
    <tr>
      <td width="173" align="center" height="50">结帐后是否<br>同时注销用户</td>
      <td width="327" align="left" height="50">
          &nbsp; 
          <input type="radio" value="n" checked name="deluser">否&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
          <input type="radio" name="deluser" value="y">是  
        </td>
    </tr>
    <tr>
      <td colspan="2" width="454" align="center" height="51"><input type="button" value="提交" name="B1" onclick="checkData();"></td>
    </tr>
  </table>
  </form>
<?php } elseif ($errno==2) {?>
<p>用户ID无效</p>
<?php } elseif ($errno==3) {?>
<p>必须输入用户ID</p>
<?php } elseif ($errno==4) {?>
<p>该用户不是先使用后付款用户</p>
<?php } ?>
</div>
</center>
<hr>
<p align="center">SIP宽带上网计费管理系统</p>
<p align="center">作者：<b>深圳市飞沃软件有限公司</b></p>

</body>

</html>

