where('userid', $params['apply_user_id']) ->find(); if (!$apply_user) { except('申请人信息不存在或已删除'); } $apply_user_id = $apply_user['userid']; $department = $apply_user['department']; } // 重新发起 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 { // 9=合同呈批申请 if ($module == 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($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) { // 编辑对应模块 self::create_module($module, $params, $id, $info, 'update'); // 编辑审批抄送 self::create_approve($approve_user, $copy_user, $userid, $id, 'update'); // 编辑审批申请 if ($module == CommonConstant::MODULE_9) { unset($data['reason']); } unset($data['order_no']); $save_data = [ 'create_at' => date('Y-m-d H:i:s'), ]; $data = array_merge($data, $save_data); $info->save($data); } else { // 添加审批申请 $info = ApproveInfo::create($data); $info_id = $info->id; // 添加对应模块 $module_data = self::create_module($module, $params, $info_id, [], 'create'); $info->module_id = $module_data['module_id']; $info->save(); // 添加审批抄送 self::create_approve($approve_user, $copy_user, $userid, $info_id, 'create'); } Db::commit(); } catch (Exception $e) { except('出现错误:' . $e->getMessage()); } if($way == CommonConstant::create){ if ($module == CommonConstant::MODULE_9) { // 生成合同编号 $contract_no = CommonService::get_contract_no($params['type']); $info->save(['reason'=>$contract_no]); } } return true; } /** * 添加对应模块 * * @param integer $module 模块类型 * @param array $params * @param integer $info_id 申请ID * @param mixed $info 申请信息 * @param string $type 类型:create=申请,update=重新发起 **/ 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($info_id, $module, $params['apply_goods'], $type); $total_amount = $other['total_amount']; } $data['info_id'] = $info_id; $data['reason'] = $params['reason']; $data['type'] = $params['type']; $data['total_amount'] = $total_amount; // $params['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($info_id, $module, $params['stock_goods'], $type); $data['info_id'] = $info_id; $data['total_amount'] = $other['total_amount']; $data['document'] = $params['document']; $data['images'] = $params['images']; $data['remark'] = $params['remark']; break; case CommonConstant::MODULE_4: // 添加领用物品 $other = self::create_goods($info_id, $module, $params['use_goods'], $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($info_id, $params['peer_user'], $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['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 == 'update') { unset($data['reason']); } 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 == 'update') { $info->module_info->save($data); $module_id = $info->module_info->id; } else { $models = CommonConstant::get_module_model_list(); $model = $models[$module]; $module_result = $model::create($data); $module_id = $module_result->id; } $module_data = $data; $result = compact("module_id", "module_data", "other"); return $result; } /** * 添加申购物品/入库物品/领用物品 * * @param integer $info_id 申请ID * @param integer $module 模块类型 * @param array $params 数据 * @param string $type 类型:create=申请,update=重新发起 **/ public static function create_goods($info_id, $module, $params, $type) { if (!$params) { return []; } $goods_list_new = []; $category_first_data = []; $category_data = []; $goods_ids = []; $goods_ids_new = []; $data = []; $total_amount = 0; 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; break; } if ($type == 'update') { $model::where('info_id', $info_id)->delete(); } 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 ($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() ->toArray(); $goods_object = $goods_list ? array_column($goods_list, null, 'id') : []; $goods_name = ''; $goods_stock_name = ''; $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_info = 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_info)) { // 商品库里有该商品规格 $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' => $val['id'], 'goods_id' => $goods_info['id'], 'name' => $val['name'], 'price' => $price, 'stock' => $stock, ]; } else { // 商品库里没有该商品规格 $goods_stock_name .= $value['goods_name'] . '-' . $val['name'] . '、'; } } $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 { // 商品库里没有该商品 $goods_name .= $value['goods_name'] . '、'; } } if ($goods_name) { // 商品库选择的商品 在商品库没有匹配到 返回错误提示 except($goods_name . '等商品在商品库里不存在或已删除'); } if ($goods_stock_name) { // 商品库选择的商品的规格 在商品库没有匹配到 返回错误提示 except($goods_stock_name . '等商品规格在商品库里不存在或已删除'); } $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_info = $category_object[$value['goods_category_first']]; $goods_category_first_id = $category_info['id']; $category_info_object = array_column($category_info['childlist'], null, 'name'); $goods_category_id = $category_info_object[$value['goods_category_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 $info_id 申请ID * @param array $params 同行人员数据 * @param string $type 类型:create=申请,update=重新发起 **/ public static function create_peer_user($info_id, $params, $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 array $copy_user 抄送ID * @param string $userid 发起人ID * @param integer $info_id 申请ID * @param string $type 类型:create=申请,update=重新发起 **/ public static function create_approve($approve_user, $copy_user, $userid, $info_id, $type) { if ($type == 'update') { // 审批全部改为历史记录 Approve::where('info_id', $info_id)->update(['state' => CommonConstant::IS_WHO_1]); // 删除掉抄送 ApproveCopy::where('info_id', $info_id)->delete(); } $approve_data = []; $copy_data = []; $flow_num = 0; $copy_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, ]; } // 抄送人 foreach ($copy_user as $key => $value) { $copy_num++; $copy_data[] = [ 'info_id' => $info_id, 'approve_user' => $value, 'approve_flow' => $copy_num, ]; } if ($approve_data) { // 添加审批 $approve_data = array_merge([$apply_data], $approve_data); Approve::insertAll($approve_data); } if ($copy_data) { // 添加抄送 ApproveCopy::insertAll($copy_data); } } /** * 列表 * * @param integer $group 类别:list=我的申请记录,form=采购审批单 * @param integer $module 模块类型 * @param integer $status 审批状态 * @param string $search 搜索 * @param integer $offset 起始位置 * @param integer $length 查询数量 * @param mixed $user 用户信息 **/ public static function get_list($group, $module, $status, $search, $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($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'); } ]); }, '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) { $module = $info['module']; if ($group == CommonConstant::IS_WHO_1) { // 提交人 $apply_user = [ 'userid' => $user['userid'], 'name' => $user['name'], 'avatar' => $user['avatar'], ]; $create_user = $apply_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('userid,name,avatar') ->where('userid', $info['apply_user_id']) ->find(); $apply_user = [ '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('userid,name,avatar') ->where('userid', $info['apply_user_id']) ->find(); $apply_user = [ 'userid' => $apply_user['userid'], 'name' => $apply_user['name'], 'avatar' => $apply_user['avatar'], ]; $create_user = $apply_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('userid,name,avatar') ->where('userid', $info['userid']) ->find(); $create_user = [ 'userid' => $apply_user_info['userid'], 'name' => $apply_user_info['name'], 'avatar' => $apply_user_info['avatar'], ]; } } } // 部门列表 $department_data = UserService::get_user_department_list($info['department']); // 关联的合同呈批申请 $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', $id) ->select(); $info['create_user'] = $create_user; $info['apply_user'] = $apply_user; $info['department_data'] = $department_data; $info['contract'] = $contract; } } else { // 信息 $info->with([ 'moduleInfo' ]); $info = $info->find($id); if ($info) { $module = $info['module']; if ($group == CommonConstant::IS_WHO_1) { // 提交人 $apply_user = [ 'userid' => $user['userid'], 'name' => $user['name'], 'avatar' => $user['avatar'], ]; $create_user = $apply_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('userid,name,avatar') ->where('userid', $info['apply_user_id']) ->find(); $apply_user = [ '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=信息 **/ public static function get_item($info, $type) { if ($info) { $module = $info['module']; switch ($module) { case CommonConstant::MODULE_1: $info['apply_goods'] = []; if ($info['type'] == ApplyConstant::TYPE_1) { if ($type == 'detail') { $info['apply_goods'] = $info->applyGoods()->field('id,info_id,goods_name,total_price,goods_stock')->select(); } else { $info->apply_goods; } } break; case CommonConstant::MODULE_3: if ($type == 'detail') { $info['stock_goods'] = $info->stockGoods()->field('id,info_id,goods_name,total_price,goods_stock')->select(); } else { $info->stock_goods; } break; case CommonConstant::MODULE_4: if ($type == 'detail') { $info['use_goods'] = $info->useGoods()->field('id,info_id,goods_name,total_price,goods_stock')->select(); } else { $info->use_goods; } break; case CommonConstant::MODULE_5: if ($type == 'detail') { $info['peer_user'] = $info->peerUser()->field('id,info_id,name')->select(); } else { $info->peer_user; } break; case CommonConstant::MODULE_8: if ($type == 'detail') { } 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,user_id,module,status,reason') ->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 没有判断评价状态 $params['comment_status'] = CommonConstant::IS_WHO_1; self::comment($info, $params); break; } return true; } /** * 评分 * * @param mixed $info 申请信息 * @param array $params 数组 **/ public static function comment($info, $params) { try { $data = $params; // 更新维修信息 ApproveMaintain::where('info_id', $info->id)->update($data); } catch (Exception $e) { except('出现错误:' . $e->getMessage()); } return true; } }