ExamineFlow.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. $data= $this->checkFlow($param,$userInfo);
  243. return resultArray(['data' => $data]);
  244. }
  245. /**
  246. * 固定审批流审批流程人员数据
  247. * @param $param
  248. * @param $userInfo
  249. *
  250. * @author alvin guogaobo
  251. * @version 1.0 版本号
  252. * @since 2021/3/15 0015 13:37
  253. */
  254. public function checkFlow($param,$userInfo){
  255. $examineStepModel = model('ExamineStep');
  256. $examineFlowModel = model('ExamineFlow');
  257. $check_user_id = $userInfo['id'];
  258. $flow_id = $param['flow_id'];
  259. $types = $param['types'];
  260. $types_id = $param['types_id'];
  261. $typesArr = ['crm_contract', 'crm_receivables', 'crm_invoice', 'oa_examine'];
  262. if (!$types || !in_array($types, $typesArr)) {
  263. return resultArray(['error' => '参数错误']);
  264. }
  265. if ($flow_id) {
  266. $examineFlowData = $examineFlowModel->getDataById($param['flow_id']);
  267. if (!$examineFlowData) {
  268. return resultArray(['error' => '参数错误']);
  269. }
  270. $typesInfo = $examineStepModel->getDataByTypes($types, $types_id);
  271. $user_id = $typesInfo['dataInfo']['owner_user_id'];
  272. if ($types == 'oa_examine') {
  273. $user_id = $typesInfo['dataInfo']['create_user_id'];
  274. }
  275. if (!$user_id) {
  276. return resultArray(['error' => '参数错误']);
  277. }
  278. } else {
  279. $user_id = $check_user_id;
  280. // 获取符合条件的审批流
  281. $examineFlowData = $examineFlowModel->getFlowByTypes($user_id, $types, $types_id);
  282. if (!$examineFlowData) {
  283. # 合同、回款、发票模块审批流停用
  284. if (in_array($types, ['crm_contract', 'crm_receivables', 'crm_invoice'])) {
  285. return resultArray(['data' => ['examineStatus' => false]]);
  286. }
  287. return resultArray(['error' => '无可用审批流,请联系管理员']);
  288. }
  289. $flow_id = $examineFlowData['flow_id'];
  290. }
  291. if ($types == 'oa_examine') {
  292. $category_id = db('oa_examine')->where(['examine_id' => $types_id])->value('category_id');
  293. }
  294. $list=[];
  295. //自选还是流程(1固定,0自选)
  296. if ($examineFlowData['config'] == 1) {
  297. //获取审批流程
  298. $stepInfo = $examineStepModel->getStepList($flow_id, $user_id, $types, $types_id, $check_user_id, $param['action'], $category_id);
  299. $stepList = $stepInfo['steplist'];
  300. $list=$stepInfo['steplist'][0]['user_id_info'];
  301. // foreach ($stepInfo['steplist'] as $k=>$v){
  302. // $data['user_id_info'][]=$v['user_id_info'];
  303. // }
  304. } else {
  305. $stepInfo = $examineStepModel->getPerStepList($types, $types_id, $user_id, $check_user_id, $param['action']);
  306. $stepList = $stepInfo['steplist'];
  307. $list=$stepInfo['steplist'][0]['user_id_info'];
  308. // foreach ($stepInfo['steplist'] as $k=>$v){
  309. // $data['user_id_info'][]=$stepInfo['steplist']['user_id_info'];
  310. // }
  311. }
  312. $data = [];
  313. $data['config'] = (int) $examineFlowData['config']; //1固定,0自选
  314. $data['stepList'] = $stepList ? : [];
  315. $data['examine_user'] = $list ? : [];
  316. $data['is_check'] = $stepInfo['is_check'] ? : 0;
  317. $data['is_recheck'] = $stepInfo['is_recheck'] ? : 0;
  318. $data['examineStatus'] = true;
  319. return $data;
  320. }
  321. /**
  322. * 自选审批人列表(授权审批类型)
  323. * @author Michael_xu
  324. * @param types 类型
  325. * @return
  326. */
  327. public function userList()
  328. {
  329. $param = $this->param;
  330. $userInfo = $this->userInfo;
  331. $types = $param['types'];
  332. $examineStepModel = model('ExamineStep');
  333. $userModel = model('User');
  334. // $examine_user_ids = $examineStepModel->getUserByPer($types);
  335. //暂定返回全部
  336. $examine_user_ids = getSubUserId(true, 1);
  337. $where = [];
  338. $where['user.id'] = array('in',$examine_user_ids);
  339. $where['status'] = ['gt',0];
  340. $where['pageType'] = 'all';
  341. $userList = $userModel->getDataList($where);
  342. return resultArray(['data' => $userList['list']]);
  343. }
  344. /**
  345. * 审批记录
  346. * @author Michael_xu
  347. * @param types 类型
  348. * @return
  349. */
  350. public function recordList()
  351. {
  352. $examineRecordModel = model('ExamineRecord');
  353. $data = $examineRecordModel->getDataList($this->param);
  354. return resultArray(['data' => $data]);
  355. }
  356. }