<?php
	session_start();
	
	require_once("db/db.php");	
	require_once("common/common.php");
	
	//设置统一语言格式
	header("Content-type: text/html; charset=gb2312"); 

	$strChainNo=trim($_REQUEST['chainno']);	
	$strBListNo=trim($_REQUEST['rblistno']);
		
	if(strlen($strBListNo)<1)
	{
		$strBListNo='0';
	}

	$strTradeMan=trim($_SESSION['username']);
		
	//1.判断单据状态是否为入住状态(主单是否为预订或取消状态，同时客房是否为已退房）
	$intOrderState=0;
	$intBillState=0;
	$strNewRoomNo='';
	$strOrderNo='';
	$sql=sprintf("SELECT b.Order_State,a.Room_BillState,a.Room_No,b.Order_No FROM TR_Order_Room_List a,TR_Order_Inform b WHERE b.Order_No=a.Order_No And a.ListNo=%s",$strBListNo);
	$result = querySql($sql);		
	if($row = fetch_array($result))
	{	
		$intOrderState=$row['Order_State'];
		$intBillState=$row['Room_BillState'];
		$strNewRoomNo=$row['Room_No'];
		$strOrderNo=$row['Order_No'];
	}
	freeResult($result);
	
	if($intBillState<>3 OR $intOrderState>1)	//预订0,入住1,取消2,完成3
	{
		//客房未退房或单据已结账，不能恢复入住！
		echo "*2*";
		exit;
	}
		
	//2.判断目标房间的状态 状态:0空房,1预订房,2在住房,3钟点房,4自用房,5退客房,6维修房,7锁房
	//判断该房间是否为空房或脏房（如果不是则不允许）
	$intBillState=0;
	$sql=sprintf("SELECT a.Room_State FROM TR_Room_Inform a,TR_Room_Type b WHERE a.TypeListNo=b.ListNo And b.Chain_No='%s' And a.Room_No='%s'",$strChainNo,$strNewRoomNo);
	$result = querySql($sql);		
	if($row = fetch_array($result))
	{	
		$intBillState=$row['Room_State'];
	}
	freeResult($result);
	if($intBillState<>0 and $intBillState<>5)
	{
		//目标房间已被占用，不能恢复入住！
		echo "*3*";
		exit;
	}
	
	//3.判断是否有预排房信息冲突提示
	$intBillState=0;
	$sql=sprintf("SELECT COUNT(a.Order_No)AS BookCount FROM TR_Order_Room_List a,TR_Order_Inform b WHERE a.Order_No=b.Order_No AND b.Order_State=0 
					AND (a.Room_BillState=0 OR a.Room_BillState=1) AND b.Chain_No='%s' AND a.Cancel_Date>GETDATE() AND a.Room_No='%s'",$strChainNo,$strNewRoomNo);
	$result = querySql($sql);		
	if($row = fetch_array($result))
	{	
		$intBillState=$row['BookCount'];
	}
	freeResult($result);
	if($intBillState>0)
	{
		//房间有预排房信息，不能恢复入住！
		echo "*4*";
		exit;
	}
	
	//4.开始执行恢复退房操作的存储过程
	$sql=sprintf("EXECUTE TR_OrderRoomReBackSited '%s',%s,'%s',1",$strOrderNo,$strBListNo,$strTradeMan);
    $result = querySql($sql);
	
	echo '*1*';	//成功

?>


