OrderController.php 44 KB

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