OrderController.php 43 KB

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