<?php
	session_start();
	
	require_once("db/db.php");	
	require_once("common/common.php");
	
	//设置统一语言格式
	header("Content-type: text/html; charset=gb2312"); 
	
	$strSelID=trim($_REQUEST['rmdoid']);	//1入住登记，2修改
	$strChainNo=trim($_REQUEST['chainno']);	
	$strRoomPrice1=trim($_REQUEST['roomprice']);
	$intCheckDays=trim($_REQUEST['checkdays']);
	$strCheckOut=trim($_REQUEST['checkout']);
	$strManName1=trim($_REQUEST['manname']);
	$intManSex=trim($_REQUEST['mansex']);	//1男，2女
	$strTelNo1=trim($_REQUEST['telno']);
	$strCardType1=trim($_REQUEST['cardtype']);
	$strCardNo1=trim($_REQUEST['cardno']);
	$strRemarks1=trim($_REQUEST['remarks']);	//备注
	$strCustomType1=trim($_REQUEST['customtype']);
	$intIsTimeRoom=trim($_REQUEST['istimeroom']);	//1钟点房,0日租
	
	$strRoomPrice= iconv('utf-8','gb2312',$strRoomPrice1);
	$strTelNo= iconv('utf-8','gb2312',$strTelNo1);
	$strManName= iconv('utf-8','gb2312',$strManName1);
	$strCardType= iconv('utf-8','gb2312',$strCardType1);
	$strRemarks= iconv('utf-8','gb2312',$strRemarks1);
	$strCustomType= iconv('utf-8','gb2312',$strCustomType1);
	$strCardNo= iconv('utf-8','gb2312',$strCardNo1);

	
	if(strlen($strSelID)<1)
	{
		$strSelID='0';
	}
	
	if($intIsTimeRoom=='1')
	{
		$intIsTimeRoom='1';
	}
	else
	{
		$intIsTimeRoom='0';
	}
	
	$strTradeMan=trim($_SESSION['username']);
	
	if($strSelID=='2')
	{
		//如果是修改
		$lngBListNo= trim($_REQUEST['rblistno']);
		$strOrderNo= trim($_REQUEST['orderno']);
		if(strlen($lngBListNo)<1)
		{
			$lngBListNo='0';
		}
		$strManSex1='男';
		if($intManSex=='2')
		{
			$strManSex1='女';
		}
		$strManSex= iconv('utf-8','gb2312',$strManSex1);
		
		//1.判断原来是否是钟点房
		$intIsTimeRoomOld=0;
		$strSubRoomNo='';
		$sql=sprintf("SELECT Sited_TimeFlag,Room_No FROM TR_Order_Room_List WHERE ListNo=%s",$lngBListNo);
		$result = querySql($sql);		
		if($row = fetch_array($result))
		{	
			$intIsTimeRoomOld=$row['Sited_TimeFlag'];
			$strSubRoomNo=$row['Room_No'];
		}
		freeResult($result);
								
		//1.开始更新保存信息
		$sql = sprintf("UPDATE TR_Order_Room_List SET RoomPrice=%s,Sited_Days=%s,Cancel_Date='%s',DefCancel_Time='%s',Man_Name='%s',Man_Sex='%s',
							Man_TelNo='%s',Man_IDType='%s',Man_IDNo='%s',Remarks='%s',Sited_TimeFlag=%s WHERE ListNo=%s And Room_BillState<3",$strRoomPrice,$intCheckDays,$strCheckOut,$strCheckOut,$strManName,$strManSex,
							$strTelNo,$strCardType,$strCardNo,$strRemarks,$intIsTimeRoom,$lngBListNo);
		executeSql($sql);
		
		
		if ($intIsTimeRoomOld>0 And $intIsTimeRoom<1)
		{
			//原来是钟点房，现在不是钟点房
			$strDefText1='入住';
			$strDefText= iconv('utf-8','gb2312',$strDefText1);
			$sql = sprintf("EXECUTE TR_SetRoomState '%s',2,0,'%s','','%s','%s',0,0",$strSubRoomNo,$strTradeMan,$strChainNo,$strDefText);
			executeSql($sql);
        }
		
		if ($intIsTimeRoomOld<1 And $intIsTimeRoom>0)
		{
			//原来不是钟点房，现在是钟点房
			$strDefText1='钟点房';
			$strDefText= iconv('utf-8','gb2312',$strDefText1);
			$sql = sprintf("EXECUTE TR_SetRoomState '%s',3,0,'%s','','%s','%s',0,0",$strSubRoomNo,$strTradeMan,$strChainNo,$strDefText);
			executeSql($sql);
		}
		
		//2.开始更新主单信息
		$sql = sprintf("UPDATE TR_Order_Inform SET Custom_Type='%s' WHERE Order_No='%s'",$strCustomType,$strOrderNo);
		executeSql($sql);
		
		echo '*1*';	//成功
		exit;
	}
	else
	{
		//如果是登记入住
		$strRoomNo1=trim($_REQUEST['roomno']);
		$strRoomNo= iconv('utf-8','gb2312',$strRoomNo1);
	
		$strPayType1=trim($_REQUEST['paytype']);
		$strPayValue=trim($_REQUEST['payvalue']);
		$strPayText1=trim($_REQUEST['payText']);
	
		$strPayType= iconv('utf-8','gb2312',$strPayType1);
		$strPayText= iconv('utf-8','gb2312',$strPayText1);
		
		if(strlen($strPayValue)<1)
		{
			$strPayValue='0';
		}
	}
	
	
	if(strlen($strRoomPrice)<1)
	{
		$strRoomPrice='0';
	}
	if(strlen($intCheckDays)<1)
	{
		$intCheckDays='1';
	}
	
	
	
	
	//echo $strRoomNo.'+/+'.$strRoomPrice.'+/+'.$intCheckDays.'+/+'.$strCheckOut.'+/+'.$strManName.'+/+'.$intManSex.'+/+'.$strTelNo.'+/+'.$strCardType.'+/+'.$strCardNo.'**'.$strRemarks.'+/+'.$strPayType.'+/+'.$strPayValue.'+/+'.$strPayText;
	//exit;

	
	
	//判断房间状态是否为锁房状态
	//1.获取客房流水号 ,a.Room_No,a.Keep_Flag,a.Keep_Mark
	$strRListNo=0;	//状态:0空房,1预订房,2在住房,3钟点房,4自用房,5退客房,6维修房,7锁房
	$sql = sprintf("SELECT a.ListNo FROM TR_Room_Inform a,TR_Room_Type b WHERE a.TypeListNo=b.ListNo AND b.Chain_No='%s' And a.Room_No='%s' And a.Room_State=0",$strChainNo,$strRoomNo);
	$result = querySql($sql);
	if($row = fetch_array($result))
	{
		$strRListNo = $row['ListNo'];
	}
	freeResult($result);	//释放数据库
		
	//判断是否有该房间的锁房信息(自助机必须先锁房再入住)
	if($strRListNo<1)
	{
		echo "*0*";	//无此房号,或此房号不是空净房
	}
	else
	{
		//2.开始创建入住登记,根据房间流水号和房间,以及宾客资料创建入住单
		$strRateName='';//iconv('utf-8','gb2312',$strTemp);
		$strManNation='';//iconv('utf-8','gb2312',$strTemp);
		$strManNationality='';//iconv('utf-8','gb2312',$strTemp);
		$datManBorth='1900-01-01';
		$strManArea='';
		$strManAddress='';
		
		$strRetCode = '';
		$sql = sprintf("EXECUTE TRS_APP_CheckIn '%s',%s,%s,'%s','%s','%s',%s,%s,
				'%s',%s,'%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s',%s,'%s'",$strChainNo,$strRListNo,$strRoomPrice,$strRateName,$strCustomType,$strCheckOut,$intCheckDays,$intIsTimeRoom,
				$strManName,$intManSex,$strManNation,$strManNationality,$strCardType,$strCardNo,$datManBorth,$strManArea,$strManAddress,$strTelNo,$strTradeMan,$strRemarks,$strPayType,$strPayValue,$strPayText);

		//echo $sql;
		//exit;
		
		$result = querySql($sql);
		if($row = fetch_array($result))
		{
			$strRetCode = $row['RetCode'];
		}
		freeResult($result);	//释放数据库
		closeDb();				//关闭数据库
		
		if(strlen($strRetCode)>1)
		{
			echo '*1*';	//成功
		}
		else
		{
				
			echo '*2*';	//失败
		}
		
	}

?>


