OrderController.php 42 KB

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