PageRenderTime 64ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/modules/my/controllers/OrderController.php

https://github.com/duofei/dawuweiba
PHP | 336 lines | 264 code | 26 blank | 46 comment | 40 complexity | 2beef8b8f37421722a7f21ff2ee95f07 MD5 | raw file
  1. <?php
  2. class OrderController extends Controller
  3. {
  4. /**
  5. * 查询订单
  6. * @param object $condition 查询条件
  7. * @param boolean $noRecommend 是否是未点评订单
  8. * @return array 订单列表
  9. */
  10. public function loadOrderList($condition, $orderName='')
  11. {
  12. if($orderName != 'NoRecommend') {
  13. $pages = new CPagination(Order::model()->count($condition));
  14. $pages->pageSize = 10;
  15. $pages->applyLimit($condition);
  16. }
  17. $orderList = Order::model()->with(array(
  18. 'shop',
  19. 'groupon',
  20. 'shopCreditLogs',
  21. 'orderGoods',
  22. 'orderGoods.goodsRateLog',
  23. 'orderLogs'=>array('order'=>'orderLogs.create_time desc'),
  24. ))->findAll($condition);
  25. $orderIsNoRating = array();
  26. $newOrderList = array();
  27. foreach ($orderList as $row) {
  28. $orderIsNoRating[$row->id] = false; // 是否未点评
  29. if($row->consignee && $row->telphone){ //用户是通过post产生,非查看电话的订单
  30. if(!$row->shopCreditLogs->id && ($row->status==Order::STATUS_COMPLETE || $row->status==Order::STATUS_DELIVERING)) {
  31. $orderIsNoRating[$row->id] = true;
  32. } else {
  33. foreach($row->orderGoods as $goods) {
  34. if(($row->status==Order::STATUS_COMPLETE || $row->status==Order::STATUS_DELIVERING) && !$goods->goodsRateLog->goods_id) {
  35. $orderIsNoRating[$row->id] = true;
  36. break;
  37. }
  38. }
  39. }
  40. }
  41. if($orderIsNoRating[$row->id]) {
  42. $newOrderList[] = $row;
  43. }
  44. }
  45. if($orderName == 'NoRecommend') {
  46. $orderList = $newOrderList;
  47. }
  48. return array('data'=>$orderList, 'orderIsNoRating'=>$orderIsNoRating, 'pages'=>$pages);
  49. }
  50. /**
  51. * 全部订单列表
  52. */
  53. public function actionList()
  54. {
  55. $this->breadcrumbs = array(
  56. '个人中心' => url('my'),
  57. '我的订单' => url('my/order/uncomplete'),
  58. '全部订单'
  59. );
  60. $condition = new CDbCriteria();
  61. $condition->addColumnCondition(array('t.user_id' => user()->id));
  62. $condition->order = 't.create_time desc';
  63. $result = $this->loadOrderList($condition);
  64. $this->pageTitle = '全部订单';
  65. $this->render('list', array('data' => $result['data'], 'pages'=>$result['pages'], 'orderIsNoRating'=>$result['orderIsNoRating'], 'id'=>'list'));
  66. }
  67. /**
  68. * 未完成订单
  69. */
  70. public function actionUncomplete()
  71. {
  72. $this->breadcrumbs = array(
  73. '个人中心' => url('my'),
  74. '我的订单' => url('my/order/uncomplete'),
  75. '未完成订单'
  76. );
  77. $condition = new CDbCriteria();
  78. $condition->addColumnCondition(array('t.user_id' => user()->id));
  79. $condition->addCondition('t.status!=' . Order::STATUS_CANCEL);
  80. $condition->addCondition('t.status!=' . Order::STATUS_COMPLETE);
  81. $condition->addCondition('t.status!=' . Order::STATUS_INVAIN);
  82. $condition->order = 't.create_time desc';
  83. $result = $this->loadOrderList($condition, 'UnComplete');
  84. $this->pageTitle = '未完成订单';
  85. $this->render('list', array('data' => $result['data'], 'pages'=>$result['pages'], 'orderIsNoRating'=>$result['orderIsNoRating'], 'id'=>'uncomplete'));
  86. }
  87. /**
  88. * 未点评订单
  89. */
  90. public function actionNorating()
  91. {
  92. $this->breadcrumbs = array(
  93. '个人中心' => url('my'),
  94. '我的订单' => url('my/order/uncomplete'),
  95. '未点评订单'
  96. );
  97. $condition = new CDbCriteria();
  98. $condition->addColumnCondition(array('t.user_id' => user()->id));
  99. $condition->addInCondition('t.status', array(Order::STATUS_COMPLETE, Order::STATUS_DELIVERING));
  100. $condition->order = 't.create_time desc';
  101. $result = $this->loadOrderList($condition, 'NoRecommend');
  102. $data = array();
  103. $norationPages = '<ul id="yw0" class="yiiPager">';
  104. $page = intval($_GET['page']) ? intval($_GET['page']) : 1;
  105. if($page > 1) {
  106. $norationPages .= '<li class="previous"><a href="'. url('my/order/norating', array('page'=>($page-1))) .'">上一页</a></li>';
  107. }
  108. $pagenum = 10;
  109. $i = 0;
  110. foreach ($result['data'] as $row) {
  111. if($i >= ($page-1)*$pagenum && $i<($page)*$pagenum) {
  112. $data[] = $row;
  113. } elseif ($i >= ($page)*$pagenum) {
  114. $norationPages .= '<li class="next ma-l5px"><a href="'. url('my/order/norating', array('page'=>($page+1))) .'">下一页</a></li>';
  115. break;
  116. }
  117. $i++;
  118. }
  119. $norationPages .= '</ul>';
  120. $this->pageTitle = '未点评订单';
  121. $this->render('list', array('data' => $data, 'norationPages'=>$norationPages, 'orderIsNoRating'=>$result['orderIsNoRating'], 'id'=>'norating'));
  122. }
  123. /**
  124. * 网络订单
  125. */
  126. public function actionOnline()
  127. {
  128. $this->breadcrumbs = array(
  129. '个人中心' => url('my'),
  130. '我的订单' => url('my/order/uncomplete'),
  131. '网络订单'
  132. );
  133. $condition = new CDbCriteria();
  134. $condition->addColumnCondition(array('t.user_id' => user()->id));
  135. $condition->addCondition('t.buy_type in (' . Shop::BUYTYPE_NETWORK . ',' . Shop::BUYTYPE_PRINTER . ')');
  136. $condition->order = 't.create_time desc';
  137. $result = $this->loadOrderList($condition);
  138. $this->pageTitle = '网络订单';
  139. $this->render('list', array('data' => $result['data'], 'pages'=>$result['pages'], 'orderIsNoRating'=>$result['orderIsNoRating'], 'id'=>'online'));
  140. }
  141. /**
  142. * 同楼订餐
  143. */
  144. public function actionGroupon()
  145. {
  146. $this->breadcrumbs = array(
  147. '个人中心' => url('my'),
  148. '我的订单' => url('my/order/uncomplete'),
  149. '同楼订餐'
  150. );
  151. $condition = new CDbCriteria();
  152. $condition->addColumnCondition(array('t.user_id' => user()->id));
  153. $condition->addCondition('t.groupon_id > 0');
  154. $condition->order = 't.create_time desc';
  155. $result = $this->loadOrderList($condition);
  156. $this->pageTitle = '网络订单';
  157. $this->render('list', array('data' => $result['data'], 'pages'=>$result['pages'], 'orderIsNoRating'=>$result['orderIsNoRating'], 'id'=>'groupon'));
  158. }
  159. /**
  160. * 电话订单列表
  161. */
  162. public function actionTelphone()
  163. {
  164. $this->breadcrumbs = array(
  165. '个人中心' => url('my'),
  166. '我的订单' => url('my/order/uncomplete'),
  167. '电话订单'
  168. );
  169. $condition = new CDbCriteria();
  170. $condition->addColumnCondition(array('t.user_id' => user()->id));
  171. $condition->addCondition('t.buy_type=' . Shop::BUYTYPE_TELPHONE);
  172. $condition->order = 't.create_time desc';
  173. $result = $this->loadOrderList($condition);
  174. $this->pageTitle = '电话订单';
  175. $this->render('list', array('data' => $result['data'], 'pages'=>$result['pages'], 'orderIsNoRating'=>$result['orderIsNoRating'], 'id'=>'telphone'));
  176. }
  177. /**
  178. * 团购订单列表
  179. * @param integer $state 订单状态
  180. */
  181. public function actionTuan($state = 0)
  182. {
  183. $state = (int)$state;
  184. $this->render('tuan');
  185. }
  186. /**
  187. * 取消一个订单
  188. * @param integer $orderid 订单ID
  189. */
  190. public function actionCancel()
  191. {
  192. $orderid = (int)$_POST['order_id'];
  193. $content = strip_tags($_POST['content']);
  194. $order = Order::model()->findByPk($orderid);
  195. if($order->user_id != user()->id) {
  196. echo '非法操作';
  197. exit;
  198. }
  199. if($order->status==Order::STATUS_UNDISPOSED) {
  200. $order->cancel_reason = $content;
  201. if(!$order->save()) {
  202. echo '取消申请失败!';
  203. }
  204. $orderlog = new OrderLog();
  205. $orderlog->order_id = $orderid;
  206. $orderlog->type_id = Order::STATUS_CANCEL;
  207. if($orderlog->save()) {
  208. // 如果当前订单是同楼订餐的话
  209. if($order->groupon_id > 0) {
  210. Groupon::loseOrder($order);
  211. }
  212. echo 1;
  213. }
  214. } else {
  215. $order->cancel_reason = $content;
  216. $order->cancel_state = STATE_ENABLED;
  217. if(!$order->save()) {
  218. echo '取消申请失败!';
  219. }
  220. echo 2;
  221. }
  222. }
  223. /**
  224. * 口味评分
  225. */
  226. public function actionPostRate()
  227. {
  228. $ordergoodsid = intval($_POST['ordergoodsid']);
  229. $mark = intval($_POST['value']);
  230. $ordergoods = OrderGoods::model()->findByPk($ordergoodsid);
  231. if(!$ordergoods) {
  232. exit;
  233. }
  234. $goodsratelog = new GoodsRateLog();
  235. $goodsratelog->goods_id = $ordergoods->goods_id;
  236. $goodsratelog->shop_id = $ordergoods->order->shop_id;
  237. $goodsratelog->mark = $mark;
  238. $goodsratelog->ordergoods_id = $ordergoodsid;
  239. if(!$goodsratelog->save()){
  240. exit;
  241. } else {
  242. if($ordergoods->order->buy_type==Shop::BUYTYPE_TELPHONE && $ordergoods->order->telphone=='') {
  243. // 如果是电话订单,并且不是通过客服联系。 不加积分
  244. echo 'exit';
  245. exit;
  246. }
  247. echo $goodsratelog->id;
  248. // 增加用户积分
  249. $integrals = intval($ordergoods->goods_amount);
  250. UserIntegralLog::addUserIntegralLog(UserIntegralLog::SOURCE_GOODSEVALUATE, $integrals);
  251. }
  252. exit;
  253. }
  254. /**
  255. * 口味点评内容
  256. */
  257. public function actionPostRateContent()
  258. {
  259. $id = intval($_POST['id']);
  260. $goodsratelog = GoodsRateLog::model()->findByPk($id);
  261. if($goodsratelog->user_id != user()->id) {
  262. echo '非法操作';
  263. exit;
  264. }
  265. $goodsratelog->content = trim(strip_tags($_POST['content']));
  266. if(!$goodsratelog->save()) {
  267. echo CHtml::errorSummary($goodsratelog);
  268. }
  269. exit;
  270. }
  271. /**
  272. * 商家服务点评内容
  273. */
  274. public function actionPostService()
  275. {
  276. $order_id = intval($_POST['order_id']);
  277. $evaluate = intval($_POST['evaluate']);
  278. $order = Order::model()->findByPk($order_id);
  279. if($order->user_id != user()->id) {
  280. echo '非法操作';
  281. exit;
  282. }
  283. if(!$order) {
  284. echo '不存在此订单';
  285. exit;
  286. }
  287. $shopcreditlog = new ShopCreditLog();
  288. $shopcreditlog->order_id = $order_id;
  289. $shopcreditlog->shop_id = $order->shop_id;
  290. $shopcreditlog->evaluate = $evaluate;
  291. if(!$shopcreditlog->save()) {
  292. exit;
  293. } else {
  294. if($order->buy_type==Shop::BUYTYPE_TELPHONE && $order->telphone=='') {
  295. // 如果是电话订单,并且不是通过客服联系。 不加积分
  296. exit;
  297. }
  298. // 增加用户积分
  299. UserIntegralLog::addUserIntegralLog(UserIntegralLog::SOURCE_SERVEEVALUATE, intval($order->amount));
  300. }
  301. exit;
  302. }
  303. public function filters()
  304. {
  305. return array(
  306. 'ajaxOnly + postRateContent, postRate, postService',
  307. 'postOnly + postRateContent, postRate, postService',
  308. );
  309. }
  310. }