<%@ Language=VBScript %>
<!--#include virtual="/inc-freehost-function/FreeHostEmail.asp"-->
<!--#include virtual="/inc-freehost-function/FreeHostFunction.asp"-->
<!--#include virtual="/inc-freehost-function/FreeHostFunctionMain.asp"-->
<%
If session("VPSAUID")="" Then
	ef=True
	ec="授权非法，有可能是超时造成的。"
elseIf session("agentusername")="" Then
	ef=True
	ec="授权非法，有可能是超时造成的。此功能只有代理可以操作。"
ElseIf not IsNumeric(session("VPSAUID"))  Then
	ef=True
	ec="产品ID应该是数字！"
elseIf session("VPSAUID")<>Clng(Request.Form ("id")) Then
	ef=True
	ec="授权非法，有可能是超时造成的。请返回上一步重新操作。"&Request.Form ("id")&"-"&session("VPSAUID")
ElseIf Request.Form ("Hostlistid")="" Then
	ef=True
	ec="请选择产品！"
ElseIf not IsNumeric(Request.Form ("Hostlistid"))  Then
	ef=True
	ec="产品号应该是数字！"
elseif IsNumeric(Request.Form ("usermoney")) Then
    If Request.Form ("usermoney")<0 then
	ef=True
	ec="很抱歉,金额不能设为负。"
	End if
end If

if not ef then
Set conn=Server.CreateObject("ADODB.Connection")
	conn.Open Application("FreeHostDBlink")
	Set dsph = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_Product_VPS where id='"&session("VPSAUID")&"'"
			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="要升级的云服务器不存在！"
end if
end If

if not ef then
if dsph("agent1")<>session("agentusername") then
	ef=True
	ec="此云服务器ID并不是当前代理名下所有！"
end If
end If

if not ef then
	Set ds = Server.CreateObject("ADODB.Recordset")	
		Sql="Select * from FreeHost.FreeHost_USER where username='"&dsph("username")&"'"
		ds.Open Sql,conn,1,3
if ds.eof 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 ("Hostlistid")&"'"
'	Response.Write sql
			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("agentusername")<>dshost("onlyusebyagn") then
	ef=True
	ec="本产品只允许特定的代理使用！"
End if
end if
end If

'if not ef then
'if dshost("allowupdate")="y" then
'	ef=True
'	ec="此产品只能直接购买不允许从别的产品升级！"
'end if
'end if
if not ef then
if dsHost("cancloud")=true then
	ef=True
	ec="弹性购买的产品不能使用本功能"
end if
end If

if not ef then
if dsph("istest")="y" 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
	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
    netby=dsserver("netby")
	ip=dsserver("ip")
Dim isAgnHostSer
if dsserver("agent1")="n" then
isAgnHostSer=false
else
isAgnHostSer=true
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
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='"& dsagent("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='"&ds("agent1")&"' 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='"&ds("agent1")&"' 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
	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="旧产品不存在！"
else
typeid=dshostold("typeid")
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 dshostold("allowupdatetoother")="y" then
'	ef=True
'	ec="很抱歉，此产品不允许向别的产品升级。"
'end if
'end if

if not ef then
if ds("agent1")<>"n" then
	Set olddsagnpri = Server.CreateObject("ADODB.Recordset")	
		Sql="Select * from FreeHost.FreeHost_Agent_price where prtype='云服务器产品' and username='"&ds("agent1")&"' 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='"&ds("agent1")&"' and id="&dshostold("id")&""
		olddsendmoney.Open Sql,conn,1,1
if olddsendmoney.eof then
oldusermoney=dshostold("usermoney")
else
oldusermoney=olddsendmoney("usermoney")
end if
olddsendmoney.close
end if
end if

'response.write isAgnHostSer
'response.end
'response.write "新产品价格："&myrealcost&" 旧产品价格："&oldmyrealcost
'response.end


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") or 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
if oldusermoneyleft<0 then oldusermoneyleft=0'防止出现特殊情况
'后面的天数用新产品所用的钱
newtypeusemoneyforuser=usermoney/365*daytoend
oldusermoneytake=newtypeusemoneyforuser-oldusermoneyleft
'if session("AgnRunasUser") Then oldusermoneytake=0'代理自己登陆用户区操作自己用户角色为0

'计算代理角色成本
oldAgentrmoneyleft=oldmyrealcost/365*daytoend
if oldAgentrmoneyleft<0 then oldAgentrmoneyleft=0'防止出现特殊情况--代理
'后面的天数用新产品所用的钱--代理
newtypeAgentmoneyforuser=myrealcost/365*daytoend
oldAgentrmoneytake=newtypeAgentmoneyforuser-oldAgentrmoneyleft
If isAgnHostSer Then
oldAgentrmoneytake=0'独立模式服务器
If dsph("username")=dsph("agent1") Then oldusermoneytake=0'代理操作自己的独立模式的云服务器产品
End if

end If'结束代理判断
end If

if not ef Then'处理手工设置扣费的情况
If dsph("username")<>dsph("agent1") Then
If Request.Form ("chmoney")<>"" And IsNumeric(Request.Form ("usermoney")) Then
If Request.Form ("usermoney")>=0 then
oldusermoneytake=Request.Form ("usermoney")
agenthandmoney=true
End if
End If
End If
End if

if not ef Then'处理手工设置扣费的情况
If dsph("username")=dsph("agent1") Then
oldusermoneytake=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
if Request.Form ("Submit")="只计算升级费用" then

moneych=oldusermoneytake
'if session("AgnRunasUser") Then moneych=oldAgentrmoneytake
%>
<!--#include virtual="/inc-freehost-function/template-vpsadm-top.asp"-->
　云服务器产品升级

<br><br><br>
<%If dsph("username")<>dsph("agent1") Then%><%=dsph("username")%>用户升级到此产品所需金额为：<%=moneych%> 元<br><%End if%>
 <font color=red>(<%=dsph("agent1")%>代理成本金额为：<%=oldAgentrmoneytake%> 元)</font><br>
<%If dsph("username")<>dsph("agent1") Then%> <font color=red>(<%=dsph("agent1")%>代理返还金额为：<%=-oldAgentrmoneytake+oldusermoneytake%> 元)</font><br><%End if%>
<%If isAgnHostSer Then%> <font color=red>(独立模式)</font><%End if%>
<!--#include virtual="/inc-freehost-function/template-vpsadm-end.asp"-->
<%

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








'dsph("useper")=100

'response.write dsph("subweb") 
'response.end
starttime=dsph ("endtime")
temptime=dsph ("endtime")


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.update
logfor="(AG)升级云服务器产品(云服务器名"&dsph("vpsname")&")"
if isAgnHostSer then logfor="(AG)升级云服务器产品独立模式(云服务器名"&dsph("vpsname")&")"
If agenthandmoney=True Then logfor=logfor&"(AG)手工指定扣费"&Request.Form ("usermoney")&"元(云服务器名"&dsph("vpsname")&")"

FreeHost_moneyNewlog conn,"y","n","n","n","n",isAgnHostSer,moneych,dsph ("username"),dsph ("agent1"),dsph ("username"),olduser_money,newuser_money,agnusername,agnStmoney,agnEndmoney,supusername,logfor,logfor,"云服务器产品",dsph("id"),pingzheng,type1,bank,haveget,havecheck

if newagn_money-oldagn_money>=0  Then
FreeHost_moneyNewlog conn,"n","y","n","n","y",isAgnHostSer,mymoneych,dsph ("agent1"),dsph ("agent1"),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 ("agent1"),dsph ("agent1"),norusername,norStmoney,norEndmoney,dsph ("agent1"),oldagn_money,newagn_money,supusername,logfor&"代理扣费",logfor&"代理扣费","云服务器产品",dsph("id"),pingzheng,type1,bank,haveget,havecheck
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")

MaxIOPS=dsph("MaxIOPS")
MaxIOPS2=dsph("MaxIOPS2")

varcon="modi "&username&" "&vpsname&" "&CpuNum&" "&MAXcpu&" "&MAXRam&" "&MAXspace&" "&vhdpath&" "&StartRam
If dsserver("OStype")="Windows Server 2012" Then varcon=varcon&" 0 "&Maxmbps8*1024*1024
If dsserver("OStype")="Windows Server 2012R2" Or dsserver("OStype")="Windows Server 2016" Then varcon=varcon&" 0 "&Maxmbps8*1024*1024&" "&dsserver("SwitchName")&" "&StartRam&" "&Replace(vhdpath,".vhd","-2.vhd")&" 10 "&(MAXspace-10)&" defaultClusterNameby7i24 "&MaxIOPS&" "&MaxIOPS2&" "&dsHost("NetCardlimtype")&" "&dsserver("SwitchName2")
If dsserver("IsCluster")=true Then varcon=Replace(varcon,"defaultClusterNameby7i24",dsserver("ClusterName"))
'modi test 1 10 1024 90 c:\vps\hjkhjh.vhd 
temp=VPSOP(URL,FreeHostsharekey,varcon,0)'0=now

'Response.Write varcon
'Response.Write temp

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

%>
<!--#include virtual="/inc-freehost-function/template-vpsadm-top.asp"-->
　云服务器产品升级

<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" bgcolor="#EEEEEE">
    <td height="39"><font color="#000000">　　　 我的原金额：</font></td>
    <td colspan="3"><font color="#000000"><%=oldagn_money%> 元</font></td>
  </tr>
  <tr bordercolor="#000000" bgcolor="#EEEEEE">
    <td height="39"><font color="#000000">　　　 我的金额变化：</font></td>
    <td colspan="3"><font color=red><%=mymoneych%></font><font color="#000000"> 元</font></td>
  </tr>
  <tr bordercolor="#000000" bgcolor="#EEEEEE">
    <td height="39"><font color="#000000">　　　 我的新金额：</font></td>
    <td colspan="3"><font color="#000000"><%=newagn_money%> 元</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>
</table>
<br>
<br>
<!--#include virtual="/inc-freehost-function/template-vpsadm-end.asp"-->
<%end if
if ef then
Response.Redirect "e.asp?e="&ec
end if%>