shiyunteng
2026-08-27 b1b3da5393c75ced1afa1467be69d60af5403878
platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/ContractInvoiceServiceImpl.java
@@ -5,7 +5,6 @@
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.StreamProgress;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjUtil;
@@ -104,8 +103,11 @@
                  .eq(ContractSubjectMatter::getMaterialCode, contractInvoiceOutBoundVo.getSubjectMatterCode())
                  .eq(StrUtil.isNotBlank(contractInvoiceOutBoundVo.getLineNo()), ContractSubjectMatter::getLineNo, contractInvoiceOutBoundVo.getLineNo())
                  .last("limit 1"));
            if (ObjUtil.isNotNull(subjectMatter) && subjectMatter.getAdvanceNum().compareTo(new BigDecimal("0")) > 0) {
            if (ObjUtil.isNotNull(subjectMatter)) {
               contractInvoiceOutBoundVo.setUnitPrice(subjectMatter.getUnitPrice());
               if (subjectMatter.getAdvanceNum().compareTo(new BigDecimal("0")) > 0) {
               contractInvoiceOutBoundVo.setInvoiceNum(contractInvoiceOutBoundVo.getOutBoundNum().subtract(subjectMatter.getAdvanceNum()));
               }
            }
            contractInvoiceOutBoundVo.setToInvoiceNum(contractInvoiceOutBoundVo.getInvoiceNum());
         });
@@ -145,6 +147,11 @@
   public R genInvoiceInfo(GenInvoiceInfoDTO genInvoiceInfoDTO) {
      if (CollUtil.isEmpty(genInvoiceInfoDTO.getOutBoundInvoiceList())) {
         return R.failed("请选择开票出库单后再操作");
      }
      genInvoiceInfoDTO.setOutBoundInvoiceList(genInvoiceInfoDTO.getOutBoundInvoiceList().stream().filter(item->
            item.getInvoiceNum().compareTo(new BigDecimal("0"))>0).collect(Collectors.toList()));
      if (CollUtil.isEmpty(genInvoiceInfoDTO.getOutBoundInvoiceList())) {
         return R.failed("请选择开票出库单后或调整开票数量再操作");
      }
      Long contractId = null;
      //预收款开票
@@ -318,11 +325,14 @@
         if (CollUtil.isNotEmpty(genInvoiceInfoDTO.getOutBoundInvoiceList())) {
            genInvoiceInfoDTO.getOutBoundInvoiceList().stream().forEach(contractOutBoundInvoiceVo -> {
               ContractOutBound contractOutBound = contractOutBoundMapper.selectById(contractOutBoundInvoiceVo.getContractOutBoundId());
               if (!CollUtil.contains(erpCode, contractOutBound.getSubjectMatterCode())) {
                  erpCode.add(contractOutBound.getSubjectMatterCode());
               ContractSubjectMatter subjectMatter = subjectMatterMapper.selectOne(Wrappers.<ContractSubjectMatter>lambdaQuery()
                     .eq(ContractSubjectMatter::getMaterialCode, contractOutBound.getSubjectMatterCode())
                     .eq(StrUtil.isNotBlank(contractOutBound.getLineNo()),ContractSubjectMatter::getLineNo,contractOutBound.getLineNo())
                     .eq(ContractSubjectMatter::getContractId, contract.getId()).last("limit 1"));
               if (!CollUtil.contains(erpCode, contractOutBound.getSubjectMatterCode()+subjectMatter.getUnitPrice())) {
                  erpCode.add(contractOutBound.getSubjectMatterCode()+subjectMatter.getUnitPrice());
                  ContractOutBoundMergeVo mergeVo = new ContractOutBoundMergeVo();
                  mergeVo.setMaterialCode(contractOutBound.getSubjectMatterCode());
                  mergeVo.setMaterialCode(contractOutBound.getSubjectMatterCode()+subjectMatter.getUnitPrice());
                  mergeVo.setMaterialInternalName(contractOutBound.getSubjectMatterName());
                  mergeVo.setQuantity(contractOutBoundInvoiceVo.getInvoiceNum());
                  Product product = productMapper.selectOne(Wrappers.<Product>lambdaQuery().eq(Product::getErpCode, contractOutBound.getSubjectMatterCode())
@@ -333,9 +343,6 @@
                     mergeVo.setTaxRate(new BigDecimal(product.getTaxRate()));
                     mergeVo.setTaxCode(product.getTaxCode());
                  }
                  ContractSubjectMatter subjectMatter = subjectMatterMapper.selectOne(Wrappers.<ContractSubjectMatter>lambdaQuery()
                        .eq(ContractSubjectMatter::getMaterialCode, contractOutBound.getSubjectMatterCode())
                        .eq(ContractSubjectMatter::getContractId, contract.getId()).last("limit 1"));
                  if (ObjUtil.isNotNull(subjectMatter)) {
                     mergeVo.setUnitPrice(subjectMatter.getUnitPrice());
                     mergeVo.setUnit(subjectMatter.getUnit());
@@ -352,7 +359,7 @@
                  mergeOutBounds.add(mergeVo);
               } else {
                  List<ContractOutBoundMergeVo> filteredList = mergeOutBounds.stream()
                        .filter(item -> contractOutBound.getSubjectMatterCode().equals(item.getMaterialCode())) // 防止空指针,建议常量在前
                        .filter(item -> item.getMaterialCode().equals(subjectMatter.getMaterialCode()+subjectMatter.getUnitPrice())) // 防止空指针,建议常量在前
                        .collect(Collectors.toList());
                  if (ArrayUtil.isNotEmpty(filteredList)) {
                     filteredList.get(0).setQuantity(filteredList.get(0).getQuantity().add(contractOutBound.getOutBoundNum()));
@@ -394,8 +401,12 @@
                     mergeVo.setSpecification(product.getProductType());
                     mergeVo.setTaxRate(new BigDecimal(product.getTaxRate()));
                     mergeVo.setTaxCode(product.getTaxCode());
                     Product productClass = productMapper.selectById(product.getParentId());
                     if (ObjUtil.isNotNull(product)) {
                  }
                  ContractSubjectMatter subjectMatter = subjectMatterMapper.selectOne(Wrappers.<ContractSubjectMatter>lambdaQuery()
                        .eq(ContractSubjectMatter::getMaterialCode, schedule.getSubjectMatterCode())
                        .eq(ContractSubjectMatter::getContractId, contract.getId()).last("limit 1"));
                  if (ObjUtil.isNotNull(subjectMatter)) {
                     mergeVo.setUnitPrice(subjectMatter.getUnitPrice());
                        if (StrUtil.equals(product.getErpCode(), "water_bill")) {
                           mergeVo.setItemName("*" + product.getTaxClass() + "*水费");
                           mergeVo.setUnit("吨");
@@ -405,16 +416,9 @@
                        } else if (StrUtil.equals(product.getErpCode(), "house_rental")) {
                           mergeVo.setItemName("*" + product.getTaxClass() + "*房屋租赁");
                        } else {
                           mergeVo.setItemName("*" + product.getTaxClass() + "*" + productClass.getProductName());
                        mergeVo.setItemName("*" + product.getTaxClass() + "*" + subjectMatter.getMaterialName());
                        }
                     }
                  }
                  ContractSubjectMatter subjectMatter = subjectMatterMapper.selectOne(Wrappers.<ContractSubjectMatter>lambdaQuery()
                        .eq(ContractSubjectMatter::getMaterialCode, schedule.getSubjectMatterCode())
                        .eq(ContractSubjectMatter::getContractId, contract.getId()).last("limit 1"));
                  if (ObjUtil.isNotNull(subjectMatter)) {
                     mergeVo.setUnitPrice(subjectMatter.getUnitPrice());
                  }
                  mergeOutBounds.add(mergeVo);
               } else {
@@ -465,7 +469,7 @@
         genInvoiceInfoDTO.getOutBoundInvoiceList().stream().forEach(contractOutBoundInvoiceVo -> {
            ContractSubjectMatter subjectMatter = contractSubjectMatterMapper.selectById(contractOutBoundInvoiceVo.getContractOutBoundId());
            if (ObjUtil.isNotNull(subjectMatter)) {
               if (!CollUtil.contains(erpCode, subjectMatter.getMaterialCode())) {
               if (!CollUtil.contains(erpCode, subjectMatter.getMaterialCode()+subjectMatter.getUnitPrice())) {
                  erpCode.add(subjectMatter.getMaterialCode());
                  ContractOutBoundMergeVo mergeVo = new ContractOutBoundMergeVo();
                  mergeVo.setMaterialCode(subjectMatter.getMaterialCode());
@@ -478,7 +482,7 @@
                     mergeVo.setSpecification(product.getProductType());
                     mergeVo.setTaxRate(new BigDecimal(product.getTaxRate()));
                     mergeVo.setTaxCode(product.getTaxCode());
                     Product productClass = productMapper.selectById(product.getParentId());
                  }
                     if (ObjUtil.isNotNull(product)) {
                        if (StrUtil.equals(product.getErpCode(), "water_bill")) {
                           mergeVo.setItemName("*" + product.getTaxClass() + "*水费");
@@ -489,10 +493,9 @@
                        } else if (StrUtil.equals(product.getErpCode(), "house_rental")) {
                           mergeVo.setItemName("*" + product.getTaxClass() + "*房屋租赁");
                        } else {
                           mergeVo.setItemName("*" + product.getTaxClass() + "*" + productClass.getProductName());
                        mergeVo.setItemName("*" + product.getTaxClass() + "*" + subjectMatter.getMaterialName());
                        }
                     }
                  }
                  mergeVo.setUnitPrice(subjectMatter.getUnitPrice());
                  mergeVo.setUnit(subjectMatter.getUnit());
@@ -523,8 +526,13 @@
   @Override
   public R toInvoice(GenInvoiceInfoDTO genInvoiceInfoDTO) {
      if (ArrayUtil.isEmpty(genInvoiceInfoDTO.getOutBoundInvoiceList())) {
      if (CollUtil.isEmpty(genInvoiceInfoDTO.getOutBoundInvoiceList())) {
         return R.failed("请选择开票出库单后再操作");
      }
      genInvoiceInfoDTO.setOutBoundInvoiceList(genInvoiceInfoDTO.getOutBoundInvoiceList().stream().filter(item->
            item.getInvoiceNum().compareTo(new BigDecimal("0"))>0).collect(Collectors.toList()));
      if (CollUtil.isEmpty(genInvoiceInfoDTO.getOutBoundInvoiceList())) {
         return R.failed("请选择开票出库单后或调整开票数量再操作");
      }
      if (StrUtil.equals(genInvoiceInfoDTO.getInvoiceType(), "3") || StrUtil.equals(genInvoiceInfoDTO.getInvoiceType(), "4")) {
         if (StrUtil.isBlank(genInvoiceInfoDTO.getVehicleNo())) {
@@ -646,9 +654,7 @@
         genInvoiceInfoDTO.getOutBoundInvoiceList().stream().forEach(contractOutBoundInvoiceVo -> {
            ContractOutBound contractOutBound = contractOutBoundMapper.selectById(contractOutBoundInvoiceVo.getContractOutBoundId());
            contractOutBound.setInvoiceStatus("1");
            if (contractOutBoundInvoiceVo.getInvoiceNum().compareTo(contractOutBound.getOutBoundNum().subtract(contractOutBound.getInvoiceNum())) <= 0) {
               contractOutBound.setInvoiceNum(contractOutBound.getInvoiceNum().add(contractOutBoundInvoiceVo.getInvoiceNum()));
            }
            contractOutBoundMapper.updateById(contractOutBound);
         });
      } else {
@@ -1220,11 +1226,12 @@
         }
         if (!StrUtil.equals(contract.getContractCategory(), "water_house")) {
            //合同状态
            if (StrUtil.equals(contract.getBillingStatus(), "2")) {
               contract.setBillingAmout(vo.getTotalPrice().add(contract.getBillingAmout()));
               if (contract.getBillingAmout().compareTo(contract.getAmount()) == 0 &&
                     contract.getPaidAmount().compareTo(contract.getAmount().subtract(contract.getDropAmount())) == 0) {
            if (contract.getBillingAmout().compareTo(contract.getAmount()) == 0) {
                  contract.setBillingStatus("3");
            }
            if (contract.getPaidAmount().compareTo(contract.getAmount().subtract(contract.getDropAmount())) >= 0&&
                  contract.getBillingAmout().compareTo(contract.getAmount()) == 0){
                  contract.setContractStatus(3);
               }
               if (contract.getBillingAmout().compareTo(contract.getAmount()) < 0) {
@@ -1242,7 +1249,7 @@
                  contract.setBillingAmout(contract.getAmount());
               }
               contractMapper.updateById(contract);
            }
         } else {
            //判断 循环是否完成
            List<ContractExecDate> execDateList = contractExecDateMapper.selectList(Wrappers.<ContractExecDate>lambdaQuery().eq(ContractExecDate::getContractId, contract.getId())
@@ -1257,8 +1264,11 @@
                  contract.setBillingStatus("2");
               }
               contract.setBillingAmout(vo.getTotalPrice().add(contract.getBillingAmout()));
               if (contract.getBillingAmout().compareTo(contract.getAmount()) == 0) {
                  contract.setBillingStatus("3");
               }
               if (contract.getBillingAmout().compareTo(contract.getAmount()) == 0 &&
                     contract.getPaidAmount().compareTo(contract.getAmount().subtract(contract.getDropAmount())) == 0) {
                     contract.getPaidAmount().compareTo(contract.getAmount().subtract(contract.getDropAmount())) >= 0) {
                  contract.setBillingStatus("3");
                  contract.setContractStatus(3);
               }
@@ -1352,10 +1362,8 @@
            if (ArrayUtil.isNotEmpty(outBoundInvoiceVoList)) {
               outBoundInvoiceVoList.stream().forEach(contractOutBoundInvoiceVo -> {
                  ContractOutBound contractOutBound = contractOutBoundMapper.selectById(contractOutBoundInvoiceVo.getContractOutBoundId());
                  if (contractOutBoundInvoiceVo.getInvoiceNum().compareTo(contractOutBound.getOutBoundNum().subtract(contractOutBound.getInvoiceNum())) <= 0) {
                     contractOutBound.setInvoiceNum(contractOutBound.getInvoiceNum().subtract(contractOutBoundInvoiceVo.getInvoiceNum()));
                     contractOutBoundMapper.updateById(contractOutBound);
                  }
               });
            }
         } else {
@@ -1437,10 +1445,8 @@
            if (ArrayUtil.isNotEmpty(outBoundInvoiceVoList)) {
               outBoundInvoiceVoList.stream().forEach(contractOutBoundInvoiceVo -> {
                  ContractOutBound contractOutBound = contractOutBoundMapper.selectById(contractOutBoundInvoiceVo.getContractOutBoundId());
                  if (contractOutBoundInvoiceVo.getInvoiceNum().compareTo(contractOutBound.getOutBoundNum().subtract(contractOutBound.getInvoiceNum())) <= 0) {
                     contractOutBound.setInvoiceNum(contractOutBound.getInvoiceNum().subtract(contractOutBoundInvoiceVo.getInvoiceNum()));
                     contractOutBoundMapper.updateById(contractOutBound);
                  }
               });
            }
         } else {