123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 审批
  4. // +----------------------------------------------------------------------
  5. // | Author: Michael_xu | gengxiaoxu@5kcrm.com
  6. // +----------------------------------------------------------------------
  7. namespace app\oa\model;
  8. use app\admin\traits\FieldVerificationTrait;
  9. use think\Db;
  10. use app\admin\model\Common;
  11. use app\admin\model\Message;
  12. use think\Validate;
  13. class Examine extends Common
  14. {
  15. use FieldVerificationTrait;
  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. // 数据验证
  278. $validateResult = $this->fieldDataValidate($param, $this->name, $param['create_user_id'], 0, $param['category_id']);
  279. if (!empty($validateResult)) {
  280. $this->error = $validateResult;
  281. return false;
  282. }
  283. $categoryInfo = $examineCategoryModel->getDataById($param['category_id']);
  284. $fileArr = $param['file_id']; //接收表单附件
  285. unset($param['file_id']);
  286. $param['start_time'] = $param['start_time'] ? : 0;
  287. $param['end_time'] = $param['end_time'] ? : 0;
  288. if ($this->data($param)->allowField(true)->save()) {
  289. //处理自定义字段数据
  290. $resData = $examineDataModel->createData($param, $this->examine_id);
  291. if ($resData) {
  292. //处理附件关系
  293. if ($fileArr) {
  294. $fileModel = new \app\admin\model\File();
  295. $resData = $fileModel->createDataById($fileArr, 'oa_examine', $this->examine_id);
  296. if ($resData == false) {
  297. $this->error = '附件上传失败';
  298. return false;
  299. }
  300. }
  301. //相关业务
  302. $rdata = [];
  303. $rdata['customer_ids'] = $param['oaExamineRelation']['customer_ids'] ? arrayToString($param['oaExamineRelation']['customer_ids']) : '';
  304. $rdata['contacts_ids'] = $param['oaExamineRelation']['contacts_ids'] ? arrayToString($param['oaExamineRelation']['contacts_ids']) : '';
  305. $rdata['business_ids'] = $param['oaExamineRelation']['business_ids'] ? arrayToString($param['oaExamineRelation']['business_ids']) : '';
  306. $rdata['contract_ids'] = $param['oaExamineRelation']['contract_ids'] ? arrayToString($param['oaExamineRelation']['contract_ids']) : '';
  307. $rdata['examine_id'] = $this->examine_id;
  308. $rdata['status'] = 1;
  309. $rdata['create_time'] = time();
  310. Db::name('OaExamineRelation')->insert($rdata);
  311. //处理差旅相关
  312. $resTravel = true;
  313. if (in_array($param['category_id'], ['3', '5']) && $param['cause']) {
  314. $resTravel = $this->createTravelById($param['cause']['list'], $this->examine_id);
  315. }
  316. if (!$resTravel) {
  317. $this->error = '相关事项保存失败,请重试';
  318. return false;
  319. }
  320. //站内信
  321. $send_user_id = stringToArray($param['check_user_id']);
  322. (new Message())->send(
  323. Message::EXAMINE_TO_DO,
  324. [
  325. 'title' => $categoryInfo['title'],
  326. 'action_id' => $this->examine_id
  327. ],
  328. $send_user_id
  329. );
  330. $data = [];
  331. $data['examine_id'] = $this->examine_id;
  332. # 添加活动记录
  333. if (!empty($rdata['customer_ids']) || !empty($rdata['contacts_ids']) || !empty($rdata['business_ids']) || !empty($rdata['contract_ids'])) {
  334. Db::name('crm_activity')->insert([
  335. 'type' => 2,
  336. 'activity_type' => 9,
  337. 'activity_type_id' => $data['examine_id'],
  338. 'content' => '审批',
  339. 'create_user_id' => $param['create_user_id'],
  340. 'update_time' => time(),
  341. 'create_time' => time(),
  342. 'customer_ids' => !empty($rdata['customer_ids']) ? $rdata['customer_ids'] : '',
  343. 'contacts_ids' => !empty($rdata['contacts_ids']) ? $rdata['contacts_ids'] : '',
  344. 'business_ids' => !empty($rdata['business_ids']) ? $rdata['business_ids'] : '',
  345. 'contract_ids' => !empty($rdata['contract_ids']) ? $rdata['contract_ids'] : '',
  346. ]);
  347. }
  348. return $data;
  349. } else {
  350. $this->error = $examineDataModel->getError();
  351. return false;
  352. }
  353. } else {
  354. $this->error = '添加失败';
  355. return false;
  356. }
  357. }
  358. /**
  359. * 编辑审批信息
  360. *
  361. * @param $param
  362. * @param string $examine_id
  363. * @return array|bool
  364. * @throws \think\Exception
  365. * @throws \think\db\exception\DataNotFoundException
  366. * @throws \think\db\exception\ModelNotFoundException
  367. * @throws \think\exception\DbException
  368. * @throws \think\exception\PDOException
  369. */
  370. public function updateDataById($param, $examine_id = '')
  371. {
  372. $examine_id = intval($examine_id);
  373. $userModel = new \app\admin\model\User();
  374. $examineCategoryModel = new \app\oa\model\ExamineCategory();
  375. $examineDataModel = new \app\oa\model\ExamineData();
  376. $create_user_id = $param['create_user_id'];
  377. $userId = $param['user_id'];
  378. unset($param['id']);
  379. unset($param['user_id']);
  380. $dataInfo = db('oa_examine')->where(['examine_id' => $examine_id])->find();
  381. if (!$dataInfo) {
  382. $this->error = '数据不存在或已删除';
  383. return false;
  384. }
  385. //过滤不能修改的字段
  386. $unUpdateField = ['create_user_id', 'is_deleted', 'delete_time'];
  387. foreach ($unUpdateField as $v) {
  388. unset($param[$v]);
  389. }
  390. $categoryInfo = $examineCategoryModel->getDataById($dataInfo['category_id']);
  391. //验证
  392. // $fieldModel = new \app\admin\model\Field();
  393. // $validateArr = $fieldModel->validateField($this->name, $dataInfo['category_id']); //获取自定义字段验证规则
  394. // $validate = new Validate($validateArr['rule'], $validateArr['message']);
  395. // $result = $validate->check($param);
  396. // if (!$result) {
  397. // $this->error = $validate->getError();
  398. // return false;
  399. // }
  400. // 数据验证
  401. $validateResult = $this->fieldDataValidate($param, $this->name, $userId, $examine_id, $param['category_id']);
  402. if (!empty($validateResult)) {
  403. $this->error = $validateResult;
  404. return false;
  405. }
  406. $fileArr = $param['file_id']; //接收表单附件
  407. unset($param['file_id']);
  408. $param['start_time'] = $param['start_time'] ? strtotime($param['start_time']) : 0;
  409. $param['end_time'] = $param['end_time'] ? strtotime($param['end_time']) : 0;
  410. if ($this->allowField(true)->save($param, ['examine_id' => $examine_id])) {
  411. //处理自定义字段数据
  412. $resData = $examineDataModel->createData($param, $examine_id);
  413. if ($resData) {
  414. //处理附件关系
  415. if ($fileArr) {
  416. $fileModel = new \app\admin\model\File();
  417. $resData = $fileModel->createDataById($fileArr, 'oa_examine', $examine_id);
  418. if ($resData == false) {
  419. $this->error = '附件上传失败';
  420. return false;
  421. }
  422. }
  423. //站内信
  424. $send_user_id = stringToArray($param['check_user_id']);
  425. if ($send_user_id) {
  426. (new Message())->send(
  427. Message::EXAMINE_TO_DO,
  428. [
  429. 'title' => $categoryInfo['title'],
  430. 'action_id' => $examine_id
  431. ],
  432. $send_user_id
  433. );
  434. }
  435. //相关业务
  436. Db::name('OaExamineRelation')->where('examine_id', $examine_id)->delete(); // 先删除在添加
  437. $rdata = [];
  438. $rdata['examine_id'] = $examine_id;
  439. $rdata['status'] = 1;
  440. $rdata['create_time'] = time();
  441. $rdata['customer_ids'] = $param['oaExamineRelation']['customer_ids'] ? arrayToString($param['oaExamineRelation']['customer_ids']) : '';
  442. $rdata['contacts_ids'] = $param['oaExamineRelation']['contacts_ids'] ? arrayToString($param['oaExamineRelation']['contacts_ids']) : '';
  443. $rdata['business_ids'] = $param['oaExamineRelation']['business_ids'] ? arrayToString($param['oaExamineRelation']['business_ids']) : '';
  444. $rdata['contract_ids'] = $param['oaExamineRelation']['contract_ids'] ? arrayToString($param['oaExamineRelation']['contract_ids']) : '';
  445. Db::name('OaExamineRelation')->insert($rdata);
  446. //处理差旅相关
  447. $resTravel = true;
  448. if (in_array($dataInfo['category_id'], ['3', '5']) && $param['cause']) {
  449. $resTravel = $this->updateTravelById($param['cause']['list'], $examine_id);
  450. }
  451. if (!$resTravel) {
  452. $this->error = '相关事项保存失败,请重试';
  453. return false;
  454. }
  455. // 站内信
  456. $send_user_id = stringToArray($param['check_user_id']);
  457. if ($send_user_id) {
  458. (new Message())->send(
  459. Message::EXAMINE_TO_DO,
  460. [
  461. 'title' => $categoryInfo['title'],
  462. 'action_id' => $examine_id
  463. ],
  464. $send_user_id
  465. );
  466. }
  467. $data = [];
  468. $data['examine_id'] = $examine_id;
  469. # 删除活动记录
  470. Db::name('crm_activity')->where(['activity_type' => 9, 'activity_type_id' => $examine_id])->delete();
  471. # 添加活动记录
  472. if (!empty($rdata['customer_ids']) || !empty($rdata['contacts_ids']) || !empty($rdata['business_ids']) || !empty($rdata['contract_ids'])) {
  473. Db::name('crm_activity')->insert([
  474. 'type' => 2,
  475. 'activity_type' => 9,
  476. 'activity_type_id' => $examine_id,
  477. 'content' => '审批',
  478. 'create_user_id' => $create_user_id,
  479. 'update_time' => time(),
  480. 'create_time' => time(),
  481. 'customer_ids' => !empty($rdata['customer_ids']) ? $rdata['customer_ids'] : '',
  482. 'contacts_ids' => !empty($rdata['contacts_ids']) ? $rdata['contacts_ids'] : '',
  483. 'business_ids' => !empty($rdata['business_ids']) ? $rdata['business_ids'] : '',
  484. 'contract_ids' => !empty($rdata['contract_ids']) ? $rdata['contract_ids'] : '',
  485. ]);
  486. }
  487. return $data;
  488. } else {
  489. $this->error = $examineDataModel->getError();
  490. return false;
  491. }
  492. } else {
  493. $this->error = '编辑失败';
  494. return false;
  495. }
  496. }
  497. /**
  498. * 审批数据
  499. * @param $id 审批ID
  500. * @return
  501. */
  502. public function getDataById($id = '')
  503. {
  504. $examineData = new \app\oa\model\ExamineData();
  505. $fileModel = new \app\admin\model\File();
  506. $userModel = new \app\admin\model\User();
  507. $recordModel = new \app\admin\model\Record();
  508. $map['examine.examine_id'] = $id;
  509. $data_view = db('oa_examine')
  510. ->where($map)
  511. ->alias('examine')
  512. ->join('__OA_EXAMINE_CATEGORY__ examine_category', 'examine_category.category_id = examine.category_id', 'LEFT');
  513. $dataInfo = $data_view
  514. ->field('examine.*,examine_category.title as category_name,examine_category.icon as examineIcon')
  515. ->find();
  516. if (!$dataInfo) {
  517. $this->error = '暂无此数据';
  518. return false;
  519. }
  520. //自定义字段信息
  521. $examineDataInfo = $examineData->getDataById($id);
  522. $dataInfo = $examineDataInfo ? array_merge($dataInfo, $examineDataInfo) : $dataInfo;
  523. $dataInfo['start_time'] = $dataInfo['start_time'] ? date('Y-m-d H:i:s', $dataInfo['start_time']) : null;
  524. $dataInfo['end_time'] = $dataInfo['end_time'] ? date('Y-m-d H:i:s', $dataInfo['end_time']) : null;;
  525. $dataInfo['create_time'] = $dataInfo['create_time'] ? date('Y-m-d H:i:s', $dataInfo['create_time']) : null;;
  526. $realname=$userModel->getUserById($dataInfo['create_user_id']);
  527. $dataInfo['value'] = !empty($realname)?$realname['realname']: '';
  528. // 处理自定义字段格式
  529. $fieldWhere = [
  530. 'types' => 'oa_examine',
  531. 'types_id' => $dataInfo['category_id'],
  532. 'form_type' => ['in', ['position', 'location', 'handwriting_sign', 'date_interval', 'detail_table']]
  533. ];
  534. $fieldList = db('admin_field')->field(['field', 'form_type'])->where($fieldWhere)->select();
  535. foreach ($fieldList AS $key => $value) {
  536. if (!empty($dataInfo[$value['field']])) {
  537. $dataInfo[$value['field']] = json_decode($dataInfo[$value['field']], true);
  538. }
  539. }
  540. //关联业务
  541. $relationArr = $recordModel->getListByRelationId('examine', $id);
  542. $dataInfo['businessList'] = $relationArr['businessList'];
  543. $dataInfo['contactsList'] = $relationArr['contactsList'];
  544. $dataInfo['contractList'] = $relationArr['contractList'];
  545. $dataInfo['customerList'] = $relationArr['customerList'];
  546. $travelList = [];
  547. if (in_array($dataInfo['category_id'], ['3', '5'])) {
  548. //行程、费用明细
  549. $whereTravel = [];
  550. $whereTravel['examine_id'] = $dataInfo['examine_id'];
  551. $travelList = db('oa_examine_travel')->where($whereTravel)->select() ?: [];
  552. foreach ($travelList as $k => $v) {
  553. //附件
  554. $fileList = [];
  555. $imgList = [];
  556. $where = [];
  557. $where['module'] = 'oa_examine_travel';
  558. $where['module_id'] = $v['travel_id'];
  559. $newFileList = [];
  560. $newFileList = $fileModel->getDataList($where, 'all');
  561. if ($newFileList['list']) {
  562. foreach ($newFileList['list'] as $val) {
  563. if ($val['types'] == 'file') {
  564. $fileList[] = $val;
  565. } else {
  566. $imgList[] = $val;
  567. }
  568. }
  569. }
  570. $travelList[$k]['start_time'] = date('Y-m-d H:i:s', $v['start_time']);
  571. $travelList[$k]['end_time'] = date('Y-m-d H:i:s', $v['end_time']);
  572. $travelList[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
  573. $travelList[$k]['fileList'] = $fileList ?: [];
  574. $travelList[$k]['imgList'] = $imgList ?: [];
  575. }
  576. }
  577. $dataInfo['travelList'] = $travelList;
  578. //附件
  579. $fileList = [];
  580. $imgList = [];
  581. $where = [];
  582. $where['module'] = 'oa_examine';
  583. $where['module_id'] = $id;
  584. $newFileList = $fileModel->getDataList($where, 'all');
  585. foreach ($newFileList['list'] as $val) {
  586. if ($val['types'] == 'file') {
  587. $fileList[] = $val;
  588. } else {
  589. $imgList[] = $val;
  590. }
  591. }
  592. $dataInfo['fileList'] = $fileList ?: [];
  593. $dataInfo['imgList'] = $imgList ?: [];
  594. $dataInfo['create_user_info'] = $realname;
  595. $dataInfo['examine_id'] = $id;
  596. return $dataInfo;
  597. }
  598. /**
  599. * 审批差旅数据保存
  600. * @param examine_id 审批ID
  601. * @return
  602. */
  603. public function createTravelById($data = [], $examine_id)
  604. {
  605. if (!$examine_id) {
  606. $this->error = '参数错误';
  607. return false;
  608. }
  609. $successRes = true;
  610. foreach ($data as $k => $v) {
  611. $newData = [];
  612. $fileArr = [];
  613. unset($v['files']);
  614. $newData = $v;
  615. $newData['examine_id'] = $examine_id;
  616. $fileArr = $v['file_id']; //接收表单附件
  617. unset($newData['file_id']);
  618. unset($newData['fileList']);
  619. unset($newData['imgList']);
  620. $newData['start_time'] = $newData['start_time'] ? strtotime($newData['start_time']) : 0;
  621. $newData['end_time'] = $newData['end_time'] ? strtotime($newData['end_time']) : 0;
  622. if ($travel_id = db('oa_examine_travel')->insertGetId($newData)) {
  623. //处理附件关系
  624. if ($fileArr) {
  625. $fileModel = new \app\admin\model\File();
  626. $resData = $fileModel->createDataById($fileArr, 'oa_examine_travel', $travel_id);
  627. if ($resData == false) {
  628. $successRes = false;
  629. return false;
  630. }
  631. }
  632. } else {
  633. $successRes = false;
  634. return false;
  635. }
  636. }
  637. if (!$successRes) {
  638. $this->error = '审批事项创建失败';
  639. return false;
  640. }
  641. return true;
  642. }
  643. /**
  644. * 审批差旅数据编辑
  645. * @param examine_id 审批ID
  646. * @return
  647. */
  648. public function updateTravelById($data = [], $examine_id)
  649. {
  650. if (!$examine_id) {
  651. $this->error = '参数错误';
  652. return false;
  653. }
  654. $oldTravelIds = db('oa_examine_travel')->where(['examine_id' => $examine_id])->column('travel_id');
  655. $oldTravelFileIds = db('oa_examine_travel_file')->where(['travel_id' => ['in', $oldTravelIds]])->column('r_id');
  656. $successRes = true;
  657. foreach ($data as $k => $v) {
  658. $newData = [];
  659. $fileArr = [];
  660. unset($v['files']);
  661. $newData = $v;
  662. $newData['examine_id'] = $examine_id;
  663. $fileArr = $v['file_id']; //接收表单附件
  664. unset($newData['file_id']);
  665. unset($newData['fileList']);
  666. unset($newData['imgList']);
  667. unset($newData['travel_id']);
  668. $newData['start_time'] = $newData['start_time'] ? strtotime($newData['start_time']) : 0;
  669. $newData['end_time'] = $newData['end_time'] ? strtotime($newData['end_time']) : 0;
  670. if ($travel_id = db('oa_examine_travel')->insertGetId($newData)) {
  671. //处理附件关系
  672. if ($fileArr) {
  673. $fileModel = new \app\admin\model\File();
  674. $resData = $fileModel->createDataById($fileArr, 'oa_examine_travel', $travel_id);
  675. if ($resData == false) {
  676. $successRes = false;
  677. return false;
  678. }
  679. }
  680. } else {
  681. $successRes = false;
  682. return false;
  683. }
  684. }
  685. if (!$successRes) {
  686. $this->error = '审批事项创建失败';
  687. return false;
  688. }
  689. //删除旧数据
  690. if ($oldTravelIds) db('oa_examine_travel')->where(['travel_id' => ['in', $oldTravelIds]])->delete();
  691. if ($oldTravelFileIds) db('oa_examine_travel_file')->where(['r_id' => ['in', $oldTravelFileIds]])->delete();
  692. return true;
  693. }
  694. }