<!--#include virtual="/inc/conn.asp"-->
<!--#include virtual="/inc/sitecon.asp"-->
<!--#include virtual="/inc/chkuser.asp"-->
<%
call logname("1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,99")

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

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

ddku=getddku(out_trade_no)


weifenpei=0

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

if not(rs.bof and rs.eof) then
	ayddcontact=trim(rs("ayddcontact"))
	dailiku=request.Cookies("kamiku")	'本商户卡密库
	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")),request.Cookies("aynameid"),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 PopErr2("库存卡密不足","mydingdan")
	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.update
	rs.close


	call logadd(request.Cookies("aynameid"),"订单补发卡密成功",0,0)
	call PopErr2("处理成功","mydingdan")


else
	
	call PopErr2("未找到该订单","mydingdan")

end if
rs.close


		


%>