ApproveInfoService.php 55 KB

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