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.

MakeHotelOrder.class.php 17 KiB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <?php
  2. require_once __DIR__ . '/../HotelLib.php';
  3. /**
  4. * Created by PhpStorm.
  5. * User: luocj
  6. * Date: 2016/9/29
  7. * Time: 17:06
  8. */
  9. class MakeHotelOrder extends HotelLib
  10. {
  11. function getOrgList()
  12. {
  13. $sql = "select a.id as org_id ,a.supplier_name as org_name from base_supplier a inner join base_supplier_sale b on a.id = b.supplier_id where a.cancel_flag = 0 and a.supplier_type = 301 and a.is_disabled = 0 and b.parent_type = 25 and b.cancel_flag=0";
  14. writeLog('sql= ' . $sql);
  15. $org_list = $this->DBTool->queryBySql($sql);
  16. $data['code'] = $org_list['code'];
  17. $data['info'] = $org_list['info'];
  18. $data['org_list'] = $org_list['rowset'];
  19. return $data;
  20. }
  21. // function getOrderList($start_date, $end_date, $hotel_id, $room_type, $org_id)
  22. // {
  23. // $data = ifNullParam(array($hotel_id, $room_type, $org_id, $start_date, $end_date));
  24. // if ($data['code'] != "0") {
  25. // return $data;
  26. // }
  27. //// $sql = "select a.prod_price as prod_price,b.run_date as run_date,a.total_count as total_count,a.saled_count as saled_count,b.base_price as base_price,d.parent_type as commision_type,d.back_commision_method as back_commision_method,d.back_percent as back_percent
  28. //// from run_hotel_distrib as a inner join run_hotel as b on b.id=a.run_id where a.distrib_id=$org_id and b.run_date
  29. //// between '$start_date' and '$end_date' and a.saled_count < a.total_count and b.HOTEL_ID =$hotel_id and b.room_type = $room_type";
  30. // //如果end_date 和 start_date 相同
  31. // if($start_date == $end_date){
  32. // $dateWhere = "b.run_date = '$start_date'";
  33. // }else{
  34. // $dateWhere = "b.run_date >= '$start_date' and b.run_date <'$end_date'";
  35. // }
  36. // $sql = "select a.distrib_id org_id,b.run_date as run_date,a.total_count as total_count,a.saled_count as saled_count,
  37. // b.base_price as base_price,d.back_commision_type as back_commision_type,d.back_commision_method as back_commision_method,
  38. // d.back_percent as back_percent,d.back_value as back_value,a.cus_price as lingshouprice,a.prod_price as fenxiaoprice,d.sale_type,
  39. // d.commision_flag as commision_flag
  40. // from run_hotel_distrib as a
  41. // inner join run_hotel as b on b.id=a.run_id
  42. // inner join opera_hotel c on c.hotel_id = b.hotel_id
  43. // inner join base_supplier_sale d on d.supplier_id =a.distrib_id
  44. // where a.distrib_id in ($org_id) and $dateWhere and a.saled_count < a.total_count and b.HOTEL_ID =$hotel_id and b.room_type = $room_type and d.parent_type = 25 and c.hotel_status = 1 and d.cancel_flag = 0 and b.is_onsale = 1 and b.run_status = 1 and a.authority_status = 1";
  45. //
  46. //
  47. // writeLog('sql= ' . $sql);
  48. // $order_list = $this->DBTool->queryBySql($sql);
  49. // $sqlt="select stock_type from run_hotel WHERE hotel_id=$hotel_id AND room_type=(select parent_room_type from run_hotel WHERE hotel_id=$hotel_id and room_type=$room_type limit 1) AND run_date>='{$start_date}' and run_date<'{$end_date}'";
  50. // $res=$this->DBTool->queryBySql($sqlt);
  51. // $data['order_list'] = $order_list['rowset'];
  52. // $data['stock_type']=$res['rowset'];
  53. //
  54. // return $data;
  55. // }
  56. function getRoomStock($hotel_id, $parent_room_type, $room_type, $org_id, $start_date, $end_date)
  57. {
  58. $sql = "CALL ht_get_room_stock({$hotel_id},{$parent_room_type},{$room_type},{$org_id},'{$start_date}','{$end_date}')";
  59. writeLog(__FUNCTION__ . " sql= " . $sql);
  60. $rowset = $this->DBTool->execProcedure($sql);
  61. $data['code'] = $rowset['code'];
  62. $data['info'] = $rowset['info'];
  63. $data['order_list'] = $rowset['rowset'];
  64. return $data;
  65. }
  66. //获取已售卖的数量
  67. public function getSaled($start_date, $end_date, $hotel_id, $room_type, $org_id, $para_stock_num = 0)
  68. {
  69. if ($start_date == $end_date) {
  70. $dateWhere = "b.run_date = '$start_date'";
  71. } else {
  72. $dateWhere = "b.run_date >= '$start_date' and b.run_date <'$end_date'";
  73. }
  74. $sql = "SELECT
  75. a.distrib_id org_id,
  76. b.run_date AS run_date,
  77. a.total_count AS total_count,
  78. a.saled_count AS saled_count
  79. FROM
  80. run_hotel_distrib AS a
  81. INNER JOIN run_hotel AS b ON b.id = a.run_id
  82. WHERE
  83. a.distrib_id IN ($org_id)
  84. AND $dateWhere
  85. AND b.HOTEL_ID = $hotel_id
  86. AND b.room_type = $room_type and a.SALED_COUNT > $para_stock_num";
  87. $order_list = $this->DBTool->queryBySql($sql);
  88. $data['order_list'] = $order_list['rowset'];
  89. return $data;
  90. }
  91. //最大数量必须大于数据库查询结果中最小的数字
  92. function getMaxCount($start_date, $end_date, $hotel_id, $param_count)
  93. {
  94. $result = 0;
  95. if ($start_date == $end_date) {
  96. $dateWhere = "run_date = '$start_date'";
  97. } else {
  98. $dateWhere = "run_date >= '$start_date' and run_date <'$end_date'";
  99. }
  100. $sql = "SELECT
  101. sum(remaining_count) tot
  102. from run_hotel r
  103. where HOTEL_ID = $hotel_id
  104. AND $dateWhere
  105. AND STOCK_TYPE in (228,230)
  106. group by RUN_DATE";
  107. $res = $this->DBTool->queryBySql($sql);
  108. if ($res['code'] == 0) {
  109. if (!empty($res['rowset'])) { //
  110. foreach ($res['rowset'] as $re) {
  111. if ($re['tot'] < $param_count) {
  112. $res['code'] = 1;
  113. $res['info'] = '固定数量不能大于可用的买断和保留房间总数!';
  114. return $res;
  115. }
  116. }
  117. }
  118. }
  119. return $res;
  120. }
  121. function getMaxNumber($start_date, $end_date, $hotel_id, $param_count)
  122. {
  123. $result = 0;
  124. if ($start_date == $end_date) {
  125. $dateWhere = "run_date = '$start_date'";
  126. } else {
  127. $dateWhere = "run_date >= '$start_date' and run_date <'$end_date'";
  128. }
  129. $sql = "SELECT
  130. sum(remaining_count) tot
  131. from run_hotel r
  132. where HOTEL_ID = $hotel_id
  133. AND $dateWhere
  134. AND STOCK_TYPE in (228,230)
  135. group by RUN_DATE";
  136. $res = $this->DBTool->queryBySql($sql);
  137. if ($res['code'] == 0) {
  138. if (!empty($res['rowset'])) { //
  139. foreach ($res['rowset'] as $re) {
  140. if ($re['tot'] < $param_count) {
  141. $res['code'] = 1;
  142. $res['info'] = '固定数量不能大于可用的买断和保留房间总数!';
  143. return $res;
  144. }
  145. }
  146. }
  147. }
  148. return $res;
  149. }
  150. //获取渠道商已经占用的库存数量
  151. function getUsedCount($start_date, $end_date, $hotel_id, $base_room, $param_count, $distrib_id, $total)
  152. {
  153. $result = 0;
  154. $count = mb_substr_count($distrib_id, ',') + 1;
  155. $param_count = $param_count * $count;
  156. if ($start_date == $end_date) {
  157. $dateWhere = "run_date = '$start_date'";
  158. } else {
  159. $dateWhere = "run_date >= '$start_date' and run_date <'$end_date'";
  160. }
  161. $sql = "
  162. SELECT
  163. sum(remaining_count) tot
  164. from run_hotel_distrib r
  165. INNER JOIN base_supplier s on r.DISTRIB_ID = s.ID and s.CANCEL_FLAG = 0
  166. INNER JOIN opera_hotel_room m on m.hotel_id = r.HOTEL_ID and m.PARENT_ROOM_TYPE= r.BASE_ROOM_TYPE and m.ROOM_TYPE = r.ROOM_TYPE
  167. where r.HOTEL_ID = $hotel_id
  168. AND $dateWhere
  169. AND DISTRIB_ID not in($distrib_id)
  170. AND BASE_ROOM_TYPE = $base_room
  171. AND m.CANCEL_FLAG=0
  172. group by RUN_DATE";
  173. $res = $this->DBTool->queryBySql($sql);
  174. if ($res['code'] == 0) {
  175. if (!empty($res['rowset'])) { //
  176. foreach ($res['rowset'] as $re) {
  177. if ($re['tot'] + $param_count > $total) {
  178. $res['code'] = 1;
  179. $res['info'] = '渠道商固定数量总和不能大于可用的买断和保留房间总数!';
  180. return $res;
  181. }
  182. }
  183. }
  184. }
  185. return $res;
  186. }
  187. /**
  188. * 录入订单
  189. * @param $user_id
  190. * @param $hotel_id
  191. * @param $room_type
  192. * @param $org_id
  193. * @param $org_num
  194. * @param $room_list
  195. * @param $cus_list
  196. * @param $checkin_time
  197. * @param $if_cancel
  198. * @param $benefit
  199. * @param $total_commission
  200. * @param $order_id
  201. * @return mixed
  202. */
  203. function makeOrderList($user_id, $hotel_id, $room_type, $org_id, $org_num, $room_list, $cus_list, $checkin_time, $if_cancel, $benefit, $total_commission, $order_id)
  204. {
  205. //处理room_list 单间佣金不超过小数点后2位
  206. $tmp = substr($room_list, 1, -1);
  207. $tmp_arr = explode('}{', $tmp);
  208. $new_room_list = '';
  209. foreach ($tmp_arr as $k => $v) {
  210. $list_info = explode(',', $v);
  211. $list_info[2] = round($list_info[2], 2);
  212. $list_info[3] = round($list_info[3], 2);
  213. $new_room_list .= '{' . $list_info[0] . ',' . $list_info[1] . ',' . $list_info[2] . ',' . $list_info[3] . '}';
  214. }
  215. $flag = $order_id == 0 ? true : false;
  216. $today = date('Y-m-d H:i:s', time());
  217. /* $data = ifNullParam(array($hotel_id, $room_type, $org_id, $org_num, $room_list, $cus_list, $checkin_time));
  218. if ($data['code'] != "0") {
  219. return $data;
  220. }*/
  221. $order_status = 313;
  222. //取消订单前判断是否为下线产品
  223. $sql_is_onsale = "select is_onsale from opera_hotel_room where hotel_id= $hotel_id and room_type = $room_type and cancel_flag = 0";
  224. $result_is_onsale = $this->DBTool->queryBySql($sql_is_onsale);
  225. if ($result_is_onsale['rowset'][0]['is_onsale'] != 1) {
  226. $data['code'] = 10086;
  227. $data['info'] = '该子房型已下线';
  228. return $data;
  229. }
  230. //原订单号不为0,是修改订单,先取消订单,带修改标识(非真正取消订单)
  231. if ($order_id != 0) {
  232. //先判断订单的状态,如果是异常处理代发单,并且修改了影响库存的变量(渠道商,入住日期范围,房间数),那么需要执行异常处理代发单的取消逻辑
  233. $sql = "select order_status from order_main where order_id=$order_id and CANCEL_FLAG=0;";
  234. $res = $this->DBTool->queryBySql($sql); //获取当前订单的状态
  235. if ($res['code'] != 0) {
  236. $result['code'] = '1';
  237. $result['info'] = '订单数据拉取失败';
  238. return $result;
  239. }
  240. $order_status = $res['rowset'][0]['order_status'];
  241. if ($order_status == 382) { //异常处理代发单状态
  242. //查询当前订单的订单渠道、入住日期范围、房间数
  243. $sql_orig = "select outside_sale_org_id,order_description from order_main where order_id=$order_id and CANCEL_FLAG=0;";
  244. $res_orig = $this->DBTool->queryBySql($sql_orig);
  245. if ($res_orig['code'] == 0) {
  246. $res_info = $res_orig['rowset'][0];
  247. $tmp = substr($res_info['order_description'], 0, -1);
  248. $tmp_arr = explode('|', $tmp);
  249. $new_str = '';
  250. foreach ($tmp_arr as $k => $v) {
  251. $info_arr = explode(',', $v);
  252. $new_str .= $info_arr[1] . ',' . $info_arr[2] . '|';
  253. }
  254. $res_info['order_description'] = $new_str;
  255. }
  256. $compare_new = '';
  257. $room_list_str = substr($room_list, 1, -1);
  258. $room_list_arr = explode('}{', $room_list_str);
  259. foreach ($room_list_arr as $r_k => $r_v) {
  260. $info_list = explode(',', $r_v);
  261. $date = substr($info_list[0], 2, -2);//入住日期
  262. $room_num = $info_list[1];//房间数
  263. $compare_new .= $date . ',' . $room_num . '|';
  264. }
  265. //判断影响库存的属性值是否被修改
  266. if ($org_id != $res_info['outside_sale_org_id'] || $compare_new != $res_info['order_description']) {
  267. //说明影响库存的属性有更改,这是的取消订单相应的库存不进行释放
  268. $sql_cancel = "CALL ht_cancel_order(" . $user_id . "," . $order_id . ", 3)";
  269. $order_status = 313;
  270. writeLog(__FUNCTION__ . "有影响库存的属性更新,删除后不释放库存" . $sql_cancel);
  271. } else {
  272. $sql_cancel = "CALL ht_cancel_order(" . $user_id . "," . $order_id . ", 2)";
  273. writeLog(__FUNCTION__ . "无影响库存的属性更新,正常释放库存" . $sql_cancel);
  274. }
  275. $rowset = $this->DBTool->execProcedure($sql_cancel);
  276. } else { //订单在未安排的情况下修改订单
  277. $sql_cancel = "CALL ht_cancel_order(" . $user_id . "," . $order_id . ", 2)";
  278. writeLog(__FUNCTION__ . "订单在未安排的情况下修改订单" . $sql_cancel);
  279. $rowset = $this->DBTool->execProcedure($sql_cancel);
  280. }
  281. if ($rowset['code'] != 0) {//修改订单时取消订单失败
  282. return $rowset;
  283. }
  284. $sql = "CALL ht_make_order({$user_id},{$hotel_id},{$room_type},{$org_id},'{$org_num}','{$new_room_list}','{$cus_list}','{$checkin_time}',{$order_id},{$order_status})";
  285. } else {
  286. $sql = "CALL ht_make_order({$user_id},{$hotel_id},{$room_type},{$org_id},'{$org_num}','{$new_room_list}','{$cus_list}','{$checkin_time}',{$order_id},{$order_status})";
  287. }
  288. //执行下单前判断该房型是否为上线
  289. if ($result_is_onsale['rowset'][0]['is_onsale'] == 1) {
  290. writeLog(__FUNCTION__ . " ht_make_order " . $sql);
  291. $rowset = $this->DBTool->execProcedure($sql);
  292. $data['code'] = $rowset['rowset'][0][0]['errcode'];
  293. $data['info'] = $rowset['rowset'][0][0]['errinfo'];
  294. } else {
  295. $data['code'] = 10086;
  296. $data['info'] = '该子房型已下线';
  297. }
  298. //echo json_encode($rowset);
  299. if ($rowset['code'] == "0" && $flag) {
  300. $this->setMemcache($rowset['rowset'][0][0]['order_id'], 313);
  301. $model = array(
  302. "user_id" => $user_id,
  303. "log_type" => 3,
  304. "hotel_id" => $hotel_id,
  305. "room_type" => $room_type,
  306. "log_desc" => "创建订单号为" . $rowset['rowset'][0][0]['order_id'] . "的订单",
  307. "order_id" => $rowset['rowset'][0][0]['order_id']
  308. );
  309. $this->DBLog->insertLog($model, '创建订单');
  310. //插入订单备注
  311. /*$memo_list_tmp = explode('||',explode(',',substr($cus_list,1,-1))[2]);
  312. foreach ($memo_list_tmp as $k=>$v){
  313. $memo_list[] = explode('|',$v);
  314. //0为操作人id,1为时间戳,2为备注类型,3为备注内容
  315. }
  316. if ($memo_list[0][0]!=''){
  317. $sql_memo = 'insert into order_comment(order_id,comment_type,comment_txt,create_user_id,update_user_id,create_time,update_time) values';
  318. foreach ($memo_list as $k=>$v){
  319. $sql_memo .= "('".$rowset['rowset'][0][0]['order_id']."',$v[2],'$v[3]',$v[0],$v[0],'".date('Y-m-d H:i:s',$v[1])."','".date('Y-m-d H:i:s',$v[1])."'),";
  320. }
  321. $sql_memo = substr($sql_memo,0,-1);
  322. $res = $this->DBTool->queryBySql($sql_memo);
  323. }*/
  324. }
  325. if ($order_id == 0 && $data['code'] == "0") {
  326. $data['code'] = $rowset['rowset'][0][0]['errcode'];
  327. $data['info'] = $rowset['rowset'][0][0]['errinfo'];
  328. $data['order_id'] = $rowset['rowset'][0][0]['order_id'];
  329. $sql = "insert into order_ht_status_log(order_id,order_status,before_status,cancel_flag,create_user_id,create_time,update_user_id,update_time) values ({$data['order_id']},313,0,0,$user_id,'{$today}',$user_id,'{$today}')";
  330. writeLog(__FUNCTION__ . " sql= " . $sql);
  331. $this->DBTool->execSql($sql);
  332. }
  333. // //出现了修改订单,取消成功但是修改不成功,则把订单的cancel_flag 给置回0
  334. // if ($data['code'] != 0 && $order_id != 0) {
  335. // $sql = "update order_main set cancel_flag = 0 where order_id = $order_id or parent_order_id = $order_id";
  336. // $this->DBTool->execSql($sql);
  337. // writeLog(__FUNCTION__ . "修改订单,取消成功但是修改不成功,则把订单的cancel_flag 给置回0, sql=" . $sql);
  338. // }
  339. return $data;
  340. }
  341. }
  342. //$obj = new MakeHotelOrder();
  343. //$data = $obj ->getOrgList('2016-09-01','2016-09-30',3,1,41);
  344. ////$data = $obj ->makeOrderList(1,3,1,4,123,);
  345. //echo json_encode($data);