123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884 |
- <?php
- namespace app\common\model;
- use app\hander\HelpHander;
- use think\Db;
- use think\Exception;
- use think\Model;
- class Apply extends Model
- {
- protected $autoWriteTimestamp = 'datetime';
- protected $insert = ['del' => 0,'status'=>1];
- public function add(){
- $data = [
- 'approval_id' => input('approvalId/d',0),
- 'org_id' => input('orgId/d',0),
- 'title' => input('title','','trim'),
- 'approval_required' => input('approvalRequired/d',0),
- 'approval_visible' => input('approvalVisible/d',0),
- 'approval_tips' => input('approvalTips','','trim'),
- 'dep_id' => input('depId/d',0),
- 'form_json' => input('formJson','','trim'),
- 'flow_json' => input('flowJson','','trim'),
- 'apply_sn' => get_unique_id(),
- 'user_id' => input('userId/d',0),
- 'create_user_id' => input('userId/d',0),
- ];
- $advanced = Db::name('approval')->where('id',$data['approval_id'])->value('advanced');
- $data['advanced'] = $advanced?$advanced:0;
- $result = validate('Apply')->check($data,[],'');
- if(true !== $result){
- HelpHander::error(validate('Apply')->getError());
- }
- $this->startTrans();
- try{
- $extra = [];
- $extra['depId'] = $data['dep_id'];
- if($data['advanced'] == 6){ // 会议室预定,自动生成编号
- $data['form_json'] = model('MeetingRoom')->createSn($data['form_json']);
- }
- $ret = $this->allowField(true)->save($data);
- if(!$ret){
- \exception('数据保存失败');
- }
- $applyId = $this->id;
- // formJson日志记录
- $res = Db::name('apply_form_log')->insert([
- 'apply_id' => $applyId,
- 'user_id' => $data['user_id'],
- 'form_json' => $data['form_json'],
- 'create_time' => date('Y-m-d H:i:s')
- ]);
- if(!$res){
- \exception('数据保存失败');
- }
- if($data['advanced']){
- $aret = model('ApplyRecord')->advancedStartAction($applyId,$data['org_id'],$data['user_id'],$data['form_json'],$data['advanced'],$extra);
- if(!$aret){
- \exception(model('ApplyRecord')->getError());
- }
- }
- // 创建审批记录
- $ret = model('ApplyRecord')->createRecord($applyId,$data);
- if(!$ret){
- \exception('审批记录生成失败'.model('ApplyRecord')->getError());
- }
- $this->commit();
- }catch (Exception $e){
- $this->rollback();
- HelpHander::error('操作失败'.$e->getMessage());
- }
- return true;
- }
- public function formatFlowJson($info){
- $flowJson = json_decode($info['flow_json'],true);
- foreach ($flowJson as $k=>$v){
- if($v['type'] == 2||$v['type'] == 8){ // 审批或加签,检查节点是否有打回的记录,有的话更新审批人
- $buid = Db::name('apply_record')
- ->where('apply_id',$info['id'])
- ->where('del',1)
- ->where('nodeid',$v['id'])
- ->where('status','<>',3) // 被转交的不用取
- ->where('back_user_id','>',0) // 被转交的不用取
- ->order('id desc')
- ->value('back_user_id');
- if($buid){
- $ulist = Db::name('apply_record')
- ->alias('a')
- ->join('user_info u','a.user_id = u.user_id')
- ->where('a.apply_id',$info['id'])
- ->where('a.del',1)
- ->where('a.status','<>',3) // 被转交的不用取
- ->where('a.nodeid',$v['id'])
- ->where('a.back_user_id',$buid)
- ->field('a.user_id,u.name')
- ->order('a.id asc')
- ->select();
- if($ulist){
- $userList = [];
- foreach ($ulist as $kk=>$vv){
- $userList[] = [
- "userId" => $vv['user_id'],
- "userName" => $vv['name'],
- "shortName" => mb_strlen($vv['name']) > 2?mb_substr($vv['name'], 0, -2):$vv['name'],
- ];
- }
- $flowJson[$k]['userList'] = $userList;
- }
- }
- }
- }
- $info['flow_json'] = json_encode($flowJson);
- return $info;
- }
- public function info($id,$userId,$orgId=0){
- $info = $this->where('id',$id)->where('del',0)->find();
- if(!$info){
- HelpHander::error('数据不存在');
- }
- // 格式化一下审批原始数据,把打回会签和被转交的加入流程中,转交的去除掉
- $info = $this->formatFlowJson($info);
- $user = Db::name('user_info')->where('user_id',$info['user_id'])->field('name,position')->find();
- $info['userName'] = $user['name'];
- $info['userPosition'] = $user['position'];
- $info['depName'] = Db::name('dep')->where('id',$info['dep_id'])->value('name');
- $info['approvalName'] = Db::name('approval')->where('id',$info['approval_id'])->value('title');
- if($info['advanced'] == 3||$info['advanced'] == 4){
- $info['contractSn'] = Db::name('contract')->where('apply_id',$info['id'])->value('contract_sn');
- }else{
- $info['contractSn'] = '';
- }
- if($info['advanced'] == 20){ // 因私出国
- $abroad = Db::name('abroad_apply')->where('apply_id',$info['id'])->find();
- $info['abroad_year'] = $abroad['year'];
- $info['abroad_sn'] = $abroad['sn'];
- }
- $info['create_time2'] = date('Y年m月d日',strtotime($info['create_time']));
- if($info['advanced'] == 5){
- $pay_apply_id = Db::name('contract_pay')->where('apply_id',$info['id'])->value('pay_apply_id');
- $contract = Db::name('contract')->where('apply_id',$pay_apply_id)->find();
- $info['contractSn'] = $contract['contract_sn'];
- $info['contractTitle'] = $contract['title'];
- $info['contractCompany2'] = $contract['company2'];
- $info['programSn'] = '';
- $info['program'] = Db::name('program')->where('id',$contract['program_id'])->value('title');
- }else if($info['advanced'] == 3||$info['advanced'] == 4){
- $contract = Db::name('contract')->where('apply_id',$info['id'])->find();
- $info['contractSn'] = $contract['contract_sn'];
- $info['contractTitle'] = $contract['title'];
- $info['contractCompany2'] = $contract['company2'];
- $info['program'] = '';
- $info['programSn'] = $contract['program_sn'];
- }else{
- $info['contractSn'] = '';
- $info['contractTitle'] = '';
- $info['contractCompany2'] = '';
- $info['program'] = '';
- $info['programSn'] = '';
- }
- $applyFormLogDtoList = Db::name('apply_form_log')
- ->alias('afl')
- ->join('user_info ui','ui.user_id = afl.user_id')
- ->where('afl.apply_id',$id)
- ->field('afl.*,ui.name')
- ->select();
- $info['applyFormLogDtoList'] = $applyFormLogDtoList?$applyFormLogDtoList:[];
- $applyRecordDtoList = Db::name('apply_record')
- ->alias('afl')
- ->join('user_info ui','ui.user_id = afl.user_id')
- ->where('afl.apply_id',$id)
- ->where('afl.del',0)
- ->field('afl.*,ui.name')
- ->order('afl.id asc')
- ->select();
- $applyRecordDtoList = $applyRecordDtoList?$applyRecordDtoList:[];
- foreach ($applyRecordDtoList as $k=>$v){
- $isBack = 0;
- if(in_array($v['type'],[2,8])){
- // 判断是否有上个审批节点
- $f = Db::name('apply_record')
- ->where('status',1)
- ->where('id','<',$v['id'])
- ->where('apply_id',$v['apply_id'])
- ->where('type','in',[2,8])
- ->where('del',0)
- ->where('nodeid','<>',$v['nodeid'])
- ->order('id desc')
- ->find();
- if($f){
- $isBack = 1;
- }
- }
- $applyRecordDtoList[$k]['is_back'] = $isBack;
- }
- $info['applyRecordDtoList'] = $applyRecordDtoList;
- // 撤销功能 只能用户自己撤销(审批人撤销功能去掉)
- $isShowUndo = true;
- $undo = Db::name('apply_record')
- ->where('apply_id',$id)
- ->where('del',0)
- ->where('status','in',[1,2])
- ->where('type','in',[2,8])
- ->find();
- if(!$undo && $userId == $info['user_id'] && $info['status'] == 1){
- $isShowUndo = false;
- }
- $data = [
- 'apply' => $info->toArray(),
- 'isShowUndo' => $isShowUndo
- ];
- return $data;
- }
- // 审批单 传$userId既是我发起的审批单
- public function lists($page,$size,$userId,$orgId,$status,$approvalId,$info,$startTime,$endTime){
- if($userId){
- $map[] = ['user_id','=',$userId];
- }
- if($orgId){
- $map[] = ['org_id','=',$orgId];
- }
- if($status){
- $map[] = ['status','=',$status];
- }
- if($approvalId){
- $map[] = ['approval_id','=',$approvalId];
- }
- if($startTime){
- $map[] = ['create_time','>=',$startTime];
- }
- if($endTime){
- $map[] = ['create_time','<=',$endTime];
- }
- if($info){
- $map[] = ['title|apply_sn|form_json','like','%'.$info.'%'];
- }
- $map[] = ['del','=',0];
- $lists = $this
- ->where($map)
- ->field('id,title,apply_sn,create_time,finish_time,status,advanced,approval_id,form_json')
- ->page($page,$size)
- ->order('id desc')
- ->select();
- foreach ($lists as $k=>$v){
- $ct = $this->getExtraTitle($v);
- if($ct){
- $lists[$k]['title'] = $v['title'].'【'.$ct.'】';
- }
- }
- $total = $this->where($map)->count();
- $data = [
- 'total' => $total,
- 'list' => $lists?$lists->toArray():[]
- ];
- return $data;
- }
- public function getExtraTitle($v){
- $ct = '';
- $formJson = json_decode($v['form_json'],true);
- if($v['advanced'] == 3){ // 合同会签
- $ct = Db::name('contract')->where('apply_id',$v['id'])->value('title');
- }else if($v['advanced'] == 4){ // 追加合同
- $ct = Db::name('contract')->where('apply_id',$v['id'])->value('title');
- }else if($v['advanced'] == 18){ // 用印
- $ct = Db::name('official_seal_apply')->where('apply_id',$v['id'])->value('content');
- }else if($v['advanced'] == 6){ // 会议室预定-会议议题
- $ct = Db::name('meeting_room_book')->where('apply_id',$v['id'])->value('projection');
- }else if($v['advanced'] == 11){ // 用车-事由
- $ct = Db::name('car_record')->where('apply_id',$v['id'])->value('reason');
- }else if($v['approval_id'] == 93){ // 请假-事由及说明
- $ct = $this->getValFromFormJson($formJson,6);
- }else if($v['approval_id'] == 99){ // 销假-原因
- $ct = $this->getValFromFormJson($formJson,10);
- }else if($v['approval_id'] == 101){ // 离京报告表-事由及说明
- $ct = $this->getValFromFormJson($formJson,3);
- }else if($v['approval_id'] == 120){ // 签报--标题
- $ct = $this->getValFromFormJson($formJson,3);
- }else if($v['approval_id'] == 89){ // 发文审批--发文事项
- $ct = $this->getValFromFormJson($formJson,2);
- }else if($v['approval_id'] == 118){ // 上会文件--会议名称
- $ct1 = $this->getValFromFormJson($formJson,2);
- $ct2 = $this->getValFromFormJson($formJson,3);
- $ct = $ct1.' | '.$ct2;
- }else if($v['approval_id'] == 128){ // 官网发布审批--文章标题
- $ct = $this->getValFromFormJson($formJson,7);
- }else if($v['approval_id'] == 130){ // 合同借阅--借阅合同
- $ct = $this->getValFromFormJson($formJson,0);
- }else if($v['approval_id'] == 131){ // 档案借阅--借阅档案
- $ct = $this->getValFromFormJson($formJson,0);
- }else if($v['approval_id'] == 149){ // 董事会上会文件--会议名称
- $ct = $this->getValFromFormJson($formJson,0);
- }
- return $ct;
- }
- // 根据idx获取表单项的值
- public function getValFromFormJson($formJson,$idx){
- $ct = '';
- foreach ($formJson as $fk=>$fv){
- if($fv['idx'] == $idx){
- $ct = !empty($fv['values'])?$fv['values']:'';
- break;
- }
- }
- return $ct;
- }
- public function queryUserApplyList($page,$size,$param,$userId,$orgId){
- if($userId){
- $map[] = ['a.user_id','=',$userId];
- }
- if($orgId){
- $map[] = ['a.org_id','=',$orgId];
- }
- if($param){
- $map[] = ['a.title|a.apply_sn|ui.name','like','%'.$param.'%'];
- }
- $map[] = ['a.del','=',0];
- $lists = Db::name('apply')
- ->alias('a')
- ->join('user_info ui','ui.user_id = a.user_id')
- ->field('a.id,a.title,a.apply_sn,a.create_time,a.update_time,a.status,ui.name as userName')
- ->where($map)
- ->page($page,$size)
- ->order('a.id desc')
- ->select();
- return $lists?$lists:[];
- }
- public function appMyApplyList($page,$size,$param,$type,$userId,$orgId){
- if($userId){
- $map[] = ['ar.user_id','=',$userId];
- }
- if($orgId){
- $map[] = ['a.org_id','=',$orgId];
- }
- if($param){
- $map[] = ['a.title|a.apply_sn|ui.name','like','%'.$param.'%'];
- }
- if($type >= 0){
- $map[] = ['ar.status','=',$type];
- }
- $map[] = ['a.del','=',0];
- $map[] = ['ar.type','in',[2,8]];
- $map[] = ['ar.del','=',0];
- $lists = Db::name('apply_record')
- ->alias('ar')
- ->join('apply a','a.id = ar.apply_id')
- ->join('user_info ui','ui.user_id = a.user_id')
- ->field('a.id,a.title,a.apply_sn,a.create_time,a.update_time,a.status,ui.name as userName,ar.status as arStatus')
- ->where($map)
- ->page($page,$size)
- ->order('ar.id desc')
- ->select();
- return $lists?$lists:[];
- }
- public function queryCopyUserApply($page,$size,$param,$status,$type,$userId,$orgId){
- if($userId){
- $map[] = ['ar.user_id','=',$userId];
- }
- if($orgId){
- $map[] = ['a.org_id','=',$orgId];
- }
- if($param){
- $map[] = ['a.title|a.apply_sn|ui.name','like','%'.$param.'%'];
- }
- if($status >= 0){
- $map[] = ['ar.status','=',$status];
- }
- $map[] = ['ar.type','=',$type];
- $map[] = ['a.del','=',0];
- $map[] = ['ar.del','=',0];
- $lists = Db::name('apply_record')
- ->alias('ar')
- ->join('apply a','a.id = ar.apply_id')
- ->join('user_info ui','ui.user_id = a.user_id')
- ->field('a.id,a.title,a.apply_sn,a.create_time,a.update_time,a.status,ui.name as userName,ar.status as arStatus,ar.id as arId')
- ->where($map)
- ->page($page,$size)
- ->order('ar.id desc')
- ->select();
- return $lists?$lists:[];
- }
- // 我审批(抄送,执行)的流程
- public function userApplyList($page,$size,$userId,$orgId,$type,$status,$approvalId,$info,$startTime,$endTime){
- if($userId){
- $map[] = ['ar.user_id','=',$userId];
- }
- if($orgId){
- $map[] = ['a.org_id','=',$orgId];
- }
- if($status >= 0){
- $map[] = ['ar.status','=',$status];
- }
- if($approvalId){
- $map[] = ['a.approval_id','=',$approvalId];
- }
- if($startTime){
- $map[] = ['a.create_time','>=',$startTime];
- }
- if($endTime){
- $map[] = ['a.create_time','<=',$endTime];
- }
- if($info){
- $map[] = ['a.title|a.apply_sn|a.form_json','like','%'.$info.'%'];
- }
- if($type){
- if($type == 2){
- $map[] = ['ar.type','in',[2,8]];
- }else{
- $map[] = ['ar.type','=',$type];
- }
- }
- $map[] = ['a.del','=',0];
- $map[] = ['ar.del','=',0];
- $lists = Db::name('apply_record')
- ->alias('ar')
- ->join('apply a','a.id = ar.apply_id')
- ->where($map)
- ->field('a.id,a.title,a.apply_sn,a.create_time,ar.finish_time as reFinishTime,ar.status,ar.id as recordId,a.advanced,a.form_json,a.approval_id')
- ->page($page,$size)
- ->order('ar.id desc')
- ->select();
- foreach ($lists as $k=>$v){
- $ct = $this->getExtraTitle($v);
- if($ct){
- $lists[$k]['title'] = $v['title'].'【'.$ct.'】';
- }
- }
- $total =Db::name('apply_record')
- ->alias('ar')
- ->join('apply a','a.id = ar.apply_id')
- ->where($map)->count();
- $data = [
- 'total' => $total,
- 'list' => $lists?$lists:[]
- ];
- return $data;
- }
- // 撤销 $type 0=管理员撤销 1=用户撤销
- public function undo($applyId,$userId,$type=0){
- $info = Db::name('apply')->where('id',$applyId)->where('del',0)->find();
- if(!$info){
- HelpHander::error('数据不存在');
- }
- if($info['status'] != 1){
- HelpHander::error('该状态无法撤销');
- }
- if($type == 1 && $info['user_id'] != $userId){
- HelpHander::error('无权限撤销');
- }
- $undo = Db::name('apply_record')
- ->where('apply_id',$applyId)
- ->where('del',0)
- ->where('status','in',[1,2])
- ->where('type','in',[2,8])
- ->find();
- if($undo){
- HelpHander::error('已有人审核,无法撤销');
- }
- Db::startTrans();
- try{
- $ret = Db::name('apply')->where('id',$applyId)->update(['status'=>3,'update_time'=>date('Y-m-d H:i:s')]);
- if(!$ret){
- \exception('撤销失败');
- }
- Db::name('apply_record')->where('apply_id',$applyId)->setField('status',5);
- $result = model('ApplyRecord')->advancedDisagreeAction($applyId,$info['advanced']);
- if(!$result){
- \exception('操作失败');
- }
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- HelpHander::error('撤销失败');
- }
- return true;
- }
- // 关联审批单列表
- public function selectApplyListByType($page,$size,$userId,$orgId,$type,$approvalId,$param){
- if($orgId){
- $map[] = ['a.org_id','=',$orgId];
- }
- // if($approvalId){
- // $map[] = ['a.approval_id','in',$approvalId];
- // }
- if($param){
- $map[] = ['a.title|a.apply_sn','like','%'.$param.'%'];
- }
- $map[] = ['a.del','=',0];
- $map[] = ['a.advanced','=',1];
- if($type == 2){ // 我发起的
- if($userId){
- $map[] = ['a.user_id','=',$userId];
- }
- $lists = Db::name('apply')
- ->alias('a')
- ->join('user_info ui','ui.user_id = a.user_id')
- ->where($map)
- ->field('a.id,a.title,a.apply_sn,a.create_time,ui.name')
- ->page($page,$size)
- ->order('a.id desc')
- ->select();
- $total = Db::name('apply')
- ->alias('a')
- ->join('user_info ui','ui.user_id = a.user_id')
- ->where($map)->count();
- }else{
- if($userId){
- $map[] = ['ar.user_id','=',$userId];
- }
- if($type == 1){ // 我审批的
- $map[] = ['ar.type','in',[2,8]];
- }else if($type == 3){
- $map[] = ['ar.type','=',3];
- }else if($type == 4){
- $map[] = ['ar.type','=',9];
- }
- $map[] = ['ar.del','=',0];
- $lists = Db::name('apply_record')
- ->alias('ar')
- ->join('apply a','a.id = ar.apply_id')
- ->join('user_info ui','ui.user_id = a.user_id')
- ->where($map)
- ->field('a.id,a.title,a.apply_sn,a.create_time,ui.name')
- ->page($page,$size)
- ->order('a.id desc')
- ->group('ar.apply_id')
- ->select();
- $total = Db::name('apply_record')
- ->alias('ar')
- ->join('apply a','a.id = ar.apply_id')
- ->join('user_info ui','ui.user_id = a.user_id')
- ->group('ar.apply_id')
- ->where($map)->count();
- }
- $data = [
- 'total' => $total,
- 'list' => $lists?$lists:[]
- ];
- return $data;
- }
- // 催办
- public function remindAudit($applyId,$userId){
- $info = Db::name('apply')
- ->where('id',$applyId)
- ->where('user_id',$userId)
- ->where('del',0)
- ->find();
- if(!$info){
- HelpHander::error('数据不存在');
- }
- if($info['status'] != 1){
- HelpHander::error('无权限催办');
- }
- $ret = $this->remindAuditCommon($info);
- if(!$ret){
- HelpHander::error('催办失败');
- }
- return true;
- }
- // 根据审批单信息催办
- public function remindAuditCommon($info){
- $records = Db::name('apply_record')
- ->where('nodeid',$info['nodeid'])
- ->where('apply_id',$info['id'])
- ->where('status',0)
- ->where('del',0)
- ->field('id,user_id')
- ->select();
- $records = $records?$records:[];
- Db::startTrans();
- try{
- foreach ($records as $k=>$v){
- // 生成消息及推送
- $context = $info['title'].'需要您审批,请尽快处理。';
- $res = model('Message')->add(3,$v['id'],2,$v['user_id'],$info['org_id'],$context);
- if(!$res){
- \exception('消息生成失败');
- }
- }
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- return false;
- }
- return true;
- }
- // 关联主合同审批单
- public function selectContractByType($page,$size,$userId,$orgId,$type,$param){
- if($orgId){
- $map[] = ['a.org_id','=',$orgId];
- }
- if($param){
- $map[] = ['a.title|a.apply_sn','like','%'.$param.'%'];
- }
- $map[] = ['a.del','=',0];
- $map[] = ['c.status','=',1];
- $map[] = ['c.type','=',0];
- if($type == 2){ // 我发起的
- if($userId){
- $map[] = ['a.user_id','=',$userId];
- }
- $lists = Db::name('apply')
- ->alias('a')
- ->join('user_info ui','ui.user_id = a.user_id')
- ->join('contract c','c.apply_id = a.id')
- ->where($map)
- ->field('a.id,a.title,a.apply_sn,a.create_time,ui.name,c.title as contractName')
- ->page($page,$size)
- ->order('a.id desc')
- ->select();
- $total = Db::name('apply')
- ->alias('a')
- ->join('user_info ui','ui.user_id = a.user_id')
- ->join('contract c','c.apply_id = a.id')
- ->where($map)->count();
- }else{
- if($userId){
- $map[] = ['ar.user_id','=',$userId];
- }
- if($type == 1){ // 我审批的
- $map[] = ['ar.type','in',[2,8]];
- }else if($type == 3){
- $map[] = ['ar.type','=',3];
- }else if($type == 4){
- $map[] = ['ar.type','=',9];
- }
- $map[] = ['ar.del','=',0];
- $lists = Db::name('apply_record')
- ->alias('ar')
- ->join('apply a','a.id = ar.apply_id')
- ->join('user_info ui','ui.user_id = a.user_id')
- ->join('contract c','c.apply_id = a.id')
- ->where($map)
- ->field('a.id,a.title,a.apply_sn,a.create_time,ui.name,c.title as contractName')
- ->page($page,$size)
- ->order('a.id desc')
- ->group('ar.apply_id')
- ->select();
- $total =Db::name('apply_record')
- ->alias('ar')
- ->join('apply a','a.id = ar.apply_id')
- ->join('user_info ui','ui.user_id = a.user_id')
- ->join('contract c','c.apply_id = a.id')
- ->group('ar.apply_id')
- ->where($map)->count();
- }
- $data = [
- 'total' => $total,
- 'list' => $lists?$lists:[]
- ];
- return $data;
- }
- // 关联未结算清合同审批单
- public function selectContractFinishByType($page,$size,$userId,$orgId,$type,$param,$ispay=1){
- if($orgId){
- $map[] = ['a.org_id','=',$orgId];
- }
- if($param){
- $map[] = ['a.title|a.apply_sn','like','%'.$param.'%'];
- }
- if(in_array($ispay,[1,2])){
- $map[] = ['c.ispay','=',$ispay];
- }
- $map[] = ['a.del','=',0];
- $map[] = ['c.status','=',1];
- $map[] = ['c.finish','=',0];
- if($type == 2){ // 我发起的
- if($userId){
- $map[] = ['a.user_id','=',$userId];
- }
- $lists = Db::name('apply')
- ->alias('a')
- ->join('user_info ui','ui.user_id = a.user_id')
- ->join('contract c','c.apply_id = a.id')
- ->where($map)
- ->field('a.id,a.title,a.apply_sn,a.create_time,ui.name,c.title as contractName,c.money,c.pay_money')
- ->page($page,$size)
- ->order('a.id desc')
- ->select();
- foreach ($lists as $k=>$v){
- $paybl = floatval($v['money']) > 0?round($v['pay_money'] / $v['money'],2) * 100:0;
- $lists[$k]['paybl'] = $paybl.'%';
- }
- $total = Db::name('apply')
- ->alias('a')
- ->join('user_info ui','ui.user_id = a.user_id')
- ->join('contract c','c.apply_id = a.id')
- ->where($map)->count();
- }else{
- if($userId){
- $map[] = ['ar.user_id','=',$userId];
- }
- if($type == 1){ // 我审批的
- $map[] = ['ar.type','in',[2,8]];
- }else if($type == 3){
- $map[] = ['ar.type','=',3];
- }else if($type == 4){
- $map[] = ['ar.type','=',9];
- }
- $map[] = ['ar.del','=',0];
- $lists = Db::name('apply_record')
- ->alias('ar')
- ->join('apply a','a.id = ar.apply_id')
- ->join('user_info ui','ui.user_id = a.user_id')
- ->join('contract c','c.apply_id = a.id')
- ->where($map)
- ->field('a.id,a.title,a.apply_sn,a.create_time,ui.name,c.title as contractName,c.money,c.pay_money')
- ->page($page,$size)
- ->order('a.id desc')
- ->group('ar.apply_id')
- ->select();
- foreach ($lists as $k=>$v){
- $paybl = floatval($v['money']) > 0?round($v['pay_money'] / $v['money'],2) * 100:0;
- $lists[$k]['paybl'] = $paybl.'%';
- }
- $total =Db::name('apply_record')
- ->alias('ar')
- ->join('apply a','a.id = ar.apply_id')
- ->join('user_info ui','ui.user_id = a.user_id')
- ->join('contract c','c.apply_id = a.id')
- ->group('ar.apply_id')
- ->where($map)->count();
- }
- $data = [
- 'total' => $total,
- 'list' => $lists?$lists:[]
- ];
- return $data;
- }
- // 删除审批单
- public function del($applyId,$userId){
- if($userId != 100003 && $userId != 100006){
- HelpHander::error('无权限操作');
- }
- $info = Db::name('apply')->where('id',$applyId)->where('del',0)->find();
- if(!$info){
- HelpHander::error('数据不存在');
- }
- Db::startTrans();
- try{
- $ret = Db::name('apply')->where('id',$applyId)->update(['del'=>1,'update_time'=>date('Y-m-d H:i:s')]);
- if(!$ret){
- \exception('删除失败');
- }
- // 高级组件处理
- if($info['advanced'] > 0 && !in_array($info['status'],[3,4,5])){
- switch ($info['advanced']){
- case '1': // 请假 --
- $ret = model('AttendanceLeave')->advancedDisagreeLeave($applyId);
- break;
- case '3': // 合同 --
- $ret = model('Contract')->advancedDelContract($applyId);
- if(!$ret){
- \exception(model('Contract')->getError());
- }
- break;
- case '4': // 追加合同 --
- $ret = model('Contract')->advancedDelAddContract($applyId);
- if(!$ret){
- \exception(model('Contract')->getError());
- }
- break;
- case '5': // 合同付款 --
- $ret = model('ContractPay')->advancedDelContractPay($applyId);
- break;
- case '6': // 会议室预定 --
- $ret = model('MeetingRoom')->advancedDisagreeMeetingRoom($applyId);
- break;
- case '7': // 合同收款 --
- $ret = model('ContractPay')->advancedDelContractGet($applyId);
- break;
- case '8': // 销假 --
- $ret = model('AttendanceLeaveTerminate')->advancedDelLeaveTerminate($applyId);
- break;
- case '9': // 离京报告 --
- $ret = model('LeaveBj')->advancedDisagreeLeaveBj($applyId);
- break;
- case '10': // 办公物品 --
- $ret = model('OfficeReceive')->advancedDisagreeOfficeReceive($applyId);
- break;
- case '11': // 用车 --
- $ret = model('CarRecord')->advancedDisagreeCarRecord($applyId);
- break;
- case '17': // 预算 --
- $ret = model('BudgetApply')->advancedDelBudgetApply($applyId);
- if(!$ret){
- \exception(model('BudgetApply')->getError());
- }
- break;
- case '18': // 用印 --
- $ret = model('OfficialSealApply')->advancedDisagreeSealApply($applyId);
- break;
- case '19': // 中心发文 --
- $ret = model('PostApply')->advancedDisagreePostApply($applyId);
- break;
- case '20': // 因私出国 --
- $ret = model('AbroadApply')->advancedDisagreeAbroadApply($applyId);
- break;
- case '21': // 日常支出
- $ret = model('BudgetPay')->advancedDelBudgetPay($applyId);
- break;
- default:
- $ret = true;
- break;
- }
- if(!$ret){
- \exception('删除失败');
- }
- }
- // 删除日志
- $ret = Db::name('del_log')->insert([
- 'user_id' => $userId,
- 'type' => 1,
- 'from_id' => $applyId,
- 'create_time' => date('Y-m-d H:i:s'),
- 'content' => '删除了'.$info['title'],
- ]);
- if(!$ret){
- \exception('操作失败');
- }
- Db::commit();
- }catch (Exception $e){
- trace($e->getMessage());
- Db::rollback();
- HelpHander::error($e->getMessage());
- }
- return true;
- }
- }
|