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