ApproveInfoService.php 55 KB

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