public function actionOrderView() { //创建查询条件 $criteria = new CDbCriteria(); $criteria->order = 't.create_time DESC'; //按时间倒序排 //如果没有指定日期,默认查询当天的订单统计 $date = Yii::app()->request->getParam('date'); if ($date) { $today = strtotime(date($date)); if (!$today) { throw new CHttpException(404, '日期格式设置有误'); } else { if ($today > time()) { throw new CHttpException(404, '设置的日期不能超过今天'); } } $tomorrow = $today + 24 * 3600; } else { $today = strtotime(date('Y-m-d')); $tomorrow = strtotime(date('Y-m-d', time() + 24 * 3600)); } $criteria->condition = '(t.status = :status1 OR t.status = :status2) AND t.create_time > :today AND t.create_time < :tomorrow'; $criteria->params = array(':status1' => 1, ':status2' => 2, ':today' => $today, ':tomorrow' => $tomorrow); $model = FoodOrder::model()->with('shops', 'members')->findAll($criteria); $data = array(); $_total_price = 0; $tongji = array(); //存储所有订餐的人员名单 $orderList = array(); //处理查询得到的每一条订单 //k表示键值,v表示每一条订单 foreach ($model as $k => $v) { $_total_price += $v->total_price; $data[$k] = $v->attributes; $data[$k]['product_info'] = unserialize($v->product_info); $data[$k]['shop_name'] = $v->shops->name; $data[$k]['user_name'] = $v->members->name; $data[$k]['create_time'] = date('Y-m-d H:i:s', $v->create_time); $data[$k]['status_text'] = Yii::app()->params['order_status'][$v->status]; $data[$k]['status_color'] = Yii::app()->params['status_color'][$v->status]; //统计 $tongji[$v->shop_id]['name'] = $v->shops->name; $tongji[$v->shop_id]['product'][] = unserialize($v->product_info); //将所有订单按shopName,username,orderinfo存储起来 $orderList[$v->shop_id]['food_class'] = $v->shops->name; $orderList[$v->shop_id]['order'][$v->food_user_id]['user_name'] = $v->members->name; $orderList[$v->shop_id]['order'][$v->food_user_id]['order_info'][] = unserialize($v->product_info); } //按餐类和地点重新组织为orderviewlist $orderViewList = array(); //统计结果 $result = array(); $foodTotalCount = 0; foreach ($orderList as $k => $v) { $result[$k]['name'] = $v['food_class']; $orderViewList[$k]['foodClass'] = $v['food_class']; $shop_total_price = 0; $foodsShopTotalCount = 0; foreach ($v['order'] as $lk => $lv) { //$orderViewList[$k]['order'][$lk]['userName']=$lv['user_name']; foreach ($lv['order_info'] as $_k => $_v) { foreach ($_v as $kk => $vv) { $shop_total_price += $vv['smallTotal']; $foodsShopTotalCount += $vv['Count']; $result[$k]['product'][$vv['Id']]['name'] = $vv['Name']; if ($result[$k]['product'][$vv['Id']]['count']) { $result[$k]['product'][$vv['Id']]['count'] += $vv['Count']; } else { $result[$k]['product'][$vv['Id']]['count'] = $vv['Count']; } if ($result[$k]['product'][$vv['Id']]['smallTotal']) { $result[$k]['product'][$vv['Id']]['smallTotal'] += $vv['smallTotal']; } else { $result[$k]['product'][$vv['Id']]['smallTotal'] = $vv['smallTotal']; } //LCF:统计orderViewList列表 //orderview if ($orderViewList[$k]['locations'][$vv['Id']]['count']) { $orderViewList[$k]['locations'][$vv['Id']]['count'] += $vv['Count']; } else { $orderViewList[$k]['locations'][$vv['Id']]['count'] = $vv['Count']; } $orderViewList[$k]['locations'][$vv['Id']]['locationName'] = $vv['Name']; $orderViewList[$k]['locations'][$vv['Id']]['orderItems'][$lk]['userName'] = $lv['user_name']; $orderViewList[$k]['locations'][$vv['Id']]['orderItems'][$lk]['count'] += $vv['Count']; } } } $result[$k]['shop_total_price'] = $shop_total_price; $orderViewList[$k]['totalCount'] = $foodsShopTotalCount; $foodTotalCount += $foodsShopTotalCount; } //输出到前端 $this->render('orderView', array('foodTotalCount' => $foodTotalCount, 'orderViewList' => $orderViewList, 'data' => $data, 'statistics' => $result, 'total_price' => $_total_price, 'date' => $date)); }
public function actionCancelOrder() { if (Yii::app()->request->isAjaxRequest) { $food_order_id = Yii::app()->request->getParam('id'); if (!$food_order_id) { $this->errorOutput(array('errorCode' => 1, 'errorText' => '没有id')); } //判断当前时间有没有已经过了订餐的时间,如果过了订餐的时间,就不能取消订单了,只能让妹子操作后台取消 if (!Yii::app()->check_time->isOnTime()) { $this->errorOutput(array('errorCode' => 1, 'errorText' => '已经过了订餐时间,您暂时不能取消订单,如果确实需要取消,请联系前台妹子')); } $orderInfo = FoodOrder::model()->find('id=:id AND food_user_id=:food_user_id', array(':id' => $food_order_id, ':food_user_id' => Yii::app()->user->member_userinfo['id'])); if (!$orderInfo) { $this->errorOutput(array('errorCode' => 1, 'errorText' => '该订单不存在')); } else { if ($orderInfo->status != 1) { $this->errorOutput(array('errorCode' => 1, 'errorText' => '该订单不能被取消')); } } $orderInfo->status = 3; if ($orderInfo->save()) { //创建一条订单日志 $foodOrderLog = new FoodOrderLog(); $foodOrderLog->food_order_id = $food_order_id; $foodOrderLog->status = $orderInfo->status; $foodOrderLog->create_time = time(); if ($foodOrderLog->save()) { $this->output(array('success' => 1, 'successText' => '取消订单成功')); } else { $this->errorOutput(array('errorCode' => 1, 'errorText' => '更新订单状态失败')); } } else { $this->errorOutput(array('errorCode' => 1, 'errorText' => '取消订单失败')); } } else { throw new CHttpException(404, Yii::t('yii', '非法操作')); } }
public function loadModel($id) { $model = FoodOrder::model()->with('shops', 'members')->findByPk($id); if ($model === null) { throw new CHttpException(404, 'The requested page does not exist.'); } return $model; }
public function actionCancelOrder() { $food_order_id = Yii::app()->request->getParam('id'); if (!$food_order_id) { Error::output(Error::ERR_NO_ORDERID); } $orderInfo = FoodOrder::model()->find('id=:id AND food_user_id=:food_user_id', array(':id' => $food_order_id, ':food_user_id' => $this->module->user['id'])); if (!$orderInfo) { Error::output(Error::ERR_NO_ORDER); } else { if ($orderInfo->status != 1) { Error::output(Error::ERR_ORDER_CANNOT_CANCEL); } } $orderInfo->status = 3; if ($orderInfo->save()) { //创建一条订单日志 $foodOrderLog = new FoodOrderLog(); $foodOrderLog->food_order_id = $food_order_id; $foodOrderLog->status = $orderInfo->status; $foodOrderLog->create_time = time(); $foodOrderLog->save(); Out::jsonOutput(array('return' => 1)); //取消成功 } else { Error::output(Error::ERR_SAVE_FAIL); } }