ApproveInfoService.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  1. <?php
  2. namespace app\common\service;
  3. use app\common\constant\ApplyConstant;
  4. use app\common\constant\CommonConstant;
  5. use app\common\model\Approve;
  6. use app\common\model\ApproveCopy;
  7. use app\common\model\ApproveApplyGoods;
  8. use app\common\model\ApproveStockGoods;
  9. use app\common\model\ApproveEvectionPeerUser;
  10. use app\common\model\ApproveInfo;
  11. use app\common\model\ApproveUseGoods;
  12. use app\common\model\Department;
  13. use app\common\model\Goods;
  14. use app\common\model\GoodsStock;
  15. use app\common\model\User;
  16. use think\Db;
  17. use think\Exception;
  18. /**
  19. * 审批申请服务类
  20. */
  21. class ApproveInfoService
  22. {
  23. /**
  24. * 申请/重新发起
  25. *
  26. * @param integer $id 申请ID
  27. * @param integer $module 模块类型
  28. * @param array $params
  29. * @param mixed $user 用户信息
  30. **/
  31. public static function create($id, $module, $params, $user)
  32. {
  33. $userid = $user['userid'];
  34. $apply_user_id = $userid;
  35. $department = $user['department'];
  36. // 申请人信息
  37. if (in_array($module, [CommonConstant::MODULE_5, CommonConstant::MODULE_6, CommonConstant::MODULE_7])) {
  38. $apply_user = User::field('userid,department')
  39. ->where('userid', $params['apply_user_id'])
  40. ->find();
  41. if (!$apply_user) {
  42. except('申请人信息不存在或已删除');
  43. }
  44. $apply_user_id = $apply_user['userid'];
  45. $department = $apply_user['department'];
  46. }
  47. // 编辑
  48. if ($id > 0) {
  49. $info = ApproveInfo::field('is_deleted', true)
  50. ->where('module', $module)
  51. ->where('user_id', $userid)
  52. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  53. ->with([
  54. 'moduleInfo'
  55. ])
  56. ->find($id);
  57. if (!$info) {
  58. except('申请记录不存在或已删除');
  59. }
  60. if (!$info->module_info) {
  61. except(CommonConstant::get_module_list()[$module] . '记录不存在或已删除');
  62. }
  63. if ($info->status != CommonConstant::STATUS_4) {
  64. except('非审批驳回状态无法操作');
  65. }
  66. }
  67. $order_no = get_order_sn($user['id']);
  68. $approve_user = $params['approve_user'] ? explode(',', $params['approve_user']) : [];
  69. $copy_user = $params['copy_user'] ? explode(',', $params['copy_user']) : [];
  70. $approve_num = $approve_user ? count($approve_user) : 0;
  71. $data = [
  72. 'module' => $module,
  73. 'user_id' => $userid,
  74. 'apply_user_id' => $apply_user_id,
  75. 'department' => $department,
  76. 'status' => CommonConstant::STATUS_2,
  77. 'approve_num' => $approve_num,
  78. 'cur_num' => 0,
  79. 'order_no' => $order_no,
  80. 'apply_date' => date("Y-m-d"),
  81. 'reason' => $params['reason'],
  82. 'type' => $params['type'],
  83. 'desc' => $params['desc'],
  84. 'start_time' => $params['start_time'],
  85. 'end_time' => $params['end_time'],
  86. 'apply_id' => $params['apply_id'],
  87. ];
  88. Db::startTrans();
  89. try {
  90. if ($id > 0) {
  91. // 编辑对应模块
  92. self::create_module($module, $params, $id, $info, 'update');
  93. // 编辑审批抄送
  94. self::create_approve($approve_user, $copy_user, $userid, $id, 'update');
  95. // 编辑审批申请
  96. $save_data = [
  97. 'create_at' => date('Y-m-d H:i:s'),
  98. ];
  99. $data = array_merge($data, $save_data);
  100. $info->save($data);
  101. } else {
  102. // 添加审批申请
  103. $info = ApproveInfo::create($data);
  104. $info_id = $info->id;
  105. // 添加对应模块
  106. $module_data = self::create_module($module, $params, $info_id, [], 'create');
  107. $info->module_id = $module_data['module_id'];
  108. $info->save();
  109. // 添加审批抄送
  110. self::create_approve($approve_user, $copy_user, $userid, $info_id, 'create');
  111. }
  112. Db::commit();
  113. } catch (Exception $e) {
  114. except('出现错误:' . $e->getMessage(), 2, $e);
  115. }
  116. return true;
  117. }
  118. /**
  119. * 添加对应模块
  120. *
  121. * @param integer $module 模块类型
  122. * @param array $params
  123. * @param integer $info_id 申请ID
  124. * @param mixed $info 申请信息
  125. * @param string $type 类型:create=申请,update=重新发起
  126. **/
  127. public static function create_module($module, $params, $info_id, $info, $type)
  128. {
  129. $other = [];
  130. switch ($module) {
  131. case CommonConstant::MODULE_1:
  132. // 添加申购物品
  133. $total_amount = 0;
  134. if ($params['type'] == ApplyConstant::TYPE_1) {
  135. $other = self::create_goods($info_id, $module, $params['apply_goods'], $type);
  136. $total_amount = $other['total_amount'];
  137. }
  138. $data['info_id'] = $info_id;
  139. $data['reason'] = $params['reason'];
  140. $data['type'] = $params['type'];
  141. $data['total_amount'] = $total_amount; // $params['total_amount']
  142. $data['apply_date'] = $params['apply_date'];
  143. $data['document'] = $params['document'];
  144. $data['images'] = $params['images'];
  145. $data['pay_type'] = $params['pay_type'];
  146. break;
  147. case CommonConstant::MODULE_2:
  148. $data['info_id'] = $info_id;
  149. $data['type'] = $params['type'];
  150. $data['word_size'] = $params['word_size'];
  151. $data['desc'] = $params['desc'];
  152. $data['number'] = $params['number'];
  153. $data['reason'] = $params['reason'];
  154. $data['remark'] = $params['remark'];
  155. $data['document'] = $params['document'];
  156. break;
  157. case CommonConstant::MODULE_3:
  158. // 添加入库物品
  159. $other = self::create_goods($info_id, $module, $params['stock_goods'], $type);
  160. $data['info_id'] = $info_id;
  161. $data['total_amount'] = $other['total_amount'];
  162. $data['document'] = $params['document'];
  163. $data['images'] = $params['images'];
  164. $data['remark'] = $params['remark'];
  165. break;
  166. case CommonConstant::MODULE_4:
  167. // 添加领用物品
  168. $other = self::create_goods($info_id, $module, $params['use_goods'], $type);
  169. $data['info_id'] = $info_id;
  170. $data['reason'] = $params['reason'];
  171. $data['document'] = $params['document'];
  172. $data['remark'] = $params['remark'];
  173. break;
  174. case CommonConstant::MODULE_5:
  175. // 添加同行人员
  176. $other = self::create_peer_user($info_id, $params['peer_user'], $type);
  177. $data['info_id'] = $info_id;
  178. $data['reason'] = $params['reason'];
  179. $data['start_time'] = $params['start_time'];
  180. $data['end_time'] = $params['end_time'];
  181. $data['document'] = $params['document'];
  182. $data['images'] = $params['images'];
  183. $data['type'] = $params['type'];
  184. $data['is_who'] = $params['is_who'];
  185. $data['remark'] = $params['remark'];
  186. break;
  187. case CommonConstant::MODULE_6:
  188. $data['info_id'] = $info_id;
  189. $data['type'] = $params['type'];
  190. $data['start_time'] = $params['start_time'];
  191. $data['end_time'] = $params['end_time'];
  192. $data['start_am'] = $params['start_am'];
  193. $data['end_am'] = $params['end_am'];
  194. $data['time'] = $params['time'];
  195. $data['reason'] = $params['reason'];
  196. $data['document'] = $params['document'];
  197. $data['remark'] = $params['remark'];
  198. break;
  199. case CommonConstant::MODULE_7:
  200. $data['info_id'] = $info_id;
  201. $data['reason'] = $params['reason'];
  202. $data['start_time'] = $params['start_time'];
  203. $data['reach_address'] = $params['reach_address'];
  204. $data['end_time'] = $params['end_time'];
  205. $data['end_address'] = $params['end_address'];
  206. $data['document'] = $params['document'];
  207. $data['images'] = $params['images'];
  208. break;
  209. case CommonConstant::MODULE_8:
  210. $data['info_id'] = $info_id;
  211. $data['type'] = $params['type'];
  212. $data['reason'] = $params['reason'];
  213. $data['desc'] = $params['desc'];
  214. $data['images'] = $params['images'];
  215. break;
  216. case CommonConstant::MODULE_9:
  217. $data['info_id'] = $info_id;
  218. $data['type'] = $params['type'];
  219. $data['reason'] = $params['reason'];
  220. $data['desc'] = $params['desc'];
  221. $data['number'] = $params['number'];
  222. $data['scope'] = $params['scope'];
  223. $data['legal_opinion'] = $params['legal_opinion'];
  224. $data['document'] = $params['document'];
  225. $data['remark'] = $params['remark'];
  226. break;
  227. case CommonConstant::MODULE_10:
  228. $data['info_id'] = $info_id;
  229. $data['founder'] = $params['founder'];
  230. $data['desc'] = $params['desc'];
  231. $data['serial_number'] = $params['serial_number'];
  232. $data['reason'] = $params['reason'];
  233. $data['apply_date'] = $params['apply_date'];
  234. $data['remark'] = $params['remark'];
  235. $data['degree'] = $params['degree'];
  236. $data['document'] = $params['document'];
  237. break;
  238. case CommonConstant::MODULE_11:
  239. $data['info_id'] = $info_id;
  240. $data['department'] = $params['department'];
  241. $data['reason'] = $params['reason'];
  242. $data['department_sign'] = $params['department_sign'];
  243. $data['remark'] = $params['remark'];
  244. $data['document'] = $params['document'];
  245. $data['desc'] = $params['desc'];
  246. $data['apply_date'] = $params['apply_date'];
  247. $data['serial_number'] = $params['serial_number'];
  248. break;
  249. }
  250. if ($type == 'update') {
  251. $info->module_info->save($data);
  252. $module_id = $info->module_info->id;
  253. } else {
  254. $models = CommonConstant::get_module_model_list();
  255. $model = $models[$module];
  256. $module_result = $model::create($data);
  257. $module_id = $module_result->id;
  258. }
  259. $module_data = $data;
  260. $result = compact("module_id", "module_data", "other");
  261. return $result;
  262. }
  263. /**
  264. * 添加申购物品/入库物品/领用物品
  265. *
  266. * @param integer $info_id 申请ID
  267. * @param integer $module 模块类型:1,3,4
  268. * @param array $params 申购物品/入库物品/领用物品数据
  269. * @param string $type 类型:create=申请,update=重新发起
  270. **/
  271. public static function create_goods($info_id, $module, $params, $type)
  272. {
  273. if (!$params) {
  274. return [];
  275. }
  276. $goods_list_new = [];
  277. $goods_ids_import = [];
  278. $goods_list_import = [];
  279. $goods_ids = [];
  280. $goods_ids_new = [];
  281. $data = [];
  282. $total_amount = 0;
  283. foreach ($params as $value) {
  284. if ($value['flag'] == 1) {
  285. // 添加新商品
  286. $goods_ids_new[] = $value;
  287. $goods_info = Goods::field('status,is_deleted,create_at', true)
  288. ->where('goods_category_first', $value['goods_category_first'])
  289. ->where('goods_category_id', $value['goods_category_id'])
  290. ->where('goods_name', $value['goods_name'])
  291. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  292. ->with([
  293. 'goodsStock',
  294. ])
  295. ->find();
  296. if ($goods_info) {
  297. $goods_list_new[] = $goods_info->toArray();
  298. }
  299. }
  300. if ($value['flag'] == 2) {
  301. // 批量导入
  302. }
  303. if ($value['flag'] == 3) {
  304. // 商品库选择
  305. $goods_ids[$value['goods_id']] = $value;
  306. }
  307. }
  308. switch ($module) {
  309. case CommonConstant::MODULE_1:
  310. $model = ApproveApplyGoods::class;
  311. break;
  312. case CommonConstant::MODULE_3:
  313. $model = ApproveStockGoods::class;
  314. break;
  315. case CommonConstant::MODULE_4:
  316. $model = ApproveUseGoods::class;
  317. break;
  318. }
  319. if ($type == 'update') {
  320. $model::where('info_id', $info_id)->delete();
  321. }
  322. // 商品库选择
  323. if ($goods_ids) {
  324. $goods_list = Goods::field('status,is_deleted,create_at', true)
  325. ->where('id', 'in', array_keys($goods_ids))
  326. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  327. ->with([
  328. 'goodsStock',
  329. ])
  330. ->select()
  331. ->toArray();
  332. $goods_object = $goods_list ? array_column($goods_list, null, 'id') : [];
  333. $goods_name = '';
  334. $goods_stock_name = '';
  335. $apply_goods_data = [];
  336. foreach ($goods_ids as $key => $value) {
  337. if (array_key_exists($value['goods_id'], $goods_object)) {
  338. // 商品库里有该商品
  339. $goods_info = $goods_object[$value['goods_id']];
  340. $goods_stock_info = array_column($goods_info['goods_stock'], null, 'id');
  341. $goods_stock_data = [];
  342. $total_price = 0;
  343. foreach ($value['goods_stock'] as $val) {
  344. if (array_key_exists($val['id'], $goods_stock_info)) {
  345. // 商品库里有该商品规格
  346. $stock = isset($val['stock']) ? $val['stock'] : 0;
  347. $price = isset($val['price']) ? $val['price'] : 0;
  348. $total_price = bcadd($total_price, $stock * $price, 2);
  349. $goods_stock_data[] = [
  350. 'id' => $val['id'],
  351. 'goods_id' => $goods_info['id'],
  352. 'name' => $val['name'],
  353. 'price' => $price,
  354. 'stock' => $stock,
  355. ];
  356. } else {
  357. // 商品库里没有该商品规格
  358. $goods_stock_name .= $value['goods_name'] . '-' . $val['name'] . '、';
  359. }
  360. }
  361. $goods_data = [
  362. 'info_id' => $info_id,
  363. 'goods_id' => $goods_info['id'],
  364. 'goods_category_first' => $goods_info['goods_category_first'],
  365. 'goods_category_id' => $goods_info['goods_category_id'],
  366. 'goods_no' => $goods_info['goods_no'],
  367. 'goods_name' => $goods_info['goods_name'],
  368. 'goods_brand' => $goods_info['goods_brand'],
  369. 'total_price' => $total_price,
  370. 'goods_stock' => $goods_stock_data,
  371. ];
  372. $data[] = $goods_data;
  373. $goods_data['goods_stock'] = json_encode($goods_stock_data, JSON_UNESCAPED_UNICODE);
  374. $apply_goods_data[] = $goods_data;
  375. $total_amount = bcadd($total_amount, $total_price, 2);
  376. } else {
  377. // 商品库里没有该商品
  378. $goods_name .= $value['goods_name'] . '、';
  379. }
  380. }
  381. if ($goods_name) {
  382. // 商品库选择的商品 在商品库没有匹配到 返回错误提示
  383. except($goods_name . '等商品在商品库里不存在或已删除');
  384. }
  385. if ($goods_stock_name) {
  386. // 商品库选择的商品的规格 在商品库没有匹配到 返回错误提示
  387. except($goods_stock_name . '等商品规格在商品库里不存在或已删除');
  388. }
  389. $model::insertAll($apply_goods_data);
  390. }
  391. // 添加新商品
  392. if ($goods_ids_new) {
  393. $goods_object = $goods_list_new ? array_column($goods_list_new, null, 'goods_name') : [];
  394. $apply_goods_data = [];
  395. foreach ($goods_ids_new as $value) {
  396. $goods_data = [
  397. 'goods_category_first' => $value['goods_category_first'],
  398. 'goods_category_id' => $value['goods_category_id'],
  399. 'goods_no' => $value['goods_no'],
  400. 'goods_name' => $value['goods_name'],
  401. 'goods_brand' => isset($value['goods_brand']) ? $value['goods_brand'] : '',
  402. ];
  403. if (array_key_exists($value['goods_name'], $goods_object)) {
  404. // 商品库里有该商品
  405. $goods_info = $goods_object[$value['goods_name']];
  406. $goods_id = $goods_info['id'];
  407. $goods_stock_info = array_column($goods_info['goods_stock'], null, 'name');
  408. } else {
  409. // 商品库里没有该商品 创建
  410. $goods = Goods::create($goods_data);
  411. $goods_id = $goods->id;
  412. $goods_stock_info = [];
  413. }
  414. $goods_stock_data = [];
  415. $total_price = 0;
  416. foreach ($value['goods_stock'] as $val) {
  417. if (array_key_exists($val['name'], $goods_stock_info)) {
  418. // 商品库里有该商品规格
  419. $stock_id = $goods_stock_info[$val['name']]['id'];
  420. } else {
  421. // 商品库里没有该商品规格 创建
  422. $stock_data = [
  423. 'goods_id' => $goods_id,
  424. 'name' => $val['name'],
  425. ];
  426. $stock = GoodsStock::create($stock_data);
  427. $stock_id = $stock->id;
  428. }
  429. $stock = isset($val['stock']) ? $val['stock'] : 0;
  430. $price = isset($val['price']) ? $val['price'] : 0;
  431. $total_price = bcadd($total_price, $stock * $price, 2);
  432. $goods_stock_data[] = [
  433. 'id' => $stock_id,
  434. 'goods_id' => $goods_id,
  435. 'name' => $val['name'],
  436. 'price' => $price,
  437. 'stock' => $stock,
  438. ];
  439. }
  440. $goods_data['info_id'] = $info_id;
  441. $goods_data['goods_id'] = $goods_id;
  442. $goods_data['total_price'] = $total_price;
  443. $goods_data['goods_stock'] = $goods_stock_data;
  444. $data[] = $goods_data;
  445. $goods_data['goods_stock'] = json_encode($goods_stock_data, JSON_UNESCAPED_UNICODE);
  446. $apply_goods_data[] = $goods_data;
  447. $total_amount = bcadd($total_amount, $total_price, 2);
  448. }
  449. $model::insertAll($apply_goods_data);
  450. }
  451. return compact("total_amount", "data");
  452. }
  453. /**
  454. * 添加同行人员
  455. *
  456. * @param integer $info_id 申请ID
  457. * @param array $params 同行人员数据
  458. * @param string $type 类型:create=申请,update=重新发起
  459. **/
  460. public static function create_peer_user($info_id, $params, $type)
  461. {
  462. if ($type == 'update') {
  463. ApproveEvectionPeerUser::where('info_id', $info_id)->delete();
  464. }
  465. $data = [];
  466. if (!$params) {
  467. return $data;
  468. }
  469. foreach ($params as $value) {
  470. if (isset($value['name']) && !empty($value['name'])) {
  471. $data[] = [
  472. 'info_id' => $info_id,
  473. 'is_who' => $value['is_who'],
  474. 'user_id' => isset($value['user_id']) ? $value['user_id'] : '',
  475. 'name' => $value['name'],
  476. 'desc' => isset($value['desc']) ? $value['desc'] : '',
  477. ];
  478. }
  479. }
  480. if ($data) {
  481. ApproveEvectionPeerUser::insertAll($data);
  482. }
  483. return $data;
  484. }
  485. /**
  486. * 添加审批抄送
  487. *
  488. * @param array $approve_user 审批ID
  489. * @param array $copy_user 抄送ID
  490. * @param string $userid 发起人ID
  491. * @param integer $info_id 申请ID
  492. * @param string $type 类型:create=申请,update=重新发起
  493. **/
  494. public static function create_approve($approve_user, $copy_user, $userid, $info_id, $type)
  495. {
  496. if ($type == 'update') {
  497. ApproveCopy::where('info_id', $info_id)->delete();
  498. }
  499. $approve_data = [];
  500. $copy_data = [];
  501. $flow_num = 0;
  502. $copy_num = 0;
  503. // 发起人
  504. $apply_data = [
  505. 'info_id' => $info_id,
  506. 'status' => 0,
  507. 'group' => CommonConstant::IS_WHO_1,
  508. 'approve_user' => $userid,
  509. 'approve_flow' => 1,
  510. ];
  511. // 审批人
  512. foreach ($approve_user as $key => $value) {
  513. $flow_num++;
  514. $approve_data[] = [
  515. 'info_id' => $info_id,
  516. 'status' => $flow_num == 1 ? CommonConstant::STATUS_2 : CommonConstant::STATUS_1,
  517. 'group' => CommonConstant::IS_WHO_0,
  518. 'approve_user' => $value,
  519. 'approve_flow' => $flow_num,
  520. ];
  521. }
  522. // 抄送人
  523. foreach ($copy_user as $key => $value) {
  524. $copy_num++;
  525. $copy_data[] = [
  526. 'info_id' => $info_id,
  527. 'approve_user' => $value,
  528. 'approve_flow' => $copy_num,
  529. ];
  530. }
  531. if ($approve_data) {
  532. // 添加审批
  533. $approve_data = array_merge([$apply_data], $approve_data);
  534. Approve::insertAll($approve_data);
  535. }
  536. if ($copy_data) {
  537. // 添加抄送
  538. ApproveCopy::insertAll($copy_data);
  539. }
  540. }
  541. /**
  542. * 我的申请记录
  543. *
  544. * @param integer $module 模块类型
  545. * @param integer $status 审批状态
  546. * @param string $search 搜索
  547. * @param integer $offset 起始位置
  548. * @param integer $length 查询数量
  549. * @param mixed $user 用户信息
  550. **/
  551. public static function get_list($module, $status, $search, $offset, $length, $user)
  552. {
  553. if (!array_key_exists($module, CommonConstant::get_module_list())) {
  554. return [];
  555. }
  556. if (!array_key_exists($status, CommonConstant::get_status_list())) {
  557. return [];
  558. }
  559. $userid = $user['userid'];
  560. $list = ApproveInfo::field('module_id,user_id,apply_user_id,department,is_deleted', true)
  561. ->where('module', $module)
  562. ->where(function ($query) use ($module, $userid) {
  563. if (in_array($module, [CommonConstant::MODULE_5, CommonConstant::MODULE_6, CommonConstant::MODULE_7])) {
  564. $query->where('user_id', $userid)->whereOr('apply_user_id', $userid);
  565. } else {
  566. $query->where('user_id', $userid);
  567. }
  568. })
  569. ->where('status', $status)
  570. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  571. ->when($search, function ($query) use ($search) {
  572. $query->where('order_no|reason', 'like', '%' . $search . '%');
  573. });
  574. $list = self::get_with($list, $module, $status);
  575. $list = $list->order('id desc')
  576. ->limit($offset, $length)
  577. ->select();
  578. return $list;
  579. }
  580. /**
  581. * 详情/信息
  582. *
  583. * @param integer $id 申请ID
  584. * @param mixed $user 用户信息 有数据=我的申请详情/信息,没有数据=审批详情
  585. * @param string $type 类型:detail=详情,info=信息
  586. **/
  587. public static function get_detail($id, $user, $type)
  588. {
  589. $info = ApproveInfo::field('is_deleted', true)
  590. ->where(function ($query) use ($user) {
  591. if ($user) {
  592. $query->where('user_id', $user['userid'])->whereOr('apply_user_id', $user['userid']);
  593. }
  594. })
  595. ->where('is_deleted', CommonConstant::IS_DELETED_0);
  596. if ($type == 'detail') {
  597. $info->with([
  598. 'moduleInfo',
  599. 'approve' => function ($query) {
  600. $query->field('is_deleted,create_at', true)
  601. ->where('state', 'in',[CommonConstant::IS_WHO_0,CommonConstant::IS_WHO_1])
  602. ->with([
  603. 'user' => function ($query) {
  604. $query->field('userid,name,avatar');
  605. }
  606. ]);
  607. },
  608. 'approveCopy' => function ($query) {
  609. $query->field('is_deleted,create_at', true)
  610. ->with([
  611. 'user' => function ($query) {
  612. $query->field('userid,name,avatar');
  613. }
  614. ]);
  615. }
  616. ]);
  617. $info = $info->find($id);
  618. if ($info) {
  619. //
  620. $module = $info['module'];
  621. // 审批提交人申请人信息
  622. if ($user) {
  623. $apply_user = [
  624. 'userid' => $user['userid'],
  625. 'name' => $user['name'],
  626. 'avatar' => $user['avatar'],
  627. ];
  628. $create_user = $apply_user;
  629. $create_user['approve_time'] = $info['create_at'];
  630. if (in_array($module, [CommonConstant::MODULE_5, CommonConstant::MODULE_6, CommonConstant::MODULE_7])) {
  631. if ($info['user_id'] != $info['apply_user_id']) {
  632. $apply_user_info = User::field('userid,name,avatar')
  633. ->where('userid', $info['apply_user_id'])
  634. ->find();
  635. $apply_user = [
  636. 'userid' => $apply_user_info['userid'],
  637. 'name' => $apply_user_info['name'],
  638. 'avatar' => $apply_user_info['avatar'],
  639. ];
  640. }
  641. }
  642. } else {
  643. $apply_user = User::field('userid,name,avatar')
  644. ->where('userid', $info['apply_user_id'])
  645. ->find();
  646. $apply_user = [
  647. 'userid' => $apply_user['userid'],
  648. 'name' => $apply_user['name'],
  649. 'avatar' => $apply_user['avatar'],
  650. ];
  651. $create_user = $apply_user;
  652. $create_user['approve_time'] = $info['create_at'];
  653. if (in_array($module, [CommonConstant::MODULE_5, CommonConstant::MODULE_6, CommonConstant::MODULE_7])) {
  654. if ($info['user_id'] != $info['apply_user_id']) {
  655. $apply_user_info = User::field('userid,name,avatar')
  656. ->where('userid', $info['userid'])
  657. ->find();
  658. $create_user = [
  659. 'userid' => $apply_user_info['userid'],
  660. 'name' => $apply_user_info['name'],
  661. 'avatar' => $apply_user_info['avatar'],
  662. ];
  663. }
  664. }
  665. }
  666. // 部门列表
  667. $department_data = Department::field('dept_id,name')
  668. ->where('dept_id', 'in', $info['department'])
  669. ->select();
  670. $info['create_user'] = $create_user;
  671. $info['apply_user'] = $apply_user;
  672. $info['department_data'] = $department_data;
  673. }
  674. } else {
  675. // 信息
  676. $info->with([
  677. 'moduleInfo'
  678. ]);
  679. $info = $info->find($id);
  680. }
  681. $info = self::get_item($info, $type);
  682. return $info;
  683. }
  684. /**
  685. * 我的申请记录-关联数据
  686. *
  687. * @param mixed $list
  688. * @param integer $module 模块类型
  689. * @param integer $status 审批状态
  690. **/
  691. public static function get_with($list, $module, $status)
  692. {
  693. $field = 'id,info_id,status,approve_user';
  694. switch ($module) {
  695. case CommonConstant::MODULE_1:
  696. // 申领商品列表
  697. if (in_array($status, [CommonConstant::STATUS_2, CommonConstant::STATUS_4])) {
  698. // 审批中或审批驳回 才关联 审批人信息
  699. $list = $list->with([
  700. 'approveOne' => function ($query) use ($status, $field) {
  701. $query->field($field)
  702. ->where('status', $status)
  703. ->where('group', CommonConstant::IS_WHO_0)
  704. ->where('state', CommonConstant::IS_WHO_0)
  705. ->with([
  706. 'user' => function ($query) {
  707. $query->field('userid,name');
  708. }
  709. ]);
  710. },
  711. 'applyGoods' => function ($query) {
  712. $query->field('id,info_id,goods_name');
  713. },
  714. ]);
  715. } else {
  716. $list = $list->with([
  717. 'applyGoods' => function ($query) {
  718. $query->field('id,info_id,goods_name');
  719. },
  720. ]);
  721. }
  722. break;
  723. case CommonConstant::MODULE_2:
  724. if (in_array($status, [CommonConstant::STATUS_2, CommonConstant::STATUS_4])) {
  725. // 审批中或审批驳回 才关联 审批人信息
  726. $list = $list->with([
  727. 'approveOne' => function ($query) use ($status, $field) {
  728. $query->field($field)
  729. ->where('status', $status)
  730. ->where('group', CommonConstant::IS_WHO_0)
  731. ->where('state', CommonConstant::IS_WHO_0)
  732. ->with([
  733. 'user' => function ($query) {
  734. $query->field('userid,name');
  735. }
  736. ]);
  737. }
  738. ]);
  739. }
  740. break;
  741. case CommonConstant::MODULE_3:
  742. // 入库商品列表
  743. if (in_array($status, [CommonConstant::STATUS_2, CommonConstant::STATUS_4])) {
  744. // 审批中或审批驳回 才关联 审批人信息
  745. $list = $list->with([
  746. 'approveOne' => function ($query) use ($status, $field) {
  747. $query->field($field)
  748. ->where('status', $status)
  749. ->where('group', CommonConstant::IS_WHO_0)
  750. ->where('state', CommonConstant::IS_WHO_0)
  751. ->with([
  752. 'user' => function ($query) {
  753. $query->field('userid,name');
  754. }
  755. ]);
  756. },
  757. 'stockGoods' => function ($query) {
  758. $query->field('id,info_id,goods_name');
  759. },
  760. ]);
  761. } else {
  762. $list = $list->with([
  763. 'stockGoods' => function ($query) {
  764. $query->field('id,info_id,goods_name');
  765. },
  766. ]);
  767. }
  768. break;
  769. case CommonConstant::MODULE_4:
  770. // 领用商品列表
  771. if (in_array($status, [CommonConstant::STATUS_2, CommonConstant::STATUS_4])) {
  772. // 审批中或审批驳回 才关联 审批人信息
  773. $list = $list->with([
  774. 'approveOne' => function ($query) use ($status, $field) {
  775. $query->field($field)
  776. ->where('status', $status)
  777. ->where('group', CommonConstant::IS_WHO_0)
  778. ->where('state', CommonConstant::IS_WHO_0)
  779. ->with([
  780. 'user' => function ($query) {
  781. $query->field('userid,name');
  782. }
  783. ]);
  784. },
  785. 'useGoods' => function ($query) {
  786. $query->field('id,info_id,goods_name');
  787. },
  788. ]);
  789. } else {
  790. $list = $list->with([
  791. 'useGoods' => function ($query) {
  792. $query->field('id,info_id,goods_name');
  793. },
  794. ]);
  795. }
  796. break;
  797. case CommonConstant::MODULE_5:
  798. // 出差同行人员列表
  799. if (in_array($status, [CommonConstant::STATUS_2, CommonConstant::STATUS_4])) {
  800. // 审批中或审批驳回 才关联 审批人信息
  801. $list = $list->with([
  802. 'approveOne' => function ($query) use ($status, $field) {
  803. $query->field($field)
  804. ->where('status', $status)
  805. ->where('group', CommonConstant::IS_WHO_0)
  806. ->where('state', CommonConstant::IS_WHO_0)
  807. ->with([
  808. 'user' => function ($query) {
  809. $query->field('userid,name');
  810. }
  811. ]);
  812. },
  813. 'peerUser' => function ($query) {
  814. $query->field('id,info_id,name');
  815. },
  816. ]);
  817. } else {
  818. $list = $list->with([
  819. 'peerUser' => function ($query) {
  820. $query->field('id,info_id,name');
  821. },
  822. ]);
  823. }
  824. break;
  825. case CommonConstant::MODULE_6:
  826. case CommonConstant::MODULE_7:
  827. case CommonConstant::MODULE_8:
  828. case CommonConstant::MODULE_9:
  829. case CommonConstant::MODULE_10:
  830. case CommonConstant::MODULE_11:
  831. if (in_array($status, [CommonConstant::STATUS_2, CommonConstant::STATUS_4])) {
  832. // 审批中或审批驳回 才关联 审批人信息
  833. $list = $list->with([
  834. 'approveOne' => function ($query) use ($status, $field) {
  835. $query->field($field)
  836. ->where('status', $status)
  837. ->where('group', CommonConstant::IS_WHO_0)
  838. ->where('state', CommonConstant::IS_WHO_0)
  839. ->with([
  840. 'user' => function ($query) {
  841. $query->field('userid,name');
  842. }
  843. ]);
  844. }
  845. ]);
  846. }
  847. break;
  848. }
  849. return $list;
  850. }
  851. /**
  852. * 详情/信息-关联数据
  853. *
  854. * @param mixed $info
  855. * @param string $type 类型:detail=详情,info=信息
  856. **/
  857. public static function get_item($info, $type)
  858. {
  859. if ($info) {
  860. $module = $info['module'];
  861. switch ($module) {
  862. case CommonConstant::MODULE_1:
  863. $info['apply_goods'] = [];
  864. if ($info['type'] == ApplyConstant::TYPE_1) {
  865. if ($type == 'detail') {
  866. $info['apply_goods'] = $info->applyGoods()->field('id,info_id,goods_name,total_price,goods_stock')->select();
  867. } else {
  868. $info->apply_goods;
  869. }
  870. }
  871. break;
  872. case CommonConstant::MODULE_3:
  873. if ($type == 'detail') {
  874. $info['stock_goods'] = $info->stockGoods()->field('id,info_id,goods_name,total_price,goods_stock')->select();
  875. } else {
  876. $info->stock_goods;
  877. }
  878. break;
  879. case CommonConstant::MODULE_4:
  880. if ($type == 'detail') {
  881. $info['use_goods'] = $info->useGoods()->field('id,info_id,goods_name,total_price,goods_stock')->select();
  882. } else {
  883. $info->use_goods;
  884. }
  885. break;
  886. case CommonConstant::MODULE_5:
  887. if ($type == 'detail') {
  888. $info['peer_user'] = $info->peerUser()->field('id,info_id,name')->select();
  889. } else {
  890. $info->peer_user;
  891. }
  892. break;
  893. }
  894. }
  895. return $info;
  896. }
  897. /**
  898. * 操作方法
  899. *
  900. * @param integer $id 申请ID
  901. * @param mixed $user 用户信息
  902. * @param string $type 类型:urging=催办,cancel=撤销
  903. */
  904. public static function make($id, $user, $type)
  905. {
  906. $userid = $user['userid'];
  907. $info = ApproveInfo::field('id,user_id,module,status')
  908. ->where(function ($query) use ($userid) {
  909. $query->where('user_id', $userid)->whereOr('apply_user_id', $userid);
  910. })
  911. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  912. ->find($id);
  913. if (!$info) {
  914. throw new Exception('申请记录不存在或已删除');
  915. }
  916. if ($info->user_id != $userid) {
  917. throw new Exception('您没有权限操作');
  918. }
  919. switch ($type) {
  920. case 'urging':
  921. if ($info->status != CommonConstant::STATUS_2) {
  922. throw new Exception('其他人已操作');
  923. }
  924. // TODO 待对接钉钉接口
  925. break;
  926. case 'cancel':
  927. if ($info->status != CommonConstant::STATUS_4) {
  928. throw new Exception('非审批驳回状态无法操作');
  929. }
  930. $info->status = CommonConstant::STATUS_5;
  931. $info->save();
  932. break;
  933. }
  934. return true;
  935. }
  936. }