| | |
| | | |
| | | // 2. 遍历员工进行薪酬核算 |
| | | for (HrmEmployeeDO employee : employees) { |
| | | // employeeId 是员工ID,用于关联薪酬档案、社保档案、核算记录 |
| | | // employeeId 是员工档案主键(hrm_employee.id),仅用于关联排班 |
| | | // userId 是系统账号 ID(system_users.id),核算记录 / 薪酬档案 / 社保档案 |
| | | // 全部按它关联 —— 员工登录后查「我的薪资」用的也是这个 ID |
| | | Long employeeId = employee.getId(); |
| | | // 2.1 检查该员工当月是否已存在核算记录(按员工ID查询) |
| | | List<HrmSalaryCalculationDO> existCalculations = salaryCalculationMapper.selectListByUserIdAndPeriod(employeeId, period); |
| | | Long userId = employee.getUserId(); |
| | | if (userId == null) { |
| | | log.warn("[calculateSalary] 员工({}) 未关联系统账号,跳过核算", employee.getName()); |
| | | continue; |
| | | } |
| | | // 2.1 检查该员工当月是否已存在核算记录(按系统账号 ID 查询) |
| | | List<HrmSalaryCalculationDO> existCalculations = salaryCalculationMapper.selectListByUserIdAndPeriod(userId, period); |
| | | if (CollUtil.isNotEmpty(existCalculations)) { |
| | | // 如果存在多条记录,提示异常数据 |
| | | if (existCalculations.size() > 1) { |
| | |
| | | } |
| | | } |
| | | |
| | | // 2.2 获取员工薪酬档案(按员工ID查询) |
| | | HrmEmployeeSalaryDO employeeSalary = employeeSalaryMapper.selectLatestByUserId(employeeId); |
| | | // 2.2 获取员工薪酬档案(按系统账号 ID 查询) |
| | | HrmEmployeeSalaryDO employeeSalary = employeeSalaryMapper.selectLatestByUserId(userId); |
| | | if (employeeSalary == null) { |
| | | // 如果没有薪酬档案,使用员工表中的基本工资创建默认数据 |
| | | employeeSalary = buildDefaultEmployeeSalary(employeeId, employee); |
| | | employeeSalary = buildDefaultEmployeeSalary(userId, employee); |
| | | } |
| | | |
| | | // 2.3 获取员工社保公积金档案(按员工ID查询,取最新生效的) |
| | | HrmEmployeeSocialSecurityDO socialSecurity = employeeSocialSecurityMapper.selectLatestByUserId(employeeId); |
| | | // 2.3 获取员工社保公积金档案(按系统账号 ID 查询,取最新生效的) |
| | | HrmEmployeeSocialSecurityDO socialSecurity = employeeSocialSecurityMapper.selectLatestByUserId(userId); |
| | | // 2.4 如果有社保档案,获取绑定的方案 |
| | | HrmSocialSecuritySchemeDO scheme = null; |
| | | if (socialSecurity != null && socialSecurity.getSocialSecuritySchemeId() != null) { |
| | |
| | | } |
| | | |
| | | // 2.5 构建核算记录(包含扣除计算,通过方案获取比例) |
| | | HrmSalaryCalculationDO calculation = buildSalaryCalculation(period, employeeId, employee, employeeSalary, socialSecurity, scheme, effectiveDate); |
| | | HrmSalaryCalculationDO calculation = buildSalaryCalculation(period, userId, employee, employeeSalary, socialSecurity, scheme, effectiveDate); |
| | | |
| | | // 2.6 保存:首次生成单号;已有待确认记录则保留单号并覆盖重算结果 |
| | | if (CollUtil.isNotEmpty(existCalculations)) { |
| | |
| | | |
| | | /** |
| | | * 构建默认的员工薪酬档案。基本工资统一取员工档案,绩效工资默认不自动生成。 |
| | | * |
| | | * @param userId 系统账号 ID(system_users.id),不是 hrm_employee.id |
| | | */ |
| | | private HrmEmployeeSalaryDO buildDefaultEmployeeSalary(Long employeeId, HrmEmployeeDO employee) { |
| | | private HrmEmployeeSalaryDO buildDefaultEmployeeSalary(Long userId, HrmEmployeeDO employee) { |
| | | BigDecimal baseSalary = employee.getBaseSalary() != null ? employee.getBaseSalary() : BigDecimal.ZERO; |
| | | return HrmEmployeeSalaryDO.builder() |
| | | .userId(employeeId) // 使用员工ID |
| | | .userId(userId) |
| | | .salaryStructureId(employee.getSalaryStructureId()) |
| | | .baseSalary(baseSalary) |
| | | .performanceSalary(BigDecimal.ZERO) |
| | |
| | | /** |
| | | * 构建薪酬核算记录(包含社保、公积金、个税、请假扣除计算) |
| | | * 通过员工社保档案绑定的方案获取缴纳比例 |
| | | * |
| | | * @param userId 系统账号 ID(system_users.id),写入核算记录的 user_id |
| | | */ |
| | | private HrmSalaryCalculationDO buildSalaryCalculation(String period, Long employeeId, HrmEmployeeDO employee, |
| | | private HrmSalaryCalculationDO buildSalaryCalculation(String period, Long userId, HrmEmployeeDO employee, |
| | | HrmEmployeeSalaryDO employeeSalary, |
| | | HrmEmployeeSocialSecurityDO socialSecurity, |
| | | HrmSocialSecuritySchemeDO scheme, |
| | |
| | | BigDecimal overtimePay = overtimeSalary.pay(); |
| | | |
| | | // 按排班累计出勤天数与排班总工时(无排班时出勤天数回退默认 22) |
| | | ScheduleStat scheduleStat = calculateScheduleStat(employeeId, period); |
| | | // 注意:排班表按员工档案 ID(hrm_employee.id)关联,与核算记录的 userId 不同 |
| | | ScheduleStat scheduleStat = calculateScheduleStat(employee.getId(), period); |
| | | Integer workDays = scheduleStat.scheduledDays() > 0 ? scheduleStat.scheduledDays() : 22; |
| | | BigDecimal totalWorkHours = scheduleStat.totalWorkHours(); |
| | | |
| | |
| | | |
| | | return HrmSalaryCalculationDO.builder() |
| | | .period(period) |
| | | .userId(employeeId) // 使用员工ID |
| | | .userId(userId) |
| | | .deptId(employee.getDeptId()) |
| | | .baseSalary(baseSalary) |
| | | .performanceSalary(performanceSalary) |
| | |
| | | // 2. 遍历员工实时计算薪酬 |
| | | List<HrmSalaryCalculationDO> resultList = new java.util.ArrayList<>(); |
| | | for (HrmEmployeeDO employee : employees) { |
| | | Long employeeId = employee.getId(); |
| | | |
| | | // 2.1 获取员工薪酬档案(按员工ID查询) |
| | | HrmEmployeeSalaryDO employeeSalary = employeeSalaryMapper.selectLatestByUserId(employeeId); |
| | | if (employeeSalary == null) { |
| | | // 如果没有薪酬档案,使用员工表中的基本工资创建默认数据 |
| | | employeeSalary = buildDefaultEmployeeSalary(employeeId, employee); |
| | | // userId 是系统账号 ID(system_users.id),薪酬档案 / 社保档案按它关联; |
| | | // 排班表则按 hrm_employee.id 关联,两者不同 |
| | | Long userId = employee.getUserId(); |
| | | if (userId == null) { |
| | | log.warn("[previewSalaryCalculation] 员工({}) 未关联系统账号,跳过", employee.getName()); |
| | | continue; |
| | | } |
| | | |
| | | // 2.2 获取员工社保公积金档案(按员工ID查询,取最新生效的) |
| | | HrmEmployeeSocialSecurityDO socialSecurity = employeeSocialSecurityMapper.selectLatestByUserId(employeeId); |
| | | // 2.1 获取员工薪酬档案(按系统账号 ID 查询) |
| | | HrmEmployeeSalaryDO employeeSalary = employeeSalaryMapper.selectLatestByUserId(userId); |
| | | if (employeeSalary == null) { |
| | | // 如果没有薪酬档案,使用员工表中的基本工资创建默认数据 |
| | | employeeSalary = buildDefaultEmployeeSalary(userId, employee); |
| | | } |
| | | |
| | | // 2.2 获取员工社保公积金档案(按系统账号 ID 查询,取最新生效的) |
| | | HrmEmployeeSocialSecurityDO socialSecurity = employeeSocialSecurityMapper.selectLatestByUserId(userId); |
| | | // 2.3 如果有社保档案,获取绑定的方案 |
| | | HrmSocialSecuritySchemeDO scheme = null; |
| | | if (socialSecurity != null && socialSecurity.getSocialSecuritySchemeId() != null) { |
| | |
| | | } |
| | | |
| | | // 2.4 构建核算记录(实时计算,不存储) |
| | | HrmSalaryCalculationDO calculation = buildSalaryCalculation(period, employeeId, employee, employeeSalary, socialSecurity, scheme, effectiveDate); |
| | | HrmSalaryCalculationDO calculation = buildSalaryCalculation(period, userId, employee, employeeSalary, socialSecurity, scheme, effectiveDate); |
| | | resultList.add(calculation); |
| | | } |
| | | return resultList; |