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