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

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>组续费</title>

<body background="bkg.bmp">
<p align="center"><font size="5"><b>组续费</b></font></p>
<hr color="#999966">
<center>
<?php
  $gpid=$_REQUEST["gpid"];               //组号
  $buylength=$_REQUEST["buylength"];     //续费数量
  $otherlength=$_REQUEST["otherlength"]; //赠送数量
  $discount=$_REQUEST["discount"];       //折扣
  if ($buylength=="") {
     $buylength="0";
  }
  if ($otherlength=="") {
	 $otherlength="0";
  }
  if ($discount=="") {
	 $discount="100";
  }
  $count=0;
  if ($_REQUEST["count"]!="") $count=intval($_REQUEST["count"]);		//续费人数
  if ($count>0) {
 	 $timestr=strftime("%Y-%m-%d %H:%M:%S");
	 $totalmoney1=$_REQUEST["totalmoney1"];         //组续费的总应收金额
	 $totalmoney2=$_REQUEST["totalmoney2"];         //组续费的总实收金额
	 $db=mysql_pconnect(get_cfg_var("SIPSERVER"),get_cfg_var("SIPUSER"),get_cfg_var("SIPPWD"));
	 mysql_select_db(get_cfg_var("SIPDB"),$db);
	 $count1=0;
     for ($n=1;$n<=$count;$n++) {
	     if ($_REQUEST["isadd".strval($n)]!="") {	//选择续费
			 $count1=$count1+1;
			 $money1=$_REQUEST["money1".strval($n)];
			 $money2=$_REQUEST["money2".strval($n)];
			 $userid=$_REQUEST["userid".strval($n)];
			 $rs = mysql_query("select type from users where userid='".$userid."' and checked='n'",$db);		//检查是否存在该用户ID
			 if ( ($row = mysql_fetch_array($rs))==FALSE ) {										//没有找到，还没有该用户注册过
				mysql_free_result($rs);
				ob_clean();                                  //清除缓存中的所有响应
				echo("<body background='bkg.bmp'>");   //输出错误提示
				echo("<p align='center'><font size='5'><b>组续费</b></font></p>");
				echo("<hr color='#999966'>");
				echo("<p align='center'>操作失败,续费组成员中".$userid."为无效ID,请检查</p>");
				echo("<hr>");
				echo("<p align='center'>SIP宽带上网计费管理系统</p>");
				echo("<p align='center'>作者：<b>深圳市飞沃软件有限公司</b></p>");
				echo("</body>");
				mysql_close($db);                                //关闭数据库连接
				exit;                              //脚本终止执行
			 }
			 else {
				$usertype=$row["type"];              //用户类型号
				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);
				$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
					  }
				   }
				}
				if ($typemode=="1") {
					if ($typeunit==1 || $typeunit==2 || $typeunit==3 || $typeunit==4 || $typeunit==5) {		//续时间类型
						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 );
							}
						}
						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";
								}
							}
							elseif ($typeunit==4) {			//包月
								//if (Now<DateAdd("m",length,starttime)) {
								if ( time() < strtotime("+".strval($length)." month",strtotime($starttime)) ) {
									$overed="n";
								}
							}
							else {						//包年
								//if (Now<DateAdd("m",length,starttime)) {	
								if ( time() < strtotime("+".strval($length)." month",strtotime($starttime)) ) {		//实际保存的长度是以月为单位
									$overed="n";
								}
							}
							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 );
						if ($money2!="0" && $money2!="") {	//有购买款资金流
							mysql_query( "insert into cash values('".$userid."','".$timestr."',".$money2.",0,'1','".$_SESSION["operator"]."')" ,$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 );
						if ($money2!="0" && $money2!="") {	//有购买款资金流
							mysql_query( "insert into cash values('".$userid."','".$timestr."',".$money2.",0,'1','".$_SESSION["operator"]."')" ,$db);
						}
					}
					else {
					}
				}
				else {	//其他非预付费用户
				   ob_clean();              //清除缓存中的所有响应
				   echo("<body background='bkg.bmp'>");         //输出错误提示
				   echo("<p align='center'><font size='5'><b>组续费</b></font></p>");
				   echo("<hr color='#999966'>");
				   echo("<p align='center'>操作失败,续费组成员中有非预付费用户,请检查</p>");
				   echo("<hr>");
				   echo("<p align='center'>SIP宽带上网计费管理系统</p>");
				   echo("<p align='center'>作者：<b>深圳市飞沃软件有限公司</b></p>");
				   echo("</body>");
				   mysql_close($db);                              //关闭数据库连接
				   exit;                            //脚本终止执行
				}
			 }
		 }
     }
     mysql_query( "insert into gpaddtimelist values(".$gpid.",'".$timestr."','".$buylength."',".$otherlength.",".strval($count1).",".strval($totalmoney1).",".$discount.",".strval($totalmoney2).")",$db );
	 mysql_query( "insert into operation values('".$_SESSION["operator"]."','".$timestr."','组续费，组号：".$gpid."')",$db );
	 //如果有错误发生则回滚事务并输出错误提示,没有则提交
    echo("续费成功");
    $rs=mysql_query("select gpname from groups where gpid=".$gpid,$db);
	if ($row = mysql_fetch_array($rs)) {
	   $gpname=$row["gpname"];
	}
	mysql_free_result($rs);
	//输出续费信息
	echo("<table align='center' border='1' bordercolor='#008080' cellspacing='0' cellpadding='0'>");
    echo("<tr><td align='center' width='100'>组名</td><td align='center' width='200'>".$gpname."</td></tr>");
	echo("<tr><td align='center' width='100'>续费人数</td><td align='center' width='200'>".strval($count1)."</td></tr>");
	echo("<tr><td align='center' width='100'>续费数量</td><td align='center' width='200'>".$buylength."</td></tr>");
	echo("<tr><td align='center' width='100'>赠送数量</td><td align='center' width='200'>".$otherlength."</td>");
	echo("<tr><td align='center' width='100'>应收金额</td><td align='center' width='200'>".strval($totalmoney1)."元</td></tr>");
	echo("<tr><td align='center' width='100'>折扣</td><td align='center' width='200'>".$discount."%</td></tr>");
	echo("<tr><td align='center' width='100'>应收金额</td><td align='center' width='200'>".strval($totalmoney2)."元</td></tr>");
	echo("</table>");
	 mysql_close($db);
  }
  else {
     echo("请选择需要续费的组员");
  }
?>
</center>
<hr>
<p align="center">SIP宽带上网计费管理系统</p>
<p align="center">作者：<b>深圳市飞沃软件有限公司</b></p>
</body>

</html>
