OrderController.php 41 KB

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