OrderController.php 41 KB

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