Flow.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. namespace addons\qingdong\model;
  3. use addons\qingdong\model\Staff as StaffModel;
  4. use app\admin\model\AuthGroup;
  5. use think\Model;
  6. use traits\model\SoftDelete;
  7. /**
  8. * 审批流程设置
  9. */
  10. class Flow extends Model
  11. {
  12. use SoftDelete;
  13. const CONTRACT_STATUS='contract';
  14. const RECEIVABLES_STATUS='receivables';
  15. const CONSUME_STATUS='consume';
  16. const ACHIEVEMENT_STATUS='achievement';
  17. const APPROVAL_STATUS='approval';
  18. const CARD_STATUS='card';//补卡
  19. const LEAVE_STATUS='leave';//请假
  20. // 表名,不含前缀
  21. protected $name = 'qingdong_flow';
  22. // 开启自动写入时间戳字段
  23. protected $autoWriteTimestamp = 'int';
  24. // 定义时间戳字段名
  25. protected $createTime = 'createtime';
  26. protected $updateTime = 'updatetime';
  27. protected $deleteTime = 'deletetime';
  28. // 追加属性
  29. protected $append = [
  30. 'groups_text',
  31. 'last_admin_text',
  32. ];
  33. /**
  34. * 角色组
  35. */
  36. public function getGroupsTextAttr($value, $data)
  37. {
  38. $group_ids = $data['group_ids'] ?? "";
  39. if(empty($group_ids)){
  40. return '全公司';
  41. }
  42. $groups = AuthGroup::where(['id' => ['in', $group_ids]])->field('name')->select();
  43. $names=[];
  44. foreach ($groups as $v){
  45. $names[]=$v['name'];
  46. }
  47. return implode(',',$names);
  48. }
  49. /**
  50. * 最后修改人
  51. */
  52. public function getLastAdminTextAttr($value,$data){
  53. $admin_id=$data['last_admin_id']??'';
  54. return \app\admin\model\Admin::where(['id'=>$admin_id])->value('nickname');
  55. }
  56. /**
  57. * 获取审批信息
  58. * @param $type
  59. * @return array|mixed
  60. */
  61. protected static function getFlowByTypes($type)
  62. {
  63. $staff = Staff::info();
  64. $type=explode('_',$type);
  65. if ($type[0] == self::APPROVAL_STATUS) {
  66. $flow = FormApproval::where(['id' => $type[1]])->find();
  67. return $flow;
  68. }
  69. $group_ids = $staff->group_ids;
  70. $group_ids = explode(',', $group_ids);
  71. $flows = Flow::where(['relation_type' => $type[0]])->order('id desc')->select();
  72. $flows = collection($flows)->toArray();
  73. $flow = [];
  74. foreach ($flows as $v) {
  75. if(empty($v['group_ids'])){
  76. $flow = $v;
  77. break;
  78. }
  79. $ids = explode(',', $v['group_ids']);
  80. if (array_intersect($group_ids, $ids)) {
  81. $flow = $v;
  82. break;
  83. }
  84. }
  85. return $flow;
  86. }
  87. /**
  88. * 获取审批
  89. * @param $type
  90. * @return array|false
  91. */
  92. public static function getsteplist($type){
  93. $examineFlowData = Flow::getFlowByTypes($type);
  94. if (!$examineFlowData) {
  95. //无可用审批流,请联系管理员
  96. return false;
  97. }
  98. $staff = Staff::info();
  99. $flow_staff_ids=[];
  100. //自选还是流程(1固定,0自选)
  101. if ($examineFlowData['status'] == 1) {
  102. $examine_ids = json_decode($examineFlowData['examine_ids'], true);
  103. if (!$examine_ids) {
  104. //无可用审批流,请联系管理员
  105. return false;
  106. }
  107. $pid = StaffModel::where(['id' => $staff->id])->value('parent_id');
  108. $stepList = [];
  109. foreach ($examine_ids as $ks => $vs) {
  110. $stepInfo = [];
  111. $stepInfo['order_id'] = $ks + 1;//第几级
  112. if ($vs['stafftype'] == 3) {//直属上级
  113. $s = StaffModel::where(['id' => $pid])->field('id,name,img')->select();
  114. $s = collection($s)->toArray();
  115. $stepInfo['status'] = $vs['stafftype'];
  116. $stepInfo['stafflist'] = $s;
  117. $stepInfo['staffids'] = $pid;
  118. $stepList[] = $stepInfo;
  119. $flow_staff_ids[]=$pid;
  120. } else if ($vs['stafftype'] == 1) {//指定员工(任意一人)
  121. $s = StaffModel::where(['id' => ['in', $vs['staff_id']]])->field('id,name,img')->select();
  122. $s = collection($s)->toArray();
  123. $stepInfo['status'] = $vs['stafftype'];
  124. $stepInfo['stafflist'] = $s;
  125. $stepInfo['staffids'] = $vs['staff_id'];
  126. $stepInfo['relation'] = 2;
  127. $stepList[] = $stepInfo;
  128. $flow_staff_ids[]=$vs['staff_id'];
  129. } else {//指定员工(并签)
  130. $s = StaffModel::where(['id' => ['in', $vs['staff_id']]])->field('id,name,img')->select();
  131. $s = collection($s)->toArray();
  132. $stepInfo['status'] = $vs['stafftype'];
  133. $stepInfo['staffids'] = $vs['staff_id'];
  134. $stepInfo['stafflist'] = $s;
  135. $stepInfo['relation'] = 1;
  136. $stepList[] = $stepInfo;
  137. $flow_staff_ids[]=$vs['staff_id'];
  138. }
  139. }
  140. } else {
  141. $stepList = [];
  142. }
  143. $data = [];
  144. $data['flow_staff_ids'] = implode(',',$flow_staff_ids); //审批id
  145. $data['flow_id'] = $examineFlowData['id']; //审批id
  146. $data['order_id']=$stepList?$stepList[0]['order_id']:0;
  147. $data['status'] = $examineFlowData['status']; //1固定,0自选
  148. $data['stepList'] = $stepList ?: [];//审批流程
  149. return $data;
  150. }
  151. /**
  152. * 获取审批详情
  153. * @param $type
  154. * @return array|false
  155. */
  156. public static function getstepdetail($relation_type,$relation_id){
  157. switch ($relation_type){
  158. case self::CONTRACT_STATUS:
  159. $model=new Contract();
  160. break;
  161. case self::RECEIVABLES_STATUS:
  162. $model=new Receivables();
  163. break;
  164. case self::CONSUME_STATUS:
  165. $model=new Consume();
  166. break;
  167. case self::ACHIEVEMENT_STATUS:
  168. $model=new AchievementRecords();
  169. break;
  170. case self::APPROVAL_STATUS:
  171. $model=new Approval();
  172. break;
  173. case self::CARD_STATUS:
  174. $model=new AttendanceCard();
  175. break;
  176. case self::LEAVE_STATUS:
  177. $model=new Leave();
  178. break;
  179. }
  180. $row=$model->get($relation_id);
  181. if(empty($row)){
  182. return false;
  183. }
  184. if($relation_type==self::APPROVAL_STATUS){
  185. $examineFlowData = FormApproval::withTrashed()->where(['id'=>$row->formapproval_id])->find();
  186. }else{
  187. $examineFlowData = Flow::withTrashed()->where(['id'=>$row->flow_id])->find();
  188. }
  189. if (!$examineFlowData) {
  190. //无可用审批流,请联系管理员
  191. return false;
  192. }
  193. $staff_id=$row->create_staff_id??($row->staff_id??$row->owner_staff_id);
  194. $records=ExamineRecord::where(['relation_type'=>$relation_type,
  195. 'relation_id'=>$relation_id])->field('check_time,status,content,check_staff_id')->select();
  196. $records=collection($records)->toArray();
  197. $examineRecord=[];
  198. foreach ($records as $v){
  199. $examineRecord[$v['check_staff_id']]=$v;
  200. }
  201. $examine_ids = json_decode($examineFlowData['examine_ids'], true);
  202. //自选还是流程(1固定,0自选)
  203. if ($examineFlowData['status'] == 1) {
  204. $pid = StaffModel::where(['id' => $staff_id])->value('parent_id');
  205. $stepList = [];
  206. foreach ($examine_ids as $ks => $vs) {
  207. $stepInfo = [];
  208. $stepInfo['order_id'] = $ks + 1;//第几级
  209. if ($vs['stafftype'] == 3) {//直属上级
  210. $s = StaffModel::where(['id' => $pid])->field('id,name,img')->select();
  211. $s = collection($s)->toArray();
  212. $stepInfo['status'] = $vs['stafftype'];
  213. $stepInfo['stafflist'] = $s;
  214. $stepInfo['staffids'] = $pid;
  215. } else if ($vs['stafftype'] == 1) {//指定员工(任意一人)
  216. $s = StaffModel::where(['id' => ['in', $vs['staff_id']]])->field('id,name,img')->select();
  217. $s = collection($s)->toArray();
  218. $stepInfo['status'] = $vs['stafftype'];
  219. $stepInfo['stafflist'] = $s;
  220. $stepInfo['staffids'] = $vs['staff_id'];
  221. $stepInfo['relation'] = 2;
  222. } else {//指定员工(并签)
  223. $s = StaffModel::where(['id' => ['in', $vs['staff_id']]])->field('id,name,img')->select();
  224. $s = collection($s)->toArray();
  225. $stepInfo['status'] = $vs['stafftype'];
  226. $stepInfo['staffids'] = $vs['staff_id'];
  227. $stepInfo['stafflist'] = $s;
  228. $stepInfo['relation'] = 1;
  229. }
  230. //审批记录
  231. foreach ($stepInfo['stafflist'] as &$staffinfo) {
  232. if (isset($examineRecord[$staffinfo['id']])) {
  233. $staffinfo['examine_reord'] = $examineRecord[$staffinfo['id']];
  234. } else {
  235. $staffinfo['examine_reord'] = ['status' => 0, 'check_time' => '', 'content' => ''];
  236. }
  237. }
  238. $stepList[] = $stepInfo;
  239. }
  240. } else {
  241. $stepList = [];
  242. $s = StaffModel::where(['id' => ['in', $row['flow_staff_ids']]])->field('id,name,img')->select();
  243. $s = collection($s)->toArray();
  244. $stepInfo['staffids'] = $row['flow_staff_ids'];
  245. $stepInfo['stafflist'] = $s;
  246. //审批记录
  247. foreach ($stepInfo['stafflist'] as &$staffinfo) {
  248. if (isset($examineRecord[$staffinfo['id']])) {
  249. $staffinfo['examine_reord'] = $examineRecord[$staffinfo['id']];
  250. } else {
  251. $staffinfo['examine_reord'] = ['status' => 0, 'check_time' => '', 'content' => ''];
  252. }
  253. }
  254. $stepList[] = $stepInfo;
  255. }
  256. $is_check=0;
  257. $staff=Staff::info();
  258. if(isset($examineRecord[$staff->id])){
  259. if($examineRecord[$staff->id]['status'] == 0){
  260. $is_check=1;
  261. }
  262. }
  263. $data = [];
  264. $data['flow_id'] = $examineFlowData['id']; //审批id
  265. $data['flow_staff_ids'] = $row['flow_staff_ids'];
  266. $data['order_id'] = $row['order_id'];
  267. $data['status'] = $examineFlowData['status']; //1固定,0自选
  268. $data['stepList'] = $stepList ?: [];//审批流程
  269. $data['is_check'] = $is_check;//审批权限(1有)
  270. $data['is_recheck'] = ($staff->id == $staff_id)?1:0;//撤销审批权限(1有)
  271. return $data;
  272. }
  273. /**
  274. * 给下一审批人发送审批通知
  275. */
  276. public static function sendStepRecord($flow,$relation_type,$relation_id,$check_staff_ids=''){
  277. if(!is_array($check_staff_ids)){
  278. $check_staff_ids=explode(',',$check_staff_ids);
  279. }
  280. if($flow['status'] == 0){//发起人自选
  281. $diff = array_diff(explode(',',$flow['flow_staff_ids']), $check_staff_ids);
  282. if(empty($diff)){
  283. return ['status'=>true,'order_id'=>1];
  284. }else{
  285. $diff=array_values($diff);
  286. ExamineRecord::addExaminse($relation_type,$relation_id, $diff[0]);
  287. return ['status'=>false,'order_id'=>1];
  288. }
  289. }
  290. foreach ($flow['stepList'] as $v){
  291. $staffIds=explode(',',$v['staffids']);
  292. $is_check=0;//是否通过
  293. if($v['status'] == 1){//任意一人 或签
  294. foreach ($staffIds as $sid) {
  295. if (in_array($sid, $check_staff_ids)) {
  296. $is_check = 1;
  297. }
  298. }
  299. }else{//直属上级和并签
  300. $is_check = 1;
  301. foreach ($staffIds as $sid) {
  302. if (!in_array($sid, $check_staff_ids)) {
  303. $is_check = 0;
  304. }
  305. }
  306. }
  307. if ($is_check != 1) {//发送审批通知
  308. foreach ($staffIds as $sid) {
  309. if (!in_array($sid, $check_staff_ids)) {
  310. ExamineRecord::addExaminse($relation_type, $relation_id, $sid);
  311. }
  312. }
  313. return ['status'=>false,'order_id'=>$v['order_id']];
  314. }
  315. }
  316. return ['status'=>true,'order_id'=>$v['order_id']+1];
  317. }
  318. }