更新审批接口

This commit is contained in:
27942
2025-12-30 15:55:20 +08:00
parent b7eff5f11e
commit d20a74c3fb
5 changed files with 123 additions and 39 deletions

View File

@@ -32,7 +32,10 @@ class Approval(models.Model):
content = models.TextField() # 内容
times = models.DateField() # 提交时间
completeTiem = models.DateField(null=True, blank=True, default=None) # 完成时间
personincharge = models.CharField(max_length=100) # 负责人
personincharge = models.CharField(max_length=100) # 负责人/审批部门
# 统一规则:
# - 纯数字字符串(如 "1", "2"= 部门ID该部门下所有人员都能看到审批
# - 非纯数字字符串(如 "张三"= 审批员用户名(只有该审批员能看到审批)
state = models.CharField(max_length=100) # 状态
type = models.CharField(max_length=100) # 类别
user_id = models.CharField(max_length=100) # 事件id

62
User/utils.py Normal file
View File

@@ -0,0 +1,62 @@
"""
审批相关的工具函数
"""
def is_department_id(value):
"""
判断personincharge字段的值是部门ID还是审批员用户名
统一规则:
- 如果是纯数字字符串(如 "1", "2", "123"表示部门ID
- 如果包含非数字字符(如 "张三", "李四"),表示审批员用户名
Args:
value: personincharge字段的值字符串
Returns:
bool: True表示是部门IDFalse表示是审批员用户名
示例:
>>> is_department_id("1")
True
>>> is_department_id("123")
True
>>> is_department_id("张三")
False
>>> is_department_id("dept:1")
False
"""
if not value:
return False
# 判断是否为纯数字字符串(去除首尾空格)
return str(value).strip().isdigit()
def format_personincharge(value, is_department=False):
"""
格式化personincharge字段的值
统一规则:
- 如果是部门ID确保是纯数字字符串
- 如果是审批员用户名,保持原样
Args:
value: 部门ID整数或字符串或审批员用户名字符串
is_department: 是否为部门ID默认False审批员用户名
Returns:
str: 格式化后的personincharge值
"""
if not value:
return ''
if is_department:
# 部门ID转换为字符串确保是纯数字
try:
return str(int(value))
except (ValueError, TypeError):
raise ValueError(f"部门ID必须是数字当前值: {value}")
else:
# 审批员用户名:保持原样,但确保是字符串
return str(value).strip()

View File

@@ -15,7 +15,7 @@ from django.contrib.sessions.backends.db import SessionStore
from django.db.models import Count, Q
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from collections import defaultdict
from .utils import is_department_id
class CreateUserView(APIView):
@@ -529,26 +529,26 @@ class roxyExhibition(APIView):
user_department_ids_str = [str(did) for did in user_department_ids]
# 构建查询条件:
# 1. 如果personincharge是部门ID匹配用户所属部门
# 2. 兼容旧数据如果personincharge是用户名匹配当前用户名
# personincharge字段统一规则:
# - 纯数字字符串 = 部门ID该部门下所有人员都能看到
# - 非纯数字字符串 = 审批员用户名(只有该审批员能看到)
query = Q(state="审核中")
# 部门匹配personincharge字段存储的是部门ID字符串格式
# 如果用户有部门则匹配部门ID
# 部门匹配personincharge字段是纯数字字符串,且匹配用户所属部门
department_query = Q()
if user_department_ids_str:
# 匹配personincharge字段等于用户所属的任一部门ID
# 匹配personincharge字段等于用户所属的任一部门ID(纯数字字符串)
department_query = Q(personincharge__in=user_department_ids_str)
# 兼容旧数据:如果personincharge是用户名
# 审批员匹配:personincharge字段是用户名(非纯数字字符串)
person_query = Q(personincharge=user.username)
# 组合查询:部门匹配 OR 用户名匹配
# 如果用户有部门,使用部门匹配;否则只使用用户名匹配
# 组合查询:部门匹配 OR 审批员匹配
# 如果用户有部门,使用部门匹配;否则只使用审批员匹配
if user_department_ids_str:
query &= (department_query | person_query)
else:
# 如果用户没有部门,只匹配用户名(兼容旧数据)
# 如果用户没有部门,只匹配审批员(兼容旧数据)
query &= person_query
approvals = Approval.objects.filter(query).order_by('-id')

View File

@@ -4,6 +4,7 @@ from rest_framework import status
import json
import ast
from User.models import User, Approval
from User.utils import format_personincharge
from .models import PreFiling, ProjectRegistration, Bid, Case, Invoice, Caselog, SealApplication, Warehousing, \
RegisterPlatform, Announcement, LawyerFlie, Schedule, permission, role
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
@@ -208,7 +209,7 @@ class Project(APIView):
title=responsiblefor + "立项登记",
content=responsiblefor + "" + times + "办理立项登记,项目类型:" + type + ",合同编号:" + ContractNo + "描述:" + ",负责人:" + responsiblefor + ",收费情况:" + charge,
times=formatted_date,
personincharge=personincharge,
personincharge=format_personincharge(personincharge, is_department=False), # 审批员用户名
state='审核中',
type="立项登记",
user_id=pro.id
@@ -466,7 +467,7 @@ class BidRegistration(APIView):
title=ProjectName + "投标登记",
content="项目名称:" + ProjectName + ",申请日期:" + times,
times=formatted_date,
personincharge=personincharge,
personincharge=format_personincharge(personincharge, is_department=False) if personincharge else "", # 审批员用户名
state='审核中',
type="投标登记",
user_id=bib.id
@@ -578,7 +579,7 @@ class EditBid(APIView):
title=bid.ProjectName + "投标登记重新编辑",
content="项目名称:" + bid.ProjectName + ",申请日期:" + (times or bid.times),
times=formatted_date,
personincharge=personincharge,
personincharge=format_personincharge(personincharge, is_department=False) if personincharge else "", # 审批员用户名
state='审核中',
type="投标登记",
user_id=bid.id
@@ -645,7 +646,7 @@ class caseManagement(APIView):
title="案件管理信息提交",
content=times + "提交了一份案件信息,请审核",
times=times,
personincharge=personincharge,
personincharge=format_personincharge(personincharge, is_department=False) if personincharge else "", # 审批员用户名
state='审核中',
type="案件管理",
user_id=case_id.id
@@ -675,7 +676,7 @@ class caseManagement(APIView):
title="案件管理信息提交",
content=times + "提交了一份案件信息,更改了变更申请",
times=times,
personincharge=personincharge,
personincharge=format_personincharge(personincharge, is_department=False) if personincharge else "", # 审批员用户名
state='审核中',
type="案件管理",
user_id=case.id
@@ -827,7 +828,7 @@ class EditCase(APIView):
title="案件管理信息提交",
content=(times or case.times) + "提交了一份案件信息,更改了变更申请",
times=formatted_date,
personincharge=personincharge,
personincharge=format_personincharge(personincharge, is_department=False) if personincharge else "", # 审批员用户名
state='审核中',
type="案件管理",
user_id=case.id
@@ -1051,7 +1052,7 @@ class Application(APIView):
title=user.username + "申请用印",
content=user.username + "" + date_str + "申请用印,用印事由:" + Reason + ",盖章份数:" + seal_number + "盖着类型:" + seal_type,
times=date_str,
personincharge=personincharge,
personincharge=format_personincharge(personincharge, is_department=False), # 审批员用户名
state='审核中',
type="申请用印",
user_id=sea.id
@@ -1163,7 +1164,7 @@ class EditApplication(APIView):
title=app.username + "申请用印重新编辑",
content=app.username + "" + date_str + "申请用印,用印事由:" + (Reason or app.Reason) + ",盖章份数:" + (seal_number or app.seal_number) + "盖着类型:" + (seal_type or app.seal_type),
times=date_str,
personincharge=personincharge,
personincharge=format_personincharge(personincharge, is_department=False) if personincharge else "", # 审批员用户名
state='审核中',
type="申请用印",
user_id=app.id

View File

@@ -4,6 +4,7 @@ from rest_framework import status
import json
import ast
from User.models import User,Approval,Department
from User.utils import format_personincharge
import datetime
from .models import Invoice,Income,Accounts,Payment,Reimbursement,BonusChange
from utility.utility import flies
@@ -26,19 +27,22 @@ class UserRegister(APIView):
Dateofjoining = request.data.get('Dateofjoining')
position = request.data.get('position')
salary = request.data.get('salary')
approval_department = request.data.get('approval_department') # 审批部门ID
personincharge = request.data.get('personincharge') # 统一使用personincharge字段部门ID或审批员用户名
if not all([username, card, Dateofjoining, position, salary]):
return Response({'status': 'error', 'message': '缺少参数', 'code': 1}, status=status.HTTP_400_BAD_REQUEST)
# 审批部门是必填的
if not approval_department:
return Response({'status': 'error', 'message': '审批部门不能为空', 'code': 1}, status=status.HTTP_400_BAD_REQUEST)
# personincharge是必填的
if not personincharge:
return Response({'status': 'error', 'message': '审批部门或审批员不能为空', 'code': 1}, status=status.HTTP_400_BAD_REQUEST)
# 验证审批部门是否存在
try:
department = Department.objects.get(id=approval_department)
except (Department.DoesNotExist, ValueError):
return Response({'status': 'error', 'message': '审批部门不存在', 'code': 1}, status=status.HTTP_400_BAD_REQUEST)
# 判断是部门ID还是审批员用户名
from User.utils import is_department_id
if is_department_id(personincharge):
# 如果是部门ID验证部门是否存在
try:
department = Department.objects.get(id=int(personincharge))
except (Department.DoesNotExist, ValueError):
return Response({'status': 'error', 'message': '审批部门不存在', 'code': 1}, status=status.HTTP_400_BAD_REQUEST)
Dateofjoinings = datetime.datetime.strptime(Dateofjoining, "%Y-%m-%d")
user = User.objects.get(username=username)
@@ -52,16 +56,15 @@ class UserRegister(APIView):
today = datetime.datetime.now()
formatted_date = today.strftime("%Y-%m-%d")
# 将审批部门ID存储到personincharge字段字符串格式
# 这样该部门下的所有人员都能看到审批信息
# 统一使用personincharge字段统一规则:纯数字字符串=部门ID非纯数字字符串=审批员用户名
Approval.objects.create(
title=username+"入职财务登记",
content=username+""+Dateofjoining+"办理入职,身份证:"+card+",岗位:"+position+"薪资:"+salary,
times=formatted_date,
personincharge=str(approval_department), # 存储部门ID字符串格式
personincharge=format_personincharge(personincharge, is_department=is_department_id(personincharge)), # 自动判断是部门ID还是审批员
state='审核中',
type="入职财务登记",
user_id=str(user.id) # 确保user_id也是字符串格式
user_id=str(user.id)
)
user.save(update_fields=['card', 'salary', 'username', 'Dateofjoining', 'position'])
@@ -109,7 +112,7 @@ class issueAnInvoice(APIView):
title=username+"申请开票",
content=""+formatted_date+"的时候审核开发票",
times=formatted_date,
personincharge=personincharge,
personincharge=format_personincharge(personincharge, is_department=False), # 审批员用户名
state="审核中",
type="开票",
user_id = invoice.id
@@ -287,7 +290,7 @@ class confirm(APIView):
title=user.username + "提交收入确认",
content=user.username + "" + times + "提交了收入确认,合同编号:" + ContractNo + ",客户名称:" + CustomerID + "收入金额:" + amount,
times=date_string,
personincharge=personincharge,
personincharge=format_personincharge(personincharge, is_department=False), # 审批员用户名
state='审核中',
type="收入确认",
user_id=income.id
@@ -480,7 +483,7 @@ class loan(APIView):
title=user.username + "提交调账申请",
content=user.username + "" + times + "提交了调账申请,合同编号:" + ContractNo + ",客户名称:" + CustomerID + "收入金额:" + amount,
times=date_string,
personincharge=personincharge,
personincharge=format_personincharge(personincharge, is_department=False), # 审批员用户名
state='审核中',
type="调账申请",
user_id=acc.id
@@ -637,7 +640,7 @@ class PaymentRequest(APIView):
title=applicant + "提交付款申请",
content=applicant + "" + times + "提交了付款申请,付款理由:" + reason + ",付款金额:" + amount + ",付款日期:" + times+",收款人:"+payee+",银行卡:"+bankcard+",开户行:"+BankName,
times=date_string,
personincharge=personincharge,
personincharge=format_personincharge(personincharge, is_department=False), # 审批员用户名
state='审核中',
type="付款申请",
user_id=pay.id
@@ -796,7 +799,7 @@ class reimbursement(APIView):
title=person + "报销申请",
content=person + "" + times + "提交了报销申请,报销理由:" + reason + ",付款金额:" + amount + ",付款日期:" + times + ",费用说明:" + FeeDescription,
times=times,
personincharge=personincharge,
personincharge=format_personincharge(personincharge, is_department=False), # 审批员用户名
state='审核中',
type="报销申请",
user_id=reim.id
@@ -942,7 +945,7 @@ class Change(APIView):
title=username + "工资/奖金变更",
content=username + "" + now.strftime('%Y-%m-%d"') + "提交了工资/奖金变更,类型:" + type + ",调整说明:" + Instructions,
times=now.strftime("%Y-%m-%d"),
personincharge=personincharge,
personincharge=format_personincharge(personincharge, is_department=False), # 审批员用户名
state='审核中',
type="工资/奖金变更",
user_id=bonus.id
@@ -1112,11 +1115,26 @@ class UserDeparture(APIView):
# 创建离职审批记录
today = datetime.datetime.now()
formatted_date = today.strftime("%Y-%m-%d")
# 统一使用personincharge字段统一规则纯数字字符串=部门ID非纯数字字符串=审批员用户名)
if personincharge:
# 判断是部门ID还是审批员用户名
from User.utils import is_department_id
if is_department_id(personincharge):
# 如果是部门ID验证部门是否存在
try:
department = Department.objects.get(id=int(personincharge))
except (Department.DoesNotExist, ValueError):
return Response({'status': 'error', 'message': '审批部门不存在', 'code': 1}, status=status.HTTP_400_BAD_REQUEST)
personincharge_value = format_personincharge(personincharge, is_department=is_department_id(personincharge))
else:
personincharge_value = ""
Approval.objects.create(
title=username + "离职财务登记",
content=username + "" + Dateofdeparture + "办理离职登记",
times=formatted_date,
personincharge=personincharge or "",
personincharge=personincharge_value,
state='审核中',
type="离职财务登记",
user_id=str(user.id)