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