<?php
/**
 * Created by PhpStorm.
 * User: Steven
 * Date: 2017/3/12
 * Time: 0:40
 * 接口调用方：携程
 * 接口返回方：合作方（us）
 */

require_once dirname(__DIR__) . '/Common/Utils.php';

class Hotel
{
    public $obj_utils = '';
    public $Currency = 'RMB';

    public function __construct()
    {
        $this->obj_utils = new roomOrderUtils();
    }

    /**
     * User:Steven
     *
     * Desc:DomesticCheckRoomAvail （可定检查）
     * 1.接口调用方：携程
     * 2.接口返回方：合作方
     * 3.调用频率：订单量的10倍以上
     * 4.使用说明：携程在给酒店下订单前，调用合作方接口。通过传入客人入住信息，得 到该房型是否可订。该接口响应时间（含网络）需要在3秒以内
     */
    public function DomesticCheckRoomAvail()
    {
        $rec_info = file_get_contents("php://input");
        $OrderData = $this->obj_utils->data_to_xml($rec_info);   //将xml转化成数组
        $roomInfo = $OrderData['HotelGroupInterfaceRoomTypeListRequest'];
        $hotel = $roomInfo['Hotel'];  //酒店编号
        $Arrival = $roomInfo['Arrival '];     //到店日期
        $Departure = $roomInfo['Departure '];    //离店日期
        $Room = $roomInfo['Room '];  //房型编号
        $CurrencyCode = $roomInfo['CurrencyCode '];   //币种（可空）
        $RoomNumber = $roomInfo['RoomNumber '];  //预定间数
        $Person = $roomInfo['Person'];    //入住人数
        $BalanceType = $roomInfo['BalanceType'];  //价格类型
        foreach ($roomInfo['RoomPrices'] as $item) {
            $EffectDate = $item['EffectDate'];    //入住日期
            $Price = $item['Price'];  //卖价
            $CNYPrice = $item['CNYPrice'];    //汇率后的卖价（可空）
            $Cost = $item['Cost'];    //底价
            $CNYCost = $item['CNYCost'];      //汇率后的底价（可空）
            $BreakFast = $item['BreakFast'];  //含早餐数
            $pdo = conn();
            //$sql="CALL HT_CHECKROOM_AVAIL($Arrival,$Departure,$Room,$RoomNumber)";
            $sql = "CALL HT_CHECKROOM_AVAIL('2016-08-23','2016-08-28','152248',3)";
            if (is_object($pdo)) {
                $result = $pdo->query($sql);
                $json2 = $result->fetchAll(PDO::FETCH_ASSOC);   //返回一个包含结果集中所有行的数组
                var_dump($json2);
            }
        }
        $roomInfo_arr['RequestResponse']['RequestResult'] = array();
        $RequestResult = $roomInfo_arr['RequestResponse']['RequestResult'];
        $pdo = conn();
        //$sql="CALL HT_CHECKROOM_AVAIL($Arrival,$Departure,$Room,$RoomNumber)";
        $sql = "CALL HT_CHECKROOM_AVAIL('2016-08-23','2016-08-23','152248',3,0,0)";
        if (is_object($pdo)) {
            $result = $pdo->query($sql);
            $res_arr = $result->fetchAll(PDO::FETCH_ASSOC);   //返回一个包含结果集中所有行的数组
            //var_dump($res_arr);
            $RequestResult['Message'] = empty($res_arr) ? '不可定' : '可定';
            $RequestResult['ResultCode'] = empty($res_arr) ? 101 : 0;
            $availInfo['IsBookable'] = empty($res_arr) ? 0 : 1;
            $availInfo['Room'] = empty($res_arr) ? '' : $res_arr[0]['PROD_ID'];
            $availInfo['InterFaceAmount'] = empty($res_arr) ? '' : '';//接口订单金额
            $availInfo['ReturnDescript'] = empty($res_arr) ? '' : '';    //错误描述
            if (!empty($res_arr) && is_array($res_arr) || count($res_arr) > 0)
                foreach ($res_arr as $item) {
                    $roomPrice['RoomPrice'] = array();
                    $roomPrice['RoomPrice']['EffectDate'] = $item['RUN_DATE'];
                    $roomPrice['RoomPrice']['Price'] = 0;
                    $roomPrice['RoomPrice']['CNYPrice'] = 0;
                    $roomPrice['RoomPrice']['Cost'] = $item['HT_ORDER_PRICE'];
                    $roomPrice['RoomPrice']['CNYCost'] = 0;
                    $roomPrice['RoomPrice']['BreakFast'] = $item['HT_BREAKFAST'];
                    $availInfo['RoomPrices'][] = $roomPrice;
                }
            $RequestResult['DomesticCheckRoomAvailResponse'] = $availInfo;
            $roomInfo_arr['RequestResponse']['RequestResult'] = $RequestResult;
        }
        return $roomInfo_arr;
    }

    /**
     * User:Steven
     *
     * Desc: DomesticSubmitNewHotelOrder （新订订单）
     * 1-1.接口调用方：携程/合作方
     * 1-2.接口返回方：合作方/携程
     * 1-3.调用频率：频繁
     * 1-4.使用说明：通过该接口，提交订单给对方系统
     * @return mixed
     */
    public function DomesticSubmitNewHotelOrderRequest()
    {
        $rec_info = file_get_contents("php://input");
        $OrderData = $this->obj_utils->data_to_xml($rec_info);   //将xml转化成数组
        $orderInfo = $OrderData['DomesticSubmitNewHotelOrderRequest'];
        //var_dump($orderInfo);
        $OrderID = $orderInfo['OrderID'];     //订单号
        $InterFaceSendID = $orderInfo['InterFaceSendID']; //订单接口发送号，同一订单每次发送编号会不同，接收方需落地保存
        $Hotel = $orderInfo['Hotel']; //  酒店编号
        $HotelName = $orderInfo['HotelName']; //酒店名
        $Arrival = $orderInfo['Arrival']; //入住日期
        $Departure = $orderInfo['Departure']; //离店日期
        $EarlyArrivalTime = $orderInfo['EarlyArrivalTime'];   //最早到店时间
        $LastArrivalTime = $orderInfo['LastArrivalTime']; //最晚到店时间
        $Person = $orderInfo['Person'];  //实际入住人数
        foreach ($orderInfo['Guests']['GuestEntity'] as $item) {
            $FirstName = $item['FirstName'];
            $LastName = $item['LastName'];
            $ChinesName = $item['ChinesName'];
        }
        $MobilePhone = $orderInfo['MobilePhone']; //携程联系电话
        $Currency = $orderInfo['Currency'];   //订单币种
        $Amount = $orderInfo['Amount'];   //订单卖价总金额（原币种），预付一般不传或者传0
        $CNYAmount = $orderInfo['CNYAmount']; //订单卖价总金额（人民币），预付一般不传或者传0
        $GuaranteeType = $orderInfo['GuaranteeType']; //担保方式
        $BalanceType = $orderInfo['BalanceType']; //支付方式
        $Room = $orderInfo['Room'];   //Ctrip房型编号，供应商需要转成自己的对应编号
        $RoomPerson = $orderInfo['RoomPerson'];   //房型最大入住人数
        $RoomName = $orderInfo['RoomName'];   //房型名称
        $Quantity = $orderInfo['Quantity'];   //预定间数
        $CostAmount = $orderInfo['CostAmount'];   //订单底价金额（原币种）,现付一般不传或者传0
        $CNYCostAmount = $orderInfo['CNYCostAmount']; //订单底价金额（人民币）,现付一般不传或者传0
        foreach ($orderInfo['RoomPrices']['RoomPrice'] as $item) {       //每个RoomPrice对应一天价格
            $EffectDate = $item['EffectDate'];
            $Price = $item['Price'];      //订单卖价金额（原币种）,预付一般不传或者传0
            $CNYPrice = $item['CNYPrice'];    //订单卖价金额（人民币）,预付一般不传或者传0
            $Cost = $item['Cost'];   //订单底价金额（原币种）,现付一般不传或者传0
            $CNYCost = $item['CNYCost']; //-订单底价金额（人民币）,现付一般不传或者传0
            $BreakFast = $item['BreakFast']; //早餐数量
        }
        //先判断此房间是否可定

        $pdo = conn();
        echo $sql = "CALL HT_CHECKROOM_AVAIL($Arrival,$Departure,$Room,$Quantity,$CostAmount,$BreakFast)";
        //$sql = "CALL HT_CHECKROOM_AVAIL('2016-08-23','2016-08-23','152248',3,0,0)";
        if (is_object($pdo)) {
            $result = $pdo->query($sql);

            $json = $result->fetchAll(PDO::FETCH_ASSOC);   //返回一个包含结果集中所有行的数组
            if ($json) {
                //TODO:添加订单信息
                $sql = "";
                //返回信息
                $orderInfo_arr['RequestResponse']['RequestResult'] = array();
                $RequestResult = $orderInfo_arr['RequestResponse']['RequestResult'];
                $RequestResult['Message'] = "";
                $ResultCode['ResultCode'] = "";
                $orderStatus['OrderStatus'] = ''; //订单状态(0:发送中;1:已发送;2:发送失败;3:酒店确认成功;4:酒店 拒绝;5其他)
                $orderStatus['InterFaceConfirmNO'] = "";      //接口确认号，酒店确认号
                $orderStatus['HotelConfirmNo'] = "";  //供应商确认号
                $orderStatus['InterFaceAmount'] = ""; //接口订单金额
                $orderStatus['ReturnCode'] = "";      //错误代码
                $orderStatus['ReturnDescript'] = "";  //返回描述
                $RequestResult['DomesticSubmitNewHotelOrderResponse'] = $orderStatus;
                return $RequestResult;
            }
        }

    }


    /**
     * User:Steven
     *
     * Desc: DomesticModifyHotelOrder（修改酒店订单）
     * 1-1.接口调用方：携程/合作方
     * 1-2.接口返回方：合作方/携程
     * 1-3.调用频率：频繁
     * 1-4.使用说明：通过该接口，提交订单给对方系统
     * @return mixed
     */
    public function DomesticModifyHotelOrder()
    {
        $rec_info = file_get_contents("php://input");
        $OrderData = $this->obj_utils->data_to_xml($rec_info);   //将xml转化成数组
        $orderInfo = $OrderData['DomesticModifyHotelOrderRequest'];
        $OrderID = $orderInfo['OrderID']; //订单号
        $InterFaceSendID = $orderInfo['InterFaceSendID']; //订单接口发送号，同一订单每次发送编号会不同，接收方需落地保存
        $OldOrderID = $orderInfo['OldOrderID'];   //上一个订单号
        $Ori_OrderID = $orderInfo['Ori_OrderID']; //原始订单号
        $Hotel = $orderInfo['Hotel']; //-Ctrip酒店编号，供应商需要转成自己的对应编号
        $HotelName = $orderInfo['HotelName'];
        $Arrival = $orderInfo['Arrival'];
        $Departure = $orderInfo['Departure'];
        $EarlyArrivalTime = $orderInfo['EarlyArrivalTime'];
        $LastArrivalTime = $orderInfo['LastArrivalTime'];
        $Person = $orderInfo['Person'];
        foreach ($orderInfo['Guests'] as $k => $v) {
            $FirstName = $v;
            $LastName = $v;
            $ChinesName = $v;
        }
        $MobilePhone = $orderInfo['MobilePhone'];     //-携程联系电话
        $Currency = $orderInfo['Currency'];
        $Amount = $orderInfo['Amount'];
        $CNYAmount = $orderInfo['CNYAmount'];
        $GuaranteeType = $orderInfo['GuaranteeType'];
        $BalanceType = $orderInfo['BalanceType'];
        $Room = $orderInfo['Room'];
        $OldRoom = $orderInfo['OldRoom']; //上个预订房型
        $RoomPerson = $orderInfo['RoomPerson'];
        $RoomName = $orderInfo['RoomName'];
        $Quantity = $orderInfo['Quantity'];   //预定间数
        $CostAmount = $orderInfo['CostAmount'];   //订单底价金额（原币种）,现付一般不传或者传0
        $CNYCostAmount = $orderInfo['CNYCostAmount']; //订单底价金额（人民币）,现付一般不传或者传0
        foreach ($orderInfo['RoomPrices'] as $item) {       //每个RoomPrice对应一天价格
            $EffectDate = $item['EffectDate'];
            $Price = $item['Price'];      //订单卖价金额（原币种）,预付一般不传或者传0
            $CNYPrice = $item['CNYPrice'];    //订单卖价金额（人民币）,预付一般不传或者传0
            $Cost = $orderInfo['Cost'];   //订单底价金额（原币种）,现付一般不传或者传0
            $CNYCost = $orderInfo['CNYCost']; //-订单底价金额（人民币）,现付一般不传或者传0
            $BreakFast = $orderInfo['BreakFast']; //早餐数量
            $OldPrice = $orderInfo['OldPrice'];   //-上个订单卖价金额（原币种）,预付一般不传或者传0
        }

        //检查修改后的房型是否可定
        $pdo = conn();
        //$sql="CALL HT_CHECKROOM_AVAIL($Arrival,$Departure,$Room,$Quantity,$CostAmount,$BreakFast)";
        $sql = "CALL HT_CHECKROOM_AVAIL('2016-08-23','2016-08-23','152248',3,0,0)";
        if (is_object($pdo)) {
            $result = $pdo->query($sql);
            $json = $result->fetchAll(PDO::FETCH_ASSOC);   //返回一个包含结果集中所有行的数组
            if ($json) {
                //TODO:修改订单信息


                //返回信息
                $orderInfo_arr['RequestResponse']['RequestResult'] = array();
                $RequestResult = $orderInfo_arr['RequestResponse']['RequestResult'];
                $RequestResult['Message'] = "";
                $ResultCode['ResultCode'] = "";
                $orderStatus['OrderStatus'] = ''; //订单状态(0:发送中;1:已发送;2:发送失败;3:酒店确认成功;4:酒店 拒绝;5其他)
                $orderStatus['InterFaceConfirmNO'] = "";      //接口确认号，酒店确认号
                $orderStatus['HotelConfirmNo'] = "";  //供应商确认号
                $orderStatus['InterFaceAmount'] = ""; //接口订单金额
                $orderStatus['ReturnCode'] = "";      //错误代码
                $orderStatus['ReturnDescript'] = "";  //返回描述
                $RequestResult['DomesticModifyHotelOrderResponse'] = $orderStatus;
                return $RequestResult;
            }
        }

    }

    /**
     * User:Steven
     *
     * Desc: DomesticCancelHotelOrder （取消订单）
     * 1-1.接口调用方：携程/合作方
     * 1-2.接口返回方：合作方/携程
     * 1-3.调用频率：频繁
     * 1-4.使用说明：通过该接口，提交订单给对方系统
     * @return mixed
     */
    public function DomesticCancelHotelOrder()
    {
        $rec_info = file_get_contents("php://input");
        $OrderData = $this->obj_utils->data_to_xml($rec_info);   //将xml转化成数组
        $cancelOrderInfo = $OrderData['DomesticCancelHotelOrderRequest'];
        $OrderID = $cancelOrderInfo['OrderID'];
        $OldOrderID = $cancelOrderInfo['OldOrderID'];
        $Ori_OrderID = $cancelOrderInfo['Ori_OrderID'];
        $InterFaceSendID = $cancelOrderInfo['InterFaceSendID'];
        $InterFaceConfirmNO = $cancelOrderInfo['InterFaceConfirmNO'];
        $Hotel = $cancelOrderInfo['Hotel'];
        $HotelName = $cancelOrderInfo['HotelName'];
        $Arrival = $cancelOrderInfo['Arrival'];
        $Departure = $cancelOrderInfo['Departure'];
        $Person = $cancelOrderInfo['Person'];
        $ClientName = $cancelOrderInfo['ClientName'];
        $Room = $cancelOrderInfo['Room'];
        $RoomName = $cancelOrderInfo['RoomName'];
        $Quantity = $cancelOrderInfo['Quantity'];     //注意确认几个半闭合标签是否存在

        //TODO:取消订单逻辑;


        //返回结果
        $orderInfo_arr['RequestResponse']['RequestResult'] = array();
        $RequestResult = $orderInfo_arr['RequestResponse']['RequestResult'];
        $RequestResult['Message'] = "";
        $RequestResult['ResultCode'] = "";
        $orderStatus['OrderStatus'] = ''; //订单状态(0:发送中;1:已发送;2:发送失败;3:酒店确认成功;4:酒店 拒绝;5其他)
        $orderStatus['OrderID'] = "";
        $orderStatus['InterFaceConfirmNO'] = "";
        $orderStatus['ReturnCode'] = "";
        $orderStatus['ReturnDescript'] = "";      //错误代码
        $RequestResult['DomesticCancelhotelOrderResponse'] = $orderStatus;
        return $RequestResult;

    }

    /**
     * User:Steven
     *
     * Desc: DomesticPushOrderStatus（订单状态推送）
     * 1-1.接口调用方：携程/合作方
     * 1-2.接口返回方：合作方/携程
     * 1-3.调用频率：频繁
     * 1-4.使用说明：订单非立即确认时调用该接口，接收方收到订单后，当订单状态变化时， 需及时推送订单状
     */
    public function DomesticPushOrderStatus()
    {

    }


}