OrderController.php 41 KB

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