Flow.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <?php
  2. namespace addons\qingdongams\model;
  3. use addons\qingdongams\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 QUOTE_STATUS='quote';
  19. const CARD_STATUS='card';//补卡
  20. const LEAVE_STATUS='leave';//请假
  21. const PARTS_STATUS='parts';//出入库
  22. const WORKORDER_STATUS='workorder';//工单
  23. const INVOICE_STATUS='invoice';//发票
  24. // 表名,不含前缀
  25. protected $name = 'qingdongams_flow';
  26. // 开启自动写入时间戳字段
  27. protected $autoWriteTimestamp = 'int';
  28. // 定义时间戳字段名
  29. protected $createTime = 'createtime';
  30. protected $updateTime = 'updatetime';
  31. protected $deleteTime = 'deletetime';
  32. // 追加属性
  33. protected $append = [
  34. 'groups_text',
  35. 'last_admin_text',
  36. ];
  37. /**
  38. * 角色组
  39. */
  40. public function getGroupsTextAttr($value, $data)
  41. {
  42. $group_ids = $data['group_ids'] ?? "";
  43. if(empty($group_ids)){
  44. return '全公司';
  45. }
  46. $groups = AuthGroup::where(['id' => ['in', $group_ids]])->field('name')->select();
  47. $names=[];
  48. foreach ($groups as $v){
  49. $names[]=$v['name'];
  50. }
  51. return implode(',',$names);
  52. }
  53. /**
  54. * 最后修改人
  55. */
  56. public function getLastAdminTextAttr($value,$data){
  57. $admin_id=$data['last_admin_id']??'';
  58. return \app\admin\model\Admin::where(['id'=>$admin_id])->value('nickname');
  59. }
  60. /**
  61. * 获取审批信息
  62. * @param $type
  63. * @return array|mixed
  64. */
  65. protected static function getFlowByTypes($type)
  66. {
  67. $staff = Staff::info();
  68. $type=explode('_',$type);
  69. if ($type[0] == self::APPROVAL_STATUS) {
  70. $flow = FormApproval::where(['id' => $type[1]])->find();
  71. return $flow;
  72. }
  73. $group_ids = $staff->group_ids;
  74. $group_ids = explode(',', $group_ids);
  75. $flows = Flow::where(['relation_type' => $type[0]])->order('id desc')->select();
  76. $flows = collection($flows)->toArray();
  77. $flow = [];
  78. foreach ($flows as $v) {
  79. if(empty($v['group_ids'])){
  80. $flow = $v;
  81. break;
  82. }
  83. $ids = explode(',', $v['group_ids']);
  84. if (array_intersect($group_ids, $ids)) {
  85. $flow = $v;
  86. break;
  87. }
  88. }
  89. return $flow;
  90. }
  91. /**
  92. * 获取审批
  93. * @param $type
  94. * @return array|false
  95. */
  96. public static function getsteplist($type){
  97. $examineFlowData = Flow::getFlowByTypes($type);
  98. if (!$examineFlowData) {
  99. self::setdefault($type);
  100. $examineFlowData = Flow::getFlowByTypes($type);
  101. }
  102. $staff = Staff::info();
  103. $examine_ids = json_decode($examineFlowData['examine_ids'], true);
  104. $flow_staff_ids=[];
  105. //自选还是流程(1固定,0自选)
  106. if ($examineFlowData['status'] == 1) {
  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. */
  154. public static function setdefault($type)
  155. {
  156. switch ($type){
  157. case self::CONTRACT_STATUS:
  158. $name='合同审批';
  159. break;
  160. case self::QUOTE_STATUS:
  161. $name='报价单审批';
  162. break;
  163. case self::RECEIVABLES_STATUS:
  164. $name='回款审批';
  165. break;
  166. case self::CONSUME_STATUS:
  167. $name='费用审批';
  168. break;
  169. case self::ACHIEVEMENT_STATUS:
  170. $name='业绩审批';
  171. break;
  172. case self::CARD_STATUS:
  173. $name='补卡审批';
  174. break;
  175. case self::LEAVE_STATUS:
  176. $name='请假审批';
  177. break;
  178. case self::INVOICE_STATUS:
  179. $name='发票';
  180. break;
  181. default:
  182. $name='审批';
  183. }
  184. $data = [
  185. 'name' => $name,
  186. 'relation_type' => $type,
  187. 'status' => 0,
  188. 'examine_ids' => '[{}]',
  189. 'group_ids' => '',
  190. 'last_modified'=>time(),
  191. 'last_admin_id'=>1,
  192. ];
  193. return (new self())->allowField(true)->save($data);
  194. }
  195. /**
  196. * 获取审批详情
  197. * @param $type
  198. * @return array|false
  199. */
  200. public static function getstepdetail($relation_type,$relation_id){
  201. switch ($relation_type){
  202. case self::CONTRACT_STATUS:
  203. $model=new Contract();
  204. break;
  205. case self::RECEIVABLES_STATUS:
  206. $model=new Receivables();
  207. break;
  208. case self::CONSUME_STATUS:
  209. $model=new Consume();
  210. break;
  211. case self::ACHIEVEMENT_STATUS:
  212. $model=new AchievementRecords();
  213. break;
  214. case self::APPROVAL_STATUS:
  215. $model=new Approval();
  216. break;
  217. case self::CARD_STATUS:
  218. $model=new AttendanceCard();
  219. break;
  220. case self::LEAVE_STATUS:
  221. $model=new Leave();
  222. break;
  223. case self::QUOTE_STATUS:
  224. $model=new Quote();
  225. break;
  226. case self::PARTS_STATUS:
  227. $model=new PartsStockReload();
  228. break;
  229. case self::WORKORDER_STATUS:
  230. $model=new Workorder();
  231. break;
  232. case self::INVOICE_STATUS:
  233. $model=new Invoice();
  234. break;
  235. }
  236. $row=$model->get($relation_id);
  237. if(empty($row)){
  238. return false;
  239. }
  240. if($relation_type==self::APPROVAL_STATUS){
  241. $examineFlowData = FormApproval::withTrashed()->where(['id'=>$row->formapproval_id])->find();
  242. }else{
  243. $examineFlowData = Flow::withTrashed()->where(['id'=>$row->flow_id])->find();
  244. }
  245. if (!$examineFlowData) {
  246. //无可用审批流,请联系管理员
  247. return false;
  248. }
  249. $staff_id=$row->create_staff_id??($row->staff_id??$row->owner_staff_id);
  250. $records=ExamineRecord::where(['relation_type'=>$relation_type,
  251. 'relation_id'=>$relation_id])->field('check_time,status,content,check_staff_id')->select();
  252. $records=collection($records)->toArray();
  253. $examineRecord=[];
  254. foreach ($records as $v){
  255. $examineRecord[$v['check_staff_id']]=$v;
  256. }
  257. $examine_ids = json_decode($examineFlowData['examine_ids'], true);
  258. //自选还是流程(1固定,0自选)
  259. if ($examineFlowData['status'] == 1) {
  260. $pid = StaffModel::where(['id' => $staff_id])->value('parent_id');
  261. $stepList = [];
  262. foreach ($examine_ids as $ks => $vs) {
  263. $stepInfo = [];
  264. $stepInfo['order_id'] = $ks + 1;//第几级
  265. if ($vs['stafftype'] == 3) {//直属上级
  266. $s = StaffModel::where(['id' => $pid])->field('id,name,img')->select();
  267. $s = collection($s)->toArray();
  268. $stepInfo['status'] = $vs['stafftype'];
  269. $stepInfo['stafflist'] = $s;
  270. $stepInfo['staffids'] = $pid;
  271. } else if ($vs['stafftype'] == 1) {//指定员工(任意一人)
  272. $s = StaffModel::where(['id' => ['in', $vs['staff_id']]])->field('id,name,img')->select();
  273. $s = collection($s)->toArray();
  274. $stepInfo['status'] = $vs['stafftype'];
  275. $stepInfo['stafflist'] = $s;
  276. $stepInfo['staffids'] = $vs['staff_id'];
  277. $stepInfo['relation'] = 2;
  278. } else {//指定员工(并签)
  279. $s = StaffModel::where(['id' => ['in', $vs['staff_id']]])->field('id,name,img')->select();
  280. $s = collection($s)->toArray();
  281. $stepInfo['status'] = $vs['stafftype'];
  282. $stepInfo['staffids'] = $vs['staff_id'];
  283. $stepInfo['stafflist'] = $s;
  284. $stepInfo['relation'] = 1;
  285. }
  286. //审批记录
  287. foreach ($stepInfo['stafflist'] as &$staffinfo) {
  288. if (isset($examineRecord[$staffinfo['id']])) {
  289. $staffinfo['examine_reord'] = $examineRecord[$staffinfo['id']];
  290. } else {
  291. $staffinfo['examine_reord'] = ['status' => 0, 'check_time' => '', 'content' => ''];
  292. }
  293. }
  294. $stepList[] = $stepInfo;
  295. }
  296. } else {
  297. $stepList = [];
  298. $s = StaffModel::where(['id' => ['in', $row['flow_staff_ids']]])->field('id,name,img')->select();
  299. $s = collection($s)->toArray();
  300. $stepInfo['staffids'] = $row['flow_staff_ids'];
  301. $stepInfo['stafflist'] = $s;
  302. //审批记录
  303. foreach ($stepInfo['stafflist'] as &$staffinfo) {
  304. if (isset($examineRecord[$staffinfo['id']])) {
  305. $staffinfo['examine_reord'] = $examineRecord[$staffinfo['id']];
  306. } else {
  307. $staffinfo['examine_reord'] = ['status' => 0, 'check_time' => '', 'content' => ''];
  308. }
  309. }
  310. $stepList[] = $stepInfo;
  311. }
  312. $is_check=0;
  313. $staff=Staff::info();
  314. if(isset($examineRecord[$staff->id])){
  315. if($examineRecord[$staff->id]['status'] == 0){
  316. $is_check=1;
  317. }
  318. }
  319. $data = [];
  320. $data['flow_id'] = $examineFlowData['id']; //审批id
  321. $data['flow_staff_ids'] = $row['flow_staff_ids'];
  322. $data['order_id'] = $row['order_id'];
  323. $data['status'] = $examineFlowData['status']; //1固定,0自选
  324. $data['stepList'] = $stepList ?: [];//审批流程
  325. $data['is_check'] = $is_check;//审批权限(1有)
  326. $data['is_recheck'] = ($staff->id == $staff_id)?1:0;//撤销审批权限(1有)
  327. return $data;
  328. }
  329. /**
  330. * 给下一审批人发送审批通知
  331. */
  332. public static function sendStepRecord($flow,$relation_type,$relation_id,$check_staff_ids=''){
  333. if(!is_array($check_staff_ids)){
  334. $check_staff_ids=explode(',',$check_staff_ids);
  335. }
  336. if($flow['status'] == 0){//发起人自选
  337. $diff = array_diff(explode(',',$flow['flow_staff_ids']), $check_staff_ids);
  338. if(empty($diff)){
  339. return ['status'=>true,'order_id'=>1];
  340. }else{
  341. $diff=array_values($diff);
  342. ExamineRecord::addExaminse($relation_type,$relation_id, $diff[0]);
  343. return ['status'=>false,'order_id'=>1];
  344. }
  345. }
  346. foreach ($flow['stepList'] as $v){
  347. $staffIds=explode(',',$v['staffids']);
  348. $is_check=0;//是否通过
  349. if($v['status'] == 1){//任意一人 或签
  350. foreach ($staffIds as $sid) {
  351. if (in_array($sid, $check_staff_ids)) {
  352. $is_check = 1;
  353. }
  354. }
  355. }else{//直属上级和并签
  356. $is_check = 1;
  357. foreach ($staffIds as $sid) {
  358. if (!in_array($sid, $check_staff_ids)) {
  359. $is_check = 0;
  360. }
  361. }
  362. }
  363. if ($is_check != 1) {//发送审批通知
  364. foreach ($staffIds as $sid) {
  365. if (!in_array($sid, $check_staff_ids)) {
  366. ExamineRecord::addExaminse($relation_type, $relation_id, $sid);
  367. }
  368. }
  369. return ['status'=>false,'order_id'=>$v['order_id']];
  370. }
  371. }
  372. return ['status'=>true,'order_id'=>$v['order_id']+1];
  373. }
  374. }