ApproveInfoService.php 69 KB

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