| | |
| | | package com.ruoyi.project.service.impl; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.text.ParseException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.time.LocalDate; |
| | | import java.time.ZoneId; |
| | |
| | | } |
| | | |
| | | /** |
| | | * 根据条件算税 |
| | | * 批量算税主方法 |
| | | * 1. 校验参数 |
| | | * 2. 排序明细,保证税时间为空的排在后面 |
| | | * 3. 遍历每条明细,分身份证号为空和不为空两种情况分别处理 |
| | | * - 身份证号为空:直接按规则赋值税后金额、税金 |
| | | * - 身份证号不为空: |
| | | * a. 查询本月第一天,获取最大序号 |
| | | * b. 查询本月累计税前、税金、税后金额 |
| | | * c. 根据"税前/税后"类型分别计算税金、税后金额 |
| | | * d. 设置算税时间、序号等 |
| | | * 4. 累计本批次总税前、税后金额 |
| | | * 5. 批量更新所有明细 |
| | | * 6. 更新主表ServiceFund的总金额、算税时间、状态 |
| | | * |
| | | * @param serviceFunddetails |
| | | * @param serviceFunddetails 费用明细列表 |
| | | * @return 是否算税成功 |
| | | */ |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Boolean calculateTax(List<ServiceFunddetail> serviceFunddetails) { |
| | | public Boolean calculateTax(List<ServiceFunddetail> serviceFunddetails, Map<String, TaxMoneySumEO> tempTaxSumMap) { |
| | | // 1. 校验参数 |
| | | if (CollectionUtils.isEmpty(serviceFunddetails)) { |
| | | throw new BaseException("算税条件为空,请检查后,再进行计算"); |
| | | } |
| | | // 用于临时保存已经算好的"费用详情数据" |
| | | List<ServiceFunddetail> temporarySave = new ArrayList<>(); |
| | | // 用于记录序号 |
| | | Map<String, Long> xhMap = new HashMap<>(); |
| | | // 2. 排序,税时间为空的排后面 |
| | | serviceFunddetails.sort(Comparator.comparing(obj -> ObjectUtils.isEmpty(obj.getTaxTime()) ? 1 : 0)); |
| | | |
| | | // 先对serviceFunddetails排序 |
| | | Comparator<ServiceFunddetail> customComparator = Comparator.comparing(obj -> { |
| | | if (ObjectUtils.isEmpty(obj.getTaxTime())) { |
| | | return 1; // 返回1表示字段为空的对象排在后面 |
| | | } else { |
| | | return 0; // 返回0表示字段不为空的对象保持原顺序 |
| | | } |
| | | }); |
| | | Collections.sort(serviceFunddetails, customComparator); |
| | | |
| | | //这个是为了获取fund_tax_time |
| | | // 3. 获取本批次fundid及主表信息 |
| | | Long fundid = serviceFunddetails.get(0).getFundid(); |
| | | log.info("算税的fundid的值为:{}", fundid); |
| | | ServiceFund serviceFund1 = serviceFundMapper.selectById(fundid); |
| | | log.info("fund表的fundtextime值为:{}", serviceFund1.getFundtaxtime()); |
| | | //保存总税前金额(用于更新fund表里的) |
| | | BigDecimal pretaxcost = BigDecimal.valueOf(0.00); |
| | | BigDecimal taxedcost = BigDecimal.valueOf(0.00); |
| | | for (ServiceFunddetail serviceFunddetail : serviceFunddetails) { |
| | | if (serviceFunddetail.getBeneficiaryname().equals("吴康松")) { |
| | | System.out.println(serviceFunddetail); |
| | | } |
| | | if (StringUtils.isEmpty(serviceFunddetail.getIdcardno())) { |
| | | logger.info("serviceFunddetail数据入参 : {}", serviceFunddetail); |
| | | //如果身份证号为空,就不用查了,可能是医院的记录 |
| | | if (StringUtils.isEmpty(serviceFunddetail.getServicesscopename()) || !serviceFunddetail.getServicesscopename().contains("税后")) { |
| | | serviceFunddetail.setTaxedamount(serviceFunddetail.getAmount()); |
| | | } else { |
| | | serviceFunddetail.setAmount(serviceFunddetail.getTaxedamount()); |
| | | } |
| | | //公司的申请金额也要加进去 |
| | | pretaxcost = pretaxcost.add(BigDecimal.valueOf(serviceFunddetail.getAmount())); |
| | | taxedcost = taxedcost.add(BigDecimal.valueOf(serviceFunddetail.getTaxedamount())); |
| | | // 4. 初始化累计金额、待更新明细列表、序号缓存、累计金额缓存 |
| | | BigDecimal pretaxcost = BigDecimal.ZERO; |
| | | BigDecimal taxedcost = BigDecimal.ZERO; |
| | | |
| | | serviceFunddetail.setTaxamount(0.0); |
| | | logger.info("serviceFunddetail更新后的数据 : {}", serviceFunddetail); |
| | | boolean b = this.updateById(serviceFunddetail); |
| | | logger.info("serviceFunddetail修改结果 : {}", b); |
| | | //创建一个用于存放“需要更新到数据库的ServiceFunddetail对象”的列表 |
| | | List<ServiceFunddetail> updateList = new ArrayList<>(); |
| | | |
| | | // 5. 遍历每条明细,分情况处理 |
| | | for (ServiceFunddetail detail : serviceFunddetails) { |
| | | // 5.1 身份证号为空,直接赋值(一些人或公司,不需要算税) |
| | | if (StringUtils.isEmpty(detail.getIdcardno())) { |
| | | if (StringUtils.isEmpty(detail.getServicesscopename()) || !detail.getServicesscopename().contains("税后")) { |
| | | // 普通明细,税后金额=申请金额 |
| | | detail.setTaxedamount(detail.getAmount()); |
| | | } else { |
| | | // 税后明细,申请金额=税后金额 |
| | | detail.setAmount(detail.getTaxedamount()); |
| | | } |
| | | // 累计本批次总金额 |
| | | pretaxcost = pretaxcost.add(BigDecimal.valueOf(detail.getAmount())); |
| | | taxedcost = taxedcost.add(BigDecimal.valueOf(detail.getTaxedamount())); |
| | | detail.setTaxamount(0.0); |
| | | updateList.add(detail); |
| | | continue; |
| | | } |
| | | |
| | | //获取当月的第一天 |
| | | Date firstDay = Date.from(LocalDate.now().with(TemporalAdjusters.firstDayOfMonth()).atStartOfDay(ZoneId.systemDefault()).toInstant()); |
| | | //获取到当前人的本月算税序号最大值 |
| | | TaxMoneyByItemEO taxMoneyByItemEO = new TaxMoneyByItemEO(); |
| | | taxMoneyByItemEO.setFirstDay(firstDay); |
| | | taxMoneyByItemEO.setIDCard(serviceFunddetail.getIdcardno()); |
| | | taxMoneyByItemEO.setTaxTime(new Date()); |
| | | //根据条件获取表中该条数据的”序号“最大值 |
| | | Long maxXH = serviceFunddetailMapper.getMaxXH(taxMoneyByItemEO); |
| | | if (maxXH == null) { |
| | | maxXH = 1L; |
| | | } else { |
| | | maxXH = maxXH + 1; |
| | | } |
| | | //序号Map与数据库表里的xh同时不为空,那就以map里的为准 |
| | | if (ObjectUtils.isNotEmpty(xhMap)) { |
| | | Long xhValue = xhMap.get(serviceFunddetail.getIdcardno()); |
| | | if (xhValue != null) maxXH = xhValue + 1; |
| | | } |
| | | // 5.2 身份证号不为空,需查历史累计金额、序号 |
| | | String idcard = detail.getIdcardno().trim(); |
| | | |
| | | TaxMoneyByItemEO taxMoneyVO = new TaxMoneyByItemEO(); |
| | | if (serviceFund1.getFundtaxtime() != null) { |
| | | taxMoneyVO.setTaxTime(serviceFund1.getFundtaxtime()); |
| | | } else { |
| | | taxMoneyVO.setTaxTime(new Date()); |
| | | } |
| | | taxMoneyVO.setFirstDay(firstDay); |
| | | taxMoneyVO.setFundID(serviceFunddetail.getFundid()); |
| | | taxMoneyVO.setIDCard(serviceFunddetail.getIdcardno()); |
| | | if (serviceFunddetail.getXh() != null) { |
| | | taxMoneyVO.setXh(serviceFunddetail.getXh()); |
| | | } else { |
| | | taxMoneyVO.setXh(maxXH); |
| | | xhMap.put(serviceFunddetail.getIdcardno(), maxXH); |
| | | } |
| | | //查出税前、税、税后的总额(不包含本次) |
| | | TaxMoneySumEO taxSum = serviceFunddetailMapper.getTaxSum(taxMoneyVO); |
| | | logger.info("查询的taxSum的值 : {}", taxSum); |
| | | if (ObjectUtils.isEmpty(taxSum)) { |
| | | // 5.2.1 查询本月累计金额(缓存避免重复查库) |
| | | TaxMoneySumEO taxSum = tempTaxSumMap.getOrDefault(idcard, null); |
| | | if (taxSum == null) { |
| | | taxSum = new TaxMoneySumEO(); |
| | | taxSum.setXh(1L); |
| | | detail.setXh(1L); |
| | | tempTaxSumMap.put(idcard, taxSum); |
| | | detail.setAllAmount(taxSum.getAmounts()); |
| | | } else { |
| | | long xh = taxSum.getXh() + 1; |
| | | taxSum.setXh(xh); |
| | | tempTaxSumMap.put(idcard, taxSum); |
| | | detail.setXh(xh); |
| | | detail.setAllAmount(taxSum.getAmounts()); |
| | | } |
| | | |
| | | //判断临时集合temporarySave中,有没有已经算过的数据 |
| | | if (CollectionUtils.isNotEmpty(temporarySave)) { |
| | | for (ServiceFunddetail serviceFunddetail1 : temporarySave) { |
| | | //如果本次的”人员类型“、”身份证号“在临时集合中存在,则将集合数据中的该受益人的,税前、税金、税后,和总的相加,并将老的覆盖 |
| | | // if (serviceFunddetail1.getApplytype().equals(serviceFunddetail.getApplytype()) && serviceFunddetail1.getBeneficiaryno().equals(serviceFunddetail.getBeneficiaryno())) { |
| | | if (serviceFunddetail1.getIdcardno().trim().equals(serviceFunddetail.getIdcardno().trim())) { |
| | | //将集合数据中的该受益人的,税前、税金、税后,和总的相加 |
| | | BigDecimal amounts = BigDecimal.valueOf(taxSum.getAmounts()).add(new BigDecimal(String.valueOf(serviceFunddetail1.getAmount()))); |
| | | BigDecimal taxAmounts = BigDecimal.valueOf(taxSum.getTaxAmounts()).add(new BigDecimal(String.valueOf(serviceFunddetail1.getTaxamount()))); |
| | | BigDecimal TaxedAmounts = BigDecimal.valueOf(taxSum.getTaxedAmounts()).add(new BigDecimal(String.valueOf(serviceFunddetail1.getTaxedamount()))); |
| | | //将老的覆盖 |
| | | taxSum.setAmounts(amounts.doubleValue()); |
| | | taxSum.setTaxAmounts(taxAmounts.doubleValue()); |
| | | taxSum.setTaxedAmounts(TaxedAmounts.doubleValue()); |
| | | } |
| | | continue; |
| | | // 5.2.3 算税逻辑 |
| | | if (detail.getServicesscopename().contains("税后")) { |
| | | // 税后明细:累计税后金额,反推税前金额和税金 |
| | | if (idcard.equals("330103196704060019")) { |
| | | log.info("330103196704060019"); |
| | | } |
| | | } |
| | | if (serviceFunddetail.getXh() == null) { |
| | | serviceFunddetail.setXh(maxXH); |
| | | } |
| | | |
| | | if (StringUtils.isEmpty(serviceFunddetail.getServicesscopename()) || !serviceFunddetail.getServicesscopename().contains("税后")) { |
| | | logger.info("计算税前信息开始:{}", serviceFunddetail); |
| | | logger.info("taxSum入参数据:{}", taxSum); |
| | | // 如何税前金额不为空,用之前的”总税前金额“,加上当前”税前金额“ |
| | | BigDecimal newAmounts = BigDecimal.valueOf(taxSum.getAmounts()).add(BigDecimal.valueOf(serviceFunddetail.getAmount())); |
| | | |
| | | // 计算总税金(包含本次) |
| | | String taxation = TaxtUtils.getTaxation(newAmounts); |
| | | //计算本次税金(总税金(包含本次) - 总税金(不包含本次)) |
| | | BigDecimal taxAmountNow = new BigDecimal(taxation).subtract(new BigDecimal(String.valueOf(taxSum.getTaxAmounts()))); |
| | | |
| | | //计算本次的税后金额(本次的税前金额 - 税金) |
| | | BigDecimal texdAfterNow = BigDecimal.valueOf(serviceFunddetail.getAmount()).subtract(taxAmountNow); |
| | | logger.info("打印newAmounts:{},taxation:{},taxAmountNow:{},texdAfterNow:{}", newAmounts, taxation, taxAmountNow, texdAfterNow); |
| | | |
| | | //将循环的数据重新放到一个新对象中 |
| | | ServiceFunddetail serviceFunddetail3 = DtoConversionUtils.sourceToTarget(serviceFunddetail, ServiceFunddetail.class); |
| | | serviceFunddetail3.setTaxamount(taxAmountNow.doubleValue()); |
| | | serviceFunddetail3.setTaxedamount(texdAfterNow.doubleValue()); |
| | | if (ObjectUtils.isEmpty(serviceFund1.getFundtaxtime())) { |
| | | serviceFunddetail3.setTaxTime(new Date()); |
| | | } else { |
| | | serviceFunddetail3.setTaxTime(serviceFund1.getFundtaxtime()); |
| | | } |
| | | logger.info("打印税前serviceFunddetail3数据 :{}", serviceFunddetail3); |
| | | |
| | | // 将该条数据更新 |
| | | |
| | | boolean b = this.updateById(serviceFunddetail3); |
| | | pretaxcost = pretaxcost.add(BigDecimal.valueOf(serviceFunddetail.getAmount())); |
| | | taxedcost = taxedcost.add(BigDecimal.valueOf(serviceFunddetail.getTaxedamount())); |
| | | // // 把该数据,放到临时的集合中 |
| | | temporarySave.add(serviceFunddetail3); |
| | | } else if (serviceFunddetail.getServicesscopename().contains("税后")) { |
| | | logger.info("计算税后信息开始:{}", serviceFunddetail); |
| | | logger.info("taxSum入参数据:{}", taxSum); |
| | | //税后不为空, 用之前的”总税后金额“,加上当前”税后金额“ |
| | | BigDecimal newTaxedAmounts = BigDecimal.valueOf(taxSum.getTaxedAmounts()).add(BigDecimal.valueOf(serviceFunddetail.getTaxedamount())); |
| | | |
| | | //通过总税后(包含本次)算出总税前 |
| | | BigDecimal newTaxedAmounts = BigDecimal.valueOf(taxSum.getTaxedAmounts()).add(BigDecimal.valueOf(detail.getTaxedamount())); |
| | | String taxationBefore = TaxtUtils.getTaxationBefore(newTaxedAmounts); |
| | | //算出本次税前(总税前(包含本次) - 总税前(不包含本次)) |
| | | BigDecimal amountNow = new BigDecimal(taxationBefore).subtract(BigDecimal.valueOf(taxSum.getAmounts())); |
| | | |
| | | //算出本次税金(本次税前 - 本次税后) |
| | | BigDecimal tax = amountNow.subtract(BigDecimal.valueOf(serviceFunddetail.getTaxedamount())); |
| | | logger.info("打印newTaxedAmounts:{},taxationBefore:{},amountNow:{},tax:{}", newTaxedAmounts, taxationBefore, amountNow, tax); |
| | | |
| | | //将循环的数据重新放到一个新对象中 |
| | | ServiceFunddetail serviceFunddetail3 = DtoConversionUtils.sourceToTarget(serviceFunddetail, ServiceFunddetail.class); |
| | | serviceFunddetail3.setAmount(amountNow.doubleValue()); |
| | | serviceFunddetail3.setTaxamount(tax.doubleValue()); |
| | | BigDecimal tax = amountNow.subtract(BigDecimal.valueOf(detail.getTaxedamount())); |
| | | detail.setAmount(amountNow.doubleValue()); |
| | | detail.setTaxamount(tax.doubleValue()); |
| | | if (ObjectUtils.isEmpty(serviceFund1.getFundtaxtime())) { |
| | | serviceFunddetail3.setTaxTime(new Date()); |
| | | detail.setTaxTime(new Date()); |
| | | } else { |
| | | serviceFunddetail3.setTaxTime(serviceFund1.getFundtaxtime()); |
| | | detail.setTaxTime(serviceFund1.getFundtaxtime()); |
| | | } |
| | | logger.info("打印serviceFunddetail3:{}", serviceFunddetail3); |
| | | //获取到当前人的本月算税序号最大值 |
| | | TaxMoneyByItemEO byItemEO = new TaxMoneyByItemEO(); |
| | | byItemEO.setFirstDay(firstDay); |
| | | byItemEO.setIDCard(serviceFunddetail.getIdcardno()); |
| | | byItemEO.setTaxTime(new Date()); |
| | | // 将该条数据更新 |
| | | updateById(serviceFunddetail3); |
| | | pretaxcost = pretaxcost.add(BigDecimal.valueOf(serviceFunddetail3.getAmount())); |
| | | taxedcost = taxedcost.add(BigDecimal.valueOf(serviceFunddetail3.getTaxedamount())); |
| | | // // 把该数据,放到临时的集合中 |
| | | temporarySave.add(serviceFunddetail3); |
| | | if (tax.doubleValue() < 0) { |
| | | log.info("小于0了"); |
| | | } |
| | | taxSum.setAmounts((BigDecimal.valueOf(taxSum.getAmounts()).add(amountNow)).doubleValue()); |
| | | taxSum.setTaxAmounts((BigDecimal.valueOf(taxSum.getTaxAmounts()).add(tax)).doubleValue()); |
| | | taxSum.setTaxedAmounts(newTaxedAmounts.doubleValue()); |
| | | } else { |
| | | // 税前明细:累计税前金额,算总税金,再算本次税金和税后金额 |
| | | BigDecimal newAmounts = BigDecimal.valueOf(taxSum.getAmounts()).add(BigDecimal.valueOf(detail.getAmount())); |
| | | if (idcard.equals("330103196704060019")) { |
| | | log.info("330103196704060019"); |
| | | } |
| | | String taxation = TaxtUtils.getTaxation(newAmounts); |
| | | BigDecimal taxAmountNow = new BigDecimal(taxation).subtract(new BigDecimal(String.valueOf(taxSum.getTaxAmounts()))); |
| | | BigDecimal texdAfterNow = BigDecimal.valueOf(detail.getAmount()).subtract(taxAmountNow); |
| | | detail.setTaxamount(taxAmountNow.doubleValue()); |
| | | detail.setTaxedamount(texdAfterNow.doubleValue()); |
| | | if (taxAmountNow.doubleValue() < 0) { |
| | | log.info("小于0了"); |
| | | } |
| | | |
| | | if (ObjectUtils.isEmpty(serviceFund1.getFundtaxtime())) { |
| | | detail.setTaxTime(new Date()); |
| | | } else { |
| | | detail.setTaxTime(serviceFund1.getFundtaxtime()); |
| | | } |
| | | taxSum.setAmounts(newAmounts.doubleValue()); |
| | | taxSum.setTaxAmounts((BigDecimal.valueOf(taxSum.getTaxAmounts()).add(taxAmountNow).doubleValue())); |
| | | taxSum.setTaxedAmounts((BigDecimal.valueOf(taxSum.getTaxedAmounts()).add(texdAfterNow)).doubleValue()); |
| | | } |
| | | |
| | | updateList.add(detail); |
| | | |
| | | pretaxcost = pretaxcost.add(BigDecimal.valueOf(detail.getAmount())); |
| | | taxedcost = taxedcost.add(BigDecimal.valueOf(detail.getTaxedamount())); |
| | | |
| | | } |
| | | // 6. 批量更新所有明细 |
| | | for (ServiceFunddetail d : updateList) { |
| | | this.updateById(d); |
| | | } |
| | | // 7. 更新主表ServiceFund |
| | | ServiceFund serviceFund = new ServiceFund(); |
| | | serviceFund.setPretaxcost(pretaxcost.doubleValue()); |
| | | serviceFund.setTaxedcost(taxedcost.doubleValue()); |
| | | serviceFund.setId(serviceFunddetails.get(0).getFundid()); |
| | | if (ObjectUtils.isEmpty(serviceFund1.getFundtaxtime())) { |
| | | serviceFund.setFundtaxtime(new Date()); |
| | | } |
| | | // 1 已算税 |
| | | serviceFund.setFundtaxtime(new Date()); |
| | | |
| | | serviceFund.setIstax(1); |
| | | serviceFundMapper.updateById(serviceFund); |
| | | |
| | | return true; |
| | | } |
| | | |
| | | // @Override |
| | | // public Boolean calculateTax2(List<ServiceFunddetail> serviceFunddetails) { |
| | | // //用于缓存每个身份证号(idcardno)本月累计的税前、税金、税后金额(TaxMoneySumEO对象) |
| | | // Map<String, TaxMoneySumEO> tempTaxSumMap = new HashMap<>(); |
| | | // |
| | | // TaxMoneyByItemEO taxMoneyVO = new TaxMoneyByItemEO(); |
| | | // Date firstDay = Date.from(LocalDate.now().with(TemporalAdjusters.firstDayOfMonth()).atStartOfDay(ZoneId.systemDefault()).toInstant()); |
| | | // taxMoneyVO.setFirstDay(firstDay); |
| | | // taxMoneyVO.setTaxTime(new Date()); |
| | | // |
| | | // List<TaxMoneySumEO> taxSum = serviceFunddetailMapper.getTaxSum(taxMoneyVO); |
| | | // for (TaxMoneySumEO taxMoneySumEO : taxSum) { |
| | | // tempTaxSumMap.put(taxMoneySumEO.getIDCardNo(), taxMoneySumEO); |
| | | // } |
| | | // |
| | | // return calculateTax(serviceFunddetails, tempTaxSumMap); |
| | | // } |
| | | |
| | | |
| | | @Override |
| | | public Map<String, Double> performance(ServiceFunddetail serviceFunddetail) { |
| | | //根据"身份证号"和"绩效日期(年份)"查询个人历史绩效 |