<?php
require("global.php");

if(!$lfjuid){
	showerr('请先登录!');
}elseif(!$webdb['daili_receive']){
	showerr('系统未启用帮商家代收货款!');
}elseif(!$webdb['rmb_pay']){
	showerr('系统未启用余额付款功能!');
}elseif($lfjdb[rmb]<=0){
	showerr('你可用余额为0!');
}

if($job=='goto'){	//用户拒绝用余额付款
	echo "<META HTTP-EQUIV=REFRESH CONTENT='0;URL=olpay.php?from_rmbpay=1&id=$ids'>";
	exit;
}

$idArray = $totalmoney = '';
$detail = explode(',',$ids);
foreach($detail AS $key=>$value){
	$idArray[intval($value)] = intval($value);
}
if(!$idArray){
	showerr('数据不存在!');
}
$query = $db->query("SELECT B.*,A.title FROM `{$_pre}join` B LEFT JOIN `{$_pre}content` A ON A.id=B.cid WHERE B.id IN (".implode(',',$idArray).") ORDER BY B.totalmoney ASC");
while($rs = $db->fetch_array($query)){
	if($step==2 && $lfjdb[rmb]>0){
		if(md5($pwd)!=$lfjdb[rmb_pwd]){
			showerr('支付密码不对!');
		}
		if($lfjdb[rmb]>$rs[totalmoney]){	//余额足够,则支付成功
			$rmb = abs($rs[totalmoney]);					
			$ifpay = 1;
			unset($idArray[$rs[id]]);
		}else{	//余额不够,先付部分贷款,然后再进一步在线支付欠款
			$rmb = abs($lfjdb[rmb]);					
			$ifpay = 0;	
		}
		if($rs[ifquit]){	//针对可退款的情况
			add_rmb($rs[uid],-$rmb,$rmb,"冻结款 购买:$rs[title]");
		}else{
			add_rmb($rs[uid],-$rmb,0,"购买:$rs[title]");
			add_rmb($rs[cuid],$rmb,0,"销售:$rs[title]");
		}
		$lfjdb[rmb] = $lfjdb[rmb] - $rmb;
		$db->query("UPDATE {$_pre}join SET ifpay='$ifpay',banktype='rmb',rmb='$rmb' WHERE id='$rs[id]'");
	}
	$totalmoney += $rs[totalmoney];
}

if($step==2){
	if($idArray){
		echo "<META HTTP-EQUIV=REFRESH CONTENT='0;URL=olpay.php?id=$ids'>";
		exit;
	}else{
		refreshto("./","恭喜你订单付款成功!",60);
	}
}


require(ROOT_PATH."inc/head.php");
require(getTpl("rmb_pay"));
require(ROOT_PATH."inc/foot.php");

?>