ExamineFlow.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. if ($res) {
  78. $config = $param['config'];
  79. if ((int)$config == 1) {
  80. //固定审批流
  81. $resStep = $examineStepModel->createStepData($param['step'], $res['flow_id']);
  82. if ($resStep) {
  83. return resultArray(['data' => '添加成功']);
  84. } else {
  85. db('admin_examine_flow')->where(['flow_id' => $res['flow_id']])->delete();
  86. return resultArray(['error' => $examineStepModel->getError()]);
  87. }
  88. }
  89. return resultArray(['data' => '添加成功']);
  90. } else {
  91. return resultArray(['error' => $examineFlowModel->getError()]);
  92. }
  93. }
  94. /**
  95. * 编辑审批流程
  96. *
  97. * @return \think\response\Json
  98. * @throws \think\Exception
  99. * @throws \think\db\exception\DataNotFoundException
  100. * @throws \think\db\exception\ModelNotFoundException
  101. * @throws \think\exception\DbException
  102. * @throws \think\exception\PDOException
  103. */
  104. public function update()
  105. {
  106. if (empty($this->param['types'])) return resultArray(['error' => '请选择关联对象!']);
  107. $examineFlowModel = model('ExamineFlow');
  108. $examineStepModel = model('ExamineStep');
  109. $param = $this->param;
  110. $userInfo = $this->userInfo;
  111. $param['update_user_id'] = $userInfo['id'];
  112. $param['create_time'] = time();
  113. $param['update_time'] = time();
  114. $flowId = $param['flow_id'];
  115. unset($param['flow_id']);
  116. # 验证名称是否重复
  117. $repeatWhere['name'] = $param['name'];
  118. $repeatWhere['is_deleted'] = 0;
  119. $repeatWhere['types'] = ['neq', 'oa_examine'];
  120. $repeatWhere['flow_id'] = ['neq', $flowId];
  121. if (db('admin_examine_flow')->where($repeatWhere)->value('flow_id')) return resultArray(['error' => '审批流名称重复!']);
  122. //处理
  123. $param['user_ids'] = arrayToString($param['user_ids']);
  124. $param['structure_ids'] = arrayToString($param['structure_ids']);
  125. $res = $examineFlowModel->createData($param);
  126. $param['config'] = $param['config'] ? 1 : 0;
  127. if ($res) {
  128. //将当前审批流标记为已删除,重新创建审批流(目的:保留审批流程记录)
  129. $upData = [];
  130. $upData['is_deleted'] = 1;
  131. $upData['delete_time'] = time();
  132. $upData['delete_user_id'] = $userInfo['id'];
  133. $upData['status'] = 0;
  134. db('admin_examine_flow')->where(['flow_id' => $flowId])->update($upData);
  135. $config = $param['config'];
  136. if ((int)$config == 1) {
  137. //固定审批流
  138. $resStep = $examineStepModel->createStepData($param['step'], $res['flow_id']);
  139. if ($resStep) {
  140. return resultArray(['data' => '添加成功']);
  141. } else {
  142. db('admin_examine_flow')->where(['flow_id' => $res['flow_id']])->delete();
  143. return resultArray(['error' => $examineStepModel->getError()]);
  144. }
  145. }
  146. return resultArray(['data' => '添加成功']);
  147. } else {
  148. return resultArray(['error' => $examineFlowModel->getError()]);
  149. }
  150. // $newData = db('admin_examine_flow')->where(['flow_id' => $param['flow_id']])->find();
  151. // $newData['user_ids'] = arrayToString($param['user_ids']);
  152. // $param['structure_ids'] = arrayToString($param['structure_ids']);
  153. // $param['update_user_id'] = $userInfo['id'];
  154. // $param['create_time'] = time();
  155. // $param['update_time'] = time();
  156. // $param['status'] = 1;
  157. // $resUpdate = $examineFlowModel->updateDataById($param, $param['flow_id']);
  158. //
  159. // if ($resUpdate) {
  160. // if ($param['config'] == 1) {
  161. // $resStep = $examineStepModel->createStepData($param['step'], $resUpdate['flow_id']);
  162. // if (!$resStep) {
  163. // return resultArray(['error' => $examineStepModel->getError()]);
  164. // }
  165. // }
  166. //
  167. // $upData = [];
  168. // $upData['is_deleted'] = 1;
  169. // $upData['delete_time'] = time();
  170. // $upData['delete_user_id'] = $userInfo['id'];
  171. // $upData['status'] = 0;
  172. // $resFlow = db('admin_examine_flow')->where(['flow_id' => $param['flow_id']])->update($upData);
  173. // if (!$resFlow) {
  174. // return resultArray(['error' => '编辑失败1']);
  175. // }
  176. // return resultArray(['data' => '编辑成功']);
  177. // } else {
  178. // return resultArray(['error' => '编辑失败2']);
  179. // }
  180. }
  181. /**
  182. * 审批流程详情
  183. * @author Michael_xu
  184. * @param
  185. * @return
  186. */
  187. public function read()
  188. {
  189. $examineFlowModel = model('ExamineFlow');
  190. $param = $this->param;
  191. $res = $examineFlowModel->getDataById($param['id']);
  192. if (!$res) {
  193. return resultArray(['error' => $examineFlowModel->getError()]);
  194. }
  195. return resultArray(['data' => $res]);
  196. }
  197. /**
  198. * 删除审批流程(逻辑删)
  199. * @author Michael_xu
  200. * @param
  201. * @return
  202. */
  203. public function delete()
  204. {
  205. $examineFlowModel = model('ExamineFlow');
  206. $param = $this->param;
  207. $data = $examineFlowModel->signDelById($param['flow_id']);
  208. if (!$data) {
  209. return resultArray(['error' => $examineFlowModel->getError()]);
  210. }
  211. return resultArray(['data' => '删除成功']);
  212. }
  213. /**
  214. * 审批流程状态
  215. * @author Michael_xu
  216. * @param ids array
  217. * @param status 1启用,0禁用
  218. * @return
  219. */
  220. public function enables()
  221. {
  222. $examineFlowModel = model('ExamineFlow');
  223. $param = $this->param;
  224. $id = [$param['flow_id']];
  225. $data = $examineFlowModel->enableDatas($id, $param['status']);
  226. if (!$data) {
  227. return resultArray(['error' => $examineFlowModel->getError()]);
  228. }
  229. return resultArray(['data' => '操作成功']);
  230. }
  231. /**
  232. * 完整审批步骤(固定审批流)
  233. * @author Michael_xu
  234. * @param flow_id 审批流ID
  235. * @param user_id 审批对象创建人ID
  236. * @return
  237. */
  238. public function stepList()
  239. {
  240. $param = $this->param;
  241. $userInfo = $this->userInfo;
  242. $examineStepModel = model('ExamineStep');
  243. $examineFlowModel = model('ExamineFlow');
  244. $check_user_id = $userInfo['id'];
  245. $flow_id = $param['flow_id'];
  246. $types = $param['types'];
  247. $types_id = $param['types_id'];
  248. $typesArr = ['crm_contract', 'crm_receivables', 'crm_invoice', 'oa_examine'];
  249. if (!$types || !in_array($types, $typesArr)) {
  250. return resultArray(['error' => '参数错误']);
  251. }
  252. if ($flow_id) {
  253. $examineFlowData = $examineFlowModel->getDataById($param['flow_id']);
  254. if (!$examineFlowData) {
  255. return resultArray(['error' => '参数错误']);
  256. }
  257. $typesInfo = $examineStepModel->getDataByTypes($types, $types_id);
  258. $user_id = $typesInfo['dataInfo']['owner_user_id'];
  259. if ($types == 'oa_examine') {
  260. $user_id = $typesInfo['dataInfo']['create_user_id'];
  261. }
  262. if (!$user_id) {
  263. return resultArray(['error' => '参数错误']);
  264. }
  265. } else {
  266. $user_id = $check_user_id;
  267. // 获取符合条件的审批流
  268. $examineFlowData = $examineFlowModel->getFlowByTypes($user_id, $types, $types_id);
  269. if (!$examineFlowData) {
  270. # 合同、回款、发票模块审批流停用
  271. if (in_array($types, ['crm_contract', 'crm_receivables', 'crm_invoice'])) {
  272. return resultArray(['data' => ['examineStatus' => false]]);
  273. }
  274. return resultArray(['error' => '无可用审批流,请联系管理员']);
  275. }
  276. $flow_id = $examineFlowData['flow_id'];
  277. }
  278. if ($types == 'oa_examine') {
  279. $category_id = db('oa_examine')->where(['examine_id' => $types_id])->value('category_id');
  280. }
  281. $list=[];
  282. //自选还是流程(1固定,0自选)
  283. if ($examineFlowData['config'] == 1) {
  284. //获取审批流程
  285. $stepInfo = $examineStepModel->getStepList($flow_id, $user_id, $types, $types_id, $check_user_id, $param['action'], $category_id);
  286. $stepList = $stepInfo['steplist'];
  287. $list=$stepInfo['steplist'][0]['user_id_info'];
  288. // foreach ($stepInfo['steplist'] as $k=>$v){
  289. // $data['user_id_info'][]=$v['user_id_info'];
  290. // }
  291. } else {
  292. $stepInfo = $examineStepModel->getPerStepList($types, $types_id, $user_id, $check_user_id, $param['action']);
  293. $stepList = $stepInfo['steplist'];
  294. $list=$stepInfo['steplist'][0]['user_id_info'];
  295. // foreach ($stepInfo['steplist'] as $k=>$v){
  296. // $data['user_id_info'][]=$stepInfo['steplist']['user_id_info'];
  297. // }
  298. }
  299. $data = [];
  300. $data['config'] = (int) $examineFlowData['config']; //1固定,0自选
  301. $data['stepList'] = $stepList ? : [];
  302. $data['examine_user'] = $list ? : [];
  303. $data['is_check'] = $stepInfo['is_check'] ? : 0;
  304. $data['is_recheck'] = $stepInfo['is_recheck'] ? : 0;
  305. $data['examineStatus'] = true;
  306. return resultArray(['data' => $data]);
  307. }
  308. /**
  309. * 自选审批人列表(授权审批类型)
  310. * @author Michael_xu
  311. * @param types 类型
  312. * @return
  313. */
  314. public function userList()
  315. {
  316. $param = $this->param;
  317. $userInfo = $this->userInfo;
  318. $types = $param['types'];
  319. $examineStepModel = model('ExamineStep');
  320. $userModel = model('User');
  321. // $examine_user_ids = $examineStepModel->getUserByPer($types);
  322. //暂定返回全部
  323. $examine_user_ids = getSubUserId(true, 1);
  324. $where = [];
  325. $where['user.id'] = array('in',$examine_user_ids);
  326. $where['status'] = ['gt',0];
  327. $where['pageType'] = 'all';
  328. $userList = $userModel->getDataList($where);
  329. return resultArray(['data' => $userList['list']]);
  330. }
  331. /**
  332. * 审批记录
  333. * @author Michael_xu
  334. * @param types 类型
  335. * @return
  336. */
  337. public function recordList()
  338. {
  339. $param = $this->param;
  340. $userInfo = $this->userInfo;
  341. $examineRecordModel = model('ExamineRecord');
  342. $list = $examineRecordModel->getDataList($param) ? : [];
  343. return resultArray(['data' => $list]);
  344. }
  345. }