0) { $info = ApproveInfo::field('user_id,is_deleted', true) ->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('非审批驳回状态无法操作'); } } $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 = count($approve_user); $data = [ 'module' => $module, 'user_id' => $userid, 'department' => $user['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' => $params['start_time'], 'end_time' => $params['end_time'], 'apply_id' => $params['apply_id'], ]; Db::startTrans(); try { if ($id > 0) { // 编辑审批申请 $save_data = [ 'create_at' => date('Y-m-d H:i:s'), ]; $data = array_merge($data, $save_data); $info->save($data); // 编辑对应模块 self::create_module($id, $module, $params, $info, 'update'); // 编辑审批抄送 self::create_approve($id, $approve_user, $copy_user, 'update'); } else { // 添加审批申请 $info = ApproveInfo::create($data); $info_id = $info->id; // 添加对应模块 $module_data = self::create_module($info_id, $module, $params, [], 'create'); $info->save(['module_id' => $module_data->id]); // 添加审批抄送 self::create_approve($info_id, $approve_user, $copy_user, 'create'); } Db::commit(); } catch (Exception $e) { except('出现错误:' . $e->getMessage(), 2, $e); } return true; } /** * 添加对应模块 * * @param integer $info_id 申请ID * @param integer $module 模块类型 * @param array $params * @param mixed $info 申请信息 * @param string $type 类型:create=申请,update=重新发起 **/ public static function create_module($info_id, $module, $params, $info, $type) { $data = []; switch ($module) { case CommonConstant::MODULE_1: // 添加申购物品 $res = self::create_goods($info_id, $module, $params['apply_goods'], $type); $data['info_id'] = $info_id; $data['reason'] = $params['reason']; $data['type'] = $params['type']; // $data['total_amount'] = $params['total_amount']; $data['total_amount'] = $res['total_amount']; $data['apply_date'] = $params['apply_date']; $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: // 添加入库物品 self::create_goods($info_id, $module, $params['stock_goods'], $type); $data['info_id'] = $info_id; $data['document'] = $params['document']; $data['images'] = $params['images']; $data['remark'] = $params['remark']; break; case CommonConstant::MODULE_4: $data['info_id'] = $info_id; $data['reason'] = $params['reason']; $data['document'] = $params['document']; $data['remark'] = $params['remark']; break; case CommonConstant::MODULE_5: // 添加同行人员 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['type'] = $params['type']; $data['reason'] = $params['reason']; $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']; 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['apply_date'] = $params['apply_date']; $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['apply_date'] = $params['apply_date']; $data['serial_number'] = $params['serial_number']; break; } if ($type == 'update') { $module_result = $info->module_info->save($data); } else { $models = CommonConstant::get_module_model_list(); $model = $models[$module]; $module_result = $model::create($data); } return $module_result; } /** * 添加申购物品/入库物品 * * @param integer $info_id 申请ID * @param integer $module 模块类型:1,3 * @param array $params 申购物品数据 * @param string $type 类型:create=申请,update=重新发起 **/ public static function create_goods($info_id, $module, $params, $type) { if (!$params) { return true; } switch ($module) { case CommonConstant::MODULE_1: $model = ApplyGoods::class; break; case CommonConstant::MODULE_3: $model = StockGoods::class; break; } if ($type == 'update') { $model::where('info_id', $info_id)->delete(); } $goods_ids = []; $goods_ids_new = []; $goods_list_new = []; $total_amount = 0; foreach ($params as $value) { if ($value['id'] > 0) { // 商品库选择 $goods_ids[$value['id']] = $value; } else { if ($value['goods_name']) { // 添加新商品 $goods_ids_new[] = $value; $goods_info = Goods::field('status,is_deleted,create_at', true) ->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(); } } else { // 批量导入 } } } // 商品库选择 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['id'], $goods_object)) { // 商品库里有该商品 $goods_info = $goods_object[$value['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)) { // 商品库里有该商品规格 $price = isset($val['price']) ? $val['price'] : 0; $total_price = bcadd($total_price, $price, 2); $goods_stock_data[] = [ 'id' => $val['id'], 'goods_id' => $goods_info['id'], 'name' => $val['name'], 'price' => $price, 'stock' => $val['stock'], ]; } else { // 商品库里没有该商品规格 $goods_stock_name .= $value['goods_name'] . $val['name'] . '、'; } } $apply_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_data' => json_encode($goods_stock_data, JSON_UNESCAPED_UNICODE), ]; $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 ($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' => $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; } $price = isset($val['price']) ? $val['price'] : 0; $total_price = bcadd($total_price, $price, 2); $goods_stock_data[] = [ 'id' => $stock_id, 'goods_id' => $goods_id, 'name' => $val['name'], 'price' => $price, 'stock' => $val['stock'], ]; } $goods_data['info_id'] = $info_id; $goods_data['total_price'] = $total_price; $goods_data['goods_data'] = 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"); } /** * 添加同行人员 * * @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(); } if (!$params) { return true; } $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); } } /** * 添加审批抄送 * * @param integer $info_id 申请ID * @param array $approve_user 审批ID * @param array $copy_user 抄送ID * @param string $type 类型:create=申请,update=重新发起 **/ public static function create_approve($info_id, $approve_user, $copy_user, $type) { if ($type == 'update') { Approve::where('info_id', $info_id)->delete(); ApproveCopy::where('info_id', $info_id)->delete(); } $data = []; $copy_data = []; $flow_num = 0; $copy_num = 0; $create_at = date('Y-m-d H:i:s'); // 审批人 foreach ($approve_user as $key => $value) { $flow_num++; $data[] = [ 'info_id' => $info_id, 'create_at' => $create_at, 'status' => $flow_num == 1 ? CommonConstant::STATUS_2 : CommonConstant::STATUS_1, 'approve_flow' => $flow_num, 'approve_user' => $value, 'approve_time' => $create_at, ]; } // 抄送人 foreach ($copy_user as $key => $value) { $copy_num++; $copy_data[] = [ 'info_id' => $info_id, 'create_at' => $create_at, 'approve_flow' => $copy_num, 'approve_user' => $value, ]; } if ($data) { // 添加审批数据 Approve::insertAll($data); } if ($copy_data) { // 添加抄送数据 ApproveCopy::insertAll($copy_data); } } /** * 我的申请记录 * * @param integer $module 模块类型 * @param integer $status 审批状态 * @param string $search 搜索 * @param integer $offset 起始位置 * @param integer $length 查询数量 * @param mixed $user 用户信息 **/ public static function get_list($module, $status, $search, $offset, $length, $user) { if (!array_key_exists($module, CommonConstant::get_module_list())) { return []; } if (!array_key_exists($status, CommonConstant::get_status_list())) { return []; } $userid = $user['userid']; $list = ApproveInfo::field('module_id,user_id,department,is_deleted', true) ->where('module', $module) ->where('user_id', $userid) ->where('status', $status) ->where('is_deleted', CommonConstant::IS_DELETED_0) ->when($search, function ($query) use ($search) { $query->where('order_no|reason', 'like', '%' . $search . '%'); }); $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 $type 类型:detail=详情,info=信息 **/ public static function get_detail($id, $user, $type) { $where = $user ? ['user_id' => $user['userid']] : []; $info = ApproveInfo::field('is_deleted', true) ->where($where) ->where('is_deleted', CommonConstant::IS_DELETED_0); if ($type == 'detail') { $info->with([ 'moduleInfo', 'approve' => function ($query) { $query->field('is_deleted,create_at', true) ->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) { // 审批申请人信息 if ($user) { $approve_info_user = [ 'userid' => $user['userid'], 'name' => $user['name'], 'avatar' => $user['avatar'], 'create_at' => $info['create_at'], ]; } else { $approve_info_user = User::field('userid,name,avatar')->where('userid', $info['user_id'])->find(); $approve_info_user['create_at'] = $info['create_at']; } // 部门列表 $department_data = Department::field('dept_id,name')->where('dept_id', 'in', $info['department'])->select(); $info['approve_info_user'] = $approve_info_user; $info['department_data'] = $department_data; } } else { $info->with([ 'moduleInfo' ]); $info = $info->find($id); } $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) ->with([ 'user' => function ($query) { $query->field('userid,name'); } ]); }, 'applyGoods' => function ($query) { $query->field('id,info_id,goods_name'); }, ]); } else { $list = $list->with([ 'applyGoods' => function ($query) { $query->field('id,info_id,goods_name'); }, ]); } break; case CommonConstant::MODULE_2: case CommonConstant::MODULE_3: 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) ->with([ 'user' => function ($query) { $query->field('userid,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) ->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) ->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) { $module = $info['module']; switch ($module) { case CommonConstant::MODULE_1: if ($type == 'detail') { $info['apply_goods'] = $info->applyGoods()->field('id,info_id,goods_name,total_amount,goods_data')->select(); } else { $info->apply_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; } return $info; } /** * 操作方法 * * @param integer $id 申请ID * @param mixed $user 用户信息 * @param string $type 类型:urging=催办,cancel=撤销 */ public static function make($id, $user, $type) { $userid = $user['userid']; $info = ApproveInfo::field('id,module,status') ->where('user_id', $userid) ->where('is_deleted', CommonConstant::IS_DELETED_0) ->find($id); if (!$info) { throw new Exception(CommonConstant::get_module_list()[$info->module] . '记录不存在或已删除'); } switch ($type) { case 'urging': if ($info->status != CommonConstant::STATUS_2) { throw new Exception('其他人已操作'); } // TODO 待对接钉钉接口 break; case 'cancel': if ($info->status != CommonConstant::STATUS_4) { throw new Exception('非审批驳回状态无法操作'); } $info->status = CommonConstant::STATUS_5; $info->save(); break; } } }