OrderController.php 38 KB

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