OrderController.php 41 KB

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