123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- <?php
- /**
- * 提成控制器
- * @author zhouxuchen 2015-12-02
- */
- class CommisionController extends AdminController {
- public function actionIndex() {
- $type_option = Commision::$type_option;
- $type = CommonFn::getComboboxData($type_option, 100, true, 100);
- $this->render('index', array(
- 'type_option' => $type,
- ));
- }
- public function actionList() {
- $search = Yii::app()->request->getParam('search', '');
- $type = intval(Yii::app()->request->getParam('type', 100));
- $start = Yii::app()->request->getParam('start', '');
- $end = Yii::app()->request->getParam('end', '');
- $params = CommonFn::getPageParams();
- $criteria = new EMongoCriteria($params);
- // 订单类型筛选
- if ($type != 100) {
- $criteria->type('==', $type);
- }
- // 时间筛选
- if ($start != '') {
- $start_time = strtotime($start);
- $criteria->booking_time('>=', $start_time);
- }
- if ($end != '') {
- $end_time = strtotime($end);
- $end_time = strtotime('+1 day', $end_time);
- $criteria->booking_time('<', $end_time);
- }
- // 模糊搜索
- if ($search != '') {
- if (CommonFn::isMongoId($search)) {
- $criteria->addCond('_id', 'or', new MongoId($search));
- $criteria->addCond('order', 'or', new MongoId($search));
- } else {
- $user_regex = new MongoRegex('/'.$search.'/');
- $criteria_user = new EMongoCriteria();
- $criteria_user->addCond('email', 'or', $user_regex);
- $criteria_user->addCond('name', 'or', $user_regex);
- $users = User::model()->findAll($criteria_user);
- $users_id = array();
- foreach ($users as $key => $row) {
- $users_id[] = $row->_id;
- }
- $criteria->addCond('user', 'in', $users_id);
- }
- }
- $cursor = Commision::model()->findAll($criteria);
- $rows = CommonFn::getRowsFromCursor($cursor);
- $parsedRows = Commision::model()->parse($rows);
- $total = $cursor->count();
- echo CommonFn::composeDatagridData($parsedRows, $total);
- }
- /**
- * 添加提成数据
- */
- public function actionAddCommision() {
- $datetime = Yii::app()->request->getParam('datetime', '');
- $user_name = Yii::app()->request->getParam('user_name', '');
- $user = intval(Yii::app()->request->getParam('user', -1));
- $order = Yii::app()->request->getParam('order', '');
- $commision = floatval(Yii::app()->request->getParam('commision', 0));
- $time = empty($datetime) ? time() : strtotime($datetime);
- // 保洁师处理
- // 优先根据userid查询
- if ($user != -1) {
- $userObj = User::get($user);
- if (empty($userObj)) {
- CommonFn::requestAjax(false, '查无此人', array());
- }
- } else if ($user_name != '') {
- $criteria = new EMongoCriteria();
- $criteria->name('==', $user_name);
- $userObj = User::model()->find($criteria);
- if (empty($userObj)) {
- CommonFn::requestAjax(false, '查无此人', array());
- } else {
- $user = $userObj->_id;
- }
- } else {
- CommonFn::requestAjax(false, '查无此人', array());
- }
- // 订单处理
- if ($order != '') {
- if (!CommonFn::isMongoId($order)) {
- CommonFn::requestAjax(false, '请检查订单ID', array());
- } else {
- $orderID = new MongoId($order);
- $orderObj = ROrder::get($orderID);
- if (empty($orderObj)) {
- $orderObj = AppendOrder::get($orderID);
- if (empty($orderObj)) {
- CommonFn::requestAjax(false, '订单不存在', array());
- } else {
- $type = 1;
- }
- } else {
- $type = 0;
- }
- }
- } else {
- $orderID = '';
- $type = -1;
- }
- $commisionObj = new Commision();
- $commisionObj->time = $time;
- $commisionObj->user = $user;
- $commisionObj->order = $orderID;
- $commisionObj->commision = $commision;
- $commisionObj->type = $type;
- $success = $commisionObj->insert();
- CommonFn::requestAjax($success, '', array());
- }
- /**
- * 异步查询某一保洁师时间段内提成总和
- */
- public function actionCommisionCountOne() {
- $search = Yii::app()->request->getParam('search', '');
- $start = Yii::app()->request->getParam('start', '');
- $end = Yii::app()->request->getParam('end', '');
- $type = intval(Yii::app()->request->getParam('type', 100));
- if (CommonFn::isMongoId($search)) {
- $user = User::get(new MongoId($search));
- } else {
- $user_regex = new MongoRegex('/'.$search.'/');
- $criteria = new EMongoCriteria();
- $criteria->addCond('email', 'or', $user_regex);
- $criteria->addCond('name', 'or', $user_regex);
- $cursor = User::model()->findAll($criteria);
- if ($cursor->count() == 1) {
- foreach ($cursor as $value) {
- $user = $value;
- }
- } else {
- $user = null;
- }
- }
- $data = array();
- if ($user) {
- $mongo = new MongoClient(DB_CONNETC);
- $db = $mongo->wozhua_o2o;
- $collection = $db->selectCollection('commision');
- $pipleline = array(
- array(
- '$match' => array(
- 'user' => array('$eq' => $user->_id),
- 'time' => array(
- '$gte' => strtotime($start),
- '$lt' => strtotime('+1 day', strtotime($end)),
- ),
- ),
- ),
- array(
- '$group' => array(
- '_id' => '$type',
- 'sum' => array('$sum' => '$commision'),
- ),
- ),
- );
- if ($type != 100) {
- $pipleline[0]['$match']['type'] = array('$eq' => $type);
- }
- $a = $collection->aggregate($pipleline);
- if (isset($a['result'])) {
- $sum = 0;
- foreach ($a['result'] as $key => $value) {
- $data[] = array(
- 'type' => Commision::$type_option[$value['_id']]['name'],
- 'sum' => $value['sum'],
- );
- $sum += $value['sum'];
- }
- if ($type == 100) {
- $data[] = array(
- 'type' => '全部',
- 'sum' => $sum,
- );
- }
- }
- }
- echo json_encode($data);
- }
- public function actionFixCommisionTime() {
- set_time_limit(0);
- $criteria = new EMongoCriteria();
- $criteria->type('noteq', -1);
- $cursor = Commision::model()->findAll($criteria);
- foreach ($cursor as $key => $model) {
- if (!$model->booking_time) {
- if ($model->type == 0) {
- $order = ROrder::get($model->order);
- if ($order) {
- $model->booking_time = $order->booking_time;
- $model->save();
- }
- } else if ($model->type == 1) {
- $append_order = AppendOrder::get($model->order);
- if ($append_order) {
- $order = ROrder::get($append_order->order);
- $model->booking_time = $order->booking_time;
- $model->save();
- }
- }
- } else {
- continue;
- }
- }
- echo 'ok';
- }
- }
|