<%@ Language=VBScript %>
<!--#include virtual="/inc-freehost-function/FreeHostFunction.asp"-->
<!--#include virtual="/inc-freehost-function/FreeHostFunctionMain.asp"-->
<%
If session("username")="" Then
	ef=True
	ec="请您先登陆用户区，再续费，在主机面板中不能续费。也有可能是超时造成了出错。"
ElseIf Request.Form ("id")="" Then
	ef=True
	ec="请选择产品！"
ElseIf Instr(1, Request.Form ("id"), "'", 1)>0 then
	ef=True
	ec="非法产品ID！"
ElseIf not IsNumeric(Request.Form ("id"))  Then
	ef=True
	ec="产品ID应该是数字！"
ElseIf Request.Form ("Sqllistid")="" Then
	ef=True
	ec="请选择产品！"
ElseIf Instr(1, Request.Form ("Sqllistid"), "'", 1)>0 then
	ef=True
	ec="非法产品号！"
ElseIf not IsNumeric(Request.Form ("Sqllistid"))  Then
	ef=True
	ec="产品号应该是数字！"
end if
if not ef then
Set conn=Server.CreateObject("ADODB.Connection")
	conn.Open Application("FreeHostDBlink")
	Set ds = Server.CreateObject("ADODB.Recordset")	
		Sql="Select * from FreeHost.FreeHost_USER where username='"&session("username")&"'"
		ds.Open Sql,conn,1,3
if ds.eof then
	ef=True
	ec="用户不存在!"
end if
end if

if not ef then
if ds("password")<>session("password") then
	ef=True
	ec="密码不正确!"
end if
end if

if not ef then
	Set dshost = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_VPSProductlist where id='"&Request.Form ("Sqllistid")&"'"
			if Instr(1,sql, ";", 1)>0 or Instr(1,sql, "--", 1)>0 then
Response.Write "非法字符！"
Response.End
end if
	dshost.Open Sql,conn,1,1
if dshost.Eof then
	ef=True
	ec="产品不存在！"
end if
end if

if not ef then
if Not IsNull(dshost("onlyusebyagn")) Then
If session("agentname")<>dshost("onlyusebyagn") then
	ef=True
	ec="本产品只允许特定的代理使用！"
End if
end if
end If

if not ef then
	Set dsph = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_Product_VPS where id='"&Request.Form ("id")&"'"
			if Instr(1,sql, ";", 1)>0 or Instr(1,sql, "--", 1)>0 then
Response.Write "非法字符！"
Response.End
end if
	dsph.Open Sql,conn,1,3
if dsph.Eof then
	ef=True
	ec="要升级的云服务器产品不存在！"
'else
 '  if Not IsNull(dsph("byhost")) then
  ' 	ef=True
'	ec="赠送的不能升级！"
 '  end if
end if
end If

if not ef then
if Not dsph("isagentpd")=true Then
	ef=True
	ec="不是代理上级的产品不能用这个功能！"
End If
End If


if not ef then
if not isnull(dsph("repaymoney")) then
	ef=True
	ec="由于此产品被管理员设置了单独的续费价格，因此，请您联系管理员帮您手工升级。"
end if
end if

if not ef then
if Not dsph("isagentpd")=true then
	ef=True
	ec="不是代理上级的产品不能用这个功能。"
end if
end If

if not ef Then
if Not dsph("isagentpd")=true then
	Set dsserver = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_ServerVPSlist where id='"&dsph ("ServerlistID")&"'"
	dsserver.Open Sql,conn,1,1
if dsserver.Eof then
	ef=True
	ec="服务器不存在！"
Else
dim isAgnHostSer
if dsserver("agent1")="n" Then
isAgnHostSer=false
else
isAgnHostSer=true
end if
end If
end if
end if

if not ef then
if ds("agent1")<>"n" then

if not ef then
  Set dsagent = Server.CreateObject("ADODB.Recordset")	
		Sql="Select * from FreeHost.FreeHost_AGENT where username='"&ds("agent1")&"'"
		if Instr(1,sql, ";", 1)>0 or Instr(1,sql, "--", 1)>0 then
Response.Write "非法字符！"
Response.End
end if
		dsagent.Open Sql,conn,1,3
If dsagent.Eof Then
		ef=True
		ec="代理会员名不存在或密码出错了！"
end if
end if

if not ef then
	Set dsagnettype = Server.CreateObject("ADODB.Recordset")	
Sql="Select * from FreeHost.FreeHost_Agent_pers where agenttype='"& session("agenttype")&"'"
		dsagnettype.Open Sql,conn,1,1
if dsagnettype.Eof then
	ef=True
	ec="代理类型折扣为空，请先设置它！"
end if
end if

if not ef then
	Set dsagnpri = Server.CreateObject("ADODB.Recordset")	
		Sql="Select * from FreeHost.FreeHost_Agent_price where prtype='云服务器产品' and username='"&session ("agentname")&"' and id="&dshost("id")&""
		dsagnpri.Open Sql,conn,1,1
if dsagnpri.eof then
myrealcost=dshost("usermoney")*dsagnettype("pers")
else
myrealcost=dsagnpri("usermoney")
end if
dsagnpri.close

	Set dsendmoney = Server.CreateObject("ADODB.Recordset")	
		Sql="Select * from FreeHost.FreeHost_Agent_user_price where prtype='云服务器产品' and username='"&session("agentname")&"' and id="&dshost("id")&""
		dsendmoney.Open Sql,conn,1,1
if dsendmoney.eof then
usermoney=dshost("usermoney")
else
usermoney=dsendmoney("usermoney")
end if
dsendmoney.close
end if
end if
end if

if not ef then
if dsph("istest")="y" then
	ef=True
	ec="试用状态的产品必须延期后成为正式产品才可以升级到高级别！"
end if
end if

if not ef then
	Set dshostold = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_VPSProductlist where VPStype='"&dsph ("VPStype")&"'"
	dshostold.Open Sql,conn,1,1
if dshostold.Eof then
	ef=True
	ec="旧产品不存在！"
end if
end if

if not ef then
if (dshostold("updatebytypeid")="y" and dshost("typeid")=dshostold("typeid")) or isnull(dshostold("updatebytypeid")) or dshostold("updatebytypeid")="n" then
else
	ef=True
	ec="此产品只能在同一类型号中升级！类型号为："&typeid
end if
end if

if not ef then
if ds("agent1")<>"n" then
if not ef then
	Set olddsagnpri = Server.CreateObject("ADODB.Recordset")	
		Sql="Select * from FreeHost.FreeHost_Agent_price where prtype='云服务器产品' and username='"&session ("agentname")&"' and id="&dshostold("id")&""
		olddsagnpri.Open Sql,conn,1,1
if olddsagnpri.eof then
oldmyrealcost=dshostold("usermoney")*dsagnettype("pers")
else
oldmyrealcost=olddsagnpri("usermoney")
end if
olddsagnpri.close

	Set olddsendmoney = Server.CreateObject("ADODB.Recordset")	
		Sql="Select * from FreeHost.FreeHost_Agent_user_price where prtype='云服务器产品' and username='"&session("agentname")&"' and id="&dshostold("id")&""
		olddsendmoney.Open Sql,conn,1,1
'response.write sql
if olddsendmoney.eof then
oldusermoney=dshostold("usermoney")
else
oldusermoney=olddsendmoney("usermoney")
end if
olddsendmoney.close
end if
end if
end if

if not ef then
if dsph("istest")<>"y" then
if dshost("VPStype")=dsph("VPStype") then
	ef=True
	ec="同级别不可以升级!"
end if
end if
end if

if not ef then
if dshostold("usermoney")>dshost("usermoney") then
	ef=True
	ec="不能向低别升级!"
end if
end if

if not ef Then
daytoend=DateDiff("d",NOW,dsph("endtime"))
if daytoend<0 Then
	ef=True
	ec="您原来的产品已过期了，请您先延期原产品后才能升级。"
End If
End If

if not ef then
if dsph("istest")="y" then  daytoend=0'试用期的产品也要设置为0

if ds("agent1")="n" then'不是代理也不是代理名下用户
'不是代理也不是代理名下用户
oldusermoneyleft=dshostold("usermoney")/365*daytoend
if oldusermoneyleft<0 then oldusermoneyleft=0'防止出现特殊情况
'后面的天数用新产品所用的钱
newtypeusemoneyforuser=dshost("usermoney")/365*daytoend
oldusermoneytake=newtypeusemoneyforuser-oldusermoneyleft
else'是代理名下用户或代理自己操作自己

oldusermoneyleft=oldusermoney/365*daytoend

'response.write "(旧产品类型代理公开售价/365 x未到期天数) <br>"
'response.write oldusermoney&"<br>"
'response.write oldusermoney/365&"<br>"
'response.write "还有"&daytoend&"天<br>"
'response.write "为:"&oldusermoneyleft&"<br>"

if oldusermoneyleft<0 then oldusermoneyleft=0'防止出现特殊情况
'后面的天数用新产品所用的钱
newtypeusemoneyforuser=usermoney/365*daytoend

'response.write "(新产品类型代理公开售价/365 x未到期天数) <br>"
'response.write usermoney&"<br>"
'response.write usermoney/365&"<br>"
'response.write "还有"&daytoend&"天<br>"
'response.write "为:"&newtypeusemoneyforuser&"<br>"

oldusermoneytake=newtypeusemoneyforuser-oldusermoneyleft

'response.write "用户升级需要支付的金额"
'response.write oldusermoneytake&"<br>"

if session("AgnRunasUser") Then oldusermoneytake=0'代理自己登陆用户区操作自己用户角色为0

'计算代理角色成本
oldAgentrmoneyleft=oldmyrealcost/365*daytoend

'response.write "<br><br>(旧产品类型代理成本售价/365 x未到期天数)  <br>"
'response.write oldmyrealcost&"<br>"
'response.write oldmyrealcost/365&"<br>"
'response.write "还有"&daytoend&"天<br>"
'response.write "为:"&oldAgentrmoneyleft&"<br>"


if oldAgentrmoneyleft<0 then oldAgentrmoneyleft=0'防止出现特殊情况--代理
'后面的天数用新产品所用的钱--代理
newtypeAgentmoneyforuser=myrealcost/365*daytoend


'response.write "(新产品类型代理成本售价/365 x未到期天数)  <br>"
'response.write myrealcost&"<br>"
'response.write myrealcost/365&"<br>"
'response.write "还有"&daytoend&"天<br>"
'response.write "为:"&newtypeAgentmoneyforuser&"<br>"

oldAgentrmoneytake=newtypeAgentmoneyforuser-oldAgentrmoneyleft

'response.write "代理的升级成本价"
'response.write oldAgentrmoneytake&"<br>"

If isAgnHostSer Then oldAgentrmoneytake=0'独立模式服务器

end If'结束代理判断
end If


if not ef Then
'oldusermoneytake=round(oldusermoneytake,2)'处理格式
oldusermoneytake=round(oldusermoneytake,0)'处理格式
if oldusermoneytake>ds("usermoney") then'检查用户角色上是否有足够的钱
	ef=True
	ec="很抱歉,您的余额不足！升级产品所需金额为:"&oldusermoneytake&"元，当前您的余额为："&ds("usermoney")&"元"
End If
End If

if not ef Then
if ds("agent1")<>"n" then'代理自己所需成本检查
'oldAgentrmoneytake=round(oldAgentrmoneytake,2)'处理格式
oldAgentrmoneytake=round(oldAgentrmoneytake,0)'处理格式
if oldAgentrmoneytake-oldusermoneytake>dsagent("usermoney") then
	ef=True
	ec="很抱歉,上级余额不足！升级产品所需金额为:"&oldAgentrmoneytake-oldusermoneytake&"元，可操作的余额为："&dsagent("usermoney")&"元"
End If
End If
End if


if not ef Then
moneych=oldusermoneytake
if session("AgnRunasUser") Then moneych=oldAgentrmoneytake
If moneych<0 Then
	ef=True
	ec="很抱歉,余额为负("&moneych&"元)！有可能是这两个产品成本价一样造成,请联系管理员处理."
End if
End if

if not ef then
if Request.Form ("Submit")="只计算升级费用" then

moneych=oldusermoneytake
if session("AgnRunasUser") Then moneych=oldAgentrmoneytake
%>
<!--#include virtual="/inc-freehost-function/htmlwrite.asp"--><%=htmlwrite("html2")%>
　云服务器升级
<%=htmlwrite("html3")%>
<br><br><br>
升级到此产品所需金额为：<%=moneych%> 元
<%if session("AgnRunasUser") Then%> <font color=red>(代理成本金额为：<%=oldAgentrmoneytake-oldusermoneytake%> 元)</font><%End if%>
<%If isAgnHostSer Then%> <font color=red>(独立模式)</font><%End if%>
<%=htmlwrite("html4")%>
<%

response.end
end if'只显示升级费用
end If

if not ef then

'开始扣费
olduser_money=ds("usermoney")
ds("usermoney") =ds("usermoney") -oldusermoneytake
newuser_money=ds("usermoney")
moneych=-oldusermoneytake
ds.update'扣用户角色费用

if ds("agent1")<>"n" then
oldagn_money=dsagent("usermoney")
dsagent("usermoney")=dsagent("usermoney")-oldAgentrmoneytake+oldusermoneytake
mymoneych=-oldAgentrmoneytake+oldusermoneytake
newagn_money=dsagent("usermoney")
dsagent.update
'response.write "新产品价格："&usermoney&" 旧产品价格："&oldusermoney&" 用户原："&ds("usermoney")&" 代理成本折算旧："&oldRmoney&" 用户折算旧："&oldmoney&" tem："&(usermoney-oldmoney)&" tem2："&(myrealcost-oldRmoney)
'response.end
end if


dim starttime,temptime,pdtype
pdtype="原类型："&dsph ("VPStype")&" 新类型："&dshost("VPStype")

'更新产品类别
dsph ("VPStype")=dshost("VPStype")

dsph("MAXCPU")=dshost("MAXCPU")
dsph("CpuNum")=dshost("CpuNum")
dsph("MAXRam")=dshost("MAXRam")
dsph("StartRam")=dshost("StartRam")
dsph("Maxmbps8")=dshost("Maxmbps8")
dsph("Minmbps8")=dshost("Minmbps8")


dsph("MAXspace")=dshost("MAXspace")

dsph("allowdomain")=dshost("allowdomain")
dsph("CDNMaxMb")=dshost("CDNMaxMb")


dsph("status")="正常"
starttime=dsph ("endtime")
temptime=dsph ("endtime")
dsph.update

'isAgnHostSer=isAgnSqlSer
logfor="用户区升级VPS:"&dsph("vpsname")
if session("AgnRunasUser")=false then 
if isAgnHostSer then logfor="自动开通升级云服务器产品独立模式"
FreeHost_moneyNewlog conn,"y","n","n","n","n",isAgnHostSer,-moneych,dsph ("username"),dsph ("username"),dsph ("username"),olduser_money,newuser_money,agnusername,agnStmoney,agnEndmoney,supusername,logfor,logfor,"云服务器产品",dsph("id"),pingzheng,type1,bank,haveget,havecheck
if ds("agent1")<>"n" then FreeHost_moneyNewlog conn,"n","y","n","n","y",isAgnHostSer,mymoneych,dsph ("agent1"),dsph ("username"),norusername,norStmoney,norEndmoney,dsph ("agent1"),oldagn_money,newagn_money,supusername,logfor&"代理收入",logfor&"代理收入","云服务器产品",dsph("id"),pingzheng,type1,bank,haveget,havecheck
else
FreeHost_moneyNewlog conn,"n","y","n","n","n",isAgnHostSer,-mymoneych,dsph ("username"),dsph ("username"),norusername,norStmoney,norEndmoney,dsph ("username"),oldagn_money,newagn_money,supusername,logfor&"扣代理成本",logfor&"扣代理成本","云服务器产品",dsph("id"),pingzheng,type1,bank,haveget,havecheck
olduser_money=oldagn_money
moneych=newagn_money-oldagn_money
newuser_money=newagn_money
end if


'URL=dsserver("site")
'FreeHostsharekey=dsserver("FreeHostsharekey")
vpsname=dsph ("vpsname")
username=dsph ("username")
'homedir=dsserver("homedir")
starttime=dsph("starttime")
endtime=dsph("endtime")
type1=dsph("VPStype")

MAXcpu=dsph("MAXCPU")
CpuNum=dsph("CpuNum")
MAXRam=dsph("MAXRam")
StartRam=dsph("StartRam")
Maxmbps8=dsph("Maxmbps8")
Minmbps8=dsph("Minmbps8")

MAXspace=dsph("MAXspace")
vhdpath=dsph("vhdpath")


'varcon="modi "&username&" "&vpsname&" "&CpuNum&" "&MAXcpu&" "&MAXRam&" "&MAXspace&" "&vhdpath&" "&StartRam
'If dsserver("OStype")="Windows Server 2012" Then varcon=varcon&" 0 "&Maxmbps8*1024*1024
'temp=VPSOP(URL,FreeHostsharekey,varcon,0)'0=now'这是代理的产品，不执行VPS命令操作，调用API升级

'调用上级API
if dsph("isagentpd")=true then


dsph("isagentpd")=dshost("isagentpd")
dsph("VPSComName")=dshost("VPSComName")
dsph("VPSApiName")=dshost("VPSApiName")
dsph("VPScpdomain")=dshost("VPScpdomain")
dsph("Agnuserid")=dshost("Agnuserid")
dsph("Domain_Agent_nonemd5_6")=dshost("Domain_Agent_nonemd5_6")
dsph("Agnidc")=dshost("Agnidc")
dsph("Agnproductid")=dshost("Agnproductid")
dsph.update

VPSApiName=dsph("VPSApiName")
userid=dsph("Agnuserid")
userstr=md5(dsph("Domain_Agent_nonemd5_6")&"7i24.com")
yearAgn=Request.Form ("year")
idc=dsph("Agnidc")
productid=dsph("Agnproductid")
attach=ds("username")
tar="http://"&VPSApiName&"/api/cloudapi.asp?userid="&userid&"&userstr="&userstr&"&productid="&dshost("Agnproductid")&"&vpsname="&dsph ("vpsname")&"&action=update&attach="&attach
'Response.write tar
temp=ONLY_GET(tar,"")

vpsname=dsph ("vpsname")
FreeHost_oplog session("username"),"信息","用户区，用户升级云服务器产品(外部代理产品):("&vpsname&") "&temp

If InStr(temp,"et=ok") then
Sql="update FreeHost.FreeHost_Product_VPS set status='正常' where vpsname='"&vpsname&"'"
    conn.Execute Sql
Else
Sql="update FreeHost.FreeHost_Product_VPS set status='失败' where vpsname='"&vpsname&"'"
    conn.Execute Sql
Response.Redirect "e.asp?e=调用上级API时出错，开通失败，但费用已扣，请联系管理员手工处理！出错原因为："&server.urlencode(temp)
End If

End if

'Response.Write varcon
'Response.Write temp
FreeHost_oplog session("username"),"信息","用户自己升级，云服务器名:"&vpsname&" 返回信息："&temp

End If

if not ef then
%>
<!--#include virtual="/inc-freehost-function/htmlwrite.asp"--><%=htmlwrite("html2")%>
云服务器管理
<%=htmlwrite("html3")%>

<br>
<br>
<br>
<table width="65%"  border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#FF0000">
  <tr align="center" bordercolor="#000000" bgcolor="#999999">
    <td height="39" colspan="4"><font color="#000000">　　　</font><font color="#990000"><font color="#FF0000">恭喜您的云服务器升级成功，以下是您的资料：</font></font></td>
  </tr>
  <tr bordercolor="#000000" bgcolor="#EEEEEE">
    <td width="44%" height="30"><font color="#000000"> 　　　 您的原金额：</font></td>
    <td width="56%" colspan="3"><%=FormatNumber(olduser_money,2,-1,-1,0)%> 元</td>
  </tr>
  <tr bordercolor="#000000" bgcolor="#EEEEEE">
    <td height="41"><font color="#000000">　　　 您的金额变化：</font></td>
    <td colspan="3"><font color=red><%=moneych%> 元</font></td>
  </tr>
  <tr bordercolor="#000000" bgcolor="#EEEEEE">
    <td height="39"><font color="#000000">　　　 您的新金额：</font></td>
    <td colspan="3"><font color="#000000"><%=FormatNumber(newuser_money,2,-1,-1,0)%> 元</font></td>
  </tr>
  <tr bordercolor="#000000">
    <td height="37" bgcolor="#eeeeee"><font color="#000000"> 　　　 到期时间是:</font></td>
    <td colspan="3" bgcolor="#eeeeee"><%=temptime%></td>
  </tr>
  <tr bordercolor="#000000" bgcolor="#eeeeee">
    <td colspan="4"><font color="#000000">　　　</font><font color="#000000">　　　</font><font color="#000000">　</font><font color="#000000">　　　</font><font color="#000000">　　　</font><font color="#000000"> 　　　</font> 　</td>
  </tr>
  <tr align="center" bordercolor="#000000">
    <td height="34" colspan="4" bgcolor="#999999"><font color="#990000">　我　们　为　您　提　供　优　质　的　服　务！</font></td>
  </tr>
</table>
<br>

<br>
<br>


<%=htmlwrite("html4")%>
<%end if
if ef then
Response.Redirect "e.asp?e="&ec
end if%>
