Examine.php 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 审批
  4. // +----------------------------------------------------------------------
  5. // | Author: Michael_xu | gengxiaoxu@5kcrm.com
  6. // +----------------------------------------------------------------------
  7. namespace app\oa\model;
  8. use think\Db;
  9. use app\admin\model\Common;
  10. use app\admin\model\Message;
  11. use think\Request;
  12. use think\Validate;
  13. use app\admin\model\Field;
  14. class Examine extends Common
  15. {
  16. /**
  17. * 为了数据库的整洁,同时又不影响Model和Controller的名称
  18. * 我们约定每个模块的数据表都加上相同的前缀,比如CRM模块用crm作为数据表前缀
  19. */
  20. protected $name = 'oa_examine';
  21. protected $createTime = 'create_time';
  22. protected $updateTime = 'update_time';
  23. protected $autoWriteTimestamp = true;
  24. private $statusArr = ['待审核', '审核中', '审核通过', '已拒绝', '已撤回'];
  25. /**
  26. * [getDataList 审批list]
  27. * @param [string] $map [查询条件]
  28. * @param [number] $page [当前页数]
  29. * @param [number] $limit [每页数量]
  30. * @return
  31. * @author Michael_xu
  32. */
  33. public function getDataList($request)
  34. {
  35. $userModel = new \app\admin\model\User();
  36. $fileModel = new \app\admin\model\File();
  37. $recordModel = new \app\admin\model\Record();
  38. $examine_by = $request['examine_by']; //1待我审批 2我已审批 all 全部
  39. $user_id = $request['user_id'];
  40. $bi = $request['bi_types'];
  41. $check_status = $request['check_status']; //0 待审批 2 审批通过 4 审批拒绝 all 全部
  42. unset($request['by']);
  43. unset($request['bi_types']);
  44. unset($request['user_id']);
  45. unset($request['check_status']);
  46. unset($request['examine_by']);
  47. $request = $this->fmtRequest($request);
  48. $map = $request['map'] ?: [];
  49. if (isset($map['search']) && $map['search']) {
  50. //普通筛选
  51. $map['examine.content'] = ['like', '%' . $map['search'] . '%'];
  52. } else {
  53. $map = where_arr($map, 'oa', 'examine', 'index'); //高级筛选
  54. }
  55. unset($map['search']);
  56. //审批类型
  57. $map['examine.category_id'] = $map['examine.category_id'] ?: array('gt', 0);
  58. $map_str = '';
  59. $logmap = '';
  60. switch ($examine_by) {
  61. case 'all' :
  62. //如果超管则能看到全部
  63. if (!isSuperAdministrators($user_id)) {
  64. $map_str = "(( check_user_id LIKE '%," . $user_id . ",%' OR check_user_id = " . $user_id . " ) OR ( flow_user_id LIKE '%," . $user_id . ",%' OR `flow_user_id` = " . $user_id . " ) )";
  65. }
  66. $map['examine.create_user_id'] = ['<>',$user_id];
  67. break;
  68. case '1' :
  69. $map['check_user_id'] = [['like', '%,' . $user_id . ',%']];
  70. break; //待审
  71. case '2' :
  72. $map_str = "(( check_user_id LIKE '%," . $user_id . ",%' OR check_user_id = " . $user_id . " )
  73. OR ( flow_user_id LIKE '%," . $user_id . ",%' OR `flow_user_id` = " . $user_id . " ) )";
  74. // $map['flow_user_id'] = [['like', '%,' . $user_id . ',%'], ['eq', $user_id], 'or'];
  75. break; //已审
  76. default:
  77. $map['examine.create_user_id'] = $user_id;
  78. break;
  79. }
  80. $order = 'examine.create_time desc,examine.update_time desc';
  81. //发起时间
  82. if ($map['examine.between_time'][0] && $map['examine.between_time'][1]) {
  83. $start_time = $map['examine.between_time'][0];
  84. $end_time = $map['examine.between_time'][1];
  85. $map['examine.create_time'] = array('between', array($start_time, $end_time));
  86. }
  87. unset($map['examine.between_time']);
  88. //审核状态 0 待审批 2 审批通过 4 审批拒绝 all 全部
  89. if (isset($check_status)) {
  90. if ($check_status == 'all') {
  91. $map['examine.check_status'] = ['egt', 0];
  92. if (isSuperAdministrators($user_id)) {
  93. unset($map['examine.create_user_id']);
  94. }
  95. } elseif ($check_status == 4) {
  96. $map['examine.check_status'] = ['eq', 3];
  97. } elseif ($check_status == 0) {
  98. $map['examine.check_status'] = ['<=', 1];
  99. } else {
  100. $map['examine.check_status'] = $check_status;
  101. }
  102. }else{
  103. if ($examine_by == 'all') {
  104. $map['examine.check_status'] = ['egt', 0];
  105. } elseif ($examine_by == 1) {
  106. $map['examine.check_status'] = ['elt', 1];
  107. } elseif($examine_by == 2) {
  108. $map['examine.check_status'] = ['egt', 2];
  109. }
  110. }
  111. $join = [
  112. ['__ADMIN_USER__ user', 'user.id = examine.create_user_id', 'LEFT'],
  113. ['__OA_EXAMINE_CATEGORY__ examine_category', 'examine_category.category_id = examine.category_id', 'LEFT'],
  114. ];
  115. $list_view = db('oa_examine')
  116. ->alias('examine')
  117. ->where($map_str)
  118. ->where($map)
  119. ->join($join);
  120. $list = $list_view
  121. ->page($request['page'], $request['limit'])
  122. ->field('examine.*,user.realname,user.thumb_img,examine_category.title as category_name,examine_category.category_id as examine_config,examine_category.icon as examineIcon')
  123. ->order($order)
  124. ->select();
  125. $dataCount = $this->alias('examine')
  126. ->where($map_str)
  127. ->where($map)
  128. ->join($join)
  129. ->count('examine_id');
  130. $admin_user_ids = $userModel->getAdminId();
  131. foreach ($list as $k => $v) {
  132. $list[$k]['create_user_info'] = $userModel->getUserById($v['create_user_id']);
  133. $causeCount = 0;
  134. $causeTitle = '';
  135. $duration = $v['duration'] ?: '0.0';
  136. $money = $v['money'] ?: '0.00';
  137. if (in_array($v['category_id'], ['3', '5'])) {
  138. $causeCount = db('oa_examine_travel')->where(['examine_id' => $v['examine_id']])->count() ?: 0;
  139. if ($v['category_id'] == 3) $causeTitle = $causeCount . '个行程,共' . $duration . '天';
  140. if ($v['category_id'] == 5) $causeTitle = $causeCount . '个报销事项,共' . $money . '元';
  141. //附件
  142. $fileList = [];
  143. $imgList = [];
  144. $where = [];
  145. $where['module'] = 'oa_examine_travel';
  146. $where['module_id'] = $v['travel_id'];
  147. $newFileList = [];
  148. $newFileList = $fileModel->getDataList($where);
  149. foreach ($newFileList['list'] as $val) {
  150. if ($val['types'] == 'file') {
  151. $fileList[] = $val;
  152. } else {
  153. $imgList[] = $val;
  154. }
  155. }
  156. $list[$k]['fileList'] = $fileList ?: [];
  157. $list[$k]['imgList'] = $imgList ?: [];
  158. }
  159. $list[$k]['causeTitle'] = $causeTitle;
  160. $list[$k]['causeCount'] = $causeCount ?: 0;
  161. //关联业务
  162. $relationArr = [];
  163. $relationArr = $recordModel->getListByRelationId('examine', $v['examine_id']);
  164. $list[$k]['businessList'] = $relationArr['businessList'];
  165. $list[$k]['contactsList'] = $relationArr['contactsList'];
  166. $list[$k]['contractList'] = $relationArr['contractList'];
  167. $list[$k]['customerList'] = $relationArr['customerList'];
  168. //附件
  169. $fileList = [];
  170. $imgList = [];
  171. $where = [];
  172. $where['module'] = 'oa_examine';
  173. $where['module_id'] = $v['examine_id'];
  174. $newFileList = [];
  175. $newFileList = $fileModel->getDataList($where);
  176. foreach ($newFileList['list'] as $val) {
  177. if ($val['types'] == 'file') {
  178. $fileList[] = $val;
  179. } else {
  180. $imgList[] = $val;
  181. }
  182. }
  183. $list[$k]['fileList'] = $fileList ?: [];
  184. $list[$k]['imgList'] = $imgList ?: [];
  185. //创建人或管理员有撤销权限
  186. $permission = [];
  187. $is_recheck = 0;
  188. $is_update = 0;
  189. $is_delete = 0;
  190. $is_check = 0;
  191. //创建人或负责人或管理员有撤销权限
  192. if ($v['create_user_id'] == $user_id || in_array($user_id, $admin_user_ids)) {
  193. if (!in_array($v['check_status'], ['2', '3', '4'])) {
  194. $is_recheck = 1;
  195. }
  196. }
  197. //创建人(失败、撤销状态时可编辑)
  198. if ($v['create_user_id'] == $user_id && in_array($v['check_status'], ['3', '4'])) {
  199. $is_update = 1;
  200. $is_delete = 1;
  201. $is_check = 0;
  202. $is_end = 0;
  203. }
  204. //添加审批相关信息
  205. $examineFlowModel = new \app\admin\model\ExamineFlow();
  206. $examineFlowData = $examineFlowModel->getFlowByTypes($user_id, 'oa_examine', $v['category_id']);
  207. //获取审批人信息
  208. if ($examineFlowData['config'] == 1) {
  209. //固定审批流
  210. $examineStepModel = new \app\admin\model\ExamineStep();
  211. // $nextStepData = $examineStepModel->nextStepUser($user_id, $examineFlowData['flow_id'], 'oa_examine', 0, 0, 0);
  212. $is_check = in_array($user_id, stringToArray($v['check_user_id'])) && in_array($v['check_status'], [0, 1]) ? 1 : 0;
  213. $is_end = 1;
  214. } else {
  215. $is_end = 0;
  216. if ($v['check_user_id'] == (',' . $user_id . ',') && in_array($v['check_status'], [0, 1])) {
  217. $is_check = 1;
  218. } else {
  219. $is_check = 0;
  220. }
  221. }
  222. if($v['check_status']==4){
  223. $usernames = db('admin_user')->whereIn('id', stringToArray($user_id))->column('realname');
  224. }else{
  225. $usernames = db('admin_user')->whereIn('id', stringToArray($v['check_user_id']))->column('realname');
  226. }
  227. $list[$k]['examine_name'] = implode($usernames, ',');
  228. $permission['is_check'] = $is_check;
  229. $permission['is_delete'] = $is_delete;
  230. $permission['is_recheck'] = $is_recheck;
  231. $permission['is_update'] = $is_update;
  232. $list[$k]['permission'] = $permission;
  233. $list[$k]['config'] = $is_end;
  234. $list[$k]['check_status_info'] = $this->statusArr[(int)$v['check_status']];
  235. $list[$k]['create_time'] = !empty($v['create_time']) ? date('Y-m-d H:i:s', $v['create_time']) : null;
  236. $list[$k]['update_time'] = !empty($v['update_time']) ? date('Y-m-d H:i:s', $v['update_time']) : null;
  237. }
  238. $data = [];
  239. $data['page']['list'] = $list;
  240. $data['page']['dataCount'] = $dataCount ?: 0;
  241. if ($request['page'] != 1 && (int)($request['page'] * $request['limit']) >= (int)$dataCount) {
  242. $data['page']['firstPage'] = false;
  243. $data['page']['lastPage'] = true;
  244. } else if ($request['page'] != 1 && (int)($request['page'] * $request['limit']) < (int)$dataCount) {
  245. $data['page']['firstPage'] = false;
  246. $data['page']['lastPage'] = false;
  247. } else if ($request['page'] == 1) {
  248. $data['page']['firstPage'] = true;
  249. $data['page']['lastPage'] = false;
  250. }
  251. return $data;
  252. }
  253. /**
  254. * 创建审批信息
  255. * @param
  256. * @return
  257. * @author Michael_xu
  258. */
  259. public function createData($param)
  260. {
  261. $fieldModel = new \app\admin\model\Field();
  262. $userModel = new \app\admin\model\User();
  263. $examineCategoryModel = new \app\oa\model\ExamineCategory();
  264. $examineDataModel = new \app\oa\model\ExamineData();
  265. if (!$param['category_id']) {
  266. $this->error = '参数错误';
  267. return false;
  268. }
  269. // 自动验证
  270. $validateArr = $fieldModel->validateField($this->name, $param['category_id']); //获取自定义字段验证规则
  271. $validate = new Validate($validateArr['rule'], $validateArr['message']);
  272. $result = $validate->check($param);
  273. if (!$result) {
  274. $this->error = $validate->getError();
  275. return false;
  276. }
  277. $categoryInfo = $examineCategoryModel->getDataById($param['category_id']);
  278. $fileArr = $param['file_id']; //接收表单附件
  279. unset($param['file_id']);
  280. $param['start_time'] = $param['start_time'] ? strtotime($param['start_time']) : 0;
  281. $param['end_time'] = $param['end_time'] ? strtotime($param['end_time']) : 0;
  282. if ($this->data($param)->allowField(true)->save()) {
  283. //处理自定义字段数据
  284. $resData = $examineDataModel->createData($param, $this->examine_id);
  285. if ($resData) {
  286. //处理附件关系
  287. if ($fileArr) {
  288. $fileModel = new \app\admin\model\File();
  289. $resData = $fileModel->createDataById($fileArr, 'oa_examine', $this->examine_id);
  290. if ($resData == false) {
  291. $this->error = '附件上传失败';
  292. return false;
  293. }
  294. }
  295. //相关业务
  296. $rdata = [];
  297. $rdata['customer_ids'] = $param['oaExamineRelation']['customer_ids'] ? arrayToString($param['oaExamineRelation']['customer_ids']) : '';
  298. $rdata['contacts_ids'] = $param['oaExamineRelation']['contacts_ids'] ? arrayToString($param['oaExamineRelation']['contacts_ids']) : '';
  299. $rdata['business_ids'] = $param['oaExamineRelation']['business_ids'] ? arrayToString($param['oaExamineRelation']['business_ids']) : '';
  300. $rdata['contract_ids'] = $param['oaExamineRelation']['contract_ids'] ? arrayToString($param['oaExamineRelation']['contract_ids']) : '';
  301. $rdata['examine_id'] = $this->examine_id;
  302. $rdata['status'] = 1;
  303. $rdata['create_time'] = time();
  304. Db::name('OaExamineRelation')->insert($rdata);
  305. //处理差旅相关
  306. $resTravel = true;
  307. if (in_array($param['category_id'], ['3', '5']) && $param['cause']) {
  308. $resTravel = $this->createTravelById($param['cause']['list'], $this->examine_id);
  309. }
  310. if (!$resTravel) {
  311. $this->error = '相关事项保存失败,请重试';
  312. return false;
  313. }
  314. //站内信
  315. $send_user_id = stringToArray($param['check_user_id']);
  316. (new Message())->send(
  317. Message::EXAMINE_TO_DO,
  318. [
  319. 'title' => $categoryInfo['title'],
  320. 'action_id' => $this->examine_id
  321. ],
  322. $send_user_id
  323. );
  324. $data = [];
  325. $data['examine_id'] = $this->examine_id;
  326. # 添加活动记录
  327. if (!empty($rdata['customer_ids']) || !empty($rdata['contacts_ids']) || !empty($rdata['business_ids']) || !empty($rdata['contract_ids'])) {
  328. Db::name('crm_activity')->insert([
  329. 'type' => 2,
  330. 'activity_type' => 9,
  331. 'activity_type_id' => $data['examine_id'],
  332. 'content' => '审批',
  333. 'create_user_id' => $param['create_user_id'],
  334. 'update_time' => time(),
  335. 'create_time' => time(),
  336. 'customer_ids' => !empty($rdata['customer_ids']) ? $rdata['customer_ids'] : '',
  337. 'contacts_ids' => !empty($rdata['contacts_ids']) ? $rdata['contacts_ids'] : '',
  338. 'business_ids' => !empty($rdata['business_ids']) ? $rdata['business_ids'] : '',
  339. 'contract_ids' => !empty($rdata['contract_ids']) ? $rdata['contract_ids'] : '',
  340. ]);
  341. }
  342. return $data;
  343. } else {
  344. $this->error = $examineDataModel->getError();
  345. return false;
  346. }
  347. } else {
  348. $this->error = '添加失败';
  349. return false;
  350. }
  351. }
  352. /**
  353. * 编辑审批信息
  354. *
  355. * @param $param
  356. * @param string $examine_id
  357. * @return array|bool
  358. * @throws \think\Exception
  359. * @throws \think\db\exception\DataNotFoundException
  360. * @throws \think\db\exception\ModelNotFoundException
  361. * @throws \think\exception\DbException
  362. * @throws \think\exception\PDOException
  363. */
  364. public function updateDataById($param, $examine_id = '')
  365. {
  366. $examine_id = intval($examine_id);
  367. $userModel = new \app\admin\model\User();
  368. $examineCategoryModel = new \app\oa\model\ExamineCategory();
  369. $examineDataModel = new \app\oa\model\ExamineData();
  370. $create_user_id = $param['create_user_id'];
  371. unset($param['id']);
  372. $dataInfo = db('oa_examine')->where(['examine_id' => $examine_id])->find();
  373. if (!$dataInfo) {
  374. $this->error = '数据不存在或已删除';
  375. return false;
  376. }
  377. //过滤不能修改的字段
  378. $unUpdateField = ['create_user_id', 'is_deleted', 'delete_time'];
  379. foreach ($unUpdateField as $v) {
  380. unset($param[$v]);
  381. }
  382. $categoryInfo = $examineCategoryModel->getDataById($dataInfo['category_id']);
  383. //验证
  384. $fieldModel = new \app\admin\model\Field();
  385. $validateArr = $fieldModel->validateField($this->name, $dataInfo['category_id']); //获取自定义字段验证规则
  386. $validate = new Validate($validateArr['rule'], $validateArr['message']);
  387. $result = $validate->check($param);
  388. if (!$result) {
  389. $this->error = $validate->getError();
  390. return false;
  391. }
  392. $fileArr = $param['file_id']; //接收表单附件
  393. unset($param['file_id']);
  394. $param['start_time'] = $param['start_time'] ? strtotime($param['start_time']) : 0;
  395. $param['end_time'] = $param['end_time'] ? strtotime($param['end_time']) : 0;
  396. if ($this->allowField(true)->save($param, ['examine_id' => $examine_id])) {
  397. //处理自定义字段数据
  398. $resData = $examineDataModel->createData($param, $examine_id);
  399. if ($resData) {
  400. //处理附件关系
  401. if ($fileArr) {
  402. $fileModel = new \app\admin\model\File();
  403. $resData = $fileModel->createDataById($fileArr, 'oa_examine', $examine_id);
  404. if ($resData == false) {
  405. $this->error = '附件上传失败';
  406. return false;
  407. }
  408. }
  409. //站内信
  410. $send_user_id = stringToArray($param['check_user_id']);
  411. if ($send_user_id) {
  412. (new Message())->send(
  413. Message::EXAMINE_TO_DO,
  414. [
  415. 'title' => $categoryInfo['title'],
  416. 'action_id' => $examine_id
  417. ],
  418. $send_user_id
  419. );
  420. }
  421. //相关业务
  422. Db::name('OaExamineRelation')->where('examine_id', $examine_id)->delete(); // 先删除在添加
  423. $rdata = [];
  424. $rdata['examine_id'] = $examine_id;
  425. $rdata['status'] = 1;
  426. $rdata['create_time'] = time();
  427. $rdata['customer_ids'] = $param['oaExamineRelation']['customer_ids'] ? arrayToString($param['oaExamineRelation']['customer_ids']) : '';
  428. $rdata['contacts_ids'] = $param['oaExamineRelation']['contacts_ids'] ? arrayToString($param['oaExamineRelation']['contacts_ids']) : '';
  429. $rdata['business_ids'] = $param['oaExamineRelation']['business_ids'] ? arrayToString($param['oaExamineRelation']['business_ids']) : '';
  430. $rdata['contract_ids'] = $param['oaExamineRelation']['contract_ids'] ? arrayToString($param['oaExamineRelation']['contract_ids']) : '';
  431. Db::name('OaExamineRelation')->insert($rdata);
  432. //处理差旅相关
  433. $resTravel = true;
  434. if (in_array($dataInfo['category_id'], ['3', '5']) && $param['cause']) {
  435. $resTravel = $this->updateTravelById($param['cause']['list'], $examine_id);
  436. }
  437. if (!$resTravel) {
  438. $this->error = '相关事项保存失败,请重试';
  439. return false;
  440. }
  441. // 站内信
  442. $send_user_id = stringToArray($param['check_user_id']);
  443. if ($send_user_id) {
  444. (new Message())->send(
  445. Message::EXAMINE_TO_DO,
  446. [
  447. 'title' => $categoryInfo['title'],
  448. 'action_id' => $examine_id
  449. ],
  450. $send_user_id
  451. );
  452. }
  453. $data = [];
  454. $data['examine_id'] = $examine_id;
  455. # 删除活动记录
  456. Db::name('crm_activity')->where(['activity_type' => 9, 'activity_type_id' => $examine_id])->delete();
  457. # 添加活动记录
  458. if (!empty($rdata['customer_ids']) || !empty($rdata['contacts_ids']) || !empty($rdata['business_ids']) || !empty($rdata['contract_ids'])) {
  459. Db::name('crm_activity')->insert([
  460. 'type' => 2,
  461. 'activity_type' => 9,
  462. 'activity_type_id' => $examine_id,
  463. 'content' => '审批',
  464. 'create_user_id' => $create_user_id,
  465. 'update_time' => time(),
  466. 'create_time' => time(),
  467. 'customer_ids' => !empty($rdata['customer_ids']) ? $rdata['customer_ids'] : '',
  468. 'contacts_ids' => !empty($rdata['contacts_ids']) ? $rdata['contacts_ids'] : '',
  469. 'business_ids' => !empty($rdata['business_ids']) ? $rdata['business_ids'] : '',
  470. 'contract_ids' => !empty($rdata['contract_ids']) ? $rdata['contract_ids'] : '',
  471. ]);
  472. }
  473. return $data;
  474. } else {
  475. $this->error = $examineDataModel->getError();
  476. return false;
  477. }
  478. } else {
  479. $this->error = '编辑失败';
  480. return false;
  481. }
  482. }
  483. /**
  484. * 审批数据
  485. * @param $id 审批ID
  486. * @return
  487. */
  488. public function getDataById($id = '')
  489. {
  490. $examineData = new \app\oa\model\ExamineData();
  491. $fieldModel = new \app\admin\model\Field();
  492. $fileModel = new \app\admin\model\File();
  493. $userModel = new \app\admin\model\User();
  494. $recordModel = new \app\admin\model\Record();
  495. $map['examine.examine_id'] = $id;
  496. $data_view = db('oa_examine')
  497. ->where($map)
  498. ->alias('examine')
  499. ->join('__OA_EXAMINE_CATEGORY__ examine_category', 'examine_category.category_id = examine.category_id', 'LEFT');
  500. $dataInfo = $data_view
  501. ->field('examine.*,examine_category.title as category_name,examine_category.icon as examineIcon')
  502. ->find();
  503. if (!$dataInfo) {
  504. $this->error = '暂无此数据';
  505. return false;
  506. }
  507. //自定义字段信息
  508. $examineDataInfo = $examineData->getDataById($id);
  509. $dataInfo = $examineDataInfo ? array_merge($dataInfo, $examineDataInfo) : $dataInfo;
  510. $dataInfo['start_time'] = $dataInfo['start_time'] ? date('Y-m-d H:i:s', $dataInfo['start_time']) : null;
  511. $dataInfo['end_time'] = $dataInfo['end_time'] ? date('Y-m-d H:i:s', $dataInfo['end_time']) : null;;
  512. //表格数据处理
  513. // $fieldList = $fieldModel->getFieldByFormType('oa_examine', 'form');
  514. // foreach ($fieldList as $k=>$v) {
  515. // $dataInfo[$v] = $fieldModel->getFormValueByField($v, $dataInfo[$v]);
  516. // }
  517. //关联业务
  518. $relationArr = [];
  519. $relationArr = $recordModel->getListByRelationId('examine', $id);
  520. $dataInfo['businessList'] = $relationArr['businessList'];
  521. $dataInfo['contactsList'] = $relationArr['contactsList'];
  522. $dataInfo['contractList'] = $relationArr['contractList'];
  523. $dataInfo['customerList'] = $relationArr['customerList'];
  524. $travelList = [];
  525. if (in_array($dataInfo['category_id'], ['3', '5'])) {
  526. //行程、费用明细
  527. $whereTravel = [];
  528. $whereTravel['examine_id'] = $dataInfo['examine_id'];
  529. $travelList = db('oa_examine_travel')->where($whereTravel)->select() ?: [];
  530. foreach ($travelList as $k => $v) {
  531. //附件
  532. $fileList = [];
  533. $imgList = [];
  534. $where = [];
  535. $where['module'] = 'oa_examine_travel';
  536. $where['module_id'] = $v['travel_id'];
  537. $newFileList = [];
  538. $newFileList = $fileModel->getDataList($where, 'all');
  539. if ($newFileList['list']) {
  540. foreach ($newFileList['list'] as $val) {
  541. if ($val['types'] == 'file') {
  542. $fileList[] = $val;
  543. } else {
  544. $imgList[] = $val;
  545. }
  546. }
  547. }
  548. $travelList[$k]['start_time'] = date('Y-m-d H:i:s', $v['start_time']);
  549. $travelList[$k]['end_time'] = date('Y-m-d H:i:s', $v['end_time']);
  550. $travelList[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
  551. $travelList[$k]['fileList'] = $fileList ?: [];
  552. $travelList[$k]['imgList'] = $imgList ?: [];
  553. }
  554. }
  555. $dataInfo['travelList'] = $travelList;
  556. //附件
  557. $fileList = [];
  558. $imgList = [];
  559. $where = [];
  560. $where['module'] = 'oa_examine';
  561. $where['module_id'] = $id;
  562. $newFileList = [];
  563. $newFileList = $fileModel->getDataList($where, 'all');
  564. foreach ($newFileList['list'] as $val) {
  565. if ($val['types'] == 'file') {
  566. $fileList[] = $val;
  567. } else {
  568. $imgList[] = $val;
  569. }
  570. }
  571. $dataInfo['fileList'] = $fileList ?: [];
  572. $dataInfo['imgList'] = $imgList ?: [];
  573. $dataInfo['create_user_info'] = $userModel->getUserById($dataInfo['create_user_id']);
  574. $dataInfo['examine_id'] = $id;
  575. return $dataInfo;
  576. }
  577. /**
  578. * 审批差旅数据保存
  579. * @param examine_id 审批ID
  580. * @return
  581. */
  582. public function createTravelById($data = [], $examine_id)
  583. {
  584. if (!$examine_id) {
  585. $this->error = '参数错误';
  586. return false;
  587. }
  588. $successRes = true;
  589. foreach ($data as $k => $v) {
  590. $newData = [];
  591. $fileArr = [];
  592. unset($v['files']);
  593. $newData = $v;
  594. $newData['examine_id'] = $examine_id;
  595. $fileArr = $v['file_id']; //接收表单附件
  596. unset($newData['file_id']);
  597. unset($newData['fileList']);
  598. unset($newData['imgList']);
  599. $newData['start_time'] = $newData['start_time'] ? strtotime($newData['start_time']) : 0;
  600. $newData['end_time'] = $newData['end_time'] ? strtotime($newData['end_time']) : 0;
  601. if ($travel_id = db('oa_examine_travel')->insertGetId($newData)) {
  602. //处理附件关系
  603. if ($fileArr) {
  604. $fileModel = new \app\admin\model\File();
  605. $resData = $fileModel->createDataById($fileArr, 'oa_examine_travel', $travel_id);
  606. if ($resData == false) {
  607. $successRes = false;
  608. return false;
  609. }
  610. }
  611. } else {
  612. $successRes = false;
  613. return false;
  614. }
  615. }
  616. if (!$successRes) {
  617. $this->error = '审批事项创建失败';
  618. return false;
  619. }
  620. return true;
  621. }
  622. /**
  623. * 审批差旅数据编辑
  624. * @param examine_id 审批ID
  625. * @return
  626. */
  627. public function updateTravelById($data = [], $examine_id)
  628. {
  629. if (!$examine_id) {
  630. $this->error = '参数错误';
  631. return false;
  632. }
  633. $oldTravelIds = db('oa_examine_travel')->where(['examine_id' => $examine_id])->column('travel_id');
  634. $oldTravelFileIds = db('oa_examine_travel_file')->where(['travel_id' => ['in', $oldTravelIds]])->column('r_id');
  635. $successRes = true;
  636. foreach ($data as $k => $v) {
  637. $newData = [];
  638. $fileArr = [];
  639. unset($v['files']);
  640. $newData = $v;
  641. $newData['examine_id'] = $examine_id;
  642. $fileArr = $v['file_id']; //接收表单附件
  643. unset($newData['file_id']);
  644. unset($newData['fileList']);
  645. unset($newData['imgList']);
  646. unset($newData['travel_id']);
  647. $newData['start_time'] = $newData['start_time'] ? strtotime($newData['start_time']) : 0;
  648. $newData['end_time'] = $newData['end_time'] ? strtotime($newData['end_time']) : 0;
  649. if ($travel_id = db('oa_examine_travel')->insertGetId($newData)) {
  650. //处理附件关系
  651. if ($fileArr) {
  652. $fileModel = new \app\admin\model\File();
  653. $resData = $fileModel->createDataById($fileArr, 'oa_examine_travel', $travel_id);
  654. if ($resData == false) {
  655. $successRes = false;
  656. return false;
  657. }
  658. }
  659. } else {
  660. $successRes = false;
  661. return false;
  662. }
  663. }
  664. if (!$successRes) {
  665. $this->error = '审批事项创建失败';
  666. return false;
  667. }
  668. //删除旧数据
  669. if ($oldTravelIds) db('oa_examine_travel')->where(['travel_id' => ['in', $oldTravelIds]])->delete();
  670. if ($oldTravelFileIds) db('oa_examine_travel_file')->where(['r_id' => ['in', $oldTravelFileIds]])->delete();
  671. return true;
  672. }
  673. }