<?php
	session_start();
	
	require_once("db/db.php");	
	require_once("common/common.php");
	
	//设置统一语言格式
	header("Content-type: text/html; charset=gb2312"); 
	
	$strroomtypeid=$_REQUEST['romtype'];				//入住房型ID
		
	$retval=0;

	$strchainno = $_SESSION["main_order_chainno"];
	$strsubno = $_SESSION["main_vipnosk"];
	$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 = "";	//返回的单号
	
	$strwxudid=trim($_SESSION["main_wxudid"]);
				
	//得到房型
	$sql=sprintf("SELECT Room_Type,WXPayForVal,WXPayForRate,WXPayForFlag,WXPayForYJ FROM TR_Room_Type WHERE ListNo=%s;",$strroomtypeid);
	$result = querySql($sql);
	$row = fetch_array($result);
	$strroomtype = trim($row['Room_Type']); //预订房型
	$WXPayForVal = trim($row['WXPayForVal']);
	$WXPayForRate = trim($row['WXPayForRate']);
	$WXPayForFlag = trim($row['WXPayForFlag']);
	$WXPayForYJ = trim($row['WXPayForYJ']);
	
	$preprice = 0;
	$day = diffBetweenTwoDays($strcheckinday, $strcheckoutday);
	if($WXPayForVal != 0)
	{
		$preprice = $WXPayForVal*$day*$strroomcount;
	}
	else if($WXPayForRate != 0)
	{
		$preprice = round($strroomprice*$WXPayForRate*$day*$strroomcount*0.01,0);
	}
	else
	{
		$preprice = round($strroomprice*$day*$strroomcount,0);
	}
	$preprice=$WXPayForYJ + $preprice;
	
	//var_dump($row);
	freeResult($result);
	//exit();
	
	//开始创建
	//$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,'','%s';",
				$strchainno,$strsubno,$strroomtype,$strroomprice,$strcheckinday,$strcheckoutday,$strroomcount,$strremarks,$strmanname,$strmanname,$strmantelno,$strkeeptime,$strwxudid);

	$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='index.php#page1';</script>";
		echo "<script>window.location.href='paysel.php?price=".$preprice."&orderno=".$strRet."';</script>";
	}
	else
	{
		//重新编辑
		$strurl='ordernew.php?romtype='.$strroomtypeid.'&stext='.$retval;
		
		//header('Location: ' .$strurl);
		echo "<script>window.location.href='ordernew.php?romtype=".$strroomtypeid."&stext=".$retval."';</script>";		
	}


function diffBetweenTwoDays ($day1, $day2)
{  
	$second1 = strtotime($day1);  
	$second2 = strtotime($day2);      
	if ($second1 < $second2) {    
		$tmp = $second2;    
		$second2 = $second1;    
		$second1 = $tmp;  
	}  
	return ($second1 - $second2) / 86400;
}
?>



