weaver-hrm-salary/src/com/engine/salary/service/impl/SalaryAcctCalculateServiceI...

268 lines
18 KiB
Java
Raw Normal View History

2022-04-11 20:17:47 +08:00
package com.engine.salary.service.impl;
import com.engine.common.util.ServiceUtil;
import com.engine.core.impl.Service;
2022-04-29 17:15:22 +08:00
import com.engine.salary.cache.SalaryCacheKey;
2022-04-11 20:17:47 +08:00
import com.engine.salary.constant.SalaryDefaultTenantConstant;
import com.engine.salary.constant.SalaryFormulaFieldConstant;
import com.engine.salary.entity.datacollection.AddUpDeduction;
import com.engine.salary.entity.datacollection.AddUpSituation;
import com.engine.salary.entity.datacollection.DataCollectionEmployee;
import com.engine.salary.entity.datacollection.dto.AttendQuoteDataDTO;
import com.engine.salary.entity.datacollection.po.OtherDeductionPO;
import com.engine.salary.entity.salaryacct.bo.*;
import com.engine.salary.entity.salaryacct.po.SalaryAcctEmployeePO;
import com.engine.salary.entity.salaryacct.po.SalaryAcctRecordPO;
import com.engine.salary.entity.salaryacct.po.SalaryAcctResultPO;
import com.engine.salary.entity.salaryacct.po.SalaryAcctResultTempPO;
import com.engine.salary.entity.salaryarchive.dto.SalaryArchiveDataDTO;
2022-04-27 09:43:24 +08:00
import com.engine.salary.entity.salaryformula.ExpressFormula;
import com.engine.salary.entity.salaryformula.po.FormulaVar;
2022-04-11 20:17:47 +08:00
import com.engine.salary.entity.salaryitem.po.SalaryItemPO;
import com.engine.salary.entity.salarysob.dto.SalarySobCycleDTO;
import com.engine.salary.entity.salarysob.po.SalarySobItemPO;
2022-04-28 15:02:11 +08:00
import com.engine.salary.enums.salaryformula.SalaryFormulaReferenceEnum;
2022-04-11 20:17:47 +08:00
import com.engine.salary.service.*;
import com.engine.salary.util.SalaryEntityUtil;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import weaver.hrm.User;
import java.time.Month;
import java.util.*;
import java.util.stream.Collectors;
/**
* 薪资核算-核算
* <p>Copyright: Copyright (c) 2022</p>
* <p>Company: 泛微软件</p>
*
* @author qiantao
* @version 1.0
**/
@Slf4j
public class SalaryAcctCalculateServiceImpl extends Service implements SalaryAcctCalculateService {
2022-04-15 13:54:47 +08:00
2022-04-11 20:17:47 +08:00
private SalaryAcctResultService getSalaryAcctResultService(User user) {
return (SalaryAcctResultService) ServiceUtil.getService(SalaryAcctResultServiceImpl.class, user);
}
private SalaryEmployeeService getSalaryEmployeeService(User user) {
return (SalaryEmployeeService) ServiceUtil.getService(SalaryEmployeeServiceImpl.class, user);
}
2022-04-15 13:54:47 +08:00
2022-04-11 20:17:47 +08:00
private SalaryArchiveService getSalaryArchiveService(User user) {
return (SalaryArchiveService) ServiceUtil.getService(SalaryArchiveServiceImpl.class, user);
}
2022-04-15 13:54:47 +08:00
2022-04-11 20:17:47 +08:00
private AddUpSituationService getAddUpSituationService(User user) {
return (AddUpSituationService) ServiceUtil.getService(AddUpSituationServiceImpl.class, user);
}
2022-04-15 13:54:47 +08:00
2022-04-11 20:17:47 +08:00
private AddUpDeductionService getAddUpDeductionService(User user) {
return (AddUpDeductionService) ServiceUtil.getService(AddUpDeductionServiceImpl.class, user);
}
2022-04-15 13:54:47 +08:00
2022-04-11 20:17:47 +08:00
private OtherDeductionService getOtherDeductionService(User user) {
return (OtherDeductionService) ServiceUtil.getService(OtherDeductionServiceImpl.class, user);
}
2022-04-15 13:54:47 +08:00
2022-04-27 17:20:33 +08:00
private SIAccountService getSIAccountService(User user) {
return (SIAccountService) ServiceUtil.getService(SIAccountServiceImpl.class, user);
}
2022-04-11 20:17:47 +08:00
private AttendQuoteDataService getAttendQuoteDataService(User user) {
return (AttendQuoteDataService) ServiceUtil.getService(AttendQuoteDataServiceImpl.class, user);
}
2022-04-15 13:54:47 +08:00
private FormulaRunService getFormulaRunService(User user) {
return (FormulaRunService) ServiceUtil.getService(FormulaRunServiceImpl.class, user);
}
2022-04-11 20:17:47 +08:00
private SalaryAcctResultTempService getSalaryAcctResultTempService(User user) {
return (SalaryAcctResultTempService) ServiceUtil.getService(SalaryAcctResultTempServiceImpl.class, user);
}
2022-04-15 13:54:47 +08:00
2022-04-29 17:15:22 +08:00
private SalaryAcctProgressService getSalaryAcctProgressService(User user) {
return (SalaryAcctProgressService) ServiceUtil.getService(SalaryAcctProgressServiceImpl.class, user);
}
2022-04-15 13:54:47 +08:00
2022-04-12 19:25:19 +08:00
private SalaryAcctEmployeeService getSalaryAcctEmployeeService(User user) {
return (SalaryAcctEmployeeService) ServiceUtil.getService(SalaryAcctEmployeeServiceImpl.class, user);
}
2022-04-11 20:17:47 +08:00
@Override
public void calculate(SalaryAcctCalculateBO salaryAcctCalculateBO, DataCollectionEmployee simpleEmployee) {
2022-05-08 12:34:05 +08:00
log.info("开始核算V3{}", salaryAcctCalculateBO);
2022-04-11 20:17:47 +08:00
Date now = new Date();
try {
// 数据库字段加密用
// 1、查询人员信息
List<Long> employeeIds = SalaryEntityUtil.properties(salaryAcctCalculateBO.getSalaryAcctEmployeePOS(), SalaryAcctEmployeePO::getEmployeeId, Collectors.toList());
List<DataCollectionEmployee> simpleEmployees = getSalaryEmployeeService(user).listByIds(employeeIds);
SalarySobCycleDTO salarySobCycleDTO = salaryAcctCalculateBO.getSalarySobCycleDTO();
// 2、查询薪资档案的数据
List<SalaryArchiveDataDTO> salaryArchiveData = getSalaryArchiveService(user).getSalaryArchiveData(salarySobCycleDTO.getSalaryCycle(), employeeIds);
// 3、查询往期累计情况(查询的是上个税款所属期的的累计情况)
List<AddUpSituation> addUpSituationPOS;
if (salarySobCycleDTO.getTaxCycle().getMonth() == Month.JANUARY) {
// 3.1、如果当前税款所属期是本年度第一个税款所属期,就不需要查询往期累计情况
addUpSituationPOS = Collections.emptyList();
} else {
addUpSituationPOS = getAddUpSituationService(user).getAddUpSituationList(salarySobCycleDTO.getTaxCycle().plusMonths(-1), employeeIds);
}
Long taxAgentId = salaryAcctCalculateBO.getSalarySobPO().getTaxAgentId();
2022-04-11 20:17:47 +08:00
// 4、查询累计专项附加扣除
List<AddUpDeduction> addUpDeductionPOS = getAddUpDeductionService(user).getAddUpDeductionList(salarySobCycleDTO.getTaxCycle(), employeeIds, taxAgentId);
2022-04-11 20:17:47 +08:00
// 5、查询其他免税扣除
List<OtherDeductionPO> otherDeductionPOS = getOtherDeductionService(user).getOtherDeductionList(salarySobCycleDTO.getTaxCycle(), employeeIds, taxAgentId);
2022-04-27 17:20:33 +08:00
//6、查询社保福利
List<Map<String, Object>> welfareData = getSIAccountService(user).welfareData(salarySobCycleDTO.getSocialSecurityCycle().toString(), employeeIds, taxAgentId);
2022-04-11 20:17:47 +08:00
// 7、查询考勤数据
List<AttendQuoteDataDTO> attendQuoteDataDTOS = getAttendQuoteDataService(user).getAttendQuoteData(salarySobCycleDTO.getSalaryMonth(), salarySobCycleDTO.getSalarySobId(), employeeIds);
// 8、查询薪资核算人员的薪资核算结果
Set<Long> salaryAcctEmployeeIds = SalaryEntityUtil.properties(salaryAcctCalculateBO.getSalaryAcctEmployeePOS(), SalaryAcctEmployeePO::getId);
List<SalaryAcctResultPO> salaryAcctResultPOS = getSalaryAcctResultService(user).listBySalaryAcctEmployeeIds(salaryAcctEmployeeIds);
// 9、查询相同税款所属期内涉及合并计税的其他薪资核算结果
Set<Long> otherSalaryAcctRecordIds = SalaryEntityUtil.properties(salaryAcctCalculateBO.getOtherSalaryAcctRecordPOS(), SalaryAcctRecordPO::getId);
List<SalaryAcctResultPO> otherSalaryAcctResultPOS = getSalaryAcctResultService(user).listBySalaryAcctRecordIdsAndEmployeeIds(otherSalaryAcctRecordIds, employeeIds);
Map<String, List<SalaryAcctResultPO>> otherSalaryAcctResultPOMap = SalaryEntityUtil.group2Map(otherSalaryAcctResultPOS, e -> e.getEmployeeId() + "_" + e.getTaxAgentId());
// 9.1、查询相同税款所属期内设计合并计税的其他薪资核算人员
2022-04-12 19:25:19 +08:00
List<SalaryAcctEmployeePO> otherSalaryAcctEmployeePOS = getSalaryAcctEmployeeService(user).listBySalaryAcctRecordIdsAndEmployeeIds(otherSalaryAcctRecordIds, employeeIds);
2022-04-11 20:17:47 +08:00
Map<String, List<SalaryAcctEmployeePO>> otherSalaryAcctEmployeePOMap = SalaryEntityUtil.group2Map(otherSalaryAcctEmployeePOS, salaryAcctEmployeePO -> salaryAcctEmployeePO.getEmployeeId() + "_" + salaryAcctEmployeePO.getTaxAgentId());
// 10、转换成公式编辑器中的变量
2022-04-29 14:17:31 +08:00
CalculateFormulaVarBO calculateFormulaVarBO = new CalculateFormulaVarBO(simpleEmployees, salaryArchiveData, addUpSituationPOS, addUpDeductionPOS, otherDeductionPOS, welfareData, attendQuoteDataDTOS, salaryAcctResultPOS);
2022-04-11 20:17:47 +08:00
Map<String, List<CalculateFormulaVarBO.FormulaVarValue>> formulaVarMap = calculateFormulaVarBO.convert2FormulaVar(salaryAcctCalculateBO);
// 本次薪资核算所用的薪资账套下的薪资项目
Map<Long, SalarySobItemPO> salaryItemIdKeySalarySobItemPOMap = SalaryEntityUtil.convert2Map(salaryAcctCalculateBO.getSalarySobItemPOS(), SalarySobItemPO::getSalaryItemId);
// 本次薪资核算所用的公式
Map<Long, ExpressFormula> expressFormulaMap = SalaryEntityUtil.convert2Map(salaryAcctCalculateBO.getExpressFormulas(), ExpressFormula::getId);
// 系统内的薪资项目
Map<Long, SalaryItemPO> salaryItemMap = SalaryEntityUtil.convert2Map(salaryAcctCalculateBO.getSalaryItemPOS(), SalaryItemPO::getId);
List<SalaryAcctResultTempPO> salaryAcctResultTempPOS = Lists.newArrayList();
// 开始核算
for (SalaryAcctEmployeePO salaryAcctEmployeePO : salaryAcctCalculateBO.getSalaryAcctEmployeePOS()) {
2022-04-29 14:17:31 +08:00
//1 获取当前薪资核算人员的公式中的变量的值
2022-04-11 20:17:47 +08:00
List<CalculateFormulaVarBO.FormulaVarValue> formulaVarValues = formulaVarMap.get(salaryAcctEmployeePO.getEmployeeId() + "_" + salaryAcctEmployeePO.getTaxAgentId());
2022-04-29 14:17:31 +08:00
//2人员信息
formulaVarValues.addAll(formulaVarMap.get(salaryAcctEmployeePO.getEmployeeId() + ""));
2022-04-11 20:17:47 +08:00
Map<String, String> formulaVarValueMap = SalaryEntityUtil.convert2Map(formulaVarValues, CalculateFormulaVarBO.FormulaVarValue::getFieldId, CalculateFormulaVarBO.FormulaVarValue::getFieldValue);
// 按照计算好的优先级计算薪资项目的值
for (List<Long> salaryItemIds : salaryAcctCalculateBO.getSalaryItemIdWithPriorityList()) {
// 同一运算优先级下的薪资项目逐个独立运算
for (Long salaryItemId : salaryItemIds) {
String resultValue;
SalaryItemPO salaryItemPO = salaryItemMap.get(salaryItemId);
ExpressFormula expressFormula;
// 如果薪资账套下重新定义了薪资项目的公式,则使用薪资账套下的公式,否则使用薪资项目本身的公式
if (salaryItemIdKeySalarySobItemPOMap.containsKey(salaryItemId)) {
SalarySobItemPO salarySobItemPO = salaryItemIdKeySalarySobItemPOMap.get(salaryItemId);
expressFormula = expressFormulaMap.get(salarySobItemPO.getFormulaId());
} else {
expressFormula = expressFormulaMap.get(salaryItemPO.getFormulaId());
}
2022-04-29 14:56:33 +08:00
if (Objects.nonNull(expressFormula)) {
2022-04-11 20:17:47 +08:00
// 运行公式
resultValue = runExpressFormula(expressFormula, formulaVarValueMap, simpleEmployee);
} else {
// 处理取值类型为“输入/导入”的薪资项目
2022-04-29 14:17:31 +08:00
String key = SalaryFormulaReferenceEnum.SALARY_ITEM.getValue() + SalaryFormulaFieldConstant.FIELD_ID_SEPARATOR + salaryItemPO.getCode();
2022-04-11 20:17:47 +08:00
resultValue = formulaVarValueMap.getOrDefault(key, StringUtils.EMPTY);
}
// 处理薪资档案
if (Objects.equals(salaryItemPO.getUseInEmployeeSalary(), NumberUtils.INTEGER_ONE)) {
2022-04-29 14:17:31 +08:00
String key = SalaryFormulaReferenceEnum.SALARY_ARCHIVES.getValue() + SalaryFormulaFieldConstant.FIELD_ID_SEPARATOR + salaryItemPO.getCode();
2022-04-11 20:17:47 +08:00
resultValue = formulaVarValueMap.getOrDefault(key, StringUtils.EMPTY);
}
// 处理合并计税
2022-04-29 14:17:31 +08:00
resultValue = handleConsolidatedTax(resultValue, salaryItemPO, salaryAcctCalculateBO, otherSalaryAcctEmployeePOMap.get(salaryAcctEmployeePO.getEmployeeId() + "_" + salaryAcctEmployeePO.getTaxAgentId()), otherSalaryAcctResultPOMap.get(salaryAcctEmployeePO.getEmployeeId() + "_" + salaryAcctEmployeePO.getTaxAgentId()));
2022-04-11 20:17:47 +08:00
// 处理小数点
resultValue = SalaryAcctFormulaBO.roundResultValue(resultValue, salaryItemPO);
// 将已经计算过的薪资项目的值转换成公式变量的值添加到集合中
2022-04-29 14:17:31 +08:00
String key = SalaryFormulaReferenceEnum.SALARY_ITEM.getValue() + SalaryFormulaFieldConstant.FIELD_ID_SEPARATOR + salaryItemPO.getCode();
2022-04-11 20:17:47 +08:00
formulaVarValueMap.put(key, resultValue);
// 值保存薪资账套下的薪资项目的核算结果
if (salaryItemIdKeySalarySobItemPOMap.containsKey(salaryItemId)) {
// 转换成薪资核算结果po
SalaryAcctResultTempPO salaryAcctResultTempPO = new SalaryAcctResultTempPO()
// .setId(IdGenerator.generate())
2022-04-29 14:17:31 +08:00
.setSalaryAcctRecordId(salaryAcctEmployeePO.getSalaryAcctRecordId()).setSalaryAcctEmpId(salaryAcctEmployeePO.getId()).setEmployeeId(salaryAcctEmployeePO.getEmployeeId()).setTaxAgentId(salaryAcctEmployeePO.getTaxAgentId()).setSalarySobId(salaryAcctEmployeePO.getSalarySobId()).setSalaryItemId(salaryItemPO.getId()).setResultValue(resultValue).setCalculateKey(salaryAcctCalculateBO.getCalculateKey()).setCreator((long) user.getUID()).setCreateTime(now).setUpdateTime(now).setTenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY).setDeleteType(0);
2022-04-11 20:17:47 +08:00
salaryAcctResultTempPOS.add(salaryAcctResultTempPO);
}
}
}
}
// 保存新的薪资核算结果(临时存储)
getSalaryAcctResultTempService(user).batchSave(salaryAcctResultTempPOS);
// 更新薪资核算进度
2022-04-29 17:15:22 +08:00
getSalaryAcctProgressService(user).getAndAddCalculatedQty(SalaryCacheKey.ACCT_PROGRESS + salaryAcctCalculateBO.getSalaryAcctRecordPO().getId(),
salaryAcctCalculateBO.getSalaryAcctEmployeePOS().size());
2022-04-11 20:17:47 +08:00
// 记录子线程执行结果
salaryAcctCalculateBO.getResults().add(new SalaryAcctCalculateBO.Result(true, StringUtils.EMPTY));
} catch (Exception e) {
2022-05-08 12:34:05 +08:00
log.error("薪资核算失败:{}", e.getMessage(), e);
2022-04-11 20:17:47 +08:00
salaryAcctCalculateBO.getResults().add(new SalaryAcctCalculateBO.Result(false, e.getMessage()));
} finally {
// 数据库字段加密用
// DSTenantKeyThreadVar.tenantKey.remove();
// 子线程执行完毕
salaryAcctCalculateBO.getChildMonitor().countDown();
}
}
/**
* 运行公式
*
* @param expressFormula
* @param formulaVarValueMap
* @return
*/
private String runExpressFormula(ExpressFormula expressFormula, Map<String, String> formulaVarValueMap, DataCollectionEmployee simpleEmployee) {
// 给公式中的变量填入值
2022-05-08 12:34:05 +08:00
try {
List<FormulaVar> formulaVars = ExpressFormulaBO.buildFormulaVar4Accounting(expressFormula, formulaVarValueMap);
Object run = getFormulaRunService(user).run(expressFormula, formulaVars, simpleEmployee);
return run == null ? StringUtils.EMPTY : run.toString();
} catch (Exception e) {
log.error("express execute fail ", e);
}
return StringUtils.EMPTY;
2022-04-11 20:17:47 +08:00
}
/**
* 处理合并计税
*
* @return
*/
private String handleConsolidatedTax(String resultValue, SalaryItemPO salaryItemPO, SalaryAcctCalculateBO salaryAcctCalculateBO, List<SalaryAcctEmployeePO> otherSalaryAcctEmployeePOS, List<SalaryAcctResultPO> otherSalaryAcctResultPOS) {
// 如果相同税款所属期内没有其他薪资核算人员,就不存在合并计税
if (CollectionUtils.isEmpty(otherSalaryAcctEmployeePOS)) {
return resultValue;
}
// 相同税款所属期内其他薪资核算记录
Set<Long> otherSalaryAcctRecordIds = SalaryEntityUtil.properties(otherSalaryAcctEmployeePOS, SalaryAcctEmployeePO::getSalaryAcctRecordId);
2022-04-29 14:17:31 +08:00
List<SalaryAcctRecordPO> otherSalaryAcctRecordPOS = salaryAcctCalculateBO.getOtherSalaryAcctRecordPOS().stream().filter(e -> otherSalaryAcctRecordIds.contains(e.getId())).collect(Collectors.toList());
2022-04-11 20:17:47 +08:00
// 相同税款所属期内,同一个个税扣缴义务人下的同一个人存在多次工资薪金类型的薪资核算记录,那么只有最早创建的薪资核算记录可以扣减如下数据:减除费用、专项扣除、专项附加扣除、其他扣除
// 根据薪资核算记录的创建时间判断是否需要做合并计税处理
boolean needConsolidatedTax = otherSalaryAcctRecordPOS.stream().anyMatch(salaryAcctRecordPO -> salaryAcctCalculateBO.getSalaryAcctRecordPO().getCreateTime().compareTo(salaryAcctRecordPO.getCreateTime()) > 0);
if (!needConsolidatedTax) {
return resultValue;
}
// 合并计税处理
return SalaryAcctConsolidatedTaxBO.handleConsolidatedTaxValue(resultValue, salaryItemPO, salaryAcctCalculateBO.getSalaryItemPOS(), otherSalaryAcctResultPOS);
}
}