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;
|
2022-12-02 17:35:14 +08:00
|
|
|
|
import com.engine.salary.entity.salarysob.po.SalarySobBackItemPO;
|
2022-04-11 20:17:47 +08:00
|
|
|
|
import com.engine.salary.entity.salarysob.po.SalarySobItemPO;
|
2022-04-28 15:02:11 +08:00
|
|
|
|
import com.engine.salary.enums.salaryformula.SalaryFormulaReferenceEnum;
|
2023-05-15 09:28:22 +08:00
|
|
|
|
import com.engine.salary.formlua.entity.standard.ExcelResult;
|
2022-04-11 20:17:47 +08:00
|
|
|
|
import com.engine.salary.service.*;
|
2023-07-04 09:32:12 +08:00
|
|
|
|
import com.engine.salary.sys.enums.TaxDeclarationFunctionEnum;
|
2022-04-11 20:17:47 +08:00
|
|
|
|
import com.engine.salary.util.SalaryEntityUtil;
|
|
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
import org.apache.commons.collections4.CollectionUtils;
|
2022-11-17 19:04:42 +08:00
|
|
|
|
import org.apache.commons.collections4.ListUtils;
|
2022-11-17 09:28:40 +08:00
|
|
|
|
import org.apache.commons.collections4.MapUtils;
|
2022-04-11 20:17:47 +08:00
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
|
import org.apache.commons.lang3.math.NumberUtils;
|
2023-09-26 14:29:12 +08:00
|
|
|
|
import org.springframework.util.StopWatch;
|
2023-08-31 15:31:34 +08:00
|
|
|
|
import weaver.general.BaseBean;
|
2022-04-11 20:17:47 +08:00
|
|
|
|
import weaver.hrm.User;
|
2023-05-16 18:01:38 +08:00
|
|
|
|
import weaver.wechat.util.Utils;
|
2022-04-11 20:17:47 +08:00
|
|
|
|
|
|
|
|
|
|
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 {
|
2023-08-31 15:31:34 +08:00
|
|
|
|
BaseBean bb = new BaseBean();
|
|
|
|
|
|
//公式运行时间超时提醒阈值
|
|
|
|
|
|
private final String formulaRunOvertimeThreshold = bb.getPropValue("hrmSalaryCustom", "formulaRunOvertimeThreshold");
|
2022-04-15 13:54:47 +08:00
|
|
|
|
|
2022-04-11 20:17:47 +08:00
|
|
|
|
private SalaryAcctResultService getSalaryAcctResultService(User user) {
|
2023-03-06 17:57:20 +08:00
|
|
|
|
return ServiceUtil.getService(SalaryAcctResultServiceImpl.class, user);
|
2022-04-11 20:17:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private SalaryEmployeeService getSalaryEmployeeService(User user) {
|
2023-03-06 17:57:20 +08:00
|
|
|
|
return ServiceUtil.getService(SalaryEmployeeServiceImpl.class, user);
|
2022-04-11 20:17:47 +08:00
|
|
|
|
}
|
2022-04-15 13:54:47 +08:00
|
|
|
|
|
2022-04-11 20:17:47 +08:00
|
|
|
|
|
|
|
|
|
|
private SalaryArchiveService getSalaryArchiveService(User user) {
|
2023-03-06 17:57:20 +08:00
|
|
|
|
return ServiceUtil.getService(SalaryArchiveServiceImpl.class, user);
|
2022-04-11 20:17:47 +08:00
|
|
|
|
}
|
2022-04-15 13:54:47 +08:00
|
|
|
|
|
2022-04-11 20:17:47 +08:00
|
|
|
|
|
|
|
|
|
|
private AddUpSituationService getAddUpSituationService(User user) {
|
2023-03-06 17:57:20 +08:00
|
|
|
|
return ServiceUtil.getService(AddUpSituationServiceImpl.class, user);
|
2022-04-11 20:17:47 +08:00
|
|
|
|
}
|
2022-04-15 13:54:47 +08:00
|
|
|
|
|
2022-04-11 20:17:47 +08:00
|
|
|
|
|
|
|
|
|
|
private AddUpDeductionService getAddUpDeductionService(User user) {
|
2023-03-06 17:57:20 +08:00
|
|
|
|
return ServiceUtil.getService(AddUpDeductionServiceImpl.class, user);
|
2022-04-11 20:17:47 +08:00
|
|
|
|
}
|
2022-04-15 13:54:47 +08:00
|
|
|
|
|
2022-04-11 20:17:47 +08:00
|
|
|
|
|
|
|
|
|
|
private OtherDeductionService getOtherDeductionService(User user) {
|
2023-03-06 17:57:20 +08:00
|
|
|
|
return ServiceUtil.getService(OtherDeductionServiceImpl.class, user);
|
2022-04-11 20:17:47 +08:00
|
|
|
|
}
|
2022-04-15 13:54:47 +08:00
|
|
|
|
|
2022-04-27 17:20:33 +08:00
|
|
|
|
private SIAccountService getSIAccountService(User user) {
|
2023-03-06 17:57:20 +08:00
|
|
|
|
return ServiceUtil.getService(SIAccountServiceImpl.class, user);
|
2022-04-27 17:20:33 +08:00
|
|
|
|
}
|
2022-04-11 20:17:47 +08:00
|
|
|
|
|
|
|
|
|
|
private AttendQuoteDataService getAttendQuoteDataService(User user) {
|
2023-03-06 17:57:20 +08:00
|
|
|
|
return ServiceUtil.getService(AttendQuoteDataServiceImpl.class, user);
|
2022-04-11 20:17:47 +08:00
|
|
|
|
}
|
2022-04-15 13:54:47 +08:00
|
|
|
|
|
|
|
|
|
|
private FormulaRunService getFormulaRunService(User user) {
|
2023-03-06 17:57:20 +08:00
|
|
|
|
return ServiceUtil.getService(FormulaRunServiceImpl.class, user);
|
2022-04-15 13:54:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-04-11 20:17:47 +08:00
|
|
|
|
|
|
|
|
|
|
private SalaryAcctResultTempService getSalaryAcctResultTempService(User user) {
|
2023-03-06 17:57:20 +08:00
|
|
|
|
return ServiceUtil.getService(SalaryAcctResultTempServiceImpl.class, user);
|
2022-04-11 20:17:47 +08:00
|
|
|
|
}
|
2022-04-15 13:54:47 +08:00
|
|
|
|
|
2023-06-06 10:30:49 +08:00
|
|
|
|
private ProgressService getSalaryAcctProgressService(User user) {
|
|
|
|
|
|
return (ProgressService) ServiceUtil.getService(ProgressServiceImpl.class, user);
|
2022-04-29 17:15:22 +08:00
|
|
|
|
}
|
2022-04-15 13:54:47 +08:00
|
|
|
|
|
2022-04-12 19:25:19 +08:00
|
|
|
|
private SalaryAcctEmployeeService getSalaryAcctEmployeeService(User user) {
|
2023-03-06 17:57:20 +08:00
|
|
|
|
return ServiceUtil.getService(SalaryAcctEmployeeServiceImpl.class, user);
|
2022-04-12 19:25:19 +08:00
|
|
|
|
}
|
2022-04-11 20:17:47 +08:00
|
|
|
|
|
|
|
|
|
|
@Override
|
2022-12-02 17:35:14 +08:00
|
|
|
|
public void calculate(SalaryAcctCalculateBO salaryAcctCalculateBO, DataCollectionEmployee simpleEmployee, List<SalarySobBackItemPO> salarySobBackItems) {
|
2024-05-16 15:56:49 +08:00
|
|
|
|
StopWatch sw = new StopWatch("核算耗时明细,id:"+salaryAcctCalculateBO.getSalaryAcctRecordPO().getId()+"");
|
2022-04-11 20:17:47 +08:00
|
|
|
|
Date now = new Date();
|
|
|
|
|
|
try {
|
|
|
|
|
|
// 数据库字段加密用
|
|
|
|
|
|
// 1、查询人员信息
|
2024-05-16 15:56:49 +08:00
|
|
|
|
sw.start("查询人员信息");
|
2022-04-11 20:17:47 +08:00
|
|
|
|
List<Long> employeeIds = SalaryEntityUtil.properties(salaryAcctCalculateBO.getSalaryAcctEmployeePOS(), SalaryAcctEmployeePO::getEmployeeId, Collectors.toList());
|
2023-03-06 17:57:20 +08:00
|
|
|
|
List<DataCollectionEmployee> simpleEmployees = getSalaryEmployeeService(user).getEmployeeByIdsAll(employeeIds);
|
2022-04-11 20:17:47 +08:00
|
|
|
|
SalarySobCycleDTO salarySobCycleDTO = salaryAcctCalculateBO.getSalarySobCycleDTO();
|
2022-08-09 16:45:22 +08:00
|
|
|
|
Long taxAgentId = salaryAcctCalculateBO.getSalarySobPO().getTaxAgentId();
|
2023-09-26 14:29:12 +08:00
|
|
|
|
sw.stop();
|
2022-04-11 20:17:47 +08:00
|
|
|
|
// 2、查询薪资档案的数据
|
2024-05-16 15:56:49 +08:00
|
|
|
|
sw.start("查询薪资档案的数据");
|
2022-08-09 16:45:22 +08:00
|
|
|
|
List<SalaryArchiveDataDTO> salaryArchiveData = getSalaryArchiveService(user).getSalaryArchiveData(salarySobCycleDTO.getSalaryCycle(), employeeIds, taxAgentId);
|
2023-09-26 14:29:12 +08:00
|
|
|
|
sw.stop();
|
2022-04-11 20:17:47 +08:00
|
|
|
|
// 3、查询往期累计情况(查询的是上个税款所属期的的累计情况)
|
2024-05-16 15:56:49 +08:00
|
|
|
|
sw.start("查询往期累计情况");
|
2022-04-11 20:17:47 +08:00
|
|
|
|
List<AddUpSituation> addUpSituationPOS;
|
|
|
|
|
|
if (salarySobCycleDTO.getTaxCycle().getMonth() == Month.JANUARY) {
|
|
|
|
|
|
// 3.1、如果当前税款所属期是本年度第一个税款所属期,就不需要查询往期累计情况
|
|
|
|
|
|
addUpSituationPOS = Collections.emptyList();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
addUpSituationPOS = getAddUpSituationService(user).getAddUpSituationList(salarySobCycleDTO.getTaxCycle().plusMonths(-1), employeeIds);
|
|
|
|
|
|
}
|
2023-09-26 14:29:12 +08:00
|
|
|
|
sw.stop();
|
2022-04-11 20:17:47 +08:00
|
|
|
|
// 4、查询累计专项附加扣除
|
2024-05-16 15:56:49 +08:00
|
|
|
|
sw.start("查询累计专项附加扣除");
|
2022-06-29 17:38:00 +08:00
|
|
|
|
List<AddUpDeduction> addUpDeductionPOS = getAddUpDeductionService(user).getAddUpDeductionList(salarySobCycleDTO.getTaxCycle(), employeeIds, taxAgentId);
|
2023-09-26 14:29:12 +08:00
|
|
|
|
sw.stop();
|
2022-04-11 20:17:47 +08:00
|
|
|
|
// 5、查询其他免税扣除
|
2024-05-16 15:56:49 +08:00
|
|
|
|
sw.start("查询其他免税扣除");
|
2022-06-29 17:38:00 +08:00
|
|
|
|
List<OtherDeductionPO> otherDeductionPOS = getOtherDeductionService(user).getOtherDeductionList(salarySobCycleDTO.getTaxCycle(), employeeIds, taxAgentId);
|
2023-09-26 14:29:12 +08:00
|
|
|
|
sw.stop();
|
2022-04-27 17:20:33 +08:00
|
|
|
|
//6、查询社保福利
|
2024-05-16 15:56:49 +08:00
|
|
|
|
sw.start("查询社保福利");
|
2022-06-29 17:38:00 +08:00
|
|
|
|
List<Map<String, Object>> welfareData = getSIAccountService(user).welfareData(salarySobCycleDTO.getSocialSecurityCycle().toString(), employeeIds, taxAgentId);
|
2023-09-26 14:29:12 +08:00
|
|
|
|
sw.stop();
|
2022-04-11 20:17:47 +08:00
|
|
|
|
// 7、查询考勤数据
|
2024-05-16 15:56:49 +08:00
|
|
|
|
sw.start("查询考勤数据");
|
2022-04-11 20:17:47 +08:00
|
|
|
|
List<AttendQuoteDataDTO> attendQuoteDataDTOS = getAttendQuoteDataService(user).getAttendQuoteData(salarySobCycleDTO.getSalaryMonth(), salarySobCycleDTO.getSalarySobId(), employeeIds);
|
2023-09-26 14:29:12 +08:00
|
|
|
|
sw.stop();
|
2022-04-11 20:17:47 +08:00
|
|
|
|
// 8、查询薪资核算人员的薪资核算结果
|
2024-05-16 15:56:49 +08:00
|
|
|
|
sw.start("查询薪资核算人员的薪资核算结果");
|
2023-05-15 09:28:22 +08:00
|
|
|
|
List<Long> salaryAcctEmployeeIds = SalaryEntityUtil.properties(salaryAcctCalculateBO.getSalaryAcctEmployeePOS(), SalaryAcctEmployeePO::getId, Collectors.toList());
|
2022-04-11 20:17:47 +08:00
|
|
|
|
List<SalaryAcctResultPO> salaryAcctResultPOS = getSalaryAcctResultService(user).listBySalaryAcctEmployeeIds(salaryAcctEmployeeIds);
|
2023-09-26 14:29:12 +08:00
|
|
|
|
sw.stop();
|
2022-12-02 17:35:14 +08:00
|
|
|
|
// 薪资回算时回算前的核算结果 (没有回算项)
|
2024-05-16 15:56:49 +08:00
|
|
|
|
sw.start("查询薪资回算时回算前的核算结果");
|
2022-12-02 17:35:14 +08:00
|
|
|
|
Map<String, List<SalaryAcctResultPO>> collect = salaryAcctResultPOS.stream().collect(Collectors.groupingBy(k -> k.getEmployeeId() + "-" + k.getTaxAgentId() + "-" + k.getSalaryItemId()));
|
|
|
|
|
|
Map<String, String> salaryAcctResultPOMap = new HashMap<>();
|
2023-03-06 17:57:20 +08:00
|
|
|
|
for (Map.Entry<String, List<SalaryAcctResultPO>> et : collect.entrySet()) {
|
|
|
|
|
|
salaryAcctResultPOMap.put(et.getKey(), et.getValue().get(0).getOriginResultValue());
|
2022-12-02 17:35:14 +08:00
|
|
|
|
}
|
2022-11-17 09:28:40 +08:00
|
|
|
|
//核算锁定的值
|
2022-11-17 18:53:22 +08:00
|
|
|
|
Map<String, SalaryAcctResultPO> salaryAcctLockResultPOS = MapUtils.emptyIfNull(salaryAcctCalculateBO.getSalaryAcctLockResultPOS());
|
2022-11-17 19:04:42 +08:00
|
|
|
|
List<Long> lockSalaryItemIds = ListUtils.emptyIfNull(salaryAcctCalculateBO.getLockSalaryItemIds());
|
2023-09-26 14:29:12 +08:00
|
|
|
|
sw.stop();
|
2022-04-11 20:17:47 +08:00
|
|
|
|
// 9、查询相同税款所属期内涉及合并计税的其他薪资核算结果
|
2024-05-16 15:56:49 +08:00
|
|
|
|
sw.start("查询相同税款所属期内涉及合并计税的其他薪资核算结果");
|
2022-04-11 20:17:47 +08:00
|
|
|
|
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());
|
2023-09-26 14:29:12 +08:00
|
|
|
|
sw.stop();
|
2022-04-11 20:17:47 +08:00
|
|
|
|
// 9.1、查询相同税款所属期内设计合并计税的其他薪资核算人员
|
2024-05-16 15:56:49 +08:00
|
|
|
|
sw.start("查询相同税款所属期内设计合并计税的其他薪资核算人员");
|
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());
|
2023-09-26 14:29:12 +08:00
|
|
|
|
sw.stop();
|
2022-04-11 20:17:47 +08:00
|
|
|
|
// 10、转换成公式编辑器中的变量
|
2024-05-16 15:56:49 +08:00
|
|
|
|
sw.start("转换成公式编辑器中的变量");
|
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);
|
2023-09-26 14:29:12 +08:00
|
|
|
|
sw.stop();
|
2024-05-16 15:56:49 +08:00
|
|
|
|
sw.start("数据结构准备");
|
2022-04-11 20:17:47 +08:00
|
|
|
|
// 本次薪资核算所用的薪资账套下的薪资项目
|
|
|
|
|
|
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);
|
2022-12-13 16:39:39 +08:00
|
|
|
|
// 获取薪资回算的薪资项目ID
|
|
|
|
|
|
Set<Long> salarySobBackItemIds = SalaryEntityUtil.properties(salarySobBackItems, SalarySobBackItemPO::getSalaryItemId);
|
|
|
|
|
|
Map<Long, SalarySobBackItemPO> salarySobBackItemMap = SalaryEntityUtil.convert2Map(salarySobBackItems, SalarySobBackItemPO::getSalaryItemId);
|
2022-04-11 20:17:47 +08:00
|
|
|
|
List<SalaryAcctResultTempPO> salaryAcctResultTempPOS = Lists.newArrayList();
|
2023-09-26 14:29:12 +08:00
|
|
|
|
sw.stop();
|
2022-04-11 20:17:47 +08:00
|
|
|
|
// 开始核算
|
2024-05-16 15:56:49 +08:00
|
|
|
|
sw.start("核算耗时");
|
2023-05-23 15:15:55 +08:00
|
|
|
|
StringBuffer noticeMsg = new StringBuffer();
|
2022-04-11 20:17:47 +08:00
|
|
|
|
for (SalaryAcctEmployeePO salaryAcctEmployeePO : salaryAcctCalculateBO.getSalaryAcctEmployeePOS()) {
|
2022-11-17 18:53:22 +08:00
|
|
|
|
Long salaryAcctEmployeePOId = salaryAcctEmployeePO.getId();
|
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-12-02 17:35:14 +08:00
|
|
|
|
//2 人员信息
|
2023-05-16 18:01:38 +08:00
|
|
|
|
List<CalculateFormulaVarBO.FormulaVarValue> empInfo = formulaVarMap.get(salaryAcctEmployeePO.getEmployeeId() + "");
|
|
|
|
|
|
formulaVarValues.addAll(empInfo);
|
2022-04-11 20:17:47 +08:00
|
|
|
|
Map<String, String> formulaVarValueMap = SalaryEntityUtil.convert2Map(formulaVarValues, CalculateFormulaVarBO.FormulaVarValue::getFieldId, CalculateFormulaVarBO.FormulaVarValue::getFieldValue);
|
|
|
|
|
|
// 按照计算好的优先级计算薪资项目的值
|
2023-09-07 15:15:59 +08:00
|
|
|
|
for (Long salaryItemId : salaryAcctCalculateBO.getSalaryItemIdWithPriorityList()) {
|
2022-04-11 20:17:47 +08:00
|
|
|
|
// 同一运算优先级下的薪资项目逐个独立运算
|
2023-09-07 15:15:59 +08:00
|
|
|
|
String resultValue;
|
|
|
|
|
|
SalaryItemPO salaryItemPO = salaryItemMap.get(salaryItemId);
|
|
|
|
|
|
ExpressFormula expressFormula;
|
|
|
|
|
|
if (salarySobBackItemMap.containsKey(salaryItemId)) {
|
|
|
|
|
|
// 如果薪资账套的回算项目中重新定义了回算项目公式,则使用薪资账套下的公式
|
|
|
|
|
|
SalarySobBackItemPO salarySobBackItemPO = salarySobBackItemMap.get(salaryItemId);
|
|
|
|
|
|
expressFormula = expressFormulaMap.get(salarySobBackItemPO.getFormulaId());
|
|
|
|
|
|
} else if (salaryItemIdKeySalarySobItemPOMap.containsKey(salaryItemId)) {
|
|
|
|
|
|
// 如果薪资账套下重新定义了薪资项目的公式,则使用薪资账套下的公式,否则使用薪资项目本身的公式
|
|
|
|
|
|
SalarySobItemPO salarySobItemPO = salaryItemIdKeySalarySobItemPOMap.get(salaryItemId);
|
|
|
|
|
|
expressFormula = expressFormulaMap.get(salarySobItemPO.getFormulaId());
|
|
|
|
|
|
} else {
|
|
|
|
|
|
expressFormula = expressFormulaMap.get(salaryItemPO.getFormulaId());
|
|
|
|
|
|
}
|
|
|
|
|
|
if (Objects.nonNull(expressFormula)) {
|
|
|
|
|
|
// 运行公式
|
|
|
|
|
|
ExcelResult result = runExpressFormula(expressFormula, formulaVarValueMap, simpleEmployee);
|
|
|
|
|
|
resultValue = Utils.null2String(result.getData());
|
|
|
|
|
|
//公式异常信息
|
|
|
|
|
|
if (!result.isStatus()) {
|
|
|
|
|
|
String username = empInfo.stream().filter(emp -> StringUtils.equals("employeeInfo_username", emp.getFieldId())).findFirst().map(CalculateFormulaVarBO.FormulaVarValue::getFieldValue).orElse("");
|
|
|
|
|
|
String errorMsg = String.format("%s的%s核算异常,原因:%s \r\n", username, salaryItemPO.getName(), result.getErrorMsg());
|
|
|
|
|
|
noticeMsg.append(errorMsg);
|
2022-04-11 20:17:47 +08:00
|
|
|
|
}
|
2023-09-07 15:15:59 +08:00
|
|
|
|
//提醒运行超时
|
|
|
|
|
|
if (StringUtils.isNotBlank(formulaRunOvertimeThreshold) && result.getRunTime() > Long.parseLong(formulaRunOvertimeThreshold)) {
|
|
|
|
|
|
String username = empInfo.stream().filter(emp -> StringUtils.equals("employeeInfo_username", emp.getFieldId())).findFirst().map(CalculateFormulaVarBO.FormulaVarValue::getFieldValue).orElse("");
|
|
|
|
|
|
String errorMsg = String.format("%s的%s核算超时,耗时:%s毫秒 \r\n", username, salaryItemPO.getName(), result.getRunTime());
|
|
|
|
|
|
noticeMsg.append(errorMsg);
|
2022-04-11 20:17:47 +08:00
|
|
|
|
}
|
2023-09-07 15:15:59 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
// 处理取值类型为“输入/导入”的薪资项目
|
2022-04-29 14:17:31 +08:00
|
|
|
|
String key = SalaryFormulaReferenceEnum.SALARY_ITEM.getValue() + SalaryFormulaFieldConstant.FIELD_ID_SEPARATOR + salaryItemPO.getCode();
|
2023-09-07 15:15:59 +08:00
|
|
|
|
resultValue = formulaVarValueMap.getOrDefault(key, StringUtils.EMPTY);
|
|
|
|
|
|
}
|
|
|
|
|
|
// 处理薪资档案
|
|
|
|
|
|
if (Objects.equals(salaryItemPO.getUseInEmployeeSalary(), NumberUtils.INTEGER_ONE)) {
|
|
|
|
|
|
String key = SalaryFormulaReferenceEnum.SALARY_ARCHIVES.getValue() + SalaryFormulaFieldConstant.FIELD_ID_SEPARATOR + salaryItemPO.getCode();
|
|
|
|
|
|
resultValue = formulaVarValueMap.getOrDefault(key, StringUtils.EMPTY);
|
|
|
|
|
|
}
|
|
|
|
|
|
// 处理合并计税
|
|
|
|
|
|
resultValue = handleConsolidatedTax(resultValue, salaryItemPO, salaryAcctCalculateBO, otherSalaryAcctEmployeePOMap.get(salaryAcctEmployeePO.getEmployeeId() + "_" + salaryAcctEmployeePO.getTaxAgentId()), otherSalaryAcctResultPOMap.get(salaryAcctEmployeePO.getEmployeeId() + "_" + salaryAcctEmployeePO.getTaxAgentId()));
|
|
|
|
|
|
|
|
|
|
|
|
// 处理小数点
|
|
|
|
|
|
resultValue = SalaryAcctFormulaBO.roundResultValue(resultValue, salaryItemPO, salarySobBackItems, salarySobBackItemMap, salaryItemIdKeySalarySobItemPOMap);
|
|
|
|
|
|
//是否锁定
|
|
|
|
|
|
if (lockSalaryItemIds.contains(salaryItemId) && salaryAcctLockResultPOS.get(salaryItemId + "_" + salaryAcctEmployeePOId) != null) {
|
|
|
|
|
|
resultValue = salaryAcctLockResultPOS.get(salaryItemId + "_" + salaryAcctEmployeePOId).getResultValue();
|
|
|
|
|
|
}
|
|
|
|
|
|
// 将已经计算过的薪资项目的值转换成公式变量的值添加到集合中
|
|
|
|
|
|
String key = SalaryFormulaReferenceEnum.SALARY_ITEM.getValue() + SalaryFormulaFieldConstant.FIELD_ID_SEPARATOR + salaryItemPO.getCode();
|
|
|
|
|
|
formulaVarValueMap.put(key, resultValue);
|
|
|
|
|
|
// 值保存薪资账套下的薪资项目的核算结果
|
|
|
|
|
|
if (salaryItemIdKeySalarySobItemPOMap.containsKey(salaryItemId) || salarySobBackItemIds.contains(salaryItemId)) {
|
|
|
|
|
|
// 转换成薪资核算结果po
|
|
|
|
|
|
SalaryAcctResultTempPO salaryAcctResultTempPO = new SalaryAcctResultTempPO()
|
|
|
|
|
|
.setSalaryAcctRecordId(salaryAcctEmployeePO.getSalaryAcctRecordId())
|
|
|
|
|
|
.setSalaryAcctEmpId(salaryAcctEmployeePOId)
|
|
|
|
|
|
.setEmployeeId(salaryAcctEmployeePO.getEmployeeId())
|
|
|
|
|
|
.setTaxAgentId(salaryAcctEmployeePO.getTaxAgentId())
|
|
|
|
|
|
.setSalarySobId(salaryAcctEmployeePO.getSalarySobId())
|
|
|
|
|
|
.setSalaryItemId(salaryItemPO.getId())
|
|
|
|
|
|
.setResultValue(resultValue)
|
|
|
|
|
|
.setOriginResultValue(salaryAcctResultPOMap.get(salaryAcctEmployeePO.getEmployeeId() + "-" + salaryAcctEmployeePO.getTaxAgentId() + "-" + salaryItemId) == null
|
|
|
|
|
|
? StringUtils.EMPTY : salaryAcctResultPOMap.get(salaryAcctEmployeePO.getEmployeeId() + "-" + salaryAcctEmployeePO.getTaxAgentId() + "-" + salaryItemId))
|
|
|
|
|
|
.setCalculateKey(salaryAcctCalculateBO.getCalculateKey())
|
|
|
|
|
|
.setCreator((long) user.getUID())
|
|
|
|
|
|
.setCreateTime(now)
|
|
|
|
|
|
.setUpdateTime(now)
|
|
|
|
|
|
.setTenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY)
|
|
|
|
|
|
.setDeleteType(0);
|
|
|
|
|
|
salaryAcctResultTempPOS.add(salaryAcctResultTempPO);
|
2022-04-11 20:17:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-09-26 14:29:12 +08:00
|
|
|
|
sw.stop();
|
2022-04-11 20:17:47 +08:00
|
|
|
|
// 保存新的薪资核算结果(临时存储)
|
2024-05-16 15:56:49 +08:00
|
|
|
|
sw.start("保存新的薪资核算结果(临时存储)");
|
2022-04-11 20:17:47 +08:00
|
|
|
|
getSalaryAcctResultTempService(user).batchSave(salaryAcctResultTempPOS);
|
2023-09-26 14:29:12 +08:00
|
|
|
|
sw.stop();
|
2022-04-11 20:17:47 +08:00
|
|
|
|
// 更新薪资核算进度
|
2023-09-26 14:29:12 +08:00
|
|
|
|
sw.start("更新薪资核算进度");
|
2022-04-29 17:15:22 +08:00
|
|
|
|
getSalaryAcctProgressService(user).getAndAddCalculatedQty(SalaryCacheKey.ACCT_PROGRESS + salaryAcctCalculateBO.getSalaryAcctRecordPO().getId(),
|
2023-05-16 18:01:38 +08:00
|
|
|
|
salaryAcctCalculateBO.getSalaryAcctEmployeePOS().size(),
|
2023-05-17 17:38:12 +08:00
|
|
|
|
noticeMsg.toString()
|
2023-05-16 18:01:38 +08:00
|
|
|
|
);
|
2023-09-26 14:29:12 +08:00
|
|
|
|
sw.stop();
|
2024-05-16 15:56:49 +08:00
|
|
|
|
log.info(sw.prettyPrint());
|
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
|
|
|
|
|
|
*/
|
2023-05-16 18:01:38 +08:00
|
|
|
|
private ExcelResult runExpressFormula(ExpressFormula expressFormula, Map<String, String> formulaVarValueMap, DataCollectionEmployee simpleEmployee) {
|
2022-04-11 20:17:47 +08:00
|
|
|
|
// 给公式中的变量填入值
|
2023-05-16 18:01:38 +08:00
|
|
|
|
ExcelResult result = new ExcelResult();
|
2022-05-08 12:34:05 +08:00
|
|
|
|
try {
|
|
|
|
|
|
List<FormulaVar> formulaVars = ExpressFormulaBO.buildFormulaVar4Accounting(expressFormula, formulaVarValueMap);
|
2023-05-16 18:01:38 +08:00
|
|
|
|
result = getFormulaRunService(user).run(expressFormula, formulaVars, simpleEmployee);
|
2022-05-08 12:34:05 +08:00
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
log.error("express execute fail ", e);
|
2023-05-16 18:01:38 +08:00
|
|
|
|
result.setStatus(false);
|
|
|
|
|
|
result.setErrorMsg(e.getMessage());
|
2022-05-08 12:34:05 +08:00
|
|
|
|
}
|
2022-11-17 09:28:40 +08:00
|
|
|
|
|
2023-05-16 18:01:38 +08:00
|
|
|
|
//核算出错,给个默认值
|
|
|
|
|
|
if (!result.isStatus() || result.getData() == null) {
|
|
|
|
|
|
if ("number".equals(expressFormula.getReturnType())) {
|
|
|
|
|
|
result.setData("0");
|
|
|
|
|
|
} else {
|
|
|
|
|
|
result.setData(StringUtils.EMPTY);
|
|
|
|
|
|
}
|
2022-11-17 09:28:40 +08:00
|
|
|
|
}
|
2023-05-16 18:01:38 +08:00
|
|
|
|
return result;
|
2022-04-11 20:17:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 处理合并计税
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
|
|
|
|
|
private String handleConsolidatedTax(String resultValue, SalaryItemPO salaryItemPO, SalaryAcctCalculateBO salaryAcctCalculateBO, List<SalaryAcctEmployeePO> otherSalaryAcctEmployeePOS, List<SalaryAcctResultPO> otherSalaryAcctResultPOS) {
|
|
|
|
|
|
// 如果相同税款所属期内没有其他薪资核算人员,就不存在合并计税
|
2023-07-04 09:32:12 +08:00
|
|
|
|
if (salaryAcctCalculateBO.getTaxDeclarationFunction() == TaxDeclarationFunctionEnum.CLOSURE || CollectionUtils.isEmpty(otherSalaryAcctEmployeePOS) || CollectionUtils.isEmpty(otherSalaryAcctResultPOS)) {
|
2022-04-11 20:17:47 +08:00
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|