ApproveInfoService.php 50 KB

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