<%
Function DoPayMoneyIDC()


UserpaymoneyEnd=Userpaymoney*forpayyear
if isRepaymoney then UserpaymoneyEnd=Repaymoney*forpayyear'管理员设置单独续费价

UserpaymoneyEnd=Clng(FormatNumber(UserpaymoneyEnd,0,-1,0,0))'四舍五入
'FormatNumber(Expression[,NumDigAfterDec[,IncLeadingDig[,UseParForNegNum[,GroupDig]]]]) 
'If Request("chmoney")<>"" and session("agentusername")<>"" and Request("usermoney")<>"" Then UserpaymoneyEnd=Clng(Request("usermoney"))'代理手工设定扣费金额

'If Request("chmoney")="" and session("agentusername")<>"" Then UserpaymoneyEnd=0'代理手工设定扣费金额(不扣用户费用时)

if Request("usermoney")<0 then
	ef=True
	ec="很抱歉,不能设为负。"
end if

if AgnRunasUser=true then UserpaymoneyEnd=0
'2005-5-10
'以代理的身份进入用户区

if UserpaymoneyEnd>RsUserds("usermoney") then
	ef=True
	ec="很抱歉,此用户的可用余额为("&RsUserds("usermoney")&"元)，需要支付的金额为："&UserpaymoneyEnd&" 元，余额不足，请您先充值。"
end If



if not ef then
olduser_money=RsUserds("usermoney")
RsUserds("usermoney")=RsUserds("usermoney") - UserpaymoneyEnd
moneych=UserpaymoneyEnd
RsUserds.update'扣费
newuser_money=RsUserds("usermoney")
session("moneylog_pdid")=RsUserds ("username")&now'域名可以直接用pid
'Log
'FreeHost_moneylog conn,RsUserds ("username"),RsUserds ("username"),moneych,logfor&forpaytype,forpaytype,session("moneylog_pdid"),"","n"
if AgnRunasUser=false then FreeHost_moneyNewlog conn,"y","n","n","n","n",isAgnHostSer,moneych,RsUserds ("username"),RsUserds ("username"),RsUserds ("username"),olduser_money,newuser_money,agnusername,agnStmoney,agnEndmoney,supusername,logfor&forpaytype,logfor&forpaytype,forpaytype,session("moneylog_pdid"),pingzheng,type1,bank,haveget,havecheck


end if

if RsUserds("agent1")="n" or ef then Exit Function'不是代理名下用户
Set dsagent = Server.CreateObject("ADODB.Recordset")	
		Sql="Select * from FreeHost.FreeHost_AGENT where username='"&RsUserds("agent1")&"'"
		dsagent.Open Sql,conn,1,3
If dsagent.Eof Then
		ef=True
		ec="代理会员名不存在！"'用户的直接代理
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='"&forpaytype&"' and username='"&RsUserds ("agent1")&"' and id="&forpayid&""
		dsagnpri.Open Sql,conn,1,1
if dsagnpri.eof then
myrealcost=RsDlist("price"&dsagnettype("id"))'得到产品定义中的此级别的代理价
'if isreplay then myrealcost=RsDlist("usermoneyagnrepay")'续费
else
myrealcost=dsagnpri("usermoney")'如果手工定义过此代理的价格就提取此价格
'if isreplay then myrealcost=dsagnpri("usermoneyrepay")'续费
end if
dsagnpri.close
if isRepaymoney then myrealcost=Repaymoney'管理员设置单独续费价
end if

if not ef then
myrealcost=myrealcost*forpayyear'时间
myrealcost=Clng(FormatNumber(myrealcost,0,-1,0,0))'四舍五入
'if forpayyear=999 then myrealcost=0'试用
'if pubisagnSer=true then'此类产品不存在独立模式
'myrealcost=0
'logfor=logfor&"独立模式"
'2005-5-10
'代理独立模式开通
'end if

if (dsagent("usermoney")+UserpaymoneyEnd-myrealcost)<0 then
	ef=True
	ec="上级的余额不足！"
end If

end if

if not ef then
oldagn_money=dsagent("usermoney")
dsagent("usermoney")=dsagent("usermoney")+UserpaymoneyEnd-myrealcost
mymoneych=UserpaymoneyEnd-myrealcost
dsagent.update
newagn_money=dsagent("usermoney")
'Log
'FreeHost_moneylog conn,dsagent("username"),RsUserds ("username"),mymoneych,logfor&"代理收入",forpaytype,session("moneylog_pdid"),"","n"
if AgnRunasUser=false then
if mymoneych>=0 then
FreeHost_moneyNewlog conn,"n","y","n","n","y",isAgnHostSer,mymoneych,dsagent("username"),RsUserds ("username"),RsUserds ("username"),norStmoney,norEndmoney,dsagent("username"),oldagn_money,newagn_money,supusername,logfor&"代理收入",logfor&"代理收入",forpaytype,session("moneylog_pdid"),pingzheng,type1,bank,haveget,havecheck
else
FreeHost_moneyNewlog conn,"n","y","n","n","n",isAgnHostSer,-mymoneych,dsagent("username"),RsUserds ("username"),RsUserds ("username"),norStmoney,norEndmoney,dsagent("username"),oldagn_money,newagn_money,supusername,logfor&"扣除代理资金",logfor&"扣除代理资金",forpaytype,session("moneylog_pdid"),pingzheng,type1,bank,haveget,havecheck
end if
else
FreeHost_moneyNewlog conn,"n","n","n","n","n",isAgnHostSer,-mymoneych,dsagent("username"),RsUserds ("username"),norusername,norStmoney,norEndmoney,dsagent("username"),oldagn_money,newagn_money,supusername,logfor&"扣代理成本",logfor&"扣代理成本",forpaytype,session("moneylog_pdid"),pingzheng,type1,bank,haveget,havecheck
olduser_money=oldagn_money
moneych=-mymoneych
newuser_money=newagn_money
end if
end if

if dsagent("agent1")="n" or ef or forpayyear=999 or pubisagnSer or isRepaymoney then Exit Function'不是子代理或出错或是试用或是[独立模式]不返还
agent_UP=dsagent("agent1")'上级代理名
agent_this_name=dsagent("username")
agent_thisgree_cost=myrealcost'本级代理成本

'UpAgentretMoneyIDC
End Function




Function UpAgentretMoneyIDC()

if agent_UP="n" or ef then Exit Function'不是子代理
'response.write agent_UP&"<br>"
'response.write agent_thisgree_cost&"<br>"

Set dsagent = Server.CreateObject("ADODB.Recordset")	
		Sql="Select * from FreeHost.FreeHost_AGENT where username='"&agent_UP&"'"
		dsagent.Open Sql,conn,1,3
If dsagent.Eof Then
		ef=True
		ec="代理会员名不存在！"'用户的直接代理
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 dsmoney_next = Server.CreateObject("ADODB.Recordset")	
		Sql="Select * from FreeHost.FreeHost_Agent_price where prtype='"&forpaytype&"' and username='"&agent_UP&"' and id="&forpayid&""
		dsmoney_next.Open Sql,conn,1,1'上级代理的成本价
if dsmoney_next.eof then

'单独定价中不存在
myrealcost_UP=RsDlist("price"&dsagnettype("id"))'得到产品定义中子代理定价

else''单独定价中存在 上级代理的成本价
myrealcost_UP=dsmoney_next("usermoney")*forpayyear
myrealcost_UP=Clng(FormatNumber(myrealcost_UP,0,-1,0,0))'四舍五入
'if isreplay then myrealcost_UP=dsmoney_next("usermoneyrepay")*forpayyear
end if'单独定价判断

'response.write myrealcost

thisagentgetmoney=agent_thisgree_cost-myrealcost_UP

if thisagentgetmoney>0 or thisagentgetmoney=0 then'是否是正常的返回折扣
agn_next_old_money=dsagent("usermoney")
dsagent("usermoney")=dsagent("usermoney")+thisagentgetmoney'to上级代理
agn_next_new_money=dsagent("usermoney")
'FreeHost_moneylog conn,dsagent("username"),agent_this_name,thisagentgetmoney,"子代理返回的收入",forpaytype,session("moneylog_pdid"),"","n"
FreeHost_moneyNewlog conn,"n","y","n","n","y",isAgnHostSer,thisagentgetmoney,dsagent("username"),agent_this_name,"",norStmoney,norEndmoney,dsagent("username"),agn_next_old_money,agn_next_new_money,supusername,logfor&"子代理返回的收入",logfor&"子代理返回的收入",forpaytype,session("moneylog_pdid"),pingzheng,type1,bank,haveget,havecheck
Else
response.write "<br>操作失败，当前用户费用已扣，返回给上级金额中存在负数而被中止："&thisagentgetmoney
FreeHost_oplog session("username"),"信息","开通IDC编号："&session("moneylog_pdid")&"时操作失败，当前用户费用已扣，返回给上级代理:"&dsagent("username")&"中存在负数而被中止"&thisagentgetmoney
response.end
'FreeHost_moneylog conn,dsagent("username"),agent_this_name,0,"由于子代理的定价比上级代理的成本价更低而不会有返回收入",forpaytype,session("moneylog_pdid"),"","n"
end if


dsagent.update

agent_UP=dsagent("agent1")'上级代理名
agent_this_name=dsagent("username")
agent_thisgree_cost=myrealcost_UP'本级代理成本

dsmoney_next.close

UpAgentretMoneyIDC

end if
End Function


%>

