ApproveInfoService.php 49 KB

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