Apply.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use think\Db;
  5. use think\Exception;
  6. use think\Model;
  7. class Apply extends Model
  8. {
  9. protected $autoWriteTimestamp = 'datetime';
  10. protected $insert = ['del' => 0,'status'=>1];
  11. public function add(){
  12. $data = [
  13. 'approval_id' => input('approvalId/d',0),
  14. 'org_id' => input('orgId/d',0),
  15. 'title' => input('title','','trim'),
  16. 'approval_required' => input('approvalRequired/d',0),
  17. 'approval_visible' => input('approvalVisible/d',0),
  18. 'approval_tips' => input('approvalTips','','trim'),
  19. 'dep_id' => input('depId/d',0),
  20. 'form_json' => input('formJson','','trim'),
  21. 'flow_json' => input('flowJson','','trim'),
  22. 'apply_sn' => get_unique_id(),
  23. 'user_id' => input('userId/d',0),
  24. 'create_user_id' => input('userId/d',0),
  25. ];
  26. $advanced = Db::name('approval')->where('id',$data['approval_id'])->value('advanced');
  27. $data['advanced'] = $advanced?$advanced:0;
  28. $result = validate('Apply')->check($data,[],'');
  29. if(true !== $result){
  30. HelpHander::error(validate('Apply')->getError());
  31. }
  32. $this->startTrans();
  33. try{
  34. $extra = [];
  35. $extra['depId'] = $data['dep_id'];
  36. if($data['advanced'] == 6){ // 会议室预定,自动生成编号
  37. $data['form_json'] = model('MeetingRoom')->createSn($data['form_json']);
  38. }
  39. $ret = $this->allowField(true)->save($data);
  40. if(!$ret){
  41. \exception('数据保存失败');
  42. }
  43. $applyId = $this->id;
  44. // formJson日志记录
  45. $res = Db::name('apply_form_log')->insert([
  46. 'apply_id' => $applyId,
  47. 'user_id' => $data['user_id'],
  48. 'form_json' => $data['form_json'],
  49. 'create_time' => date('Y-m-d H:i:s')
  50. ]);
  51. if(!$res){
  52. \exception('数据保存失败');
  53. }
  54. if($data['advanced']){
  55. $aret = model('ApplyRecord')->advancedStartAction($applyId,$data['org_id'],$data['user_id'],$data['form_json'],$data['advanced'],$extra);
  56. if(!$aret){
  57. \exception(model('ApplyRecord')->getError());
  58. }
  59. }
  60. // 创建审批记录
  61. $ret = model('ApplyRecord')->createRecord($applyId,$data);
  62. if(!$ret){
  63. \exception('审批记录生成失败'.model('ApplyRecord')->getError());
  64. }
  65. $this->commit();
  66. }catch (Exception $e){
  67. $this->rollback();
  68. HelpHander::error('操作失败'.$e->getMessage());
  69. }
  70. return true;
  71. }
  72. public function formatFlowJson($info){
  73. $flowJson = json_decode($info['flow_json'],true);
  74. foreach ($flowJson as $k=>$v){
  75. if($v['type'] == 2||$v['type'] == 8){ // 审批或加签,检查节点是否有打回的记录,有的话更新审批人
  76. $buid = Db::name('apply_record')
  77. ->where('apply_id',$info['id'])
  78. ->where('del',1)
  79. ->where('nodeid',$v['id'])
  80. ->where('status','<>',3) // 被转交的不用取
  81. ->where('back_user_id','>',0) // 被转交的不用取
  82. ->order('id desc')
  83. ->value('back_user_id');
  84. if($buid){
  85. $ulist = Db::name('apply_record')
  86. ->alias('a')
  87. ->join('user_info u','a.user_id = u.user_id')
  88. ->where('a.apply_id',$info['id'])
  89. ->where('a.del',1)
  90. ->where('a.status','<>',3) // 被转交的不用取
  91. ->where('a.nodeid',$v['id'])
  92. ->where('a.back_user_id',$buid)
  93. ->field('a.user_id,u.name')
  94. ->order('a.id asc')
  95. ->select();
  96. if($ulist){
  97. $userList = [];
  98. foreach ($ulist as $kk=>$vv){
  99. $userList[] = [
  100. "userId" => $vv['user_id'],
  101. "userName" => $vv['name'],
  102. "shortName" => mb_strlen($vv['name']) > 2?mb_substr($vv['name'], 0, -2):$vv['name'],
  103. ];
  104. }
  105. $flowJson[$k]['userList'] = $userList;
  106. }
  107. }
  108. }
  109. }
  110. $info['flow_json'] = json_encode($flowJson);
  111. return $info;
  112. }
  113. public function info($id,$userId,$orgId=0){
  114. $info = $this->where('id',$id)->where('del',0)->find();
  115. if(!$info){
  116. HelpHander::error('数据不存在');
  117. }
  118. // 格式化一下审批原始数据,把打回会签和被转交的加入流程中,转交的去除掉
  119. $info = $this->formatFlowJson($info);
  120. $user = Db::name('user_info')->where('user_id',$info['user_id'])->field('name,position')->find();
  121. $info['userName'] = $user['name'];
  122. $info['userPosition'] = $user['position'];
  123. $info['depName'] = Db::name('dep')->where('id',$info['dep_id'])->value('name');
  124. $info['approvalName'] = Db::name('approval')->where('id',$info['approval_id'])->value('title');
  125. if($info['advanced'] == 3||$info['advanced'] == 4){
  126. $info['contractSn'] = Db::name('contract')->where('apply_id',$info['id'])->value('contract_sn');
  127. }else{
  128. $info['contractSn'] = '';
  129. }
  130. if($info['advanced'] == 20){ // 因私出国
  131. $abroad = Db::name('abroad_apply')->where('apply_id',$info['id'])->find();
  132. $info['abroad_year'] = $abroad['year'];
  133. $info['abroad_sn'] = $abroad['sn'];
  134. }
  135. $info['create_time2'] = date('Y年m月d日',strtotime($info['create_time']));
  136. if($info['advanced'] == 5){
  137. $pay_apply_id = Db::name('contract_pay')->where('apply_id',$info['id'])->value('pay_apply_id');
  138. $contract = Db::name('contract')->where('apply_id',$pay_apply_id)->find();
  139. $info['contractSn'] = $contract['contract_sn'];
  140. $info['contractTitle'] = $contract['title'];
  141. $info['contractCompany2'] = $contract['company2'];
  142. $info['programSn'] = '';
  143. $info['program'] = Db::name('program')->where('id',$contract['program_id'])->value('title');
  144. }else if($info['advanced'] == 3||$info['advanced'] == 4){
  145. $contract = Db::name('contract')->where('apply_id',$info['id'])->find();
  146. $info['contractSn'] = $contract['contract_sn'];
  147. $info['contractTitle'] = $contract['title'];
  148. $info['contractCompany2'] = $contract['company2'];
  149. $info['program'] = '';
  150. $info['programSn'] = $contract['program_sn'];
  151. }else{
  152. $info['contractSn'] = '';
  153. $info['contractTitle'] = '';
  154. $info['contractCompany2'] = '';
  155. $info['program'] = '';
  156. $info['programSn'] = '';
  157. }
  158. $applyFormLogDtoList = Db::name('apply_form_log')
  159. ->alias('afl')
  160. ->join('user_info ui','ui.user_id = afl.user_id')
  161. ->where('afl.apply_id',$id)
  162. ->field('afl.*,ui.name')
  163. ->select();
  164. $info['applyFormLogDtoList'] = $applyFormLogDtoList?$applyFormLogDtoList:[];
  165. $applyRecordDtoList = Db::name('apply_record')
  166. ->alias('afl')
  167. ->join('user_info ui','ui.user_id = afl.user_id')
  168. ->where('afl.apply_id',$id)
  169. ->where('afl.del',0)
  170. ->field('afl.*,ui.name')
  171. ->order('afl.id asc')
  172. ->select();
  173. $applyRecordDtoList = $applyRecordDtoList?$applyRecordDtoList:[];
  174. foreach ($applyRecordDtoList as $k=>$v){
  175. $isBack = 0;
  176. if(in_array($v['type'],[2,8])){
  177. // 判断是否有上个审批节点
  178. $f = Db::name('apply_record')
  179. ->where('status',1)
  180. ->where('id','<',$v['id'])
  181. ->where('apply_id',$v['apply_id'])
  182. ->where('type','in',[2,8])
  183. ->where('del',0)
  184. ->where('nodeid','<>',$v['nodeid'])
  185. ->order('id desc')
  186. ->find();
  187. if($f){
  188. $isBack = 1;
  189. }
  190. }
  191. $applyRecordDtoList[$k]['is_back'] = $isBack;
  192. }
  193. $info['applyRecordDtoList'] = $applyRecordDtoList;
  194. // 撤销功能 只能用户自己撤销(审批人撤销功能去掉)
  195. $isShowUndo = true;
  196. $undo = Db::name('apply_record')
  197. ->where('apply_id',$id)
  198. ->where('del',0)
  199. ->where('status','in',[1,2])
  200. ->where('type','in',[2,8])
  201. ->find();
  202. if(!$undo && $userId == $info['user_id'] && $info['status'] == 1){
  203. $isShowUndo = false;
  204. }
  205. $data = [
  206. 'apply' => $info->toArray(),
  207. 'isShowUndo' => $isShowUndo
  208. ];
  209. return $data;
  210. }
  211. // 审批单 传$userId既是我发起的审批单
  212. public function lists($page,$size,$userId,$orgId,$status,$approvalId,$info,$startTime,$endTime){
  213. if($userId){
  214. $map[] = ['user_id','=',$userId];
  215. }
  216. if($orgId){
  217. $map[] = ['org_id','=',$orgId];
  218. }
  219. if($status){
  220. $map[] = ['status','=',$status];
  221. }
  222. if($approvalId){
  223. $map[] = ['approval_id','=',$approvalId];
  224. }
  225. if($startTime){
  226. $map[] = ['create_time','>=',$startTime];
  227. }
  228. if($endTime){
  229. $map[] = ['create_time','<=',$endTime];
  230. }
  231. if($info){
  232. $map[] = ['title|apply_sn|form_json','like','%'.$info.'%'];
  233. }
  234. $map[] = ['del','=',0];
  235. $lists = $this
  236. ->where($map)
  237. ->field('id,title,apply_sn,create_time,finish_time,status,advanced,approval_id,form_json')
  238. ->page($page,$size)
  239. ->order('id desc')
  240. ->select();
  241. foreach ($lists as $k=>$v){
  242. $ct = $this->getExtraTitle($v);
  243. if($ct){
  244. $lists[$k]['title'] = $v['title'].'【'.$ct.'】';
  245. }
  246. }
  247. $total = $this->where($map)->count();
  248. $data = [
  249. 'total' => $total,
  250. 'list' => $lists?$lists->toArray():[]
  251. ];
  252. return $data;
  253. }
  254. public function getExtraTitle($v){
  255. $ct = '';
  256. $formJson = json_decode($v['form_json'],true);
  257. if($v['advanced'] == 3){ // 合同会签
  258. $ct = Db::name('contract')->where('apply_id',$v['id'])->value('title');
  259. }else if($v['advanced'] == 4){ // 追加合同
  260. $ct = Db::name('contract')->where('apply_id',$v['id'])->value('title');
  261. }else if($v['advanced'] == 18){ // 用印
  262. $ct = Db::name('official_seal_apply')->where('apply_id',$v['id'])->value('content');
  263. }else if($v['advanced'] == 6){ // 会议室预定-会议议题
  264. $ct = Db::name('meeting_room_book')->where('apply_id',$v['id'])->value('projection');
  265. }else if($v['advanced'] == 11){ // 用车-事由
  266. $ct = Db::name('car_record')->where('apply_id',$v['id'])->value('reason');
  267. }else if($v['approval_id'] == 93){ // 请假-事由及说明
  268. $ct = $this->getValFromFormJson($formJson,6);
  269. }else if($v['approval_id'] == 99){ // 销假-原因
  270. $ct = $this->getValFromFormJson($formJson,10);
  271. }else if($v['approval_id'] == 101){ // 离京报告表-事由及说明
  272. $ct = $this->getValFromFormJson($formJson,3);
  273. }else if($v['approval_id'] == 120){ // 签报--标题
  274. $ct = $this->getValFromFormJson($formJson,3);
  275. }else if($v['approval_id'] == 89){ // 发文审批--发文事项
  276. $ct = $this->getValFromFormJson($formJson,2);
  277. }else if($v['approval_id'] == 118){ // 上会文件--会议名称
  278. $ct1 = $this->getValFromFormJson($formJson,2);
  279. $ct2 = $this->getValFromFormJson($formJson,3);
  280. $ct = $ct1.' | '.$ct2;
  281. }else if($v['approval_id'] == 128){ // 官网发布审批--文章标题
  282. $ct = $this->getValFromFormJson($formJson,7);
  283. }else if($v['approval_id'] == 130){ // 合同借阅--借阅合同
  284. $ct = $this->getValFromFormJson($formJson,0);
  285. }else if($v['approval_id'] == 131){ // 档案借阅--借阅档案
  286. $ct = $this->getValFromFormJson($formJson,0);
  287. }else if($v['approval_id'] == 149){ // 董事会上会文件--会议名称
  288. $ct = $this->getValFromFormJson($formJson,0);
  289. }
  290. return $ct;
  291. }
  292. // 根据idx获取表单项的值
  293. public function getValFromFormJson($formJson,$idx){
  294. $ct = '';
  295. foreach ($formJson as $fk=>$fv){
  296. if($fv['idx'] == $idx){
  297. $ct = !empty($fv['values'])?$fv['values']:'';
  298. break;
  299. }
  300. }
  301. return $ct;
  302. }
  303. public function queryUserApplyList($page,$size,$param,$userId,$orgId){
  304. if($userId){
  305. $map[] = ['a.user_id','=',$userId];
  306. }
  307. if($orgId){
  308. $map[] = ['a.org_id','=',$orgId];
  309. }
  310. if($param){
  311. $map[] = ['a.title|a.apply_sn|ui.name','like','%'.$param.'%'];
  312. }
  313. $map[] = ['a.del','=',0];
  314. $lists = Db::name('apply')
  315. ->alias('a')
  316. ->join('user_info ui','ui.user_id = a.user_id')
  317. ->field('a.id,a.title,a.apply_sn,a.create_time,a.update_time,a.status,ui.name as userName')
  318. ->where($map)
  319. ->page($page,$size)
  320. ->order('a.id desc')
  321. ->select();
  322. return $lists?$lists:[];
  323. }
  324. public function appMyApplyList($page,$size,$param,$type,$userId,$orgId){
  325. if($userId){
  326. $map[] = ['ar.user_id','=',$userId];
  327. }
  328. if($orgId){
  329. $map[] = ['a.org_id','=',$orgId];
  330. }
  331. if($param){
  332. $map[] = ['a.title|a.apply_sn|ui.name','like','%'.$param.'%'];
  333. }
  334. if($type >= 0){
  335. $map[] = ['ar.status','=',$type];
  336. }
  337. $map[] = ['a.del','=',0];
  338. $map[] = ['ar.type','in',[2,8]];
  339. $map[] = ['ar.del','=',0];
  340. $lists = Db::name('apply_record')
  341. ->alias('ar')
  342. ->join('apply a','a.id = ar.apply_id')
  343. ->join('user_info ui','ui.user_id = a.user_id')
  344. ->field('a.id,a.title,a.apply_sn,a.create_time,a.update_time,a.status,ui.name as userName,ar.status as arStatus')
  345. ->where($map)
  346. ->page($page,$size)
  347. ->order('ar.id desc')
  348. ->select();
  349. return $lists?$lists:[];
  350. }
  351. public function queryCopyUserApply($page,$size,$param,$status,$type,$userId,$orgId){
  352. if($userId){
  353. $map[] = ['ar.user_id','=',$userId];
  354. }
  355. if($orgId){
  356. $map[] = ['a.org_id','=',$orgId];
  357. }
  358. if($param){
  359. $map[] = ['a.title|a.apply_sn|ui.name','like','%'.$param.'%'];
  360. }
  361. if($status >= 0){
  362. $map[] = ['ar.status','=',$status];
  363. }
  364. $map[] = ['ar.type','=',$type];
  365. $map[] = ['a.del','=',0];
  366. $map[] = ['ar.del','=',0];
  367. $lists = Db::name('apply_record')
  368. ->alias('ar')
  369. ->join('apply a','a.id = ar.apply_id')
  370. ->join('user_info ui','ui.user_id = a.user_id')
  371. ->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')
  372. ->where($map)
  373. ->page($page,$size)
  374. ->order('ar.id desc')
  375. ->select();
  376. return $lists?$lists:[];
  377. }
  378. // 我审批(抄送,执行)的流程
  379. public function userApplyList($page,$size,$userId,$orgId,$type,$status,$approvalId,$info,$startTime,$endTime){
  380. if($userId){
  381. $map[] = ['ar.user_id','=',$userId];
  382. }
  383. if($orgId){
  384. $map[] = ['a.org_id','=',$orgId];
  385. }
  386. if($status >= 0){
  387. $map[] = ['ar.status','=',$status];
  388. }
  389. if($approvalId){
  390. $map[] = ['a.approval_id','=',$approvalId];
  391. }
  392. if($startTime){
  393. $map[] = ['a.create_time','>=',$startTime];
  394. }
  395. if($endTime){
  396. $map[] = ['a.create_time','<=',$endTime];
  397. }
  398. if($info){
  399. $map[] = ['a.title|a.apply_sn|a.form_json','like','%'.$info.'%'];
  400. }
  401. if($type){
  402. if($type == 2){
  403. $map[] = ['ar.type','in',[2,8]];
  404. }else{
  405. $map[] = ['ar.type','=',$type];
  406. }
  407. }
  408. $map[] = ['a.del','=',0];
  409. $map[] = ['ar.del','=',0];
  410. $lists = Db::name('apply_record')
  411. ->alias('ar')
  412. ->join('apply a','a.id = ar.apply_id')
  413. ->where($map)
  414. ->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')
  415. ->page($page,$size)
  416. ->order('ar.id desc')
  417. ->select();
  418. foreach ($lists as $k=>$v){
  419. $ct = $this->getExtraTitle($v);
  420. if($ct){
  421. $lists[$k]['title'] = $v['title'].'【'.$ct.'】';
  422. }
  423. }
  424. $total =Db::name('apply_record')
  425. ->alias('ar')
  426. ->join('apply a','a.id = ar.apply_id')
  427. ->where($map)->count();
  428. $data = [
  429. 'total' => $total,
  430. 'list' => $lists?$lists:[]
  431. ];
  432. return $data;
  433. }
  434. // 撤销 $type 0=管理员撤销 1=用户撤销
  435. public function undo($applyId,$userId,$type=0){
  436. $info = Db::name('apply')->where('id',$applyId)->where('del',0)->find();
  437. if(!$info){
  438. HelpHander::error('数据不存在');
  439. }
  440. if($info['status'] != 1){
  441. HelpHander::error('该状态无法撤销');
  442. }
  443. if($type == 1 && $info['user_id'] != $userId){
  444. HelpHander::error('无权限撤销');
  445. }
  446. $undo = Db::name('apply_record')
  447. ->where('apply_id',$applyId)
  448. ->where('del',0)
  449. ->where('status','in',[1,2])
  450. ->where('type','in',[2,8])
  451. ->find();
  452. if($undo){
  453. HelpHander::error('已有人审核,无法撤销');
  454. }
  455. Db::startTrans();
  456. try{
  457. $ret = Db::name('apply')->where('id',$applyId)->update(['status'=>3,'update_time'=>date('Y-m-d H:i:s')]);
  458. if(!$ret){
  459. \exception('撤销失败');
  460. }
  461. Db::name('apply_record')->where('apply_id',$applyId)->setField('status',5);
  462. $result = model('ApplyRecord')->advancedDisagreeAction($applyId,$info['advanced']);
  463. if(!$result){
  464. \exception('操作失败');
  465. }
  466. Db::commit();
  467. }catch (Exception $e){
  468. Db::rollback();
  469. HelpHander::error('撤销失败');
  470. }
  471. return true;
  472. }
  473. // 关联审批单列表
  474. public function selectApplyListByType($page,$size,$userId,$orgId,$type,$approvalId,$param){
  475. if($orgId){
  476. $map[] = ['a.org_id','=',$orgId];
  477. }
  478. // if($approvalId){
  479. // $map[] = ['a.approval_id','in',$approvalId];
  480. // }
  481. if($param){
  482. $map[] = ['a.title|a.apply_sn','like','%'.$param.'%'];
  483. }
  484. $map[] = ['a.del','=',0];
  485. $map[] = ['a.advanced','=',1];
  486. if($type == 2){ // 我发起的
  487. if($userId){
  488. $map[] = ['a.user_id','=',$userId];
  489. }
  490. $lists = Db::name('apply')
  491. ->alias('a')
  492. ->join('user_info ui','ui.user_id = a.user_id')
  493. ->where($map)
  494. ->field('a.id,a.title,a.apply_sn,a.create_time,ui.name')
  495. ->page($page,$size)
  496. ->order('a.id desc')
  497. ->select();
  498. $total = Db::name('apply')
  499. ->alias('a')
  500. ->join('user_info ui','ui.user_id = a.user_id')
  501. ->where($map)->count();
  502. }else{
  503. if($userId){
  504. $map[] = ['ar.user_id','=',$userId];
  505. }
  506. if($type == 1){ // 我审批的
  507. $map[] = ['ar.type','in',[2,8]];
  508. }else if($type == 3){
  509. $map[] = ['ar.type','=',3];
  510. }else if($type == 4){
  511. $map[] = ['ar.type','=',9];
  512. }
  513. $map[] = ['ar.del','=',0];
  514. $lists = Db::name('apply_record')
  515. ->alias('ar')
  516. ->join('apply a','a.id = ar.apply_id')
  517. ->join('user_info ui','ui.user_id = a.user_id')
  518. ->where($map)
  519. ->field('a.id,a.title,a.apply_sn,a.create_time,ui.name')
  520. ->page($page,$size)
  521. ->order('a.id desc')
  522. ->group('ar.apply_id')
  523. ->select();
  524. $total = Db::name('apply_record')
  525. ->alias('ar')
  526. ->join('apply a','a.id = ar.apply_id')
  527. ->join('user_info ui','ui.user_id = a.user_id')
  528. ->group('ar.apply_id')
  529. ->where($map)->count();
  530. }
  531. $data = [
  532. 'total' => $total,
  533. 'list' => $lists?$lists:[]
  534. ];
  535. return $data;
  536. }
  537. // 催办
  538. public function remindAudit($applyId,$userId){
  539. $info = Db::name('apply')
  540. ->where('id',$applyId)
  541. ->where('user_id',$userId)
  542. ->where('del',0)
  543. ->find();
  544. if(!$info){
  545. HelpHander::error('数据不存在');
  546. }
  547. if($info['status'] != 1){
  548. HelpHander::error('无权限催办');
  549. }
  550. $ret = $this->remindAuditCommon($info);
  551. if(!$ret){
  552. HelpHander::error('催办失败');
  553. }
  554. return true;
  555. }
  556. // 根据审批单信息催办
  557. public function remindAuditCommon($info){
  558. $records = Db::name('apply_record')
  559. ->where('nodeid',$info['nodeid'])
  560. ->where('apply_id',$info['id'])
  561. ->where('status',0)
  562. ->where('del',0)
  563. ->field('id,user_id')
  564. ->select();
  565. $records = $records?$records:[];
  566. Db::startTrans();
  567. try{
  568. foreach ($records as $k=>$v){
  569. // 生成消息及推送
  570. $context = $info['title'].'需要您审批,请尽快处理。';
  571. $res = model('Message')->add(3,$v['id'],2,$v['user_id'],$info['org_id'],$context);
  572. if(!$res){
  573. \exception('消息生成失败');
  574. }
  575. }
  576. Db::commit();
  577. }catch (Exception $e){
  578. Db::rollback();
  579. return false;
  580. }
  581. return true;
  582. }
  583. // 关联主合同审批单
  584. public function selectContractByType($page,$size,$userId,$orgId,$type,$param){
  585. if($orgId){
  586. $map[] = ['a.org_id','=',$orgId];
  587. }
  588. if($param){
  589. $map[] = ['a.title|a.apply_sn','like','%'.$param.'%'];
  590. }
  591. $map[] = ['a.del','=',0];
  592. $map[] = ['c.status','=',1];
  593. $map[] = ['c.type','=',0];
  594. if($type == 2){ // 我发起的
  595. if($userId){
  596. $map[] = ['a.user_id','=',$userId];
  597. }
  598. $lists = Db::name('apply')
  599. ->alias('a')
  600. ->join('user_info ui','ui.user_id = a.user_id')
  601. ->join('contract c','c.apply_id = a.id')
  602. ->where($map)
  603. ->field('a.id,a.title,a.apply_sn,a.create_time,ui.name,c.title as contractName')
  604. ->page($page,$size)
  605. ->order('a.id desc')
  606. ->select();
  607. $total = Db::name('apply')
  608. ->alias('a')
  609. ->join('user_info ui','ui.user_id = a.user_id')
  610. ->join('contract c','c.apply_id = a.id')
  611. ->where($map)->count();
  612. }else{
  613. if($userId){
  614. $map[] = ['ar.user_id','=',$userId];
  615. }
  616. if($type == 1){ // 我审批的
  617. $map[] = ['ar.type','in',[2,8]];
  618. }else if($type == 3){
  619. $map[] = ['ar.type','=',3];
  620. }else if($type == 4){
  621. $map[] = ['ar.type','=',9];
  622. }
  623. $map[] = ['ar.del','=',0];
  624. $lists = Db::name('apply_record')
  625. ->alias('ar')
  626. ->join('apply a','a.id = ar.apply_id')
  627. ->join('user_info ui','ui.user_id = a.user_id')
  628. ->join('contract c','c.apply_id = a.id')
  629. ->where($map)
  630. ->field('a.id,a.title,a.apply_sn,a.create_time,ui.name,c.title as contractName')
  631. ->page($page,$size)
  632. ->order('a.id desc')
  633. ->group('ar.apply_id')
  634. ->select();
  635. $total =Db::name('apply_record')
  636. ->alias('ar')
  637. ->join('apply a','a.id = ar.apply_id')
  638. ->join('user_info ui','ui.user_id = a.user_id')
  639. ->join('contract c','c.apply_id = a.id')
  640. ->group('ar.apply_id')
  641. ->where($map)->count();
  642. }
  643. $data = [
  644. 'total' => $total,
  645. 'list' => $lists?$lists:[]
  646. ];
  647. return $data;
  648. }
  649. // 关联未结算清合同审批单
  650. public function selectContractFinishByType($page,$size,$userId,$orgId,$type,$param,$ispay=1){
  651. if($orgId){
  652. $map[] = ['a.org_id','=',$orgId];
  653. }
  654. if($param){
  655. $map[] = ['a.title|a.apply_sn','like','%'.$param.'%'];
  656. }
  657. if(in_array($ispay,[1,2])){
  658. $map[] = ['c.ispay','=',$ispay];
  659. }
  660. $map[] = ['a.del','=',0];
  661. $map[] = ['c.status','=',1];
  662. $map[] = ['c.finish','=',0];
  663. if($type == 2){ // 我发起的
  664. if($userId){
  665. $map[] = ['a.user_id','=',$userId];
  666. }
  667. $lists = Db::name('apply')
  668. ->alias('a')
  669. ->join('user_info ui','ui.user_id = a.user_id')
  670. ->join('contract c','c.apply_id = a.id')
  671. ->where($map)
  672. ->field('a.id,a.title,a.apply_sn,a.create_time,ui.name,c.title as contractName,c.money,c.pay_money')
  673. ->page($page,$size)
  674. ->order('a.id desc')
  675. ->select();
  676. foreach ($lists as $k=>$v){
  677. $paybl = floatval($v['money']) > 0?round($v['pay_money'] / $v['money'],2) * 100:0;
  678. $lists[$k]['paybl'] = $paybl.'%';
  679. }
  680. $total = Db::name('apply')
  681. ->alias('a')
  682. ->join('user_info ui','ui.user_id = a.user_id')
  683. ->join('contract c','c.apply_id = a.id')
  684. ->where($map)->count();
  685. }else{
  686. if($userId){
  687. $map[] = ['ar.user_id','=',$userId];
  688. }
  689. if($type == 1){ // 我审批的
  690. $map[] = ['ar.type','in',[2,8]];
  691. }else if($type == 3){
  692. $map[] = ['ar.type','=',3];
  693. }else if($type == 4){
  694. $map[] = ['ar.type','=',9];
  695. }
  696. $map[] = ['ar.del','=',0];
  697. $lists = Db::name('apply_record')
  698. ->alias('ar')
  699. ->join('apply a','a.id = ar.apply_id')
  700. ->join('user_info ui','ui.user_id = a.user_id')
  701. ->join('contract c','c.apply_id = a.id')
  702. ->where($map)
  703. ->field('a.id,a.title,a.apply_sn,a.create_time,ui.name,c.title as contractName,c.money,c.pay_money')
  704. ->page($page,$size)
  705. ->order('a.id desc')
  706. ->group('ar.apply_id')
  707. ->select();
  708. foreach ($lists as $k=>$v){
  709. $paybl = floatval($v['money']) > 0?round($v['pay_money'] / $v['money'],2) * 100:0;
  710. $lists[$k]['paybl'] = $paybl.'%';
  711. }
  712. $total =Db::name('apply_record')
  713. ->alias('ar')
  714. ->join('apply a','a.id = ar.apply_id')
  715. ->join('user_info ui','ui.user_id = a.user_id')
  716. ->join('contract c','c.apply_id = a.id')
  717. ->group('ar.apply_id')
  718. ->where($map)->count();
  719. }
  720. $data = [
  721. 'total' => $total,
  722. 'list' => $lists?$lists:[]
  723. ];
  724. return $data;
  725. }
  726. // 删除审批单
  727. public function del($applyId,$userId){
  728. if($userId != 100003 && $userId != 100006){
  729. HelpHander::error('无权限操作');
  730. }
  731. $info = Db::name('apply')->where('id',$applyId)->where('del',0)->find();
  732. if(!$info){
  733. HelpHander::error('数据不存在');
  734. }
  735. Db::startTrans();
  736. try{
  737. $ret = Db::name('apply')->where('id',$applyId)->update(['del'=>1,'update_time'=>date('Y-m-d H:i:s')]);
  738. if(!$ret){
  739. \exception('删除失败');
  740. }
  741. // 高级组件处理
  742. if($info['advanced'] > 0 && !in_array($info['status'],[3,4,5])){
  743. switch ($info['advanced']){
  744. case '1': // 请假 --
  745. $ret = model('AttendanceLeave')->advancedDisagreeLeave($applyId);
  746. break;
  747. case '3': // 合同 --
  748. $ret = model('Contract')->advancedDelContract($applyId);
  749. if(!$ret){
  750. \exception(model('Contract')->getError());
  751. }
  752. break;
  753. case '4': // 追加合同 --
  754. $ret = model('Contract')->advancedDelAddContract($applyId);
  755. if(!$ret){
  756. \exception(model('Contract')->getError());
  757. }
  758. break;
  759. case '5': // 合同付款 --
  760. $ret = model('ContractPay')->advancedDelContractPay($applyId);
  761. break;
  762. case '6': // 会议室预定 --
  763. $ret = model('MeetingRoom')->advancedDisagreeMeetingRoom($applyId);
  764. break;
  765. case '7': // 合同收款 --
  766. $ret = model('ContractPay')->advancedDelContractGet($applyId);
  767. break;
  768. case '8': // 销假 --
  769. $ret = model('AttendanceLeaveTerminate')->advancedDelLeaveTerminate($applyId);
  770. break;
  771. case '9': // 离京报告 --
  772. $ret = model('LeaveBj')->advancedDisagreeLeaveBj($applyId);
  773. break;
  774. case '10': // 办公物品 --
  775. $ret = model('OfficeReceive')->advancedDisagreeOfficeReceive($applyId);
  776. break;
  777. case '11': // 用车 --
  778. $ret = model('CarRecord')->advancedDisagreeCarRecord($applyId);
  779. break;
  780. case '17': // 预算 --
  781. $ret = model('BudgetApply')->advancedDelBudgetApply($applyId);
  782. if(!$ret){
  783. \exception(model('BudgetApply')->getError());
  784. }
  785. break;
  786. case '18': // 用印 --
  787. $ret = model('OfficialSealApply')->advancedDisagreeSealApply($applyId);
  788. break;
  789. case '19': // 中心发文 --
  790. $ret = model('PostApply')->advancedDisagreePostApply($applyId);
  791. break;
  792. case '20': // 因私出国 --
  793. $ret = model('AbroadApply')->advancedDisagreeAbroadApply($applyId);
  794. break;
  795. case '21': // 日常支出
  796. $ret = model('BudgetPay')->advancedDelBudgetPay($applyId);
  797. break;
  798. default:
  799. $ret = true;
  800. break;
  801. }
  802. if(!$ret){
  803. \exception('删除失败');
  804. }
  805. }
  806. // 删除日志
  807. $ret = Db::name('del_log')->insert([
  808. 'user_id' => $userId,
  809. 'type' => 1,
  810. 'from_id' => $applyId,
  811. 'create_time' => date('Y-m-d H:i:s'),
  812. 'content' => '删除了'.$info['title'],
  813. ]);
  814. if(!$ret){
  815. \exception('操作失败');
  816. }
  817. Db::commit();
  818. }catch (Exception $e){
  819. trace($e->getMessage());
  820. Db::rollback();
  821. HelpHander::error($e->getMessage());
  822. }
  823. return true;
  824. }
  825. }