<?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']);
	
	$strchainno = $_SESSION["main_order_chainno"];
	$main_vipno = $_SESSION["main_vipno"];

	$sql = sprintf("SELECT Sum(PayFor_Value)AS SumPayFor FROM TR_Consume_List WHERE order_no='%s'",$orderno);
	$result = querySql($sql);
	$row = fetch_array($result);
	$havepayfor = $row['SumPayFor']; //预订房型
	freeResult($result);
	
	if($havepayfor>0)
	{
		closeDb();
		$strtemp='已支付成功！';
		echo "<script>window.location.href='index.php';</script>";
		exit;
	}
	//
	$sql = sprintf("SELECT a.VIP_LiftSum,a.VIP_IntegVal,a.VIP_UsedSum,b.Sub_State,a.VIP_State FROM VIP_Main a,VIP_Item b WHERE a.VIP_NO=b.VIP_NO AND b.Sub_No='%s'", $_SESSION["main_vipno"]);
	//echo $sql;
	//exit();
	$result = querySql($sql);
	$row = fetch_array($result);
	$VIP_LiftSum = $row['VIP_LiftSum']; //预订房型
	
	if($VIP_LiftSum<$price)
	{
		closeDb();
		$strtemp='会员账户余额不足';
		echo "<script>window.location.href='paysel.php?price=".$price."&orderno=".$orderno."&payer=支付失败:".$strtemp."';</script>";
		exit;
	}
	
	$_SESSION["main_wxpayforyd"]="1";
	
	$sql = sprintf("EXECUTE VIP_ScoreSaveValWeb '%s','%s','预付款',%f,0,'%s',''", $strchainno, $main_vipno, $price, $orderno);
	$result1 = querySql($sql);
	$row1 = fetch_array($result1);
	$retval = $row1['RetVal'];
	freeResult($result1);
	closeDb();
	
	if($retval>0)
	{	
		$strtemp='未知'.trim($retval);
		if($retval==1)
		{
			$strtemp='会员账户不存在';
		}
		elseif($retval==2)
		{
			$strtemp='会员账户停用';
		}
		elseif($retval==3)
		{
			$strtemp='会员账户余额不足';
		}
		$str = 'paysel.php?price=' .$price."&orderno=".$orderno."&paytype=1&payer=支付失败:".$strtemp;
	}
	else
	{
		$str = 'prepaysave.php?preprice=' .$price."&orderno=".$orderno."&paytype=1&wxbill=";
	}
	echo "<script>window.location='".$str."';</script>";

	//header('Location: prepaysave.php?preprice=' .$price."&orderno=".$orderno."&paytype=1");

	//echo $sql;
	
	//$row = fetch_array($result);
	//var_dump($row);
	

	/*
	$strroomtypeid=$_REQUEST['romtype'];				//入住房型ID
		
	$retval=0;

	$strchainno = $_SESSION["main_order_chainno"];
	$strsubno = $_SESSION["main_vipno"];
	$strcheckinday = $_SESSION["main_order_checkinday"];	//入住日期
	$strcheckoutday = $_SESSION["main_order_checkoutday"];	//入住日期
	$strroomcount = trim($_REQUEST['rmcount']);				//订房数量
	$strremarks = trim($_REQUEST['rmtxt']);					//预订说明
	$strmanname = trim($_REQUEST['rmname']);				//预订人
	$strmantelno = trim($_REQUEST['rmtelno']);				//联系电话
	$strkeeptime = trim($_REQUEST['rmkptime']);				//保留时间
	$strroomprice = trim($_REQUEST['rmprice']);				//价格
	$strbreakfast = "0";	//是否含早餐
	$strinvoice = "0";		//是否开发票
	$strneworderno = "";	//返回的单号
	
	//得到房型
	$sql=sprintf("SELECT Room_Type FROM TR_Room_Type WHERE ListNo=%s;",$strroomtypeid);
	$result = querySql($sql);
	$row = fetch_array($result);
	$strroomtype = trim($row['Room_Type']); //预订房型
	freeResult($result);
	
	
	//开始创建
	$sql = sprintf("EXECUTE TRS_CancelOrderNoBook '%s','APP预订取消订单'",$strorderno);
	
	$sql = sprintf("EXECUTE TRS_WebNewBookOrder '%s','%s','%s',%s,'%s','%s',%s,'%s','%s','%s','%s','2012-02-01 %s',0,0,'';",
				$strchainno,$strsubno,$strroomtype,$strroomprice,$strcheckinday,$strcheckoutday,$strroomcount,$strremarks,$strmanname,$strmanname,$strmantelno,$strkeeptime);

	$strRet=0;		//得到处理返回值
	$retval=0;
	
	$result = querySql($sql);
	if($row = fetch_array($result))
	{
		$strRet=$row['Res'];
		if($strRet=='1')
		{
			$retval=1;	
			//无此会员或已注销
		}
		elseif($strRet=='2')
		{
			$retval=2;
			//分店信息不正确
		}
		elseif($strRet=='3')
		{
			$retval=3;
			//可用房数不足
		}
		elseif($strRet=='4')
		{
			$retval=4;
			//保存订单失败
		}
	}
	freeResult($result);
	closeDb();
	
	if($retval<1)
	{	
		//header('Location: index.php');
		//echo "<script>window.location.href='orderlist.php';</script>";
		echo "<script>window.location.href='paysel.php?price=".$strroomprice."';</script>";
	}
	else
	{
		//重新编辑
		$strurl='ordernew.php?romtype='.$strroomtypeid.'&stext='.$retval;
		
		//header('Location: ' .$strurl);
		echo "<script>window.location.href='ordernew.php?romtype=".$strroomtypeid."&stext=".$retval."';</script>";		
	}*/
?>



