weaver-hrm-salary/src/com/engine/salary/service/impl/TaxDeclarationServiceImpl.java

279 lines
14 KiB
Java
Raw Normal View History

2022-03-01 18:49:22 +08:00
package com.engine.salary.service.impl;
2022-05-09 11:46:49 +08:00
import com.engine.common.util.ServiceUtil;
2022-03-01 18:49:22 +08:00
import com.engine.core.impl.Service;
2022-04-16 15:33:51 +08:00
import com.engine.salary.common.LocalDateRange;
2022-06-08 16:36:50 +08:00
import com.engine.salary.entity.salaryacct.po.SalaryAcctRecordPO;
2022-06-10 13:33:48 +08:00
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-06-02 18:02:27 +08:00
import com.engine.salary.entity.taxagent.po.TaxAgentPO;
2022-06-10 13:33:48 +08:00
import com.engine.salary.entity.taxdeclaration.bo.TaxDeclarationBO;
2022-04-16 15:33:51 +08:00
import com.engine.salary.entity.taxdeclaration.param.TaxDeclarationListQueryParam;
import com.engine.salary.entity.taxdeclaration.param.TaxDeclarationSaveParam;
import com.engine.salary.entity.taxdeclaration.po.TaxDeclarationPO;
2022-06-10 13:33:48 +08:00
import com.engine.salary.enums.salaryaccounting.SalaryAcctRecordStatusEnum;
2022-05-09 11:46:49 +08:00
import com.engine.salary.exception.SalaryRunTimeException;
2022-06-10 13:33:48 +08:00
import com.engine.salary.mapper.datacollection.AddUpSituationMapper;
import com.engine.salary.mapper.salaryacct.SalaryAcctRecordMapper;
2022-04-16 15:33:51 +08:00
import com.engine.salary.mapper.taxdeclaration.TaxDeclarationMapper;
2022-06-10 13:33:48 +08:00
import com.engine.salary.service.*;
2022-04-16 15:33:51 +08:00
import com.engine.salary.util.SalaryDateUtil;
2022-05-09 11:46:49 +08:00
import com.engine.salary.util.SalaryEntityUtil;
import com.engine.salary.util.SalaryI18nUtil;
2022-04-16 15:33:51 +08:00
import com.engine.salary.util.db.MapperProxyFactory;
import com.engine.salary.util.page.PageInfo;
2022-05-25 13:10:03 +08:00
import com.engine.salary.util.page.SalaryPageUtil;
2022-04-16 15:33:51 +08:00
import org.apache.commons.collections4.CollectionUtils;
2022-06-10 13:33:48 +08:00
import org.apache.commons.lang3.BooleanUtils;
2022-05-09 11:46:49 +08:00
import weaver.hrm.User;
2022-03-01 18:49:22 +08:00
2022-04-16 15:33:51 +08:00
import java.util.*;
2022-06-10 13:33:48 +08:00
import java.util.stream.Collectors;
2022-03-01 18:49:22 +08:00
public class TaxDeclarationServiceImpl extends Service implements TaxDeclarationService {
2022-04-16 15:33:51 +08:00
private TaxDeclarationMapper getTaxDeclarationMapper() {
return MapperProxyFactory.getProxy(TaxDeclarationMapper.class);
}
2022-06-10 13:33:48 +08:00
private SalaryAcctRecordMapper getSalaryAcctRecordMapper() {
return MapperProxyFactory.getProxy(SalaryAcctRecordMapper.class);
}
private AddUpSituationMapper getAddUpSituationMapper() {
return MapperProxyFactory.getProxy(AddUpSituationMapper.class);
}
2022-05-09 11:46:49 +08:00
private TaxDeclarationDetailService getTaxDeclarationDetailService(User user) {
2022-06-02 18:02:27 +08:00
return ServiceUtil.getService(TaxDeclarationDetailServiceImpl.class, user);
2022-05-09 11:46:49 +08:00
}
private AddUpSituationService getAddUpSituationService(User user) {
2022-06-02 18:02:27 +08:00
return ServiceUtil.getService(AddUpSituationServiceImpl.class, user);
2022-05-09 11:46:49 +08:00
}
2022-06-02 18:02:27 +08:00
private TaxAgentService getTaxAgentService(User user) {
return ServiceUtil.getService(TaxAgentServiceImpl.class, user);
2022-03-01 18:49:22 +08:00
}
2022-06-10 13:33:48 +08:00
private SalarySobService getSalarySobService(User user) {
return ServiceUtil.getService(SalarySobServiceImpl.class, user);
}
private SalaryAcctResultService getSalaryAcctResultService(User user) {
return ServiceUtil.getService(SalaryAcctResultServiceImpl.class, user);
}
private SalaryItemService getSalaryItemService(User user) {
return ServiceUtil.getService(SalaryItemServiceImpl.class, user);
}
private SalaryAcctRecordService getSalaryAcctRecordService(User user) {
return ServiceUtil.getService(SalaryAcctRecordServiceImpl.class, user);
}
2022-06-02 18:02:27 +08:00
2022-03-01 18:49:22 +08:00
@Override
2022-04-16 15:33:51 +08:00
public PageInfo<TaxDeclarationPO> listPageByParam(TaxDeclarationListQueryParam queryParam) {
2022-06-02 18:02:27 +08:00
long currentEmployeeId = user.getUID();
2022-04-16 15:33:51 +08:00
// 分页参数
TaxDeclarationPO po = TaxDeclarationPO.builder().build();
LocalDateRange localDateRange = new LocalDateRange();
if (Objects.nonNull(queryParam.getFromSalaryMonth())) {
localDateRange.setFromDate(SalaryDateUtil.localDateToDate(queryParam.getFromSalaryMonth().atDay(1)));
}
if (Objects.nonNull(queryParam.getEndSalaryMonth())) {
localDateRange.setEndDate(SalaryDateUtil.localDateToDate(queryParam.getEndSalaryMonth().atEndOfMonth()));
}
po.setSalaryMonths(localDateRange);
2022-06-02 18:02:27 +08:00
// 分权
Boolean openDevolution = getTaxAgentService(user).isNeedAuth(currentEmployeeId);
if (openDevolution) {
// 查询负责管理的个税扣缴义务人
Collection<TaxAgentPO> taxAgentPOS = getTaxAgentService(user).listAllTaxAgents(currentEmployeeId);
if (CollectionUtils.isEmpty(taxAgentPOS)) {
return new PageInfo<>(new ArrayList<>());
}
Set<Long> taxAgentIds = SalaryEntityUtil.properties(taxAgentPOS, TaxAgentPO::getId);
po.setTaxAgentIds(taxAgentIds);
}
2022-04-16 15:33:51 +08:00
// 查询个税申报表
2022-05-25 13:10:03 +08:00
SalaryPageUtil.start(queryParam.getCurrent(), queryParam.getPageSize());
2022-04-16 15:33:51 +08:00
List<TaxDeclarationPO> taxDeclarationPOS = getTaxDeclarationMapper().listSome(po);
2022-06-10 13:33:48 +08:00
return new PageInfo<>(taxDeclarationPOS, TaxDeclarationPO.class);
2022-04-16 15:33:51 +08:00
}
2022-05-09 11:46:49 +08:00
2022-04-16 15:33:51 +08:00
//根据id查询taxAgents
@Override
2022-06-09 19:02:18 +08:00
public List<TaxAgentPO> countByTaxDeclarationId(Collection<Long> taxAgentIds) {
2022-04-16 15:33:51 +08:00
if (CollectionUtils.isEmpty(taxAgentIds)) {
return Collections.emptyList();
}
2022-06-09 19:02:18 +08:00
return getTaxAgentService(user).listByIds(taxAgentIds);
2022-04-16 15:33:51 +08:00
}
2022-05-09 11:46:49 +08:00
2022-04-16 15:33:51 +08:00
//根据id获取TaxDeclaration
@Override
2022-06-10 13:33:48 +08:00
public TaxDeclarationPO getById(Long id) {
2022-04-16 15:33:51 +08:00
return getTaxDeclarationMapper().getById(id);
}
@Override
public void save(TaxDeclarationSaveParam saveParam) {
2022-06-10 13:33:48 +08:00
long currentEmployeeId = user.getUID();
// 个税扣缴义务人id
Set<Long> taxAgentIds;
Long taxAgentId = saveParam.getTaxAgentId();
if(taxAgentId != null){
taxAgentIds = Collections.singleton(taxAgentId);
}else {
Boolean openDevolution = getTaxAgentService(user).isOpenDevolution();
if (BooleanUtils.isFalse(openDevolution)) {
taxAgentIds = SalaryEntityUtil.properties(getTaxAgentService(user).listAll(), TaxAgentPO::getId);
} else {
taxAgentIds = SalaryEntityUtil.properties(getTaxAgentService(user).listAllTaxAgentsAsAdmin(currentEmployeeId), TaxAgentPO::getId);
}
2022-06-10 13:33:48 +08:00
}
// 检查是否具有权限
if (CollectionUtils.isEmpty(taxAgentIds)) {
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(0, "对不起,您不具备任何个税扣缴义务人的管理权限"));
}
// 查询个税扣缴义务人
List<TaxAgentPO> taxAgentPOS = getTaxAgentService(user).listByIds(taxAgentIds);
Map<Long, String> taxAgentNameMap = SalaryEntityUtil.convert2Map(taxAgentPOS, TaxAgentPO::getId, TaxAgentPO::getName);
// 薪资所属月的日期范围
LocalDateRange salaryMonthDateRange = SalaryDateUtil.localDate2Range(SalaryDateUtil.localDateToDate(saveParam.getSalaryMonth().atDay(1)));
if (Objects.isNull(salaryMonthDateRange)) {
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(84026, "薪资所属月参数错误"));
}
// 查询薪资所属月个税扣缴义务人已经生成过的个税申报表
List<TaxDeclarationPO> taxDeclarationPOS = listBySalaryMonthTax(TaxDeclarationPO.builder().salaryMonths(salaryMonthDateRange).taxAgentIds(taxAgentNameMap.keySet()).build());
// 已经生成过个税申报表,不允许再次生成个税申报表
if (CollectionUtils.isNotEmpty(taxDeclarationPOS)) {
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(107986, "{0}在{1}已经生成过个税申报表,不允许再次生成")
.replace("{0}", taxAgentNameMap.get(taxDeclarationPOS.get(0).getTaxAgentId()))
.replace("{1}", saveParam.getSalaryMonth().toString()));
}
// 查询薪资所属月的薪资核算记录
List<SalaryAcctRecordPO> salaryAcctRecordPOS = listBySalaryMonth(SalaryAcctRecordPO.builder().salaryMonths(salaryMonthDateRange).build());
// 无薪资核算记录,不允许生成个税申报表
if (CollectionUtils.isEmpty(salaryAcctRecordPOS)) {
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(98874, "{0}无申报数据").replace("{0}", saveParam.getSalaryMonth().toString()));
}
// 查询薪资核算结果
List<SalaryAcctResultPO> salaryAcctResultPOS = getSalaryAcctResultService(user)
.listBySalaryAcctRecordIdsAndTaxAgentIds(SalaryEntityUtil.properties(salaryAcctRecordPOS, SalaryAcctRecordPO::getId), taxAgentIds);
// 无薪资核算结果,不允许生成个税申报表
if (CollectionUtils.isEmpty(salaryAcctResultPOS)) {
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(110093, "{0}无可申报数据")
.replace("{0}", saveParam.getSalaryMonth().toString()));
}
Set<Long> salaryAcctRecordIds = SalaryEntityUtil.properties(salaryAcctResultPOS, SalaryAcctResultPO::getSalaryAcctRecordId);
salaryAcctRecordPOS = salaryAcctRecordPOS.stream().filter(salaryAcctRecordPO -> salaryAcctRecordIds.contains(salaryAcctRecordPO.getId())).collect(Collectors.toList());
// 如果存在未归档的,也不允许生成个税申报表
boolean notArchived = salaryAcctRecordPOS.stream().anyMatch(salaryAcctRecordPO -> Objects.equals(salaryAcctRecordPO.getStatus(), SalaryAcctRecordStatusEnum.NOT_ARCHIVED.getValue()));
2022-06-10 13:33:48 +08:00
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> salarySobIds = SalaryEntityUtil.properties(salaryAcctRecordPOS, SalaryAcctRecordPO::getSalarySobId);
List<SalarySobPO> salarySobPOS = getSalarySobService(user).listByIds(salarySobIds);
// 查询所有薪资项目
List<SalaryItemPO> salaryItemPOS = getSalaryItemService(user).listAll();
// 处理要保存的数据
TaxDeclarationBO.Result result = TaxDeclarationBO.handle(saveParam, taxCycle, user, salaryItemPOS, salarySobPOS, salaryAcctResultPOS);
// 保存个税申报表
if (CollectionUtils.isNotEmpty(result.getNeedInsertTaxDeclarations())) {
getTaxDeclarationMapper().batchInsert(result.getNeedInsertTaxDeclarations());
}
// 保存个税申报表明细
if (CollectionUtils.isNotEmpty(result.getNeedInsertTaxDeclarationDetails())) {
getTaxDeclarationDetailService(user).batchSave(result.getNeedInsertTaxDeclarationDetails());
}
// 保存累计情况
if (CollectionUtils.isNotEmpty(result.getNeedInsertAddUpSituations())) {
getAddUpSituationService(user).deleteByTaxYearMonthAndTaxAgentIds(SalaryDateUtil.localDate2YearMonth(taxCycle), taxAgentIds);
2022-07-13 09:53:11 +08:00
getAddUpSituationService(user).batchSave((List)result.getNeedInsertAddUpSituations());
2022-06-10 13:33:48 +08:00
}
// 更新薪资核算记录的状态
getSalaryAcctRecordService(user).updateStatusByIds(salaryAcctRecordIds, SalaryAcctRecordStatusEnum.DECLARED);
2022-03-01 18:49:22 +08:00
}
2022-05-09 11:46:49 +08:00
@Override
2022-06-08 16:36:50 +08:00
public void deleteBySalaryMonth(SalaryAcctRecordPO salaryAcctRecordPO) {
2022-05-09 11:46:49 +08:00
// 薪资所属月的日期范围
2022-06-08 16:36:50 +08:00
LocalDateRange salaryMonthDateRange = SalaryDateUtil.localDate2Range(salaryAcctRecordPO.getSalaryMonth());
2022-05-09 11:46:49 +08:00
if (Objects.isNull(salaryMonthDateRange)) {
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(84026, "参数错误"));
}
List<TaxDeclarationPO> taxDeclarationPOS = listBySalaryMonth(salaryMonthDateRange);
if (CollectionUtils.isEmpty(taxDeclarationPOS)) {
return;
}
// 删除个税申报表
Set<Long> taxDeclarationIds = SalaryEntityUtil.properties(taxDeclarationPOS, TaxDeclarationPO::getId);
getTaxDeclarationMapper().deleteByIds(taxDeclarationIds);
// 删除个税申报表详情
getTaxDeclarationDetailService(user).deleteByTaxDeclarationIds(taxDeclarationIds);
// 删除往期累计情况
2022-06-08 16:36:50 +08:00
getAddUpSituationService(user).deleteAddUpSituationList(salaryAcctRecordPO.getTaxCycle(), Collections.emptyList());
2022-05-09 11:46:49 +08:00
}
public List<TaxDeclarationPO> listBySalaryMonth(LocalDateRange salaryMonthDateRange) {
return getTaxDeclarationMapper().listSome(TaxDeclarationPO.builder().salaryMonths(salaryMonthDateRange).build());
}
2022-06-10 13:33:48 +08:00
public List<TaxDeclarationPO> listBySalaryMonthTax(TaxDeclarationPO build) {
return getTaxDeclarationMapper().listSome(build);
}
public List<SalaryAcctRecordPO> listBySalaryMonth(SalaryAcctRecordPO po) {
return getSalaryAcctRecordMapper().listSome(po);
}
2022-08-02 10:59:12 +08:00
@Override
public boolean checkByAuthority(TaxDeclarationPO taxDeclarationPO, Long employeeId) {
// 判断是否开启了分权
Boolean openDevolution = getTaxAgentService(user).isOpenDevolution();
// 判断是否是总管理员
Boolean isChief = getTaxAgentService(user).isChief(employeeId);
if (openDevolution && !isChief) {
// 查询负责管理的个税扣缴义务人
Collection<TaxAgentPO> taxAgentPOS = getTaxAgentService(user).listAllTaxAgentsAsAdmin(employeeId);
if (CollectionUtils.isEmpty(taxAgentPOS)) {
return false;
}
Set<Long> taxAgentIds = SalaryEntityUtil.properties(taxAgentPOS, TaxAgentPO::getId);
return taxAgentIds.contains(taxDeclarationPO.getTaxAgentId());
}
// 查询个税申报表
return true;
}
2022-03-01 18:49:22 +08:00
}