You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

219 lines
9.5 KiB

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: wangxj
  5. * Date: 2017/4/12
  6. * Time: 13:54
  7. * $OrderID, $InterFaceSendID, $Hotel, $HotelName, $Arrival, $Departure,
  8. * $EarlyArrivalTime, $LastArrivalTime, $Person, $Notice, $IsHoldRoom,
  9. * $IsFlashLive, $Guests, $mobilePhone, $Currency, $Amount, $CNYAmount, $GuaranteeType, $BalanceType,
  10. * $Room, $RoomPerson, $RoomName, $Quantity, $CostAmount, $CNYCostAmount,
  11. * $RoomPrices, $AddOptionals, $InterFaceConfirmNo, $HotelConfirmNo, $InterFaceAmount, $ReturnCode, $ReturnDescript,
  12. * $OrderStatus, $FGToPP, $DepositAmount, $IsFreeSale
  13. */
  14. namespace backend\modules\hotel\models;
  15. use backend\common\Utils;
  16. use Yii;
  17. use common\models\BaseSupplier;
  18. use common\models\BaseSupplierSale;
  19. use yii\base\Model;
  20. class CtripRoomInfo extends Model
  21. {
  22. public $relation_supplier_id;
  23. public $Spider_hotel; //蜘蛛的酒店id,opera_hotel HOTEL_ID
  24. public $Spider_room; //蜘蛛的房型id,opera_hotel_room ID
  25. public $Spider_room_type; //蜘蛛的房型id,opera_hotel_room ROOM_TYPE
  26. public $RoomID; //Ctrip房型ID
  27. public $StartDate; //开始时间
  28. public $EndDate; //结束时间
  29. public $Editer; //提交人:集团名拼音
  30. public $RoomInfoItems = []; //保留房用最晚预订时间
  31. public $defaultItem; //一个房态的默认值
  32. public $ChangeDefault; //Boolean 是否同时修改房态默认值(F-否,T-是)
  33. public $HoldDeadline; //【现付产品使用】9999 无规定
  34. public $UserLimited; //【现付产品使用】2-超时担保,3-一律担保,9999-无限制
  35. public $Note; // 备注
  36. public $ReserveTime; //小时数,例如 1 为一小时 。计算公式:订单入住日24点向 后推算
  37. public $Roomstatus; //G良好 N满房
  38. public $RoomsInv; // 临时保留房总数
  39. public $Guarantee; //担保 默认:空字符
  40. public $GuaranteeLCT; //担保最晚取消时间 【现付产品使用】小时数,不可 取消设置为23988
  41. public $PrepayLCT; //预付最晚取消时间【预付产品使用】小时数,不可 取消设置为23988
  42. // public $AllNeedGuarantee; //需要担保的类型 【现付产品使用】全额/峰时担保(F-否,T-全额担保,B-峰时担 保)
  43. public $LateReserveTime; //订单最晚预定时间
  44. public $DeductType; //预付房型扣款类型 【预付产品使用】(C-全部扣款,F-首日扣款)
  45. public $BlanceType; //付款类型 推送对应的现付,预付,和PKG的CODE
  46. public function __construct(array $config = [])
  47. {
  48. $this->relation_supplier_id = Yii::$app->params['ctrip']['relation_supplier_id'];
  49. $this->defaultItem['Stays'] = '1';
  50. $this->defaultItem['ChangeDefault'] = 'F';
  51. $this->defaultItem['Recommend'] = '2';
  52. $this->defaultItem['Recommend_Intl'] = '2';
  53. $this->defaultItem['HoldDeadline'] = '9999'; //现付产品使用
  54. $this->defaultItem['UserLimited'] = '9999';
  55. $this->defaultItem['Note'] = '';
  56. $this->defaultItem['Restorable'] = 'T';
  57. $this->defaultItem['Guarantee'] = '';
  58. $this->defaultItem['CheckType'] = 'S';
  59. $this->defaultItem['GuaranteeLCT'] = '23988';
  60. $this->defaultItem['AllNeedGuarantee'] = 'F';
  61. // $this->defaultItem['RoomGiftID'] = '';
  62. $this->defaultItem['DeductType'] = 'C';
  63. //Roomstatus N满房 G良好
  64. //RoomsInv 房间数量
  65. //Note 备注
  66. //ReserveTime 保留房最晚预订时间
  67. //PrepayLCT 预付订单 最晚取消时间
  68. //LateReserveTime 订单最晚预订时间
  69. parent::__construct($config);
  70. }
  71. public function rules()
  72. {
  73. return [
  74. [[
  75. 'RoomID', 'StartDate', 'EndDate', 'defaultItem', 'RoomInfoItems',
  76. ], 'required'],
  77. [['RoomInfoItems'], 'validateItems'],
  78. [['RoomID'], 'validatePrimary', 'params' => ['message' => '获取mapping失败']],
  79. ];
  80. }
  81. /**
  82. * @inheritdoc
  83. */
  84. public function attributeLabels()
  85. {
  86. return [
  87. 'RoomID' => '渠道房型ID',
  88. 'RoomInfoItems' => '房态数组',
  89. 'StartDate' => '起始时间',
  90. 'EndDate' => '结束时间',
  91. 'defaultItem' => '房态默认值',
  92. ];
  93. }
  94. /**
  95. * User: wangxj
  96. *
  97. * 加载数据
  98. *
  99. * @param array $post
  100. * @param null $formName
  101. * @return bool
  102. */
  103. public function load($data, $formName = null)
  104. {
  105. $scope = $formName === null ? $this->formName() : $formName;
  106. if ($scope === '' && !empty($data)) {
  107. $this->setAttributes($data, false);
  108. $this->RoomID = RoomRelation::getChannelId($this->Spider_room, Yii::$app->params['ctrip']['relation_supplier_id']);
  109. $startDate = $data['StartDate'];
  110. $endDate = $data['EndDate'];
  111. //mapping酒店 id和房型id
  112. $room = OperaHotelRoom::findOne($data['Spider_room']);
  113. if (empty($room)) {
  114. return false;
  115. }
  116. /*$hotel = OperaHotel::findOne(['HOTEL_ID' => $room['HOTEL_ID']]);
  117. if (empty($hotel)) {
  118. return false;
  119. }*/
  120. $stocks = RunHotelDistrib::find()
  121. ->select(['DISTINCT(RUN_DATE)', 'a.CREATE_USER_ID', 'a.CREATE_TIME', 'a.UPDATE_USER_ID', 'a.UPDATE_TIME', 'a.DISTRIB_ID', 'a.HOTEL_ID', 'a.BASE_ROOM_TYPE', 'a.ROOM_TYPE',
  122. 'a.REMAINING_COUNT', 'a.SALED_COUNT', 'a.PROD_PRICE', 'a.CUS_PRICE', 'a.RUN_STATUS', 'a.OVERSELL_FLAG', 'a.CONSUME_STOCK_TYPE', 'b.AUTHORITY_STATUS', 'b.LATEST_COMFIRM_TIME'])
  123. ->leftJoin('opera_room_distrib b', 'b.DISTRIB_ID = a.DISTRIB_ID AND b.ROOM_ID=' . $room['ID'])
  124. ->where(['a.DISTRIB_ID' => Yii::$app->params['ctrip']['supplier_id'], 'a.HOTEL_ID' => $room['HOTEL_ID'], 'a.BASE_ROOM_TYPE' => $room['PARENT_ROOM_TYPE'], 'a.ROOM_TYPE' => $room['ROOM_TYPE']])
  125. ->andFilterWhere(['between', 'a.RUN_DATE', $startDate, $endDate])
  126. ->from('run_hotel_distrib a')
  127. ->orderBy('a.RUN_DATE')
  128. // -> createCommand() -> getRawSql();
  129. ->asArray()->all();
  130. foreach ($stocks as $stock) {
  131. $subRoom = RunHotelSubRoom::findOne(['HOTEL_ID' => $room['HOTEL_ID'], 'BASE_ROOM_TYPE' => $room['PARENT_ROOM_TYPE'],
  132. 'ROOM_TYPE' => $room['ROOM_TYPE'], 'RUN_DATE' => $stock['RUN_DATE']]);
  133. $item = $this->defaultItem;
  134. $item['Roomstatus'] = (
  135. $room->IS_ONSALE == OperaHotelRoom::IS_ONSALE &&
  136. $stock['RUN_STATUS'] == RunHotelDistrib::ROOM_STATUS_SALE &&
  137. $subRoom->RUN_STATUS == RunHotelSubRoom::RUN_STATUS_OPEN &&
  138. $stock['AUTHORITY_STATUS'] == RunHotelDistrib::AUTHORITY_STATUS_ON
  139. ) ? 'G' : 'N';
  140. //如果超卖关了,设置为只销售保留房
  141. if ($item['Roomstatus'] == 'G' && $stock['OVERSELL_FLAG'] == RunHotelDistrib::OVERSELL_OFF) {
  142. if ($stock['REMAINING_COUNT'] <= 0) {
  143. $item['Roomstatus'] = 'N';
  144. } else
  145. $item['Roomstatus'] = 'L';
  146. }
  147. $item['RoomsInv'] = ($room->IS_ONSALE == OperaHotelRoom::IS_OFFSALE || $stock['RUN_STATUS'] == RunHotelDistrib::ROOM_STATUS_CLOSE) ? 0 : $stock['REMAINING_COUNT'];
  148. //保留房最晚预订时间
  149. $item['ReserveTime'] = Utils::timeHour($stock['LATEST_COMFIRM_TIME']);
  150. $item['PrepayLCT'] = Utils::timeHour($room->LASTEST_CANCEL_DAY, 'rule');
  151. $item['LateReserveTime'] = Utils::timeHour($room->LASTEST_BOOK_TIME);
  152. $this->RoomInfoItems[] = ['RoomInfoItem' => $item];
  153. }
  154. return true;
  155. } elseif (isset($data[$scope])) {
  156. $this->setAttributes($data[$scope]);
  157. return true;
  158. } else {
  159. return false;
  160. }
  161. }
  162. /**
  163. * User: wangxj
  164. *
  165. * 检查mapping的酒店房型是否正常
  166. *
  167. * @param $attribute
  168. * @param $params
  169. */
  170. public function validatePrimary($attribute, $params)
  171. {
  172. if ($this->Spider_room == 0 || $this->RoomID == 0) {
  173. $this->addError($attribute, $params['message']);
  174. }
  175. }
  176. /**
  177. * User: wangxj
  178. *
  179. * 检查房态数据是否正常
  180. *
  181. * @param $attribute
  182. // * @param $params
  183. */
  184. public function validateItems($attribute)
  185. {
  186. if (count($this->$attribute) <= 0) {
  187. $this->addError($attribute, '无法获取日期');
  188. } else {
  189. //判断一下时间是否对应,startDate to endDate中的日期,是否都一一对应
  190. $start = strtotime($this->StartDate);
  191. $end = strtotime($this->EndDate);
  192. $day_count = ($end - $start) / (3600 * 24);
  193. if ($day_count >= 120) {
  194. $this->addError($attribute, '一次最多推送120天数据');
  195. } elseif ($day_count + 1 != count($this->$attribute)) {
  196. $this->addError($attribute, '房态数据没有一一对应');
  197. } else {
  198. foreach ($this->$attribute as $key => $item) {
  199. }
  200. }
  201. }
  202. }
  203. }