OrderController.php 39 KB

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