ApproveInfoService.php 57 KB

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