123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394 |
- <?php
- namespace addons\qingdongams\model;
- use addons\qingdongams\model\Staff as StaffModel;
- use app\admin\model\AuthGroup;
- use think\Model;
- use traits\model\SoftDelete;
- /**
- * 审批流程设置
- */
- class Flow extends Model
- {
- use SoftDelete;
- const CONTRACT_STATUS='contract';
- const RECEIVABLES_STATUS='receivables';
- const CONSUME_STATUS='consume';
- const ACHIEVEMENT_STATUS='achievement';
- const APPROVAL_STATUS='approval';
- const QUOTE_STATUS='quote';
- const CARD_STATUS='card';//补卡
- const LEAVE_STATUS='leave';//请假
- const PARTS_STATUS='parts';//出入库
- const WORKORDER_STATUS='workorder';//工单
- const INVOICE_STATUS='invoice';//发票
- // 表名,不含前缀
- protected $name = 'qingdongams_flow';
- // 开启自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- protected $deleteTime = 'deletetime';
- // 追加属性
- protected $append = [
- 'groups_text',
- 'last_admin_text',
- ];
- /**
- * 角色组
- */
- public function getGroupsTextAttr($value, $data)
- {
- $group_ids = $data['group_ids'] ?? "";
- if(empty($group_ids)){
- return '全公司';
- }
- $groups = AuthGroup::where(['id' => ['in', $group_ids]])->field('name')->select();
- $names=[];
- foreach ($groups as $v){
- $names[]=$v['name'];
- }
- return implode(',',$names);
- }
- /**
- * 最后修改人
- */
- public function getLastAdminTextAttr($value,$data){
- $admin_id=$data['last_admin_id']??'';
- return \app\admin\model\Admin::where(['id'=>$admin_id])->value('nickname');
- }
- /**
- * 获取审批信息
- * @param $type
- * @return array|mixed
- */
- protected static function getFlowByTypes($type)
- {
- $staff = Staff::info();
- $type=explode('_',$type);
- if ($type[0] == self::APPROVAL_STATUS) {
- $flow = FormApproval::where(['id' => $type[1]])->find();
- return $flow;
- }
- $group_ids = $staff->group_ids;
- $group_ids = explode(',', $group_ids);
- $flows = Flow::where(['relation_type' => $type[0]])->order('id desc')->select();
- $flows = collection($flows)->toArray();
- $flow = [];
- foreach ($flows as $v) {
- if(empty($v['group_ids'])){
- $flow = $v;
- break;
- }
- $ids = explode(',', $v['group_ids']);
- if (array_intersect($group_ids, $ids)) {
- $flow = $v;
- break;
- }
- }
- return $flow;
- }
- /**
- * 获取审批
- * @param $type
- * @return array|false
- */
- public static function getsteplist($type){
- $examineFlowData = Flow::getFlowByTypes($type);
- if (!$examineFlowData) {
- self::setdefault($type);
- $examineFlowData = Flow::getFlowByTypes($type);
- }
- $staff = Staff::info();
- $examine_ids = json_decode($examineFlowData['examine_ids'], true);
- $flow_staff_ids=[];
- //自选还是流程(1固定,0自选)
- if ($examineFlowData['status'] == 1) {
- $pid = StaffModel::where(['id' => $staff->id])->value('parent_id');
- $stepList = [];
- foreach ($examine_ids as $ks => $vs) {
- $stepInfo = [];
- $stepInfo['order_id'] = $ks + 1;//第几级
- if ($vs['stafftype'] == 3) {//直属上级
- $s = StaffModel::where(['id' => $pid])->field('id,name,img')->select();
- $s = collection($s)->toArray();
- $stepInfo['status'] = $vs['stafftype'];
- $stepInfo['stafflist'] = $s;
- $stepInfo['staffids'] = $pid;
- $stepList[] = $stepInfo;
- $flow_staff_ids[]=$pid;
- } else if ($vs['stafftype'] == 1) {//指定员工(任意一人)
- $s = StaffModel::where(['id' => ['in', $vs['staff_id']]])->field('id,name,img')->select();
- $s = collection($s)->toArray();
- $stepInfo['status'] = $vs['stafftype'];
- $stepInfo['stafflist'] = $s;
- $stepInfo['staffids'] = $vs['staff_id'];
- $stepInfo['relation'] = 2;
- $stepList[] = $stepInfo;
- $flow_staff_ids[]=$vs['staff_id'];
- } else {//指定员工(并签)
- $s = StaffModel::where(['id' => ['in', $vs['staff_id']]])->field('id,name,img')->select();
- $s = collection($s)->toArray();
- $stepInfo['status'] = $vs['stafftype'];
- $stepInfo['staffids'] = $vs['staff_id'];
- $stepInfo['stafflist'] = $s;
- $stepInfo['relation'] = 1;
- $stepList[] = $stepInfo;
- $flow_staff_ids[]=$vs['staff_id'];
- }
- }
- } else {
- $stepList = [];
- }
- $data = [];
- $data['flow_staff_ids'] = implode(',',$flow_staff_ids); //审批id
- $data['flow_id'] = $examineFlowData['id']; //审批id
- $data['order_id']=$stepList?$stepList[0]['order_id']:0;
- $data['status'] = $examineFlowData['status']; //1固定,0自选
- $data['stepList'] = $stepList ?: [];//审批流程
- return $data;
- }
- /**
- * 设置默认
- */
- public static function setdefault($type)
- {
- switch ($type){
- case self::CONTRACT_STATUS:
- $name='合同审批';
- break;
- case self::QUOTE_STATUS:
- $name='报价单审批';
- break;
- case self::RECEIVABLES_STATUS:
- $name='回款审批';
- break;
- case self::CONSUME_STATUS:
- $name='费用审批';
- break;
- case self::ACHIEVEMENT_STATUS:
- $name='业绩审批';
- break;
- case self::CARD_STATUS:
- $name='补卡审批';
- break;
- case self::LEAVE_STATUS:
- $name='请假审批';
- break;
- case self::INVOICE_STATUS:
- $name='发票';
- break;
- default:
- $name='审批';
- }
- $data = [
- 'name' => $name,
- 'relation_type' => $type,
- 'status' => 0,
- 'examine_ids' => '[{}]',
- 'group_ids' => '',
- 'last_modified'=>time(),
- 'last_admin_id'=>1,
- ];
- return (new self())->allowField(true)->save($data);
- }
- /**
- * 获取审批详情
- * @param $type
- * @return array|false
- */
- public static function getstepdetail($relation_type,$relation_id){
- switch ($relation_type){
- case self::CONTRACT_STATUS:
- $model=new Contract();
- break;
- case self::RECEIVABLES_STATUS:
- $model=new Receivables();
- break;
- case self::CONSUME_STATUS:
- $model=new Consume();
- break;
- case self::ACHIEVEMENT_STATUS:
- $model=new AchievementRecords();
- break;
- case self::APPROVAL_STATUS:
- $model=new Approval();
- break;
- case self::CARD_STATUS:
- $model=new AttendanceCard();
- break;
- case self::LEAVE_STATUS:
- $model=new Leave();
- break;
- case self::QUOTE_STATUS:
- $model=new Quote();
- break;
- case self::PARTS_STATUS:
- $model=new PartsStockReload();
- break;
- case self::WORKORDER_STATUS:
- $model=new Workorder();
- break;
- case self::INVOICE_STATUS:
- $model=new Invoice();
- break;
- }
- $row=$model->get($relation_id);
- if(empty($row)){
- return false;
- }
- if($relation_type==self::APPROVAL_STATUS){
- $examineFlowData = FormApproval::withTrashed()->where(['id'=>$row->formapproval_id])->find();
- }else{
- $examineFlowData = Flow::withTrashed()->where(['id'=>$row->flow_id])->find();
- }
- if (!$examineFlowData) {
- //无可用审批流,请联系管理员
- return false;
- }
- $staff_id=$row->create_staff_id??($row->staff_id??$row->owner_staff_id);
- $records=ExamineRecord::where(['relation_type'=>$relation_type,
- 'relation_id'=>$relation_id])->field('check_time,status,content,check_staff_id')->select();
- $records=collection($records)->toArray();
- $examineRecord=[];
- foreach ($records as $v){
- $examineRecord[$v['check_staff_id']]=$v;
- }
- $examine_ids = json_decode($examineFlowData['examine_ids'], true);
- //自选还是流程(1固定,0自选)
- if ($examineFlowData['status'] == 1) {
- $pid = StaffModel::where(['id' => $staff_id])->value('parent_id');
- $stepList = [];
- foreach ($examine_ids as $ks => $vs) {
- $stepInfo = [];
- $stepInfo['order_id'] = $ks + 1;//第几级
- if ($vs['stafftype'] == 3) {//直属上级
- $s = StaffModel::where(['id' => $pid])->field('id,name,img')->select();
- $s = collection($s)->toArray();
- $stepInfo['status'] = $vs['stafftype'];
- $stepInfo['stafflist'] = $s;
- $stepInfo['staffids'] = $pid;
- } else if ($vs['stafftype'] == 1) {//指定员工(任意一人)
- $s = StaffModel::where(['id' => ['in', $vs['staff_id']]])->field('id,name,img')->select();
- $s = collection($s)->toArray();
- $stepInfo['status'] = $vs['stafftype'];
- $stepInfo['stafflist'] = $s;
- $stepInfo['staffids'] = $vs['staff_id'];
- $stepInfo['relation'] = 2;
- } else {//指定员工(并签)
- $s = StaffModel::where(['id' => ['in', $vs['staff_id']]])->field('id,name,img')->select();
- $s = collection($s)->toArray();
- $stepInfo['status'] = $vs['stafftype'];
- $stepInfo['staffids'] = $vs['staff_id'];
- $stepInfo['stafflist'] = $s;
- $stepInfo['relation'] = 1;
- }
- //审批记录
- foreach ($stepInfo['stafflist'] as &$staffinfo) {
- if (isset($examineRecord[$staffinfo['id']])) {
- $staffinfo['examine_reord'] = $examineRecord[$staffinfo['id']];
- } else {
- $staffinfo['examine_reord'] = ['status' => 0, 'check_time' => '', 'content' => ''];
- }
- }
- $stepList[] = $stepInfo;
- }
- } else {
- $stepList = [];
- $s = StaffModel::where(['id' => ['in', $row['flow_staff_ids']]])->field('id,name,img')->select();
- $s = collection($s)->toArray();
- $stepInfo['staffids'] = $row['flow_staff_ids'];
- $stepInfo['stafflist'] = $s;
- //审批记录
- foreach ($stepInfo['stafflist'] as &$staffinfo) {
- if (isset($examineRecord[$staffinfo['id']])) {
- $staffinfo['examine_reord'] = $examineRecord[$staffinfo['id']];
- } else {
- $staffinfo['examine_reord'] = ['status' => 0, 'check_time' => '', 'content' => ''];
- }
- }
- $stepList[] = $stepInfo;
- }
- $is_check=0;
- $staff=Staff::info();
- if(isset($examineRecord[$staff->id])){
- if($examineRecord[$staff->id]['status'] == 0){
- $is_check=1;
- }
- }
- $data = [];
- $data['flow_id'] = $examineFlowData['id']; //审批id
- $data['flow_staff_ids'] = $row['flow_staff_ids'];
- $data['order_id'] = $row['order_id'];
- $data['status'] = $examineFlowData['status']; //1固定,0自选
- $data['stepList'] = $stepList ?: [];//审批流程
- $data['is_check'] = $is_check;//审批权限(1有)
- $data['is_recheck'] = ($staff->id == $staff_id)?1:0;//撤销审批权限(1有)
- return $data;
- }
- /**
- * 给下一审批人发送审批通知
- */
- public static function sendStepRecord($flow,$relation_type,$relation_id,$check_staff_ids=''){
- if(!is_array($check_staff_ids)){
- $check_staff_ids=explode(',',$check_staff_ids);
- }
- if($flow['status'] == 0){//发起人自选
- $diff = array_diff(explode(',',$flow['flow_staff_ids']), $check_staff_ids);
- if(empty($diff)){
- return ['status'=>true,'order_id'=>1];
- }else{
- $diff=array_values($diff);
- ExamineRecord::addExaminse($relation_type,$relation_id, $diff[0]);
- return ['status'=>false,'order_id'=>1];
- }
- }
- foreach ($flow['stepList'] as $v){
- $staffIds=explode(',',$v['staffids']);
- $is_check=0;//是否通过
- if($v['status'] == 1){//任意一人 或签
- foreach ($staffIds as $sid) {
- if (in_array($sid, $check_staff_ids)) {
- $is_check = 1;
- }
- }
- }else{//直属上级和并签
- $is_check = 1;
- foreach ($staffIds as $sid) {
- if (!in_array($sid, $check_staff_ids)) {
- $is_check = 0;
- }
- }
- }
- if ($is_check != 1) {//发送审批通知
- foreach ($staffIds as $sid) {
- if (!in_array($sid, $check_staff_ids)) {
- ExamineRecord::addExaminse($relation_type, $relation_id, $sid);
- }
- }
- return ['status'=>false,'order_id'=>$v['order_id']];
- }
- }
- return ['status'=>true,'order_id'=>$v['order_id']+1];
- }
- }
|