ApproveInfoService.php 45 KB

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