OrderController.php 39 KB

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