<%@ Language=VBScript %>
<!--#include virtual="/inc-freehost-function/FreeHostFunction.asp"-->
<!--#include virtual="/inc-freehost-function/FreeHostFunctionMain.asp"-->
<%'response.end
If session("username")="" Then
	ef=True
	ec="授权非法，有可能是超时造成的。"
ElseIf Request("id")<>"" And not IsNumeric(Request("id"))  Then
	ef=True
	ec="产品ID应该是数字！"
ElseIf not IsNumeric(Request("usermoney"))  Then
	ef=True
	ec="金额应该是数字！"
ElseIf Request("title")="" Then
	ef=True
	ec="每项内容都必须要输入！"
ElseIf Request("recman")="" Then
	ef=True
	ec="每项内容都必须要输入！"
ElseIf Request("postid")="" Then
	ef=True
	ec="每项内容都必须要输入！"
ElseIf Request("address")="" Then
	ef=True
	ec="每项内容都必须要输入！"
ElseIf Request("con")="" Then
	ef=True
	ec="每项内容都必须要输入！"
elseIf checkscript(Request.Form ("title")) Then
	ef=True
	ec="标题有非法字符！如<>script"
elseIf checkscript(Request.Form ("con")) Then
	ef=True
	ec="内容有非法字符！如<>script"
elseIf checkscript(Request.Form ("address")) Then
	ef=True
	ec="内容有非法字符！如<>script"
elseIf checkscript(Request.Form ("usercodeinfo")) Then
	ef=True
	ec="内容有非法字符！如<>script"
'ElseIf Request("usercodeinfo")="" Then
'	ef=True
'	ec="每项内容都必须要输入！"
ElseIf Request("sendtype")<>"快递" And Request("sendtype")<>"平邮" And Request("sendtype")<>"" 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")&"'"
		if Instr(1,sql, ";", 1)>0 or Instr(1,sql, "--", 1)>0 then
Response.Write "非法字符！"
Response.End
end if
		ds.Open Sql,conn,1,3
if ds.eof then
	ef=True
	ec="用户不存在!"
end if
end If

if not ef Then
if session("AgnRunasUser")=True then
	Set ds = Server.CreateObject("ADODB.Recordset")	
		Sql="Select * from FreeHost.FreeHost_agent where username='"&session("username")&"'"
		ds.Open Sql,conn,1,3
if ds.eof then
	ef=True
	ec="代理不存在!"
end if
end If
end If

if not ef Then
If Clng(Request("usermoney"))<Application("MinFapiaomoney") Or  Clng(Request("usermoney"))<0 Then
	ef=True
	ec="您输入的开发票金额小于最少的允许金额!"
End if
End If

if not ef Then
		Set dsuser = Server.CreateObject("ADODB.Recordset")	
Sql="Select sum(usermoney) from FreeHost.FreeHost_Fapiao where username='"&session ("username")&"' and apptime>'"&year(now)&"-1-1'"
dsuser.Open Sql,conn,1,1
usermoneyALLthisyear=dsuser(0)
dsuser.close
If isnull(usermoneyALLthisyear) Then usermoneyALLthisyear=0
	Set dsmoney = Server.CreateObject("ADODB.Recordset")	
	Sql="Select sum(abs(usermoney)) from FreeHost.FreeHost_moneylog where username='"&session ("username")&"' and isnew='y' and isdel is null and isadd='y' and attime>'"&year(now)&"-1-1' and (bank='在线支付' or bank='中行' or bank='工行' or bank='建行' or bank='农行' or bank='招行' or bank='交行' or bank='邮政' or bank='现金' or bank='公司帐号' or bank='其他' or bank='实收')"
	dsmoney.Open Sql,conn,1,1
totalmthisyear=fix(dsmoney(0))
If isnull(totalmthisyear) Then totalmthisyear=0
'Response.write sql
'Response.write (usermoneyALLthisyear+Request("usermoney"))&":1:<br>"
'Response.write totalmthisyear&":2:<br>"
'Response.end
If (usermoneyALLthisyear+Request("usermoney"))>totalmthisyear Then
	ef=True
	ec="很抱歉，申请后的本年度开票金额"&(usermoneyALLthisyear+Request("usermoney"))&"元，不能超过本年度入款金额："&totalmthisyear&"元"
End if
End If

if not ef then
	Set dsfapiao = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_Fapiao  where username='"&session ("username")&"' and id ='"&Request("id")&"' "
dsfapiao.Open Sql,conn,1,3
if Not dsfapiao.Eof Then
If dsfapiao("status")<>"等候处理" Then
	ef=True
	ec="只有状态是""等候处理""的申请才能更改。"
End If
End If
End If

if not ef Then
if dsfapiao.Eof Then
If Request("sendtype")="快递" Then
If Request("usermoney")*Application("FapiaoPaypercent")/100+Application("Fapiaokuai")>ds("usermoney") Then
	ef=True
	ec="您帐上的可用金额不足!共需要"&Request("usermoney")*Application("FapiaoPaypercent")/100+Application("Fapiaokuai")&"元"
End If
Else
If Request("usermoney")*Application("FapiaoPaypercent")/100>ds("usermoney") Then
	ef=True
	ec="您帐上的可用金额不足!共需要"&Request("usermoney")*Application("FapiaoPaypercent")/100&"元"
End If
End If
End if
End If

if not ef then
%>
<!--#include virtual="/inc-freehost-function/htmlwrite.asp"--><%=htmlwrite("html2")%>发票申请
<%=htmlwrite("html3")%><br>
<%

if dsfapiao.Eof Then
dsfapiao.addnew
dsfapiao("usermoney")=Request("usermoney")
dsfapiao("sendtype")=Request("sendtype")
isnew=true
End if

dsfapiao("username")=session("username")
dsfapiao("title")=Request("title")
dsfapiao("titleID")=Request("titleID")
dsfapiao("recman")=Request("recman")
dsfapiao("postid")=Request("postid")
dsfapiao("address")=Request("address")
dsfapiao("con")=Request("con")
dsfapiao("usercodeinfo")=Request("usercodeinfo")
dsfapiao("mobi")=Request("mobi")

dsfapiao("apptime")=Now
dsfapiao("status")="等候处理"

If session("agentname")<>"n" then
dsfapiao("agent1")=ds("agent1")
dsfapiao("agent2")="n"
Else
dsfapiao("agent1")="n"
dsfapiao("agent2")="n"
End if

dsfapiao.update

if isnew=true Then

	Set dsfapiao = Server.CreateObject("ADODB.Recordset")	
	Sql="Select top 1 * from FreeHost.FreeHost_Fapiao  where username='"&session ("username")&"' order by id desc"
dsfapiao.Open Sql,conn,1,1

olduser_money=ds("usermoney")
oldagn_money=ds("usermoney")
Dim mymoneych

If Application("Fapiaokuai")>0 And Request("sendtype")="快递" Then
mymoneych=Request("usermoney")*Application("FapiaoPaypercent")/100+Application("Fapiaokuai")
else
mymoneych=Request("usermoney")*Application("FapiaoPaypercent")/100
End if

ds("usermoney")=ds("usermoney")-mymoneych
ds.update
newuser_money=ds("usermoney")
newagn_money=ds("usermoney")
logfor="用户申请发票"
if session("AgnRunasUser")=True then logfor="直接代理申请发票"

logfor=logfor&"("&Request("title")&")"

If Application("Fapiaokuai")>0 And Request("sendtype")="快递" Then logfor=logfor&"(含快递费"& Application("Fapiaokuai")&"元)"

if session("AgnRunasUser")=True then
FreeHost_moneyNewlog conn,"n","y","n","n","n",isAgnHostSer,mymoneych,ds ("username"),ds ("username"),norusername,norStmoney,norEndmoney,ds ("username"),oldagn_money,newagn_money,supusername,logfor,logfor&"扣代理的费用","发票申请",dsfapiao("id"),pingzheng,type1,bank,haveget,havecheck
else
FreeHost_moneyNewlog conn,"y","n","n","n","n",isAgnHostSer,mymoneych,ds ("username"),ds ("username"),ds ("username"),olduser_money,newuser_money,agnusername,agnStmoney,agnEndmoney,supusername,logfor,logfor,"发票申请",dsfapiao("id"),pingzheng,type1,bank,haveget,havecheck
End If

If ds("agent1")="n" Then
Application("noticemesg")=Application("noticemesg")&logfor
End if

End If


%>
保存成功，请等候业务人员帮您处理。
<%=htmlwrite("html4")%>
<%
end if
if ef then
Response.Redirect "e.asp?e="&ec
end if%>
