OrderController.php 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  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-11")->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-11")->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. var_dump($coupons);
  471. echo "1";
  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. }