weaver-hrm-salary/src/com/engine/salary/wrapper/TaxDeclarationWrapper.java

294 lines
16 KiB
Java
Raw Normal View History

2022-04-16 15:33:51 +08:00
package com.engine.salary.wrapper;
import com.engine.common.util.ServiceUtil;
import com.engine.core.impl.Service;
import com.engine.salary.biz.EmployBiz;
import com.engine.salary.biz.SalaryItemBiz;
import com.engine.salary.common.LocalDateRange;
import com.engine.salary.component.WeaFormOption;
2022-05-26 13:41:58 +08:00
import com.engine.salary.entity.datacollection.AddUpSituation;
2022-04-16 15:33:51 +08:00
import com.engine.salary.entity.datacollection.DataCollectionEmployee;
import com.engine.salary.entity.salaryacct.po.SalaryAcctRecordPO;
import com.engine.salary.entity.salaryacct.po.SalaryAcctResultPO;
import com.engine.salary.entity.salaryitem.po.SalaryItemPO;
import com.engine.salary.entity.salarysob.po.SalarySobPO;
2022-05-31 16:41:11 +08:00
import com.engine.salary.entity.taxagent.po.TaxAgentPO;
2022-04-16 15:33:51 +08:00
import com.engine.salary.entity.taxdeclaration.TaxDeclaration;
import com.engine.salary.entity.taxdeclaration.bo.TaxDeclarationBO;
import com.engine.salary.entity.taxdeclaration.dto.TaxDeclarationFormDTO;
import com.engine.salary.entity.taxdeclaration.dto.TaxDeclarationInfoDTO;
import com.engine.salary.entity.taxdeclaration.dto.TaxDeclarationListDTO;
import com.engine.salary.entity.taxdeclaration.param.TaxDeclarationListQueryParam;
import com.engine.salary.entity.taxdeclaration.param.TaxDeclarationSaveParam;
import com.engine.salary.entity.taxdeclaration.po.TaxDeclarationPO;
import com.engine.salary.entity.taxrate.TaxAgent;
import com.engine.salary.enums.salaryaccounting.SalaryAcctRecordStatusEnum;
import com.engine.salary.exception.SalaryRunTimeException;
import com.engine.salary.mapper.datacollection.AddUpSituationMapper;
import com.engine.salary.mapper.salaryacct.SalaryAcctRecordMapper;
import com.engine.salary.mapper.taxdeclaration.TaxDeclarationMapper;
import com.engine.salary.service.*;
2022-04-18 18:56:17 +08:00
import com.engine.salary.service.impl.*;
2022-04-16 15:33:51 +08:00
import com.engine.salary.util.SalaryDateUtil;
import com.engine.salary.util.SalaryEntityUtil;
import com.engine.salary.util.SalaryI18nUtil;
import com.engine.salary.util.db.MapperProxyFactory;
import com.engine.salary.util.page.PageInfo;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.transaction.annotation.Transactional;
import weaver.hrm.User;
2022-04-19 20:58:30 +08:00
import java.text.SimpleDateFormat;
2022-04-16 15:33:51 +08:00
import java.util.*;
import java.util.stream.Collectors;
/**
2022-06-02 18:02:27 +08:00
* 个税申报表
* <p>Copyright: Copyright (c) 2022</p>
* <p>Company: 泛微软件</p>
*
* @author qiantao
* @version 1.0
**/
2022-04-16 15:33:51 +08:00
public class TaxDeclarationWrapper extends Service {
private TaxDeclarationService getTaxDeclarationService(User user) {
2022-05-31 16:41:11 +08:00
return ServiceUtil.getService(TaxDeclarationServiceImpl.class, user);
2022-04-16 15:33:51 +08:00
}
2022-05-31 21:05:59 +08:00
private TaxAgentService getTaxAgentService(User user) {
return ServiceUtil.getService(TaxAgentServiceImpl.class, user);
2022-04-16 15:33:51 +08:00
}
2022-05-09 17:47:19 +08:00
private TaxDeclarationDetailService getTaxDeclarationDetailService(User user) {
2022-05-31 16:41:11 +08:00
return ServiceUtil.getService(TaxDeclarationDetailServiceImpl.class, user);
2022-05-09 17:47:19 +08:00
}
private SalaryAcctRecordService getSalaryAcctRecordService(User user) {
2022-05-31 16:41:11 +08:00
return ServiceUtil.getService(SalaryAcctRecordServiceImpl.class, user);
2022-05-09 17:47:19 +08:00
}
2022-04-16 15:33:51 +08:00
private TaxDeclarationMapper getTaxDeclarationMapper() {
return MapperProxyFactory.getProxy(TaxDeclarationMapper.class);
}
private SalaryAcctRecordMapper getSalaryAcctRecordMapper() {
return MapperProxyFactory.getProxy(SalaryAcctRecordMapper.class);
}
2022-05-09 17:47:19 +08:00
private AddUpSituationMapper getAddUpSituationMapper() {
return MapperProxyFactory.getProxy(AddUpSituationMapper.class);
}
private AddUpSituationService getAddUpSituationService(User user) {
return (AddUpSituationService) ServiceUtil.getService(AddUpSituationServiceImpl.class, user);
}
private SalarySobService getSalarySobService(User user) {
return (SalarySobService) ServiceUtil.getService(SalarySobServiceImpl.class, user);
}
2022-05-30 18:03:20 +08:00
private SalaryAcctResultService getSalaryAcctResultService(User user) {
return ServiceUtil.getService(SalaryAcctResultServiceImpl.class, user);
}
2022-04-16 15:33:51 +08:00
/**
* 个税申报表列表
*
* @param queryParam 列表查询条件
* @param
* @return
*/
public PageInfo listPage(TaxDeclarationListQueryParam queryParam) {
EmployBiz employBiz = new EmployBiz();
// 询个税申报表(分页)
PageInfo<TaxDeclarationPO> page = getTaxDeclarationService(user).listPageByParam(queryParam);
PageInfo<TaxDeclarationListDTO> dtoPage = new PageInfo<TaxDeclarationListDTO>(TaxDeclarationListDTO.class);
dtoPage.setPageNum(queryParam.getCurrent());
dtoPage.setPageSize(queryParam.getPageSize());
List<TaxDeclarationPO> list = page.getList();
if (CollectionUtils.isNotEmpty(list)) {
// 查询人员
List<Long> employeeIds = SalaryEntityUtil.properties(list, TaxDeclarationPO::getCreator, Collectors.toList());
List<DataCollectionEmployee> employeeComInfos = employBiz.getEmployeeByIdsAll(employeeIds);
// 查询个税扣缴义务人
Set<Long> taxAgentIds = SalaryEntityUtil.properties(list, TaxDeclarationPO::getTaxAgentId);
2022-04-18 18:56:17 +08:00
//List<TaxAgent> taxAgentPOS = new TaxAgentBiz().listByIds(taxAgentIds);
List<TaxAgent> taxAgentPOS = getTaxDeclarationService(user).countByTaxDeclarationId(taxAgentIds);
2022-04-16 15:33:51 +08:00
// 转换成列表dto
2022-05-09 17:47:19 +08:00
List<TaxDeclarationListDTO> taxDeclarationListDTOS = TaxDeclarationBO.convert2ListDTO(list, employeeComInfos, taxAgentPOS);
2022-04-16 15:33:51 +08:00
dtoPage.setList(taxDeclarationListDTOS);
}
return dtoPage;
}
2022-05-09 17:47:19 +08:00
public TaxDeclarationFormDTO getForm(Long id) {
TaxDeclarationFormDTO formDTO = new TaxDeclarationFormDTO();
if (Objects.nonNull(id)) {
// 查询个税申报表
TaxDeclaration taxDeclaration = getTaxDeclarationService(user).getById(id);
if (Objects.isNull(taxDeclaration)) {
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(98877, "个税申报表不存在或已删除"));
2022-04-16 15:33:51 +08:00
}
2022-05-09 17:47:19 +08:00
// 查询个税扣缴义务人
2022-05-31 16:41:11 +08:00
TaxAgentPO taxAgent = getTaxAgentService(user).getById(id);
2022-05-09 17:47:19 +08:00
//日期转换
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
String transformDate = simpleDateFormat.format(taxDeclaration.getSalaryMonth());
// 转换成个税申报表详情dto
formDTO = TaxDeclarationFormDTO.builder()
.salaryMonth(SalaryDateUtil.String2YearMonth(transformDate))
.taxAgentId(taxDeclaration.getTaxAgentId())
2022-05-31 16:41:11 +08:00
.taxAgentName(Optional.ofNullable(taxAgent).map(TaxAgentPO::getName).orElse(""))
2022-05-09 17:47:19 +08:00
.description(taxDeclaration.getDescription())
.build();
}
// 转换成前端所需的数据格式
// WeaForm weaForm = SalaryFormatUtil.<TaxDeclarationFormDTO>getInstance().buildForm(TaxDeclarationFormDTO.class, formDTO);
2022-04-16 15:33:51 +08:00
2022-05-09 17:47:19 +08:00
// 查询租户所有的个税扣缴义务人
2022-05-31 16:41:11 +08:00
Collection<TaxAgentPO> taxAgentListDTOS =getTaxAgentService(user).listAll();
2022-05-09 17:47:19 +08:00
// 表单中个税扣缴义务人的可选项
List<WeaFormOption> weaFormOptions = Lists.newArrayListWithExpectedSize(taxAgentListDTOS.size());
2022-05-31 16:41:11 +08:00
for (TaxAgentPO taxAgent : taxAgentListDTOS) {
2022-05-09 17:47:19 +08:00
weaFormOptions.add(new WeaFormOption("" + taxAgent.getId(), taxAgent.getName()));
}
2022-04-16 15:33:51 +08:00
// weaForm.getItems().forEach((k, v) -> {
// if (StringUtils.equals("taxAgentId", k)) {
// v.setOptions(weaFormOptions);
// }
// if (StringUtils.equals("salaryMonth", k)) {
// Map<String, Object> otherParams = new HashMap<>();
// otherParams.put("type", "month");
// v.setOtherParams(otherParams);
// }
// });
2022-05-09 17:47:19 +08:00
return formDTO;
}
/**
2022-04-16 15:33:51 +08:00
* 查询个税申报表的基本信息
*
2022-05-09 17:47:19 +08:00
* @param id 个税申报表id
2022-04-16 15:33:51 +08:00
* @return
*/
public TaxDeclarationInfoDTO getTaxDeclarationInfoById(Long id) {
// 查询个税申报表
TaxDeclaration taxDeclaration = getTaxDeclarationService(user).getById(id);
if (Objects.isNull(taxDeclaration)) {
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(98877, "个税申报表不存在或已删除"));
}
2022-04-19 20:58:30 +08:00
//日期转换
2022-05-09 17:47:19 +08:00
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
String transformDate = simpleDateFormat.format(taxDeclaration.getSalaryMonth());
2022-04-16 15:33:51 +08:00
// 查询个税扣缴义务人
2022-05-31 16:41:11 +08:00
TaxAgentPO taxAgentPO = getTaxAgentService(user).getById(taxDeclaration.getTaxAgentId());
2022-04-16 15:33:51 +08:00
return TaxDeclarationInfoDTO.builder()
2022-04-19 20:58:30 +08:00
.salaryMonth(SalaryDateUtil.String2YearMonth(transformDate))
2022-04-16 15:33:51 +08:00
.taxAgentId(taxDeclaration.getTaxAgentId())
2022-05-31 16:41:11 +08:00
.taxAgentName(Optional.ofNullable(taxAgentPO).map(TaxAgentPO::getName).orElse(""))
2022-04-16 15:33:51 +08:00
.build();
}
2022-05-09 17:47:19 +08:00
/**
2022-04-16 15:33:51 +08:00
* 保存
*
2022-05-09 17:47:19 +08:00
* @param saveParam 保存参数
2022-04-16 15:33:51 +08:00
*/
@Transactional(rollbackFor = Exception.class)
public void save(TaxDeclarationSaveParam saveParam) {
2022-04-20 21:36:27 +08:00
User user = saveParam.getUser();
2022-04-16 15:33:51 +08:00
// 薪资所属月的日期范围
2022-05-09 17:47:19 +08:00
LocalDateRange salaryMonthDateRange = SalaryDateUtil.localDate2Range(SalaryDateUtil.localDateToDate(saveParam.getSalaryMonth().atDay(1)));
2022-04-16 15:33:51 +08:00
if (Objects.isNull(salaryMonthDateRange)) {
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(84026, "参数错误"));
}
// 查询薪资所属月已经生成过的个税申报表
List<TaxDeclarationPO> taxDeclarationPOS = listBySalaryMonthTax(salaryMonthDateRange);
// 已经生成过个税申报表,不允许再次生成个税申报表
if (CollectionUtils.isNotEmpty(taxDeclarationPOS)) {
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(98873, "{0}已经生成过个税申报表,不允许再次生成")
.replace("{0}", saveParam.getSalaryMonth().toString()));
}
// 查询薪资所属月的薪资核算记录
List<SalaryAcctRecordPO> salaryAcctRecordPOS = listBySalaryMonth(salaryMonthDateRange);
// 无薪资核算记录,不允许生成个税申报表
if (CollectionUtils.isEmpty(salaryAcctRecordPOS)) {
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(98874, "{0}无申报数据")
.replace("{0}", saveParam.getSalaryMonth().toString()));
}
// 如果存在未归档的,也不允许生成个税申报表
boolean notArchived = salaryAcctRecordPOS.stream().anyMatch(salaryAcctRecordPO -> !Objects.equals(salaryAcctRecordPO.getStatus(), SalaryAcctRecordStatusEnum.ARCHIVED.getValue()));
if (notArchived) {
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(98875, "{0}有未归档数据,请全部归档后再申报")
.replace("{0}", saveParam.getSalaryMonth().toString()));
}
// 如果当前薪资所属月下存在不同的税款所属期,属于异常业务场景,不允许生成个税申报表
Date taxCycle = salaryAcctRecordPOS.get(0).getTaxCycle();
boolean differentTaxCycle = salaryAcctRecordPOS.stream().anyMatch(salaryAcctRecordPO -> salaryAcctRecordPO.getTaxCycle().compareTo(taxCycle) != 0);
if (differentTaxCycle) {
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(98876, "{0}存在不同的税款所属期,无法正常生成个税申报表,请调整账套设置,重新核算后再生成个税申报表")
.replace("{0}", saveParam.getSalaryMonth().toString()));
}
// 查询薪资核算结果
Set<Long> salaryAcctRecordIds = SalaryEntityUtil.properties(salaryAcctRecordPOS, SalaryAcctRecordPO::getId);
2022-05-30 18:03:20 +08:00
List<SalaryAcctResultPO> salaryAcctResultPOS = getSalaryAcctResultService(user).listBySalaryAcctRecordIds(salaryAcctRecordIds);
2022-04-16 15:33:51 +08:00
// 无薪资核算结果,不允许生成个税申报表
if (CollectionUtils.isEmpty(salaryAcctResultPOS)) {
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(98874, "{0}无申报数据")
.replace("{0}", saveParam.getSalaryMonth().toString()));
}
// 查询薪资账套
Set<Long> salarySobIds = SalaryEntityUtil.properties(salaryAcctRecordPOS, SalaryAcctRecordPO::getSalarySobId);
2022-05-09 17:47:19 +08:00
List<SalarySobPO> salarySobPOS = getSalarySobService(user).listByIds(salarySobIds);
2022-04-16 15:33:51 +08:00
// 查询所有薪资项目
2022-04-18 18:56:17 +08:00
List<SalaryItemPO> salaryItemPOS = new SalaryItemBiz().listAll();
2022-04-16 15:33:51 +08:00
// 处理要保存的数据
2022-05-09 17:47:19 +08:00
TaxDeclarationBO.Result result = TaxDeclarationBO.handle(saveParam, taxCycle, user, salaryItemPOS, salarySobPOS, salaryAcctResultPOS);
2022-04-16 15:33:51 +08:00
// 保存个税申报表
if (CollectionUtils.isNotEmpty(result.getNeedInsertTaxDeclarations())) {
2022-04-18 18:56:17 +08:00
getTaxDeclarationMapper().batchInsert(result.getNeedInsertTaxDeclarations());
2022-04-16 15:33:51 +08:00
}
// 保存个税申报表明细
if (CollectionUtils.isNotEmpty(result.getNeedInsertTaxDeclarationDetails())) {
2022-05-09 17:47:19 +08:00
getTaxDeclarationDetailService(user).batchSave(result.getNeedInsertTaxDeclarationDetails());
2022-04-16 15:33:51 +08:00
}
// 保存累计情况
if (CollectionUtils.isNotEmpty(result.getNeedInsertAccumulatedSituations())) {
2022-05-09 17:47:19 +08:00
Set<Long> taxAgentIds = SalaryEntityUtil.properties(result.getNeedInsertTaxDeclarations(), TaxDeclarationPO::getTaxAgentId);
getAddUpSituationService(user).deleteByTaxYearMonthAndTaxAgentIds(SalaryDateUtil.localDate2YearMonth(taxCycle),taxAgentIds);
2022-05-26 13:41:58 +08:00
List<List<AddUpSituation>> partition = Lists.partition((List) result.getNeedInsertAccumulatedSituations(), 100);
partition.forEach(getAddUpSituationMapper()::insertData);
2022-04-16 15:33:51 +08:00
}
// 更新薪资核算记录的状态
2022-05-09 17:47:19 +08:00
getSalaryAcctRecordService(user).updateStatusByIds(salaryAcctRecordIds, SalaryAcctRecordStatusEnum.DECLARED);
2022-04-16 15:33:51 +08:00
// 查询个税扣缴义务人
2022-05-09 17:47:19 +08:00
// Set<Long> taxAgentIds = SalaryEntityUtil.properties(result.getNeedInsertTaxDeclarations(), TaxDeclarationPO::getTaxAgentId);
// List<TaxAgent> taxAgentPOS = new TaxAgentBiz().listByIds(taxAgentIds);
// Map<Long, String> taxAgentNameMap = SalaryEntityUtil.convert2Map(taxAgentPOS, TaxAgent::getId, TaxAgent::getName);
2022-04-16 15:33:51 +08:00
// 记录日志
/*for (TaxDeclarationPO taxDeclarationPO : result.getNeedInsertTaxDeclarations()) {
String targetName = SalaryDateUtil.toYearMonth(taxDeclarationPO.getSalaryMonth())
+ "(" + taxAgentNameMap.getOrDefault(taxDeclarationPO.getTaxAgentId(), StringUtils.EMPTY) + ")";
LoggerContext<TaxDeclarationPO> loggerContext = new LoggerContext<>();
loggerContext.setTargetId(String.valueOf(taxDeclarationPO.getId()));
loggerContext.setTargetName(targetName);
loggerContext.setOperateType(OperateTypeEnum.UPDATE.getValue());
loggerContext.setOperateTypeName(SalaryI18nUtil.getI18nLabel(99815, "生成个税申报表"));
loggerContext.setOperatedesc(SalaryI18nUtil.getI18nLabel(99815, "生成个税申报表"));
taxDeclarationLoggerTemplate.write(loggerContext);
}*/
}
public List<TaxDeclarationPO> listBySalaryMonthTax(LocalDateRange salaryMonthDateRange) {
return getTaxDeclarationMapper().listSome(TaxDeclarationPO.builder().salaryMonths(salaryMonthDateRange).build());
}
public List<SalaryAcctRecordPO> listBySalaryMonth(LocalDateRange salaryMonthDateRange) {
return getSalaryAcctRecordMapper().listSome(SalaryAcctRecordPO.builder().salaryMonths(salaryMonthDateRange).build());
}
}