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


<?php
    $n=0;            //响应信息号
	$userid="";
	$usertype="";
	$username="";
	$usertypename="";
	$length1=0;
	$money1=0;
	$price=0;
	$discount=0;
	$length2=0;
	$money2=0;
	$money1str="";
	$pricestr="";
	$length2str="";
	$money2str="";
	$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 userid,type,username,name,typemode,unit from users,types where users.type=types.typeid and loginname='".$loginname."' and checked='n'",$db);		//检查是否存在该用户ID
		if ( ($row = mysql_fetch_array($rs))==FALSE ) {										//没有找到，还没有该用户注册过
			mysql_free_result($rs);
			$n=2;
		}
		else {
			$userid=$row["userid"];
			$usertype=$row["type"];
			$username=$row["username"];
			$usertypename=$row["name"];
			$typemode=$row["typemode"];
			$typeunit=$row["unit"];
			mysql_free_result($rs);
			$rs = mysql_query("select ipaddr from login where userid='".$userid."'",$db);		//检查是否正在上网
			if ( ($row = mysql_fetch_array($rs))==FALSE ) {		//没有上网
				mysql_free_result($rs);
				if ($typemode!="1") {
					$n=3;
				}
				else {
					//检查该用户的上网时间或流量是否已经用完
					$rs = mysql_query("select starttime 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"];
						mysql_free_result($rs);
						$rs = mysql_query("select sum(length),sum(datalength),sum(money2) from addtimelist where userid='".$userid."' and addtime>='".$starttime."'",$db);		//不算赠送的部分
						if ($row = mysql_fetch_array($rs)) {
							$length1=$row[0];
							$length2=$row[1];
							$money1=round($row[2],2);
						    if (is_null($length1)) $length1=0;
						    if (is_null($length2)) $length2=0;
						    if (is_null($money1)) $money1=0;
						}
						mysql_free_result($rs);
						$rs = mysql_query("select price,discount from addtimelist where userid='".$userid."' and addtime>='".$starttime."'",$db);
						if ($row = mysql_fetch_array($rs)) {
							$price=$row["price"];
							$discount=$row["discount"];
							$row = mysql_fetch_array($rs);
							if ($row) {
								while ($row) {
									if ($row["price"]<$price) {	//取最小者
										$price=$row["price"];
									}
									if ($row["discount"]>$discount) {	//取最大者
										$discount=$row["discount"];
									}
									$row = mysql_fetch_array($rs);
								}
							}
						}
						mysql_free_result($rs);
						if ($typeunit==4) {		//包月
							if (strtotime("+".$length1." month",strtotime($starttime))>time()) {	//还没到月数期限
								$tmptime=strtotime("+".strval($length1-1)." month",strtotime($starttime));	//到期的前一个月
								$length2=DateDiff("m",time(),$tmptime);		//还剩下没有使用的月数
								$n=6;
							}
							else {
								$n=5;				//当前已经到最后一月，不能退费
							}
						}
						elseif ($typeunit==1||$typeunit==2) {		//按分钟或小时计时
							$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;
								if ($typeunit==1) {
									$alltime=ceil($alltime/60);
									if ($alltime>=$length1) {		//已经超过购买的分钟总数
										$n=5;			//购买时间已经用完
									}
									else {
										$length2=$length1-$alltime;
										$n=6;
									}
								}
								else {
									$alltime=ceil($alltime/3600);
									if ($alltime>=$length1) {		//已经超过购买的小时总数
										$n=5;			//购买时间已经用完
									}
									else {
										$length2=$length1-$alltime;
										$n=6;
									}
								}
							}
							else { 				//还没有上网记录
								$length2=$length1;
							}
							mysql_free_result($rs);
						}
						elseif ($typeunit==3) {		//按天计时
							if (strtotime("+".$length1." day",strtotime($starttime))>time()) {	//还没到天数期限
								$tmptime=strtotime("+".strval($length1-1)." day",strtotime($starttime));	//到期的前一天
								$length2=DateDiff("d",time(),$tmptime);		//还剩下没有使用的天数
								$n=6;
							}
							else {
								$n=5;
							}
						}
						elseif ($typeunit==5) {		//包年
							if (strtotime("+".$length1." year",strtotime($starttime))>time()) {	//还没到年数期限
								$tmptime=strtotime("+".strval($length1-1)." year",strtotime($starttime));	//到期的前一年
								$length2=DateDiff("yyyy",time(),$tmptime);
								$n=6;
							}
							else {
								$n=5;
							}
						}
						elseif ($typeunit==6||$typeunit==7) {		//计流量
							$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;
								if ($typeunit==6) {
									$nn=ceil($alldata/1024);
									$length1=$length2;
									if ($alldata>=$length1) {		//已经超过购买的流量总数
										$n=5;
									}
									else {
										$length2=$length1-$alldata;
										$n=6;
									}
								}
								else {
									$alldata=round($alldata/1024/1024,2);	//保留小数点后2位
									$length1=$length2;
									if ($alldata>=$length1) {		//已经超过购买的流量总数
										$n=5;
									}
									else {
										$length2=$length1-$alldata;
										$n=6;
									}
								}
							}
							else {			//还没有上过网
								$length2=$length1;
								$n=6;
							}
							mysql_free_result($rs);
						}
						else {
							$length1=0;
							$length2=0;
						}
					}
				}
			}
			else {
				mysql_free_result($rs);
				$n=7;
			}
		}
		mysql_close($db);
	}
    else {
       $n=4;
	}
	if ($n==6) {		//有退费
		$money1=$length2*$price;
		$money2=round(($money1*$discount)/100,2);
	}
?>

<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">
<div id='info' align="center">
<center>
<?php if ($n==6) { ?>
<form name=form1 method="POST" action="returnmoney2.php">
  <input type="hidden" name="userid" value="<?php echo $userid; ?>">
  <input type="hidden" name="loginname" value="<?php echo $loginname; ?>">
  <input type="hidden" name="username" value="<?php echo $username; ?>">
  <input type="hidden" name="usertype" value="<?php echo $usertype; ?>">
  <input type="hidden" name="usertypename" value="<?php echo $usertypename; ?>">
  <input type="hidden" name="price" value="<?php echo strval($price); ?>">
  <input type="hidden" name="length" value="<?php echo strval($length2); ?>">
  <input type="hidden" name="money1" value="<?php echo strval($money1); ?>">
  <input type="hidden" name="money2" value="<?php echo strval($money2); ?>">
  <input type="hidden" name="discount" value="<?php echo strval($discount); ?>">
<table border="1" width="524" height="183" bordercolor="#008080" cellspacing="0" cellpadding="0">
    <tr>
      <td width="124" align="center" height="50">用户ID</td>
      <td width="386" align="left" height="50">&nbsp;&nbsp;<?php echo $loginname; ?>
      </td>
    </tr>
    <tr>
      <td width="124" align="center" height="50">用户名称</td>
      <td width="386" align="left" height="50">&nbsp;&nbsp;<?php echo $username; ?>
      </td>
    </tr>
    <tr>
      <td width="124" align="center" height="50">用户类型</td>
      <td width="386" align="left" height="50">&nbsp;&nbsp;<?php echo $usertypename; ?>
      </td>
    </tr>
    <tr>
      <td width="124" align="center" height="50">购买时单价</td>
      <td width="386" align="left" height="50">&nbsp;&nbsp;<?php echo strval($price); ?>元
      </td>
    </tr>
    <tr>
      <td width="124" align="center" height="50">退费数量</td>
      <td width="386" align="left" height="50">&nbsp;&nbsp;<?php echo strval($length2); ?>(赠送部分不能退费)
      </td>
    </tr>
    <tr>
      <td width="124" align="center" height="50">退费合计</td>
      <td width="386" align="left" height="50">&nbsp;&nbsp;<?php echo strval($money1); ?>元
      </td>
    </tr>
    <tr>
      <td width="124" align="center" height="50">购买时折扣率</td>
      <td width="386" align="left" height="50">&nbsp;&nbsp;<?php echo strval($discount); ?>
      </td>
    </tr>
    <tr>
      <td width="124" align="center" height="50">实际退费</td>
      <td width="386" align="left" height="50">&nbsp;&nbsp;<?php echo strval($money2); ?>元
      </td>
    </tr>
    <tr>
      <td colspan="2" width="464" align="center" height="51">
	<a href=queryadd2_result.php?userid=<?php echo $userid; ?> target=_blank>详细缴费清单</a>
      </td>
    </tr>
    <tr>
      <td colspan="2" width="464" align="center" height="51">
          <input type="submit" value="确定退费" name="B1">
          <p>退费后，该用户将被同时注销，不能再使用！</p>
      </td>
    </tr>
</table>
</form>
<?php } elseif ($n==2) { ?>
<p>不存在该用户ID</p>
<?php } elseif ($n==3) { ?>
<p>该用户类型为：<?php echo $usertypename; ?>,不是预付费类型</p>
<?php } elseif ($n==4) { ?>
<p>请输入需要退费的用户ID</p>
<?php } elseif ($n==5) { ?>
<p>该用户已经没有费用可退！</p>
<?php } elseif ($n==7) { ?>
<p>该用户正在上网，不能退费！</p>
<?php } else { ?>
<p>处理错误！</p>
<?php } ?>
  </center>
</div>
<hr>
<p align="center">SIP宽带上网计费管理系统</p>
<p align="center">作者：<b>深圳市飞沃软件有限公司</b></p>

</body>

</html>

<?php
function DateDiff($opertype,$starttime,$endtime)
{
	if ($starttime>=$endtime) return 0;	//starttime在endtime之后，返回0
	$n=0;
	if ($opertype=="d") {
		if ( strftime("%Y-%m-%d",$starttime)==strftime("%Y-%m-%d",$endtime) ) return 0;	//同一天
		while (TRUE) {
			$n++;
			if ( strftime("%Y-%m-%d",strtotime("+".strval($n)." day",$starttime))==strftime("%Y-%m-%d",$endtime) ) {
				//年月日部分已经相同
				break;
			}
		}
	}
	elseif ($opertype=="m") {
		if ( strftime("%Y-%m",$starttime)==strftime("%Y-%m",$endtime) ) return 0;	//同一月
		while (TRUE) {
			$n++;
			if ( strftime("%Y-%m",strtotime("+".strval($n)." month",$starttime))==strftime("%Y-%m",$endtime) ) {
				//年月部分已经相同
				break;
			}
		}
	}
	elseif ($opertype=="yyyy") {
		if ( strftime("%Y",$starttime)==strftime("%Y",$endtime) ) return 0;	//同一年
		while (TRUE) {
			$n++;
			if ( strftime("%Y",strtotime("+".strval($n)." year",$starttime))==strftime("%Y",$endtime) ) {
				//年部分已经相同
				break;
			}
		}
	}
	elseif ($opertype=="s") {
		$n=$endtime-$starttime;
	}
	else {
		$n=0;
	}
	return $n;
}
?>
