CommisionController.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. /**
  3. * 提成控制器
  4. * @author zhouxuchen 2015-12-02
  5. */
  6. class CommisionController extends AdminController {
  7. public function actionIndex() {
  8. $type_option = Commision::$type_option;
  9. $type = CommonFn::getComboboxData($type_option, 100, true, 100);
  10. $this->render('index', array(
  11. 'type_option' => $type,
  12. ));
  13. }
  14. public function actionList() {
  15. $search = Yii::app()->request->getParam('search', '');
  16. $type = intval(Yii::app()->request->getParam('type', 100));
  17. $start = Yii::app()->request->getParam('start', '');
  18. $end = Yii::app()->request->getParam('end', '');
  19. $params = CommonFn::getPageParams();
  20. $criteria = new EMongoCriteria($params);
  21. // 订单类型筛选
  22. if ($type != 100) {
  23. $criteria->type('==', $type);
  24. }
  25. // 时间筛选
  26. if ($start != '') {
  27. $start_time = strtotime($start);
  28. $criteria->booking_time('>=', $start_time);
  29. }
  30. if ($end != '') {
  31. $end_time = strtotime($end);
  32. $end_time = strtotime('+1 day', $end_time);
  33. $criteria->booking_time('<', $end_time);
  34. }
  35. // 模糊搜索
  36. if ($search != '') {
  37. if (CommonFn::isMongoId($search)) {
  38. $criteria->addCond('_id', 'or', new MongoId($search));
  39. $criteria->addCond('order', 'or', new MongoId($search));
  40. } else {
  41. $user_regex = new MongoRegex('/'.$search.'/');
  42. $criteria_user = new EMongoCriteria();
  43. $criteria_user->addCond('email', 'or', $user_regex);
  44. $criteria_user->addCond('name', 'or', $user_regex);
  45. $users = User::model()->findAll($criteria_user);
  46. $users_id = array();
  47. foreach ($users as $key => $row) {
  48. $users_id[] = $row->_id;
  49. }
  50. $criteria->addCond('user', 'in', $users_id);
  51. }
  52. }
  53. $cursor = Commision::model()->findAll($criteria);
  54. $rows = CommonFn::getRowsFromCursor($cursor);
  55. $parsedRows = Commision::model()->parse($rows);
  56. $total = $cursor->count();
  57. echo CommonFn::composeDatagridData($parsedRows, $total);
  58. }
  59. /**
  60. * 添加提成数据
  61. */
  62. public function actionAddCommision() {
  63. $datetime = Yii::app()->request->getParam('datetime', '');
  64. $user_name = Yii::app()->request->getParam('user_name', '');
  65. $user = intval(Yii::app()->request->getParam('user', -1));
  66. $order = Yii::app()->request->getParam('order', '');
  67. $commision = floatval(Yii::app()->request->getParam('commision', 0));
  68. $time = empty($datetime) ? time() : strtotime($datetime);
  69. // 保洁师处理
  70. // 优先根据userid查询
  71. if ($user != -1) {
  72. $userObj = User::get($user);
  73. if (empty($userObj)) {
  74. CommonFn::requestAjax(false, '查无此人', array());
  75. }
  76. } else if ($user_name != '') {
  77. $criteria = new EMongoCriteria();
  78. $criteria->name('==', $user_name);
  79. $userObj = User::model()->find($criteria);
  80. if (empty($userObj)) {
  81. CommonFn::requestAjax(false, '查无此人', array());
  82. } else {
  83. $user = $userObj->_id;
  84. }
  85. } else {
  86. CommonFn::requestAjax(false, '查无此人', array());
  87. }
  88. // 订单处理
  89. if ($order != '') {
  90. if (!CommonFn::isMongoId($order)) {
  91. CommonFn::requestAjax(false, '请检查订单ID', array());
  92. } else {
  93. $orderID = new MongoId($order);
  94. $orderObj = ROrder::get($orderID);
  95. if (empty($orderObj)) {
  96. $orderObj = AppendOrder::get($orderID);
  97. if (empty($orderObj)) {
  98. CommonFn::requestAjax(false, '订单不存在', array());
  99. } else {
  100. $type = 1;
  101. }
  102. } else {
  103. $type = 0;
  104. }
  105. }
  106. } else {
  107. $orderID = '';
  108. $type = -1;
  109. }
  110. $commisionObj = new Commision();
  111. $commisionObj->time = $time;
  112. $commisionObj->user = $user;
  113. $commisionObj->order = $orderID;
  114. $commisionObj->commision = $commision;
  115. $commisionObj->type = $type;
  116. $success = $commisionObj->insert();
  117. CommonFn::requestAjax($success, '', array());
  118. }
  119. /**
  120. * 异步查询某一保洁师时间段内提成总和
  121. */
  122. public function actionCommisionCountOne() {
  123. $search = Yii::app()->request->getParam('search', '');
  124. $start = Yii::app()->request->getParam('start', '');
  125. $end = Yii::app()->request->getParam('end', '');
  126. $type = intval(Yii::app()->request->getParam('type', 100));
  127. if (CommonFn::isMongoId($search)) {
  128. $user = User::get(new MongoId($search));
  129. } else {
  130. $user_regex = new MongoRegex('/'.$search.'/');
  131. $criteria = new EMongoCriteria();
  132. $criteria->addCond('email', 'or', $user_regex);
  133. $criteria->addCond('name', 'or', $user_regex);
  134. $cursor = User::model()->findAll($criteria);
  135. if ($cursor->count() == 1) {
  136. foreach ($cursor as $value) {
  137. $user = $value;
  138. }
  139. } else {
  140. $user = null;
  141. }
  142. }
  143. $data = array();
  144. if ($user) {
  145. $mongo = new MongoClient(DB_CONNETC);
  146. $db = $mongo->wozhua_o2o;
  147. $collection = $db->selectCollection('commision');
  148. $pipleline = array(
  149. array(
  150. '$match' => array(
  151. 'user' => array('$eq' => $user->_id),
  152. 'time' => array(
  153. '$gte' => strtotime($start),
  154. '$lt' => strtotime('+1 day', strtotime($end)),
  155. ),
  156. ),
  157. ),
  158. array(
  159. '$group' => array(
  160. '_id' => '$type',
  161. 'sum' => array('$sum' => '$commision'),
  162. ),
  163. ),
  164. );
  165. if ($type != 100) {
  166. $pipleline[0]['$match']['type'] = array('$eq' => $type);
  167. }
  168. $a = $collection->aggregate($pipleline);
  169. if (isset($a['result'])) {
  170. $sum = 0;
  171. foreach ($a['result'] as $key => $value) {
  172. $data[] = array(
  173. 'type' => Commision::$type_option[$value['_id']]['name'],
  174. 'sum' => $value['sum'],
  175. );
  176. $sum += $value['sum'];
  177. }
  178. if ($type == 100) {
  179. $data[] = array(
  180. 'type' => '全部',
  181. 'sum' => $sum,
  182. );
  183. }
  184. }
  185. }
  186. echo json_encode($data);
  187. }
  188. public function actionFixCommisionTime() {
  189. set_time_limit(0);
  190. $criteria = new EMongoCriteria();
  191. $criteria->type('noteq', -1);
  192. $cursor = Commision::model()->findAll($criteria);
  193. foreach ($cursor as $key => $model) {
  194. if (!$model->booking_time) {
  195. if ($model->type == 0) {
  196. $order = ROrder::get($model->order);
  197. if ($order) {
  198. $model->booking_time = $order->booking_time;
  199. $model->save();
  200. }
  201. } else if ($model->type == 1) {
  202. $append_order = AppendOrder::get($model->order);
  203. if ($append_order) {
  204. $order = ROrder::get($append_order->order);
  205. $model->booking_time = $order->booking_time;
  206. $model->save();
  207. }
  208. }
  209. } else {
  210. continue;
  211. }
  212. }
  213. echo 'ok';
  214. }
  215. }