OrderController.php 46 KB

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