ExamineLogic.php 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. <?php
  2. namespace app\oa\logic;
  3. use app\admin\model\Common;
  4. use app\oa\model\Examine;
  5. use think\Db;
  6. use think\Validate;
  7. class ExamineLogic extends Common
  8. {
  9. private $statusArr = ['0' => '待审核', '1' => '审核中', '2' => '审核通过', '3' => '已拒绝', '4' => '已撤回'];
  10. public function getDataList($request)
  11. {
  12. $userModel = new \app\admin\model\User();
  13. $fileModel = new \app\admin\model\File();
  14. $recordModel = new \app\admin\model\Record();
  15. $examine_by = $request['examine_by']; //1待我审批 2我已审批 all 全部
  16. $user_id = $request['user_id'];
  17. $bi = $request['bi_types'];
  18. $check_status = $request['check_status']; //0 待审批 2 审批通过 4 审批拒绝 all 全部
  19. unset($request['by']);
  20. unset($request['bi_types']);
  21. unset($request['user_id']);
  22. unset($request['check_status']);
  23. unset($request['examine_by']);
  24. $request = $this->fmtRequest($request);
  25. $map = $request['map'] ?: [];
  26. if (isset($map['search']) && $map['search']) {
  27. //普通筛选
  28. $map['examine.content'] = ['like', '%' . $map['search'] . '%'];
  29. } else {
  30. $map = where_arr($map, 'oa', 'examine', 'index'); //高级筛选
  31. }
  32. unset($map['search']);
  33. //审批类型
  34. $map['examine.category_id'] = $map['examine.category_id'] ?: array('gt', 0);
  35. $map_str = '';
  36. $logmap = '';
  37. switch ($examine_by) {
  38. case 'all' :
  39. //如果超管则能看到全部
  40. if (!isSuperAdministrators($user_id)) {
  41. $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 . " ) )";
  42. }
  43. break;
  44. case '1' :
  45. $map['check_user_id'] = [['like', '%,' . $user_id . ',%']];
  46. break; //待审
  47. case '2' :
  48. $map_str = "(( check_user_id LIKE '%," . $user_id . ",%' OR check_user_id = " . $user_id . " )
  49. OR ( flow_user_id LIKE '%," . $user_id . ",%' OR `flow_user_id` = " . $user_id . " ) )";
  50. // $map['flow_user_id'] = [['like', '%,' . $user_id . ',%'], ['eq', $user_id], 'or'];
  51. break; //已审
  52. default:
  53. $map['examine.create_user_id'] = $user_id;
  54. break;
  55. }
  56. $order = 'examine.create_time desc,examine.update_time desc';
  57. //发起时间
  58. if ($map['examine.between_time'][0] && $map['examine.between_time'][1]) {
  59. $start_time = $map['examine.between_time'][0];
  60. $end_time = $map['examine.between_time'][1];
  61. $map['examine.create_time'] = array('between', array($start_time, $end_time));
  62. }
  63. unset($map['examine.between_time']);
  64. //审核状态 0 待审批 2 审批通过 4 审批拒绝 all 全部
  65. if (isset($check_status)) {
  66. if ($check_status == 'all') {
  67. $map['examine.check_status'] = ['egt', 0];
  68. if (isSuperAdministrators($user_id)) {
  69. unset($map['examine.create_user_id']);
  70. }
  71. } elseif ($check_status == 4) {
  72. $map['examine.check_status'] = ['eq', 3];
  73. } elseif ($check_status == 0) {
  74. $map['examine.check_status'] = ['<=', 1];
  75. } else {
  76. $map['examine.check_status'] = $check_status;
  77. }
  78. } else {
  79. if ($examine_by == 'all') {
  80. $map['examine.check_status'] = ['egt', 0];
  81. } elseif ($examine_by == 1) {
  82. $map['examine.check_status'] = ['elt', 1];
  83. } elseif ($examine_by == 2) {
  84. $map['examine.check_status'] = ['egt', 2];
  85. }
  86. }
  87. $join = [
  88. ['__ADMIN_USER__ user', 'user.id = examine.create_user_id', 'LEFT'],
  89. ['__OA_EXAMINE_CATEGORY__ examine_category', 'examine_category.category_id = examine.category_id', 'LEFT'],
  90. ];
  91. $list_view = db('oa_examine')
  92. ->alias('examine')
  93. ->where($map_str)
  94. ->where($map)
  95. ->join($join);
  96. $res = [];
  97. $list = $list_view
  98. ->page($request['page'], $request['limit'])
  99. ->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')
  100. ->order($order)
  101. ->select();
  102. foreach ($list as $k => $v) {
  103. $causeCount = 0;
  104. $causeTitle = '';
  105. $duration = $v['duration'] ?: '0.0';
  106. $money = $v['money'] ?: '0.00';
  107. $list[$k]['causeTitle'] = $causeTitle;
  108. $list[$k]['causeCount'] = $causeCount ?: 0;
  109. $item = db('oa_examine_travel')->where(['examine_id' => $v['examine_id']])->select();
  110. if ($item) {
  111. foreach ($item as $key => $value) {
  112. if($v['check_status']==4){
  113. $usernames = '';
  114. }else{
  115. $usernames = db('admin_user')->whereIn('id', stringToArray($v['check_user_id']))->column('realname');
  116. }
  117. //关联业务
  118. $relationArr = [];
  119. $relationArr = $recordModel->getListByRelationId('examine', $v['examine_id']);
  120. $item[$key]['relation'] = arrayToString(array_column($relationArr['businessList'], 'name')) . ' ' .
  121. arrayToString(array_column($relationArr['contactsList'], 'name')) . ' ' .
  122. arrayToString(array_column($relationArr['contractList'], 'name')) . ' ' .
  123. arrayToString(array_column($relationArr['customerList'], 'name'));
  124. $res[] = [
  125. 'category_name' => $v['category_name'],
  126. 'create_time' => !empty($v['create_time']) ? date('Y-m-d H:i:s', $v['create_time']) : null,
  127. 'realname' => $v['realname'],
  128. 'check_status_info' => $this->statusArr[(int)$v['check_status']],
  129. 'examine_name' => implode($usernames, ','),
  130. 'content' => $v['content'],
  131. 'remark' => $v['remark'],
  132. 'duration' => $v['duration'],
  133. 'vehicle' => $value['vehicle'],
  134. 'trip' => $value['trip'],
  135. 'money' => $value['money'],
  136. 'traffic' => $value['traffic'],
  137. 'stay' => $value['stay'],
  138. 'diet' => $value['diet'],
  139. 'other' => $value['other'],
  140. 'start_address' => $value['start_address'],
  141. 'end_address' => $value['end_address'],
  142. 'start_time' => !empty($value['start_time']) ? date('Y-m-d H:i:s', $value['start_time']) : null,
  143. 'end_time' => !empty($value['end_time']) ? date('Y-m-d H:i:s', $value['end_time']) : null,
  144. 'description' => $value['description'],
  145. 'replyList' => str_replace(',', ' ', $item[$key]['relation']),
  146. ];
  147. }
  148. } else {
  149. $list[$k]['create_time'] = !empty($v['create_time']) ? date('Y-m-d H:i:s', $v['create_time']) : null;
  150. $list[$k]['start_time'] = !empty($v['start_time']) ? date('Y-m-d H:i:s', $v['start_time']) : null;
  151. $list[$k]['end_time'] = !empty($v['end_time']) ? date('Y-m-d H:i:s', $v['end_time']) : null;
  152. if($v['check_status']==4){
  153. $usernames = '';
  154. }else{
  155. $usernames = db('admin_user')->whereIn('id', stringToArray($v['check_user_id']))->column('realname');
  156. }
  157. //关联业务
  158. $relationArr = [];
  159. $relationArr = $recordModel->getListByRelationId('examine', $v['examine_id']);
  160. $list[$k]['relation'] = arrayToString(array_column($relationArr['businessList'], 'name')) . ' ' .
  161. arrayToString(array_column($relationArr['contactsList'], 'name')) . ' ' .
  162. arrayToString(array_column($relationArr['contractList'], 'name')) . ' ' .
  163. arrayToString(array_column($relationArr['customerList'], 'name'));
  164. $res[] = [
  165. 'category_name' => $v['category_name'],
  166. 'create_time' => !empty($v['create_time']) ? date('Y-m-d H:i:s', $v['create_time']) : null,
  167. 'realname' => $v['realname'],
  168. 'check_status_info' => $this->statusArr[(int)$v['check_status']],
  169. 'examine_name' => implode($usernames, ','),
  170. 'content' => $v['content'],
  171. 'remark' => $v['remark'],
  172. 'duration' => $v['duration'],
  173. 'vehicle' => '',
  174. 'money' => $v['money'],
  175. 'traffic' => '',
  176. 'stay' => '',
  177. 'diet' => '',
  178. 'other' => '',
  179. 'start_address' => '',
  180. 'end_address' => '',
  181. 'start_time' => !empty($v['start_time']) ? date('Y-m-d H:i:s', $v['start_time']) : null,
  182. 'end_time' => !empty($v['end_time']) ? date('Y-m-d H:i:s', $v['end_time']) : null,
  183. 'description' => '',
  184. 'replyList' => str_replace(',', ' ', $item[$key]['relation']),
  185. ];
  186. }
  187. }
  188. return $res;
  189. }
  190. /**
  191. * 审批导出
  192. * @param $param
  193. * @return mixed
  194. */
  195. public function excelExport($param)
  196. {
  197. $excelModel = new \app\admin\model\Excel();
  198. $data = $this->getDataList($param);
  199. $list = [];
  200. switch ($param['category_id']) {
  201. case '1' :
  202. $field_list = [
  203. '0' => ['name' => '审批类型', 'field' => 'category_name'],
  204. '1' => ['name' => '创建时间', 'field' => 'create_time'],
  205. '2' => ['name' => '创建人', 'field' => 'realname'],
  206. '3' => ['name' => '状态', 'field' => 'check_status_info'],
  207. '4' => ['name' => '当前审批人', 'field' => 'examine_name'],
  208. '5' => ['name' => '备注', 'field' => 'description'],
  209. '6' => ['name' => '关联业务', 'field' => 'replyList'],
  210. ];
  211. break;
  212. case '2' :
  213. $field_list = [
  214. '0' => ['name' => '审批类型', 'field' => 'category_name'],
  215. '1' => ['name' => '创建时间', 'field' => 'create_time'],
  216. '2' => ['name' => '创建人', 'field' => 'realname'],
  217. '3' => ['name' => '状态', 'field' => 'check_status_info'],
  218. '4' => ['name' => '当前审批人', 'field' => 'examine_name'],
  219. '5' => ['name' => '审批内容', 'field' => 'content'],
  220. '6' => ['name' => '开始时间', 'field' => 'start_time'],
  221. '7' => ['name' => '结束时间', 'field' => 'end_time'],
  222. '8' => ['name' => '时长', 'field' => 'duration'],
  223. '9' => ['name' => '备注', 'field' => 'description'],
  224. '10' => ['name' => '关联业务', 'field' => 'replyList'],
  225. ];
  226. break;
  227. case '3' :
  228. $field_list = [
  229. '0' => ['name' => '审批类型', 'field' => 'category_name'],
  230. '1' => ['name' => '创建时间', 'field' => 'create_time'],
  231. '2' => ['name' => '创建人', 'field' => 'realname'],
  232. '3' => ['name' => '状态', 'field' => 'check_status_info'],
  233. '4' => ['name' => '当前审批人', 'field' => 'examine_name'],
  234. '5' => ['name' => '出差事由', 'field' => 'content'],
  235. '6' => ['name' => '备注', 'field' => 'remark'],
  236. '7' => ['name' => '出差总天数', 'field' => 'duration'],
  237. '8' => ['name' => '交通工具', 'field' => 'vehicle'],
  238. '9' => ['name' => '单程往返', 'field' => 'trip'],
  239. '10' => ['name' => '出发城市', 'field' => 'start_address'],
  240. '11' => ['name' => '目的城市', 'field' => 'end_address'],
  241. '12' => ['name' => '开始时间', 'field' => 'start_time'],
  242. '13' => ['name' => '结束时间', 'field' => 'end_time'],
  243. '14' => ['name' => '出差备注', 'field' => 'description'],
  244. '15' => ['name' => '时长', 'field' => 'duration'],
  245. '16' => ['name' => '关联业务', 'field' => 'replyList'],
  246. ];
  247. break;
  248. case '4' :
  249. $field_list = [
  250. '0' => ['name' => '审批类型', 'field' => 'category_name'],
  251. '1' => ['name' => '创建时间', 'field' => 'create_time'],
  252. '2' => ['name' => '创建人', 'field' => 'realname',],
  253. '3' => ['name' => '状态', 'field' => 'check_status_info'],
  254. '4' => ['name' => '当前审批人', 'field' => 'examine_name'],
  255. '5' => ['name' => '加班原因', 'field' => 'content'],
  256. '6' => ['name' => '开始时间', 'field' => 'start_time'],
  257. '7' => ['name' => '结束时间', 'field' => 'end_time'],
  258. '8' => ['name' => '加班总天数', 'field' => 'duration'],
  259. '9' => ['name' => '备注', 'field' => 'description'],
  260. '10' => ['name' => '关联业务', 'field' => 'replyList'],
  261. ];
  262. break;
  263. case '5':
  264. $field_list = [
  265. '0' => ['name' => '审批类型', 'field' => 'category_name'],
  266. '1' => ['name' => '创建时间', 'field' => 'create_time'],
  267. '2' => ['name' => '创建人', 'field' => 'realname'],
  268. '3' => ['name' => '状态', 'field' => 'check_status_info'],
  269. '4' => ['name' => '当前审批人', 'field' => 'examine_name'],
  270. '5' => ['name' => '差旅内容', 'field' => 'content'],
  271. '6' => ['name' => '报销总金额', 'field' => 'money'],
  272. '7' => ['name' => '备注', 'field' => 'remark'],
  273. '8' => ['name' => '出发城市', 'field' => 'start_address'],
  274. '9' => ['name' => '目的城市', 'field' => 'end_address'],
  275. '10' => ['name' => '开始时间', 'field' => 'start_time'],
  276. '11' => ['name' => '结束时间', 'field' => 'end_time'],
  277. '12' => ['name' => '交通费', 'field' => 'traffic'],
  278. '13' => ['name' => '住宿费', 'field' => 'stay'],
  279. '14' => ['name' => '餐饮费', 'field' => 'diet'],
  280. '15' => ['name' => '其他费用', 'field' => 'other'],
  281. '16' => ['name' => '合计', 'field' => 'money'],
  282. '17' => ['name' => '费用明细描述', 'field' => 'description'],
  283. '18' => ['name' => '关联业务', 'field' => 'relation'],
  284. ];
  285. break;
  286. case '6' :
  287. $field_list = [
  288. '0' => ['name' => '审批类型', 'field' => 'category_name'],
  289. '1' => ['name' => '创建时间', 'field' => 'create_time'],
  290. '2' => ['name' => '创建人', 'field' => 'realname'],
  291. '3' => ['name' => '状态', 'field' => 'check_status_info'],
  292. '4' => ['name' => '当前审批人', 'field' => 'examine_name'],
  293. '5' => ['name' => '借款事由', 'field' => 'content'],
  294. '6' => ['name' => '开始时间', 'field' => 'start_time'],
  295. '7' => ['name' => '结束时间', 'field' => 'end_time'],
  296. '8' => ['name' => '借款金额', 'field' => 'money'],
  297. '9' => ['name' => '备注', 'field' => 'description'],
  298. '10' => ['name' => '关联业务', 'field' => 'replyList'],
  299. ];
  300. break;
  301. default :
  302. $field_list = [
  303. '0' => ['name' => '审批类型', 'field' => 'category_name'],
  304. '1' => ['name' => '创建时间', 'field' => 'create_time'],
  305. '2' => ['name' => '创建人', 'field' => 'realname'],
  306. '3' => ['name' => '状态', 'field' => 'check_status_info'],
  307. '4' => ['name' => '当前审批人', 'field' => 'examine_name'],
  308. '5' => ['name' => '备注', 'field' => 'description'],
  309. '6' => ['name' => '关联业务', 'field' => 'replyList'],
  310. ];
  311. }
  312. $file_name = 'oa_examine';
  313. return $excelModel->dataExportCsv($file_name, $field_list, $data);
  314. }
  315. /**
  316. * 审批数据
  317. * @param $param
  318. */
  319. public function myExamine($param)
  320. {
  321. $param['status'] = $param['status'] == 'all' ? 3 : $param['status'];
  322. $auth_user_ids = getSubUserId(true, 0, $param['user_id']);
  323. $user_id = $param['user_id'];
  324. $where = [];
  325. $whereOr = [];
  326. if ($param['status'] == 0) {
  327. $where['a.check_status'] = ['elt', 1];
  328. $whereOr = function ($query) use ($user_id) {
  329. $query->where('a.check_user_id', ['like', '%' . $user_id . '%'])->whereOr('a.flow_user_id', ['like', '%' . $user_id . '%']);
  330. };
  331. } elseif ($param['status'] == 1) {
  332. $where['a.check_status'] = ['in', [2, 3]];
  333. $whereOr = function ($query) use ($user_id) {
  334. $query->where('a.check_user_id', ['like', '%' . $user_id . '%'])->whereOr('a.flow_user_id', ['like', '%' . $user_id . '%']);
  335. };
  336. } elseif ($param['status'] == 3) {
  337. $where['a.check_status'] = ['lt', 5];
  338. $whereOr = function ($query) use ($user_id) {
  339. $query->where('a.check_user_id', ['like', '%' . $user_id . '%'])->whereOr('a.flow_user_id', ['like', '%' . $user_id . '%']);
  340. };
  341. }
  342. $userModel = new \app\admin\model\User();
  343. switch ($param['type']) {
  344. case '1':
  345. //合同
  346. $list = db('crm_contract')
  347. ->alias('a')
  348. ->join('__CRM_CUSTOMER__ customer', 'a.customer_id = customer.customer_id', 'LEFT')
  349. ->join('__ADMIN_USER__ user', 'user.id = a.create_user_id', 'LEFT')
  350. ->join('__ADMIN_EXAMINE_FLOW__ examine_flow', 'examine_flow.flow_id = a.flow_id', 'LEFT')
  351. ->where($where)
  352. ->where($whereOr)
  353. ->field(
  354. 'a.contract_id as catagory_id,a.name,a.create_time,a.check_status,a.create_user_id,a.check_user_id,a.flow_user_id,
  355. customer.name as customer_name,
  356. a.customer_id,user.realname,examine_flow.name as examine_name'
  357. )
  358. ->page($param['page'], $param['limit'])
  359. ->order('a.create_time desc')
  360. ->select();
  361. foreach ($list as $k => $v) {
  362. $list[$k]['customer_id_info']['customer_id'] = $v['customer_id'];
  363. $list[$k]['customer_id_info']['name'] = $v['customer_name'];
  364. $list[$k]['create_user_info'] = $userModel->getUserById($v['create_user_id']);
  365. }
  366. $dataCount = db('crm_contract')
  367. ->alias('a')
  368. ->join('__CRM_CUSTOMER__ customer', 'a.customer_id = customer.customer_id', 'LEFT')
  369. ->join('__ADMIN_USER__ user', 'user.id = a.create_user_id', 'LEFT')
  370. ->where($where)
  371. ->where($whereOr)
  372. ->count();
  373. break;
  374. case '2':
  375. //回款
  376. $list = db('crm_receivables')
  377. ->alias('a')
  378. ->join('__ADMIN_USER__ user', 'user.id = a.create_user_id', 'LEFT')
  379. ->join('__ADMIN_EXAMINE_FLOW__ examine_flow', 'examine_flow.flow_id = a.flow_id', 'LEFT')
  380. ->join('__CRM_CONTRACT__ contract', 'a.contract_id = contract.contract_id', 'LEFT')
  381. ->where($where)
  382. ->where($whereOr)
  383. ->field(
  384. 'a.receivables_id as catagory_id ,a.number as name,a.create_time,a.check_status,a.check_user_id,a.flow_user_id,
  385. contract.name as contract_name,a.create_user_id,a.contract_id,user.realname,examine_flow.name as examine_name'
  386. )
  387. ->page($param['page'], $param['limit'])
  388. ->order('a.create_time desc')
  389. ->select();
  390. $dataCount = db('crm_receivables')
  391. ->alias('a')
  392. ->join('__ADMIN_USER__ user', 'user.id = a.create_user_id', 'LEFT')
  393. ->join('__CRM_CONTRACT__ contract', 'a.contract_id = contract.contract_id', 'LEFT')
  394. ->where($where)
  395. ->where($whereOr)
  396. ->count();
  397. foreach ($list as $k => $v) {
  398. $list[$k]['create_user_info'] = $userModel->getUserById($v['create_user_id']);
  399. $list[$k]['contract_id_info']['contract_id'] = $v['contract_id'];
  400. $list[$k]['contract_id_info']['name'] = $v['contract_name'];
  401. }
  402. break;
  403. case '3':
  404. $list = db('crm_invoice')
  405. ->alias('a')
  406. ->join('__ADMIN_USER__ user', 'user.id = a.create_user_id', 'LEFT')
  407. ->join('__ADMIN_EXAMINE_FLOW__ examine_flow', 'examine_flow.flow_id = a.flow_id', 'LEFT')
  408. ->where($where)
  409. ->where($whereOr)
  410. ->field(
  411. 'a.invoice_id as catagory_id ,a.invoice_apple_number as name,a.create_time,a.check_status,a.create_user_id,a.check_user_id,a.flow_user_id,user.realname,examine_flow.name as examine_name'
  412. )
  413. ->page($param['page'], $param['limit'])
  414. ->order('a.create_time desc')
  415. ->select();
  416. foreach ($list as $k => $v) {
  417. $list[$k]['create_user_info'] = $userModel->getUserById($v['create_user_id']);
  418. }
  419. $dataCount = db('crm_invoice')
  420. ->alias('a')
  421. ->join('__ADMIN_USER__ user', 'user.id = a.create_user_id', 'LEFT')
  422. ->where($where)
  423. ->where($whereOr)
  424. ->count();
  425. break;
  426. }
  427. foreach ($list as $key => $v) {
  428. $list[$key]['create_time'] = date('Y-m-d H:i:s', $v['create_time']) ?: '';
  429. }
  430. $data = [];
  431. $data['page']['list'] = $list;
  432. $data['page']['dataCount'] = $dataCount ?: 0;
  433. if ($param['page'] != 1 && ($param['page'] * $param['limit']) >= $dataCount) {
  434. $data['page']['firstPage'] = false;
  435. $data['page']['lastPage'] = true;
  436. } else if ($param['page'] != 1 && (int)($param['page'] * $param['limit']) < $dataCount) {
  437. $data['page']['firstPage'] = false;
  438. $data['page']['lastPage'] = false;
  439. } else if ($param['page'] == 1) {
  440. $data['page']['firstPage'] = true;
  441. $data['page']['lastPage'] = false;
  442. }
  443. return $data;
  444. }
  445. }