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 ($stock > $total_stock) {
  539. // 商品规格库存不足
  540. $error_data .= $value['goods_name'] . '-' . $val['name'] . '的库存不足' . "\n";
  541. }
  542. $total_price = bcadd($total_price, $stock * $price, 2);
  543. $goods_stock_data[] = [
  544. 'id' => $val['id'],
  545. 'goods_id' => $goods_info['id'],
  546. 'name' => $val['name'],
  547. 'price' => $price,
  548. 'stock' => $stock,
  549. ];
  550. } else {
  551. // 商品库里没有该商品规格
  552. $error_data .= $value['goods_name'] . '-' . $val['name'] . '规格商品库不存在' . "\n";
  553. }
  554. }
  555. $goods_data = [
  556. 'info_id' => $info_id,
  557. 'goods_id' => $goods_info['id'],
  558. 'goods_category_first' => $goods_info['goods_category_first'],
  559. 'goods_category_id' => $goods_info['goods_category_id'],
  560. 'goods_no' => $goods_info['goods_no'],
  561. 'goods_name' => $goods_info['goods_name'],
  562. 'goods_brand' => $goods_info['goods_brand'],
  563. 'total_price' => $total_price,
  564. 'goods_stock' => $goods_stock_data,
  565. ];
  566. $data[] = $goods_data;
  567. $goods_data['goods_stock'] = json_encode($goods_stock_data, JSON_UNESCAPED_UNICODE);
  568. $apply_goods_data[] = $goods_data;
  569. $total_amount = bcadd($total_amount, $total_price, 2);
  570. } else {
  571. // 商品库里没有该商品
  572. $error_data .= $value['goods_name'] . '商品库不存在' . "\n";
  573. }
  574. }
  575. if ($error_data) {
  576. // 商品库选择的商品 商品库选择的商品的规格 在商品库没有匹配到 返回错误提示
  577. except($error_data);
  578. }
  579. $model::insertAll($apply_goods_data);
  580. }
  581. // 批量导入
  582. if ($category_first_data) {
  583. $category_first_list = GoodsCategoryService::get_list([['name', 'in', array_keys($category_first_data)]], 1);
  584. $category_first_object = $category_first_list ? array_column($category_first_list->toArray(), null, 'name') : [];
  585. foreach ($category_first_data as $key => $value) {
  586. if (array_key_exists($key, $category_first_object)) {
  587. // 一级里有该商品分类
  588. $category_first_info = $category_first_object[$key];
  589. $goods_category_first_id = $category_first_info['id'];
  590. $category_second_object = $category_first_info['childlist'] ? array_column($category_first_info['childlist'], null, 'name') : [];
  591. } else {
  592. // 一级里没有该商品分类 创建
  593. $goods_category_first = GoodsCategory::create(['name' => $key]);
  594. $goods_category_first_id = $goods_category_first->id;
  595. $category_second_object = [];
  596. }
  597. $childlist = [];
  598. foreach ($value as $kk => $vv) {
  599. if (array_key_exists($kk, $category_second_object)) {
  600. // 二级里有该商品分类
  601. $category_second_info = $category_second_object[$kk];
  602. $goods_category_id = $category_second_info['id'];
  603. } else {
  604. // 二级里没有该商品分类 创建
  605. $goods_category = GoodsCategory::create(['pid' => $goods_category_first_id, 'name' => $kk]);
  606. $goods_category_id = $goods_category->id;
  607. }
  608. $childlist[] = [
  609. 'id' => $goods_category_id,
  610. 'name' => $kk,
  611. ];
  612. }
  613. $category_data[] = [
  614. 'id' => $goods_category_first_id,
  615. 'name' => $key,
  616. 'childlist' => $childlist
  617. ];
  618. }
  619. }
  620. $category_object = $category_data ? array_column($category_data, null, 'name') : [];
  621. foreach ($params as $key => $value) {
  622. if ($value['flag'] == '1') {
  623. // 添加新商品
  624. $goods_ids_new[$key] = $value;
  625. $goods_info = Goods::field('status,is_deleted,create_at', true)
  626. ->where('goods_category_first', $value['goods_category_first'])
  627. ->where('goods_category_id', $value['goods_category_id'])
  628. ->where('goods_name', $value['goods_name'])
  629. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  630. ->with([
  631. 'goodsStock',
  632. ])
  633. ->find();
  634. if ($goods_info) {
  635. $goods_list_new[] = $goods_info->toArray();
  636. }
  637. }
  638. if ($value['flag'] == '2') {
  639. // 批量导入
  640. if (array_key_exists($value['goods_category_first'], $category_object)) {
  641. $category_first_info = $category_object[$value['goods_category_first']];
  642. $goods_category_first_id = $category_first_info['id'];
  643. $category_second_object = array_column($category_first_info['childlist'], null, 'name');
  644. $category_second_info = $category_second_object[$value['goods_category_id']];
  645. $goods_category_id = $category_second_info['id'];
  646. $goods_ids_new[$key] = $value;
  647. $goods_ids_new[$key]['goods_category_first'] = $goods_category_first_id;
  648. $goods_ids_new[$key]['goods_category_id'] = $goods_category_id;
  649. $goods_info = Goods::field('status,is_deleted,create_at', true)
  650. ->where('goods_category_first', $goods_category_first_id)
  651. ->where('goods_category_id', $goods_category_id)
  652. ->where('goods_name', $value['goods_name'])
  653. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  654. ->with([
  655. 'goodsStock',
  656. ])
  657. ->find();
  658. if ($goods_info) {
  659. $goods_list_new[] = $goods_info->toArray();
  660. }
  661. }
  662. }
  663. }
  664. // 添加新商品
  665. if ($goods_ids_new) {
  666. $goods_object = $goods_list_new ? array_column($goods_list_new, null, 'goods_name') : [];
  667. $apply_goods_data = [];
  668. foreach ($goods_ids_new as $value) {
  669. $goods_data = [
  670. 'goods_category_first' => $value['goods_category_first'],
  671. 'goods_category_id' => $value['goods_category_id'],
  672. 'goods_no' => $value['goods_no'],
  673. 'goods_name' => $value['goods_name'],
  674. 'goods_brand' => isset($value['goods_brand']) ? $value['goods_brand'] : '',
  675. ];
  676. if (array_key_exists($value['goods_name'], $goods_object)) {
  677. // 商品库里有该商品
  678. $goods_info = $goods_object[$value['goods_name']];
  679. $goods_id = $goods_info['id'];
  680. $goods_stock_info = array_column($goods_info['goods_stock'], null, 'name');
  681. } else {
  682. // 商品库里没有该商品 创建
  683. $goods = Goods::create($goods_data);
  684. $goods_id = $goods->id;
  685. $goods_stock_info = [];
  686. }
  687. $goods_stock_data = [];
  688. $total_price = 0;
  689. foreach ($value['goods_stock'] as $val) {
  690. if (array_key_exists($val['name'], $goods_stock_info)) {
  691. // 商品库里有该商品规格
  692. $stock_id = $goods_stock_info[$val['name']]['id'];
  693. } else {
  694. // 商品库里没有该商品规格 创建
  695. $stock_data = [
  696. 'goods_id' => $goods_id,
  697. 'name' => $val['name'],
  698. ];
  699. $stock = GoodsStock::create($stock_data);
  700. $stock_id = $stock->id;
  701. }
  702. $stock = isset($val['stock']) && $val['stock'] > 0 ? $val['stock'] : 0;
  703. $price = isset($val['price']) && $val['price'] > 0 ? $val['price'] : 0;
  704. $total_price = bcadd($total_price, $stock * $price, 2);
  705. $goods_stock_data[] = [
  706. 'id' => $stock_id,
  707. 'goods_id' => $goods_id,
  708. 'name' => $val['name'],
  709. 'price' => $price,
  710. 'stock' => $stock,
  711. ];
  712. }
  713. $goods_data['info_id'] = $info_id;
  714. $goods_data['goods_id'] = $goods_id;
  715. $goods_data['total_price'] = $total_price;
  716. $goods_data['goods_stock'] = $goods_stock_data;
  717. $data[] = $goods_data;
  718. $goods_data['goods_stock'] = json_encode($goods_stock_data, JSON_UNESCAPED_UNICODE);
  719. $apply_goods_data[] = $goods_data;
  720. $total_amount = bcadd($total_amount, $total_price, 2);
  721. }
  722. $model::insertAll($apply_goods_data);
  723. }
  724. return compact("total_amount", "data");
  725. }
  726. /**
  727. * 添加同行人员
  728. *
  729. * @param integer $module 模块类型
  730. * @param array $params 同行人员数据
  731. * @param integer $info_id 申请ID
  732. * @param string $type 类型:create=申请,update=重新发起
  733. **/
  734. public static function create_peer_user($module, $params, $info_id, $type)
  735. {
  736. if ($type == 'update') {
  737. ApproveEvectionPeerUser::where('info_id', $info_id)->delete();
  738. }
  739. $data = [];
  740. if (!$params) {
  741. return $data;
  742. }
  743. foreach ($params as $value) {
  744. if (isset($value['name']) && !empty($value['name'])) {
  745. $data[] = [
  746. 'info_id' => $info_id,
  747. 'is_who' => $value['is_who'],
  748. 'user_id' => isset($value['user_id']) ? $value['user_id'] : '',
  749. 'name' => $value['name'],
  750. 'desc' => isset($value['desc']) ? $value['desc'] : '',
  751. ];
  752. }
  753. }
  754. if ($data) {
  755. ApproveEvectionPeerUser::insertAll($data);
  756. }
  757. return $data;
  758. }
  759. /**
  760. * 添加审批人
  761. *
  762. * @param array $approve_user 审批ID
  763. * @param integer $info_id 申请ID
  764. * @param string $type 类型:create=申请,update=重新发起
  765. * @param string $userid 发起人ID
  766. **/
  767. public static function create_approve($approve_user, $info_id, $type, $userid)
  768. {
  769. if ($type == 'update') {
  770. // 审批全部改为历史记录
  771. Approve::where('info_id', $info_id)->where('status', 'in', [CommonConstant::STATUS_3, CommonConstant::STATUS_4])->update(['state' => CommonConstant::IS_WHO_1]);
  772. Approve::where('info_id', $info_id)->where('status', 'in', [CommonConstant::STATUS_1, CommonConstant::STATUS_2])->update(['state' => CommonConstant::IS_WHO_0]);
  773. }
  774. $approve_data = [];
  775. $flow_num = 0;
  776. // 发起人
  777. $apply_data = [
  778. 'info_id' => $info_id,
  779. 'status' => 0,
  780. 'group' => CommonConstant::IS_WHO_1,
  781. 'approve_user' => $userid,
  782. 'approve_flow' => 1,
  783. ];
  784. // 审批人
  785. foreach ($approve_user as $key => $value) {
  786. $flow_num++;
  787. $approve_data[] = [
  788. 'info_id' => $info_id,
  789. 'status' => $flow_num == 1 ? CommonConstant::STATUS_2 : CommonConstant::STATUS_1,
  790. 'group' => CommonConstant::IS_WHO_0,
  791. 'approve_user' => $value,
  792. 'approve_flow' => $flow_num,
  793. ];
  794. }
  795. if ($approve_data) {
  796. // 添加审批人
  797. $approve_data = array_merge([$apply_data], $approve_data);
  798. Approve::insertAll($approve_data);
  799. }
  800. return true;
  801. }
  802. /**
  803. * 添加抄送人
  804. *
  805. * @param array $copy_user 抄送ID
  806. * @param integer $info_id 申请ID
  807. * @param string $type 类型:create=申请,update=重新发起
  808. **/
  809. public static function create_approve_copy($copy_user, $info_id, $type)
  810. {
  811. if ($type == 'update') {
  812. // 删除掉抄送
  813. ApproveCopy::where('info_id', $info_id)->delete();
  814. }
  815. $copy_data = [];
  816. $copy_num = 0;
  817. // 抄送人
  818. foreach ($copy_user as $key => $value) {
  819. $copy_num++;
  820. $copy_data[] = [
  821. 'info_id' => $info_id,
  822. 'approve_user' => $value,
  823. 'approve_flow' => $copy_num,
  824. ];
  825. }
  826. if ($copy_data) {
  827. // 添加抄送人
  828. ApproveCopy::insertAll($copy_data);
  829. }
  830. return true;
  831. }
  832. /**
  833. * 入库商品/领用商品组成数据
  834. *
  835. * @param mixed $goods 商品
  836. * @param integer $module 模块类型
  837. * @param integer $status 状态:2=审批中,3=审批同意,4=审批驳回
  838. **/
  839. public static function composition_data($goods, $module, $status)
  840. {
  841. $goodsStockSave = [];
  842. $goodsStockLog = [];
  843. $redisStock = [];
  844. $type = 0;
  845. foreach ($goods as $key => $val) {
  846. $goods_stock = $status == CommonConstant::STATUS_2 ? $val['goods_stock'] : $val['goods_stock_text'];
  847. foreach ($goods_stock as $k => $v) {
  848. if ($module == CommonConstant::MODULE_3) {
  849. // 增加剩余库存
  850. $goodsStockSave[] = [
  851. 'id' => $v['id'],
  852. 'stock' => ['inc', $v['stock']],
  853. ];
  854. }
  855. if ($module == CommonConstant::MODULE_4) {
  856. if ($status == CommonConstant::STATUS_2) {
  857. // 扣除剩余库存 增加冻结库存
  858. $goodsStockSave[] = [
  859. 'id' => $v['id'],
  860. 'stock' => ['dec', $v['stock']],
  861. 'freeze_stock' => ['inc', $v['stock']],
  862. ];
  863. }
  864. if ($status == CommonConstant::STATUS_3) {
  865. // 扣除冻结库存
  866. $goodsStockSave[] = [
  867. 'id' => $v['id'],
  868. 'freeze_stock' => ['dec', $v['stock']],
  869. ];
  870. $type = 1;
  871. }
  872. if ($status == CommonConstant::STATUS_4) {
  873. // 增加剩余库存 扣除冻结库存
  874. $goodsStockSave[] = [
  875. 'id' => $v['id'],
  876. 'stock' => ['inc', $v['stock']],
  877. 'freeze_stock' => ['dec', $v['stock']],
  878. ];
  879. }
  880. $redisStock[] = [
  881. 'id' => 'dingtalk_stock_' . $val['info_id'] . '_' . $v['goods_id'] . '_' . $v['id'],
  882. 'stock' => $v['stock'],
  883. ];
  884. }
  885. if ($status == CommonConstant::STATUS_3) {
  886. $goodsStockLog[] = [
  887. 'type' => $type,
  888. 'info_id' => $val['info_id'],
  889. 'goods_id' => $v['goods_id'],
  890. 'stock_id' => $v['id'],
  891. 'name' => $v['name'],
  892. 'price' => $v['price'],
  893. 'stock' => $v['stock'],
  894. ];
  895. }
  896. }
  897. }
  898. return [
  899. $goodsStockSave,
  900. $goodsStockLog,
  901. $redisStock
  902. ];
  903. }
  904. /**
  905. * 列表
  906. *
  907. * @param integer $group 类别:list=我的申请记录,form=采购审批单
  908. * @param integer $module 模块类型
  909. * @param integer $status 审批状态
  910. * @param string $search 搜索
  911. * @param string $start_time 申请开始时间
  912. * @param string $end_time 申请结束时间
  913. * @param integer $offset 起始位置
  914. * @param integer $length 查询数量
  915. * @param mixed $user 用户信息
  916. **/
  917. public static function get_list($group, $module, $status, $search, $start_time, $end_time, $offset, $length, $user)
  918. {
  919. if ($group == 'list') {
  920. if (!array_key_exists($module, CommonConstant::get_module_list())) {
  921. return [];
  922. }
  923. if (!array_key_exists($status, CommonConstant::get_approve_status_list())) {
  924. return [];
  925. }
  926. $type = 0;
  927. } else {
  928. $module = CommonConstant::MODULE_1;
  929. $status = CommonConstant::STATUS_3;
  930. $type = ApplyConstant::TYPE_1;
  931. }
  932. $userid = $user['userid'];
  933. $list = ApproveInfo::field('module_id,user_id,apply_user_id,department,is_deleted', true)
  934. ->where('module', $module)
  935. ->where(function ($query) use ($module, $userid) {
  936. if (in_array($module, [CommonConstant::MODULE_5, CommonConstant::MODULE_6, CommonConstant::MODULE_7])) {
  937. $query->where('user_id', $userid)->whereOr('apply_user_id', $userid);
  938. } else {
  939. $query->where('user_id', $userid);
  940. }
  941. })
  942. ->where('status', $status)
  943. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  944. ->when(!empty($search), function ($query) use ($search) {
  945. $query->where('order_no|reason', 'like', '%' . $search . '%');
  946. })
  947. ->when(!empty($start_time) && !empty($end_time), function ($query) use ($start_time, $end_time) {
  948. $query->where('create_at', 'BETWEEN', [$start_time, $end_time]);
  949. })
  950. ->when($type > 0, function ($query) use ($type) {
  951. $query->where('type', $type);
  952. });
  953. $list = self::get_with($list, $module, $status);
  954. $list = $list->order('id desc')
  955. ->limit($offset, $length)
  956. ->select();
  957. return $list;
  958. }
  959. /**
  960. * 详情/信息
  961. *
  962. * @param integer $id 申请ID
  963. * @param mixed $user 用户信息
  964. * @param string $group 类别:0=审批人/后台,1=提交人申请人 审批详情,我的申请详情/信息
  965. * @param string $type 类型:detail=详情,info=信息
  966. **/
  967. public static function get_detail($id, $user, $group, $type)
  968. {
  969. $info = ApproveInfo::field('is_deleted', true)
  970. ->where(function ($query) use ($user, $group) {
  971. if ($group == CommonConstant::IS_WHO_1) {
  972. $query->where('user_id', $user['userid'])->whereOr('apply_user_id', $user['userid']);
  973. }
  974. })
  975. ->where('is_deleted', CommonConstant::IS_DELETED_0);
  976. if ($type == 'detail') {
  977. $info->with([
  978. 'moduleInfo',
  979. 'approve' => function ($query) {
  980. $query->field('is_deleted,create_at', true)
  981. ->where('state', 'in', [CommonConstant::IS_WHO_0, CommonConstant::IS_WHO_1])
  982. ->with([
  983. 'user' => function ($query) {
  984. $query->field('userid,name,avatar,signature,signature_status');
  985. }
  986. ]);
  987. },
  988. 'approveCopy' => function ($query) {
  989. $query->field('is_deleted,create_at', true)
  990. ->with([
  991. 'user' => function ($query) {
  992. $query->field('userid,name,avatar');
  993. }
  994. ]);
  995. }
  996. ]);
  997. $info = $info->find($id);
  998. if ($info) {
  999. // 部门列表
  1000. $department_data = UserService::get_user_department_list($info['department']);
  1001. $info['department_data'] = $department_data;
  1002. }
  1003. } else {
  1004. // 信息
  1005. $info->with([
  1006. 'moduleInfo'
  1007. ]);
  1008. $info = $info->find($id);
  1009. }
  1010. if ($info) {
  1011. $module = $info['module'];
  1012. if ($group == CommonConstant::IS_WHO_1) {
  1013. // 先默认当前是提交人
  1014. $create_user = [
  1015. 'id' => $user['id'],
  1016. 'userid' => $user['userid'],
  1017. 'name' => $user['name'],
  1018. 'avatar' => $user['avatar'],
  1019. ];
  1020. $apply_user = $create_user;
  1021. if (in_array($module, [CommonConstant::MODULE_5, CommonConstant::MODULE_6, CommonConstant::MODULE_7])) {
  1022. if ($user['userid'] == $info['user_id']) {
  1023. // 当前是提交人
  1024. if ($info['user_id'] != $info['apply_user_id']) {
  1025. $apply_user_info = User::field('id,userid,name,avatar')
  1026. ->where('userid', $info['apply_user_id'])
  1027. ->find();
  1028. $apply_user = [
  1029. 'id' => $apply_user_info['id'],
  1030. 'userid' => $apply_user_info['userid'],
  1031. 'name' => $apply_user_info['name'],
  1032. 'avatar' => $apply_user_info['avatar'],
  1033. ];
  1034. }
  1035. }
  1036. if ($user['userid'] == $info['apply_user_id']) {
  1037. // 当前是申请人
  1038. if ($info['user_id'] != $info['apply_user_id']) {
  1039. $apply_user_info = User::field('id,userid,name,avatar')
  1040. ->where('userid', $info['user_id'])
  1041. ->find();
  1042. $create_user = [
  1043. 'id' => $apply_user_info['id'],
  1044. 'userid' => $apply_user_info['userid'],
  1045. 'name' => $apply_user_info['name'],
  1046. 'avatar' => $apply_user_info['avatar'],
  1047. ];
  1048. }
  1049. }
  1050. }
  1051. }
  1052. if ($group == CommonConstant::IS_WHO_0) {
  1053. // 审批人
  1054. $apply_user = User::field('id,userid,name,avatar')
  1055. ->where('userid', $info['apply_user_id'])
  1056. ->find();
  1057. $create_user = [
  1058. 'id' => $apply_user['id'],
  1059. 'userid' => $apply_user['userid'],
  1060. 'name' => $apply_user['name'],
  1061. 'avatar' => $apply_user['avatar'],
  1062. ];
  1063. $apply_user = $create_user;
  1064. if (in_array($module, [CommonConstant::MODULE_5, CommonConstant::MODULE_6, CommonConstant::MODULE_7])) {
  1065. if ($info['user_id'] != $info['apply_user_id']) {
  1066. $apply_user_info = User::field('id,userid,name,avatar')
  1067. ->where('userid', $info['user_id'])
  1068. ->find();
  1069. $create_user = [
  1070. 'id' => $apply_user_info['id'],
  1071. 'userid' => $apply_user_info['userid'],
  1072. 'name' => $apply_user_info['name'],
  1073. 'avatar' => $apply_user_info['avatar'],
  1074. ];
  1075. }
  1076. }
  1077. }
  1078. $info['create_user'] = $create_user;
  1079. $info['apply_user'] = $apply_user;
  1080. }
  1081. $info = self::get_item($info, $type);
  1082. return $info;
  1083. }
  1084. /**
  1085. * 列表-关联数据
  1086. *
  1087. * @param mixed $list
  1088. * @param integer $module 模块类型
  1089. * @param integer $status 审批状态
  1090. **/
  1091. public static function get_with($list, $module, $status)
  1092. {
  1093. $field = 'id,info_id,status,approve_user';
  1094. switch ($module) {
  1095. case CommonConstant::MODULE_1:
  1096. // 申领商品列表
  1097. if (in_array($status, [CommonConstant::STATUS_2, CommonConstant::STATUS_4])) {
  1098. // 审批中或审批驳回 才关联 审批人信息
  1099. $list = $list->with([
  1100. 'approveOne' => function ($query) use ($status, $field) {
  1101. $query->field($field)
  1102. ->where('status', $status)
  1103. ->where('group', CommonConstant::IS_WHO_0)
  1104. ->where('state', CommonConstant::IS_WHO_0)
  1105. ->with([
  1106. 'user' => function ($query) {
  1107. $query->field('userid,name');
  1108. }
  1109. ]);
  1110. },
  1111. 'applyGoods' => function ($query) {
  1112. $query->field('create_at', true);
  1113. },
  1114. ]);
  1115. } else {
  1116. $list = $list->with([
  1117. 'applyGoods' => function ($query) {
  1118. $query->field('create_at', true);
  1119. },
  1120. ]);
  1121. }
  1122. break;
  1123. case CommonConstant::MODULE_2:
  1124. if (in_array($status, [CommonConstant::STATUS_2, CommonConstant::STATUS_4])) {
  1125. // 审批中或审批驳回 才关联 审批人信息
  1126. $list = $list->with([
  1127. 'approveOne' => function ($query) use ($status, $field) {
  1128. $query->field($field)
  1129. ->where('status', $status)
  1130. ->where('group', CommonConstant::IS_WHO_0)
  1131. ->where('state', CommonConstant::IS_WHO_0)
  1132. ->with([
  1133. 'user' => function ($query) {
  1134. $query->field('userid,name');
  1135. }
  1136. ]);
  1137. }
  1138. ]);
  1139. }
  1140. break;
  1141. case CommonConstant::MODULE_3:
  1142. // 入库商品列表
  1143. if (in_array($status, [CommonConstant::STATUS_2, CommonConstant::STATUS_4])) {
  1144. // 审批中或审批驳回 才关联 审批人信息
  1145. $list = $list->with([
  1146. 'approveOne' => function ($query) use ($status, $field) {
  1147. $query->field($field)
  1148. ->where('status', $status)
  1149. ->where('group', CommonConstant::IS_WHO_0)
  1150. ->where('state', CommonConstant::IS_WHO_0)
  1151. ->with([
  1152. 'user' => function ($query) {
  1153. $query->field('userid,name');
  1154. }
  1155. ]);
  1156. },
  1157. 'stockGoods' => function ($query) {
  1158. $query->field('id,info_id,goods_name');
  1159. },
  1160. ]);
  1161. } else {
  1162. $list = $list->with([
  1163. 'stockGoods' => function ($query) {
  1164. $query->field('id,info_id,goods_name');
  1165. },
  1166. ]);
  1167. }
  1168. break;
  1169. case CommonConstant::MODULE_4:
  1170. // 领用商品列表
  1171. if (in_array($status, [CommonConstant::STATUS_2, CommonConstant::STATUS_4])) {
  1172. // 审批中或审批驳回 才关联 审批人信息
  1173. $list = $list->with([
  1174. 'approveOne' => function ($query) use ($status, $field) {
  1175. $query->field($field)
  1176. ->where('status', $status)
  1177. ->where('group', CommonConstant::IS_WHO_0)
  1178. ->where('state', CommonConstant::IS_WHO_0)
  1179. ->with([
  1180. 'user' => function ($query) {
  1181. $query->field('userid,name');
  1182. }
  1183. ]);
  1184. },
  1185. 'useGoods' => function ($query) {
  1186. $query->field('id,info_id,goods_name');
  1187. },
  1188. ]);
  1189. } else {
  1190. $list = $list->with([
  1191. 'useGoods' => function ($query) {
  1192. $query->field('id,info_id,goods_name');
  1193. },
  1194. ]);
  1195. }
  1196. break;
  1197. case CommonConstant::MODULE_5:
  1198. // 出差同行人员列表
  1199. if (in_array($status, [CommonConstant::STATUS_2, CommonConstant::STATUS_4])) {
  1200. // 审批中或审批驳回 才关联 审批人信息
  1201. $list = $list->with([
  1202. 'approveOne' => function ($query) use ($status, $field) {
  1203. $query->field($field)
  1204. ->where('status', $status)
  1205. ->where('group', CommonConstant::IS_WHO_0)
  1206. ->where('state', CommonConstant::IS_WHO_0)
  1207. ->with([
  1208. 'user' => function ($query) {
  1209. $query->field('userid,name');
  1210. }
  1211. ]);
  1212. },
  1213. 'peerUser' => function ($query) {
  1214. $query->field('id,info_id,name');
  1215. },
  1216. ]);
  1217. } else {
  1218. $list = $list->with([
  1219. 'peerUser' => function ($query) {
  1220. $query->field('id,info_id,name');
  1221. },
  1222. ]);
  1223. }
  1224. break;
  1225. case CommonConstant::MODULE_6:
  1226. case CommonConstant::MODULE_7:
  1227. case CommonConstant::MODULE_8:
  1228. case CommonConstant::MODULE_9:
  1229. case CommonConstant::MODULE_10:
  1230. case CommonConstant::MODULE_11:
  1231. if (in_array($status, [CommonConstant::STATUS_2, CommonConstant::STATUS_4])) {
  1232. // 审批中或审批驳回 才关联 审批人信息
  1233. $list = $list->with([
  1234. 'approveOne' => function ($query) use ($status, $field) {
  1235. $query->field($field)
  1236. ->where('status', $status)
  1237. ->where('group', CommonConstant::IS_WHO_0)
  1238. ->where('state', CommonConstant::IS_WHO_0)
  1239. ->with([
  1240. 'user' => function ($query) {
  1241. $query->field('userid,name');
  1242. }
  1243. ]);
  1244. }
  1245. ]);
  1246. }
  1247. break;
  1248. }
  1249. return $list;
  1250. }
  1251. /**
  1252. * 详情/信息-关联数据
  1253. *
  1254. * @param mixed $info
  1255. * @param string $type 类型:detail=详情,info=信息,edit=审批人修改
  1256. **/
  1257. public static function get_item($info, $type)
  1258. {
  1259. if ($info) {
  1260. $module = $info['module'];
  1261. switch ($module) {
  1262. case CommonConstant::MODULE_1:
  1263. if ($info['type'] == ApplyConstant::TYPE_1) {
  1264. // $info['apply_goods'] = $info->applyGoods()->select();
  1265. $data = $info->apply_goods;
  1266. $info['data'] = $data;
  1267. }
  1268. $contract = [];
  1269. if ($type == 'detail') {
  1270. // 关联的合同呈批申请
  1271. $contract = ApproveInfo::field('id,reason')
  1272. ->where('module', CommonConstant::MODULE_9)
  1273. ->where('status', CommonConstant::STATUS_3)
  1274. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  1275. ->where('apply_id', $info['id'])
  1276. ->select();
  1277. }
  1278. $info['contract'] = $contract;
  1279. break;
  1280. case CommonConstant::MODULE_3:
  1281. // $info['stock_goods'] = $info->stockGoods()->select();
  1282. $data = $info->stock_goods;
  1283. $info['data'] = $data;
  1284. $apply = null;
  1285. if ($type != 'edit') {
  1286. // 关联的申购申请单
  1287. if ($info['apply_id'] > 0) {
  1288. $apply = ApproveInfo::field('id,order_no')
  1289. ->where('module', CommonConstant::MODULE_1)
  1290. ->where('status', CommonConstant::STATUS_3)
  1291. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  1292. ->find($info['apply_id']);
  1293. }
  1294. }
  1295. $info['apply'] = $apply;
  1296. break;
  1297. case CommonConstant::MODULE_4:
  1298. // $info['use_goods'] = $info->useGoods()->select();
  1299. $data = $info->use_goods;
  1300. $info['data'] = $data;
  1301. break;
  1302. case CommonConstant::MODULE_5:
  1303. $info['peer_user'] = $info->peerUser()->field('id,info_id,name')->select();
  1304. // $info->peer_user;
  1305. $info['data'] = $info['peer_user'];
  1306. break;
  1307. case CommonConstant::MODULE_9:
  1308. $apply = null;
  1309. if ($type != 'edit') {
  1310. // 关联的申购申请单
  1311. if ($info['apply_id'] > 0) {
  1312. $apply = ApproveInfo::field('id,order_no')
  1313. ->where('module', CommonConstant::MODULE_1)
  1314. ->where('status', CommonConstant::STATUS_3)
  1315. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  1316. ->find($info['apply_id']);
  1317. }
  1318. }
  1319. $info['apply'] = $apply;
  1320. break;
  1321. }
  1322. }
  1323. return $info;
  1324. }
  1325. /**
  1326. * 操作方法
  1327. *
  1328. * @param integer $id 申请ID
  1329. * @param array $params 数组
  1330. * @param mixed $user 用户信息
  1331. * @param string $type 类型:urging=催办,cancel=撤销,comment=评分
  1332. */
  1333. public static function make($id, $params, $user, $type)
  1334. {
  1335. $userid = $user['userid'];
  1336. $info = ApproveInfo::field('id,module,user_id,status,reason,maintain_user_id')
  1337. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  1338. ->find($id);
  1339. if (!$info) {
  1340. except('申请记录不存在或已删除');
  1341. }
  1342. if ($info->user_id != $userid) {
  1343. except('您没有权限操作');
  1344. }
  1345. switch ($type) {
  1346. case 'urging':
  1347. if ($info->status != CommonConstant::STATUS_2) {
  1348. except('其他人已操作');
  1349. }
  1350. // TODO 待对接钉钉接口
  1351. break;
  1352. case 'cancel':
  1353. if ($info->status != CommonConstant::STATUS_4) {
  1354. except('非审批驳回状态无法操作');
  1355. }
  1356. $info->status = CommonConstant::STATUS_5;
  1357. $info->save();
  1358. if ($info->module == CommonConstant::MODULE_9) {
  1359. // 9=合同呈批 释放合同编号
  1360. CommonService::set_contract_no($info->reason);
  1361. }
  1362. break;
  1363. case 'comment':
  1364. if ($info->status != CommonConstant::STATUS_3) {
  1365. except('非审批同意状态无法操作');
  1366. }
  1367. if ($info->module != CommonConstant::MODULE_8) {
  1368. // 8=维修申请
  1369. except(CommonConstant::get_module_list()[CommonConstant::MODULE_8] . '记录不存在或已删除');
  1370. }
  1371. // TODO 没有判断评价状态
  1372. Db::startTrans();
  1373. try {
  1374. // 添加评价记录
  1375. $data = $params;
  1376. $data['info_id'] = $id;
  1377. $data['user_id'] = $info->maintain_user_id;
  1378. ApproveMaintainUserLog::create($data);
  1379. // 更新维修信息
  1380. $params['comment_status'] = CommonConstant::IS_WHO_1;
  1381. ApproveMaintain::where('info_id', $id)->update($params);
  1382. Db::commit();
  1383. } catch (Exception $e) {
  1384. Db::rollback();
  1385. except('出现错误:' . $e->getMessage());
  1386. }
  1387. break;
  1388. }
  1389. return true;
  1390. }
  1391. /**
  1392. * 下载文件pdf
  1393. *
  1394. * @param integer $id 申请ID
  1395. **/
  1396. public static function download($id)
  1397. {
  1398. $data = self::get_detail($id, [], CommonConstant::IS_WHO_0, 'detail');
  1399. if (!$data) {
  1400. except('申请记录不存在或已删除');
  1401. }
  1402. // 维修人员
  1403. $data['maintain_user'] = $data['module'] == CommonConstant::MODULE_8 && $data['maintain_user_id'] > 0 ? ApproveMaintainUser::field('id,name')->find($data['maintain_user_id']) :null;
  1404. unset($data['approve'][0]);// 去掉审批流程里面的创建人
  1405. $data['approve_count'] = count($data['approve']) + count($data['approve_copy']);
  1406. $data['subject'] = sysconf('subject');
  1407. $vo = $data->toArray();
  1408. $template = 'index_' . $data['module'];
  1409. $get_module_list = CommonConstant::get_module_list();
  1410. $get_approve_status_list = CommonConstant::get_approve_status_list();
  1411. $get_approve_status_list_admin = CommonConstant::get_approve_status_list_admin();
  1412. $get_is_who_list = CommonConstant::get_is_who_list();
  1413. $get_pay_type_list = ApplyConstant::get_pay_type_list();
  1414. $get_degree_list = OfferConstant::get_degree_list();
  1415. $get_type_list = CommonService::get_type_list($data['module']);
  1416. $title = $get_module_list[$data['module']];
  1417. $name = $data['create_user']['name'] . '提交的' . $title . '审批单';
  1418. $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');
  1419. $html = self::get_html($data);
  1420. $realpath = self::getPath($name);
  1421. $pdf = new \TCPDF();
  1422. $pdf = self::setPdfAttr($pdf);
  1423. self::exportPdf($pdf, $html, $realpath);
  1424. return ['url' => $realpath, 'fullurl' => 'https://' . $_SERVER['HTTP_HOST'] . $realpath];
  1425. }
  1426. // 获取html
  1427. public static function get_html($data)
  1428. {
  1429. // $htmlUrl = app()->getRootPath() . 'public/1.html';
  1430. // $html = file_get_contents($htmlUrl);
  1431. $html = view('common/' . $data['template'], $data)->getContent();
  1432. return $html;
  1433. }
  1434. // 获取路径
  1435. public static function getPath($name)
  1436. {
  1437. //保存文件
  1438. $subPath = date('Ymd');
  1439. $savePath = './upload/pdf/' . $subPath;
  1440. // 检查上传目录
  1441. if (!is_dir($savePath)) {
  1442. // 检查目录是否编码后的
  1443. if (is_dir(base64_decode($savePath))) {
  1444. $savePath = base64_decode($savePath);
  1445. } else {
  1446. // 尝试创建目录
  1447. if (!mkdir($savePath, 0755, true)) {
  1448. except('上传目录' . $savePath . '不存在');
  1449. return false;
  1450. }
  1451. }
  1452. } else {
  1453. if (!is_writeable($savePath)) {
  1454. except('上传目录' . $savePath . '不可写');
  1455. return false;
  1456. }
  1457. }
  1458. $filename = date('Ymd') . sha1(date('YmdHis', time()) . 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. }