<!--#include virtual="/inc/conn.asp"-->
<!--#include virtual="/inc/chkuser.asp"-->
<%
call lognamecode()
call logquanxian("d2",1)

set rsok=server.CreateObject("adodb.recordset")

out_trade_no=trim(SafeRequest("id"))
did=SafeRequest("did")
trade_no=SafeRequest("id")
url=SafeRequest("url")

if out_trade_no="" then call PopErr("请检查订单号")
if did="" then call PopErr("请检查订单号")

ddku=getddku(out_trade_no)

  


'自收订单转失败
if SafeRequest("act")="zishoushibai" then

rsok.open "select * from "&ddku&" where aydingdansum='"&out_trade_no&"'",conn,1,3	

	if not(rsok.bof and rsok.eof) then
	if trim(rsok("qudao"))<>"自收" and trim(rsok("qudao"))<>"平台代收" and trim(rsok("qudao"))<>"商家自收" then call PopErr2("未找到该自收订单","ik_admindd.asp?ddku="&ddku&"&act=one")
	
			rsok("zishouzt")=1
			rsok.update
			rsok.close
			
			'if url="shou2" then
			'response.Redirect "admindd_zishou2.asp"
			'else
			%>
			<script language=javascript>  
			alert("状态改为联系客服-成功");
			this.window.opener = null;  
			window.close();  
			</script>
			<%
			'end if
			response.End()
			
	
	end if


response.End()
end if


'自收订单转关闭
if SafeRequest("act")="zishouoff" then

rsok.open "select * from "&ddku&" where aydingdansum='"&out_trade_no&"'",conn,1,3	

	if not(rsok.bof and rsok.eof) then
	if trim(rsok("qudao"))<>"自收" and trim(rsok("qudao"))<>"平台代收" and trim(rsok("qudao"))<>"商家自收" then call PopErr2("未找到该自收订单","ik_admindd.asp?ddku="&ddku&"&act=one")
	
			rsok("ddchaoshi")=1
			rsok.update
			rsok.close
			
			conn.execute "update aybuylx set ddchaoshi='1' where aydddsum='"&out_trade_no&"'"
			
			'if url="shou2" then
			'response.Redirect "admindd_zishou2.asp"
			'else
			%>
			<script language=javascript>  
			alert("关闭-成功");
			this.window.opener = null;  
			window.close();  
			</script>
			<%
			'end if
			response.End()
			
	
	end if


response.End()
end if



'自收订单 转成功
if SafeRequest("act")="zishouok" then
		
	weifenpei=0
	rs.open "select * from "&ddku&" where aydingdansum='"&out_trade_no&"' and ayddsuo=0",conn,1,3
	if not(rs.bof and rs.eof) then
		ayddcontact=trim(rs("ayddcontact"))
		dailiku=getkamikuname(rs("aydingdanuser"))	'本商户卡密库
		goodid=rs("ayddshangid") 			'本商户商品ID
		aydingdanuser=rs("aydingdanuser")
		qudao=trim(rs("qudao"))
		aydingdanzj=rs("aydingdanzj")
		
		
		'查询代理信息
		dailikuo=trim(rs("dailikuo"))'代理关系
		if dailikuo<>"" then
		dailistr=split(dailikuo,"|")
		
			'获取所有代理商ID 写入卡密 由代理卖出
			aykadstr=""
			For dailibuysum=0 To Ubound(dailistr) 
				didstr=split(dailistr(dailibuysum),",")
				aykadstr=aykadstr&tocunid(didstr(3))&","
			next
		
		dailiuserstr=split(dailistr(Ubound(dailistr)),",")
		dailiku=trim(dailiuserstr(5))	'总代卡密库
		goodid=dailiuserstr(1)			'总代商品ID
		end if
		
		'已分配卡密数量	
		yifenpei=getddokkami(trim(rs("aydingdansum")),rs("aydingdanuser"),dailiku)
		'未分配卡密数量
		weifenpei=ccur(rs("ayddquantity"))-ccur(yifenpei)
		
		'获取库存卡数量
		set kuncunnum=conn.execute("select count(*) as num from "&dailiku&" where aykagoodid="&goodid&" and aykayesno=0")
		kuncun=kuncunnum("num")
	
		if ccur(kuncun)<ccur(weifenpei) then
		call PopErr("库存卡密不足")
		response.End()
		end if
			
		conn.execute "update top ("&clng(weifenpei)&") "&trim(dailiku)&" set aykayesno=1,aykakai='"&now()&"',aykasuoyoudd='"&out_trade_no&"',aykatel='"&ayddcontact&"',aykadstr='"&aykadstr&"' where aykagoodid="&clng(goodid)&" and aykayesno=0"	
			
		rs("kamino")=0
		rs("aydingdanzt")=1
		rs.update
		rs.close
		
		if qudao="平台代收" then
			set rs2=server.CreateObject("adodb.recordset")
			jiekoulirun=0.6
			if ccur(aydingdanzj)>=5 then yingkouyongjin=0.006*aydingdanzj:else:yingkouyongjin=0.03:end if
			
			rs2.open "select * from ayname where aynameid="&aydingdanuser&"",conn,1,3
			yongjin=rs2("yongjin")
			if ccur(yongjin)<=ccur(yingkouyongjin) then
			rs2("yongjin")=0
			else
			rs2("yongjin")=ccur(rs2("yongjin"))-ccur(yingkouyongjin)
			end if
			rs2.update
			rs2.close
		end if
	%>
	<script language=javascript>  
	alert("操作成功");
	this.window.opener = null;  
	window.close();  
	</script>
	<%
	end if
	rs.close
response.End()
end if







weifenpei=0

rs.open "select * from "&ddku&" where aydingdansum='"&out_trade_no&"' and ayddsuo=0",conn,1,3

if not(rs.bof and rs.eof) then
	ayddcontact=trim(rs("ayddcontact"))
	dailiku=getkamikuname(rs("aydingdanuser"))	'本商户卡密库
	goodid=rs("ayddshangid") 			'本商户商品ID
	
	'查询代理信息
	dailikuo=trim(rs("dailikuo"))'代理关系
	if dailikuo<>"" then
	dailistr=split(dailikuo,"|")
	
		'获取所有代理商ID 写入卡密 由代理卖出
		aykadstr=""
		For dailibuysum=0 To Ubound(dailistr) 
			didstr=split(dailistr(dailibuysum),",")
			aykadstr=aykadstr&tocunid(didstr(3))&","
		next
	
	dailiuserstr=split(dailistr(Ubound(dailistr)),",")
	dailiku=trim(dailiuserstr(5))	'总代卡密库
	goodid=dailiuserstr(1)			'总代商品ID
	end if
	
	'已分配卡密数量	
	yifenpei=getddokkami(trim(rs("aydingdansum")),rs("aydingdanuser"),dailiku)
	'未分配卡密数量
	weifenpei=ccur(rs("ayddquantity"))-ccur(yifenpei)
	
	'获取库存卡数量
	set kuncunnum=conn.execute("select count(*) as num from "&dailiku&" where aykagoodid="&goodid&" and aykayesno=0")
	kuncun=kuncunnum("num")


	
	if ccur(kuncun)<ccur(weifenpei) then
	call PopErr("库存卡密不足")
	response.End()
	end if
		
	conn.execute "update top ("&clng(weifenpei)&") "&trim(dailiku)&" set aykayesno=1,aykakai='"&now()&"',aykasuoyoudd='"&out_trade_no&"',aykatel='"&ayddcontact&"',aykadstr='"&aykadstr&"' where aykagoodid="&clng(goodid)&" and aykayesno=0"	
		
	rs("kamino")=0
	rs("aydingdanzt")=1
	rs.update
	rs.close


	call logadd(request.Cookies("aynameid"),"管理后台订单补发卡密成功",0,0)
%>
			<script language=javascript>  
			alert("补发卡密成功");
			this.window.opener = null;  
			window.close();  
			</script>

<%
else
	
	call PopErr2("未找到该订单","ik_admindd.asp")

end if
rs.close


%>