<?php
	session_start();
	
	require_once("db/db.php");	
	require_once("common/common.php");
	
	//设置统一语言格式
	header("Content-type: text/html; charset=gb2312"); 
	
	$price = trim($_REQUEST['price']);
	$orderno = trim($_REQUEST['orderno']);
	$payforid = trim($_REQUEST['payid']);	//1会员余额支付,2会员积分支付,
	
	//1.判断支付方式是否正确
	if($payforid!='1' & $payforid !='2')
	{
		$strtemp='支付方式选择不正确';
		echo "<script>window.location.href='orderpaysel.php?selbillno=".$orderno."&payer=支付失败:".$strtemp."';</script>";
		exit;
	}
	
	$main_vipno = $_SESSION["main_vipno"];
	//2.得到默认分店
	$strchainno = '';
	
	$sql = sprintf("SELECT Chain_No FROM Chain_Infor Where IsDefChain>0");
	$result = querySql($sql);
	if($row = fetch_array($result))
	{
		$strchainno = trim($row['Chain_No']);
	}
	freeResult($result);	

	//3.判断会员卡余额
	$strSelSubNo = '';
	$VIP_LiftSum='0';
	$VIP_IntegSum='0';
	$strVIPState='';
	$strSubState='';
	
	$sql = sprintf("SELECT a.VIP_LiftSum,a.VIP_IntegVal,a.VIP_UsedSum,b.Sub_State,a.VIP_State,b.Sub_No FROM VIP_Main a,VIP_Item b WHERE a.VIP_NO=b.VIP_NO AND b.Sub_No='%s'", $main_vipno);

	$result = querySql($sql);
	if($row = fetch_array($result))
	{
		$VIP_LiftSum = $row['VIP_LiftSum']; 	//会员账户余额
		$VIP_IntegSum = $row['VIP_IntegVal']; 	//会员积分
		$strSelSubNo = trim($row['Sub_No']); 	//当前会员号
		$strVIPState = trim($row['VIP_State']);
		$strSubState = trim($row['Sub_State']);
	}
	freeResult($result);	
	
	//如果会员未关联到帐单中，则更新帐单中的会员号为当前会员号
	$sql = sprintf("UPDATE WX_Bill_Inform SET Sub_No='%s' WHERE Bill_No='%s'", $main_vipno,$orderno);
	executeSql($sql);
	
	//计算应收积分值
	$fltintegralcashrate=1;
	$sql=sprintf("SELECT IntegralCashRate from VIP_Config");
	$result = querySql($sql);
	
	if($row = fetch_array($result))
	{
		$fltintegralcashrate = $row['IntegralCashRate'];
	}
	freeResult($result);	//释放数据库
	
	$fltPayIntegVal = round($fltintegralcashrate * $price,0);
	
	
	//判断值
	if(strlen($strSelSubNo)<1)
	{
		closeDb();
		$strtemp='会员账户不存在';
		echo "<script>window.location.href='orderpaysel.php?selbillno=".$orderno."&payer=支付失败:".$strtemp."';</script>";
		exit;
	}
	
	if($strVIPState!='在册' | $strSubState!='在册')
	{
		closeDb();
		$strtemp='会员账户停止';
		echo "<script>window.location.href='orderpaysel.php?selbillno=".$orderno."&payer=支付失败:".$strtemp."';</script>";
		exit;
	}
	
	$strPayDoID ='0';
	if($payforid=='1')
	{
		if($VIP_LiftSum<$price)
		{
			closeDb();
			$strtemp='会员账户余额不足';
			echo "<script>window.location.href='orderpaysel.php?selbillno=".$orderno."&payer=支付失败:".$strtemp."';</script>";
			exit;
		}
	}
	elseif($payforid=='2')
	{
		if($VIP_IntegSum<$fltPayIntegVal)
		{
			closeDb();
			$strtemp='会员积分不足';
			echo "<script>window.location.href='orderpaysel.php?selbillno=".$orderno."&payer=支付失败:".$strtemp."';</script>";
			exit;
		}	
		$strPayDoID ='1';
	}
	
	//4.开始提交支付
	//@intDoType	INT,		--0会员账户,1会员积分,2电子券,3微信支付
	//@Sub_No	VARCHAR(14),
	//@Bill_No	VARCHAR(16)
	
	$sql = sprintf("EXECUTE WXG_BillPayForDo %s,'%s','%s'", $strPayDoID, $orderno,$strchainno);
	if(executeSql($sql))
	{
		//支付成功返回到订单界面
		$strgourl = 'Location: orderlist.php?bstat=1';
	}
	else
	{
		//支付失败
		$strgourl = 'Location: ordermodify.php?selbillno'.$orderno;
	}
	closeDb();
	header($strgourl);	
?>



