OrderController.php 41 KB

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