|
- <?php
-
- namespace backend\modules\hotel\models;
-
- use common\components\zHttp;
- use Yii;
- use backend\common\Utils;
- use common\models\BaseSupplierSale;
- use yii\db\ActiveRecord;
- use common\models\BaseSupplier as HotelSupplier;
- use yii\db\Expression;
- use yii\db\Exception;
-
- /**
- * This is the model class for table "run_hotel_distrib".
- * 渠道固定数量
- *
- * @property integer $ID
- * @property integer $CREATE_USER_ID
- * @property string $CREATE_TIME
- * @property integer $UPDATE_USER_ID
- * @property string $UPDATE_TIME
- * @property integer $DISTRIB_ID
- * @property integer $HOTEL_ID
- * @property integer $BASE_ROOM_TYPE
- * @property integer $ROOM_TYPE
- * @property string $RUN_DATE
- * @property integer $REMAINING_COUNT
- * @property integer $SALED_COUNT
- * @property string $PROD_PRICE
- * @property string $CUS_PRICE
- * @property integer $RUN_STATUS
- * @property integer $OVERSELL_FLAG
- * @property string $CONSUME_STOCK_TYPE
- * @property integer $AUTHORITY_STATUS
- */
- class RunHotelDistrib extends ActiveRecord
- {
- const ROOM_STATUS_CLOSE = 329; //关房
- const ROOM_STATUS_SALE = 326; //固定数量
- const OVERSELL_ON = 1; //超卖开
- const OVERSELL_OFF = 0; //
- const AUTHORITY_STATUS_ON = 1;
- const AUTHORITY_STATUS_OFF = 0;
-
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return 'run_hotel_distrib';
- }
-
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['CREATE_USER_ID', 'UPDATE_USER_ID', 'DISTRIB_ID', 'HOTEL_ID', 'BASE_ROOM_TYPE', 'ROOM_TYPE', 'REMAINING_COUNT', 'SALED_COUNT', 'RUN_STATUS', 'OVERSELL_FLAG'], 'integer'],
- [['CREATE_TIME'], 'required'],
- [['UPDATE_TIME'], 'safe'],
- [['PROD_PRICE', 'CUS_PRICE'], 'number'],
- [['CREATE_TIME'], 'string', 'max' => 20],
- [['RUN_DATE'], 'string', 'max' => 10],
- [['CONSUME_STOCK_TYPE'], 'string', 'max' => 100],
- ];
- }
-
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'ID' => 'ID',
- 'CREATE_USER_ID' => 'Create User ID',
- 'CREATE_TIME' => 'Create Time',
- 'UPDATE_USER_ID' => 'Update User ID',
- 'UPDATE_TIME' => 'Update Time',
- 'DISTRIB_ID' => 'Distrib ID',
- 'HOTEL_ID' => 'Hotel ID',
- 'BASE_ROOM_TYPE' => 'Base Room Type',
- 'ROOM_TYPE' => 'Room Type',
- 'RUN_DATE' => 'Run Date',
- 'REMAINING_COUNT' => 'Remaining Count',
- 'SALED_COUNT' => 'Saled Count',
- 'PROD_PRICE' => 'Prod Price',
- 'CUS_PRICE' => 'Cus Price',
- 'RUN_STATUS' => 'Run Status',
- 'OVERSELL_FLAG' => 'Oversell Flag',
- 'CONSUME_STOCK_TYPE' => 'Consume Stock Type',
- ];
- }
-
- public function getBaseSupplier()
- {
- return $this->hasOne(HotelSupplier::className(), ['ID' => 'DISTRIB_ID']);
- }
-
- public function getBaseSupplierSale()
- {
- return $this->hasMany(BaseSupplierSale::className(), ['SUPPLIER_ID' => 'DISTRIB_ID']);
- }
-
- /**
- * @return \yii\db\ActiveQuery 酒店
- */
- public function getOperaHotel()
- {
- return $this->hasOne(OperaHotel::className(), ['HOTEL_ID' => 'HOTEL_ID']);
- }
-
-
- public function getOperaHotelRoom()
- {
- return $this->hasOne(OperaHotelRoom::className(), ['HOTEL_ID' => 'HOTEL_ID', 'PARENT_ROOM_TYPE' => 'BASE_ROOM_TYPE', 'ROOM_TYPE' => 'ROOM_TYPE']);
- }
-
- public function getRunHotelSubRoom()
- {
- return $this->hasOne(RunHotelSubRoom::className(), ['HOTEL_ID' => 'HOTEL_ID', 'BASE_ROOM_TYPE' => 'BASE_ROOM_TYPE', 'ROOM_TYPE' => 'ROOM_TYPE', 'RUN_DATE' => 'RUN_DATE']);
- }
-
- public function getRoomStock()
- {
- return $this->hasMany(RunHotel::className(), ['HOTEL_ID' => 'HOTEL_ID', 'BASE_ROOM_TYPE' => 'BASE_ROOM_TYPE', 'RUN_DATE' => 'RUN_DATE']);
- }
-
- public function getRunHotel()
- {
- return $this->hasOne(RunHotel::className(), ['HOTEL_ID' => 'HOTEL_ID', 'BASE_ROOM_TYPE' => 'BASE_ROOM_TYPE', 'RUN_DATE' => 'RUN_DATE']);
- }
-
- /**
- * Author:Steven
- * Desc:获取可用的酒店、房型产品数据
- * @param $room_id
- * @param $begin_date
- * @param $end_date
- * @return array|ActiveRecord[]
- */
- public static function getAvailProduct($room_id, $begin_date, $end_date)
- {
- $model = RunHotelDistrib::find()
- ->select([
- 'a.RUN_DATE',
- 'a.REMAINING_COUNT',
- 'a.PROD_PRICE',
- 'a.RUN_STATUS',
- 'a.OVERSELL_FLAG',
- 'a.CUS_PRICE',
- 'b.RUN_STATUS AS ROOM_RUN_STATUS',
- 'b.IS_ONSALE AS ROOM_IS_ONSALE',
- 'c.ID AS RP_CODE',
- 'c.HOTEL_ID',
- 'c.PARENT_ROOM_TYPE',
- 'c.IS_ONSALE',
- 'c.ROOM_NAME',
- 'c.INNER_IDENTIFY',
- 'd.HOTEL_NAME',
- 'e.AUTHORITY_STATUS',
- ])
- ->leftJoin('run_hotel_sub_room b', 'a.HOTEL_ID=b.HOTEL_ID and a.BASE_ROOM_TYPE=b.BASE_ROOM_TYPE and a.ROOM_TYPE=b.ROOM_TYPE AND a.RUN_DATE=b.RUN_DATE')
- ->leftJoin('opera_hotel_room c', 'a.HOTEL_ID = c.HOTEL_ID AND a.BASE_ROOM_TYPE = c.PARENT_ROOM_TYPE AND a.ROOM_TYPE = c.ROOM_TYPE AND c.CANCEL_FLAG=0')
- ->leftJoin('opera_hotel d', 'a.HOTEL_ID=d.HOTEL_ID')
- ->leftJoin('opera_room_distrib e', 'a.DISTRIB_ID=e.DISTRIB_ID and e.ROOM_ID=c.ID')
- ->from('run_hotel_distrib a')
- ->where([
- 'c.ID' => $room_id,
- 'a.DISTRIB_ID' => Yii::$app->params['ali']['relation_supplier_id']
- ]);
- $model->andFilterWhere(['between', 'a.RUN_DATE', $begin_date, $end_date]);
- $data = $model->andFilterWhere(['between', 'b.RUN_DATE', $begin_date, $end_date])
- // -> createCommand() -> getRawSql();
- ->asArray()->all();
- return $data;
- }
-
- /**
- * User:Steven
- * Desc:可定检查
- * @param $roomInfo
- * @return array|\yii\db\ActiveRecord[]
- */
- public function CheckRoomAvail($roomInfo)
- {
- /*$hotel = $roomInfo['Hotel']; //酒店编号
- $Arrival = Utils::formatDataTime($roomInfo['Arrival'], 1); //到店日期
- $Departure = Utils::formatDataTime($roomInfo['Departure'], 1); //离店日期
- $Room = $roomInfo['Room']; //房型编号
- $CurrencyCode = $roomInfo['Currency']; //币种(可空)
- $RoomNumber = $roomInfo['RoomNumber']; //预定间数
- $Person = $roomInfo['Person']; //入住人数
- $BalanceType = $roomInfo['BalanceType']; //价格类型
- $IsHoldRoom = $roomInfo['IsHoldRoom']; //-是否保留房(F或空-否,R-是)*/
-
- $date_list = array();
- $roomData = isset($roomInfo['room_prices']['RoomPrice']['EffectDate']) ? $roomInfo['room_prices'] : $roomInfo['room_prices']['RoomPrice'];
- if (empty($roomInfo['room_prices'])) { //可订检查补偿
- $date_list = Utils::createDateRangeArray($roomInfo['Arrival'], $roomInfo['Departure']);
- } else {
- foreach ($roomData as $item) {
- $EffectDate = Utils::formatDataTime($item['EffectDate'], 1); //入住日期
- $EffectDate = date('Y-m-d', strtotime($EffectDate));
- // $date_list .= $date_list == '' ? '\'' . $EffectDate . '\'' : "," . '\'' . $EffectDate . '\'';
- $date_list[] = $EffectDate;
- /*
- $Cost = $item['Cost']; //底价
- $CNYCost = $item['CNYCost']; //汇率后的底价(可空)
- $BreakFast = $item['BreakFast']; //含早餐数*/
- }
- }
- $query = RunHotelDistrib::find()
- ->joinWith('operaHotel b')
- ->joinWith('operaHotelRoom c')
- ->joinWith('runHotel d')
- ->leftJoin('opera_room_distrib e', 'e.DISTRIB_ID=a.DISTRIB_ID and e.ROOM_ID=c.ID')
- ->from('run_hotel_distrib a');
- $query->select([
- 'a.HOTEL_ID',
- 'a.BASE_ROOM_TYPE',
- 'a.ROOM_TYPE',
- 'a.SALED_COUNT',
- 'a.PROD_PRICE',
- 'a.CUS_PRICE',
- 'c.BREAKFAST_INCLUDE',
- 'c.IS_ONSALE',
- 'b.CANCEL_FLAG as CANCEL_FLAG_B',
- 'c.CANCEL_FLAG as CANCEL_FLAG_C',
- 'a.RUN_STATUS',
- 'a.OVERSELL_FLAG',
- 'a.REMAINING_COUNT',
- 'a.RUN_DATE',
- 'e.AUTHORITY_STATUS',
- 'SUM(case d.stock_type when 228 then d.remaining_count else 0 end) as buyout',
- 'SUM(case d.stock_type when 229 then d.remaining_count else 0 end) as inquiry',
- 'sum(case d.stock_type when 230 then d.remaining_count else 0 end) as retain'
- ]);
- //228 买断 229 现询 230 保留
- $query->where([
- 'a.DISTRIB_ID' => Yii::$app->params['ctrip']['supplier_id'],
- 'a.HOTEL_ID' => $roomInfo['hotel_id'],
- 'c.ID' => $roomInfo['room_id'],
- 'd.IS_ONSALE' => 1
- ]);
- $query->andFilterWhere([
- 'in', 'a.RUN_DATE', $date_list
- ]);
- $query->groupBy('a.run_date');
- $res = $query->asArray()->all();
- // $sql = $query->createCommand()->getRawSql();
- return $res;
- /* $sql = "select a.hotel_id,a.base_room_type,a.room_type,a.saled_count,a.prod_price,a.cus_price,c.breakfast_include
- from run_hotel_distrib a
- left join opera_hotel b on a.hotel_id=b.hotel_id
- left join opera_hotel_room c on a.hotel_id=c.hotel_id and a.base_room_type=c.parent_room_type and a.room_type=c.room_type
- where b.cancel_flag=0 and c.is_onsale=1 and a.distrib_id=669 and a.hotel_id=107 and a.base_room_type=100078 and a.room_type=3 $sql_str
- and a.run_date='2017-04-10' and a.authority_status=1 and a.run_status<>329 limit 1";*/
- }
-
- /**
- * User:Steven
- * Desc:获取渠携程渠道房价
- * @param $param array
- * param StartDate
- * param EndDate
- * param hotel_id
- * param parent_room_type
- * param room_type
- * @return array|bool|\yii\db\ActiveRecord[]
- */
- public function getRoomPrice($param)
- {
-
- $begin_date = isset($param['StartDate']) ? $param['StartDate'] : false;
- $end_date = isset($param['EndDate']) ? $param['EndDate'] : false;
- $hotel_id = isset($param['hotel_id']) ? $param['hotel_id'] : false;
- $base_room_id = isset($param['parent_room_type']) ? $param['parent_room_type'] : false;
- $room_id = isset($param['room_type']) ? $param['room_type'] : false;
- //判断当前酒店、房型是否已经mapping
- if (!$begin_date || !$end_date || !$hotel_id || !$base_room_id || !$room_id) {
- $rs['code'] = 1;
- $rs['info'] = "获取渠携程渠道房价错误,缺少必要参数|$begin_date|$end_date|$hotel_id|$base_room_id|$room_id";
- return $rs;
- }
- $hotel = HotelRelation::findOne(['HotelId' => $param['hotel_id'], 'ChannelId' => Yii::$app->params['ctrip']['relation_supplier_id']]);
- $operate_hotel_room = OperaHotelRoom::findOne(['HOTEL_ID' => $param['hotel_id'], 'PARENT_ROOM_TYPE' => $param['parent_room_type'], 'ROOM_TYPE' => $param['room_type'], 'CANCEL_FLAG' => 0]);
- $room = RoomRelation::findOne(['RoomId' => $operate_hotel_room->ID, 'ChannelId' => Yii::$app->params['ctrip']['relation_supplier_id']]);
- if (!$hotel || !$room) {
- $rs['code'] = 2;
- $rs['info'] = "当前酒店或者房型未mapping,参数$begin_date|$end_date|$hotel_id|$base_room_id|$room_id";
- return $rs;
- }
-
- $query = RunHotelDistrib::find()
- ->joinWith('operaHotelRoom c')
- ->leftJoin('opera_room_distrib d', 'a.DISTRIB_ID=d.DISTRIB_ID and d.ROOM_ID=c.ID')
- ->from('run_hotel_distrib a');
- $query->select(['DISTINCT(a.RUN_DATE)', 'a.PROD_PRICE', 'c.BREAKFAST_INCLUDE', 'c.ID as ROOM_ID']);
- $query->where([
- 'a.DISTRIB_ID' => Yii::$app->params['ctrip']['supplier_id'],
- 'a.HOTEL_ID' => $hotel_id,
- 'a.BASE_ROOM_TYPE' => $base_room_id,
- 'a.ROOM_TYPE' => $room_id,
- 'd.AUTHORITY_STATUS' => 1
- ]);
-
- $query->andFilterWhere([
- 'between', 'a.RUN_DATE', $begin_date, $end_date
- ]);
- $res = $query->asArray()->all();
- $rs['code'] = 0;
- $rs['info'] = "数据获取成功!";
- $rs['data'] = $res;
- return $rs;
-
- }
-
-
- /**
- * User:Steven
- * Desc:获取保留房库存
- * @param $param array
- * param StartDate
- * param EndDate
- * param hotel_id
- * param parent_room_type
- * param room_type
- *
- * @return array|\yii\db\ActiveRecord[]
- */
- public function getRoomQuantity($param)
- {
- $begin_date = isset($param['StartDate']) ? ($param['StartDate'] >= date('Y-m-d', time()) ? $param['StartDate'] : date('Y-m-d', time())) : false;
- $end_date = isset($param['EndDate']) ? ($param['EndDate'] >= date('Y-m-d', time()) ? $param['EndDate'] : date('Y-m-d', time())) : false;
- $hotel_id = isset($param['hotel_id']) ? $param['hotel_id'] : false;
- $base_room_id = isset($param['parent_room_type']) ? $param['parent_room_type'] : false;
- $room_id = isset($param['room_type']) ? $param['room_type'] : false;
- if (!$begin_date || !$end_date || !$hotel_id || !$base_room_id || !$room_id) {
- $rs['code'] = 1;
- $rs['info'] = "获取保留房库存错误,缺少必要参数|$begin_date|$end_date|$hotel_id|$base_room_id|$room_id";
- return $rs;
- }
- $hotel = HotelRelation::findOne(['HotelId' => $param['hotel_id'], 'ChannelId' => Yii::$app->params['ctrip']['relation_supplier_id']]);
- $operate_hotel_room = OperaHotelRoom::findOne(['HOTEL_ID' => $param['hotel_id'], 'PARENT_ROOM_TYPE' => $param['parent_room_type'], 'ROOM_TYPE' => $param['room_type'], 'CANCEL_FLAG' => 0]);
- $room = RoomRelation::findOne(['RoomId' => $operate_hotel_room->ID, 'ChannelId' => Yii::$app->params['ctrip']['relation_supplier_id']]);
- if (!$hotel || !$room) {
- $rs['code'] = 2;
- $rs['info'] = "当前酒店或者房型未mapping,参数$begin_date|$end_date|$hotel_id|$base_room_id|$room_id";
- return $rs;
- }
-
- // 获取推送数据
- $query = RunHotelDistrib::find()
- ->joinWith('operaHotelRoom c')
- ->joinWith('runHotelSubRoom d')
- ->joinWith('operaHotel e')
- ->leftJoin('opera_room_distrib f', 'a.DISTRIB_ID=f.DISTRIB_ID AND f.ROOM_ID=c.ID')
- ->from('run_hotel_distrib a');
- $query->select(['DISTINCT(a.RUN_DATE)', 'a.REMAINING_COUNT', 'a.RUN_STATUS', 'a.OVERSELL_FLAG', 'c.IS_ONSALE', 'c.LASTEST_BOOK_TIME', 'c.ID as ROOM_ID', 'f.LATEST_COMFIRM_TIME as ReserveTime',
- 'd.IS_ONSALE as BASE_ROOM_IS_ONSALE', 'd.RUN_STATUS as BASE_ROOM_RUN_STATUS', 'f.AUTHORITY_STATUS']);
- $query->where([
- 'c.CANCEL_FLAG' => 0,
- 'e.CANCEL_FLAG' => 0,
- 'a.DISTRIB_ID' => Yii::$app->params['ctrip']['supplier_id'],
- 'a.HOTEL_ID' => $hotel_id,
- 'a.BASE_ROOM_TYPE' => $base_room_id,
- 'a.ROOM_TYPE' => $room_id,
- ]);
-
- $query->andFilterWhere([
- 'between', 'a.RUN_DATE', $begin_date, $end_date
- ]);
- // $sql = $query -> createCommand() -> getRawSql();
- $res = $query->asArray()->all();
- $rs['code'] = 0;
- $rs['info'] = "数据获取成功!";
- $rs['data'] = $res;
- return $rs;
- }
-
- /**
- * Author:Steven
- * Desc:清零失效保留房的库存
- * @param $distrib_id
- * @param $begin_date
- * @param $hotel_id
- * @param $parent_room_type
- * @param $room_type
- * @return bool
- * @throws Exception
- */
- public function clearReservingRoom($distrib_id, $end_date, $hotel_id, $parent_room_type, $room_type)
- {
- $transaction = Yii::$app->db->beginTransaction();
- try {
- //更新基础房型的库存
- $distribe_data = RunHotelDistrib::find()
- ->leftJoin('opera_hotel_base_room b', 'a.HOTEL_ID=b.HOTEL_ID and a.BASE_ROOM_TYPE=b.MAIN_ID')
- ->leftJoin('opera_hotel_room c', 'a.HOTEL_ID=c.HOTEL_ID and a.BASE_ROOM_TYPE=c.PARENT_ROOM_TYPE and a.ROOM_TYPE=c.ROOM_TYPE')
- ->leftJoin('base_supplier d', 'a.DISTRIB_ID=d.ID')
- ->from('run_hotel_distrib a')
- ->select(['a.DISTRIB_ID', 'a.HOTEL_ID', 'a.BASE_ROOM_TYPE', 'a.ROOM_TYPE', 'a.RUN_DATE', 'b.BASE_ROOM_NAME', 'c.ROOM_NAME', 'c.INNER_IDENTIFY',
- 'd.SUPPLIER_NAME', 'SUM(a.REMAINING_COUNT) as COUNT'])
- ->where([
- 'a.DISTRIB_ID' => $distrib_id,
- 'a.HOTEL_ID' => $hotel_id,
- 'a.BASE_ROOM_TYPE' => $parent_room_type,
- 'a.ROOM_TYPE' => $room_type,
- 'b.CANCEL_FLAG' => 0,
- 'c.CANCEL_FLAG' => 0,
- 'd.CANCEL_FLAG' => 0
- ])->groupBy(['a.RUN_DATE'])->andFilterWhere(['between', 'a.RUN_DATE', date('Y-m-d'), $end_date])->asArray()->all();
-
- $run_hotel = RunHotel::find()
- ->select(['HOTEL_ID', 'BASE_ROOM_TYPE', 'RUN_DATE', 'STOCK_TYPE', 'REMAINING_COUNT',
- 'SUM(case stock_type when 228 then remaining_count else 0 end) as BUYOUT', //买断
- 'SUM(case stock_type when 229 then remaining_count else 0 end) as INQUIRY', //现询
- 'SUM(case stock_type when 230 then remaining_count else 0 end) as RETAIN'])//保留
- ->where([
- 'HOTEL_ID' => $hotel_id,
- 'BASE_ROOM_TYPE' => $parent_room_type
- ])->andFilterWhere(['between', 'RUN_DATE', date('Y-m-d'), $end_date])->groupBy('RUN_DATE')->orderBy(['RUN_DATE' => SORT_ASC, 'STOCK_TYPE' => SORT_ASC])
- ->asArray()->all();
-
- foreach ($run_hotel as $item) { //每个日期有三条记录
- if ($item['RETAIN'] == 0 && $item['BUYOUT'] == 0) { //买断、保留均无库存,不再更新
- continue;
- }
- foreach ($distribe_data as $val) {
- if ($item['RUN_DATE'] == $val['RUN_DATE']) {
- if ($val['COUNT'] == 0) {
- continue;
- }
- if ($val['COUNT'] > $item['RETAIN']) { //说明保留房库存不够扣除,还需再扣除买断
- ////买断房需要扣除的库存
- $remain_count = ($item['BUYOUT'] - ($val['COUNT'] - $item['RETAIN'])) <= 0 ? 0 : ($item['BUYOUT'] - ($val['COUNT'] - $item['RETAIN']));
- //扣除买断
- $buyout_effect = RunHotel::updateAll([
- 'REMAINING_COUNT' => $remain_count,
- 'UPDATE_TIME' => date('Y-m-d H:i:s')
- ], [
- 'and', ['=', 'STOCK_TYPE', 228],
- ['=', 'RUN_DATE', $val['RUN_DATE']],
- ['=', 'HOTEL_ID', $hotel_id],
- ['=', 'BASE_ROOM_TYPE', $parent_room_type]
- ]);
- if ($buyout_effect == 0) {
- throw new \Exception();
- }
- $opera_hotel_log = new OperaHotelLog();
- $opera_hotel_log->CREATE_USER_ID = 2;
- $opera_hotel_log->CREATE_TIME = date('Y-m-d H:i:s');
- $opera_hotel_log->LOG_TYPE = 2; //房型操作日志
- $opera_hotel_log->HOTEL_ID = $hotel_id;
- $opera_hotel_log->PARENT_ROOM_TYPE = $parent_room_type;
- $opera_hotel_log->ROOM_TYPE = 0;
- $opera_hotel_log->LOG_DESC = '保留房失效设置:' . $val['BASE_ROOM_NAME'] . $val['RUN_DATE'] . '买断库存扣除' . $remain_count . '间';
- $opera_hotel_log->save();
- }
- //扣除保留库存
- if ($item['RETAIN'] > 0) {
- // 基础房型保留房数量库存小于渠道保留房剩余库存
- $rc_count = ($item['RETAIN'] - $val['COUNT']) <= 0 ? 0 : ($item['RETAIN'] - $val['COUNT']);
- $retain_effect = RunHotel::updateAll([
- 'REMAINING_COUNT' => $rc_count,
- 'UPDATE_TIME' => date('Y-m-d H:i:s')
- ], [
- 'and', ['=', 'STOCK_TYPE', 230],
- ['=', 'RUN_DATE', $val['RUN_DATE']],
- ['=', 'HOTEL_ID', $hotel_id],
- ['=', 'BASE_ROOM_TYPE', $parent_room_type]
- ]);
- if ($retain_effect == 0) {
- throw new \Exception();
- }
- $opera_hotel_log = new OperaHotelLog();
- $opera_hotel_log->CREATE_USER_ID = 2;
- $opera_hotel_log->CREATE_TIME = date('Y-m-d H:i:s');
- $opera_hotel_log->LOG_TYPE = 2; //房型操作日志
- $opera_hotel_log->HOTEL_ID = $hotel_id;
- $opera_hotel_log->PARENT_ROOM_TYPE = $parent_room_type;
- $opera_hotel_log->ROOM_TYPE = 0;
- $opera_hotel_log->LOG_DESC = '保留房失效设置:' . $val['BASE_ROOM_NAME'] . $val['RUN_DATE'] . '保留库存扣除' . $val['COUNT'] . '间';
- $opera_hotel_log->save();
- }
- }
- }
- }
- //更新渠道的固定数量(保留房) 直接将库存置为o
- $effect_num = RunHotelDistrib::updateAll(['REMAINING_COUNT' => 0, 'UPDATE_TIME' => date('Y-m-d H:i:s')], [
- 'and', ['=', 'DISTRIB_ID', $distrib_id],
- ['=', 'HOTEL_ID', $hotel_id],
- ['=', 'BASE_ROOM_TYPE', $parent_room_type],
- ['=', 'ROOM_TYPE', $room_type],
- ['between', 'RUN_DATE', date('Y-m-d'), $end_date]
- ]);
-
- foreach ($distribe_data as $value) {
- if ($value['COUNT'] == 0) {
- continue;
- }
- $opera_hotel_log = new OperaHotelLog();
- $opera_hotel_log->CREATE_USER_ID = 2;
- $opera_hotel_log->CREATE_TIME = date('Y-m-d H:i:s');
- $opera_hotel_log->LOG_TYPE = 2; //房型操作日志
- $opera_hotel_log->HOTEL_ID = $hotel_id;
- $opera_hotel_log->PARENT_ROOM_TYPE = $parent_room_type;
- $opera_hotel_log->ROOM_TYPE = 0;
- $opera_hotel_log->LOG_DESC = '保留房失效设置:' . "{$value['SUPPLIER_NAME']}/{$value['BASE_ROOM_NAME']}/{$value['ROOM_NAME']}{$value['INNER_IDENTIFY']}-"
- . date('Y-m-d') . "~~{$end_date}" . '保留库存扣除' . $value['COUNT'] . '间';
- $opera_hotel_log->save();
- }
-
- //推送房态数据到渠道(异步)
- $soap_param = array('hotel_id' => $hotel_id, 'parent_room_type' => $parent_room_type, 'room_type' => $room_type,
- 'begin_date' => date('Y-m-d'), 'end_date' => $end_date, 'channel_id' => $distrib_id);
- zHttp::syncRequest(CommonOrder::SEND_REQUEST_GET, Yii::$app->params['push_info_interface'], array('param' => $soap_param));
- $transaction->commit();
- } catch (\Exception $e) {
- $transaction->rollBack();
- return false;
- }
- return true;
- }
-
- /**
- * Des:根据日期及酒店ID获取子房型数据
- * Name: getAllRoomListByHotelDate
- * @param $params
- * @return array
- * @author 倪宗锋
- */
- public function getAllRoomListByHotelDate($params)
- {
- $baseSupplierSale = new BaseSupplierSale();
- $price = 'a.' . $baseSupplierSale->getSaleType($params['org_id']);
- $select = [
- 'hotel_id' => 'a.HOTEL_ID',//酒店ID
- 'base_room_type' => 'a.BASE_ROOM_TYPE',//基础房型ID
- 'room_type' => 'a.ROOM_TYPE',//房型ID
- 'room_id' => 'b.ID',//房型ID
- 'hotel_name' => new Expression("(select hotel_name from opera_hotel where hotel_id = a.HOTEL_ID)"),//酒店名称
- 'room_name' => new Expression("CONCAT(b.ROOM_NAME,IFNULL(d.DISTRIB_ROOM_NAME,''))"),//子房型名称
- 'base_room_name' => 'c.BASE_ROOM_NAME',//基础房型名称
- 'c.BED_TYPE',//床型
- 'area_size' => new Expression("IFNULL(c.AREA_SIZE,'')"),//房型面积
- 'avg_price' => new Expression("truncate(avg($price) ,2)"),//均价
- 'min_price' => new Expression("truncate(MIN($price) ,2)"),//最低价
- 'total_money' => new Expression("truncate(sum($price) ,2)"),//单份产品总价
- 'date_price' => new Expression("group_concat(concat(a.RUN_DATE,'|',truncate($price ,2)) SEPARATOR '||' )"),
- 'sub_room_sale_days' => //子房型上架天数
- new Expression("SUM((SELECT f.IS_ONSALE from run_hotel_sub_room f where a.HOTEL_ID=f.HOTEL_ID and a.BASE_ROOM_TYPE=f.BASE_ROOM_TYPE and a.ROOM_TYPE=f.ROOM_TYPE and a.RUN_DATE=f.RUN_DATE))"),
- 'room_sale_days' => //房型上架天数
- new Expression("COUNT(DISTINCT a.RUN_DATE)"),
- 'hotel_sale_days' => //酒店上架天数
- new Expression("SUM((SELECT if(SUM(1)=0,0,1) from run_hotel g where a.HOTEL_ID=g.HOTEL_ID and a.BASE_ROOM_TYPE=g.BASE_ROOM_TYPE and a.RUN_DATE=g.RUN_DATE))"),
- 'gift' => //礼包信息
- new Expression("IFNULL(GROUP_CONCAT((select CONCAT(ID,'|||',GIFT_NAME,'|||',a.RUN_DATE) from opera_hotel_gift where ID=a.GIFT_ID) SEPARATOR '||||'),'')"),
- 'room_open_days' =>//房型的房态开房天数
- new Expression("SUM((SELECT f.RUN_STATUS from run_hotel_sub_room f where a.HOTEL_ID=f.HOTEL_ID and a.BASE_ROOM_TYPE=f.BASE_ROOM_TYPE and a.ROOM_TYPE=f.ROOM_TYPE and a.RUN_DATE=f.RUN_DATE))"),
- 'org_room_open_days' => //渠道房型房态 开房天数
- new Expression("sum(if(a.RUN_STATUS=326,1,0))"),
- 'stock' => //库存
- new Expression("min(if(a.OVERSELL_FLAG=1,(SELECT SUM(REMAINING_COUNT) from run_hotel g where a.HOTEL_ID=g.HOTEL_ID and a.BASE_ROOM_TYPE=g.BASE_ROOM_TYPE and a.RUN_DATE=g.RUN_DATE and g.STOCK_TYPE in (228,229,230)), a.REMAINING_COUNT))"),
- 'b.LASTEST_BOOK_TIME',//最后预定时间 获取到PHP处理时间
- 'b.LASTEST_CANCEL_DAY',//最晚取消时间
- 'b.BREAKFAST_INCLUDE',//早餐人数
- 'room_img' => new Expression("ifnull(c.ROOM_IMG,'')"),//基础房型图片
- 'in_time' => new Expression("(select EARLIEST_CHECKIN_TIME from opera_hotel where hotel_id = a.HOTEL_ID)"),//最早入住时间
- ];
- $where = [
- 'and',
- ['=', 'a.HOTEL_ID', $params['hotel_id']],//酒店ID
- ['=', 'a.DISTRIB_ID', $params['org_id']],//分销商ID
- ['>=', 'a.RUN_DATE', $params['start_date']],//入住时间
- ['<', 'a.RUN_DATE', $params['end_date']],//离店时间
- ['=', 'd.AUTHORITY_STATUS', 1]
- ];
- if (empty($params['room_id']) == false) {//房型ID
- $where[] = ['=', 'b.ID', $params['room_id']];
- }
- $getList = self::find()->select($select)
- ->from(self::tableName() . ' a')
- ->innerJoin(OperaHotelRoom::tableName() . ' b', 'a.HOTEL_ID=b.HOTEL_ID and a.BASE_ROOM_TYPE=b.PARENT_ROOM_TYPE and a.ROOM_TYPE=b.ROOM_TYPE and b.CANCEL_FLAG=0 ')
- ->innerJoin(OperaRoomDistrib::tableName() . ' d', 'b.ID=d.ROOM_ID and a.DISTRIB_ID=d.DISTRIB_ID and d.CANCEL_FLAG=0')
- ->innerJoin(OperaHotelBaseRoom::tableName() . ' c', 'a.BASE_ROOM_TYPE=c.MAIN_ID and a.HOTEL_ID=c.HOTEL_ID and c.CANCEL_FLAG=0')
- ->where($where)
- ->groupBy('b.ID')
- ->orderBy('avg_price')
- // ->createCommand()
- // ->getRawSql();
- // print_r($getList);die;
- ->asArray()
- ->all();
- return $getList;
- }
-
- /**
- * Function Description:查询子房型数据列表,按日期、销售方式、渠道商(微信检索列表)
- * Function Name: getRoomTypeList
- * @param $start_date
- * @param $end_date
- * @param $base_supplier_sale
- * @param $distrib_id
- *
- * @return array|ActiveRecord[]
- *
- * @author 娄梦宁
- */
- public function getRoomTypeList($start_date, $end_date, $base_supplier_sale, $distrib_id)
- {
- $select = [
- 'a.room_type',
- 'e.hotel_id',
- 'e.hotel_name',
- 'e.star_level',
- 'area_id' => new Expression('(select parent_id from base_area where id=e.area_id)'),
- 'is_promotion' => new Expression('ifnull(e.is_promotion,"")'),
- 'hotel_label' => new Expression("ifnull((select GROUP_CONCAT(res_name) from base_resource where instr((select SALE_LABEL from opera_hotel where hotel_id=e.hotel_id),RES_ID) and res_type_id=709 GROUP BY res_type_id),'')"),
- 'brand' => new Expression('ifnull((select res_name from base_resource where res_id=e.brand),"")'),
- 'hotel_type' => new Expression('ifnull((select res_name from base_resource where res_id=e.hotel_type),"")'),
- 'sale_label' => new Expression("ifnull((select GROUP_CONCAT(res_name) from base_resource where instr((select SALE_LABEL from opera_hotel where hotel_id=a.hotel_id),RES_ID) and res_type_id=710 GROUP BY res_type_id),'')"),
- 'recommend_level' => new Expression('ifnull(e.recommend_level,"")'),
- 'hotel_image' => new Expression('ifnull(e.hotel_image,"")'),
- 'business_area' => new Expression('ifnull(e.business_area,"")'),
- 'b.breakfast_include',
- 'bed_type' => new Expression('(if(b.BED_TYPE=0,c.BED_TYPE,b.bed_type))'),
- 'a.run_date',
- 'a.room_type',
- 'price' => new Expression('a.' . $base_supplier_sale),
- 'a.room_type',
- 'a.run_status',
- 'a.base_room_type',
- 'area_name' => new Expression('(select area_name from base_area bs where bs.id=e.AREA_ID)'),
- 'stock' => new Expression('(if(a.run_status=326,(if(a.OVERSELL_FLAG=1,(SELECT SUM(REMAINING_COUNT) from run_hotel g where a.HOTEL_ID=g.HOTEL_ID and a.BASE_ROOM_TYPE=g.BASE_ROOM_TYPE and a.RUN_DATE=g.RUN_DATE), a.REMAINING_COUNT)),0))'),
- ];
- $where = [
- 'and',
- ['=', 'd.authority_status', 1],
- ['>=', 'a.run_date', $start_date],
- ['<', 'a.run_date', $end_date],
- ['=', 'a.distrib_id', $distrib_id],
- ];
- $room_list = self::find()
- ->select($select)
- ->from(self::tableName() . ' a')
- ->innerJoin(OperaHotelRoom::tableName() . ' b', 'a.HOTEL_ID=b.HOTEL_ID and a.BASE_ROOM_TYPE=b.PARENT_ROOM_TYPE and a.ROOM_TYPE=b.ROOM_TYPE and b.CANCEL_FLAG=0 ')
- ->innerJoin(OperaRoomDistrib::tableName() . ' d', 'b.ID=d.ROOM_ID and a.DISTRIB_ID=d.DISTRIB_ID and d.CANCEL_FLAG=0')
- ->innerJoin(OperaHotelBaseRoom::tableName() . ' c', 'a.BASE_ROOM_TYPE=c.MAIN_ID and a.HOTEL_ID=c.HOTEL_ID and c.CANCEL_FLAG=0')
- ->innerJoin(OperaHotel::tableName() . ' e', 'a.hotel_id=e.hotel_id and e.cancel_flag =0 and e.hotel_status=1')
- ->where($where)
- ->asArray()
- ->all();
- return $room_list;
- }
-
-
- /**
- * Notes:
- * User: Steven
- * Date: 2018/2/7
- * Time: 13:36
- * @param $params
- * @return bool
- * @throws Exception
- */
- public function setMultipleRoomPriceID($params)
- {
- //查询渠道的售卖方式
- $supplier = HotelSupplier::find()->select([
- 'a.SUPPLIER_NAME', 'b.SALE_TYPE', 'b.COMMISION_FLAG', 'b.COMMISION_TYPE', 'b.BACK_COMMISION_TYPE',
- 'b.BACK_COMMISION_METHOD', 'b.BACK_PERCENT', 'b.BACK_VALUE'])
- ->leftJoin('base_supplier_sale b', "b.SUPPLIER_ID=a.ID and b.PARENT_TYPE=25")
- ->from('base_supplier a')->where(['a.ID' => $params['distrib_id']])->asArray()->one();
- $price_type = $supplier['SALE_TYPE'] = HotelSupplier::SALE_TYPE_DISTRIB ? 'PROD_PRICE' : 'CUS_PRICE';
- if (empty($supplier)) {
- return false;
- }
- $row_arr = explode('||', $params['room_info_str']);
- $transaction = Yii::$app->db->beginTransaction();
- try {
- foreach ($row_arr as $room_info) {
- $room_info_row = substr($room_info, 1, (strlen($room_info) - 2));
- $item = explode(',', $room_info_row);
- //0:hotel_id;1:base_room_id;2:room_id;3:start_date;4:end_date;5:week;6:channel_id; 7:price;8:operator
- $date_arr = Utils::createDateByWeek($item[3], $item[4], str_split($item[5]));
- $count = RunHotelDistrib::updateAll(
- [
- "{$price_type}" => new Expression('PROD_PRICE' . $item[8] . $item[7]),
- 'UPDATE_TIME' => date('Y-m-d H:i:s')
- ],
- ['and',
- ['=', 'HOTEL_ID', $item[0]],
- ['=', 'BASE_ROOM_TYPE', $item[1]],
- ['=', 'ROOM_TYPE', $item[2]],
- ['=', 'DISTRIB_ID', $item[6]],
- ['in', 'RUN_DATE', $date_arr]
- ]);
- if ($count == 0) {
- throw new Exception('');
- }
- $request_param = array('hotel_id' => $item[0], 'parent_room_type' => $item[1], 'room_type' => $item[2],
- 'begin_date' => $item[3], 'end_date' => $item[4], 'channel_id' => $item[6]);
- zHttp::syncRequest(CommonOrder::SEND_REQUEST_GET, Yii::$app->params['push_info_interface'], array('param' => $request_param));
- }
- $transaction->commit();
- return true;
- } catch (\Exception $e) {
- $error = $e->getMessage();
- $transaction->rollBack();
- return false;
- }
- }
- }
-
- ?>
|