OrderController.php 41 KB

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