1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576 |
- <?php
- namespace app\common\service;
- use app\common\constant\ApplyConstant;
- use app\common\constant\CommonConstant;
- use app\common\constant\OfferConstant;
- use app\common\model\Approve;
- use app\common\model\ApproveCopy;
- use app\common\model\ApproveApplyGoods;
- use app\common\model\ApproveMaintain;
- use app\common\model\ApproveMaintainUser;
- use app\common\model\ApproveMaintainUserLog;
- use app\common\model\ApproveStockGoods;
- use app\common\model\ApproveEvectionPeerUser;
- use app\common\model\ApproveInfo;
- use app\common\model\ApproveUseGoods;
- use app\common\model\Goods;
- use app\common\model\GoodsCategory;
- use app\common\model\GoodsStock;
- use app\common\model\User;
- use think\cache\driver\Redis;
- use think\Db;
- use think\Exception;
- /**
- * 审批申请服务类
- */
- class ApproveInfoService
- {
- /**
- * 申请/重新发起
- *
- * @param integer $id 申请ID
- * @param integer $module 模块类型
- * @param array $params
- * @param mixed $user 用户信息
- * @param string $way 方式:create=申请,update=重新发起
- **/
- public static function create($id, $module, $params, $user, $way)
- {
- $userid = $user['userid'];
- $apply_user_id = $userid;
- $department = $user['department'];
- // 5=出差申请,6=请假申请,7=用车申请 申请人信息 提交人和申请人不是同一人
- if (in_array($module, [CommonConstant::MODULE_5, CommonConstant::MODULE_6, CommonConstant::MODULE_7])) {
- if ($userid != $params['apply_user_id']) {
- $apply_user = User::field('userid,department')
- ->where('userid', $params['apply_user_id'])
- ->find();
- if (!$apply_user) {
- except('申请人信息不存在或已删除');
- }
- $apply_user_id = $apply_user['userid'];
- }
- }
- // 重新发起
- if ($id > 0) {
- $info = ApproveInfo::field('is_deleted', true)
- ->where('module', $module)
- ->where('user_id', $userid)
- ->where('is_deleted', CommonConstant::IS_DELETED_0)
- ->with([
- 'moduleInfo'
- ])
- ->find($id);
- if (!$info) {
- except('申请记录不存在或已删除');
- }
- if (!$info->module_info) {
- except(CommonConstant::get_module_list()[$module] . '记录不存在或已删除');
- }
- if ($info->status != CommonConstant::STATUS_4) {
- except('非审批驳回状态无法操作');
- }
- } else {
- // 3=入库申请,9=合同呈批申请
- if (in_array($module, [CommonConstant::MODULE_3, CommonConstant::MODULE_9])) {
- // 存在申购申请单 判断申购申请单
- if ($params['apply_id'] > 0) {
- $apply = ApproveInfo::field('id')
- ->where('module', CommonConstant::MODULE_1)
- ->where('status', CommonConstant::STATUS_3)
- ->where('is_deleted', CommonConstant::IS_DELETED_0)
- ->find($params['apply_id']);
- if (!$apply) {
- except('申购申请单不存在或已删除');
- }
- }
- }
- }
- $order_no = get_order_sn($user['id']);
- $approve_user = $params['approve_user'] ? explode(',', $params['approve_user']) : [];
- $copy_user = $params['copy_user'] ? explode(',', $params['copy_user']) : [];
- $approve_num = $approve_user ? count($approve_user) : 0;
- $data = [
- 'module' => $module,
- 'user_id' => $userid,
- 'apply_user_id' => $apply_user_id,
- 'department' => $department,
- 'status' => CommonConstant::STATUS_2,
- 'approve_num' => $approve_num,
- 'cur_num' => 0,
- 'order_no' => $order_no,
- 'apply_date' => date("Y-m-d"),
- 'reason' => $params['reason'],
- 'type' => $params['type'],
- 'desc' => $params['desc'],
- 'start_time' => $module == CommonConstant::MODULE_6 ? $params['start_time'] . ' ' . $params['start_am'] : $params['start_time'],
- 'end_time' => $module == CommonConstant::MODULE_6 ? $params['end_time'] . ' ' . $params['end_am'] : $params['end_time'],
- 'apply_id' => $params['apply_id'],
- 'maintain_user_id' => 0,
- ];
- Db::startTrans();
- try {
- if ($id > 0) {
- // 编辑对应模块
- $result = self::create_module($module, $params, $id, $info, 'update');
- // 编辑审批人
- self::create_approve($approve_user, $id, 'update', $userid);
- // 编辑抄送人
- self::create_approve_copy($copy_user, $id, 'update');
- // 编辑审批申请
- if ($module == CommonConstant::MODULE_9) {
- // 9=合同呈批 不编辑合同编号
- unset($data['reason'], $data['type']);
- }
- unset($data['order_no']);
- $data['create_at'] = date('Y-m-d H:i:s');
- $info->save($data);
- } else {
- // 添加审批申请
- $info = ApproveInfo::create($data);
- $info_id = $info->id;
- // 添加对应模块
- $result = self::create_module($module, $params, $info_id, [], 'create');
- $info->module_id = $result['module_id'];
- $info->save();
- // 添加审批人
- self::create_approve($approve_user, $info_id, 'create', $userid);
- // 添加抄送人
- self::create_approve_copy($copy_user, $info_id, 'create');
- }
- user_log('approve', json_encode($result, JSON_UNESCAPED_UNICODE));
- Db::commit();
- } catch (Exception $e) {
- Db::rollback();
- // except('出现错误:' . $e->getMessage() . '|' . $e->getFile() . '|' . $e->getLine());
- except('出现错误:' . $e->getMessage());
- }
- // 4=领用申请 增加冻结库存
- if ($module == CommonConstant::MODULE_4) {
- list($goodsStockSave, $goodsStockLog, $redisStock) = self::composition_data($result['other']['data'], CommonConstant::MODULE_4, CommonConstant::STATUS_2);
- if ($goodsStockSave) {
- // 批量更新商品库存
- (new GoodsStock)->saveAll($goodsStockSave);
- }
- if ($redisStock) {
- // 库存缓存记录
- $redis = new Redis(config('redis.'));
- foreach ($redisStock as $key => $value) {
- $redis->inc($value['id'], $value['stock']);
- }
- }
- }
- // 9=合同呈批申请 提交 生成合同编号
- if ($module == CommonConstant::MODULE_9 && $way == CommonConstant::create) {
- $contract_no = CommonService::get_contract_no($params['type']);
- $info->save(['reason' => $contract_no]);
- $models = CommonConstant::get_module_model_list();
- $model = $models[$module];
- $model::where('id', $result['module_id'])->update(['reason' => $contract_no]);
- }
- return true;
- }
- /**
- * 添加对应模块
- *
- * @param integer $module 模块类型
- * @param array $params
- * @param integer $info_id 申请ID
- * @param mixed $info 申请信息
- * @param string $type 类型:create=申请,update=重新发起,edit:审批人修改 只有module=4才传edit
- * @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"}]}]}}
- **/
- public static function create_module($module, $params, $info_id, $info, $type)
- {
- $other = [];
- switch ($module) {
- case CommonConstant::MODULE_1:
- // 添加申购物品
- $total_amount = 0;
- if ($params['type'] == ApplyConstant::TYPE_1) {
- $other = self::create_goods($module, $params['apply_goods'], $info_id, $info, $type);
- $total_amount = $other['total_amount'];
- }
- $data['info_id'] = $info_id;
- $data['reason'] = $params['reason'];
- $data['type'] = $params['type'];
- $data['total_amount'] = $params['total_amount']; // $total_amount
- $data['start_time'] = $params['start_time'];
- $data['document'] = $params['document'];
- $data['images'] = $params['images'];
- $data['pay_type'] = $params['pay_type'];
- break;
- case CommonConstant::MODULE_2:
- $data['info_id'] = $info_id;
- $data['type'] = $params['type'];
- $data['word_size'] = $params['word_size'];
- $data['desc'] = $params['desc'];
- $data['number'] = $params['number'];
- $data['reason'] = $params['reason'];
- $data['remark'] = $params['remark'];
- $data['document'] = $params['document'];
- break;
- case CommonConstant::MODULE_3:
- // 添加入库物品
- $other = self::create_goods($module, $params['stock_goods'], $info_id, $info, $type);
- $total_amount = $other['total_amount'];
- $data['info_id'] = $info_id;
- $data['total_amount'] = $total_amount;
- $data['document'] = $params['document'];
- $data['images'] = $params['images'];
- $data['remark'] = $params['remark'];
- break;
- case CommonConstant::MODULE_4:
- // 添加领用物品
- $other = self::create_goods($module, $params['use_goods'], $info_id, $info, $type);
- $data['info_id'] = $info_id;
- $data['reason'] = $params['reason'];
- $data['document'] = $params['document'];
- $data['remark'] = $params['remark'];
- break;
- case CommonConstant::MODULE_5:
- // 添加同行人员
- $other = self::create_peer_user($module, $params['peer_user'], $info_id, $type);
- $data['info_id'] = $info_id;
- $data['reason'] = $params['reason'];
- $data['start_time'] = $params['start_time'];
- $data['end_time'] = $params['end_time'];
- $data['document'] = $params['document'];
- $data['images'] = $params['images'];
- $data['type'] = $params['type'];
- $data['is_who'] = $params['is_who'];
- $data['remark'] = $params['remark'];
- break;
- case CommonConstant::MODULE_6:
- $data['info_id'] = $info_id;
- $data['type'] = $params['type'];
- $data['start_time'] = $params['start_time'];
- $data['end_time'] = $params['end_time'];
- $data['start_am'] = $params['start_am'];
- $data['end_am'] = $params['end_am'];
- $data['time'] = $params['time'];
- $data['reason'] = $params['reason'];
- $data['document'] = $params['document'];
- $data['images'] = $params['images'];
- $data['remark'] = $params['remark'];
- break;
- case CommonConstant::MODULE_7:
- $data['info_id'] = $info_id;
- $data['reason'] = $params['reason'];
- $data['start_time'] = $params['start_time'];
- $data['reach_address'] = $params['reach_address'];
- $data['end_time'] = $params['end_time'];
- $data['end_address'] = $params['end_address'];
- $data['document'] = $params['document'];
- $data['images'] = $params['images'];
- break;
- case CommonConstant::MODULE_8:
- $data['info_id'] = $info_id;
- $data['type'] = $params['type'];
- $data['reason'] = $params['reason'];
- $data['desc'] = $params['desc'];
- $data['images'] = $params['images'];
- break;
- case CommonConstant::MODULE_9:
- $data['info_id'] = $info_id;
- $data['reason'] = $params['reason'];
- $data['type'] = $params['type'];
- $data['desc'] = $params['desc'];
- $data['number'] = $params['number'];
- $data['scope'] = $params['scope'];
- $data['legal_opinion'] = $params['legal_opinion'];
- $data['document'] = $params['document'];
- $data['remark'] = $params['remark'];
- if ($type != 'create') {
- unset($data['reason'], $data['type']);
- }
- break;
- case CommonConstant::MODULE_10:
- $data['info_id'] = $info_id;
- $data['founder'] = $params['founder'];
- $data['desc'] = $params['desc'];
- $data['serial_number'] = $params['serial_number'];
- $data['reason'] = $params['reason'];
- $data['start_time'] = $params['start_time'];
- $data['remark'] = $params['remark'];
- $data['degree'] = $params['degree'];
- $data['document'] = $params['document'];
- break;
- case CommonConstant::MODULE_11:
- $data['info_id'] = $info_id;
- $data['department'] = $params['department'];
- $data['reason'] = $params['reason'];
- $data['department_sign'] = $params['department_sign'];
- $data['remark'] = $params['remark'];
- $data['document'] = $params['document'];
- $data['desc'] = $params['desc'];
- $data['start_time'] = $params['start_time'];
- $data['serial_number'] = $params['serial_number'];
- break;
- }
- if ($type == 'create') {
- $models = CommonConstant::get_module_model_list();
- $model = $models[$module];
- $module_result = $model::create($data);
- $module_id = $module_result->id;
- $module_info = $data;
- } else {
- $info->module_info->save($data);
- $module_id = $info->module_info->id;
- $module_info = $info->module_info;
- }
- return compact("module_id", "module_info", "other");
- }
- /**
- * 添加申购商品/入库商品/领用商品
- *
- * @param integer $module 模块类型
- * @param array $params 数据
- * @param integer $info_id 申请ID
- * @param mixed $info 申请信息
- * @param string $type 类型:create=申请,update=重新发起,edit:审批人修改
- * @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"}]}]}
- **/
- public static function create_goods($module, $params, $info_id, $info, $type)
- {
- if (!$params) {
- return [];
- }
- $goods_list_new = [];
- $category_first_data = [];
- $category_data = [];
- $goods_ids = [];
- $goods_ids_new = [];
- $data = [];
- $total_amount = 0;
- foreach ($params as $value) {
- if ($value['flag'] == '2') {
- // 批量导入
- $category_first_data[$value['goods_category_first']][$value['goods_category_id']] = $value['goods_category_id'];
- }
- if ($value['flag'] == '3') {
- // 商品库选择
- $goods_ids[$value['goods_id']] = $value;
- }
- if ($module == CommonConstant::MODULE_4 && $value['flag'] == '1') {
- // 商品库选择 TODO 这里修复 前端导入领用模块商品后,所返回的商品是过滤后的flag=3;然后,更改操作,对商品进行了规格库存修改,导致了flag=1
- $value['flag'] = '3';
- $goods_ids[$value['goods_id']] = $value;
- }
- }
- switch ($module) {
- case CommonConstant::MODULE_1:
- $model = ApproveApplyGoods::class;
- break;
- case CommonConstant::MODULE_3:
- $model = ApproveStockGoods::class;
- break;
- case CommonConstant::MODULE_4:
- $model = ApproveUseGoods::class;
- if ($type == 'edit') {
- $old_goods_data = [];
- $old_goods_freeze_stock_data = []; // 旧数据的冻结库存
- foreach ($info['data'] as $key => $val) {
- $old_goods_data[$val['goods_id']] = $val;
- $goods_stock = $val['goods_stock_text'];
- foreach ($goods_stock as $k => $v) {
- $old_goods_freeze_stock_data[$v['id']] = [
- 'id' => $v['id'],
- 'stock' => $v['stock'],
- ];
- }
- }
- $goods_list = Goods::field('status,is_deleted,create_at', true)
- ->where('id', 'in', array_keys($goods_ids))
- ->where('is_deleted', CommonConstant::IS_DELETED_0)
- ->with([
- 'goodsStock',
- ])
- ->select();
- $goods_object = $goods_list ? array_column($goods_list->toArray(), null, 'id') : [];
- $error_data = '';
- $apply_goods_data = [];
- $add_data = [];
- $del_ids = [];
- $goodsStockSave = [];
- $redisStock = [];
- foreach ($goods_ids as $key => $value) {
- if (array_key_exists($value['goods_id'], $goods_object)) {
- // 商品库里有该商品
- $goods_info = $goods_object[$value['goods_id']];
- $goods_stock_object = array_column($goods_info['goods_stock'], null, 'id');
- $goods_stock_data = [];
- $total_price = 0;
- foreach ($value['goods_stock'] as $val) {
- if (array_key_exists($val['id'], $goods_stock_object)) {
- // 商品库里有该商品规格
- $goods_stock_info = $goods_stock_object[$val['id']];
- $total_stock = $goods_stock_info['stock'];
- $stock = isset($val['stock']) && $val['stock'] > 0 ? $val['stock'] : 0;
- $price = isset($val['price']) && $val['price'] > 0 ? $val['price'] : 0;
- $freeze_stock = array_key_exists($val['id'], $old_goods_freeze_stock_data) ? $old_goods_freeze_stock_data[$val['id']]['stock'] : 0;
- if ($stock > ($total_stock + $freeze_stock)) {
- // 商品规格库存不足
- $error_data .= $value['goods_name'] . '-' . $val['name'] . '的库存不足' . "\n";
- }
- $total_price = bcadd($total_price, $stock * $price, 2);
- $goods_stock_data[] = [
- 'id' => $val['id'],
- 'goods_id' => $goods_info['id'],
- 'name' => $val['name'],
- 'price' => $price,
- 'stock' => $stock,
- ];
- if ($freeze_stock > 0) {
- if ($stock > $freeze_stock) {
- // 扣除剩余库存 增加冻结库存
- $save_stock = $stock - $freeze_stock;
- $goodsStockSave[] = [
- 'id' => $val['id'],
- 'stock' => ['dec', $save_stock],
- 'freeze_stock' => ['inc', $save_stock],
- ];
- $redisStock[] = [
- 'id' => 'dingtalk_stock_' . $info_id . '_' . $value['goods_id'] . '_' . $val['id'],
- 'exp' => 'inc',
- 'stock' => $save_stock,
- ];
- }
- if ($stock < $freeze_stock) {
- // 增加剩余库存 扣除冻结库存
- $save_stock = $freeze_stock - $stock;
- $goodsStockSave[] = [
- 'id' => $val['id'],
- 'stock' => ['inc', $save_stock],
- 'freeze_stock' => ['dec', $save_stock],
- ];
- $redisStock[] = [
- 'id' => 'dingtalk_stock_' . $info_id . '_' . $value['goods_id'] . '_' . $val['id'],
- 'exp' => 'dec',
- 'stock' => $save_stock,
- ];
- }
- }
- } else {
- // 商品库里没有该商品规格
- $error_data .= $value['goods_name'] . '-' . $val['name'] . '规格商品库不存在' . "\n";
- }
- }
- $total_amount = bcadd($total_amount, $total_price, 2);
- $goods_data = [
- 'info_id' => $info_id,
- 'goods_id' => $goods_info['id'],
- 'goods_category_first' => $goods_info['goods_category_first'],
- 'goods_category_id' => $goods_info['goods_category_id'],
- 'goods_no' => $goods_info['goods_no'],
- 'goods_name' => $goods_info['goods_name'],
- 'goods_brand' => $goods_info['goods_brand'],
- 'total_price' => $total_price,
- 'goods_stock' => $goods_stock_data,
- ];
- $data[] = $goods_data;
- $goods_data['goods_stock'] = json_encode($goods_stock_data, JSON_UNESCAPED_UNICODE);
- if (array_key_exists($value['goods_id'], $old_goods_data)) {
- // 修改
- $apply_goods_data[$value['goods_id']] = $goods_data;
- } else {
- // 添加
- $add_data[] = $goods_data;
- }
- } else {
- // 商品库里没有该商品
- $error_data .= $value['goods_name'] . '商品库不存在' . "\n";
- }
- }
- if ($error_data) {
- // 商品库选择的商品 商品库选择的商品的规格 在商品库没有匹配到 返回错误提示
- except($error_data);
- }
- foreach ($info['data'] as $index) {
- if (array_key_exists($index['goods_id'], $apply_goods_data)) {
- // 更新
- $save_data = $apply_goods_data[$index['goods_id']];
- $index->save($save_data);
- } else {
- // 删除
- $del_ids[] = $index['id'];
- }
- }
- if ($add_data) {
- $model::insertAll($add_data);
- }
- if ($del_ids) {
- $model::where(['id' => ['IN', $del_ids]])->delete();
- }
- return compact("total_amount", "data", "goodsStockSave", "redisStock");
- }
- break;
- }
- if ($type == 'update') {
- $model::where('info_id', $info_id)->delete();
- }
- // 商品库选择
- if ($goods_ids) {
- $goods_list = Goods::field('status,is_deleted,create_at', true)
- ->where('id', 'in', array_keys($goods_ids))
- ->where('is_deleted', CommonConstant::IS_DELETED_0)
- ->with([
- 'goodsStock',
- ])
- ->select();
- $goods_object = $goods_list ? array_column($goods_list->toArray(), null, 'id') : [];
- $error_data = '';
- $apply_goods_data = [];
- foreach ($goods_ids as $key => $value) {
- if (array_key_exists($value['goods_id'], $goods_object)) {
- // 商品库里有该商品
- $goods_info = $goods_object[$value['goods_id']];
- $goods_stock_object = array_column($goods_info['goods_stock'], null, 'id');
- $goods_stock_data = [];
- $total_price = 0;
- foreach ($value['goods_stock'] as $val) {
- if (array_key_exists($val['id'], $goods_stock_object)) {
- // 商品库里有该商品规格
- $goods_stock_info = $goods_stock_object[$val['id']];
- $total_stock = $goods_stock_info['stock'];
- $stock = isset($val['stock']) && $val['stock'] > 0 ? $val['stock'] : 0;
- $price = isset($val['price']) && $val['price'] > 0 ? $val['price'] : 0;
- if ($stock > $total_stock) {
- // 商品规格库存不足
- $error_data .= $value['goods_name'] . '-' . $val['name'] . '的库存不足' . "\n";
- }
- $total_price = bcadd($total_price, $stock * $price, 2);
- $goods_stock_data[] = [
- 'id' => $val['id'],
- 'goods_id' => $goods_info['id'],
- 'name' => $val['name'],
- 'price' => $price,
- 'stock' => $stock,
- ];
- } else {
- // 商品库里没有该商品规格
- $error_data .= $value['goods_name'] . '-' . $val['name'] . '规格商品库不存在' . "\n";
- }
- }
- $goods_data = [
- 'info_id' => $info_id,
- 'goods_id' => $goods_info['id'],
- 'goods_category_first' => $goods_info['goods_category_first'],
- 'goods_category_id' => $goods_info['goods_category_id'],
- 'goods_no' => $goods_info['goods_no'],
- 'goods_name' => $goods_info['goods_name'],
- 'goods_brand' => $goods_info['goods_brand'],
- 'total_price' => $total_price,
- 'goods_stock' => $goods_stock_data,
- ];
- $data[] = $goods_data;
- $goods_data['goods_stock'] = json_encode($goods_stock_data, JSON_UNESCAPED_UNICODE);
- $apply_goods_data[] = $goods_data;
- $total_amount = bcadd($total_amount, $total_price, 2);
- } else {
- // 商品库里没有该商品
- $error_data .= $value['goods_name'] . '商品库不存在' . "\n";
- }
- }
- if ($error_data) {
- // 商品库选择的商品 商品库选择的商品的规格 在商品库没有匹配到 返回错误提示
- except($error_data);
- }
- $model::insertAll($apply_goods_data);
- }
- // 批量导入
- if ($category_first_data) {
- $category_first_list = GoodsCategoryService::get_list([['name', 'in', array_keys($category_first_data)]], 1);
- $category_first_object = $category_first_list ? array_column($category_first_list->toArray(), null, 'name') : [];
- foreach ($category_first_data as $key => $value) {
- if (array_key_exists($key, $category_first_object)) {
- // 一级里有该商品分类
- $category_first_info = $category_first_object[$key];
- $goods_category_first_id = $category_first_info['id'];
- $category_second_object = $category_first_info['childlist'] ? array_column($category_first_info['childlist'], null, 'name') : [];
- } else {
- // 一级里没有该商品分类 创建
- $goods_category_first = GoodsCategory::create(['name' => $key]);
- $goods_category_first_id = $goods_category_first->id;
- $category_second_object = [];
- }
- $childlist = [];
- foreach ($value as $kk => $vv) {
- if (array_key_exists($kk, $category_second_object)) {
- // 二级里有该商品分类
- $category_second_info = $category_second_object[$kk];
- $goods_category_id = $category_second_info['id'];
- } else {
- // 二级里没有该商品分类 创建
- $goods_category = GoodsCategory::create(['pid' => $goods_category_first_id, 'name' => $kk]);
- $goods_category_id = $goods_category->id;
- }
- $childlist[] = [
- 'id' => $goods_category_id,
- 'name' => $kk,
- ];
- }
- $category_data[] = [
- 'id' => $goods_category_first_id,
- 'name' => $key,
- 'childlist' => $childlist
- ];
- }
- }
- $category_object = $category_data ? array_column($category_data, null, 'name') : [];
- foreach ($params as $key => $value) {
- if ($value['flag'] == '1') {
- // 添加新商品
- $goods_ids_new[$key] = $value;
- $goods_info = Goods::field('status,is_deleted,create_at', true)
- ->where('goods_category_first', $value['goods_category_first'])
- ->where('goods_category_id', $value['goods_category_id'])
- ->where('goods_name', $value['goods_name'])
- ->where('is_deleted', CommonConstant::IS_DELETED_0)
- ->with([
- 'goodsStock',
- ])
- ->find();
- if ($goods_info) {
- $goods_list_new[] = $goods_info->toArray();
- }
- }
- if ($value['flag'] == '2') {
- // 批量导入
- if (array_key_exists($value['goods_category_first'], $category_object)) {
- $category_first_info = $category_object[$value['goods_category_first']];
- $goods_category_first_id = $category_first_info['id'];
- $category_second_object = array_column($category_first_info['childlist'], null, 'name');
- $category_second_info = $category_second_object[$value['goods_category_id']];
- $goods_category_id = $category_second_info['id'];
- $goods_ids_new[$key] = $value;
- $goods_ids_new[$key]['goods_category_first'] = $goods_category_first_id;
- $goods_ids_new[$key]['goods_category_id'] = $goods_category_id;
- $goods_info = Goods::field('status,is_deleted,create_at', true)
- ->where('goods_category_first', $goods_category_first_id)
- ->where('goods_category_id', $goods_category_id)
- ->where('goods_name', $value['goods_name'])
- ->where('is_deleted', CommonConstant::IS_DELETED_0)
- ->with([
- 'goodsStock',
- ])
- ->find();
- if ($goods_info) {
- $goods_list_new[] = $goods_info->toArray();
- }
- }
- }
- }
- // 添加新商品
- if ($goods_ids_new) {
- $goods_object = $goods_list_new ? array_column($goods_list_new, null, 'goods_name') : [];
- $apply_goods_data = [];
- foreach ($goods_ids_new as $value) {
- $goods_data = [
- 'goods_category_first' => $value['goods_category_first'],
- 'goods_category_id' => $value['goods_category_id'],
- 'goods_no' => $value['goods_no'],
- 'goods_name' => $value['goods_name'],
- 'goods_brand' => isset($value['goods_brand']) ? $value['goods_brand'] : '',
- ];
- if (array_key_exists($value['goods_name'], $goods_object)) {
- // 商品库里有该商品
- $goods_info = $goods_object[$value['goods_name']];
- $goods_id = $goods_info['id'];
- $goods_stock_info = array_column($goods_info['goods_stock'], null, 'name');
- } else {
- // 商品库里没有该商品 创建
- $goods = Goods::create($goods_data);
- $goods_id = $goods->id;
- $goods_stock_info = [];
- }
- $goods_stock_data = [];
- $total_price = 0;
- foreach ($value['goods_stock'] as $val) {
- if (array_key_exists($val['name'], $goods_stock_info)) {
- // 商品库里有该商品规格
- $stock_id = $goods_stock_info[$val['name']]['id'];
- } else {
- // 商品库里没有该商品规格 创建
- $stock_data = [
- 'goods_id' => $goods_id,
- 'name' => $val['name'],
- ];
- $stock = GoodsStock::create($stock_data);
- $stock_id = $stock->id;
- }
- $stock = isset($val['stock']) && $val['stock'] > 0 ? $val['stock'] : 0;
- $price = isset($val['price']) && $val['price'] > 0 ? $val['price'] : 0;
- $total_price = bcadd($total_price, $stock * $price, 2);
- $goods_stock_data[] = [
- 'id' => $stock_id,
- 'goods_id' => $goods_id,
- 'name' => $val['name'],
- 'price' => $price,
- 'stock' => $stock,
- ];
- }
- $goods_data['info_id'] = $info_id;
- $goods_data['goods_id'] = $goods_id;
- $goods_data['total_price'] = $total_price;
- $goods_data['goods_stock'] = $goods_stock_data;
- $data[] = $goods_data;
- $goods_data['goods_stock'] = json_encode($goods_stock_data, JSON_UNESCAPED_UNICODE);
- $apply_goods_data[] = $goods_data;
- $total_amount = bcadd($total_amount, $total_price, 2);
- }
- $model::insertAll($apply_goods_data);
- }
- return compact("total_amount", "data");
- }
- /**
- * 添加同行人员
- *
- * @param integer $module 模块类型
- * @param array $params 同行人员数据
- * @param integer $info_id 申请ID
- * @param string $type 类型:create=申请,update=重新发起
- **/
- public static function create_peer_user($module, $params, $info_id, $type)
- {
- if ($type == 'update') {
- ApproveEvectionPeerUser::where('info_id', $info_id)->delete();
- }
- $data = [];
- if (!$params) {
- return $data;
- }
- foreach ($params as $value) {
- if (isset($value['name']) && !empty($value['name'])) {
- $data[] = [
- 'info_id' => $info_id,
- 'is_who' => $value['is_who'],
- 'user_id' => isset($value['user_id']) ? $value['user_id'] : '',
- 'name' => $value['name'],
- 'desc' => isset($value['desc']) ? $value['desc'] : '',
- ];
- }
- }
- if ($data) {
- ApproveEvectionPeerUser::insertAll($data);
- }
- return $data;
- }
- /**
- * 添加审批人
- *
- * @param array $approve_user 审批ID
- * @param integer $info_id 申请ID
- * @param string $type 类型:create=申请,update=重新发起
- * @param string $userid 发起人ID
- **/
- public static function create_approve($approve_user, $info_id, $type, $userid)
- {
- if ($type == 'update') {
- // 审批全部改为历史记录
- Approve::where('info_id', $info_id)->where('status', 'in', [CommonConstant::STATUS_3, CommonConstant::STATUS_4])->update(['state' => CommonConstant::IS_WHO_1]);
- Approve::where('info_id', $info_id)->where('status', 'in', [CommonConstant::STATUS_1, CommonConstant::STATUS_2])->update(['state' => CommonConstant::IS_WHO_0]);
- }
- $approve_data = [];
- $flow_num = 0;
- // 发起人
- $apply_data = [
- 'info_id' => $info_id,
- 'status' => 0,
- 'group' => CommonConstant::IS_WHO_1,
- 'approve_user' => $userid,
- 'approve_flow' => 1,
- ];
- // 审批人
- foreach ($approve_user as $key => $value) {
- $flow_num++;
- $approve_data[] = [
- 'info_id' => $info_id,
- 'status' => $flow_num == 1 ? CommonConstant::STATUS_2 : CommonConstant::STATUS_1,
- 'group' => CommonConstant::IS_WHO_0,
- 'approve_user' => $value,
- 'approve_flow' => $flow_num,
- ];
- }
- if ($approve_data) {
- // 添加审批人
- $approve_data = array_merge([$apply_data], $approve_data);
- Approve::insertAll($approve_data);
- }
- return true;
- }
- /**
- * 添加抄送人
- *
- * @param array $copy_user 抄送ID
- * @param integer $info_id 申请ID
- * @param string $type 类型:create=申请,update=重新发起
- **/
- public static function create_approve_copy($copy_user, $info_id, $type)
- {
- if ($type == 'update') {
- // 删除掉抄送
- ApproveCopy::where('info_id', $info_id)->delete();
- }
- $copy_data = [];
- $copy_num = 0;
- // 抄送人
- foreach ($copy_user as $key => $value) {
- $copy_num++;
- $copy_data[] = [
- 'info_id' => $info_id,
- 'approve_user' => $value,
- 'approve_flow' => $copy_num,
- ];
- }
- if ($copy_data) {
- // 添加抄送人
- ApproveCopy::insertAll($copy_data);
- }
- return true;
- }
- /**
- * 入库商品/领用商品组成数据
- *
- * @param mixed $goods 商品
- * @param integer $module 模块类型
- * @param integer $status 状态:2=审批中,3=审批同意,4=审批驳回
- **/
- public static function composition_data($goods, $module, $status)
- {
- $goodsStockSave = [];
- $goodsStockLog = [];
- $redisStock = [];
- $type = 0;
- foreach ($goods as $key => $val) {
- $goods_stock = $status == CommonConstant::STATUS_2 ? $val['goods_stock'] : $val['goods_stock_text'];
- foreach ($goods_stock as $k => $v) {
- if ($module == CommonConstant::MODULE_3) {
- // 增加剩余库存
- $goodsStockSave[] = [
- 'id' => $v['id'],
- 'stock' => ['inc', $v['stock']],
- ];
- }
- if ($module == CommonConstant::MODULE_4) {
- if ($status == CommonConstant::STATUS_2) {
- // 扣除剩余库存 增加冻结库存
- $goodsStockSave[] = [
- 'id' => $v['id'],
- 'stock' => ['dec', $v['stock']],
- 'freeze_stock' => ['inc', $v['stock']],
- ];
- }
- if ($status == CommonConstant::STATUS_3) {
- // 扣除冻结库存
- $goodsStockSave[] = [
- 'id' => $v['id'],
- 'freeze_stock' => ['dec', $v['stock']],
- ];
- $type = 1;
- }
- if ($status == CommonConstant::STATUS_4) {
- // 增加剩余库存 扣除冻结库存
- $goodsStockSave[] = [
- 'id' => $v['id'],
- 'stock' => ['inc', $v['stock']],
- 'freeze_stock' => ['dec', $v['stock']],
- ];
- }
- $redisStock[] = [
- 'id' => 'dingtalk_stock_' . $val['info_id'] . '_' . $v['goods_id'] . '_' . $v['id'],
- 'stock' => $v['stock'],
- ];
- }
- if ($status == CommonConstant::STATUS_3) {
- $goodsStockLog[] = [
- 'type' => $type,
- 'info_id' => $val['info_id'],
- 'goods_id' => $v['goods_id'],
- 'stock_id' => $v['id'],
- 'name' => $v['name'],
- 'price' => $v['price'],
- 'stock' => $v['stock'],
- ];
- }
- }
- }
- return [
- $goodsStockSave,
- $goodsStockLog,
- $redisStock
- ];
- }
- /**
- * 列表
- *
- * @param integer $group 类别:list=我的申请记录,form=采购审批单
- * @param integer $module 模块类型
- * @param integer $status 审批状态
- * @param string $search 搜索
- * @param string $start_time 申请开始时间
- * @param string $end_time 申请结束时间
- * @param integer $offset 起始位置
- * @param integer $length 查询数量
- * @param mixed $user 用户信息
- **/
- public static function get_list($group, $module, $status, $search, $start_time, $end_time, $offset, $length, $user)
- {
- if ($group == 'list') {
- if (!array_key_exists($module, CommonConstant::get_module_list())) {
- return [];
- }
- if (!array_key_exists($status, CommonConstant::get_approve_status_list())) {
- return [];
- }
- $type = 0;
- } else {
- $module = CommonConstant::MODULE_1;
- $status = CommonConstant::STATUS_3;
- $type = ApplyConstant::TYPE_1;
- }
- $userid = $user['userid'];
- $list = ApproveInfo::field('module_id,user_id,apply_user_id,department,is_deleted', true)
- ->where('module', $module)
- ->where(function ($query) use ($module, $userid) {
- if (in_array($module, [CommonConstant::MODULE_5, CommonConstant::MODULE_6, CommonConstant::MODULE_7])) {
- $query->where('user_id', $userid)->whereOr('apply_user_id', $userid);
- } else {
- $query->where('user_id', $userid);
- }
- })
- ->where('status', $status)
- ->where('is_deleted', CommonConstant::IS_DELETED_0)
- ->when(!empty($search), function ($query) use ($search) {
- $query->where('order_no|reason', 'like', '%' . $search . '%');
- })
- ->when(!empty($start_time) && !empty($end_time), function ($query) use ($start_time, $end_time) {
- $query->where('create_at', 'BETWEEN', [$start_time, $end_time]);
- })
- ->when($type > 0, function ($query) use ($type) {
- $query->where('type', $type);
- });
- $list = self::get_with($list, $module, $status);
- $list = $list->order('id desc')
- ->limit($offset, $length)
- ->select();
- return $list;
- }
- /**
- * 详情/信息
- *
- * @param integer $id 申请ID
- * @param mixed $user 用户信息
- * @param string $group 类别:0=审批人/后台,1=提交人申请人 审批详情,我的申请详情/信息
- * @param string $type 类型:detail=详情,info=信息
- **/
- public static function get_detail($id, $user, $group, $type)
- {
- $info = ApproveInfo::field('is_deleted', true)
- ->where(function ($query) use ($user, $group) {
- if ($group == CommonConstant::IS_WHO_1) {
- $query->where('user_id', $user['userid'])->whereOr('apply_user_id', $user['userid']);
- }
- })
- ->where('is_deleted', CommonConstant::IS_DELETED_0);
- if ($type == 'detail') {
- $info->with([
- 'moduleInfo',
- 'approve' => function ($query) {
- $query->field('is_deleted,create_at', true)
- ->where('state', 'in', [CommonConstant::IS_WHO_0, CommonConstant::IS_WHO_1])
- ->with([
- 'user' => function ($query) {
- $query->field('userid,name,avatar,signature,signature_status');
- }
- ]);
- },
- 'approveCopy' => function ($query) {
- $query->field('is_deleted,create_at', true)
- ->with([
- 'user' => function ($query) {
- $query->field('userid,name,avatar');
- }
- ]);
- }
- ]);
- $info = $info->find($id);
- if ($info) {
- // 部门列表
- $department_data = UserService::get_user_department_list($info['department']);
- $info['department_data'] = $department_data;
- }
- } else {
- // 信息
- $info->with([
- 'moduleInfo'
- ]);
- $info = $info->find($id);
- }
- if ($info) {
- $module = $info['module'];
- if ($group == CommonConstant::IS_WHO_1) {
- // 先默认当前是提交人
- $create_user = [
- 'id' => $user['id'],
- 'userid' => $user['userid'],
- 'name' => $user['name'],
- 'avatar' => $user['avatar'],
- ];
- $apply_user = $create_user;
- if (in_array($module, [CommonConstant::MODULE_5, CommonConstant::MODULE_6, CommonConstant::MODULE_7])) {
- if ($user['userid'] == $info['user_id']) {
- // 当前是提交人
- if ($info['user_id'] != $info['apply_user_id']) {
- $apply_user_info = User::field('id,userid,name,avatar')
- ->where('userid', $info['apply_user_id'])
- ->find();
- $apply_user = [
- 'id' => $apply_user_info['id'],
- 'userid' => $apply_user_info['userid'],
- 'name' => $apply_user_info['name'],
- 'avatar' => $apply_user_info['avatar'],
- ];
- }
- }
- if ($user['userid'] == $info['apply_user_id']) {
- // 当前是申请人
- if ($info['user_id'] != $info['apply_user_id']) {
- $apply_user_info = User::field('id,userid,name,avatar')
- ->where('userid', $info['user_id'])
- ->find();
- $create_user = [
- 'id' => $apply_user_info['id'],
- 'userid' => $apply_user_info['userid'],
- 'name' => $apply_user_info['name'],
- 'avatar' => $apply_user_info['avatar'],
- ];
- }
- }
- }
- }
- if ($group == CommonConstant::IS_WHO_0) {
- // 审批人
- $apply_user = User::field('id,userid,name,avatar')
- ->where('userid', $info['apply_user_id'])
- ->find();
- $create_user = [
- 'id' => $apply_user['id'],
- 'userid' => $apply_user['userid'],
- 'name' => $apply_user['name'],
- 'avatar' => $apply_user['avatar'],
- ];
- $apply_user = $create_user;
- if (in_array($module, [CommonConstant::MODULE_5, CommonConstant::MODULE_6, CommonConstant::MODULE_7])) {
- if ($info['user_id'] != $info['apply_user_id']) {
- $apply_user_info = User::field('id,userid,name,avatar')
- ->where('userid', $info['user_id'])
- ->find();
- $create_user = [
- 'id' => $apply_user_info['id'],
- 'userid' => $apply_user_info['userid'],
- 'name' => $apply_user_info['name'],
- 'avatar' => $apply_user_info['avatar'],
- ];
- }
- }
- }
- $info['create_user'] = $create_user;
- $info['apply_user'] = $apply_user;
- }
- $info = self::get_item($info, $type);
- return $info;
- }
- /**
- * 列表-关联数据
- *
- * @param mixed $list
- * @param integer $module 模块类型
- * @param integer $status 审批状态
- **/
- public static function get_with($list, $module, $status)
- {
- $field = 'id,info_id,status,approve_user';
- switch ($module) {
- case CommonConstant::MODULE_1:
- // 申领商品列表
- if (in_array($status, [CommonConstant::STATUS_2, CommonConstant::STATUS_4])) {
- // 审批中或审批驳回 才关联 审批人信息
- $list = $list->with([
- 'approveOne' => function ($query) use ($status, $field) {
- $query->field($field)
- ->where('status', $status)
- ->where('group', CommonConstant::IS_WHO_0)
- ->where('state', CommonConstant::IS_WHO_0)
- ->with([
- 'user' => function ($query) {
- $query->field('userid,name');
- }
- ]);
- },
- 'applyGoods' => function ($query) {
- $query->field('create_at', true);
- },
- ]);
- } else {
- $list = $list->with([
- 'applyGoods' => function ($query) {
- $query->field('create_at', true);
- },
- ]);
- }
- break;
- case CommonConstant::MODULE_2:
- if (in_array($status, [CommonConstant::STATUS_2, CommonConstant::STATUS_4])) {
- // 审批中或审批驳回 才关联 审批人信息
- $list = $list->with([
- 'approveOne' => function ($query) use ($status, $field) {
- $query->field($field)
- ->where('status', $status)
- ->where('group', CommonConstant::IS_WHO_0)
- ->where('state', CommonConstant::IS_WHO_0)
- ->with([
- 'user' => function ($query) {
- $query->field('userid,name');
- }
- ]);
- }
- ]);
- }
- break;
- case CommonConstant::MODULE_3:
- // 入库商品列表
- if (in_array($status, [CommonConstant::STATUS_2, CommonConstant::STATUS_4])) {
- // 审批中或审批驳回 才关联 审批人信息
- $list = $list->with([
- 'approveOne' => function ($query) use ($status, $field) {
- $query->field($field)
- ->where('status', $status)
- ->where('group', CommonConstant::IS_WHO_0)
- ->where('state', CommonConstant::IS_WHO_0)
- ->with([
- 'user' => function ($query) {
- $query->field('userid,name');
- }
- ]);
- },
- 'stockGoods' => function ($query) {
- $query->field('id,info_id,goods_name');
- },
- ]);
- } else {
- $list = $list->with([
- 'stockGoods' => function ($query) {
- $query->field('id,info_id,goods_name');
- },
- ]);
- }
- break;
- case CommonConstant::MODULE_4:
- // 领用商品列表
- if (in_array($status, [CommonConstant::STATUS_2, CommonConstant::STATUS_4])) {
- // 审批中或审批驳回 才关联 审批人信息
- $list = $list->with([
- 'approveOne' => function ($query) use ($status, $field) {
- $query->field($field)
- ->where('status', $status)
- ->where('group', CommonConstant::IS_WHO_0)
- ->where('state', CommonConstant::IS_WHO_0)
- ->with([
- 'user' => function ($query) {
- $query->field('userid,name');
- }
- ]);
- },
- 'useGoods' => function ($query) {
- $query->field('id,info_id,goods_name');
- },
- ]);
- } else {
- $list = $list->with([
- 'useGoods' => function ($query) {
- $query->field('id,info_id,goods_name');
- },
- ]);
- }
- break;
- case CommonConstant::MODULE_5:
- // 出差同行人员列表
- if (in_array($status, [CommonConstant::STATUS_2, CommonConstant::STATUS_4])) {
- // 审批中或审批驳回 才关联 审批人信息
- $list = $list->with([
- 'approveOne' => function ($query) use ($status, $field) {
- $query->field($field)
- ->where('status', $status)
- ->where('group', CommonConstant::IS_WHO_0)
- ->where('state', CommonConstant::IS_WHO_0)
- ->with([
- 'user' => function ($query) {
- $query->field('userid,name');
- }
- ]);
- },
- 'peerUser' => function ($query) {
- $query->field('id,info_id,name');
- },
- ]);
- } else {
- $list = $list->with([
- 'peerUser' => function ($query) {
- $query->field('id,info_id,name');
- },
- ]);
- }
- break;
- case CommonConstant::MODULE_6:
- case CommonConstant::MODULE_7:
- case CommonConstant::MODULE_8:
- case CommonConstant::MODULE_9:
- case CommonConstant::MODULE_10:
- case CommonConstant::MODULE_11:
- if (in_array($status, [CommonConstant::STATUS_2, CommonConstant::STATUS_4])) {
- // 审批中或审批驳回 才关联 审批人信息
- $list = $list->with([
- 'approveOne' => function ($query) use ($status, $field) {
- $query->field($field)
- ->where('status', $status)
- ->where('group', CommonConstant::IS_WHO_0)
- ->where('state', CommonConstant::IS_WHO_0)
- ->with([
- 'user' => function ($query) {
- $query->field('userid,name');
- }
- ]);
- }
- ]);
- }
- break;
- }
- return $list;
- }
- /**
- * 详情/信息-关联数据
- *
- * @param mixed $info
- * @param string $type 类型:detail=详情,info=信息,edit=审批人修改
- **/
- public static function get_item($info, $type)
- {
- if ($info) {
- $module = $info['module'];
- switch ($module) {
- case CommonConstant::MODULE_1:
- if ($info['type'] == ApplyConstant::TYPE_1) {
- // $info['apply_goods'] = $info->applyGoods()->select();
- $data = $info->apply_goods;
- $info['data'] = $data;
- }
- $contract = [];
- if ($type == 'detail') {
- // 关联的合同呈批申请
- $contract = ApproveInfo::field('id,reason')
- ->where('module', CommonConstant::MODULE_9)
- ->where('status', CommonConstant::STATUS_3)
- ->where('is_deleted', CommonConstant::IS_DELETED_0)
- ->where('apply_id', $info['id'])
- ->select();
- }
- $info['contract'] = $contract;
- break;
- case CommonConstant::MODULE_3:
- // $info['stock_goods'] = $info->stockGoods()->select();
- $data = $info->stock_goods;
- $info['data'] = $data;
- $apply = null;
- if ($type != 'edit') {
- // 关联的申购申请单
- if ($info['apply_id'] > 0) {
- $apply = ApproveInfo::field('id,order_no')
- ->where('module', CommonConstant::MODULE_1)
- ->where('status', CommonConstant::STATUS_3)
- ->where('is_deleted', CommonConstant::IS_DELETED_0)
- ->find($info['apply_id']);
- }
- }
- $info['apply'] = $apply;
- break;
- case CommonConstant::MODULE_4:
- // $info['use_goods'] = $info->useGoods()->select();
- $data = $info->use_goods;
- $info['data'] = $data;
- break;
- case CommonConstant::MODULE_5:
- $info['peer_user'] = $info->peerUser()->field('id,info_id,name')->select();
- // $info->peer_user;
- $info['data'] = $info['peer_user'];
- break;
- case CommonConstant::MODULE_9:
- $apply = null;
- if ($type != 'edit') {
- // 关联的申购申请单
- if ($info['apply_id'] > 0) {
- $apply = ApproveInfo::field('id,order_no')
- ->where('module', CommonConstant::MODULE_1)
- ->where('status', CommonConstant::STATUS_3)
- ->where('is_deleted', CommonConstant::IS_DELETED_0)
- ->find($info['apply_id']);
- }
- }
- $info['apply'] = $apply;
- break;
- }
- }
- return $info;
- }
- /**
- * 操作方法
- *
- * @param integer $id 申请ID
- * @param array $params 数组
- * @param mixed $user 用户信息
- * @param string $type 类型:urging=催办,cancel=撤销,comment=评分
- */
- public static function make($id, $params, $user, $type)
- {
- $userid = $user['userid'];
- $info = ApproveInfo::field('id,module,user_id,status,reason,maintain_user_id')
- ->where('is_deleted', CommonConstant::IS_DELETED_0)
- ->find($id);
- if (!$info) {
- except('申请记录不存在或已删除');
- }
- if ($info->user_id != $userid) {
- except('您没有权限操作');
- }
- switch ($type) {
- case 'urging':
- if ($info->status != CommonConstant::STATUS_2) {
- except('其他人已操作');
- }
- // TODO 待对接钉钉接口
- break;
- case 'cancel':
- if ($info->status != CommonConstant::STATUS_4) {
- except('非审批驳回状态无法操作');
- }
- $info->status = CommonConstant::STATUS_5;
- $info->save();
- if ($info->module == CommonConstant::MODULE_9) {
- // 9=合同呈批 释放合同编号
- CommonService::set_contract_no($info->reason);
- }
- break;
- case 'comment':
- if ($info->status != CommonConstant::STATUS_3) {
- except('非审批同意状态无法操作');
- }
- if ($info->module != CommonConstant::MODULE_8) {
- // 8=维修申请
- except(CommonConstant::get_module_list()[CommonConstant::MODULE_8] . '记录不存在或已删除');
- }
- // TODO 没有判断评价状态
- Db::startTrans();
- try {
- // 添加评价记录
- $data = $params;
- $data['info_id'] = $id;
- $data['user_id'] = $info->maintain_user_id;
- ApproveMaintainUserLog::create($data);
- // 更新维修信息
- $params['comment_status'] = CommonConstant::IS_WHO_1;
- ApproveMaintain::where('info_id', $id)->update($params);
- Db::commit();
- } catch (Exception $e) {
- Db::rollback();
- except('出现错误:' . $e->getMessage());
- }
- break;
- }
- return true;
- }
- /**
- * 下载文件pdf
- *
- * @param integer $id 申请ID
- **/
- public static function download($id)
- {
- $data = self::get_detail($id, [], CommonConstant::IS_WHO_0, 'detail');
- if (!$data) {
- except('申请记录不存在或已删除');
- }
- // 维修人员
- $data['maintain_user'] = $data['module'] == CommonConstant::MODULE_8 && $data['maintain_user_id'] > 0 ? ApproveMaintainUser::field('id,name')->find($data['maintain_user_id']) :null;
- unset($data['approve'][0]);// 去掉审批流程里面的创建人
- $data['approve_count'] = count($data['approve']) + count($data['approve_copy']);
- $data['subject'] = sysconf('subject');
- $vo = $data->toArray();
- $template = 'index_' . $data['module'];
- $get_module_list = CommonConstant::get_module_list();
- $get_approve_status_list = CommonConstant::get_approve_status_list();
- $get_approve_status_list_admin = CommonConstant::get_approve_status_list_admin();
- $get_is_who_list = CommonConstant::get_is_who_list();
- $get_pay_type_list = ApplyConstant::get_pay_type_list();
- $get_degree_list = OfferConstant::get_degree_list();
- $get_type_list = CommonService::get_type_list($data['module']);
- $title = $get_module_list[$data['module']];
- $name = $data['create_user']['name'] . '提交的' . $title . '审批单';
- $data = compact('vo', 'template', 'get_module_list', 'get_approve_status_list', 'get_approve_status_list_admin', 'get_is_who_list', 'get_pay_type_list', 'get_degree_list', 'get_type_list');
- $html = self::get_html($data);
- $realpath = self::getPath($name);
- $pdf = new \TCPDF();
- $pdf = self::setPdfAttr($pdf);
- self::exportPdf($pdf, $html, $realpath);
- return ['url' => $realpath, 'fullurl' => 'https://' . $_SERVER['HTTP_HOST'] . $realpath];
- }
- // 获取html
- public static function get_html($data)
- {
- // $htmlUrl = app()->getRootPath() . 'public/1.html';
- // $html = file_get_contents($htmlUrl);
- $html = view('common/' . $data['template'], $data)->getContent();
- return $html;
- }
- // 获取路径
- public static function getPath($name)
- {
- //保存文件
- $subPath = date('Ymd');
- $savePath = './upload/pdf/' . $subPath;
- // 检查上传目录
- if (!is_dir($savePath)) {
- // 检查目录是否编码后的
- if (is_dir(base64_decode($savePath))) {
- $savePath = base64_decode($savePath);
- } else {
- // 尝试创建目录
- if (!mkdir($savePath, 0755, true)) {
- except('上传目录' . $savePath . '不存在');
- return false;
- }
- }
- } else {
- if (!is_writeable($savePath)) {
- except('上传目录' . $savePath . '不可写');
- return false;
- }
- }
- $filename = date('Ymd') . sha1(date('YmdHis', time()) . uniqid());
- $filename = $name . date('Ymd') . time();
- $suffix = '.pdf';
- $realpath = substr($savePath, 1) . '/' . $filename . $suffix;
- return $realpath;
- }
- // 设置PDF参数
- public static function setPdfAttr($pdf)
- {
- // 设置页面信息
- $pdf->SetTitle("简历文件");
- $pdf->setAuthor('Dya');
- $pdf->setCreator(PDF_CREATOR);
- $pdf->SetSubject('TCPDF Tutorial');
- $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
- //页眉页脚
- $pdf->setPrintHeader(false);
- $pdf->setPrintFooter(false);
- // 自动分页 (第二个参数可以设置距离底部多少距离时分页)
- $pdf->setAutoPageBreak(true, 15);
- // 设置边距(左 上 右 下) 右边距默认左侧值 下边距是bool值(是否覆盖默认页边距)
- $pdf->setMargins(26, 18, 26);
- // 定义默认的单间距字体 (设置为等宽字体)
- $pdf->SetDefaultMonospacedFont('courier');
- // 设置图像比例因子
- $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
- // 设置默认字体
- $pdf->SetFont('stsongstdlight', '', 10, '', true);
- return $pdf;
- }
- // 生成下载pdf
- public static function exportPdf($pdf, $html, $realpath)
- {
- // 新增页面
- $pdf->AddPage();
- $pdf->writeHTML($html, true, false, true, false, '');
- $pdf->Output($_SERVER['DOCUMENT_ROOT'] . $realpath, 'F');
- }
- }
|