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($module == CommonConstant::MODULE_4){ 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/2.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 . '不存在'); } } } else { if (!is_writeable($savePath)) { except('上传目录' . $savePath . '不可写'); } } $filename = date('YmdHis') . 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'); } }