OrderController.php 41 KB

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