OrderController.php 39 KB

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