fix: 退休检测改为按最新合同类型判断,排除劳务协议员工

detectRetirementRisks、updateEmployeesRetirementDays、日历退休日期查询
均改为 include 最新合同并在代码中判断 contractType,而非用 some 查询
(some 会匹配到历史 FIXED 合同导致劳务人员仍被检测)
This commit is contained in:
freedakgmail
2026-08-17 21:04:00 +08:00
parent 0ea069cd57
commit f2cb5e66b2
2 changed files with 23 additions and 12 deletions
+13 -8
View File
@@ -236,19 +236,24 @@ export async function confirmRetirementPolicy(orgId: string, policyId: string):
// 批量更新员工距退休天数(基于个人出生日期 individually 计算) // 批量更新员工距退休天数(基于个人出生日期 individually 计算)
export async function updateEmployeesRetirementDays(orgId: string): Promise<void> { export async function updateEmployeesRetirementDays(orgId: string): Promise<void> {
// 查询有正式劳动合同的员工 // 查询所有在职员工(含合同信息),在代码中按最新合同判断
const employees = await prisma.employee.findMany({ const employees = await prisma.employee.findMany({
where: { where: { orgId, status: 'ACTIVE', birthDate: { not: null } },
orgId, include: { contracts: { orderBy: { createdAt: 'desc' }, take: 1 } },
contracts: { some: { contractType: { in: ['FIXED', 'UNFIXED'] } } },
},
select: { id: true, birthDate: true, gender: true, femaleWorkerType: true },
}) })
for (const emp of employees) { for (const emp of employees) {
if (!emp.birthDate) continue const latestContractType = emp.contracts[0]?.contractType
if (!latestContractType || !['FIXED', 'UNFIXED'].includes(latestContractType)) {
// 非正式劳动合同:清除退休天数
await prisma.employee.update({
where: { id: emp.id },
data: { retirementDaysLeft: null },
})
continue
}
const gender = emp.gender || '男' const gender = emp.gender || '男'
const daysLeft = calcRetirementDaysLeft(emp.birthDate, gender, emp.femaleWorkerType) const daysLeft = calcRetirementDaysLeft(emp.birthDate!, gender, emp.femaleWorkerType)
if (daysLeft !== null) { if (daysLeft !== null) {
await prisma.employee.update({ await prisma.employee.update({
where: { id: emp.id }, where: { id: emp.id },
+10 -4
View File
@@ -488,13 +488,16 @@ export async function detectMonthlyTasks(orgId: string) {
*/ */
export async function detectRetirementRisks(orgId: string) { export async function detectRetirementRisks(orgId: string) {
const employees = await prisma.employee.findMany({ const employees = await prisma.employee.findMany({
where: { orgId, status: 'ACTIVE', birthDate: { not: null }, contracts: { some: { contractType: { in: ['FIXED', 'UNFIXED'] } } } }, where: { orgId, status: 'ACTIVE', birthDate: { not: null } },
select: { id: true, name: true, gender: true, birthDate: true, femaleWorkerType: true }, include: { contracts: { orderBy: { createdAt: 'desc' }, take: 1 } },
}) })
const risks: { employeeId: string; type: RiskType; level: RiskLevel; title: string; description: string; actionUrl: string }[] = [] const risks: { employeeId: string; type: RiskType; level: RiskLevel; title: string; description: string; actionUrl: string }[] = []
for (const emp of employees) { for (const emp of employees) {
// 只对最新合同为正式劳动合同的员工进行退休检测
const latestContractType = emp.contracts[0]?.contractType
if (!latestContractType || !['FIXED', 'UNFIXED'].includes(latestContractType)) continue
if (!emp.birthDate) continue if (!emp.birthDate) continue
const gender = emp.gender || '男' const gender = emp.gender || '男'
const fwt = emp.femaleWorkerType const fwt = emp.femaleWorkerType
@@ -1204,16 +1207,19 @@ export async function getMonthlyCalendar(orgId: string, month: string) {
}) })
} }
// 6. 退休日期(仅正式劳动合同员工) // 6. 退休日期(仅最新合同为正式劳动合同员工)
const retiringEmployees = await prisma.employee.findMany({ const retiringEmployees = await prisma.employee.findMany({
where: { where: {
orgId, orgId,
status: 'ACTIVE', status: 'ACTIVE',
retirementDaysLeft: { gte: 0, lte: 365 }, retirementDaysLeft: { gte: 0, lte: 365 },
contracts: { some: { contractType: { in: ['FIXED', 'UNFIXED'] } } },
}, },
include: { contracts: { orderBy: { createdAt: 'desc' }, take: 1 } },
}) })
for (const emp of retiringEmployees) { for (const emp of retiringEmployees) {
// 只对最新合同为正式劳动合同的员工显示退休日期
const latestContractType = emp.contracts[0]?.contractType
if (!latestContractType || !['FIXED', 'UNFIXED'].includes(latestContractType)) continue
if (emp.retirementDaysLeft !== null) { if (emp.retirementDaysLeft !== null) {
const retireDate = new Date() const retireDate = new Date()
retireDate.setDate(retireDate.getDate() + emp.retirementDaysLeft) retireDate.setDate(retireDate.getDate() + emp.retirementDaysLeft)