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.
 
 
 
 
 
 

387 lines
17 KiB

  1. <?php
  2. namespace backend\modules\hotel\models\search;
  3. use backend\modules\api\models\BaseUser;
  4. use common\models\BaseSupplier;
  5. use Yii;
  6. use yii\base\Model;
  7. use yii\data\ActiveDataProvider;
  8. use backend\modules\hotel\models\OrderMain;
  9. use common\models\BaseUserAuth;
  10. /**
  11. * searchOrderMain represents the model behind the search form about `backend\modules\hotel\models\OrderMain`.
  12. * @property string $status_dict_type
  13. */
  14. class searchOrderMain extends OrderMain
  15. {
  16. public $status_dict_type;
  17. public $search_key;
  18. public $date_from; //查询日期 起始日期
  19. public $date_to; //查询日期 结束日期
  20. public $hotel_name; //酒店名称
  21. public $channel_id; //渠道
  22. public $diret_flag; //是否是直连订单
  23. public $confirm_num; //s是否有确认号
  24. public $sub_refuse_flag;
  25. public $invoice_status; //发票状态
  26. public $peiru;
  27. public $peichu;
  28. public $hotel_id;
  29. public static $date_type_array = [1 => '预订日期', '入住日期', '离店日期'];
  30. // public $search_status;
  31. /**
  32. * @inheritdoc
  33. */
  34. public function rules()
  35. {
  36. return [
  37. [['date_from', 'date_to', 'search_key', 'date_type', 'ORDER_STATUS', 'CHANNEL_ORDER_STATUS', 'sub_refuse_flag', 'hotel_name', 'channel_id', 'diret_flag',
  38. 'confirm_num', 'PROD_START_STATION_DATE', 'hotel_id', 'PROD_END_STATION_DATE', 'invoice_status', 'peiru', 'peichu'], 'safe'],
  39. ];
  40. }
  41. public function getSupplier()
  42. {
  43. return $this->hasOne(BaseSupplier::className(), ['ID' => 'PROD_TOP_ORG_ID']);
  44. }
  45. /**
  46. * @Author wanglg
  47. * @Desc 导出订单列表查询
  48. * @param $params
  49. * @return ActiveDataProvider
  50. */
  51. public function exportList($params)
  52. {
  53. $query = self::find()->joinWith('statusLabel a')
  54. ->joinWith('allMemo')
  55. ->joinWith('salesMan')
  56. ->joinWith('channelStatusLabel b');
  57. $dataProvider = new ActiveDataProvider([
  58. 'query' => $query,
  59. 'sort' => false,
  60. 'pagination' => false,
  61. ]);
  62. $this->load($params);
  63. $query->andFilterWhere([
  64. 'order_main.CANCEL_FLAG' => 0,
  65. 'ORDER_PROD_TYPE' => 25, //酒店主订单
  66. ]);
  67. $query->addSelect(['order_main.*', 'b.type_name as channel_type_name', 'GROUP_CONCAT(order_comment.COMMENT_TXT separator ";") AS CUSTOMER_MEMO',
  68. '(select count(*) from order_main m where m.PARENT_ORDER_ID=order_main.order_id AND m.CANCEL_FLAG=0) as room_count']);
  69. $query->andFilterWhere(['or',
  70. ['like', 'order_main.ORDER_ID', trim($this->search_key)],
  71. ['like', 'CUSTOMER_NAME', trim($this->search_key)],
  72. ['like', 'CUSTOMER_MOBILE', trim($this->search_key)]]);
  73. //预订时间
  74. $date_start = ' 00:00:00';
  75. $date_end = ' 23:59:59';
  76. if ($this->date_type == 2) {
  77. $query->andFilterWhere(['or', ['and', ['<=', 'PROD_START_STATION_DATE', $this->date_from], ['>', 'prod_end_station_date', $this->date_from]],
  78. ['and', ['<=', 'prod_start_station_date', $this->date_to], ['>', 'prod_end_station_date', $this->date_to]],
  79. ['and', ['>=', 'prod_start_station_date', $this->date_from], ['<=', 'prod_end_station_date', $this->date_to]]]);
  80. } elseif ($this->date_type == 3) {
  81. $date_column = 'PROD_END_STATION_DATE';
  82. $query->andFilterWhere(['between', $date_column, $this->date_from, $this->date_to]);
  83. } else {
  84. $date_column = 'order_main.CREATE_TIME';
  85. $query->andFilterWhere(['between', $date_column, $this->date_from . $date_start, $this->date_to . $date_end]);
  86. if ($this->PROD_START_STATION_DATE != '' || $this->PROD_END_STATION_DATE != '') { //注意,查询时这里的PROD_END_STATION_DATE不是离店日期,是入住的范围终点
  87. $this->PROD_START_STATION_DATE == '' ? $this->PROD_START_STATION_DATE = $this->PROD_END_STATION_DATE : true;
  88. $this->PROD_END_STATION_DATE == '' ? $this->PROD_END_STATION_DATE = $this->PROD_START_STATION_DATE : true;
  89. $query->andFilterWhere(['between', 'order_main.PROD_START_STATION_DATE', $this->PROD_START_STATION_DATE, $this->PROD_END_STATION_DATE]);
  90. }
  91. }
  92. if ($this->diret_flag) { //直连标志
  93. $query->andFilterWhere(['order_main.DOCKING_TYPE' => $this->diret_flag]);
  94. }
  95. if ($this->confirm_num) {
  96. if ($this->confirm_num == 1) {
  97. $query->andFilterWhere(['NOT', ['order_main.ORDER_CONFIRM_CODE' => 'null']]);
  98. } else {
  99. $query->andWhere(['order_main.ORDER_CONFIRM_CODE' => null]);
  100. }
  101. }
  102. if ($this->channel_id) {
  103. $query->andFilterWhere(['order_main.OUTSIDE_SALE_ORG_ID' => $this->channel_id]);
  104. }
  105. if ($this->hotel_name) {
  106. $query->andFilterWhere(['like', 'order_main.PARENT_PROD_NAME', trim($this->hotel_name)]);
  107. }
  108. $query->groupBy('order_main.ORDER_ID');
  109. $query->orderBy(['CREATE_TIME' => SORT_DESC]);
  110. if ($this->ORDER_STATUS) {
  111. $query->andFilterWhere(['order_main.ORDER_STATUS' => $this->ORDER_STATUS]);
  112. }
  113. if ($this->CHANNEL_ORDER_STATUS) {
  114. $query->andFilterWhere(['order_main.CHANNEL_ORDER_STATUS' => $this->CHANNEL_ORDER_STATUS]);
  115. }
  116. $query->groupBy('order_main.ORDER_ID');
  117. $sql = $query->createCommand()->getRawSql();
  118. return $dataProvider;
  119. // return $query -> asArray() -> all();
  120. }
  121. /**
  122. * Author:Steven
  123. * Desc:酒店订单列表
  124. * @param $params
  125. * @return array
  126. */
  127. public function search_list($params)
  128. {
  129. //statusLabel 订单状态
  130. $query = self::find()->joinWith('statusLabel a')
  131. ->joinWith(['user', 'allMemo', 'operaHotelRoom', 'baseChannel', 'channelStatusLabel b', 'orderInvoiceInfo c'])/*->joinWith('user')
  132. ->joinWith('allMemo')
  133. ->joinWith('operaHotelRoom')
  134. ->joinWith('baseChannel')
  135. ->joinWith('channelStatusLabel b')
  136. ->joinWith('orderInvoiceInfo c')*/
  137. ;
  138. // add conditions that should always apply here ->leftJoin('dict_type a','order_main.CHANNEL_ORDER_STATUS=dict_type.ID')
  139. $dataProvider = new ActiveDataProvider([
  140. 'query' => $query,
  141. 'sort' => false,
  142. 'pagination' => [
  143. 'pageSize' => 20,
  144. ]
  145. ]);
  146. $this->load($params);
  147. #region 功能描述数据权限
  148. //酒店运营人员只能看到自己运营的酒店订单,酒店采购负责人只能看到自己采购的酒店订单,其他(酒店管理人员、酒店客服人员、酒店客服管理人员)数据均可见
  149. $user_role = Yii::$app->user->identity->USER_ROLE; //获取当前用户的角色
  150. if ($user_role == BaseUserAuth::HOTEL_OPERATOR) { //酒店运营人员 ||$user_role==BaseUserAuth::BUS_OPERA_CUS
  151. $query->andFilterWhere([
  152. 'order_main.PRINCIPAL_ID' => Yii::$app->user->id,
  153. ]);
  154. } else if ($user_role == BaseUserAuth::HOTEL_PURCHASE) { //酒店采购负责人
  155. $query->andFilterWhere([
  156. 'order_main.SALES_MAN' => Yii::$app->user->id,
  157. ]);
  158. }
  159. #endregion
  160. if (!empty($params['cookie_order_id'])) {
  161. $query->andFilterWhere([
  162. 'order_main.ORDER_ID' => $params['cookie_order_id'],
  163. ]);
  164. }
  165. $query->andFilterWhere([
  166. 'order_main.CANCEL_FLAG' => 0,
  167. 'ORDER_PROD_TYPE' => 25, //酒店主订单
  168. // 'order_main.ORDER_STATUS' => $this->ORDER_STATUS //前台需要根据不同状态获取数量,这里不需要
  169. ]);
  170. $query->addSelect(['order_main.*']);
  171. $query->andFilterWhere(['or',
  172. ['like', 'order_main.ORDER_ID', trim($this->search_key)],
  173. ['like', 'CUSTOMER_NAME', trim($this->search_key)],
  174. ['like', 'CUSTOMER_MOBILE', trim($this->search_key)],
  175. ['like', 'ORDER_CONFIRM_CODE', trim($this->search_key)],
  176. ['like', 'order_main.OUTSIDE_ORDER_NO', trim($this->search_key)]]);
  177. //预订时间
  178. $date_start = ' 00:00:00';
  179. $date_end = ' 23:59:59';
  180. if ($this->date_type == 2) {
  181. $query->andFilterWhere(['or', ['and', ['<=', 'PROD_START_STATION_DATE', $this->date_from], ['>', 'prod_end_station_date', $this->date_from]],
  182. ['and', ['<=', 'prod_start_station_date', $this->date_to], ['>', 'prod_end_station_date', $this->date_to]],
  183. ['and', ['>=', 'prod_start_station_date', $this->date_from], ['<=', 'prod_end_station_date', $this->date_to]]]);
  184. //日期相同和不同
  185. /*if ($this->date_from == $this->date_to) {
  186. $query->andFilterWhere(['PROD_START_STATION_DATE' => $this->date_from]);
  187. } else {
  188. // $query->andFilterWhere(['and',
  189. // ['>=', 'PROD_START_STATION_DATE', $this->date_from],
  190. // ['<=', 'PROD_END_STATION_DATE', date('Y-m-d', strtotime('+1 days', strtotime($this->date_to)))]]);
  191. $query -> andFilterWhere(['or', ['and', ['<=', 'PROD_START_STATION_DATE', $this->date_from], ['>', 'prod_end_station_date', $this->date_from]],
  192. ['and', ['<=', 'prod_start_station_date', $this->date_to], ['>', 'prod_end_station_date', $this->date_to]],
  193. ['and', ['>=', 'prod_start_station_date', $this->date_from], ['<=', 'prod_end_station_date', $this->date_to]]]);
  194. }*/
  195. } elseif ($this->date_type == 3) {
  196. $date_column = 'PROD_END_STATION_DATE';
  197. $query->andFilterWhere(['between', $date_column, $this->date_from, $this->date_to]);
  198. } else {
  199. $date_column = 'order_main.CREATE_TIME';
  200. $query->andFilterWhere(['between', $date_column, $this->date_from . $date_start, $this->date_to . $date_end]);
  201. if ($this->PROD_START_STATION_DATE != '' || $this->PROD_END_STATION_DATE != '') { //注意,查询时这里的PROD_END_STATION_DATE不是离店日期,是入住的范围终点
  202. $this->PROD_START_STATION_DATE == '' ? $this->PROD_START_STATION_DATE = $this->PROD_END_STATION_DATE : true;
  203. $this->PROD_END_STATION_DATE == '' ? $this->PROD_END_STATION_DATE = $this->PROD_START_STATION_DATE : true;
  204. $query->andFilterWhere(['between', 'order_main.PROD_START_STATION_DATE', $this->PROD_START_STATION_DATE, $this->PROD_END_STATION_DATE]);
  205. }
  206. // $query->andFilterWhere(['>=', 'order_main.PROD_START_STATION_DATE', $this->date_from]);
  207. }
  208. if ($this->invoice_status) {
  209. $query->andFilterWhere(['c.Status' => $this->invoice_status]);
  210. }
  211. if ($this->diret_flag) { //直连标志
  212. $query->andFilterWhere(['order_main.DOCKING_TYPE' => $this->diret_flag]);
  213. }
  214. if ($this->confirm_num) {
  215. if ($this->confirm_num == 1) {
  216. $query->andFilterWhere(['NOT', ['order_main.ORDER_CONFIRM_CODE' => 'null']]);
  217. } else {
  218. $query->andWhere(['order_main.ORDER_CONFIRM_CODE' => null]);
  219. }
  220. }
  221. if ($this->channel_id) {
  222. $query->andFilterWhere(['order_main.OUTSIDE_SALE_ORG_ID' => $this->channel_id]);
  223. }
  224. if ($this->hotel_name) {
  225. $query->andFilterWhere(['like', 'order_main.PARENT_PROD_NAME', trim($this->hotel_name)]);
  226. }
  227. $query->groupBy('order_main.ORDER_ID');
  228. $query->orderBy(['CREATE_TIME' => SORT_DESC, 'RUN_DATE'=> SORT_ASC]);
  229. $sql = $query->createCommand()->getRawSql();
  230. if ($this->ORDER_STATUS) {
  231. $query->andFilterWhere(['order_main.ORDER_STATUS' => $this->ORDER_STATUS]);
  232. }
  233. if ($this->CHANNEL_ORDER_STATUS) {
  234. $query->andFilterWhere(['order_main.CHANNEL_ORDER_STATUS' => $this->CHANNEL_ORDER_STATUS]);
  235. }
  236. $statusCount = Yii::$app->db->createCommand('select t.order_status,count(*) as count from(' . $sql . ') as t GROUP BY t.ORDER_STATUS')->queryAll();
  237. $channnelStatusCount = Yii::$app->db->createCommand('select t.channel_order_status,count(*) as count from(' . $sql . ') as t GROUP BY t.CHANNEL_ORDER_STATUS')->queryAll();
  238. $status_arr = [];
  239. $channel_status_arr = [];
  240. foreach ($statusCount as $item) {
  241. $status_arr[0] += $item['count'];
  242. $status_arr[$item['order_status']] = $item['count'];
  243. }
  244. foreach ($channnelStatusCount as $item) {
  245. $channel_status_arr[$item['channel_order_status']] = $item['count'];
  246. }
  247. return ['dataProvider' => $dataProvider, 'status_arr' => $status_arr, 'channel_status_arr' => $channel_status_arr];
  248. }
  249. /**
  250. * 酒店供应商查询列表
  251. *
  252. * @param array $params
  253. *
  254. * @return ActiveDataProvider
  255. */
  256. public function supplierSearch($params)
  257. {
  258. //INNER JOIN order_ht_status_log g on m.ORDER_ID=g.ORDER_ID and g.ORDER_STATUS=198
  259. $query = self::find()->joinWith('statusLabel')->joinWith('user')->joinWith('publicMemo')->joinWith('operaHotel')
  260. ->innerJoin('order_ht_status_log', 'order_main.ORDER_ID = order_ht_status_log.ORDER_ID and order_ht_status_log.ORDER_STATUS=198');
  261. // add conditions that should always apply here
  262. $dataProvider = new ActiveDataProvider([
  263. 'query' => $query,
  264. 'pagination' => [
  265. 'pageSize' => 10,
  266. ],
  267. 'sort' => false
  268. // [
  269. // 'defaultOrder'=> ['CREATE_TIME'=> SORT_DESC],
  270. // 'attributes' => false
  271. // ]
  272. ]);
  273. $this->load($params);
  274. $login_user = Yii::$app->user->identity;
  275. /* @var $login_user \common\models\User */
  276. $this->search_key = trim($this->search_key);
  277. $query->andFilterWhere([
  278. 'order_main.CANCEL_FLAG' => 0,
  279. 'ORDER_PROD_TYPE' => 25, //酒店主订单
  280. 'opera_hotel.SUPPLIER_ID' => $login_user->ORG_ID, //当前账号供应商酒店下的订单
  281. // 'order_main.ORDER_STATUS' => $this->ORDER_STATUS //前台需要根据不同状态获取数量,这里不需要
  282. ]);
  283. $query->addSelect([
  284. 'order_main.*',
  285. '(select count(*) from order_main m where m.PARENT_ORDER_ID=order_main.order_id AND m.CANCEL_FLAG=0) as room_count',
  286. '(select count(*) from order_main m where m.PARENT_ORDER_ID=order_main.order_id AND m.STOCK_TYPE=229 and m.CANCEL_FLAG=0) as sub_refuse_flag',
  287. ]);
  288. $query->andFilterWhere(['or',
  289. ['like', 'order_main.ORDER_ID', $this->search_key],
  290. ['like', 'CUSTOMER_NAME', $this->search_key],
  291. ['like', 'CUSTOMER_MOBILE', $this->search_key]]);
  292. //预订时间
  293. $date_start = ' 00:00:00';
  294. $date_end = ' 23:59:59';
  295. if ($this->date_type == 2) {
  296. //日期相同和不同
  297. if ($this->date_from == $this->date_to) {
  298. $query->andFilterWhere(['PROD_START_STATION_DATE' => $this->date_from]);
  299. } else {
  300. $query->andFilterWhere(['and',
  301. ['>=', 'PROD_START_STATION_DATE', $this->date_from],
  302. ['<=', 'PROD_END_STATION_DATE', date('Y-m-d', strtotime('+1 days', strtotime($this->date_to)))]]);
  303. }
  304. } elseif ($this->date_type == 3) {
  305. $date_column = 'PROD_END_STATION_DATE';
  306. $query->andFilterWhere(['between', $date_column, $this->date_from, $this->date_to]);
  307. } else {
  308. $date_column = 'order_main.CREATE_TIME';
  309. $query->andFilterWhere(['between', $date_column, $this->date_from . $date_start, $this->date_to . $date_end]);
  310. }
  311. $query->groupBy('order_main.ORDER_ID');
  312. $query->orderBy(['CREATE_TIME' => SORT_DESC]);
  313. // $sql = $query -> createCommand()->getRawSql();
  314. return $dataProvider;
  315. }
  316. public function attributeLabels()
  317. {
  318. return array_merge(
  319. parent::attributeLabels(), [
  320. 'ID' => 'ID',
  321. 'CREATE_USER_ID' => '跟单客服',
  322. 'CREATE_USER_NAME' => '操作人',
  323. 'CREATE_TIME' => '预订时间',
  324. 'ORDER_ID' => '订单编号',
  325. 'OUTSIDE_ORDER_NO' => '渠道商订单号',
  326. 'PROD_NAME' => '预订产品',
  327. 'BASE_PRICE' => '成本金额',
  328. 'CUSTOMER_NAME' => '客人姓名',
  329. 'CUSTOMER_MEMO' => '备注',
  330. 'ORDER_STATUS' => '订单状态',
  331. 'date_type' => '时间范围',
  332. 'date_from' => '起始日期',
  333. 'date_to' => '结束日期',
  334. 'SUPPLIER_NAME' => '订单渠道',
  335. 'ORDER_PRICE' => '订单金额',
  336. 'PROD_START_STATION_DATE' => '入住时间',
  337. 'PROD_END_STATION_DATE' => '离店时间',
  338. 'PARENT_PROD_NAME' => '酒店名称',
  339. 'PROD_SUPPLY_ORG_NAME' => '供应商',
  340. 'TOTAL_COMMISSION' => '渠道佣金',
  341. 'SALES_NAME' => '采购负责人',
  342. 'PRINCIPAL' => '运营负责人',
  343. 'ORDER_TITLE_ID' => '是否是组合订单',
  344. 'PAY_WAY' => '支付方式',
  345. 'PROFIT_VALUE' => '利润',
  346. ]);
  347. }
  348. }