ExamineFlow.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 审批流程
  4. // +----------------------------------------------------------------------
  5. // | Author: Michael_xu | gengxiaoxu@5kcrm.com
  6. // +----------------------------------------------------------------------
  7. namespace app\admin\controller;
  8. use think\Hook;
  9. use think\Request;
  10. use think\Db;
  11. class ExamineFlow extends ApiCommon
  12. {
  13. /**
  14. * 用于判断权限
  15. * @permission 无限制
  16. * @allow 登录用户可访问
  17. * @other 其他根据系统设置
  18. **/
  19. public function _initialize()
  20. {
  21. $action = [
  22. 'permission'=>[],
  23. 'allow'=>['index','save','update','read','delete','enables','steplist','userlist','recordlist']
  24. ];
  25. Hook::listen('check_auth',$action);
  26. $request = Request::instance();
  27. $a = strtolower($request->action());
  28. if (!in_array($a, $action['permission'])) {
  29. parent::_initialize();
  30. }
  31. //权限判断
  32. $unAction = ['steplist','userlist','recordlist'];
  33. if (!in_array($a, $unAction) && !checkPerByAction('admin', 'examine_flow', 'index')) {
  34. header('Content-Type:application/json; charset=utf-8');
  35. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  36. }
  37. }
  38. /**
  39. * 审批流程列表
  40. * @author Michael_xu
  41. * @return
  42. */
  43. public function index()
  44. {
  45. $examineFlowModel = model('ExamineFlow');
  46. $param = $this->param;
  47. //过滤审批类型中关联的审批流
  48. $param['types'] = ['neq','oa_examine'];
  49. $data = $examineFlowModel->getDataList($param);
  50. return resultArray(['data' => $data]);
  51. }
  52. /**
  53. * 添加审批流程
  54. *
  55. * @return \think\response\Json
  56. * @throws \think\Exception
  57. * @throws \think\exception\PDOException
  58. */
  59. public function save()
  60. {
  61. if (empty($this->param['types'])) return resultArray(['error' => '请选择关联对象!']);
  62. $examineFlowModel = model('ExamineFlow');
  63. $examineStepModel = model('ExamineStep');
  64. $param = $this->param;
  65. $userInfo = $this->userInfo;
  66. $param['update_user_id'] = $userInfo['id'];
  67. # 验证名称是否重复
  68. $repeatWhere['name'] = $param['name'];
  69. $repeatWhere['is_deleted'] = 0;
  70. $repeatWhere['types'] = ['neq', 'oa_examine'];
  71. if (db('admin_examine_flow')->where($repeatWhere)->value('flow_id')) return resultArray(['error' => '审批流名称重复!']);
  72. //处理
  73. $param['user_ids'] = arrayToString($param['user_ids']);
  74. $param['structure_ids'] = arrayToString($param['structure_ids']);
  75. $res = $examineFlowModel->createData($param);
  76. $param['config'] = $param['config'] ? 1 : 0;
  77. SystemActionLog($userInfo['id'], 'admin_examine','approval', $res['flow_id'], 'save', $param['name'], '', '','添加了审批流:'.$param['name']);
  78. if ($res) {
  79. $config = $param['config'];
  80. if ((int)$config == 1) {
  81. //固定审批流
  82. $resStep = $examineStepModel->createStepData($param['step'], $res['flow_id']);
  83. if ($resStep) {
  84. return resultArray(['data' => '添加成功']);
  85. } else {
  86. db('admin_examine_flow')->where(['flow_id' => $res['flow_id']])->delete();
  87. return resultArray(['error' => $examineStepModel->getError()]);
  88. }
  89. }
  90. return resultArray(['data' => '添加成功']);
  91. } else {
  92. return resultArray(['error' => $examineFlowModel->getError()]);
  93. }
  94. }
  95. /**
  96. * 编辑审批流程
  97. *
  98. * @return \think\response\Json
  99. * @throws \think\Exception
  100. * @throws \think\db\exception\DataNotFoundException
  101. * @throws \think\db\exception\ModelNotFoundException
  102. * @throws \think\exception\DbException
  103. * @throws \think\exception\PDOException
  104. */
  105. public function update()
  106. {
  107. if (empty($this->param['types'])) return resultArray(['error' => '请选择关联对象!']);
  108. $examineFlowModel = model('ExamineFlow');
  109. $examineStepModel = model('ExamineStep');
  110. $param = $this->param;
  111. $userInfo = $this->userInfo;
  112. $param['update_user_id'] = $userInfo['id'];
  113. $param['create_time'] = time();
  114. $param['update_time'] = time();
  115. $flowId = $param['flow_id'];
  116. unset($param['flow_id']);
  117. # 验证名称是否重复
  118. $repeatWhere['name'] = $param['name'];
  119. $repeatWhere['is_deleted'] = 0;
  120. $repeatWhere['types'] = ['neq', 'oa_examine'];
  121. $repeatWhere['flow_id'] = ['neq', $flowId];
  122. if (db('admin_examine_flow')->where($repeatWhere)->value('flow_id')) return resultArray(['error' => '审批流名称重复!']);
  123. //处理
  124. $param['user_ids'] = arrayToString($param['user_ids']);
  125. $param['structure_ids'] = arrayToString($param['structure_ids']);
  126. $res = $examineFlowModel->createData($param);
  127. $param['config'] = $param['config'] ? 1 : 0;
  128. if ($res) {
  129. //将当前审批流标记为已删除,重新创建审批流(目的:保留审批流程记录)
  130. $upData = [];
  131. $upData['is_deleted'] = 1;
  132. $upData['delete_time'] = time();
  133. $upData['delete_user_id'] = $userInfo['id'];
  134. $upData['status'] = 0;
  135. db('admin_examine_flow')->where(['flow_id' => $flowId])->update($upData);
  136. # 添加系统操作日志
  137. SystemActionLog($userInfo['id'], 'admin_examine','approval', $flowId, 'update', $param['name'], '', '','编辑了审批流:'.$param['name']);
  138. $config = $param['config'];
  139. if ((int)$config == 1) {
  140. //固定审批流
  141. $resStep = $examineStepModel->createStepData($param['step'], $res['flow_id']);
  142. if ($resStep) {
  143. return resultArray(['data' => '添加成功']);
  144. } else {
  145. db('admin_examine_flow')->where(['flow_id' => $res['flow_id']])->delete();
  146. return resultArray(['error' => $examineStepModel->getError()]);
  147. }
  148. }
  149. return resultArray(['data' => '添加成功']);
  150. } else {
  151. return resultArray(['error' => $examineFlowModel->getError()]);
  152. }
  153. // $newData = db('admin_examine_flow')->where(['flow_id' => $param['flow_id']])->find();
  154. // $newData['user_ids'] = arrayToString($param['user_ids']);
  155. // $param['structure_ids'] = arrayToString($param['structure_ids']);
  156. // $param['update_user_id'] = $userInfo['id'];
  157. // $param['create_time'] = time();
  158. // $param['update_time'] = time();
  159. // $param['status'] = 1;
  160. // $resUpdate = $examineFlowModel->updateDataById($param, $param['flow_id']);
  161. //
  162. // if ($resUpdate) {
  163. // if ($param['config'] == 1) {
  164. // $resStep = $examineStepModel->createStepData($param['step'], $resUpdate['flow_id']);
  165. // if (!$resStep) {
  166. // return resultArray(['error' => $examineStepModel->getError()]);
  167. // }
  168. // }
  169. //
  170. // $upData = [];
  171. // $upData['is_deleted'] = 1;
  172. // $upData['delete_time'] = time();
  173. // $upData['delete_user_id'] = $userInfo['id'];
  174. // $upData['status'] = 0;
  175. // $resFlow = db('admin_examine_flow')->where(['flow_id' => $param['flow_id']])->update($upData);
  176. // if (!$resFlow) {
  177. // return resultArray(['error' => '编辑失败1']);
  178. // }
  179. // return resultArray(['data' => '编辑成功']);
  180. // } else {
  181. // return resultArray(['error' => '编辑失败2']);
  182. // }
  183. }
  184. /**
  185. * 审批流程详情
  186. * @author Michael_xu
  187. * @param
  188. * @return
  189. */
  190. public function read()
  191. {
  192. $examineFlowModel = model('ExamineFlow');
  193. $param = $this->param;
  194. $res = $examineFlowModel->getDataById($param['id']);
  195. if (!$res) {
  196. return resultArray(['error' => $examineFlowModel->getError()]);
  197. }
  198. return resultArray(['data' => $res]);
  199. }
  200. /**
  201. * 删除审批流程(逻辑删)
  202. * @author Michael_xu
  203. * @param
  204. * @return
  205. */
  206. public function delete()
  207. {
  208. $examineFlowModel = model('ExamineFlow');
  209. $param = $this->param;
  210. $userInfo = $this->userInfo;
  211. $data = $examineFlowModel->signDelById($param['flow_id']);
  212. if (!$data) {
  213. return resultArray(['error' => $examineFlowModel->getError()]);
  214. }
  215. # 系统操作日志
  216. $newData = db('admin_examine_flow')->where(['flow_id' => $param['flow_id']])->find();
  217. SystemActionLog($userInfo['id'], 'admin_examine','approval', $param['flow_id'], 'update', $newData['name'], '', '','删除了审批流:'.$newData['name']);
  218. return resultArray(['data' => '删除成功']);
  219. }
  220. /**
  221. * 审批流程状态
  222. * @author Michael_xu
  223. * @param ids array
  224. * @param status 1启用,0禁用
  225. * @return
  226. */
  227. public function enables()
  228. {
  229. $examineFlowModel = model('ExamineFlow');
  230. $param = $this->param;
  231. $userInfo=$this->userInfo;
  232. $id = [$param['flow_id']];
  233. $data = $examineFlowModel->enableDatas($id, $param['status']);
  234. # 系统操作日志
  235. if (!$data) {
  236. return resultArray(['error' => $examineFlowModel->getError()]);
  237. }
  238. if($param['status']==0){
  239. $content='禁用了:';
  240. }else{
  241. $content='启用了:';
  242. }
  243. $dataInfo=db('admin_examine_flow')->where('flow_id',$param['flow_id'])->find();
  244. // p(3333);
  245. SystemActionLog($userInfo['id'], 'admin_examine','approval', $param['flow_id'], 'update', $dataInfo['name'], '', '',$content.$dataInfo['name']);
  246. return resultArray(['data' => '操作成功']);
  247. }
  248. /**
  249. * 完整审批步骤(固定审批流)
  250. * @author Michael_xu
  251. * @param flow_id 审批流ID
  252. * @param user_id 审批对象创建人ID
  253. * @return
  254. */
  255. public function stepList()
  256. {
  257. $param = $this->param;
  258. $userInfo = $this->userInfo;
  259. $data= $this->checkFlow($param,$userInfo);
  260. return resultArray(['data' => $data]);
  261. }
  262. /**
  263. * 固定审批流审批流程人员数据
  264. * @param $param
  265. * @param $userInfo
  266. *
  267. * @author alvin guogaobo
  268. * @version 1.0 版本号
  269. * @since 2021/3/15 0015 13:37
  270. */
  271. public function checkFlow($param,$userInfo){
  272. $examineStepModel = model('ExamineStep');
  273. $examineFlowModel = model('ExamineFlow');
  274. $check_user_id = $userInfo['id'];
  275. $flow_id = $param['flow_id'];
  276. $types = $param['types'];
  277. $types_id = $param['types_id'];
  278. $typesArr = ['crm_contract', 'crm_receivables', 'crm_invoice', 'oa_examine'];
  279. if (!$types || !in_array($types, $typesArr)) {
  280. return resultArray(['error' => '参数错误']);
  281. }
  282. if ($flow_id) {
  283. $examineFlowData = $examineFlowModel->getDataById($param['flow_id']);
  284. if (!$examineFlowData) {
  285. return resultArray(['error' => '参数错误']);
  286. }
  287. $typesInfo = $examineStepModel->getDataByTypes($types, $types_id);
  288. $user_id = $typesInfo['dataInfo']['owner_user_id'];
  289. if ($types == 'oa_examine') {
  290. $user_id = $typesInfo['dataInfo']['create_user_id'];
  291. }
  292. if (!$user_id) {
  293. return resultArray(['error' => '参数错误']);
  294. }
  295. } else {
  296. $user_id = $check_user_id;
  297. // 获取符合条件的审批流
  298. $examineFlowData = $examineFlowModel->getFlowByTypes($user_id, $types, $types_id);
  299. if (!$examineFlowData) {
  300. # 合同、回款、发票模块审批流停用
  301. if (in_array($types, ['crm_contract', 'crm_receivables', 'crm_invoice'])) {
  302. return resultArray(['data' => ['examineStatus' => false]]);
  303. }
  304. return resultArray(['error' => '无可用审批流,请联系管理员']);
  305. }
  306. $flow_id = $examineFlowData['flow_id'];
  307. }
  308. if ($types == 'oa_examine') {
  309. $category_id = db('oa_examine')->where(['examine_id' => $types_id])->value('category_id');
  310. }
  311. $list=[];
  312. //自选还是流程(1固定,0自选)
  313. if ($examineFlowData['config'] == 1) {
  314. //获取审批流程
  315. $stepInfo = $examineStepModel->getStepList($flow_id, $user_id, $types, $types_id, $check_user_id, $param['action'], $category_id);
  316. $stepList = $stepInfo['steplist'];
  317. $list=$stepInfo['steplist'][0]['user_id_info'];
  318. // foreach ($stepInfo['steplist'] as $k=>$v){
  319. // $data['user_id_info'][]=$v['user_id_info'];
  320. // }
  321. } else {
  322. $stepInfo = $examineStepModel->getPerStepList($types, $types_id, $user_id, $check_user_id, $param['action']);
  323. $stepList = $stepInfo['steplist'];
  324. $list=$stepInfo['steplist'][0]['user_id_info'];
  325. // foreach ($stepInfo['steplist'] as $k=>$v){
  326. // $data['user_id_info'][]=$stepInfo['steplist']['user_id_info'];
  327. // }
  328. }
  329. $data = [];
  330. $data['config'] = (int) $examineFlowData['config']; //1固定,0自选
  331. $data['stepList'] = $stepList ? : [];
  332. $data['examine_user'] = $list ? : [];
  333. $data['is_check'] = $stepInfo['is_check'] ? : 0;
  334. $data['is_recheck'] = $stepInfo['is_recheck'] ? : 0;
  335. $data['examineStatus'] = true;
  336. return $data;
  337. }
  338. /**
  339. * 自选审批人列表(授权审批类型)
  340. * @author Michael_xu
  341. * @param types 类型
  342. * @return
  343. */
  344. public function userList()
  345. {
  346. $param = $this->param;
  347. $userInfo = $this->userInfo;
  348. $types = $param['types'];
  349. $examineStepModel = model('ExamineStep');
  350. $userModel = model('User');
  351. // $examine_user_ids = $examineStepModel->getUserByPer($types);
  352. //暂定返回全部
  353. $examine_user_ids = getSubUserId(true, 1);
  354. $where = [];
  355. $where['user.id'] = array('in',$examine_user_ids);
  356. $where['status'] = ['gt',0];
  357. $where['pageType'] = 'all';
  358. $userList = $userModel->getDataList($where);
  359. return resultArray(['data' => $userList['list']]);
  360. }
  361. /**
  362. * 审批记录
  363. * @author Michael_xu
  364. * @param types 类型
  365. * @return
  366. */
  367. public function recordList()
  368. {
  369. $examineRecordModel = model('ExamineRecord');
  370. $data = $examineRecordModel->getDataList($this->param);
  371. return resultArray(['data' => $data]);
  372. }
  373. }