|
- <?php
- /**
- * Created by PhpStorm.
- * User: wangxj
- * Date: 2017/4/12
- * Time: 13:54
- * $OrderID, $InterFaceSendID, $Hotel, $HotelName, $Arrival, $Departure,
- * $EarlyArrivalTime, $LastArrivalTime, $Person, $Notice, $IsHoldRoom,
- * $IsFlashLive, $Guests, $MobilePhone, $Currency, $Amount, $CNYAmount, $GuaranteeType, $BalanceType,
- * $Room, $RoomPerson, $RoomName, $Quantity, $CostAmount, $CNYCostAmount,
- * $RoomPrices, $AddOptionals, $InterFaceConfirmNo, $HotelConfirmNo, $InterFaceAmount, $ReturnCode, $ReturnDescript,
- * $OrderStatus, $FGToPP, $DepositAmount, $IsFreeSale
- */
-
- namespace backend\modules\hotel\models;
-
- use backend\common\Utils;
- use Yii;
- use common\models\BaseSupplierSale;
- use yii\base\Model;
- use yii\db\Exception;
- use backend\modules\api\logic\BaseBalance;
-
- /**
- * Class CtripOrder
- * @package backend\modules\hotel\models
- *
- * @property string $OrderID
- * @property string $OldOrderID
- * @property string $Ori_OrderID
- * @property string $InterFaceConfirmNO
- * @property string $ClientName
- * @property string $InterFaceSendID
- * @property string $Hotel
- * @property string $HotelName
- * @property string $Arrival
- * @property string $Departure,
- * @property string $EarlyArrivalTime
- * @property string $LastArrivalTime
- * @property string $Person
- * @property string $Notice
- * @property string $IF_LAST_PROD
- * @property string $IsHoldRoom,
- * @property string $IsFlashLive
- * @property array $Guests
- * @property string $MobilePhone
- * @property string $Currency
- * @property string $Amount
- * @property string $CNYAmount
- * @property string $GuaranteeType
- * @property string $BalanceType,
- * @property string $Room
- * @property string $RoomPerson
- * @property string $RoomName
- * @property string $Quantity
- * @property string $CostAmount
- * @property string $CNYCostAmount,
- * @property array $RoomPrices
- * @property string $AddOptionals
- * @property string $InterFaceConfirmNo
- * @property string $HotelConfirmNo
- * @property string $InterFaceAmount
- * @property string $ReturnCode
- * @property string $ReturnDescript,
- * @property string $OrderStatus
- * @property string $FGToPP
- * @property string $DepositAmount
- * @property string $IsFreeSale
- * @property string $Spider_order_id
- *
- */
- class CtripOrder extends Model
- {
- const ORDER_DOCKING_TYPE = 556; // 对接进来的订单
-
- const ORDER_STATUS_WAITING = 550; //待处理
- const ORDER_STATUS_ACCEPT = 551; //已接单
- const ORDER_STATUS_REJECT = 552; //已拒绝
- const ORDER_STATUS_CANCEL_APPLY = 553; //申请取消
- const ORDER_STATUS_CANCEL_REJECT = 554; //取消被拒
- const ORDER_STATUS_CANCEL_ACCEPT = 563; //已取消
-
- public $relation_supplier_id;
-
- public $OrderID;
- public $OldOrderID;
- public $Ori_OrderID;
- public $InterFaceConfirmNO;
- public $ClientName;
- public $InterFaceSendID;
- public $Hotel;
- public $Spider_hotel; //蜘蛛的酒店id,opera_hotel HOTEL_ID
- public $HotelName;
- public $Arrival;
- public $Departure;
- public $EarlyArrivalTime;
- public $LastArrivalTime;
- public $Person;
- public $Notice;
- public $IF_LAST_PROD = 1; //礼盒为2
- public $IsHoldRoom;
- public $IsFlashLive;
- public $Guests;
- public $MobilePhone;
- public $Currency;
- public $Amount;
- public $CNYAmount;
- public $GuaranteeType = 9999; //担保类型 无限制
- public $BalanceType = 'PP'; //支付方式 预付
- public $Room;
- public $Spider_room; //蜘蛛的房型id,opera_hotel_room ID
- public $Spider_parent_room_type; //蜘蛛的房型id,opera_hotel_room PARENT_ROOM_TYPE
- public $Spider_room_type; //蜘蛛的房型id,opera_hotel_room ROOM_TYPE
- public $Spider_sale_type; //渠道的销售方式
- public $Spider_order_id; //渠道的销售方式
- public $RoomPerson;
- public $RoomName;
- public $Quantity;
- public $CostAmount;
- public $CNYCostAmount;
- public $RoomPrices;
- public $CNYPrice;
- public $CNYCost;
- public $AddOptionals;
- public $InterFaceConfirmNo;
- public $HotelConfirmNo;
- public $InterFaceAmount;
- public $ReturnCode;
- public $ReturnDescript;
- public $OrderStatus;
- public $FGToPP;
- public $DepositAmount;
- public $IsFreeSale;
- public $RoomNumber;
- public $cus_list = '';
- public $room_list = '';
- public $new_room_list = '';
-
- public $white_list_flag = null;
-
- //返回值 该返回值为标准返回码,对渠道而言,需要根据渠道需要的
- const RETURN_CODE_SUCCESS = 0; //操作成功
- const RETURN_CODE_CANCEL_FAIL = 100; //取消失败/异常
- const RETURN_CODE_STOCK_FAIL = 101; //酒店满房/房量不足
- const RETURN_CODE_NOTICE_FAIL = 102; //备注不接受
- const RETURN_CODE_PRICE_FAIL = 103; //价格错误
- const RETURN_CODE_DATE_FAIL = 104; //日期错误/日期超过最大入住天数
- const RETURN_CODE_CHECKOUT = 105; //已入住不允许取消或修改
- const RETURN_CODE_EXPIRED = 106; //已过最晚取消修改时间
- const RETURN_CODE_NOT_EXIST = 107; //修改/取消原订单不存在
- const RETURN_CODE_PROGRAM_FAIL = 108; //程序错误
- const RETURN_CODE_NETWORK_FAIL = 109; //网络问题(程序错误)
- const RETURN_CODE_GUARANTEE_FAIL = 110; //担保错误
- const RETURN_CODE_HOTEL_CLOSED = 111; //酒店已停止合作
- const RETURN_CODE_MAPPING_FAIL = 112; //直连匹配错误(程序错误)
- const RETURN_CODE_REPEAT = 113; //重复预订
- const RETURN_CODE_NEED_TOTAL_GUARANTEE = 114; // 需要全额担保
- const RETURN_CODE_OVER_NUM = 115; //人数超限
- const RETURN_CODE_OVER_STOCK = 116; //客人当天累计已超间,请提供担保 重新预订
- const RETURN_CODE_SYSTEM_FAIL = 117; //服务器运行错误(程序错误)
- const RETURN_CODE_AUTH_FAIL = 118; //用户认证或授权错误(程序错误)
- const RETURN_CODE_PARAMS_FAIL = 119; //参数错误(程序错误)
- const RETURN_CODE_INFO_FAIL = 120; //预订信息不正确
- const RETURN_CODE_NOT_ALLOW_UPDATE = 121; //订单不允许修改
- const RETURN_CODE_NOT_ALLOW_CANCEL = 122; //订单不允许取消
- const RETURN_CODE_NEED_ALL_GUARANTEE = 123; //需要一律担保
- const RETURN_CODE_BREAKFAST_FAIL = 124; //早餐不一致
- const RETURN_CODE_CURRENCY_FAIL = 125; //币种错误
- const RETURN_CODE_UNAUTHORIZED = 126; //渠道未授权
- const RETURN_CODE_ROOM_CLOSED = 127; //房型已下线
- const RETURN_CODE_OTHER = 9999; //其他
-
- //携程可订检查返回code
- const RETURN_CODE_CHCEK_STOCK_FAIL = 1000;
- const RETURN_CODE_CHCEK_STOCK_LESS = 1001;
- const RETURN_CODE_CHCEK_NO_ROOM_STATUS = 1002;
- const RETURN_CODE_CHCEK_PRICE_FAIL = 1003;
- const RETURN_CODE_CHCEK_API_TIMEOUT = 1004;
- const RETURN_CODE_CHCEK_CANNOT_ANALYZE = 1005;
- const RETURN_CODE_CHCEK_RMB_FAIL = 1006;
- const RETURN_CODE_CHCEK_DATE_FAIL = 1007;
- const RETURN_CODE_CHCEK_UNKNOWN = 1008;
-
-
- //返回给渠道的订单状态
- const ORDER_STATUS_COMFIRMING = 1; //待确认 已发送到合作方,确认中
- const ORDER_STATUS_FAIL = 2; //失败 订单未发送到合作方或返回代码 为失败
- const ORDER_STATUS_COMFIRMED = 3; // 确认订单
- const ORDER_STATUS_REFUSED = 4; //拒绝订单
-
-
- CONST RETURN_MSG = [
- self::RETURN_CODE_SUCCESS => '操作成功',
- self::RETURN_CODE_CANCEL_FAIL => '取消失败/异常',
- self::RETURN_CODE_STOCK_FAIL => '酒店满房/房量不足',
- self::RETURN_CODE_NOTICE_FAIL => '备注不接受',
- self::RETURN_CODE_PRICE_FAIL => '价格错误',
- self::RETURN_CODE_DATE_FAIL => '日期错误/日期超过最大入住天数',
- self::RETURN_CODE_CHECKOUT => '已入住不允许取消或修改',
- self::RETURN_CODE_EXPIRED => '已过最晚取消修改时间',
- self::RETURN_CODE_NOT_EXIST => '修改/取消原订单不存在',
- self::RETURN_CODE_PROGRAM_FAIL => '程序错误',
- self::RETURN_CODE_NETWORK_FAIL => '网络问题(程序错误)',
- self::RETURN_CODE_GUARANTEE_FAIL => '担保错误',
- self::RETURN_CODE_HOTEL_CLOSED => '酒店已停止合作/酒店已停售/下架',
- self::RETURN_CODE_MAPPING_FAIL => '直连匹配错误(程序错误)',
- self::RETURN_CODE_REPEAT => '重复预定',
- self::RETURN_CODE_NEED_TOTAL_GUARANTEE => '需要全额担保',
- self::RETURN_CODE_OVER_NUM => '人数超限',
- self::RETURN_CODE_OVER_STOCK => '客人当天累计已超间,请提供担保 重新预订',
- self::RETURN_CODE_SYSTEM_FAIL => '服务器运行错误(程序错误)',
- self::RETURN_CODE_AUTH_FAIL => '用户认证或授权错误(程序错误)',
- self::RETURN_CODE_PARAMS_FAIL => '参数错误(程序错误)',
- self::RETURN_CODE_INFO_FAIL => '预订信息不正确',
- self::RETURN_CODE_NOT_ALLOW_UPDATE => '订单不允许修改',
- self::RETURN_CODE_NOT_ALLOW_CANCEL => '订单不允许取消',
- self::RETURN_CODE_NEED_ALL_GUARANTEE => '需要一律担保',
- self::RETURN_CODE_BREAKFAST_FAIL => '早餐不一致',
- self::RETURN_CODE_CURRENCY_FAIL => '币种错误',
- self::RETURN_CODE_UNAUTHORIZED => '该渠道未授权',
- self::RETURN_CODE_CURRENCY_FAIL => '币种错误',
- self::RETURN_CODE_ROOM_CLOSED => '房型已下线',
- self:: RETURN_CODE_OTHER => '其他'
- ];
-
-
- public function __construct(array $config = [])
- {
- $this->relation_supplier_id = Yii::$app->params['ctrip']['relation_supplier_id'];
- parent::__construct($config);
- }
-
- public function rules()
- {
- return [
- [['OrderID', 'InterFaceSendID', 'Hotel', 'HotelName', 'Arrival', 'Departure',
- 'EarlyArrivalTime', 'LastArrivalTime', 'Person',
- 'Amount', 'CNYAmount', 'Room', 'RoomPerson', 'RoomName',
- 'Quantity', 'CostAmount', 'CNYCostAmount', 'RoomPrices',], 'required', 'on' => ['new-order']],
- [['Spider_hotel', 'Spider_room'], 'required', 'on' => ['new-order']],
- ['Currency', 'validateCurrency', 'params' => ['message' => '币种验证失败。'], 'on' => ['new-order']],
- ['CNYCostAmount', 'validateCNYCostAmount', 'params' => ['message' => '价格错误'], 'on' => ['new-order']],
- ['IsHoldRoom', 'validateHoldRoom', 'params' => ['message' => '无保留房'], 'on' => ['new-order']],
- ['spider_hotel', 'validateSpider', 'params' => ['message' => 'mapping失败'], 'on' => ['new-order']],
- [['Hotel', 'Arrival', 'Departure', 'Room', 'RoomNumber', 'Currency', 'Person', 'BalanceType', 'IsHoldRoom'], 'required', 'on' => ['check-room-avail']],
- ];
- }
-
- /**
- * @inheritdoc
- */
- public function scenarios()
- {
- $scenarios['new-order'] = ['OrderID', 'InterFaceSendID', 'Hotel', 'HotelName', 'Arrival', 'Departure',
- 'EarlyArrivalTime', 'LastArrivalTime', 'Person', 'Notice', 'IsHoldRoom', 'MobilePhone', 'Currency',
- 'Amount', 'CNYAmount', 'GuaranteeType', 'BalanceType', 'Room', 'RoomPerson', 'RoomName', 'Spider_hotel', 'Spider_room',
- 'Quantity', 'CostAmount', 'CNYCostAmount',];
- $scenarios['check-room-avail'] = ['Hotel', 'Arrival', 'Departure', 'Room', 'Currency', 'RoomNumber', 'Person', 'BlanceType', 'IsHoldRoom', 'RoomPrices', 'EffectDate', 'CNYCost', 'BreakFast'];
- $scenarios['cancel-order'] = ['OrderID', 'InterFaceSendID', 'Hotel', 'HotelName', 'Arrival', 'Departure',
- 'EarlyArrivalTime', 'LastArrivalTime', 'Person', 'Notice', 'IsHoldRoom', 'MobilePhone', 'Currency',
- 'Amount', 'CNYAmount', 'GuaranteeType', 'BalanceType', 'Room', 'RoomPerson', 'RoomName', 'Spider_hotel', 'Spider_room',
- 'Quantity', 'CostAmount', 'CNYCostAmount',];
- $scenarios['order-status'] = ['OrderID', 'InterFaceSendID'];
- return $scenarios;
- }
-
- /**
- * User: wangxj
- *
- * 加载数据
- *
- * @param array $data
- * @param null $formName
- * @return bool
- */
- public function load($data, $formName = null)
- {
- $scope = $formName === null ? $this->formName() : $formName;
- if ($scope === '' && !empty($data)) {
- $this->setAttributes($data, false);
-
- $this->Arrival = substr($this->Arrival, 0, 10);
- $this->Departure = substr($this->Departure, 0, 10);
-
- //mapping酒店 id和房型id
- $this->Spider_hotel = HotelRelation::getSpiderId($this->Hotel, $this->relation_supplier_id);
- $this->Spider_room = RoomRelation::getSpiderId($this->Room, $this->relation_supplier_id);
- $room = OperaHotelRoom::findOne($this->Spider_room);
- if ($room != null) {
- $this->Spider_room_type = $room->ROOM_TYPE;
- $this->Spider_parent_room_type = $room->PARENT_ROOM_TYPE;
- }
- $supplier = \common\models\BaseSupplier::findOne(['ID' => \Yii::$app->params['ctrip']['supplier_id']]);
- if ($supplier == null) {
- return false;
- }
- $commissionRule = $supplier->getCommissionRule(BaseSupplierSale::SALE_HOTEL);
- if ($commissionRule != null && $commissionRule->COMMISION_TYPE == BaseSupplierSale::TYPE_CHANNEL) {
- $base_com = $commissionRule->BACK_PERCENT;
- $this->Spider_sale_type = $commissionRule->SALE_TYPE;
- } else if ($commissionRule == null) {
- $this->addError('Spider_sale_type', '没有对应渠道销售');
- return false;
- } else {
- $base_com = 0;
- $this->Spider_sale_type = $commissionRule->SALE_TYPE;
- }
- /*$customer_name = CtripOrder::getGuestsName($this->Guests);
- // $guests = isset($this->Guests['GuestEntity'][0]) ? $this->Guests['GuestEntity'] : $this->Guests;
- // if (is_array($guests)) {
- // foreach ($guests as $GuestEntity) {
- // $FirstName = empty($GuestEntity['FirstName']) ? '' : $GuestEntity['FirstName'];
- // $LastName = empty($GuestEntity['LastName']) ? '' : $GuestEntity['LastName'];
- // if ($customer_name == '' || (strstr($GuestEntity['ChinesName'], trim($customer_name)) == false && strstr(($FirstName . ' ' . $LastName), trim($customer_name)) == false)) {
- // $customer_name .= empty($GuestEntity['ChinesName']) ? ($FirstName . ' ' . $LastName) : $GuestEntity['ChinesName'] . ' ';
- // }
- // }
- // }
- //对接的电话号码都是假的,不存
- $this->MobilePhone = '';
- $customer_name = trim($customer_name); //630 临时携程账号
- $customer_name = str_replace(',', ',', $customer_name);*/
- //xml simplexml_load_string json_decode会把空字符串转换为空数组,而不是空字符串
- /* if (empty($this->Notice) || (is_array($this->Notice) && trim($this->Notice[0]) == '')) {
- /* $this->Notice = '';
- $this->cus_list = "{{$customer_name}," . ($this->MobilePhone) . ",}";
- } else {
- $this->Notice = str_replace(',', ',', $this->Notice);
- $this->Notice = str_replace('|', ',', $this->Notice);
- $this->Notice = trim(str_replace(';', ';', $this->Notice));
- if (strpos($this->Notice, '礼盒') >= 0) {
- $this->IF_LAST_PROD = 2;
- }
- // $this->cus_list = "{{$customer_name}," . ($this->MobilePhone) . "," . Yii::$app->params['ctrip']['base_user_id'] . "|" . time() . "|0|{$this->Notice}}";
- }
-
- $supplier = BaseSupplier::findOne(['ID' => \Yii::$app->params['ctrip']['supplier_id']]);
- if ($supplier == null) {
- return false;
- }
-
- $commissionRule = $supplier->getCommissionRule(BaseSupplierSale::SALE_HOTEL);
- if ($commissionRule != null && $commissionRule->COMMISION_TYPE == BaseSupplierSale::TYPE_CHANNEL) {
- $base_com = $commissionRule->BACK_PERCENT;
- $this->Spider_sale_type = $commissionRule->SALE_TYPE;
- } else if ($commissionRule == null) {
- $this->addError('Spider_sale_type', '没有对应渠道销售');
- return false;
- } else {
- $base_com = 0;
- $this->Spider_sale_type = $commissionRule->SALE_TYPE;
- }
- $roomPrices = isset($this->RoomPrices['RoomPrice'][0]) ? $this->RoomPrices['RoomPrice'] : $this->RoomPrices;
- if (!empty($roomPrices)) {
- $date = array_column($roomPrices, 'EffectDate');
- array_multisort($date, SORT_ASC, $roomPrices);
- if (is_array($roomPrices)) {
- foreach ($roomPrices as $RoomPrice) { //每个RoomPrice对应一天价格
- $EffectDate = substr($RoomPrice['EffectDate'], 0, 10);
- // $Price = $RoomPrice['Price']; //订单卖价金额(原币种),预付一般不传或者传0
- // $CNYPrice = $RoomPrice['CNYPrice']; //订单卖价金额(人民币),预付一般不传或者传0
- // $Cost = $RoomPrice['Cost']; //订单底价金额(原币种),现付一般不传或者传0
- $CNYCost = $RoomPrice['Cost']; //-订单底价金额(人民币),现付一般不传或者传0
- // $BreakFast = $RoomPrice['BreakFast']; //早餐数量
- $commission = $base_com * $CNYCost / 100;
- $this->new_room_list .= "{''" . $EffectDate . "''," . $this->Quantity . "," . $CNYCost . "," . $this->Quantity * $commission . "}";
- }
-
- }
- }*/
-
- $spider_order = OrderMain::findOne(['CANCEL_FLAG' => 0, 'OUTSIDE_ORDER_NO' => $this->OrderID, 'PARENT_PROD_ID' => $this->Spider_hotel]);
- if ($spider_order != null) {
- $this->Spider_order_id = $spider_order->ORDER_ID;
- }
-
- return true;
- } elseif (isset($data[$scope])) {
- $this->setAttributes($data[$scope]);
- return true;
- } else {
- return false;
- }
- }
-
- /**
- * User:Steven
- * Desc:加载可定检查的相关数据
- * @param $data
- * @param null $formName
- * @return bool
- */
- public function load_valid($data, $formName = null)
- {
- $scope = $formName === null ? $this->formName() : $formName;
- if ($scope === '' && !empty($data)) {
- $this->setAttributes($data, false);
-
- //mapping酒店 id和房型id
- $this->Spider_hotel = HotelRelation::getSpiderId($this->Hotel, $this->relation_supplier_id);
- $this->Spider_room = RoomRelation::getSpiderId($this->Room, $this->relation_supplier_id);
- $this->Spider_room_type = OperaHotelRoom::getSpiderRoomType($this->Room);
- return true;
- } elseif (isset($data[$scope])) {
- $this->setAttributes($data[$scope]);
- return true;
- } else {
- return false;
- }
- }
-
- /**
- * User:Steven
- * Desc:可定检查
- * param $roomInfo
- * @return array|\yii\db\ActiveRecord[]
- */
- public function CheckRoomAvail()
- {
- $model = new RunHotelDistrib();
- $roomInfo['hotel_id'] = $this->Spider_hotel;
- $roomInfo['room_id'] = $this->Spider_room;
- $roomInfo['room_prices'] = $this->RoomPrices;
- $roomInfo['Arrival'] = $this->Arrival;
- $roomInfo['Departure'] = $this->Departure;
- $res = $model->CheckRoomAvail($roomInfo);
- return $res;
- }
-
- /**
- * Notes:生成携程订单
- * User: Steven
- * Date: 2018/1/10
- * Time: 18:00
- * @throws Exception
- */
- public function makeOrder()
- {
- $params = [
- 'CreateUserID' => Yii::$app->params['ctrip']['base_user_id'],
- 'OrderID' => $this->OrderID, //渠道订单号
- 'HotelID' => $this->Spider_hotel, //酒店ID
- 'RoomID' => $this->Spider_room, //房型ID
- 'DistribID' => Yii::$app->params['ctrip']['supplier_id'],//渠道ID
- 'RoomNum' => $this->Quantity, //房间数量
- 'CheckIn' => $this->Arrival, //入住日期
- 'CheckOut' => $this->Departure, //离店日期,
- 'TotalPrice' => $this->CostAmount,//订单总价
- 'Currency' => $this->Currency, //币种
- 'ContactName' => '',//联系人姓名
- 'ContactTel' => '', //联系人电话
- 'PayType' => CommonOrder::PAY_TYPE_CREDIT, // 638:支付宝支付; 221:现金支付 ;275:授信支付;278:微信支付
- 'PayTradeNo' => '', //支付流水号
- 'OrderGuests' => CtripOrder::getGuestsName($this->Guests), //入住人信息
- 'Comment' => $this->Notice, //客人备注
- 'NeedInvoice' => 0,
- 'InvoiceInfos' => [ //发票信息
- ],
- 'RoomPrices' => CtripOrder::getRoomPrices($this->RoomPrices),
- 'OrderTitleID' => 0
- ];
- $new_order = new CommonOrder(['scenario' => 'BookHotelOrder']);
- if ($new_order->load($params, '') && $new_order->validate()) {
- $res = $new_order->bookHotelOrder();
- if ($res['code'] == CommonOrder::RETURN_CODE_SUCCESS) {
- $this->Spider_order_id = $res['data']['order_id'];
- return self::RETURN_CODE_SUCCESS;
- }
- $return_code = self::RETURN_CODE_PROGRAM_FAIL; //程序错误
- if ($res['code'] == CommonOrder::RETURN_CODE_UNAUTHORIZED || $res['code'] == CommonOrder::RETURN_CODE_ROOM_CLOSED ||
- $res['code'] == CommonOrder::RETURN_CODE_STOCK_FAIL) {
- $return_code = self::RETURN_CODE_STOCK_FAIL; //酒店满房/房量不足
- }
- if ($res['code'] == CommonOrder::RETURN_CODE_PRICE_FAIL) {
- return self::RETURN_CODE_PRICE_FAIL; //价格错误
- }
- if ($res['code'] == CommonOrder::RETURN_CODE_PROGRAM_FAIL) {
- return self::RETURN_CODE_PROGRAM_FAIL; //程序错误
- }
- return $return_code;
- } else {
- $error = array_values($new_order->getFirstErrors());
- //TODO:通知
- return self::RETURN_CODE_PROGRAM_FAIL;
- }
- }
-
- /**
- * User: wangxj
- *
- * 取消订单
- *
- * @return array|false
- */
- /*public function cancelOrder()
- {
- $user_id = Yii::$app->params['ctrip']['base_user_id'];
-
- //最后一个参数是cancel_flag,旧的下单逻辑,如果是修改导致的取消,cancel_flag置为4
- $sql = "CALL ht_cancel_order(" . $user_id . "," . $this->Spider_order_id . ",1)";
- $db = Yii::$app->db;
- $result = $db->createCommand($sql)->queryOne();
- //供应商余额
- $model = new BaseBalance($this->Spider_order_id, 2, '取消还款');
- $sup_data = $model->cancelBalanceMain();
- $model->addLog($sup_data);
- return $result;
- }*/
-
- /**
- * Notes:携程取消订单
- * User: Steven
- * Date: 2018/1/12
- * Time: 17:50
- * @return array|int
- * @throws Exception
- * @throws \Exception
- */
- public function applyCancel()
- {
- $order = OrderMain::findOne([
- 'cancel_flag' => 0,
- 'PARENT_PROD_ID' => $this->Spider_hotel,
- 'OUTSIDE_ORDER_NO' => $this->OrderID,
- 'DOCKING_TYPE' => OrderMain::ORDER_TYPE_CHANNEL,
- 'PARENT_ORDER_ID' => 0
- ]);
- if (empty($order)) { //订单不存在
- return self::RETURN_CODE_NOT_EXIST;
- }
- if ($order->CHANNEL_ORDER_STATUS == OrderMain::ORDER_CHANNEL_STATUS_APPLY_CANCEL_CONFIRMED) { //渠道接单状态是已取消 ,依然返回同意取消
- $this->saveLog(2, '渠道状态已经变更为已取消,二次取消,同意取消' . "[{$order->CHANNEL_ORDER_STATUS}]/[{$order->ORDER_STATUS}]");
- return self::RETURN_CODE_SUCCESS;
- }
-
- $white_list = OrderHtExceptionList::find()->where(['order_id' => $order->ORDER_ID])->one();
- if ($white_list != null) { //加入白名单的直接同意取消即可
- if ($white_list->status == OrderHtExceptionList::AGREE_CANCEL) { //同意取消
- $channel_status = OrderMain::ORDER_CHANNEL_STATUS_APPLY_CANCEL_CONFIRMED;
- } else { //拒绝取消
- $channel_status = OrderMain::ORDER_CHANNEL_STATUS_APPLY_CANCEL_REJECT;
- }
- //变更渠道的订单状态为已取消,但是不取消订单,即不回退库存,发人工退改申请
- $count = OrderMain::updateAll(['CHANNEL_ORDER_STATUS' => $channel_status, 'UPDATE_TIME' => date('Y-m-d H:i:s', time())],
- ['ORDER_PROD_TYPE' => [25, 26], 'CANCEL_FLAG' => 0, 'OUTSIDE_ORDER_NO' => $this->OrderID, 'DOCKING_TYPE' => OrderMain::ORDER_TYPE_CHANNEL]);
- if ($count == 0) {
- throw new \Exception();
- }
- if ($channel_status == OrderMain::ORDER_CHANNEL_STATUS_APPLY_CANCEL_CONFIRMED) {
- $this->saveLog(2, '该订单已加入白名单,白名单类型:同意取消,同意渠道取消。' . "[{$order->CHANNEL_ORDER_STATUS}]/[{$order->ORDER_STATUS}]");
- return self::RETURN_CODE_SUCCESS;
- } else {
- $this->saveLog(2, '该订单已加入白名单,白名单类型:拒绝取消,拒绝渠道取消。' . "[{$order->CHANNEL_ORDER_STATUS}]/[{$order->ORDER_STATUS}]");
- return self::RETURN_CODE_NOT_ALLOW_CANCEL;
- }
-
- }
- //白名单的订单不需要在这里直接取消订单,先同意渠道取消订单,然后人工发退改申请
- $rs = $this->validateCancel($order);
- if (!$rs || $order->CHANNEL_ORDER_STATUS != OrderMain::ORDER_CHANNEL_STATUS_WAITING ||
- !in_array($order->ORDER_STATUS, [OrderMain::ORDER_STATUS_NO_PAID, OrderMain::ORDER_STATUS_WAITING_SEND, OrderMain::ORDER_STATUS_WAITING_CONFIRM])) { //该情况不能退单
- $this->saveLog(2, '拒绝渠道取消。' . "[{$order->CHANNEL_ORDER_STATUS}]/[{$order->ORDER_STATUS}]");
- return self::RETURN_CODE_NOT_ALLOW_CANCEL;
- }
- $transaction = Yii::$app->db->beginTransaction();
- try {
- //日志
- $this->saveLog(2, '同意渠道取消。' . "[{$order->CHANNEL_ORDER_STATUS}]/[{$order->ORDER_STATUS}]");
-
- //我们没有发单给酒店,订单状态直接变为已取消,根据订单库存类型,
- //1、订单状态改为已取消
- //2、回退库存
- //对于已发单待安排的订单(这种情况如果直接取消的话,需要客服告知酒店需要取消,避免不必要的沟通麻烦),不需要直接取消订单,由客服操作取消,并且这个状态下酒店是不能确认订单的
- if ($order->ORDER_STATUS == OrderMain::ORDER_STATUS_WAITING_SEND) { //待发单 (携程无待支付状态,所以不需要要判断) 需要直接取消订单
- $order->ORDER_STATUS = OrderMain::ORDER_STATUS_CANCEL;
- $order->ORDER_VALID_STATUS = 0;
- $cancel = $order->rollbackStocks();
- if (!$cancel) {
- throw new \Exception();
- }
- //供应商余额
- /* $model = new BaseBalance($order->ORDER_ID, 2, '取消还款');
- $sup_data = $model->cancelBalanceMain();
- $model->addLog($sup_data);*/
- }
- $count = OrderMain::updateAll(['CHANNEL_ORDER_STATUS' => OrderMain::ORDER_CHANNEL_STATUS_APPLY_CANCEL_CONFIRMED, 'ORDER_VALID_STATUS' => $order->ORDER_VALID_STATUS,
- 'ORDER_STATUS' => $order->ORDER_STATUS, 'UPDATE_TIME' => date('Y-m-d H:i:s', time())],
- ['ORDER_PROD_TYPE' => [25, 26], 'CANCEL_FLAG' => 0, 'OUTSIDE_ORDER_NO' => $this->OrderID, 'DOCKING_TYPE' => OrderMain::ORDER_TYPE_CHANNEL]);
- if ($count == 0) {
- throw new \Exception();
- }
- $orderComment = new OrderComment();
- $orderComment->ORDER_ID = $order->ORDER_ID;
- $orderComment->COMMENT_TYPE = OrderComment::TYPE_CANCEL_NOTE;
- $orderComment->COMMENT_TXT = $this->Notice;
- $orderComment->CREATE_USER_ID = Yii::$app->params['ctrip']['base_user_id'];
- $orderComment->UPDATE_USER_ID = Yii::$app->params['ctrip']['base_user_id'];
- $orderComment->save();
- $transaction->commit();
- return self::RETURN_CODE_SUCCESS;
- } catch (Exception $exception) {
- $transaction->rollBack();
- return self::RETURN_CODE_CANCEL_FAIL;
- }
- //
-
-
- /*if ($order != null && $rs || ($this->white_list_flag != null && $this->white_list_flag == 1)) {
- // $timeMinutes = (time() - strtotime($order->CREATE_TIME)) / 60;
- if ($order->CHANNEL_ORDER_STATUS == OrderMain::ORDER_CHANNEL_STATUS_APPLY_CANCEL_CONFIRMED) { //渠道接单状态是已取消 ,依然返回同意取消
- $this->saveLog(2, '渠道状态已经变更为已取消,二次取消,同意取消');
- return ['code' => 1, 'data' => $order];
- }
- if (
- (
- //15分钟内,渠道状态未接单 直接取消
- ($order->CHANNEL_ORDER_STATUS != OrderMain::ORDER_CHANNEL_STATUS_WAITING) //非待接单状态 不能取消
- && //待支付、待确认、
- (in_array($order->ORDER_STATUS, [OrderMain::ORDER_STATUS_CONFIRMED, OrderMain::ORDER_STATUS_FINISH, OrderMain::ORDER_STATUS_CANCEL]) //已确认、已完成、已取消
- || $order->CHANNEL_ORDER_STATUS == OrderMain::ORDER_CHANNEL_STATUS_APPLY_CANCEL_REJECT //取消被拒 已经拒绝的也不允许取消
- || ($this->white_list_flag != null && $this->white_list_flag == 2)
- || ($order->CHANNEL_ORDER_STATUS != OrderMain::ORDER_CHANNEL_STATUS_WAITING) //如果渠道接单状态为待处理,可以直接取消
- )) && //白名单
- ($this->white_list_flag == null || ($this->white_list_flag != null && $this->white_list_flag == 2)) //白名单为拒绝取消也不能取消 2:拒绝取消
- ) {
- return ['code' => 2, 'msg' => '不符合取消规则,拒绝取消'];
- } else {
-
- }
- } else {
- $this->saveLog(2, '拒绝渠道取消' . ',validateCancel:' . !empty($rs) . 'order对象:' . !empty($order));
- return ['code' => 2, 'msg' => '不符合取消规则,拒绝取消'];
- }*/
- }
-
- /**
- * User:Steven
- * Desc:获取订单状态
- */
- public function getOrderStatus()
- {
- $query = OrderMain::find()->select(['CHANNEL_ORDER_STATUS', 'ORDER_STATUS', 'ORDER_ID'])->where(['OUTSIDE_ORDER_NO' => $this->OrderID, 'PARENT_ORDER_ID' => 0])->asArray()->one();
- return $query;
- }
-
- /**
- * User: wangxj
- *
- * 验证币种是否正确
- *
- * @param $attribute string
- * @param $params array
- *
- */
- public function validateCurrency($attribute, $params)
- {
- if ($this->$attribute != null && $this->$attribute != Yii::$app->params['ctrip']['currency_type']) {
- $this->addError('Currency', self::RETURN_CODE_CURRENCY_FAIL);
- }
- }
-
- /**
- * User: wangxj
- *
- * 验证是否可以取消
- * 根据时间判断是否可以取消
- * 未接单的订单全部返回取消成功;已接单15分钟内,返回取消成功;已接单超过15分钟,且在最晚取消时间内,返回成功;
- * 已接单超过15分钟,超过最晚取消时间,返回失败
- *
- * @param OrderMain $order
- *
- * @return bool
- *
- */
- public function validateCancel($order)
- {
- //这里现在不需要进行酒店和房型的验证,由于携程修改订单(取消+新订)的时候,取消的时候传入的修改后订单的room_id等信息
- //Ctrip-姜欢(1902623359) 2017/12/25 10:40:00 :这个取消传的是修改后订单的roomid,取消单不用判roomid的
- //订单的酒店是否匹配,房型是否匹配
- /*if ($this->Spider_hotel != $order->PARENT_PROD_ID || $this->Spider_room_type != $order->PROD_ID) {
- return false;
- }*/
-
- //根据时间判断是否可以取消
- //$timeMin = (time() - strtotime($order->CREATE_TIME)) / 60;
- //禅道需求:http://192.168.6.248/zentao/task-view-567.html
- //1.对于携程15分钟之内已接单的订单,携程接口申请取消时,根据订单状态接口返回是否同意取消;
- //2.对于待发单状态的订单,接口返回同意取消,渠道接单状态变更为“已取消”,订单状态变更为已取消,记录3条订单日志;
- //3.对于已取消状态的订单,接口返回同意取消,渠道接单状态变更为“已取消”,记录2条订单日志;
- //4.对于待安排、已安排、已入住状态的订单,接口返回拒绝取消,渠道接单状态变更为“取消被拒”,记录3条订单日志;
- //5.对于退改中、待退单确认状态的订单,不会出现这样的情况。
- //根据这里的描述,好像没有15分钟的强制条件了
- // if ($timeMin <= 15 || $order->CHANNEL_ORDER_STATUS == OrderMain::ORDER_CHANNEL_STATUS_WAITING) {
- if ($order->CHANNEL_ORDER_STATUS == OrderMain::ORDER_CHANNEL_STATUS_WAITING) {
- return true;
- } else {
- $room = OperaHotelRoom::findOne(['HOTEL_ID' => $order->PARENT_PROD_ID, 'ROOM_TYPE' => $order->PROD_ID]);
- if ($room == null) {
- return false;
- }
- //在最晚取消时间内
- if (time() < $room->getLatestCancel($order->PROD_START_STATION_DATE)) {
- return true;
- }
- //超过最晚取消时间
- return false;
- }
-
- }
-
- /**
- * User: wangxj
- *
- * 验证金额是否一致,如果传过来的金额和我们设置的金额不一致,下单失败
- * 103 价格错误
- * @param $attribute string
- * @param $params array
- *
- */
- public function validateCNYCostAmount($attribute, $params)
- {
- $hotel = $this->Spider_hotel;
- $room_type = $this->Spider_room_type;
- $sale_type = $this->Spider_sale_type;
- $ctripInfo = Yii::$app->params['ctrip'];
- $roomPrices = isset($this->RoomPrices['RoomPrice'][0]) ? $this->RoomPrices['RoomPrice'] : $this->RoomPrices;
- foreach ($roomPrices as $roomPrice) {
- $EffectDate = substr($roomPrice['EffectDate'], 0, 10);
- $CNYCost = $roomPrice['Cost']; //-订单底价金额(人民币),现付一般不传或者传0
- $filed = BaseSupplierSale::getSaleField($sale_type);
- $room = RunHotelDistrib::findOne(['HOTEL_ID' => $hotel, 'ROOM_TYPE' => $room_type, 'RUN_DATE' => $EffectDate, 'DISTRIB_ID' => $ctripInfo['supplier_id']]);
- if ($room != null) {
- $cost = $room->$filed;
- if ((int)($cost * 100) != (int)($CNYCost * 100)) {
- $this->clearErrors($attribute);
- $this->addError($attribute, self::RETURN_CODE_PRICE_FAIL);
- }
- } else {
- $this->clearErrors($attribute);
- $this->addError($attribute, self::RETURN_CODE_ROOM_CLOSED);
- }
- }
- }
-
- /**
- * User: wangxj
- *
- * 检查保留房是否有房
- *
- * @param $attribute
- * @param $params
- */
- public function validateHoldRoom($attribute, $params)
- {
- if ($this->$attribute == 'R') {
- $roomPrices = isset($this->RoomPrices['RoomPrice'][0]) ? $this->RoomPrices['RoomPrice'] : $this->RoomPrices;
- foreach ($roomPrices as $roomPrice) {
- $EffectDate = substr($roomPrice['EffectDate'], 0, 10);
- $room_stock = RunHotelDistrib::find()->where(['RUN_DATE' => $EffectDate,
- 'ROOM_TYPE' => $this->Spider_room_type, 'RUN_STATUS' => RunHotelDistrib::ROOM_STATUS_SALE,
- 'HOTEL_ID' => $this->Spider_hotel])->andFilterWhere(['>=', 'REMAINING_COUNT', $this->Quantity])->one();;
- if ($room_stock == null) {
- $this->addError($attribute, self::RETURN_CODE_STOCK_FAIL);
- }
- }
- }
- }
-
- /**
- * User: wangxj
- *
- * 检查mapping的酒店和房型是否正常
- *
- * @param $attribute
- * @param $params
- */
- public function validateSpider($attribute, $params)
- {
- if ($this->Spider_hotel == 0 || $this->Spider_room == 0) {
- $this->addError($attribute, self::RETURN_CODE_MAPPING_FAIL);
- }
- }
-
- /**
- * User: wangxj
- *
- * 根据订单当前状态,写入不同日志,条数和内容可能都不相同
- *
- * @param $user_id integer
- * @param $msg string
- *
- */
- public function saveLog($user_id, $msg)
- {
- $orderLog = new OperaHotelLog();
- $orderLog->CREATE_USER_ID = $user_id;
- $orderLog->HOTEL_ID = $this->Spider_hotel;
- $orderLog->LOG_TYPE = OperaHotelLog::LOG_TYPE_ORDER;
- $orderLog->ROOM_TYPE = $this->Spider_room_type;
- $orderLog->PARENT_ROOM_TYPE = $this->Spider_parent_room_type;
- $orderLog->LOG_DESC = $msg;
- $orderLog->ORDER_ID = $this->Spider_order_id;
- $orderLog->save();
- }
-
- public static function getGuestsName($array)
- {
- $customer_name = '';
- $guests = isset($array['GuestEntity'][0]) ? $array['GuestEntity'] : $array;
- if (is_array($guests)) {
- $customer_name = [];
- foreach ($guests as $GuestEntity) {
- if (!empty($GuestEntity['ChinesName'])) {
- $customer_name[]['Name'] = $GuestEntity['ChinesName'];
- } else {
- $customer_name[]['Name'] = $GuestEntity['LastName'] . '/' . $GuestEntity['FirstName'];
- }
- /*$FirstName = empty($GuestEntity['FirstName']) ? '' : $GuestEntity['FirstName'];
- $LastName = empty($GuestEntity['LastName']) ? '' : $GuestEntity['LastName'];
- if ($customer_name == '' || (strstr($GuestEntity['ChinesName'], trim($customer_name)) == false && strstr(($FirstName . ' ' . $LastName), trim($customer_name)) == false)) {
- $customer_name .= ',' . (empty($GuestEntity['ChinesName']) ? ($FirstName . ' ' . $LastName) : $GuestEntity['ChinesName'] . ' ');
- }*/
- }
- }
- return $customer_name;
- }
-
- /**
- * Notes:
- * User: Steven
- * Date: 2018/1/11
- * Time: 11:48
- * @param $RoomPrices
- * @return array
- */
- public static function getRoomPrices($RoomPrices)
- {
- $roomPrices = isset($RoomPrices['RoomPrice'][0]) ? $RoomPrices['RoomPrice'] : $RoomPrices;
- $date = array_column($roomPrices, 'EffectDate');
- array_multisort($date, SORT_ASC, $roomPrices);
- if (!empty($roomPrices)) {
- $RoomPricesArr = [];
- foreach ($roomPrices as $RoomPrice) { //每个RoomPrice对应一天价格
- $EffectDate = substr($RoomPrice['EffectDate'], 0, 10);
- $RoomPricesArr[] = [
- 'RunDate' => $EffectDate,
- 'Price' => $RoomPrice['Cost']
- ];
- }
- }
- return $RoomPricesArr;
- }
- }
|