diff --git a/User/models.py b/User/models.py index 3786064..0e62823 100644 --- a/User/models.py +++ b/User/models.py @@ -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 diff --git a/User/utils.py b/User/utils.py new file mode 100644 index 0000000..650c903 --- /dev/null +++ b/User/utils.py @@ -0,0 +1,62 @@ +""" +审批相关的工具函数 +""" + +def is_department_id(value): + """ + 判断personincharge字段的值是部门ID还是审批员用户名 + + 统一规则: + - 如果是纯数字字符串(如 "1", "2", "123"),表示部门ID + - 如果包含非数字字符(如 "张三", "李四"),表示审批员用户名 + + Args: + value: personincharge字段的值(字符串) + + Returns: + bool: True表示是部门ID,False表示是审批员用户名 + + 示例: + >>> 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() + diff --git a/User/views.py b/User/views.py index 793286a..c1eb668 100644 --- a/User/views.py +++ b/User/views.py @@ -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') diff --git a/business/views.py b/business/views.py index 3c5b484..5dced75 100644 --- a/business/views.py +++ b/business/views.py @@ -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 diff --git a/finance/views.py b/finance/views.py index d54bf3c..a91fd38 100644 --- a/finance/views.py +++ b/finance/views.py @@ -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)