ROrderController.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. <?php
  2. class ROrderController extends AdminController{
  3. public function actionIndex()
  4. {
  5. $status_option = ROrder::$status_option;
  6. $status = CommonFn::getComboboxData($status_option, 100, true, 100);
  7. $channel_option = ROrder::$channel_option;
  8. $channels = CommonFn::getComboboxData($channel_option, 100, true, 100);
  9. // 服务点信息
  10. $criteria_station = new EMongoCriteria();
  11. $cursor = Station::model()->findAll($criteria_station);
  12. $rows = CommonFn::getRowsFromCursor($cursor);
  13. $parsedRows = Station::model()->parse($rows);
  14. $station_data = array();
  15. foreach ($parsedRows as $key => $v) {
  16. $station_data = array_merge($station_data, array($v['id'] => array('name' => $v['name'])));
  17. }
  18. $station = CommonFn::getComboboxData($station_data, 'all', true, 'all');
  19. // 订单类型
  20. $type_data = Yii::app()->params['o2o_service'];
  21. $type = CommonFn::getComboboxData($type_data, 100, true, 100);
  22. $this->render('index', array(
  23. 'status' => $status,
  24. 'channels' => $channels,
  25. 'station' => $station,
  26. 'type' => $type
  27. ));
  28. }
  29. public function actionList(){
  30. $filter_status = intval(Yii::app()->request->getParam('status', 100));
  31. $search = Yii::app()->request->getParam('search', '');
  32. $id = Yii::app()->request->getParam('id', '');
  33. $date_start_order = Yii::app()->request->getParam('date_start_order');
  34. $date_end_order = Yii::app()->request->getParam('date_end_order');
  35. $date_start_book= Yii::app()->request->getParam('date_start_book');
  36. $date_end_book = Yii::app()->request->getParam('date_end_book');
  37. $type = Yii::app()->request->getParam('type', 100);
  38. $have_pay = Yii::app()->request->getParam('have_pay', 0);
  39. $channel = Yii::app()->request->getParam('channel', 100);
  40. //$station = Yii::app()->request->getParam('station', 'all');
  41. $params = CommonFn::getPageParams();
  42. $criteria = new EMongoCriteria($params);
  43. if ($filter_status != 100){
  44. $criteria->status('==', $filter_status);
  45. }
  46. if ($id != ''){
  47. $order_id = new MongoId($id);
  48. $criteria->_id('==', $order_id);
  49. }
  50. if ($channel != 100){
  51. $criteria->channel('==', $channel);
  52. }
  53. if ($type != 100) {
  54. $criteria->type = $type;
  55. }
  56. if ($search != ''){
  57. if(CommonFn::isMongoId($search)){
  58. //$criteria->_id('==', new MongoId($search));
  59. $criteria->addCond('_id','or',new MongoId($search));
  60. $criteria->addCond('user','or',new MongoId($search));
  61. }
  62. else if(preg_match('/\d{8,11}/', $search)){
  63. $criteria->addCond('address.mobile','==',$search);
  64. if($have_pay){
  65. $criteria->addCond('status','>',0);
  66. }
  67. }else if(strlen($search) == 27){
  68. $criteria->charge_id('==', $search);
  69. }else{
  70. $criteria->addCond('address.name','==',$search);
  71. }
  72. }
  73. // 下单时间处理
  74. if (!empty($date_start_order) && !empty($date_end_order)) {
  75. // 开始时间处理
  76. $timestamp_start_order = strtotime($date_start_order);
  77. // 结束时间处理,需通过strototime()增加一天
  78. $timestamp_end_order = strtotime('+1 day', strtotime($date_end_order));
  79. $criteria->order_time('>=', $timestamp_start_order);
  80. $criteria->order_time('<=', $timestamp_end_order);
  81. }
  82. // 预约时间处理
  83. if (!empty($date_start_book) && !empty($date_end_book)) {
  84. // 开始时间处理
  85. $timestamp_start_book = strtotime($date_start_book);
  86. // 结束时间处理,需通过strototime()增加一天
  87. $timestamp_end_book = strtotime('+1 day', strtotime($date_end_book));
  88. $criteria->booking_time('>=', $timestamp_start_book);
  89. $criteria->booking_time('<=', $timestamp_end_book);
  90. }
  91. // 服务站处理
  92. // if ($station != 'all') {
  93. // $criteria->station = new MongoId($station);
  94. //}
  95. $cursor = ROrder::model()->findAll($criteria);
  96. $total = $cursor->count();
  97. $rows = CommonFn::getRowsFromCursor($cursor);
  98. $parsedRows = ROrder::model()->parse($rows);
  99. echo CommonFn::composeDatagridData($parsedRows, $total);
  100. }
  101. public function actionEdit(){
  102. $id = Yii::app()->request->getParam('id', '');
  103. $status = intval(Yii::app()->request->getParam('status',1));
  104. $booking_time = intval(Yii::app()->request->getParam('booking_time', time()));
  105. $deal_time = intval(Yii::app()->request->getParam('deal_time', time()));
  106. $remark = Yii::app()->request->getParam('remark', '');
  107. //$station = Yii::app()->request->getParam('station_id', '');
  108. $technician_id = intval(Yii::app()->request->getParam('technician', 0));
  109. $technician_name = Yii::app()->request->getParam('technician_name', '');
  110. // 保洁师信息检查
  111. // 根据ID直接查询保洁师信息(优先使用联想功能)
  112. if ($technician_id != 0) {
  113. $technician_obj = TechInfo::get($technician_id);
  114. if ($technician_obj) {
  115. $technicians_id = $technician_obj->_id;
  116. $technician_name = $technician_obj->name;
  117. } else {
  118. CommonFn::requestAjax(false, '保洁师不存在');
  119. }
  120. // ID为0时根据输入框信息查询
  121. } else if ($technician_name != '') {
  122. $criteria = new EMongoCriteria();
  123. $criteria->name = $technician_name;
  124. $technician_obj = TechInfo::model()->find($criteria);
  125. if ($technician_obj) {
  126. $technician_id = $technician_obj->_id;
  127. $technician_name = $technician_obj->name;
  128. } else {
  129. CommonFn::requestAjax(false, '保洁师不存在');
  130. }
  131. } else {
  132. $technician_obj = null;
  133. $technician_id = 0;
  134. $technician_name = '';
  135. }
  136. if($status == 100){
  137. CommonFn::requestAjax(false, '必须指定状态!');
  138. }
  139. $criteria = new EMongoCriteria();
  140. $criteria->_id = new MongoId($id);
  141. $order = ROrder::model()->find($criteria);
  142. // if($order->status == -1 || $order->status == -2){
  143. // CommonFn::requestAjax(false, '已取消,已退款订单不支持更改');
  144. // }
  145. // 获取用户信息,修改用户订单统计
  146. // zhouxuchen 2015-11-16
  147. $user_id = $order->user;
  148. if (!empty($user_id)) {
  149. $user = RUser::get($user_id);
  150. } else {
  151. $user = '';
  152. }
  153. if (empty($order)){
  154. CommonFn::requestAjax(false, '订单不存在');
  155. }
  156. if(($order->status!=-1||$order->status!=-2)&&($status==-1||$status==-2)){
  157. foreach ($order->coupons as $user_coupon) {
  158. $user_coupon = UserCoupon::get($user_coupon);
  159. $user_coupon->status = 1;
  160. $user_coupon->update(array('status'),true);
  161. }
  162. }
  163. if($order->status!=-2 && $status == -2){
  164. $order->refund_time = time();
  165. $order_info = $order->parseRow($order);
  166. $month = date('m',$order_info['booking_time']);
  167. $day = date('d',$order_info['booking_time']);
  168. $address = $order_info['address']['poi']['name'].$order_info['address']['detail'];
  169. CommonSMS::send('order_retrieve',array('month'=>$month,'day'=>$day,'address'=>$address,'mobile'=>$order_info['address']['mobile']));
  170. // 申请退款处理完成后通知保洁师
  171. if ($technician_obj && $technician_obj->weixin_userid) {
  172. $url_prefix = ENVIRONMENT == 'product' ? 'http://api.wozhua.mobi' : 'http://apitest.wozhua.mobi';
  173. $wechat = O2oApp::getWechatActive();
  174. $wechat_data = array(
  175. 'touser' => $technician_obj->weixin_userid,
  176. 'msgtype' => 'news',
  177. 'agentid' => '24',
  178. 'news' => array(
  179. 'articles' => array(
  180. array(
  181. 'title' => '壹管家提示-订单退款完成',
  182. 'description' => $technician_obj->name.'你好!用户于'.date('m月d日H:i', $order->apply_refund_time).'申请退款的订单已处理完成。',
  183. 'url' => $url_prefix.'/index.php?r=o2o/myOrder/info&order='.$id.'&user='.$technician_id,
  184. ),
  185. ),
  186. ),
  187. );
  188. if (!empty($order->append_orders)) {
  189. $count = count($order->append_orders);
  190. $wechat_data['news']['articles'][0]['description'] .= "\n\n本订单包含".$count."个追加订单,请注意查看。";
  191. }
  192. $wechat->sendMessage($wechat_data);
  193. }
  194. // if (!empty($order->append_orders)) {
  195. // $wechat_data = array(
  196. // 'touser' => $technician_obj->userid,
  197. // 'msgtype' => 'news',
  198. // 'agentid' => '24',
  199. // 'news' => array(
  200. // 'articles' => array(
  201. // array(
  202. // 'title' => '壹管家提示-追加订单退款完成',
  203. // 'description' => $technician_obj->name.'你好!用户于'.date('m月d日H:i', $order->apply_refund_time).'申请退款的追加订单已处理完成,请点击下方条目查看详情。',
  204. // ),
  205. // ),
  206. // ),
  207. // );
  208. // foreach ($order->append_orders as $key => $value) {
  209. // $wechat_data['news']['articles'][] = array(
  210. // 'description' => '追加订单' . ($key + 1),
  211. // 'url' => 'http://api.wozhua.mobi/index.php?r=o2o/myOrder/info&order='.$id.'&user='.$technician_id,
  212. // );
  213. // }
  214. // }
  215. }
  216. //修改点评订单预约时间
  217. if($order->channel == 'dianping' && $order->booking_time != $booking_time){
  218. $update_dianping = ROrder::updateOrderInfo($order->_id,array('serviceTime' => date('Y-m-d H:i:s',$booking_time)));
  219. if($update_dianping == false){
  220. CommonFn::requestAjax(false, '更新点评到家数据失败');
  221. }
  222. }
  223. //取消订单
  224. if($order->status!=-1 && $status == -1){
  225. // 已完成订单不能取消
  226. if ($order->status == 6) {
  227. CommonFn::requestAjax(false, '已完成订单不可取消');
  228. }
  229. $order->cancel_time = time();
  230. $order_info = $order->parseRow($order);
  231. if($order->channel == 'dianping'){
  232. $update_dianping = ROrder::cancelDianPingOrder($order->_id);
  233. if($update_dianping == false){
  234. CommonFn::requestAjax(false, '更新点评到家数据失败');
  235. }
  236. // 客服取消订单,用户有效订单数减少
  237. // zhouxuchen 2015-11-16
  238. if (!empty($user) && $user->order_count > 0 && $order->status > 0) {
  239. $user->order_count -= 1;
  240. $user->save();
  241. }
  242. }
  243. }
  244. //确认接单
  245. if($order->status!=3 && $status == 3){
  246. if($order->channel == 'dianping'){
  247. $update_dianping = ROrder::setDianPingStatus($order->_id,13);
  248. if($update_dianping == false){
  249. CommonFn::requestAjax(false, '更新点评到家数据失败');
  250. }
  251. }
  252. $order_info = $order->parseRow($order);
  253. }
  254. //订单完成后执行
  255. if($order->status!=6 && $status == 6 ){
  256. $order->finish_time = time();
  257. if($order->channel == 'dianping'){
  258. if($order->status == -1){
  259. CommonFn::requestAjax(false, '已取消大众点评订单,不可设置为已完成状态');
  260. }
  261. $update_dianping = ROrder::setDianPingStatus($order->_id,5);
  262. if($update_dianping == false){
  263. // CommonFn::requestAjax(false, '更新点评到家数据失败');
  264. }
  265. }
  266. $order_info = $order->parseRow($order);
  267. if($order->channel == 'dianping'){
  268. CommonSMS::send('dianping_final_order',array('mobile'=>$order_info['address']['mobile']));
  269. }else{
  270. $result = Service::factory('ScoreService')->changeScore((string)$order->user,intval($order->final_price),'下单奖爪币');
  271. if($result){
  272. $z_message = new ZMessage();
  273. $from_user = Yii::app()->params['sys_user'];
  274. $message_data = array(
  275. 'from_user' => $from_user,
  276. 'to_user' => (string)$order->user,
  277. 'content' => '您成功下单,获得了'.intval($order->final_price).'个爪币的奖励。',
  278. 'pics' => array(),
  279. 'voice' => array(),
  280. 'video'=> array()
  281. );
  282. $z_message->addMessage($message_data);
  283. CommonSMS::send('final_order',array('name' =>$order_info['address']['name'],'num' =>intval($order->final_price),'mobile'=>$order_info['address']['mobile']));
  284. }
  285. }
  286. // 保洁师订单统计处理
  287. if ($technician_obj) {
  288. $tech_order_count = $technician_obj->order_count + 1;
  289. $technician_obj->order_count = $tech_order_count;
  290. $technician_obj->save();
  291. }
  292. }
  293. // 葡萄订单状态处理
  294. if ($order->channel == 'putao' && $status != $order->status) {
  295. if (in_array($status, array(0, 1, 2, 7, -2))) {
  296. CommonFn::requestAjax(false, '该选项状态不适用于葡萄生活', array());
  297. }
  298. $update_putao = Putao::getInstance()->updateToPlatform($order->_id, $status);
  299. if ($update_putao['code'] == -1) {
  300. CommonFn::requestAjax(false, '更新葡萄生活数据失败/'.$update_putao['msg'], array());
  301. } else {
  302. $remark_info = $update_putao['msg'];
  303. $remark = $remark == '' ? $remark : $remark."\n";
  304. $remark .= $remark_info."\n".date('Y-m-d H:i:s', time());
  305. }
  306. }
  307. // 触宝订单状态处理
  308. // TODO
  309. if ($order->channel == 'chubao' && $status != $order->status) {
  310. $chubao_info = $order->third_platform_info;
  311. if (empty($user->name) || !isset($chubao_info['transactionId'])) {
  312. CommonFn::requestAjax(false, '触宝用户信息不全');
  313. } else {
  314. $userId = $user->name;
  315. }
  316. if (in_array($status, array(-1, 6))) {
  317. $chubao = new Chubao();
  318. $chubaoClient = $chubao->getClient();
  319. $chubaoConfig = Yii::app()->params['chubao'];
  320. // 服务信息
  321. $product_list = $order->products;
  322. $product_id = $product_list[0]['product'];
  323. $product = Product::get($product_id);
  324. $o2o_service = Yii::app()->params['o2o_service'];
  325. $shortInfo = $o2o_service[$product->type]['name'];
  326. // chubaoOrderPush实例化及基本设置
  327. $chubaoPush = $chubao->getOrderPush();
  328. $chubaoPush->setUserId($userId);
  329. $chubaoPush->setOrderId($id);
  330. $chubaoPush->setOrderService($chubaoConfig['service']);
  331. $chubaoPush->setOrderTitle('壹管家宠物');
  332. $chubaoPush->setOrderShortInfo('壹管家上门服务-'.$shortInfo);
  333. $chubaoPush->setOrderCreateTime(date('YmdHis', $order->order_time));
  334. $chubaoPush->setOrderFinishTime('');
  335. $chubaoPush->setOrderUrl($chubaoConfig['orderUrl'].'/'.$id);
  336. // 取消订单
  337. if ($status == -1) {
  338. // 已付款情况下申请退款
  339. if (in_array($order->status, array(1,2,3,4,5))) {
  340. $chubaoRefund = $chubao->getTradeRefund();
  341. $chubaoRefund->setUserId($userId);
  342. $chubaoRefund->setNotifyUrl($chubaoConfig['refundCallbackUrl']);
  343. $chubaoRefund->setTransactionId($chubao_info['transactionId']);
  344. $chubaoRefund->setRefundNo($id);
  345. $chubaoRefund->setTotalFee($order->final_price * 100);
  346. $chubaoRefund->setCashFee($order->final_price * 100);
  347. $chubaoRefund->setRefundFee($order->final_price * 100);
  348. $refundRes = $chubaoClient->execute($chubaoRefund);
  349. if ($refundRes['refundStatus'] == 'REFUND_FAIL') {
  350. CommonFn::requestAjax(false, '触宝订单退款失败');
  351. }
  352. }
  353. // 取消订单
  354. $chubaoPush->setOrderStatus('订单已被客服取消');
  355. $chubaoPush->setTradeStatus('TRADE_CLOSED');
  356. $pushRes = $chubaoClient->execute($chubaoPush);
  357. if ($pushRes['result'] == 'FAIL') {
  358. CommonFn::requestAjax(false, '触宝订单取消失败');
  359. }
  360. // 确认订单
  361. } else if ($status == 6) {
  362. $chubaoPush->setOrderStatus('订单已被客服确认完成');
  363. $chubaoPush->setTradeStatus('TRADE_FINISH');
  364. $pushRes = $chubaoClient->execute($chubaoPush);
  365. if ($pushRes['result'] == 'FAIL') {
  366. CommonFn::requestAjax(false, '触宝订单确认失败');
  367. }
  368. }
  369. }
  370. }
  371. $order->status = $status;
  372. $order->booking_time = $booking_time;
  373. $order->deal_time = $deal_time;
  374. $order->remark = $remark;
  375. //$order->station = new MongoId($station);
  376. // 是否通知保洁师
  377. $toTech = $order->technician != $technician_id ? true : false;
  378. $order->technician = $technician_id;
  379. $order->technician_name = $technician_name;
  380. $arr_order = array('cancel_time','refund_time','finish_time','status','booking_time','deal_time','remark', 'station', 'technician', 'technician_name');
  381. $success = $order->save(true,$arr_order);
  382. // 通知保洁师
  383. if (in_array($status, array(1,2,3,4,5)) && $toTech && $success) {
  384. // if ($toTech) {
  385. if ($technician_obj && $technician_obj->weixin_userid) {
  386. $url_prefix = ENVIRONMENT == 'product' ? 'http://api.wozhua.mobi' : 'http://apitest.wozhua.mobi';
  387. $wechat = O2oApp::getWechatActive();
  388. $wechat_data = array(
  389. 'touser' => $technician_obj->weixin_userid,
  390. 'msgtype' => 'news',
  391. 'agentid' => '24',
  392. 'news' => array(
  393. 'articles' => array(
  394. array(
  395. 'title' => '壹管家提示-新订单',
  396. 'description' => $technician_obj->name.'你好!刚刚有一个新的订单被分配给你,请点击查看。',
  397. 'url' => $url_prefix.'/index.php?r=o2o/myOrder/info&order='.$id.'&user='.$technician_id,
  398. ),
  399. ),
  400. ),
  401. );
  402. $wechat->sendMessage($wechat_data);
  403. }
  404. }
  405. CommonFn::requestAjax($success, '', array());
  406. }
  407. public function actionGetDianPingInfo(){
  408. $id = Yii::app()->request->getParam('id', '');
  409. $post_args = array(
  410. 'methodName' => 'loadOrderInfo',
  411. 'orderId' => $id
  412. );
  413. $sign = CommonFn::getDianPingSing($post_args);
  414. if($sign === false){
  415. return false;
  416. }
  417. $post_args['sign'] = $sign;
  418. $ch = curl_init();
  419. curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  420. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  421. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  422. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  423. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  424. curl_setopt($ch, CURLOPT_URL, 'http://m.api.51ping.com/tohome/openapi/wozhua/');
  425. curl_setopt($ch, CURLOPT_POSTFIELDS,$post_args);
  426. $result = curl_exec($ch);
  427. if($result === false) {
  428. return false;
  429. }
  430. curl_close($ch);
  431. echo $result;
  432. die();
  433. }
  434. // 根据手机号 获取对应用户的地址信息
  435. public function actionGetUserInfo(){
  436. $mobile = Yii::app()->request->getParam('mobile', '');
  437. $length = strlen($mobile);
  438. if ($length < 11) {
  439. $result[] = array(
  440. 'id' => 0,
  441. 'data' => '',
  442. 'description' => '请继续输入',
  443. 'content' => array()
  444. );
  445. echo json_encode($result);exit;
  446. }
  447. // $mobile_regex = new MongoRegex($mobile);
  448. $criteria = new EMongoCriteria();
  449. $criteria->sort('order_time', EMongoCriteria::SORT_DESC);
  450. $criteria->addCond('address.mobile', '==', $mobile);
  451. $cursor = ROrder::model()->findAll($criteria);
  452. $rows = CommonFn::getRowsFromCursor($cursor);
  453. if (empty($rows)) {
  454. $result[] = array(
  455. 'id' => 0,
  456. 'data' => '',
  457. 'description' => '没有匹配信息',
  458. 'content' => array()
  459. );
  460. echo json_encode($result);exit;
  461. }
  462. $result = array();
  463. $details = array();
  464. $index = 0;
  465. foreach($rows as $row){
  466. // 加入根据detail筛选,避免产生重复信息
  467. if (in_array($row['address']['detail'], $details)) continue;
  468. $details[] = $row['address']['detail'];
  469. $pio = isset($row['address']['poi']['name'])?$row['address']['poi']['name']:'';
  470. // 坐标获取(根据不同的来源)
  471. if ($row['channel'] == 'dianping') {
  472. $latitude = $row['address']['position']['lat'];
  473. $longitude = $row['address']['position']['lng'];
  474. } else {
  475. $latitude = isset($row['address']['position']['1'])?$row['address']['position']['1']:'';
  476. $longitude = isset($row['address']['position']['0'])?$row['address']['position']['0']:'';
  477. }
  478. // poi获取
  479. $poi_name = isset($row['address']['poi']['name']) ? $row['address']['poi']['name'] : '';
  480. $poi_uid = isset($row['address']['poi']['uid']) ? $row['address']['poi']['uid'] : '';
  481. $result[] = array(
  482. 'id' => $index,
  483. 'data' => $row['address']['mobile'],
  484. 'description' => $row['address']['province'].' '.$row['address']['city'].' '.$row['address']['area'].' '.$pio.$row['address']['detail'],
  485. 'content' => array(
  486. 'name' => $row['address']['name'],
  487. 'latitude' => $latitude,
  488. 'longitude' => $longitude,
  489. 'province' => $row['address']['province'],
  490. 'city' => $row['address']['city'],
  491. 'area' => $row['address']['area'],
  492. 'detail' => $row['address']['detail'],
  493. 'poi_name' => $poi_name,
  494. 'poi_uid' => $poi_uid,
  495. 'memo' => $row['memo']
  496. )
  497. );
  498. $index++;
  499. }
  500. echo json_encode($result);exit;
  501. /*$map = new MongoCode('
  502. function(){
  503. emit(this.address.name,this.address);
  504. }
  505. ');
  506. $reduce = new MongoCode('
  507. function(key,values){
  508. var ret={age:key,names:values[0]};
  509. return ret;
  510. }
  511. ');
  512. $query = array('address.mobile' => array('=' => $mobile));
  513. $instance = Message::model()->getDb();
  514. $cmd = $instance->command(array(
  515. 'mapreduce' => 'orders',
  516. 'map' => $map,
  517. 'reduce' => $reduce,
  518. 'query' => $query,
  519. 'out' => array('merge' => 'admin_message')
  520. ));*/
  521. /**
  522. * note time : 2015-10-16
  523. * noted by : zhouxuchen
  524. */
  525. /*$mongo = new MongoClient(DB_CONNETC);
  526. $pipeline = array(
  527. array(
  528. '$match' => array(
  529. '_id' => array('address.mobile' => $mobile)
  530. ),
  531. '$group' => array(
  532. 'mobile' => array('state' => '$state' ),
  533. 'user' => array('$first' => '$pop' )
  534. )
  535. )
  536. );
  537. $out = $mongo->wozhua_o2o->orders->aggregate($pipeline);
  538. var_dump($out);exit;*/
  539. }
  540. public function actionAdd () {
  541. // ------ 必须传入的数值 ------
  542. // --------- 订单信息 ---------
  543. $channel = Yii::app()->request->getParam('channel', '');
  544. $booking_time = Yii::app()->request->getParam('booking_time_add', '');
  545. $order_time = Yii::app()->request->getParam('order_time_add', '');
  546. $price = Yii::app()->request->getParam('price', 0);
  547. $final_price = Yii::app()->request->getParam('final_price', 0);
  548. $status = Yii::app()->request->getParam('status', -3);
  549. $station = Yii::app()->request->getParam('station', '5548b05e0eb9fbc5728b51ea');
  550. // --------- 地址信息 ---------
  551. $mobile = Yii::app()->request->getParam('mobile', 0);
  552. $latitude = Yii::app()->request->getParam('latitude', 0);
  553. $longitude = Yii::app()->request->getParam('longitude', 0);
  554. $province = Yii::app()->request->getParam('province', '');
  555. $city = Yii::app()->request->getParam('city', '');
  556. $area = Yii::app()->request->getParam('area', '');
  557. $poi_name = Yii::app()->request->getParam('poi_name', '');
  558. $detail = Yii::app()->request->getParam('detail', '');
  559. // 数据完整性检查
  560. // 2015-11-02 因存在赠送订单,删除金额的数据检查 : $price == 0 || $final_price == 0 ||
  561. // 2015-11-16 取消服务点录入,删除服务点数据检查 : || empty($station)
  562. $flag = empty($channel) || empty($booking_time) || empty($order_time);
  563. $flag = $flag || $status == -3;
  564. $flag = $flag || $mobile == 0 || $latitude == 0 || $longitude == 0;
  565. $flag = $flag || empty($province) || empty($city) || empty($area) || empty($poi_name) || empty($detail);
  566. if ($flag) {
  567. CommonFn::requestAjax(false, '请检查数据完整性', array());
  568. exit;
  569. }
  570. //if($channel == 'wz_app' || $channel == 'wx_pub'){
  571. //CommonFn::requestAjax(false, '不能录入渠道为壹管家微信||壹管家APP的订单', array());
  572. //exit;
  573. //}
  574. // 时间处理
  575. $booking_time = strtotime($booking_time);
  576. $order_time = strtotime($order_time);
  577. // ------ 可以留空的数值 ------
  578. $box = Yii::app()->request->getParam('box', array());
  579. $coupons = Yii::app()->request->getParam('coupons', array());
  580. $memo = Yii::app()->request->getParam('memo', '');
  581. $remark = Yii::app()->request->getParam('remark', '');
  582. $precedence = Yii::app()->request->getParam('precedence', 0);
  583. $have_comment = Yii::app()->request->getParam('have_comment', 0);
  584. $name = Yii::app()->request->getParam('name', '');
  585. $type = Yii::app()->request->getParam('type', 0);
  586. $user = Yii::app()->request->getParam('user', '');
  587. $poi_uid = Yii::app()->request->getParam('poi_uid', '');
  588. // 用户名的判断
  589. $channel_option = ROrder::$channel_option;
  590. $name = empty($name) ? $channel_option[$channel]['name'].'用户' : $name;
  591. // 支付渠道
  592. $pay_channel = $channel;
  593. // 地址数据整合
  594. $address = array(
  595. 'province' => $province,
  596. 'city' => $city,
  597. 'area' => $area,
  598. 'detail' => $detail,
  599. 'mobile' => $mobile,
  600. 'position' => array(
  601. // 'lat' => (float)$latitude,
  602. // 'lng' => (float)$longitude,
  603. 0 => (float)$longitude,
  604. 1 => (float)$latitude
  605. ),
  606. 'poi' => array(
  607. 'name' => $poi_name,
  608. 'uid' => $poi_uid
  609. ),
  610. 'name' => $name
  611. );
  612. // 订单类型判断
  613. if ($type == 0) {
  614. $criteria = new EMongoCriteria();
  615. $criteria->_id = new MongoId($main_products);
  616. $cursor = Product::model()->find($criteria);
  617. $type = $cursor->type;
  618. }
  619. $rOrder = new ROrder();
  620. $rOrder->channel = $channel;
  621. $rOrder->booking_time = intval($booking_time);
  622. $rOrder->order_time = intval($order_time);
  623. $rOrder->price = intval($price);
  624. $rOrder->final_price = intval($final_price);
  625. $rOrder->precedence = intval($precedence);
  626. $rOrder->coupons = $coupons;
  627. $rOrder->user = $user;
  628. $rOrder->status = intval($status);
  629. $rOrder->memo = $memo;
  630. $rOrder->remark = $remark;
  631. $rOrder->type = strval($type); // 数据库内使用string类型
  632. $rOrder->have_comment = intval($have_comment);
  633. $rOrder->station = new MongoId($station);
  634. $rOrder->address = $address;
  635. $rOrder->pay_channel = $pay_channel;
  636. $addROrder_arr = array('channel', 'booking_time', 'order_time', 'price', 'final_price', 'precedence', 'coupons', 'user', 'status', 'memo', 'remark', 'type', 'have_comment', 'station', 'address', 'pay_channel');
  637. $success = $rOrder->save(true, $addROrder_arr);
  638. CommonFn::requestAjax($success, '', array());
  639. }
  640. /**
  641. * 请求申请退款订单接口
  642. */
  643. public function actionCheckRefundOrder() {
  644. $criteria = new EMongoCriteria();
  645. $criteria->status('==', -3);
  646. $cursor = ROrder::model()->findAll($criteria);
  647. $count = $cursor->count();
  648. if ($count > 0) {
  649. $data = array('code' => 1, 'count' => $count);
  650. } else {
  651. $data = array('code' => 0, 'count' => $count);
  652. }
  653. $list = new ARedisList('append_order_list');
  654. if($list->getCount() > 0){
  655. $key = $list->shift();
  656. $list->unshift($key);
  657. $data['procession_append_order_id'] = $key;
  658. $data['code'] = 2;
  659. }
  660. echo json_encode($data);
  661. }
  662. public function actionCancelProcess() {
  663. $orderid = Yii::app()->request->getParam('orderid','');
  664. $list = new ARedisList('append_order_list');
  665. $key = $list->shift();
  666. }
  667. /**
  668. * 重新选择保洁师接口
  669. */
  670. public function actionResetTech() {
  671. $id = Yii::app()->request->getParam('id', '');
  672. $technician_id = intval(Yii::app()->request->getParam('reset_technician', 0));
  673. $technician_name = Yii::app()->request->getParam('reset_technician_name', '');
  674. // 保洁师信息检查
  675. // 根据ID直接查询保洁师信息(优先使用联想功能)
  676. if ($technician_id != 0) {
  677. $technician_obj = TechInfo::get($technician_id);
  678. if ($technician_obj) {
  679. $technician_id = $technician_obj->_id;
  680. $technician_name = $technician_obj->name;
  681. } else {
  682. CommonFn::requestAjax(false, '保洁师不存在');
  683. }
  684. // ID为0时根据输入框信息查询
  685. } else if ($technician_name != '') {
  686. $criteria = new EMongoCriteria();
  687. $criteria->name = $technician_name;
  688. $technician_obj = TechInfo::model()->find($criteria);
  689. if ($technician_obj) {
  690. $technician_id = $technician_obj->_id;
  691. $technician_name = $technician_obj->name;
  692. } else {
  693. CommonFn::requestAjax(false, '保洁师不存在');
  694. }
  695. } else {
  696. CommonFn::requestAjax(false, '保洁师姓名不能为空');
  697. }
  698. $orderid = new MongoId($id);
  699. $order = ROrder::model()->get($orderid);
  700. $toTech = $order->technician != $technician_id ? true : false;
  701. $fromTech = $order->technician;
  702. $order->technician = $technician_id;
  703. $order->technician_name = $technician_name;
  704. $success = $order->save(true, array('technician', 'technician_name'));
  705. if ($toTech && $success) {
  706. // 发送给被分配保洁师
  707. $wechat = O2oApp::getWechatActive();
  708. $url_prefix = ENVIRONMENT == 'product' ? 'http://api.wozhua.mobi' : 'http://apitest.wozhua.mobi';
  709. if (!empty($technician_obj->weixin_userid)) {
  710. $wechat_data = array(
  711. 'touser' => $technician_obj->weixin_userid,
  712. 'msgtype' => 'news',
  713. 'agentid' => '24',
  714. 'news' => array(
  715. 'articles' => array(
  716. array(
  717. 'title' => '壹管家提示-新订单',
  718. 'description' => $technician_obj->name.'你好!刚刚有一个新的订单被分配给你,请点击查看。',
  719. 'url' => $url_prefix.'/index.php?r=o2o/myOrder/info&order='.$id.'&user='.$technician_id,
  720. ),
  721. ),
  722. ),
  723. );
  724. $wechat->sendMessage($wechat_data);
  725. }
  726. // 发送给原保洁师
  727. $fromTechObj = TechInfo::get($fromTech);
  728. if (!empty($fromTechObj) && !empty($fromTechObj->weixin_userid)) {
  729. $wechat_data = array(
  730. 'touser' => $fromTechObj->weixin_userid,
  731. 'msgtype' => 'news',
  732. 'agentid' => '24',
  733. 'news' => array(
  734. 'articles' => array(
  735. array(
  736. 'title' => '壹管家提示-订单已被重新分配',
  737. 'description' => $fromTechObj->name.'你好!预定时间在'.date('m月d日H:i', $order->booking_time).'的订单已被分配给其他保洁师。',
  738. 'url' => $url_prefix.'/index.php?r=o2o/myOrder/info&order='.$id.'&user='.$technician_id,
  739. ),
  740. ),
  741. ),
  742. );
  743. $wechat->sendMessage($wechat_data);
  744. }
  745. }
  746. CommonFn::requestAjax($success, '', array());
  747. }
  748. }