OrderController.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  1. <?php
  2. /**
  3. * OrderController o2o下单相关接口
  4. *
  5. * @author leeon<leeon_on@qq.com>
  6. *
  7. */
  8. class OrderController extends O2oBaseController{
  9. public function actionAdd(){
  10. $user_id = Yii::app()->getRequest()->getParam("user_id");
  11. $products = json_decode(Yii::app()->getRequest()->getParam("products","[]"),true);
  12. $memo = Yii::app()->getRequest()->getParam("memo","");
  13. $booking_time = Yii::app()->getRequest()->getParam("booking_time",'');
  14. $precedence = intval(Yii::app()->getRequest()->getParam("precedence",0));
  15. $coupons = json_decode(Yii::app()->getRequest()->getParam("coupons","[]"),true);
  16. $address_id = Yii::app()->getRequest()->getParam("address_id");
  17. $tech_id = Yii::app()->getRequest()->getParam("tech_id");
  18. $station = Yii::app()->getRequest()->getParam("station");
  19. $order_channel = Yii::app()->getRequest()->getParam("order_channel");
  20. CommonFn::requestAjax(false,'系统升级中,暂时不能下单');
  21. if(!$precedence){
  22. $booking_check = strtotime($booking_time);
  23. $verity_check = $booking_check - time();
  24. if($verity_check < 13800){
  25. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  26. }
  27. }
  28. if(!$user_id||!$products||!$address_id){
  29. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  30. }
  31. $user_obj = CommonFn::apigetObJ($user_id,'ZUser',CommonFn::getMessage('user','id_not_exist'),array(),201);
  32. $address_list = $user_obj->shop_address?$user_obj->shop_address:array();
  33. $address = array();
  34. foreach ($address_list as $value) {
  35. if($address_id == $value['address_id']){
  36. $address = $value;
  37. }
  38. }
  39. if(!$address){
  40. file_put_contents('/data/erroraddressc.log',var_export($address,true),FILE_APPEND);
  41. CommonFn::requestAjax(false,CommonFn::getMessage('shop','address_false'));
  42. }else{
  43. if($station){
  44. $station = Station::get(new MongoId($station));
  45. if(!$station){
  46. file_put_contents('/data/erroraddressa.log',var_export($address,true),FILE_APPEND);
  47. CommonFn::requestAjax(false,CommonFn::getMessage('o2o','address_cannot_service'));
  48. }
  49. $divide_station = $station->_id;
  50. }else{
  51. file_put_contents('/data/erroraddressb.log',var_export($address,true),FILE_APPEND);
  52. CommonFn::requestAjax(false,CommonFn::getMessage('o2o','address_cannot_service'));
  53. }
  54. $o2o_address['name'] = $address['name'];
  55. $o2o_address['mobile'] = $address['mobile'];
  56. $o2o_address['province'] = $address['address']['province'];
  57. $o2o_address['city'] = $address['address']['city'];
  58. $o2o_address['area'] = $address['address']['area'];
  59. $o2o_address['detail'] = $address['address']['detail'];
  60. $o2o_address['position'] = isset($address['position'])?$address['position']:array(0,0);
  61. $o2o_address['poi'] = isset($address['address']['poi'])?$address['address']['poi']:array();
  62. }
  63. $product_list = array();
  64. $price = 0;
  65. $service_type = 0;
  66. foreach ($products as $product) {
  67. if(isset($product['product_id']) && $product['count'] >= 1){
  68. $product_temp = array();
  69. $product_obj = Product::get(new MongoId($product['product_id']));
  70. if($product_obj){
  71. $price += ($product_obj->price*$product['count']);
  72. $product_temp['product'] = $product_obj->_id;
  73. $product_temp['count'] = $product['count'];
  74. $product_list[] = $product_temp;
  75. if($product_obj->is_extra == 0){
  76. $service_type = $product_obj->type;
  77. }
  78. }else{
  79. CommonFn::requestAjax(false,CommonFn::getMessage('o2o','product_not_exist'));
  80. }
  81. }else{
  82. CommonFn::requestAjax(false,CommonFn::getMessage('o2o','product_illegal'));
  83. }
  84. }
  85. if($precedence){
  86. $price += 40;
  87. }
  88. $final_price = $price;
  89. $used_coupon = array();
  90. foreach ($coupons as $coupon){
  91. $user_coupon = UserCoupon::get(new MongoId($coupon));
  92. $current_time = time();
  93. if($user_coupon && (string)$user_coupon->user == $user_id && $user_coupon->start_time < $current_time && $user_coupon->end_time > $current_time && $user_coupon->status == 1){
  94. $coupon = Coupon::get($user_coupon->coupon);
  95. //判断优惠券可用时间
  96. $weekend_check = (date('w',strtotime($booking_time))==0 || date('w',strtotime($booking_time))==6)?1:0;
  97. $booking_hour = date('H',strtotime($booking_time));
  98. if($weekend_check){
  99. if($coupon->workday_limit==1){
  100. CommonFn::requestAjax(false,CommonFn::getMessage('o2o','coupon_unuseable'));
  101. }
  102. }else{
  103. if($coupon->workday_limit==2){
  104. CommonFn::requestAjax(false,CommonFn::getMessage('o2o','coupon_unuseable'));
  105. }
  106. }
  107. if($coupon->time_limit_start && $coupon->time_limit_end && ($coupon->time_limit_start > $booking_hour || $coupon->time_limit_end < $booking_hour)){
  108. CommonFn::requestAjax(false,CommonFn::getMessage('o2o','coupon_unuseable'));
  109. }
  110. $new_user_coupons = Yii::app()->params['new_user_coupons'];
  111. if(($coupon->type == 0 || $coupon->type == $service_type ) && $coupon->status==1 && $coupon->min_price <= $final_price){
  112. $final_price = $final_price-$coupon->value>=0?$final_price-$coupon->value:0;
  113. $used_coupon[] = $user_coupon->_id;
  114. $user_coupon->status = -1;
  115. $user_coupon->use_time = time();
  116. $user_coupon->update(array('status','use_time'),true);
  117. }else{
  118. CommonFn::requestAjax(false,CommonFn::getMessage('o2o','coupon_unuseable'));
  119. }
  120. }else{
  121. CommonFn::requestAjax(false,CommonFn::getMessage('o2o','coupon_unuseable'));
  122. }
  123. }
  124. $order = new ROrder();
  125. $order->order_time = time();
  126. $order->booking_time = strtotime($booking_time);
  127. $order->products = $product_list;
  128. $order->precedence = $precedence;
  129. $order->price = $price;
  130. $order->channel = $order_channel;
  131. $order->final_price = ceil($final_price*100)/100;
  132. $order->address = $o2o_address;
  133. $order->memo = $memo;
  134. $order->station = $divide_station;
  135. $order->coupons = $used_coupon;
  136. if(isset($user_coupon->coupon)){
  137. $order->coupon_type = $user_coupon->coupon;
  138. }
  139. if($order->final_price == 0){
  140. $order->status = 1;
  141. }else{
  142. $order->status = 0;
  143. }
  144. $order->user = $user_obj->_id;
  145. $order->type = $service_type;
  146. $order->technician = intval($tech_id);
  147. if($order->save()){
  148. $data = ROrder::model()->parseRow($order);
  149. foreach ($products as $product) {
  150. if(isset($product['product_id']) && $product['count'] >= 1){
  151. $product_obj = Product::get(new MongoId($product['product_id']));
  152. if($product_obj){
  153. $product_obj->sale_count+=$product['count'];
  154. $product_obj->update(array('sale_count'),true);
  155. }
  156. }
  157. }
  158. CommonFn::requestAjax(true,CommonFn::getMessage('message','operation_success'),$data);
  159. }
  160. }
  161. public function actionList(){
  162. $user_id = Yii::app()->getRequest()->getParam("user_id");
  163. $type = Yii::app()->getRequest()->getParam("type");
  164. $order_type = Yii::app()->getRequest()->getParam("order_type");
  165. if(!$order_type){
  166. $order_type = $type;
  167. }
  168. $user_obj = CommonFn::apigetObJ($user_id,"ZUser",CommonFn::getMessage('user','id_not_exist'),201);
  169. $page = intval(Yii::app()->getRequest()->getParam("page",1));
  170. $pagesize = Yii::app()->params['ROrderListPageSize'];
  171. if($order_type == 1 ){
  172. $conditions = array(
  173. 'user'=>array('==',$user_obj->_id),
  174. 'status'=>array('notin',array(-3,-2,-1,6,7))
  175. );
  176. }elseif ($order_type == 2) {
  177. $conditions = array(
  178. 'user'=>array('==',$user_obj->_id),
  179. 'status'=>array('in',array(-3,-2,-1,7))
  180. );
  181. }elseif($order_type == 3){
  182. $conditions = array(
  183. 'user'=>array('==',$user_obj->_id),
  184. 'status'=>array('==',6)
  185. );
  186. }else{
  187. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  188. }
  189. $order = array(
  190. 'booking_time'=>'desc'
  191. );
  192. $model = new ROrder();
  193. $pagedata = CommonFn::getPagedata($model,$page,$pagesize,$conditions,$order);
  194. $order_list = $pagedata['res'];
  195. foreach ($order_list as $key => $value) {
  196. $order_list[$key] = $model->output($value);
  197. }
  198. $data = array_values($order_list);
  199. CommonFn::requestAjax(true,CommonFn::getMessage('message','operation_success'),$data,200,array('sum_count' => $pagedata['sum_count'],'sum_page'=>$pagedata['sum_page'],'page_size'=>$pagedata['page_size'],'current_page'=>$pagedata['current_page']));
  200. }
  201. public function actionPay(){
  202. $user_id = Yii::app()->getRequest()->getParam("user_id");
  203. $pay_channel = Yii::app()->getRequest()->getParam("pay_channel");
  204. $order_id = Yii::app()->getRequest()->getParam("order_id");
  205. if(!$user_id || !$pay_channel || !$order_id || !CommonFn::isMongoId($order_id)){
  206. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  207. }
  208. $user_obj = RUser::get(new MongoId($user_id));
  209. $order = ROrder::get(new MongoId($order_id));
  210. if(!$order || !$user_obj){
  211. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  212. }
  213. if($order && (string)$order->user != $user_id){
  214. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  215. }
  216. if($order->charge_id){
  217. CommonFn::requestAjax(false,'此订单已支付过',array('have_pay'=>1));
  218. }
  219. foreach ($order->products as $value) {
  220. if(isset($value['product'])){
  221. $product = Product::get($value['product']);
  222. }
  223. if($product){
  224. break;
  225. }
  226. }
  227. if(isset($product)&&$product){
  228. switch ($product->type) {
  229. case 1:
  230. $str = '宠物洗澡';
  231. break;
  232. case 2:
  233. $str = '宠物剪毛';
  234. break;
  235. case 3:
  236. $str = '宠物美容';
  237. break;
  238. default:
  239. $str = '宠物洗澡';
  240. break;
  241. }
  242. }
  243. $str = isset($str)?$str:'上门';
  244. $amount = ceil($order->final_price*1000)/10;
  245. $result = Service::factory('PayService')->Pay($pay_channel,$amount,(string)$order->_id,$str,$str,$user_obj->wx_pub_openid);
  246. if($result === false){
  247. CommonFn::requestAjax(false,'支付遇到点问题了,请稍候再试');
  248. }else{
  249. // 支付成功后用户有效订单数增加
  250. if (isset($user_obj->order_count)) {
  251. $user_obj->order_count += 1;
  252. } else {
  253. $user_obj->order_count = 1;
  254. }
  255. $user_obj->save();
  256. CommonFn::requestAjax(true,'success',json_decode($result),200,array('booking_time'=>$order->booking_time));
  257. }
  258. }
  259. public function actionDel(){
  260. $user_id = Yii::app()->getRequest()->getParam("user_id");
  261. $order_id = Yii::app()->getRequest()->getParam("order_id");
  262. if(!$user_id || !$order_id || !CommonFn::isMongoId($order_id)){
  263. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  264. }
  265. $user_obj = RUser::get(new MongoId($user_id));
  266. $order = ROrder::get(new MongoId($order_id));
  267. $status = -1;
  268. if(!$order || !$user_obj){
  269. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  270. }
  271. if($order && (string)$order->user != $user_id){
  272. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  273. }
  274. if($order->status != 0){
  275. CommonFn::requestAjax(false,'此订单暂不支持取消');
  276. }
  277. if( $order->status!=-1 && $order->status!=-2 ){
  278. foreach ($order->coupons as $user_coupon) {
  279. $user_coupon = UserCoupon::get($user_coupon);
  280. $user_coupon->status = 1;
  281. $user_coupon->update(array('status'),true);
  282. }
  283. }
  284. // 触宝信息更新
  285. $from = Yii::app()->request->getParam('from', '');
  286. if ($from == 'chubao') {
  287. $chubaoConfig = Yii::app()->params['chubao'];
  288. $chubao = new Chubao();
  289. $client = $chubao->getClient();
  290. $orderPush = $chubao->getOrderPush();
  291. $userId = $user_obj->name;
  292. $product_list = $order->products;
  293. $product_id = $product_list[0]['product'];
  294. $product = Product::get($product_id);
  295. $o2o_service = Yii::app()->params['o2o_service'];
  296. $shortInfo = $o2o_service[$product->type]['name'];
  297. $orderPush->setUserId($userId);
  298. $orderPush->setOrderId($order_id);
  299. $orderPush->setOrderService($chubaoConfig['service']);
  300. $orderPush->setTitle('壹管家宠物');
  301. $orderPush->setShortInfo('壹管家上门服务-'.$shortInfo);
  302. $orderPush->setOrderStatus('订单已取消');
  303. $orderPush->setCreateTime(date('YmdHis', $order->order_time));
  304. $orderPush->setFinishTime('');
  305. $orderPush->setOrderUrl($chubaoConfig['orderUrl'].'/'.$order_id);
  306. $orderPush->setTradeStatus('TRADE_CLOSED');
  307. $response = $client->execute($orderPush);
  308. if ($response['result'] == 'FAIL') {
  309. CommonFn::requestAjax(false, '订单取消失败,请稍后重试');
  310. }
  311. }
  312. $order->status = $status;
  313. $arr_order = array('status');
  314. $success = $order->update($arr_order,true);
  315. if($success){
  316. $order_info = $order->parseRow($order);
  317. }
  318. CommonFn::requestAjax($success, '');
  319. }
  320. public function actionUsableCoupon(){
  321. $user_id = Yii::app()->getRequest()->getParam("user_id");
  322. $get_all = Yii::app()->getRequest()->getParam("get_all");
  323. $type = Yii::app()->getRequest()->getParam("type");
  324. $booking_time = Yii::app()->getRequest()->getParam("booking_time");
  325. if($booking_time){
  326. $booking_time = strtotime($booking_time);
  327. $weekend_check = (date('w',$booking_time)==0||date('w',$booking_time)==6)?1:0;
  328. $booking_hour = date('H',$booking_time);
  329. }
  330. $device_id = Yii::app()->getRequest()->getParam("device_id",'');
  331. $products = json_decode(Yii::app()->getRequest()->getParam("products",'[]'),true);
  332. if(!CommonFn::isMongoId($user_id)){
  333. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  334. }
  335. $UserCoupon = new UserCoupon();
  336. if($get_all){
  337. $criteria = new EMongoCriteria();
  338. $criteria->user('==',new MongoId($user_id));
  339. $current_time = time();
  340. $criteria->end_time('>=',$current_time);
  341. $criteria->status('==',1);
  342. $coupons = UserCoupon::model()->findAll($criteria);
  343. $coupon_list = array();
  344. $coupon_list['useable_coupons'] = array();
  345. foreach ($coupons as $value) {
  346. $coupon = Coupon::get($value->coupon);
  347. if($coupon->status!=1){
  348. continue;
  349. }
  350. $coupon_list['useable_coupons'][] = $UserCoupon->parseRow($value,array('id','start_time','end_time','start_time_str','end_time_str','coupon'));
  351. }
  352. if(count($coupon_list['useable_coupons']) <= 15){
  353. $criteria = new EMongoCriteria();
  354. $criteria->user('==',new MongoId($user_id));
  355. $criteria->status('==',-1);
  356. $coupons = UserCoupon::model()->findAll($criteria);
  357. $coupon_list['used_coupons'] = array();
  358. foreach ($coupons as $value) {
  359. $coupon = $UserCoupon->parseRow($value,array('id','start_time','end_time','start_time_str','end_time_str','coupon'));
  360. $coupon['unuseable_reason'] = '已使用';
  361. $coupon_list['used_coupons'][] = $coupon;
  362. }
  363. $criteria = new EMongoCriteria();
  364. $criteria->user('==',new MongoId($user_id));
  365. $current_time = time();
  366. $criteria->end_time('<',$current_time);
  367. $coupons = UserCoupon::model()->findAll($criteria);
  368. $coupon_list['overtime_coupons'] = array();
  369. foreach ($coupons as $value) {
  370. $coupon = $UserCoupon->parseRow($value,array('id','start_time','end_time','start_time_str','end_time_str','coupon'));
  371. $coupon['unuseable_reason'] = '已过期';
  372. $coupon_list['overtime_coupons'][] = $coupon;
  373. }
  374. }else{
  375. $coupon_list['used_coupons'] = array();
  376. $coupon_list['overtime_coupons'] = array();
  377. }
  378. CommonFn::requestAjax(true,CommonFn::getMessage('message','operation_success'),$coupon_list);
  379. }
  380. $price = 0;
  381. if($products){
  382. foreach ($products as $product) {
  383. if(isset($product['product_id']) && $product['count'] >= 1){
  384. $product_temp = array();
  385. $product_obj = Product::get(new MongoId($product['product_id']));
  386. if($product_obj){
  387. $price += ($product_obj->price*$product['count']);
  388. }else{
  389. CommonFn::requestAjax(false,CommonFn::getMessage('o2o','product_not_exist'));
  390. }
  391. }else{
  392. CommonFn::requestAjax(false,CommonFn::getMessage('o2o','product_illegal'));
  393. }
  394. }
  395. }
  396. $criteria = new EMongoCriteria();
  397. $criteria->user('==',new MongoId($user_id));
  398. $current_time = time();
  399. $criteria->start_time('<=',$current_time);
  400. $criteria->end_time('>=',$current_time);
  401. $criteria->status('==',1);
  402. $coupons = UserCoupon::model()->findAll($criteria);
  403. $coupon_list = array();
  404. $unuseable_coupons = array();
  405. foreach ($coupons as $value) {
  406. $coupon = Coupon::get($value->coupon);
  407. if($coupon->status!=1){
  408. continue;
  409. }
  410. if($coupon->min_price>$price || ($type && $coupon->type != $type && $coupon->type != 0)){
  411. continue;
  412. }
  413. if($booking_time){
  414. if($weekend_check){
  415. if($coupon->workday_limit==1){
  416. $unuseable_coupons[] = $UserCoupon->parseRow($value,array('id','start_time','end_time','start_time_str','end_time_str','coupon'));
  417. continue;
  418. }
  419. }else{
  420. if($coupon->workday_limit==2){
  421. $unuseable_coupons[] = $UserCoupon->parseRow($value,array('id','start_time','end_time','start_time_str','end_time_str','coupon'));
  422. continue;
  423. }
  424. }
  425. if($coupon->time_limit_start && $coupon->time_limit_end && ($coupon->time_limit_start > $booking_hour || $coupon->time_limit_end < $booking_hour)){
  426. $unuseable_coupons[] = $UserCoupon->parseRow($value,array('id','start_time','end_time','start_time_str','end_time_str','coupon'));
  427. continue;
  428. }
  429. }
  430. $coupon_list[] = $UserCoupon->parseRow($value,array('id','start_time','end_time','start_time_str','end_time_str','coupon'));
  431. }
  432. CommonFn::requestAjax(true,CommonFn::getMessage('message','operation_success'),$coupon_list,200,array('unuseable_coupons' => $unuseable_coupons));
  433. }
  434. public function actionCheckAddress(){
  435. $user_id = Yii::app()->getRequest()->getParam("user_id");
  436. $address_id = Yii::app()->getRequest()->getParam("address_id");
  437. $user_obj = CommonFn::apigetObJ($user_id,'ZUser',CommonFn::getMessage('user','id_not_exist'),array(),201);
  438. $address_list = $user_obj->shop_address?$user_obj->shop_address:array();
  439. $address = array();
  440. foreach ($address_list as $value) {
  441. if($address_id == $value['address_id']){
  442. $address = $value;
  443. }
  444. }
  445. if(!$address){
  446. file_put_contents('/data/erroraddress_user.log',var_export($user_id,true),FILE_APPEND);
  447. CommonFn::requestAjax(false,CommonFn::getMessage('shop','address_false'));
  448. }else{
  449. if(isset($address['address']['province']) && ($address['address']['province'] == '上海市' || $address['address']['province'] == '')){
  450. CommonFn::requestAjax(true,'success',array('station' => '5548b05e0eb9fbc5728b51ea'));
  451. }else{
  452. file_put_contents('/data/erroraddress.log',var_export($address,true),FILE_APPEND);
  453. CommonFn::requestAjax(false,CommonFn::getMessage('o2o','address_cannot_service'));
  454. }
  455. }
  456. }
  457. public function actionAppend(){
  458. $user_id = Yii::app()->getRequest()->getParam("user_id");
  459. $order_id = Yii::app()->getRequest()->getParam("order_id");
  460. $products = json_decode(Yii::app()->getRequest()->getParam("products","[]"),true);
  461. if(!$user_id || !$products || !$order_id || !CommonFn::isMongoId($order_id) || !CommonFn::isMongoId($user_id)){
  462. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  463. }
  464. $user_obj = RUser::get(new MongoId($user_id));
  465. $order = ROrder::get(new MongoId($order_id));
  466. if(!$order || !$user_obj){
  467. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  468. }
  469. if($order && (string)$order->user != $user_id){
  470. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  471. }
  472. $product_list = array();
  473. $price = 0;
  474. foreach ($products as $product) {
  475. if(isset($product['product_id']) && $product['count'] >= 1){
  476. $product_temp = array();
  477. $product_obj = Product::get(new MongoId($product['product_id']));
  478. if($product_obj){
  479. $price += ($product_obj->price*$product['count']);
  480. $product_temp['product'] = $product_obj->_id;
  481. $product_temp['count'] = $product['count'];
  482. $product_list[] = $product_temp;
  483. if($product_obj->is_extra != 1){
  484. CommonFn::requestAjax(false,CommonFn::getMessage('o2o','product_illegal'));
  485. }
  486. }else{
  487. CommonFn::requestAjax(false,CommonFn::getMessage('o2o','product_not_exist'));
  488. }
  489. }else{
  490. CommonFn::requestAjax(false,CommonFn::getMessage('o2o','product_illegal'));
  491. }
  492. }
  493. $append_product = new AppendOrder();
  494. $append_product->order = $order->_id;
  495. $append_product->products = $product_list;
  496. $append_product->price = $price;
  497. $append_product->append_time = time();
  498. $success = $append_product->save();
  499. if($success){
  500. $order->append_orders[] = $append_product->_id;
  501. $order->update(array('append_orders'),true);
  502. $list = new ARedisList('append_order_list');
  503. $list->push((string)$order->_id);
  504. $append_info = $append_product->parseRow($append_product);
  505. CommonFn::requestAjax(true,CommonFn::getMessage('message','operation_success'),$append_info);
  506. }
  507. CommonFn::requestAjax($success, '');
  508. }
  509. public function actionAppendPay(){
  510. $user_id = Yii::app()->getRequest()->getParam("user_id");
  511. $pay_channel = Yii::app()->getRequest()->getParam("pay_channel");
  512. $order_id = Yii::app()->getRequest()->getParam("order_id");
  513. $append_id = Yii::app()->getRequest()->getParam("append_id");
  514. if(!$user_id || !$pay_channel || !$order_id || !$append_id || !CommonFn::isMongoId($append_id)){
  515. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  516. }
  517. $user_obj = RUser::get(new MongoId($user_id));
  518. $order = ROrder::get(new MongoId($order_id));
  519. $append_product = AppendOrder::get(new MongoId($append_id));
  520. if(!$order || !$user_obj){
  521. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  522. }
  523. if($order && (string)$order->user != $user_id){
  524. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  525. }
  526. if($append_product->status == 1){
  527. CommonFn::requestAjax(false,'此订单已支付过',array('have_pay'=>1));
  528. }
  529. $str = '附加服务';
  530. $amount = ceil($append_product->price*1000)/10;
  531. $result = Service::factory('PayService')->Pay($pay_channel,$amount,(string)$append_product->_id,$str,$str,$user_obj->wx_pub_openid);
  532. if($result === false){
  533. CommonFn::requestAjax(false,'支付遇到点问题了,请稍候再试');
  534. }else{
  535. // 追加订单并付款后通知保洁师
  536. if (!empty($order->technician)) {
  537. $techObj = TechInfo::get($order->technician);
  538. if (!empty($techObj) && !empty($techObj->weixin_userid)) {
  539. $wechat = O2oApp::getWechatActive();
  540. $url_prefix = ENVIRONMENT == 'product' ? 'http://api.wozhua.mobi' : 'http://apitest.wozhua.mobi';
  541. $wechat_data = array(
  542. 'touser' => $techObj->weixin_userid,
  543. 'msgtype' => 'news',
  544. 'agentid' => '24',
  545. 'news' => array(
  546. 'articles' => array(
  547. array(
  548. 'title' => '壹管家提示-新的追加订单',
  549. 'description' => $techObj->name.'你好!预定时间在'.date('m月d日H:i', $order->booking_time).'的订单刚刚被用户追加了新的服务,请点击查看。',
  550. 'url' => $url_prefix.'/index.php?r=o2o/myOrder/info&order='.(string)$order->_id.'&user='.$order->technician,
  551. ),
  552. ),
  553. ),
  554. );
  555. $wechat->sendMessage($wechat_data);
  556. }
  557. }
  558. CommonFn::requestAjax(true,'success',json_decode($result));
  559. }
  560. }
  561. public function actionRetrieve(){
  562. $user_id = Yii::app()->getRequest()->getParam("user_id");
  563. $order_id = Yii::app()->getRequest()->getParam("order_id");
  564. $from = Yii::app()->request->getParam('from', '');
  565. if(!$user_id || !$order_id ){
  566. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  567. }
  568. $user_obj = RUser::get(new MongoId($user_id));
  569. $order = ROrder::get(new MongoId($order_id));
  570. if(!$order || !$user_obj){
  571. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  572. }
  573. if($order && (string)$order->user != $user_id){
  574. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  575. }
  576. if($order->status != 1){
  577. CommonFn::requestAjax(false,'此订单不可申请退款');
  578. }
  579. $order->status = -3;
  580. $order->apply_refund_time = time();
  581. // 触宝平台更新
  582. if ($from == 'chubao') {
  583. $chubao = new Chubao();
  584. $client = $chubao->getClient();
  585. $tradeRefund = $chubao->getTradeRefund();
  586. $chubaoConfig = Yii::app()->params['chubao'];
  587. $chubao_info = $order->third_platform_info;
  588. $userId = $user_obj->name;
  589. $tradeRefund->setUserId($userId);
  590. $tradeRefund->setNotifyUrl($chubaoConfig['refundCallbackUrl']);
  591. $tradeRefund->setTransactionId($chubao_info['transactionId']);
  592. $tradeRefund->setRefundNo($order_id);
  593. $tradeRefund->setTotalFee($order->final_price * 100);
  594. $tradeRefund->setCashFee($order->final_price * 100);
  595. $tradeRefund->setRefundFee($order->final_price * 100);
  596. $response = $client->execute($tradeRefund);
  597. $refundStatus = $response['refundStatus'];
  598. if ($refundStatus == 'REFUND_FAIL') {
  599. CommonFn::requestAjax(false, '申请退款失败');
  600. }
  601. $chubao_info['redundId'] = $response['refundId'];
  602. $order->third_platform_info = $chubao_info;
  603. $order->save(true, array('third_platform_info'));
  604. }
  605. if($order->update(array('status','apply_refund_time'),true)){
  606. // 发送通知给保洁师
  607. if (!empty($order->technician)) {
  608. $techObj = TechInfo::get($order->technician);
  609. if (!empty($techObj) && !empty($techObj->weixin_userid)) {
  610. $wechat = O2oApp::getWechatActive();
  611. $url_prefix = ENVIRONMENT == 'product' ? 'http://api.wozhua.mobi' : 'http://apitest.wozhua.mobi';
  612. $wechat_data = array(
  613. 'touser' => $techObj->weixin_userid,
  614. 'msgtype' => 'news',
  615. 'agentid' => '24',
  616. 'news' => array(
  617. 'articles' => array(
  618. array(
  619. 'title' => '壹管家提示-申请退款',
  620. 'description' => $techObj->name.'你好!预定时间在'.date('m月d日H:i', $order->booking_time).'的订单刚刚已被用户申请退款,请点击查看。',
  621. 'url' => $url_prefix.'/index.php?r=o2o/myOrder/info&order='.$order_id.'&user='.$order->technician,
  622. ),
  623. ),
  624. ),
  625. );
  626. $wechat->sendMessage($wechat_data);
  627. }
  628. }
  629. CommonFn::requestAjax(true,'申请退款成功,请等待确认');
  630. }else{
  631. CommonFn::requestAjax(false,'请稍后再试');
  632. }
  633. }
  634. public function actionConfirmComplete(){
  635. $user_id = Yii::app()->getRequest()->getParam("user_id");
  636. $order_id = Yii::app()->getRequest()->getParam("order_id");
  637. $from = Yii::app()->request->getParam('from', '');
  638. if(!$user_id || !$order_id ){
  639. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  640. }
  641. $user_obj = RUser::get(new MongoId($user_id));
  642. $order = ROrder::get(new MongoId($order_id));
  643. if(!$order || !$user_obj){
  644. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  645. }
  646. if($order && (string)$order->user != $user_id){
  647. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  648. }
  649. if(!in_array($order->status, array(1,2,3,4,5))){
  650. CommonFn::requestAjax(false,'此订单不支持修改状态');
  651. }
  652. $order->status = 6;
  653. $order->finish_time = time();
  654. // 触宝信息更新
  655. if ($from == 'chubao') {
  656. $chubaoConfig = Yii::app()->params['chubao'];
  657. $chubao = new Chubao();
  658. $client = $chubao->getClient();
  659. $orderPush = $chubao->getOrderPush();
  660. $userId = $user_obj->name;
  661. $orderPush->setUserId($userId);
  662. $orderPush->setOrderId($order_id);
  663. $orderPush->setOrderService($chubaoConfig['service']);
  664. $orderPush->setTitle('壹管家宠物');
  665. $product_list = $order->products;
  666. $product_id = $product_list[0]['product'];
  667. $product = Product::get($product_id);
  668. $o2o_service = Yii::app()->params['o2o_service'];
  669. $shortInfo = $o2o_service[$product->type]['name'];
  670. $orderPush->setOrderShortInfo('壹管家上门服务-'.$shortInfo);
  671. $orderPush->setOrderStatus('订单已完成');
  672. $orderPush->setOrderCreateTime(date('YmdHis', $order->order_time));
  673. $orderPush->setOrderFinishTime(date('YmdHis', time()));
  674. $orderPush->setOrderUrl($chubaoConfig['orderUrl'].'/'.$order_id);
  675. $orderPush->setTradeStatus('TRADE_FINISH');
  676. $response = $client->execute($orderPush);
  677. if ($response['result'] == 'FAIL') {
  678. CommonFn::requestAjax(false,'请稍后再试');
  679. }
  680. $success = $order->update(array('finish_time','status'),true);
  681. if ($success) {
  682. CommonFn::requestAjax(true,'订单已完成,期待您的下次预约');
  683. } else {
  684. CommonFn::requestAjax(false,'请稍后再试');
  685. }
  686. }
  687. $success = $order->update(array('finish_time','status'),true);
  688. if($success && $from != 'chubao'){
  689. $result = Service::factory('ScoreService')->changeScore($user_id,intval($order->final_price),'下单奖爪币');
  690. if($result){
  691. $z_message = new ZMessage();
  692. $from_user = Yii::app()->params['sys_user'];
  693. $message_data = array(
  694. 'from_user' => $from_user,
  695. 'to_user' => $user_id,
  696. 'content' => '您成功下单,获得了'.intval($order->final_price).'个爪币的奖励。',
  697. 'pics' => array(),
  698. 'voice' => array(),
  699. 'video'=> array()
  700. );
  701. $z_message->addMessage($message_data);
  702. CommonSMS::send('final_order',array('name' =>$order->address['name'],'num' =>intval($order->final_price),'mobile'=>$order->address['mobile']));
  703. }
  704. // 保洁师订单统计
  705. if ($order->technician) {
  706. $tech_obj = TechInfo::get($order->technician);
  707. if ($tech_obj) {
  708. $order_count = $tech_obj->order_count + 1;
  709. $tech_obj->order_count = $order_count;
  710. }
  711. }
  712. CommonFn::requestAjax(true,'订单已完成,期待您的下次预约');
  713. }else{
  714. CommonFn::requestAjax(false,'请稍后再试');
  715. }
  716. }
  717. public function actionDetail(){
  718. $user_id = Yii::app()->getRequest()->getParam("user_id");
  719. $order_id = Yii::app()->getRequest()->getParam("order_id");
  720. if(!$user_id || !$order_id || !CommonFn::isMongoId($order_id) || !CommonFn::isMongoId($user_id)){
  721. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  722. }
  723. $user_obj = RUser::get(new MongoId($user_id));
  724. $order = ROrder::get(new MongoId($order_id));
  725. if(!$order || !$user_obj){
  726. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  727. }
  728. if($order && (string)$order->user != $user_id){
  729. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  730. }
  731. $order_info = $order->parseRow($order);
  732. CommonFn::requestAjax(true,'',$order_info);
  733. }
  734. //选择保洁师
  735. public function actionSelectTech(){
  736. $booking_time = Yii::app()->getRequest()->getParam("booking_time");
  737. $service_type = json_decode(Yii::app()->getRequest()->getParam("service_type","[]"),true);
  738. $user_id = Yii::app()->getRequest()->getParam("user_id");
  739. $address_id = Yii::app()->getRequest()->getParam("address_id");
  740. $user_obj = CommonFn::apigetObJ($user_id,'ZUser',CommonFn::getMessage('user','id_not_exist'),array(),201);
  741. $address_list = $user_obj->shop_address?$user_obj->shop_address:array();
  742. $address = array();
  743. foreach ($address_list as $value) {
  744. if($address_id == $value['address_id']){
  745. $address = $value;
  746. }
  747. }
  748. if(strtotime($booking_time) < time()){
  749. CommonFn::requestAjax(false,'此时间段不可预约');
  750. }
  751. if(!$address){
  752. CommonFn::requestAjax(false,CommonFn::getMessage('shop','address_false'));
  753. }
  754. if(!isset($address['position'][0])||$address['position'][0]==0||is_int($address['position'][0])){
  755. $address_res = CommonFn::simple_http('http://api.map.baidu.com/geocoder/v2/?ak=B349f0b32ef6e78b2e678f45cb9fddaf&address='.$address['address']['area'].$address['address']['detail'].'&city='.$address['address']['city'].'&output=json');
  756. $address_info = json_decode($address_res,true);
  757. $position = $address_info['result']['location'];
  758. $detail_res = CommonFn::simple_http('http://api.map.baidu.com/geocoder/v2/?ak=B349f0b32ef6e78b2e678f45cb9fddaf&location='.$position['lat'].','.$position['lng'].'&output=json&pois=1');
  759. $detail_info = json_decode($detail_res,true);
  760. $business = explode(',',$detail_info['result']['business']);
  761. }else{
  762. $detail_res = CommonFn::simple_http('http://api.map.baidu.com/geocoder/v2/?ak=B349f0b32ef6e78b2e678f45cb9fddaf&location='.$address['position'][1].','.$address['position'][0].'&output=json&pois=1');
  763. $detail_info = json_decode($detail_res,true);
  764. $business = explode(',',$detail_info['result']['business']);
  765. }
  766. $user_model = new TechInfo();
  767. $criteria = new EMongoCriteria();
  768. $criteria->service_type('all', $service_type);
  769. if( !in_array(9, $service_type) && !in_array(10, $service_type)){
  770. foreach ($business as $value) {
  771. $criteria->addCond('business','or',$value);
  772. }
  773. }
  774. $criteria->status('==', 1);
  775. $cursor = $user_model->findAll($criteria);
  776. //获取满足服务类型保洁师列表
  777. $match_service_tech_list = array();
  778. $tech_list = array();
  779. foreach ($cursor as $key => $value) {
  780. $match_service_tech_list[] = $value->_id;
  781. $tech_list[$value->_id] = $value;
  782. }
  783. $parse_time = strtotime(date('Y-m-d H:00',strtotime($booking_time)));
  784. $FreeTimeRecord = FreeTimeRecord::get($parse_time);
  785. // $last_parse_time = strtotime(date('Y-m-d H:00',strtotime($booking_time)))-3600;
  786. // $LastFreeTimeRecord = FreeTimeRecord::get($last_parse_time);
  787. // $next_parse_time = strtotime(date('Y-m-d H:00',strtotime($booking_time)))-3600;
  788. // $NextFreeTimeRecord = FreeTimeRecord::get($next_parse_time);
  789. //获取此时段空闲保洁师列表
  790. $free_tech_list = array();
  791. // $last_free_tech_list = array();
  792. // $next_free_tech_list = array();
  793. if($FreeTimeRecord){
  794. $free_tech_list = $FreeTimeRecord->free_technician;
  795. }
  796. // if($LastFreeTimeRecord){
  797. // $last_free_tech_list = $LastFreeTimeRecord->free_technician;
  798. // }
  799. // if($NextFreeTimeRecord){
  800. // $next_free_tech_list = $NextFreeTimeRecord->free_technician;
  801. // }
  802. //空闲且满足服务能力保洁师,推荐用户选择
  803. $recommend_tech = array_intersect($match_service_tech_list,$free_tech_list);
  804. $can_select_tech = array();
  805. foreach ($recommend_tech as $key => $value) {
  806. $tech = $tech_list[$value];
  807. unset($tech_list[$value]);
  808. $tmp['id'] = $tech->_id;
  809. $tmp['name'] = $tech->name;
  810. $tmp['desc'] = $tech->desc;
  811. $tmp['service_type'] = $tech->service_type;
  812. $tmp['avatar'] = $tech->avatar;
  813. $tmp['favourable_count'] = $tech->favourable_count;
  814. $can_select_tech[] = $tmp;
  815. }
  816. //满足服务能力,但此时段不空闲,展示给用户此保洁师下个空闲时段
  817. $service_match_tech = array();
  818. foreach ($tech_list as $key => $value) {
  819. $tmp['id'] = $value->_id;
  820. $tmp['name'] = $value->name;
  821. $tmp['desc'] = $value->desc;
  822. $tmp['service_type'] = $value->service_type;
  823. $tmp['avatar'] = $value->avatar;
  824. $tmp['favourable_count'] = $value->favourable_count;
  825. $criteria = new EMongoCriteria();
  826. $criteria->free_technician('==', $tmp['id']);
  827. $criteria->_id('>',time());
  828. $criteria->limit(1);
  829. $criteria->sort('_id', EMongoCriteria::SORT_ASC);
  830. if(FreeTimeRecord::model()->count($criteria)){
  831. $free_time = FreeTimeRecord::model()->findAll($criteria);
  832. foreach ($free_time as $next_free_time) {
  833. $tmp['next_free_time'] = FreeTimeRecord::parseFreeTime($next_free_time->_id);
  834. break;
  835. }
  836. }else{
  837. continue;
  838. }
  839. $service_match_tech[] = $tmp;
  840. }
  841. $data['can_select_tech'] = $can_select_tech;
  842. $data['service_match_tech'] = $service_match_tech;
  843. CommonFn::requestAjax(true,'',$data);
  844. }
  845. /**
  846. * 获取评价列表接口
  847. */
  848. public function actionTechComment() {
  849. $page = intval(Yii::app()->getRequest()->getParam("page",1));
  850. $tech_id = intval(Yii::app()->getRequest()->getParam("tech_id"));
  851. $pagesize = Yii::app()->params['O2oCommentListPageSize'];
  852. if(!$tech_id){
  853. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  854. }
  855. $conditions = array(
  856. 'technician'=>array('==',$tech_id),
  857. 'status'=>array('==',1),
  858. );
  859. $order = array(
  860. 'time' => 'desc'
  861. );
  862. $model = new Comment();
  863. $pagedata = CommonFn::getPagedata($model,$page,$pagesize,$conditions,$order);
  864. $data['comments'] = $pagedata['res'];
  865. CommonFn::requestAjax(true,CommonFn::getMessage('message','operation_success'),$data,200,array('sum_count' => $pagedata['sum_count'],'sum_page'=>$pagedata['sum_page'],'page_size'=>$pagedata['page_size'],'current_page'=>$pagedata['current_page']));
  866. }
  867. }