DBTool->queryBySql($sql); $data['code'] = $org_list['code']; $data['info'] = $org_list['info']; $data['org_list'] = $org_list['rowset']; return $data; } // function getOrderList($start_date, $end_date, $hotel_id, $room_type, $org_id) // { // $data = ifNullParam(array($hotel_id, $room_type, $org_id, $start_date, $end_date)); // if ($data['code'] != "0") { // return $data; // } //// $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 //// 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 //// 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"; // //如果end_date 和 start_date 相同 // if($start_date == $end_date){ // $dateWhere = "b.run_date = '$start_date'"; // }else{ // $dateWhere = "b.run_date >= '$start_date' and b.run_date <'$end_date'"; // } // $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, // b.base_price as base_price,d.back_commision_type as back_commision_type,d.back_commision_method as back_commision_method, // 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, // d.commision_flag as commision_flag // from run_hotel_distrib as a // inner join run_hotel as b on b.id=a.run_id // inner join opera_hotel c on c.hotel_id = b.hotel_id // inner join base_supplier_sale d on d.supplier_id =a.distrib_id // 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"; // // // writeLog('sql= ' . $sql); // $order_list = $this->DBTool->queryBySql($sql); // $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}'"; // $res=$this->DBTool->queryBySql($sqlt); // $data['order_list'] = $order_list['rowset']; // $data['stock_type']=$res['rowset']; // // return $data; // } function getRoomStock($hotel_id, $parent_room_type, $room_type, $org_id, $start_date, $end_date) { $sql = "CALL ht_get_room_stock({$hotel_id},{$parent_room_type},{$room_type},{$org_id},'{$start_date}','{$end_date}')"; writeLog(__FUNCTION__ . " sql= " . $sql); $rowset = $this->DBTool->execProcedure($sql); $data['code'] = $rowset['code']; $data['info'] = $rowset['info']; $data['order_list'] = $rowset['rowset']; return $data; } //获取已售卖的数量 public function getSaled($start_date, $end_date, $hotel_id, $room_type, $org_id, $para_stock_num = 0) { if ($start_date == $end_date) { $dateWhere = "b.run_date = '$start_date'"; } else { $dateWhere = "b.run_date >= '$start_date' and b.run_date <'$end_date'"; } $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 FROM run_hotel_distrib AS a INNER JOIN run_hotel AS b ON b.id = a.run_id WHERE a.distrib_id IN ($org_id) AND $dateWhere AND b.HOTEL_ID = $hotel_id AND b.room_type = $room_type and a.SALED_COUNT > $para_stock_num"; $order_list = $this->DBTool->queryBySql($sql); $data['order_list'] = $order_list['rowset']; return $data; } //最大数量必须大于数据库查询结果中最小的数字 function getMaxCount($start_date, $end_date, $hotel_id, $param_count) { $result = 0; if ($start_date == $end_date) { $dateWhere = "run_date = '$start_date'"; } else { $dateWhere = "run_date >= '$start_date' and run_date <'$end_date'"; } $sql = "SELECT sum(remaining_count) tot from run_hotel r where HOTEL_ID = $hotel_id AND $dateWhere AND STOCK_TYPE in (228,230) group by RUN_DATE"; $res = $this->DBTool->queryBySql($sql); if ($res['code'] == 0) { if (!empty($res['rowset'])) { // foreach ($res['rowset'] as $re) { if ($re['tot'] < $param_count) { $res['code'] = 1; $res['info'] = '固定数量不能大于可用的买断和保留房间总数!'; return $res; } } } } return $res; } function getMaxNumber($start_date, $end_date, $hotel_id, $param_count) { $result = 0; if ($start_date == $end_date) { $dateWhere = "run_date = '$start_date'"; } else { $dateWhere = "run_date >= '$start_date' and run_date <'$end_date'"; } $sql = "SELECT sum(remaining_count) tot from run_hotel r where HOTEL_ID = $hotel_id AND $dateWhere AND STOCK_TYPE in (228,230) group by RUN_DATE"; $res = $this->DBTool->queryBySql($sql); if ($res['code'] == 0) { if (!empty($res['rowset'])) { // foreach ($res['rowset'] as $re) { if ($re['tot'] < $param_count) { $res['code'] = 1; $res['info'] = '固定数量不能大于可用的买断和保留房间总数!'; return $res; } } } } return $res; } //获取渠道商已经占用的库存数量 function getUsedCount($start_date, $end_date, $hotel_id, $base_room, $param_count, $distrib_id, $total) { $result = 0; $count = mb_substr_count($distrib_id, ',') + 1; $param_count = $param_count * $count; if ($start_date == $end_date) { $dateWhere = "run_date = '$start_date'"; } else { $dateWhere = "run_date >= '$start_date' and run_date <'$end_date'"; } $sql = " SELECT sum(remaining_count) tot from run_hotel_distrib r INNER JOIN base_supplier s on r.DISTRIB_ID = s.ID and s.CANCEL_FLAG = 0 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 where r.HOTEL_ID = $hotel_id AND $dateWhere AND DISTRIB_ID not in($distrib_id) AND BASE_ROOM_TYPE = $base_room AND m.CANCEL_FLAG=0 group by RUN_DATE"; $res = $this->DBTool->queryBySql($sql); if ($res['code'] == 0) { if (!empty($res['rowset'])) { // foreach ($res['rowset'] as $re) { if ($re['tot'] + $param_count > $total) { $res['code'] = 1; $res['info'] = '渠道商固定数量总和不能大于可用的买断和保留房间总数!'; return $res; } } } } return $res; } /** * 录入订单 * @param $user_id * @param $hotel_id * @param $room_type * @param $org_id * @param $org_num * @param $room_list * @param $cus_list * @param $checkin_time * @param $if_cancel * @param $benefit * @param $total_commission * @param $order_id * @return mixed */ 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) { //处理room_list 单间佣金不超过小数点后2位 $tmp = substr($room_list, 1, -1); $tmp_arr = explode('}{', $tmp); $new_room_list = ''; foreach ($tmp_arr as $k => $v) { $list_info = explode(',', $v); $list_info[2] = round($list_info[2], 2); $list_info[3] = round($list_info[3], 2); $new_room_list .= '{' . $list_info[0] . ',' . $list_info[1] . ',' . $list_info[2] . ',' . $list_info[3] . '}'; } $flag = $order_id == 0 ? true : false; $today = date('Y-m-d H:i:s', time()); /* $data = ifNullParam(array($hotel_id, $room_type, $org_id, $org_num, $room_list, $cus_list, $checkin_time)); if ($data['code'] != "0") { return $data; }*/ $order_status = 313; //取消订单前判断是否为下线产品 $sql_is_onsale = "select is_onsale from opera_hotel_room where hotel_id= $hotel_id and room_type = $room_type and cancel_flag = 0"; $result_is_onsale = $this->DBTool->queryBySql($sql_is_onsale); if ($result_is_onsale['rowset'][0]['is_onsale'] != 1) { $data['code'] = 10086; $data['info'] = '该子房型已下线'; return $data; } //原订单号不为0,是修改订单,先取消订单,带修改标识(非真正取消订单) if ($order_id != 0) { //先判断订单的状态,如果是异常处理代发单,并且修改了影响库存的变量(渠道商,入住日期范围,房间数),那么需要执行异常处理代发单的取消逻辑 $sql = "select order_status from order_main where order_id=$order_id and CANCEL_FLAG=0;"; $res = $this->DBTool->queryBySql($sql); //获取当前订单的状态 if ($res['code'] != 0) { $result['code'] = '1'; $result['info'] = '订单数据拉取失败'; return $result; } $order_status = $res['rowset'][0]['order_status']; if ($order_status == 382) { //异常处理代发单状态 //查询当前订单的订单渠道、入住日期范围、房间数 $sql_orig = "select outside_sale_org_id,order_description from order_main where order_id=$order_id and CANCEL_FLAG=0;"; $res_orig = $this->DBTool->queryBySql($sql_orig); if ($res_orig['code'] == 0) { $res_info = $res_orig['rowset'][0]; $tmp = substr($res_info['order_description'], 0, -1); $tmp_arr = explode('|', $tmp); $new_str = ''; foreach ($tmp_arr as $k => $v) { $info_arr = explode(',', $v); $new_str .= $info_arr[1] . ',' . $info_arr[2] . '|'; } $res_info['order_description'] = $new_str; } $compare_new = ''; $room_list_str = substr($room_list, 1, -1); $room_list_arr = explode('}{', $room_list_str); foreach ($room_list_arr as $r_k => $r_v) { $info_list = explode(',', $r_v); $date = substr($info_list[0], 2, -2);//入住日期 $room_num = $info_list[1];//房间数 $compare_new .= $date . ',' . $room_num . '|'; } //判断影响库存的属性值是否被修改 if ($org_id != $res_info['outside_sale_org_id'] || $compare_new != $res_info['order_description']) { //说明影响库存的属性有更改,这是的取消订单相应的库存不进行释放 $sql_cancel = "CALL ht_cancel_order(" . $user_id . "," . $order_id . ", 3)"; $order_status = 313; writeLog(__FUNCTION__ . "有影响库存的属性更新,删除后不释放库存" . $sql_cancel); } else { $sql_cancel = "CALL ht_cancel_order(" . $user_id . "," . $order_id . ", 2)"; writeLog(__FUNCTION__ . "无影响库存的属性更新,正常释放库存" . $sql_cancel); } $rowset = $this->DBTool->execProcedure($sql_cancel); } else { //订单在未安排的情况下修改订单 $sql_cancel = "CALL ht_cancel_order(" . $user_id . "," . $order_id . ", 2)"; writeLog(__FUNCTION__ . "订单在未安排的情况下修改订单" . $sql_cancel); $rowset = $this->DBTool->execProcedure($sql_cancel); } if ($rowset['code'] != 0) {//修改订单时取消订单失败 return $rowset; } $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})"; } else { $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})"; } //执行下单前判断该房型是否为上线 if ($result_is_onsale['rowset'][0]['is_onsale'] == 1) { writeLog(__FUNCTION__ . " ht_make_order " . $sql); $rowset = $this->DBTool->execProcedure($sql); $data['code'] = $rowset['rowset'][0][0]['errcode']; $data['info'] = $rowset['rowset'][0][0]['errinfo']; } else { $data['code'] = 10086; $data['info'] = '该子房型已下线'; } //echo json_encode($rowset); if ($rowset['code'] == "0" && $flag) { $this->setMemcache($rowset['rowset'][0][0]['order_id'], 313); $model = array( "user_id" => $user_id, "log_type" => 3, "hotel_id" => $hotel_id, "room_type" => $room_type, "log_desc" => "创建订单号为" . $rowset['rowset'][0][0]['order_id'] . "的订单", "order_id" => $rowset['rowset'][0][0]['order_id'] ); $this->DBLog->insertLog($model, '创建订单'); //插入订单备注 /*$memo_list_tmp = explode('||',explode(',',substr($cus_list,1,-1))[2]); foreach ($memo_list_tmp as $k=>$v){ $memo_list[] = explode('|',$v); //0为操作人id,1为时间戳,2为备注类型,3为备注内容 } if ($memo_list[0][0]!=''){ $sql_memo = 'insert into order_comment(order_id,comment_type,comment_txt,create_user_id,update_user_id,create_time,update_time) values'; foreach ($memo_list as $k=>$v){ $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])."'),"; } $sql_memo = substr($sql_memo,0,-1); $res = $this->DBTool->queryBySql($sql_memo); }*/ } if ($order_id == 0 && $data['code'] == "0") { $data['code'] = $rowset['rowset'][0][0]['errcode']; $data['info'] = $rowset['rowset'][0][0]['errinfo']; $data['order_id'] = $rowset['rowset'][0][0]['order_id']; $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}')"; writeLog(__FUNCTION__ . " sql= " . $sql); $this->DBTool->execSql($sql); } // //出现了修改订单,取消成功但是修改不成功,则把订单的cancel_flag 给置回0 // if ($data['code'] != 0 && $order_id != 0) { // $sql = "update order_main set cancel_flag = 0 where order_id = $order_id or parent_order_id = $order_id"; // $this->DBTool->execSql($sql); // writeLog(__FUNCTION__ . "修改订单,取消成功但是修改不成功,则把订单的cancel_flag 给置回0, sql=" . $sql); // } return $data; } } //$obj = new MakeHotelOrder(); //$data = $obj ->getOrgList('2016-09-01','2016-09-30',3,1,41); ////$data = $obj ->makeOrderList(1,3,1,4,123,); //echo json_encode($data);