ApproveInfoService.php 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576
  1. <?php
  2. namespace app\common\service;
  3. use app\common\constant\ApplyConstant;
  4. use app\common\constant\CommonConstant;
  5. use app\common\constant\OfferConstant;
  6. use app\common\model\Approve;
  7. use app\common\model\ApproveCopy;
  8. use app\common\model\ApproveApplyGoods;
  9. use app\common\model\ApproveMaintain;
  10. use app\common\model\ApproveMaintainUser;
  11. use app\common\model\ApproveMaintainUserLog;
  12. use app\common\model\ApproveStockGoods;
  13. use app\common\model\ApproveEvectionPeerUser;
  14. use app\common\model\ApproveInfo;
  15. use app\common\model\ApproveUseGoods;
  16. use app\common\model\Goods;
  17. use app\common\model\GoodsCategory;
  18. use app\common\model\GoodsStock;
  19. use app\common\model\User;
  20. use think\cache\driver\Redis;
  21. use think\Db;
  22. use think\Exception;
  23. /**
  24. * 审批申请服务类
  25. */
  26. class ApproveInfoService
  27. {
  28. /**
  29. * 申请/重新发起
  30. *
  31. * @param integer $id 申请ID
  32. * @param integer $module 模块类型
  33. * @param array $params
  34. * @param mixed $user 用户信息
  35. * @param string $way 方式:create=申请,update=重新发起
  36. **/
  37. public static function create($id, $module, $params, $user, $way)
  38. {
  39. $userid = $user['userid'];
  40. $apply_user_id = $userid;
  41. $department = $user['department'];
  42. // 5=出差申请,6=请假申请,7=用车申请 申请人信息 提交人和申请人不是同一人
  43. if (in_array($module, [CommonConstant::MODULE_5, CommonConstant::MODULE_6, CommonConstant::MODULE_7])) {
  44. if ($userid != $params['apply_user_id']) {
  45. $apply_user = User::field('userid,department')
  46. ->where('userid', $params['apply_user_id'])
  47. ->find();
  48. if (!$apply_user) {
  49. except('申请人信息不存在或已删除');
  50. }
  51. $apply_user_id = $apply_user['userid'];
  52. }
  53. }
  54. // 重新发起
  55. if ($id > 0) {
  56. $info = ApproveInfo::field('is_deleted', true)
  57. ->where('module', $module)
  58. ->where('user_id', $userid)
  59. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  60. ->with([
  61. 'moduleInfo'
  62. ])
  63. ->find($id);
  64. if (!$info) {
  65. except('申请记录不存在或已删除');
  66. }
  67. if (!$info->module_info) {
  68. except(CommonConstant::get_module_list()[$module] . '记录不存在或已删除');
  69. }
  70. if ($info->status != CommonConstant::STATUS_4) {
  71. except('非审批驳回状态无法操作');
  72. }
  73. } else {
  74. // 3=入库申请,9=合同呈批申请
  75. if (in_array($module, [CommonConstant::MODULE_3, CommonConstant::MODULE_9])) {
  76. // 存在申购申请单 判断申购申请单
  77. if ($params['apply_id'] > 0) {
  78. $apply = ApproveInfo::field('id')
  79. ->where('module', CommonConstant::MODULE_1)
  80. ->where('status', CommonConstant::STATUS_3)
  81. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  82. ->find($params['apply_id']);
  83. if (!$apply) {
  84. except('申购申请单不存在或已删除');
  85. }
  86. }
  87. }
  88. }
  89. $order_no = get_order_sn($user['id']);
  90. $approve_user = $params['approve_user'] ? explode(',', $params['approve_user']) : [];
  91. $copy_user = $params['copy_user'] ? explode(',', $params['copy_user']) : [];
  92. $approve_num = $approve_user ? count($approve_user) : 0;
  93. $data = [
  94. 'module' => $module,
  95. 'user_id' => $userid,
  96. 'apply_user_id' => $apply_user_id,
  97. 'department' => $department,
  98. 'status' => CommonConstant::STATUS_2,
  99. 'approve_num' => $approve_num,
  100. 'cur_num' => 0,
  101. 'order_no' => $order_no,
  102. 'apply_date' => date("Y-m-d"),
  103. 'reason' => $params['reason'],
  104. 'type' => $params['type'],
  105. 'desc' => $params['desc'],
  106. 'start_time' => $module == CommonConstant::MODULE_6 ? $params['start_time'] . ' ' . $params['start_am'] : $params['start_time'],
  107. 'end_time' => $module == CommonConstant::MODULE_6 ? $params['end_time'] . ' ' . $params['end_am'] : $params['end_time'],
  108. 'apply_id' => $params['apply_id'],
  109. 'maintain_user_id' => 0,
  110. ];
  111. Db::startTrans();
  112. try {
  113. if ($id > 0) {
  114. // 编辑对应模块
  115. $result = self::create_module($module, $params, $id, $info, 'update');
  116. // 编辑审批人
  117. self::create_approve($approve_user, $id, 'update', $userid);
  118. // 编辑抄送人
  119. self::create_approve_copy($copy_user, $id, 'update');
  120. // 编辑审批申请
  121. if ($module == CommonConstant::MODULE_9) {
  122. // 9=合同呈批 不编辑合同编号
  123. unset($data['reason'], $data['type']);
  124. }
  125. unset($data['order_no']);
  126. $data['create_at'] = date('Y-m-d H:i:s');
  127. $info->save($data);
  128. } else {
  129. // 添加审批申请
  130. $info = ApproveInfo::create($data);
  131. $info_id = $info->id;
  132. // 添加对应模块
  133. $result = self::create_module($module, $params, $info_id, [], 'create');
  134. $info->module_id = $result['module_id'];
  135. $info->save();
  136. // 添加审批人
  137. self::create_approve($approve_user, $info_id, 'create', $userid);
  138. // 添加抄送人
  139. self::create_approve_copy($copy_user, $info_id, 'create');
  140. }
  141. user_log('approve', json_encode($result, JSON_UNESCAPED_UNICODE));
  142. Db::commit();
  143. } catch (Exception $e) {
  144. Db::rollback();
  145. // except('出现错误:' . $e->getMessage() . '|' . $e->getFile() . '|' . $e->getLine());
  146. except('出现错误:' . $e->getMessage());
  147. }
  148. // 4=领用申请 增加冻结库存
  149. if ($module == CommonConstant::MODULE_4) {
  150. list($goodsStockSave, $goodsStockLog, $redisStock) = self::composition_data($result['other']['data'], CommonConstant::MODULE_4, CommonConstant::STATUS_2);
  151. if ($goodsStockSave) {
  152. // 批量更新商品库存
  153. (new GoodsStock)->saveAll($goodsStockSave);
  154. }
  155. if ($redisStock) {
  156. // 库存缓存记录
  157. $redis = new Redis(config('redis.'));
  158. foreach ($redisStock as $key => $value) {
  159. $redis->inc($value['id'], $value['stock']);
  160. }
  161. }
  162. }
  163. // 9=合同呈批申请 提交 生成合同编号
  164. if ($module == CommonConstant::MODULE_9 && $way == CommonConstant::create) {
  165. $contract_no = CommonService::get_contract_no($params['type']);
  166. $info->save(['reason' => $contract_no]);
  167. $models = CommonConstant::get_module_model_list();
  168. $model = $models[$module];
  169. $model::where('id', $result['module_id'])->update(['reason' => $contract_no]);
  170. }
  171. return true;
  172. }
  173. /**
  174. * 添加对应模块
  175. *
  176. * @param integer $module 模块类型
  177. * @param array $params
  178. * @param integer $info_id 申请ID
  179. * @param mixed $info 申请信息
  180. * @param string $type 类型:create=申请,update=重新发起,edit:审批人修改 只有module=4才传edit
  181. * @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"}]}]}}
  182. **/
  183. public static function create_module($module, $params, $info_id, $info, $type)
  184. {
  185. $other = [];
  186. switch ($module) {
  187. case CommonConstant::MODULE_1:
  188. // 添加申购物品
  189. $total_amount = 0;
  190. if ($params['type'] == ApplyConstant::TYPE_1) {
  191. $other = self::create_goods($module, $params['apply_goods'], $info_id, $info, $type);
  192. $total_amount = $other['total_amount'];
  193. }
  194. $data['info_id'] = $info_id;
  195. $data['reason'] = $params['reason'];
  196. $data['type'] = $params['type'];
  197. $data['total_amount'] = $params['total_amount']; // $total_amount
  198. $data['start_time'] = $params['start_time'];
  199. $data['document'] = $params['document'];
  200. $data['images'] = $params['images'];
  201. $data['pay_type'] = $params['pay_type'];
  202. break;
  203. case CommonConstant::MODULE_2:
  204. $data['info_id'] = $info_id;
  205. $data['type'] = $params['type'];
  206. $data['word_size'] = $params['word_size'];
  207. $data['desc'] = $params['desc'];
  208. $data['number'] = $params['number'];
  209. $data['reason'] = $params['reason'];
  210. $data['remark'] = $params['remark'];
  211. $data['document'] = $params['document'];
  212. break;
  213. case CommonConstant::MODULE_3:
  214. // 添加入库物品
  215. $other = self::create_goods($module, $params['stock_goods'], $info_id, $info, $type);
  216. $total_amount = $other['total_amount'];
  217. $data['info_id'] = $info_id;
  218. $data['total_amount'] = $total_amount;
  219. $data['document'] = $params['document'];
  220. $data['images'] = $params['images'];
  221. $data['remark'] = $params['remark'];
  222. break;
  223. case CommonConstant::MODULE_4:
  224. // 添加领用物品
  225. $other = self::create_goods($module, $params['use_goods'], $info_id, $info, $type);
  226. $data['info_id'] = $info_id;
  227. $data['reason'] = $params['reason'];
  228. $data['document'] = $params['document'];
  229. $data['remark'] = $params['remark'];
  230. break;
  231. case CommonConstant::MODULE_5:
  232. // 添加同行人员
  233. $other = self::create_peer_user($module, $params['peer_user'], $info_id, $type);
  234. $data['info_id'] = $info_id;
  235. $data['reason'] = $params['reason'];
  236. $data['start_time'] = $params['start_time'];
  237. $data['end_time'] = $params['end_time'];
  238. $data['document'] = $params['document'];
  239. $data['images'] = $params['images'];
  240. $data['type'] = $params['type'];
  241. $data['is_who'] = $params['is_who'];
  242. $data['remark'] = $params['remark'];
  243. break;
  244. case CommonConstant::MODULE_6:
  245. $data['info_id'] = $info_id;
  246. $data['type'] = $params['type'];
  247. $data['start_time'] = $params['start_time'];
  248. $data['end_time'] = $params['end_time'];
  249. $data['start_am'] = $params['start_am'];
  250. $data['end_am'] = $params['end_am'];
  251. $data['time'] = $params['time'];
  252. $data['reason'] = $params['reason'];
  253. $data['document'] = $params['document'];
  254. $data['images'] = $params['images'];
  255. $data['remark'] = $params['remark'];
  256. break;
  257. case CommonConstant::MODULE_7:
  258. $data['info_id'] = $info_id;
  259. $data['reason'] = $params['reason'];
  260. $data['start_time'] = $params['start_time'];
  261. $data['reach_address'] = $params['reach_address'];
  262. $data['end_time'] = $params['end_time'];
  263. $data['end_address'] = $params['end_address'];
  264. $data['document'] = $params['document'];
  265. $data['images'] = $params['images'];
  266. break;
  267. case CommonConstant::MODULE_8:
  268. $data['info_id'] = $info_id;
  269. $data['type'] = $params['type'];
  270. $data['reason'] = $params['reason'];
  271. $data['desc'] = $params['desc'];
  272. $data['images'] = $params['images'];
  273. break;
  274. case CommonConstant::MODULE_9:
  275. $data['info_id'] = $info_id;
  276. $data['reason'] = $params['reason'];
  277. $data['type'] = $params['type'];
  278. $data['desc'] = $params['desc'];
  279. $data['number'] = $params['number'];
  280. $data['scope'] = $params['scope'];
  281. $data['legal_opinion'] = $params['legal_opinion'];
  282. $data['document'] = $params['document'];
  283. $data['remark'] = $params['remark'];
  284. if ($type != 'create') {
  285. unset($data['reason'], $data['type']);
  286. }
  287. break;
  288. case CommonConstant::MODULE_10:
  289. $data['info_id'] = $info_id;
  290. $data['founder'] = $params['founder'];
  291. $data['desc'] = $params['desc'];
  292. $data['serial_number'] = $params['serial_number'];
  293. $data['reason'] = $params['reason'];
  294. $data['start_time'] = $params['start_time'];
  295. $data['remark'] = $params['remark'];
  296. $data['degree'] = $params['degree'];
  297. $data['document'] = $params['document'];
  298. break;
  299. case CommonConstant::MODULE_11:
  300. $data['info_id'] = $info_id;
  301. $data['department'] = $params['department'];
  302. $data['reason'] = $params['reason'];
  303. $data['department_sign'] = $params['department_sign'];
  304. $data['remark'] = $params['remark'];
  305. $data['document'] = $params['document'];
  306. $data['desc'] = $params['desc'];
  307. $data['start_time'] = $params['start_time'];
  308. $data['serial_number'] = $params['serial_number'];
  309. break;
  310. }
  311. if ($type == 'create') {
  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. } else {
  318. $info->module_info->save($data);
  319. $module_id = $info->module_info->id;
  320. $module_info = $info->module_info;
  321. }
  322. return compact("module_id", "module_info", "other");
  323. }
  324. /**
  325. * 添加申购商品/入库商品/领用商品
  326. *
  327. * @param integer $module 模块类型
  328. * @param array $params 数据
  329. * @param integer $info_id 申请ID
  330. * @param mixed $info 申请信息
  331. * @param string $type 类型:create=申请,update=重新发起,edit:审批人修改
  332. * @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"}]}]}
  333. **/
  334. public static function create_goods($module, $params, $info_id, $info, $type)
  335. {
  336. if (!$params) {
  337. return [];
  338. }
  339. $goods_list_new = [];
  340. $category_first_data = [];
  341. $category_data = [];
  342. $goods_ids = [];
  343. $goods_ids_new = [];
  344. $data = [];
  345. $total_amount = 0;
  346. foreach ($params as $value) {
  347. if ($value['flag'] == '2') {
  348. // 批量导入
  349. $category_first_data[$value['goods_category_first']][$value['goods_category_id']] = $value['goods_category_id'];
  350. }
  351. if ($value['flag'] == '3') {
  352. // 商品库选择
  353. $goods_ids[$value['goods_id']] = $value;
  354. }
  355. if ($module == CommonConstant::MODULE_4 && $value['flag'] == '1') {
  356. // 商品库选择 TODO 这里修复 前端导入领用模块商品后,所返回的商品是过滤后的flag=3;然后,更改操作,对商品进行了规格库存修改,导致了flag=1
  357. $value['flag'] = '3';
  358. $goods_ids[$value['goods_id']] = $value;
  359. }
  360. }
  361. switch ($module) {
  362. case CommonConstant::MODULE_1:
  363. $model = ApproveApplyGoods::class;
  364. break;
  365. case CommonConstant::MODULE_3:
  366. $model = ApproveStockGoods::class;
  367. break;
  368. case CommonConstant::MODULE_4:
  369. $model = ApproveUseGoods::class;
  370. if ($type == 'edit') {
  371. $old_goods_data = [];
  372. $old_goods_freeze_stock_data = []; // 旧数据的冻结库存
  373. foreach ($info['data'] as $key => $val) {
  374. $old_goods_data[$val['goods_id']] = $val;
  375. $goods_stock = $val['goods_stock_text'];
  376. foreach ($goods_stock as $k => $v) {
  377. $old_goods_freeze_stock_data[$v['id']] = [
  378. 'id' => $v['id'],
  379. 'stock' => $v['stock'],
  380. ];
  381. }
  382. }
  383. $goods_list = Goods::field('status,is_deleted,create_at', true)
  384. ->where('id', 'in', array_keys($goods_ids))
  385. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  386. ->with([
  387. 'goodsStock',
  388. ])
  389. ->select();
  390. $goods_object = $goods_list ? array_column($goods_list->toArray(), null, 'id') : [];
  391. $error_data = '';
  392. $apply_goods_data = [];
  393. $add_data = [];
  394. $del_ids = [];
  395. $goodsStockSave = [];
  396. $redisStock = [];
  397. foreach ($goods_ids as $key => $value) {
  398. if (array_key_exists($value['goods_id'], $goods_object)) {
  399. // 商品库里有该商品
  400. $goods_info = $goods_object[$value['goods_id']];
  401. $goods_stock_object = array_column($goods_info['goods_stock'], null, 'id');
  402. $goods_stock_data = [];
  403. $total_price = 0;
  404. foreach ($value['goods_stock'] as $val) {
  405. if (array_key_exists($val['id'], $goods_stock_object)) {
  406. // 商品库里有该商品规格
  407. $goods_stock_info = $goods_stock_object[$val['id']];
  408. $total_stock = $goods_stock_info['stock'];
  409. $stock = isset($val['stock']) && $val['stock'] > 0 ? $val['stock'] : 0;
  410. $price = isset($val['price']) && $val['price'] > 0 ? $val['price'] : 0;
  411. $freeze_stock = array_key_exists($val['id'], $old_goods_freeze_stock_data) ? $old_goods_freeze_stock_data[$val['id']]['stock'] : 0;
  412. if ($stock > ($total_stock + $freeze_stock)) {
  413. // 商品规格库存不足
  414. $error_data .= $value['goods_name'] . '-' . $val['name'] . '的库存不足' . "\n";
  415. }
  416. $total_price = bcadd($total_price, $stock * $price, 2);
  417. $goods_stock_data[] = [
  418. 'id' => $val['id'],
  419. 'goods_id' => $goods_info['id'],
  420. 'name' => $val['name'],
  421. 'price' => $price,
  422. 'stock' => $stock,
  423. ];
  424. if ($freeze_stock > 0) {
  425. if ($stock > $freeze_stock) {
  426. // 扣除剩余库存 增加冻结库存
  427. $save_stock = $stock - $freeze_stock;
  428. $goodsStockSave[] = [
  429. 'id' => $val['id'],
  430. 'stock' => ['dec', $save_stock],
  431. 'freeze_stock' => ['inc', $save_stock],
  432. ];
  433. $redisStock[] = [
  434. 'id' => 'dingtalk_stock_' . $info_id . '_' . $value['goods_id'] . '_' . $val['id'],
  435. 'exp' => 'inc',
  436. 'stock' => $save_stock,
  437. ];
  438. }
  439. if ($stock < $freeze_stock) {
  440. // 增加剩余库存 扣除冻结库存
  441. $save_stock = $freeze_stock - $stock;
  442. $goodsStockSave[] = [
  443. 'id' => $val['id'],
  444. 'stock' => ['inc', $save_stock],
  445. 'freeze_stock' => ['dec', $save_stock],
  446. ];
  447. $redisStock[] = [
  448. 'id' => 'dingtalk_stock_' . $info_id . '_' . $value['goods_id'] . '_' . $val['id'],
  449. 'exp' => 'dec',
  450. 'stock' => $save_stock,
  451. ];
  452. }
  453. }
  454. } else {
  455. // 商品库里没有该商品规格
  456. $error_data .= $value['goods_name'] . '-' . $val['name'] . '规格商品库不存在' . "\n";
  457. }
  458. }
  459. $total_amount = bcadd($total_amount, $total_price, 2);
  460. $goods_data = [
  461. 'info_id' => $info_id,
  462. 'goods_id' => $goods_info['id'],
  463. 'goods_category_first' => $goods_info['goods_category_first'],
  464. 'goods_category_id' => $goods_info['goods_category_id'],
  465. 'goods_no' => $goods_info['goods_no'],
  466. 'goods_name' => $goods_info['goods_name'],
  467. 'goods_brand' => $goods_info['goods_brand'],
  468. 'total_price' => $total_price,
  469. 'goods_stock' => $goods_stock_data,
  470. ];
  471. $data[] = $goods_data;
  472. $goods_data['goods_stock'] = json_encode($goods_stock_data, JSON_UNESCAPED_UNICODE);
  473. if (array_key_exists($value['goods_id'], $old_goods_data)) {
  474. // 修改
  475. $apply_goods_data[$value['goods_id']] = $goods_data;
  476. } else {
  477. // 添加
  478. $add_data[] = $goods_data;
  479. }
  480. } else {
  481. // 商品库里没有该商品
  482. $error_data .= $value['goods_name'] . '商品库不存在' . "\n";
  483. }
  484. }
  485. if ($error_data) {
  486. // 商品库选择的商品 商品库选择的商品的规格 在商品库没有匹配到 返回错误提示
  487. except($error_data);
  488. }
  489. foreach ($info['data'] as $index) {
  490. if (array_key_exists($index['goods_id'], $apply_goods_data)) {
  491. // 更新
  492. $save_data = $apply_goods_data[$index['goods_id']];
  493. $index->save($save_data);
  494. } else {
  495. // 删除
  496. $del_ids[] = $index['id'];
  497. }
  498. }
  499. if ($add_data) {
  500. $model::insertAll($add_data);
  501. }
  502. if ($del_ids) {
  503. $model::where(['id' => ['IN', $del_ids]])->delete();
  504. }
  505. return compact("total_amount", "data", "goodsStockSave", "redisStock");
  506. }
  507. break;
  508. }
  509. if ($type == 'update') {
  510. $model::where('info_id', $info_id)->delete();
  511. }
  512. // 商品库选择
  513. if ($goods_ids) {
  514. $goods_list = Goods::field('status,is_deleted,create_at', true)
  515. ->where('id', 'in', array_keys($goods_ids))
  516. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  517. ->with([
  518. 'goodsStock',
  519. ])
  520. ->select();
  521. $goods_object = $goods_list ? array_column($goods_list->toArray(), null, 'id') : [];
  522. $error_data = '';
  523. $apply_goods_data = [];
  524. foreach ($goods_ids as $key => $value) {
  525. if (array_key_exists($value['goods_id'], $goods_object)) {
  526. // 商品库里有该商品
  527. $goods_info = $goods_object[$value['goods_id']];
  528. $goods_stock_object = array_column($goods_info['goods_stock'], null, 'id');
  529. $goods_stock_data = [];
  530. $total_price = 0;
  531. foreach ($value['goods_stock'] as $val) {
  532. if (array_key_exists($val['id'], $goods_stock_object)) {
  533. // 商品库里有该商品规格
  534. $goods_stock_info = $goods_stock_object[$val['id']];
  535. $total_stock = $goods_stock_info['stock'];
  536. $stock = isset($val['stock']) && $val['stock'] > 0 ? $val['stock'] : 0;
  537. $price = isset($val['price']) && $val['price'] > 0 ? $val['price'] : 0;
  538. if($module == CommonConstant::MODULE_4){
  539. if ($stock > $total_stock) {
  540. // 商品规格库存不足
  541. $error_data .= $value['goods_name'] . '-' . $val['name'] . '的库存不足' . "\n";
  542. }
  543. }
  544. $total_price = bcadd($total_price, $stock * $price, 2);
  545. $goods_stock_data[] = [
  546. 'id' => $val['id'],
  547. 'goods_id' => $goods_info['id'],
  548. 'name' => $val['name'],
  549. 'price' => $price,
  550. 'stock' => $stock,
  551. ];
  552. } else {
  553. // 商品库里没有该商品规格
  554. $error_data .= $value['goods_name'] . '-' . $val['name'] . '规格商品库不存在' . "\n";
  555. }
  556. }
  557. $goods_data = [
  558. 'info_id' => $info_id,
  559. 'goods_id' => $goods_info['id'],
  560. 'goods_category_first' => $goods_info['goods_category_first'],
  561. 'goods_category_id' => $goods_info['goods_category_id'],
  562. 'goods_no' => $goods_info['goods_no'],
  563. 'goods_name' => $goods_info['goods_name'],
  564. 'goods_brand' => $goods_info['goods_brand'],
  565. 'total_price' => $total_price,
  566. 'goods_stock' => $goods_stock_data,
  567. ];
  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. } else {
  573. // 商品库里没有该商品
  574. $error_data .= $value['goods_name'] . '商品库不存在' . "\n";
  575. }
  576. }
  577. if ($error_data) {
  578. // 商品库选择的商品 商品库选择的商品的规格 在商品库没有匹配到 返回错误提示
  579. except($error_data);
  580. }
  581. $model::insertAll($apply_goods_data);
  582. }
  583. // 批量导入
  584. if ($category_first_data) {
  585. $category_first_list = GoodsCategoryService::get_list([['name', 'in', array_keys($category_first_data)]], 1);
  586. $category_first_object = $category_first_list ? array_column($category_first_list->toArray(), null, 'name') : [];
  587. foreach ($category_first_data as $key => $value) {
  588. if (array_key_exists($key, $category_first_object)) {
  589. // 一级里有该商品分类
  590. $category_first_info = $category_first_object[$key];
  591. $goods_category_first_id = $category_first_info['id'];
  592. $category_second_object = $category_first_info['childlist'] ? array_column($category_first_info['childlist'], null, 'name') : [];
  593. } else {
  594. // 一级里没有该商品分类 创建
  595. $goods_category_first = GoodsCategory::create(['name' => $key]);
  596. $goods_category_first_id = $goods_category_first->id;
  597. $category_second_object = [];
  598. }
  599. $childlist = [];
  600. foreach ($value as $kk => $vv) {
  601. if (array_key_exists($kk, $category_second_object)) {
  602. // 二级里有该商品分类
  603. $category_second_info = $category_second_object[$kk];
  604. $goods_category_id = $category_second_info['id'];
  605. } else {
  606. // 二级里没有该商品分类 创建
  607. $goods_category = GoodsCategory::create(['pid' => $goods_category_first_id, 'name' => $kk]);
  608. $goods_category_id = $goods_category->id;
  609. }
  610. $childlist[] = [
  611. 'id' => $goods_category_id,
  612. 'name' => $kk,
  613. ];
  614. }
  615. $category_data[] = [
  616. 'id' => $goods_category_first_id,
  617. 'name' => $key,
  618. 'childlist' => $childlist
  619. ];
  620. }
  621. }
  622. $category_object = $category_data ? array_column($category_data, null, 'name') : [];
  623. foreach ($params as $key => $value) {
  624. if ($value['flag'] == '1') {
  625. // 添加新商品
  626. $goods_ids_new[$key] = $value;
  627. $goods_info = Goods::field('status,is_deleted,create_at', true)
  628. ->where('goods_category_first', $value['goods_category_first'])
  629. ->where('goods_category_id', $value['goods_category_id'])
  630. ->where('goods_name', $value['goods_name'])
  631. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  632. ->with([
  633. 'goodsStock',
  634. ])
  635. ->find();
  636. if ($goods_info) {
  637. $goods_list_new[] = $goods_info->toArray();
  638. }
  639. }
  640. if ($value['flag'] == '2') {
  641. // 批量导入
  642. if (array_key_exists($value['goods_category_first'], $category_object)) {
  643. $category_first_info = $category_object[$value['goods_category_first']];
  644. $goods_category_first_id = $category_first_info['id'];
  645. $category_second_object = array_column($category_first_info['childlist'], null, 'name');
  646. $category_second_info = $category_second_object[$value['goods_category_id']];
  647. $goods_category_id = $category_second_info['id'];
  648. $goods_ids_new[$key] = $value;
  649. $goods_ids_new[$key]['goods_category_first'] = $goods_category_first_id;
  650. $goods_ids_new[$key]['goods_category_id'] = $goods_category_id;
  651. $goods_info = Goods::field('status,is_deleted,create_at', true)
  652. ->where('goods_category_first', $goods_category_first_id)
  653. ->where('goods_category_id', $goods_category_id)
  654. ->where('goods_name', $value['goods_name'])
  655. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  656. ->with([
  657. 'goodsStock',
  658. ])
  659. ->find();
  660. if ($goods_info) {
  661. $goods_list_new[] = $goods_info->toArray();
  662. }
  663. }
  664. }
  665. }
  666. // 添加新商品
  667. if ($goods_ids_new) {
  668. $goods_object = $goods_list_new ? array_column($goods_list_new, null, 'goods_name') : [];
  669. $apply_goods_data = [];
  670. foreach ($goods_ids_new as $value) {
  671. $goods_data = [
  672. 'goods_category_first' => $value['goods_category_first'],
  673. 'goods_category_id' => $value['goods_category_id'],
  674. 'goods_no' => $value['goods_no'],
  675. 'goods_name' => $value['goods_name'],
  676. 'goods_brand' => isset($value['goods_brand']) ? $value['goods_brand'] : '',
  677. ];
  678. if (array_key_exists($value['goods_name'], $goods_object)) {
  679. // 商品库里有该商品
  680. $goods_info = $goods_object[$value['goods_name']];
  681. $goods_id = $goods_info['id'];
  682. $goods_stock_info = array_column($goods_info['goods_stock'], null, 'name');
  683. } else {
  684. // 商品库里没有该商品 创建
  685. $goods = Goods::create($goods_data);
  686. $goods_id = $goods->id;
  687. $goods_stock_info = [];
  688. }
  689. $goods_stock_data = [];
  690. $total_price = 0;
  691. foreach ($value['goods_stock'] as $val) {
  692. if (array_key_exists($val['name'], $goods_stock_info)) {
  693. // 商品库里有该商品规格
  694. $stock_id = $goods_stock_info[$val['name']]['id'];
  695. } else {
  696. // 商品库里没有该商品规格 创建
  697. $stock_data = [
  698. 'goods_id' => $goods_id,
  699. 'name' => $val['name'],
  700. ];
  701. $stock = GoodsStock::create($stock_data);
  702. $stock_id = $stock->id;
  703. }
  704. $stock = isset($val['stock']) && $val['stock'] > 0 ? $val['stock'] : 0;
  705. $price = isset($val['price']) && $val['price'] > 0 ? $val['price'] : 0;
  706. $total_price = bcadd($total_price, $stock * $price, 2);
  707. $goods_stock_data[] = [
  708. 'id' => $stock_id,
  709. 'goods_id' => $goods_id,
  710. 'name' => $val['name'],
  711. 'price' => $price,
  712. 'stock' => $stock,
  713. ];
  714. }
  715. $goods_data['info_id'] = $info_id;
  716. $goods_data['goods_id'] = $goods_id;
  717. $goods_data['total_price'] = $total_price;
  718. $goods_data['goods_stock'] = $goods_stock_data;
  719. $data[] = $goods_data;
  720. $goods_data['goods_stock'] = json_encode($goods_stock_data, JSON_UNESCAPED_UNICODE);
  721. $apply_goods_data[] = $goods_data;
  722. $total_amount = bcadd($total_amount, $total_price, 2);
  723. }
  724. $model::insertAll($apply_goods_data);
  725. }
  726. return compact("total_amount", "data");
  727. }
  728. /**
  729. * 添加同行人员
  730. *
  731. * @param integer $module 模块类型
  732. * @param array $params 同行人员数据
  733. * @param integer $info_id 申请ID
  734. * @param string $type 类型:create=申请,update=重新发起
  735. **/
  736. public static function create_peer_user($module, $params, $info_id, $type)
  737. {
  738. if ($type == 'update') {
  739. ApproveEvectionPeerUser::where('info_id', $info_id)->delete();
  740. }
  741. $data = [];
  742. if (!$params) {
  743. return $data;
  744. }
  745. foreach ($params as $value) {
  746. if (isset($value['name']) && !empty($value['name'])) {
  747. $data[] = [
  748. 'info_id' => $info_id,
  749. 'is_who' => $value['is_who'],
  750. 'user_id' => isset($value['user_id']) ? $value['user_id'] : '',
  751. 'name' => $value['name'],
  752. 'desc' => isset($value['desc']) ? $value['desc'] : '',
  753. ];
  754. }
  755. }
  756. if ($data) {
  757. ApproveEvectionPeerUser::insertAll($data);
  758. }
  759. return $data;
  760. }
  761. /**
  762. * 添加审批人
  763. *
  764. * @param array $approve_user 审批ID
  765. * @param integer $info_id 申请ID
  766. * @param string $type 类型:create=申请,update=重新发起
  767. * @param string $userid 发起人ID
  768. **/
  769. public static function create_approve($approve_user, $info_id, $type, $userid)
  770. {
  771. if ($type == 'update') {
  772. // 审批全部改为历史记录
  773. Approve::where('info_id', $info_id)->where('status', 'in', [CommonConstant::STATUS_3, CommonConstant::STATUS_4])->update(['state' => CommonConstant::IS_WHO_1]);
  774. Approve::where('info_id', $info_id)->where('status', 'in', [CommonConstant::STATUS_1, CommonConstant::STATUS_2])->update(['state' => CommonConstant::IS_WHO_0]);
  775. }
  776. $approve_data = [];
  777. $flow_num = 0;
  778. // 发起人
  779. $apply_data = [
  780. 'info_id' => $info_id,
  781. 'status' => 0,
  782. 'group' => CommonConstant::IS_WHO_1,
  783. 'approve_user' => $userid,
  784. 'approve_flow' => 1,
  785. ];
  786. // 审批人
  787. foreach ($approve_user as $key => $value) {
  788. $flow_num++;
  789. $approve_data[] = [
  790. 'info_id' => $info_id,
  791. 'status' => $flow_num == 1 ? CommonConstant::STATUS_2 : CommonConstant::STATUS_1,
  792. 'group' => CommonConstant::IS_WHO_0,
  793. 'approve_user' => $value,
  794. 'approve_flow' => $flow_num,
  795. ];
  796. }
  797. if ($approve_data) {
  798. // 添加审批人
  799. $approve_data = array_merge([$apply_data], $approve_data);
  800. Approve::insertAll($approve_data);
  801. }
  802. return true;
  803. }
  804. /**
  805. * 添加抄送人
  806. *
  807. * @param array $copy_user 抄送ID
  808. * @param integer $info_id 申请ID
  809. * @param string $type 类型:create=申请,update=重新发起
  810. **/
  811. public static function create_approve_copy($copy_user, $info_id, $type)
  812. {
  813. if ($type == 'update') {
  814. // 删除掉抄送
  815. ApproveCopy::where('info_id', $info_id)->delete();
  816. }
  817. $copy_data = [];
  818. $copy_num = 0;
  819. // 抄送人
  820. foreach ($copy_user as $key => $value) {
  821. $copy_num++;
  822. $copy_data[] = [
  823. 'info_id' => $info_id,
  824. 'approve_user' => $value,
  825. 'approve_flow' => $copy_num,
  826. ];
  827. }
  828. if ($copy_data) {
  829. // 添加抄送人
  830. ApproveCopy::insertAll($copy_data);
  831. }
  832. return true;
  833. }
  834. /**
  835. * 入库商品/领用商品组成数据
  836. *
  837. * @param mixed $goods 商品
  838. * @param integer $module 模块类型
  839. * @param integer $status 状态:2=审批中,3=审批同意,4=审批驳回
  840. **/
  841. public static function composition_data($goods, $module, $status)
  842. {
  843. $goodsStockSave = [];
  844. $goodsStockLog = [];
  845. $redisStock = [];
  846. $type = 0;
  847. foreach ($goods as $key => $val) {
  848. $goods_stock = $status == CommonConstant::STATUS_2 ? $val['goods_stock'] : $val['goods_stock_text'];
  849. foreach ($goods_stock as $k => $v) {
  850. if ($module == CommonConstant::MODULE_3) {
  851. // 增加剩余库存
  852. $goodsStockSave[] = [
  853. 'id' => $v['id'],
  854. 'stock' => ['inc', $v['stock']],
  855. ];
  856. }
  857. if ($module == CommonConstant::MODULE_4) {
  858. if ($status == CommonConstant::STATUS_2) {
  859. // 扣除剩余库存 增加冻结库存
  860. $goodsStockSave[] = [
  861. 'id' => $v['id'],
  862. 'stock' => ['dec', $v['stock']],
  863. 'freeze_stock' => ['inc', $v['stock']],
  864. ];
  865. }
  866. if ($status == CommonConstant::STATUS_3) {
  867. // 扣除冻结库存
  868. $goodsStockSave[] = [
  869. 'id' => $v['id'],
  870. 'freeze_stock' => ['dec', $v['stock']],
  871. ];
  872. $type = 1;
  873. }
  874. if ($status == CommonConstant::STATUS_4) {
  875. // 增加剩余库存 扣除冻结库存
  876. $goodsStockSave[] = [
  877. 'id' => $v['id'],
  878. 'stock' => ['inc', $v['stock']],
  879. 'freeze_stock' => ['dec', $v['stock']],
  880. ];
  881. }
  882. $redisStock[] = [
  883. 'id' => 'dingtalk_stock_' . $val['info_id'] . '_' . $v['goods_id'] . '_' . $v['id'],
  884. 'stock' => $v['stock'],
  885. ];
  886. }
  887. if ($status == CommonConstant::STATUS_3) {
  888. $goodsStockLog[] = [
  889. 'type' => $type,
  890. 'info_id' => $val['info_id'],
  891. 'goods_id' => $v['goods_id'],
  892. 'stock_id' => $v['id'],
  893. 'name' => $v['name'],
  894. 'price' => $v['price'],
  895. 'stock' => $v['stock'],
  896. ];
  897. }
  898. }
  899. }
  900. return [
  901. $goodsStockSave,
  902. $goodsStockLog,
  903. $redisStock
  904. ];
  905. }
  906. /**
  907. * 列表
  908. *
  909. * @param integer $group 类别:list=我的申请记录,form=采购审批单
  910. * @param integer $module 模块类型
  911. * @param integer $status 审批状态
  912. * @param string $search 搜索
  913. * @param string $start_time 申请开始时间
  914. * @param string $end_time 申请结束时间
  915. * @param integer $offset 起始位置
  916. * @param integer $length 查询数量
  917. * @param mixed $user 用户信息
  918. **/
  919. public static function get_list($group, $module, $status, $search, $start_time, $end_time, $offset, $length, $user)
  920. {
  921. if ($group == 'list') {
  922. if (!array_key_exists($module, CommonConstant::get_module_list())) {
  923. return [];
  924. }
  925. if (!array_key_exists($status, CommonConstant::get_approve_status_list())) {
  926. return [];
  927. }
  928. $type = 0;
  929. } else {
  930. $module = CommonConstant::MODULE_1;
  931. $status = CommonConstant::STATUS_3;
  932. $type = ApplyConstant::TYPE_1;
  933. }
  934. $userid = $user['userid'];
  935. $list = ApproveInfo::field('module_id,user_id,apply_user_id,department,is_deleted', true)
  936. ->where('module', $module)
  937. ->where(function ($query) use ($module, $userid) {
  938. if (in_array($module, [CommonConstant::MODULE_5, CommonConstant::MODULE_6, CommonConstant::MODULE_7])) {
  939. $query->where('user_id', $userid)->whereOr('apply_user_id', $userid);
  940. } else {
  941. $query->where('user_id', $userid);
  942. }
  943. })
  944. ->where('status', $status)
  945. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  946. ->when(!empty($search), function ($query) use ($search) {
  947. $query->where('order_no|reason', 'like', '%' . $search . '%');
  948. })
  949. ->when(!empty($start_time) && !empty($end_time), function ($query) use ($start_time, $end_time) {
  950. $query->where('create_at', 'BETWEEN', [$start_time, $end_time]);
  951. })
  952. ->when($type > 0, function ($query) use ($type) {
  953. $query->where('type', $type);
  954. });
  955. $list = self::get_with($list, $module, $status);
  956. $list = $list->order('id desc')
  957. ->limit($offset, $length)
  958. ->select();
  959. return $list;
  960. }
  961. /**
  962. * 详情/信息
  963. *
  964. * @param integer $id 申请ID
  965. * @param mixed $user 用户信息
  966. * @param string $group 类别:0=审批人/后台,1=提交人申请人 审批详情,我的申请详情/信息
  967. * @param string $type 类型:detail=详情,info=信息
  968. **/
  969. public static function get_detail($id, $user, $group, $type)
  970. {
  971. $info = ApproveInfo::field('is_deleted', true)
  972. ->where(function ($query) use ($user, $group) {
  973. if ($group == CommonConstant::IS_WHO_1) {
  974. $query->where('user_id', $user['userid'])->whereOr('apply_user_id', $user['userid']);
  975. }
  976. })
  977. ->where('is_deleted', CommonConstant::IS_DELETED_0);
  978. if ($type == 'detail') {
  979. $info->with([
  980. 'moduleInfo',
  981. 'approve' => function ($query) {
  982. $query->field('is_deleted,create_at', true)
  983. ->where('state', 'in', [CommonConstant::IS_WHO_0, CommonConstant::IS_WHO_1])
  984. ->with([
  985. 'user' => function ($query) {
  986. $query->field('userid,name,avatar,signature,signature_status');
  987. }
  988. ]);
  989. },
  990. 'approveCopy' => function ($query) {
  991. $query->field('is_deleted,create_at', true)
  992. ->with([
  993. 'user' => function ($query) {
  994. $query->field('userid,name,avatar');
  995. }
  996. ]);
  997. }
  998. ]);
  999. $info = $info->find($id);
  1000. if ($info) {
  1001. // 部门列表
  1002. $department_data = UserService::get_user_department_list($info['department']);
  1003. $info['department_data'] = $department_data;
  1004. }
  1005. } else {
  1006. // 信息
  1007. $info->with([
  1008. 'moduleInfo'
  1009. ]);
  1010. $info = $info->find($id);
  1011. }
  1012. if ($info) {
  1013. $module = $info['module'];
  1014. if ($group == CommonConstant::IS_WHO_1) {
  1015. // 先默认当前是提交人
  1016. $create_user = [
  1017. 'id' => $user['id'],
  1018. 'userid' => $user['userid'],
  1019. 'name' => $user['name'],
  1020. 'avatar' => $user['avatar'],
  1021. ];
  1022. $apply_user = $create_user;
  1023. if (in_array($module, [CommonConstant::MODULE_5, CommonConstant::MODULE_6, CommonConstant::MODULE_7])) {
  1024. if ($user['userid'] == $info['user_id']) {
  1025. // 当前是提交人
  1026. if ($info['user_id'] != $info['apply_user_id']) {
  1027. $apply_user_info = User::field('id,userid,name,avatar')
  1028. ->where('userid', $info['apply_user_id'])
  1029. ->find();
  1030. $apply_user = [
  1031. 'id' => $apply_user_info['id'],
  1032. 'userid' => $apply_user_info['userid'],
  1033. 'name' => $apply_user_info['name'],
  1034. 'avatar' => $apply_user_info['avatar'],
  1035. ];
  1036. }
  1037. }
  1038. if ($user['userid'] == $info['apply_user_id']) {
  1039. // 当前是申请人
  1040. if ($info['user_id'] != $info['apply_user_id']) {
  1041. $apply_user_info = User::field('id,userid,name,avatar')
  1042. ->where('userid', $info['user_id'])
  1043. ->find();
  1044. $create_user = [
  1045. 'id' => $apply_user_info['id'],
  1046. 'userid' => $apply_user_info['userid'],
  1047. 'name' => $apply_user_info['name'],
  1048. 'avatar' => $apply_user_info['avatar'],
  1049. ];
  1050. }
  1051. }
  1052. }
  1053. }
  1054. if ($group == CommonConstant::IS_WHO_0) {
  1055. // 审批人
  1056. $apply_user = User::field('id,userid,name,avatar')
  1057. ->where('userid', $info['apply_user_id'])
  1058. ->find();
  1059. $create_user = [
  1060. 'id' => $apply_user['id'],
  1061. 'userid' => $apply_user['userid'],
  1062. 'name' => $apply_user['name'],
  1063. 'avatar' => $apply_user['avatar'],
  1064. ];
  1065. $apply_user = $create_user;
  1066. if (in_array($module, [CommonConstant::MODULE_5, CommonConstant::MODULE_6, CommonConstant::MODULE_7])) {
  1067. if ($info['user_id'] != $info['apply_user_id']) {
  1068. $apply_user_info = User::field('id,userid,name,avatar')
  1069. ->where('userid', $info['user_id'])
  1070. ->find();
  1071. $create_user = [
  1072. 'id' => $apply_user_info['id'],
  1073. 'userid' => $apply_user_info['userid'],
  1074. 'name' => $apply_user_info['name'],
  1075. 'avatar' => $apply_user_info['avatar'],
  1076. ];
  1077. }
  1078. }
  1079. }
  1080. $info['create_user'] = $create_user;
  1081. $info['apply_user'] = $apply_user;
  1082. }
  1083. $info = self::get_item($info, $type);
  1084. return $info;
  1085. }
  1086. /**
  1087. * 列表-关联数据
  1088. *
  1089. * @param mixed $list
  1090. * @param integer $module 模块类型
  1091. * @param integer $status 审批状态
  1092. **/
  1093. public static function get_with($list, $module, $status)
  1094. {
  1095. $field = 'id,info_id,status,approve_user';
  1096. switch ($module) {
  1097. case CommonConstant::MODULE_1:
  1098. // 申领商品列表
  1099. if (in_array($status, [CommonConstant::STATUS_2, CommonConstant::STATUS_4])) {
  1100. // 审批中或审批驳回 才关联 审批人信息
  1101. $list = $list->with([
  1102. 'approveOne' => function ($query) use ($status, $field) {
  1103. $query->field($field)
  1104. ->where('status', $status)
  1105. ->where('group', CommonConstant::IS_WHO_0)
  1106. ->where('state', CommonConstant::IS_WHO_0)
  1107. ->with([
  1108. 'user' => function ($query) {
  1109. $query->field('userid,name');
  1110. }
  1111. ]);
  1112. },
  1113. 'applyGoods' => function ($query) {
  1114. $query->field('create_at', true);
  1115. },
  1116. ]);
  1117. } else {
  1118. $list = $list->with([
  1119. 'applyGoods' => function ($query) {
  1120. $query->field('create_at', true);
  1121. },
  1122. ]);
  1123. }
  1124. break;
  1125. case CommonConstant::MODULE_2:
  1126. if (in_array($status, [CommonConstant::STATUS_2, CommonConstant::STATUS_4])) {
  1127. // 审批中或审批驳回 才关联 审批人信息
  1128. $list = $list->with([
  1129. 'approveOne' => function ($query) use ($status, $field) {
  1130. $query->field($field)
  1131. ->where('status', $status)
  1132. ->where('group', CommonConstant::IS_WHO_0)
  1133. ->where('state', CommonConstant::IS_WHO_0)
  1134. ->with([
  1135. 'user' => function ($query) {
  1136. $query->field('userid,name');
  1137. }
  1138. ]);
  1139. }
  1140. ]);
  1141. }
  1142. break;
  1143. case CommonConstant::MODULE_3:
  1144. // 入库商品列表
  1145. if (in_array($status, [CommonConstant::STATUS_2, CommonConstant::STATUS_4])) {
  1146. // 审批中或审批驳回 才关联 审批人信息
  1147. $list = $list->with([
  1148. 'approveOne' => function ($query) use ($status, $field) {
  1149. $query->field($field)
  1150. ->where('status', $status)
  1151. ->where('group', CommonConstant::IS_WHO_0)
  1152. ->where('state', CommonConstant::IS_WHO_0)
  1153. ->with([
  1154. 'user' => function ($query) {
  1155. $query->field('userid,name');
  1156. }
  1157. ]);
  1158. },
  1159. 'stockGoods' => function ($query) {
  1160. $query->field('id,info_id,goods_name');
  1161. },
  1162. ]);
  1163. } else {
  1164. $list = $list->with([
  1165. 'stockGoods' => function ($query) {
  1166. $query->field('id,info_id,goods_name');
  1167. },
  1168. ]);
  1169. }
  1170. break;
  1171. case CommonConstant::MODULE_4:
  1172. // 领用商品列表
  1173. if (in_array($status, [CommonConstant::STATUS_2, CommonConstant::STATUS_4])) {
  1174. // 审批中或审批驳回 才关联 审批人信息
  1175. $list = $list->with([
  1176. 'approveOne' => function ($query) use ($status, $field) {
  1177. $query->field($field)
  1178. ->where('status', $status)
  1179. ->where('group', CommonConstant::IS_WHO_0)
  1180. ->where('state', CommonConstant::IS_WHO_0)
  1181. ->with([
  1182. 'user' => function ($query) {
  1183. $query->field('userid,name');
  1184. }
  1185. ]);
  1186. },
  1187. 'useGoods' => function ($query) {
  1188. $query->field('id,info_id,goods_name');
  1189. },
  1190. ]);
  1191. } else {
  1192. $list = $list->with([
  1193. 'useGoods' => function ($query) {
  1194. $query->field('id,info_id,goods_name');
  1195. },
  1196. ]);
  1197. }
  1198. break;
  1199. case CommonConstant::MODULE_5:
  1200. // 出差同行人员列表
  1201. if (in_array($status, [CommonConstant::STATUS_2, CommonConstant::STATUS_4])) {
  1202. // 审批中或审批驳回 才关联 审批人信息
  1203. $list = $list->with([
  1204. 'approveOne' => function ($query) use ($status, $field) {
  1205. $query->field($field)
  1206. ->where('status', $status)
  1207. ->where('group', CommonConstant::IS_WHO_0)
  1208. ->where('state', CommonConstant::IS_WHO_0)
  1209. ->with([
  1210. 'user' => function ($query) {
  1211. $query->field('userid,name');
  1212. }
  1213. ]);
  1214. },
  1215. 'peerUser' => function ($query) {
  1216. $query->field('id,info_id,name');
  1217. },
  1218. ]);
  1219. } else {
  1220. $list = $list->with([
  1221. 'peerUser' => function ($query) {
  1222. $query->field('id,info_id,name');
  1223. },
  1224. ]);
  1225. }
  1226. break;
  1227. case CommonConstant::MODULE_6:
  1228. case CommonConstant::MODULE_7:
  1229. case CommonConstant::MODULE_8:
  1230. case CommonConstant::MODULE_9:
  1231. case CommonConstant::MODULE_10:
  1232. case CommonConstant::MODULE_11:
  1233. if (in_array($status, [CommonConstant::STATUS_2, CommonConstant::STATUS_4])) {
  1234. // 审批中或审批驳回 才关联 审批人信息
  1235. $list = $list->with([
  1236. 'approveOne' => function ($query) use ($status, $field) {
  1237. $query->field($field)
  1238. ->where('status', $status)
  1239. ->where('group', CommonConstant::IS_WHO_0)
  1240. ->where('state', CommonConstant::IS_WHO_0)
  1241. ->with([
  1242. 'user' => function ($query) {
  1243. $query->field('userid,name');
  1244. }
  1245. ]);
  1246. }
  1247. ]);
  1248. }
  1249. break;
  1250. }
  1251. return $list;
  1252. }
  1253. /**
  1254. * 详情/信息-关联数据
  1255. *
  1256. * @param mixed $info
  1257. * @param string $type 类型:detail=详情,info=信息,edit=审批人修改
  1258. **/
  1259. public static function get_item($info, $type)
  1260. {
  1261. if ($info) {
  1262. $module = $info['module'];
  1263. switch ($module) {
  1264. case CommonConstant::MODULE_1:
  1265. if ($info['type'] == ApplyConstant::TYPE_1) {
  1266. // $info['apply_goods'] = $info->applyGoods()->select();
  1267. $data = $info->apply_goods;
  1268. $info['data'] = $data;
  1269. }
  1270. $contract = [];
  1271. if ($type == 'detail') {
  1272. // 关联的合同呈批申请
  1273. $contract = ApproveInfo::field('id,reason')
  1274. ->where('module', CommonConstant::MODULE_9)
  1275. ->where('status', CommonConstant::STATUS_3)
  1276. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  1277. ->where('apply_id', $info['id'])
  1278. ->select();
  1279. }
  1280. $info['contract'] = $contract;
  1281. break;
  1282. case CommonConstant::MODULE_3:
  1283. // $info['stock_goods'] = $info->stockGoods()->select();
  1284. $data = $info->stock_goods;
  1285. $info['data'] = $data;
  1286. $apply = null;
  1287. if ($type != 'edit') {
  1288. // 关联的申购申请单
  1289. if ($info['apply_id'] > 0) {
  1290. $apply = ApproveInfo::field('id,order_no')
  1291. ->where('module', CommonConstant::MODULE_1)
  1292. ->where('status', CommonConstant::STATUS_3)
  1293. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  1294. ->find($info['apply_id']);
  1295. }
  1296. }
  1297. $info['apply'] = $apply;
  1298. break;
  1299. case CommonConstant::MODULE_4:
  1300. // $info['use_goods'] = $info->useGoods()->select();
  1301. $data = $info->use_goods;
  1302. $info['data'] = $data;
  1303. break;
  1304. case CommonConstant::MODULE_5:
  1305. $info['peer_user'] = $info->peerUser()->field('id,info_id,name')->select();
  1306. // $info->peer_user;
  1307. $info['data'] = $info['peer_user'];
  1308. break;
  1309. case CommonConstant::MODULE_9:
  1310. $apply = null;
  1311. if ($type != 'edit') {
  1312. // 关联的申购申请单
  1313. if ($info['apply_id'] > 0) {
  1314. $apply = ApproveInfo::field('id,order_no')
  1315. ->where('module', CommonConstant::MODULE_1)
  1316. ->where('status', CommonConstant::STATUS_3)
  1317. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  1318. ->find($info['apply_id']);
  1319. }
  1320. }
  1321. $info['apply'] = $apply;
  1322. break;
  1323. }
  1324. }
  1325. return $info;
  1326. }
  1327. /**
  1328. * 操作方法
  1329. *
  1330. * @param integer $id 申请ID
  1331. * @param array $params 数组
  1332. * @param mixed $user 用户信息
  1333. * @param string $type 类型:urging=催办,cancel=撤销,comment=评分
  1334. */
  1335. public static function make($id, $params, $user, $type)
  1336. {
  1337. $userid = $user['userid'];
  1338. $info = ApproveInfo::field('id,module,user_id,status,reason,maintain_user_id')
  1339. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  1340. ->find($id);
  1341. if (!$info) {
  1342. except('申请记录不存在或已删除');
  1343. }
  1344. if ($info->user_id != $userid) {
  1345. except('您没有权限操作');
  1346. }
  1347. switch ($type) {
  1348. case 'urging':
  1349. if ($info->status != CommonConstant::STATUS_2) {
  1350. except('其他人已操作');
  1351. }
  1352. // TODO 待对接钉钉接口
  1353. break;
  1354. case 'cancel':
  1355. if ($info->status != CommonConstant::STATUS_4) {
  1356. except('非审批驳回状态无法操作');
  1357. }
  1358. $info->status = CommonConstant::STATUS_5;
  1359. $info->save();
  1360. if ($info->module == CommonConstant::MODULE_9) {
  1361. // 9=合同呈批 释放合同编号
  1362. CommonService::set_contract_no($info->reason);
  1363. }
  1364. break;
  1365. case 'comment':
  1366. if ($info->status != CommonConstant::STATUS_3) {
  1367. except('非审批同意状态无法操作');
  1368. }
  1369. if ($info->module != CommonConstant::MODULE_8) {
  1370. // 8=维修申请
  1371. except(CommonConstant::get_module_list()[CommonConstant::MODULE_8] . '记录不存在或已删除');
  1372. }
  1373. // TODO 没有判断评价状态
  1374. Db::startTrans();
  1375. try {
  1376. // 添加评价记录
  1377. $data = $params;
  1378. $data['info_id'] = $id;
  1379. $data['user_id'] = $info->maintain_user_id;
  1380. ApproveMaintainUserLog::create($data);
  1381. // 更新维修信息
  1382. $params['comment_status'] = CommonConstant::IS_WHO_1;
  1383. ApproveMaintain::where('info_id', $id)->update($params);
  1384. Db::commit();
  1385. } catch (Exception $e) {
  1386. Db::rollback();
  1387. except('出现错误:' . $e->getMessage());
  1388. }
  1389. break;
  1390. }
  1391. return true;
  1392. }
  1393. /**
  1394. * 下载文件pdf
  1395. *
  1396. * @param integer $id 申请ID
  1397. **/
  1398. public static function download($id)
  1399. {
  1400. $data = self::get_detail($id, [], CommonConstant::IS_WHO_0, 'detail');
  1401. if (!$data) {
  1402. except('申请记录不存在或已删除');
  1403. }
  1404. // 维修人员
  1405. $data['maintain_user'] = $data['module'] == CommonConstant::MODULE_8 && $data['maintain_user_id'] > 0 ? ApproveMaintainUser::field('id,name')->find($data['maintain_user_id']) :null;
  1406. unset($data['approve'][0]);// 去掉审批流程里面的创建人
  1407. $data['approve_count'] = count($data['approve']) + count($data['approve_copy']);
  1408. $data['subject'] = sysconf('subject');
  1409. $vo = $data->toArray();
  1410. $template = 'index_' . $data['module'];
  1411. $get_module_list = CommonConstant::get_module_list();
  1412. $get_approve_status_list = CommonConstant::get_approve_status_list();
  1413. $get_approve_status_list_admin = CommonConstant::get_approve_status_list_admin();
  1414. $get_is_who_list = CommonConstant::get_is_who_list();
  1415. $get_pay_type_list = ApplyConstant::get_pay_type_list();
  1416. $get_degree_list = OfferConstant::get_degree_list();
  1417. $get_type_list = CommonService::get_type_list($data['module']);
  1418. $title = $get_module_list[$data['module']];
  1419. $name = $data['create_user']['name'] . '提交的' . $title . '审批单';
  1420. $data = compact('vo', 'template', 'get_module_list', 'get_approve_status_list', 'get_approve_status_list_admin', 'get_is_who_list', 'get_pay_type_list', 'get_degree_list', 'get_type_list');
  1421. $html = self::get_html($data);
  1422. $realpath = self::getPath($name);
  1423. $pdf = new \TCPDF();
  1424. $pdf = self::setPdfAttr($pdf);
  1425. self::exportPdf($pdf, $html, $realpath);
  1426. return ['url' => $realpath, 'fullurl' => 'https://' . $_SERVER['HTTP_HOST'] . $realpath];
  1427. }
  1428. // 获取html
  1429. public static function get_html($data)
  1430. {
  1431. // $htmlUrl = app()->getRootPath() . 'public/1.html';
  1432. // $html = file_get_contents($htmlUrl);
  1433. $html = view('common/' . $data['template'], $data)->getContent();
  1434. return $html;
  1435. }
  1436. // 获取路径
  1437. public static function getPath($name)
  1438. {
  1439. //保存文件
  1440. $subPath = date('Ymd');
  1441. $savePath = './upload/pdf/' . $subPath;
  1442. // 检查上传目录
  1443. if (!is_dir($savePath)) {
  1444. // 检查目录是否编码后的
  1445. if (is_dir(base64_decode($savePath))) {
  1446. $savePath = base64_decode($savePath);
  1447. } else {
  1448. // 尝试创建目录
  1449. if (!mkdir($savePath, 0755, true)) {
  1450. except('上传目录' . $savePath . '不存在');
  1451. }
  1452. }
  1453. } else {
  1454. if (!is_writeable($savePath)) {
  1455. except('上传目录' . $savePath . '不可写');
  1456. }
  1457. }
  1458. $filename = date('YmdHis') . uniqid();
  1459. $filename = $name . date('Ymd') . time();
  1460. $suffix = '.pdf';
  1461. $realpath = substr($savePath, 1) . '/' . $filename . $suffix;
  1462. return $realpath;
  1463. }
  1464. // 设置PDF参数
  1465. public static function setPdfAttr($pdf)
  1466. {
  1467. // 设置页面信息
  1468. $pdf->SetTitle("简历文件");
  1469. $pdf->setAuthor('Dya');
  1470. $pdf->setCreator(PDF_CREATOR);
  1471. $pdf->SetSubject('TCPDF Tutorial');
  1472. $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
  1473. //页眉页脚
  1474. $pdf->setPrintHeader(false);
  1475. $pdf->setPrintFooter(false);
  1476. // 自动分页 (第二个参数可以设置距离底部多少距离时分页)
  1477. $pdf->setAutoPageBreak(true, 15);
  1478. // 设置边距(左 上 右 下) 右边距默认左侧值 下边距是bool值(是否覆盖默认页边距)
  1479. $pdf->setMargins(26, 18, 26);
  1480. // 定义默认的单间距字体 (设置为等宽字体)
  1481. $pdf->SetDefaultMonospacedFont('courier');
  1482. // 设置图像比例因子
  1483. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  1484. // 设置默认字体
  1485. $pdf->SetFont('stsongstdlight', '', 10, '', true);
  1486. return $pdf;
  1487. }
  1488. // 生成下载pdf
  1489. public static function exportPdf($pdf, $html, $realpath)
  1490. {
  1491. // 新增页面
  1492. $pdf->AddPage();
  1493. $pdf->writeHTML($html, true, false, true, false, '');
  1494. $pdf->Output($_SERVER['DOCUMENT_ROOT'] . $realpath, 'F');
  1495. }
  1496. }