OrderController.php 44 KB

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