shiyunteng
2026-09-07 c631edf462f4ab19403f5194dec743168e2a0e79
platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/ContractInvoiceServiceImpl.java
@@ -133,6 +133,37 @@
   }
   @Override
   public R getMergeContractOutBound(Long[] ids) {
      if (CollUtil.isEmpty(Arrays.asList(ids))) {
         return R.failed("请选择合并开票的合同");
      }
      MPJLambdaWrapper<ContractOutBound> wrapper = new MPJLambdaWrapper<ContractOutBound>()
            .selectAll(ContractOutBound.class)
            .in(ContractOutBound::getContractId, ids);
      List<ContractInvoiceOutBoundVo> invoiceOutBoundVoList = contractOutBoundMapper.selectJoinList(ContractInvoiceOutBoundVo.class, wrapper);
      if (CollUtil.isNotEmpty(invoiceOutBoundVoList)) {
         invoiceOutBoundVoList.stream().forEach(contractInvoiceOutBoundVo -> {
            contractInvoiceOutBoundVo.setInvoiceNum(contractInvoiceOutBoundVo.getOutBoundNum().subtract(contractInvoiceOutBoundVo.getInvoiceNum()));
            ContractSubjectMatter subjectMatter = contractSubjectMatterMapper.selectOne(Wrappers.<ContractSubjectMatter>lambdaQuery()
                  .in(ContractSubjectMatter::getContractId, ids)
                  .eq(ContractSubjectMatter::getMaterialCode, contractInvoiceOutBoundVo.getSubjectMatterCode())
                  .eq(StrUtil.isNotBlank(contractInvoiceOutBoundVo.getLineNo()), ContractSubjectMatter::getLineNo, contractInvoiceOutBoundVo.getLineNo())
                  .last("limit 1"));
            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());
         });
      }
      return R.ok(invoiceOutBoundVoList.stream().filter(item -> item.getUnitPrice().compareTo(new BigDecimal("0")) > 0).collect(Collectors.toList()));
   }
   @Override
   public R getContractAdvance(Long contractId) {
      MPJLambdaWrapper<ContractSubjectMatter> wrapper = new MPJLambdaWrapper<ContractSubjectMatter>()
            .selectAll(ContractSubjectMatter.class)
@@ -231,7 +262,7 @@
         return R.failed("获取购方单位异常");
      }
      //机动车开票
      if (StrUtil.equals(genInvoiceInfoDTO.getInvoiceType(), "3") || StrUtil.equals(genInvoiceInfoDTO.getInvoiceType(), "4")) {
      if (StrUtil.equals(genInvoiceInfoDTO.getInvoiceType(), "4")) {
         if (StrUtil.isBlank(genInvoiceInfoDTO.getVehicleNo())) {
            return R.failed("机动车开票请输入车架号");
         }
@@ -256,6 +287,122 @@
      GenInvoiceInfoVo vo = genInvoiceInfoVo(genInvoiceInfoDTO, contract, b);
      vo = transBipVo(vo,b,customer);
      return R.ok(vo);
   }
   @Override
   public R genMergeInvoiceInfo(GenMergeInvoiceDTO mergeInvoiceDTO) {
      R<SysDept> r = remoteDeptService.getById(SecurityUtils.getUser().getCompId());
      if (!r.isOk()) {
         return R.failed("获取开票单位异常");
      }
      SysDept b = r.getData();
      BusinessCustomer customer = businessCustomerMapper.selectById(mergeInvoiceDTO.getBusGuestId());
      if (ObjUtil.isNull(customer)) {
         return R.failed("获取购方单位异常");
      }
      GenInvoiceInfoVo vo = genMergeInvoiceVo(mergeInvoiceDTO, customer, b);
      vo = transBipVo(vo, b, customer);
      return R.ok(vo);
   }
   private GenInvoiceInfoVo genMergeInvoiceVo(GenMergeInvoiceDTO mergeInvoiceDTO, BusinessCustomer customer, SysDept b) {
      GenInvoiceInfoVo vo = new GenInvoiceInfoVo();
      vo.setPartyA(customer.getCompanyName());
      vo.setPartyAOrgCode(customer.getCreditCode());
      vo.setPartyAOpenBank(customer.getBankName());
      vo.setPartyABankAccount(customer.getBankAccount());
      vo.setPartyB(b.getOrgName());
      vo.setPartyBOrgCode(b.getOrgCode());
      vo.setPartyBOpenBank(b.getOrgBank());
      vo.setPartyBBankAccount(b.getOrgBankAccount());
      vo.setInvoiceType(customer.getInvoiceType());
      vo.setRemarks(mergeInvoiceDTO.getRemarks());
      vo.setOutBoundInvoiceList(mergeInvoiceDTO.getOutBoundInvoiceList());
      vo.setContractIds(mergeInvoiceDTO.getContractIds());
      if (StrUtil.isNotBlank(mergeInvoiceDTO.getInvoiceType())) {
         vo.setInvoiceVehicleType(mergeInvoiceDTO.getInvoiceType());
      }
      List<InvoiceItemVo> invoiceItemVoList = new ArrayList<>();
      List<String> erpCode = new ArrayList<>();
      List<ContractOutBoundMergeVo> mergeOutBounds = new ArrayList<>();
      if (CollUtil.isNotEmpty(mergeInvoiceDTO.getOutBoundInvoiceList())) {
         mergeInvoiceDTO.getOutBoundInvoiceList().stream().forEach(contractOutBoundInvoiceVo -> {
            ContractOutBound contractOutBound = contractOutBoundMapper.selectById(contractOutBoundInvoiceVo.getContractOutBoundId());
            ContractSubjectMatter subjectMatter = subjectMatterMapper.selectOne(Wrappers.<ContractSubjectMatter>lambdaQuery()
                  .eq(ContractSubjectMatter::getMaterialCode, contractOutBound.getSubjectMatterCode())
                  .eq(StrUtil.isNotBlank(contractOutBound.getLineNo()), ContractSubjectMatter::getLineNo, contractOutBound.getLineNo())
                  .eq(ContractSubjectMatter::getContractId, contractOutBound.getContractId()).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() + subjectMatter.getUnitPrice());
               mergeVo.setMaterialInternalName(contractOutBound.getSubjectMatterName());
               mergeVo.setQuantity(contractOutBoundInvoiceVo.getInvoiceNum());
               Product product = new Product();
               List<String> smjList = new ArrayList<>();
               smjList.add(smj);
               smjList.add(smjsc);
               smjList.add(smjzt);
               if (("" + SecurityUtils.getUser().getCompId()).equals(smj) || ("" + SecurityUtils.getUser().getCompId()).equals(smjsc) ||
                     ("" + SecurityUtils.getUser().getCompId()).equals(smjzt)) {
                  product = productMapper.selectOne(Wrappers.<Product>lambdaQuery().eq(Product::getErpCode, contractOutBound.getSubjectMatterCode())
                        .in(Product::getCompId, smjList).last("limit 1"));
               } else {
                  product = productMapper.selectOne(Wrappers.<Product>lambdaQuery().eq(Product::getErpCode, contractOutBound.getSubjectMatterCode())
                        .eq(Product::getCompId, SecurityUtils.getUser().getCompId()).last("limit 1"));
               }
               if (ObjUtil.isNotNull(product)) {
                  mergeVo.setTaxClass(product.getTaxClass());
                  mergeVo.setSpecification(product.getProductType());
                  mergeVo.setTaxRate(new BigDecimal(product.getTaxRate()));
                  mergeVo.setTaxCode(product.getTaxCode());
               }
               if (ObjUtil.isNotNull(subjectMatter)) {
                  mergeVo.setUnitPrice(subjectMatter.getUnitPrice());
                  mergeVo.setUnit(subjectMatter.getUnit());
                  if (StrUtil.equals(product.getErpCode(), "water_bill")) {
                     mergeVo.setItemName("*" + product.getTaxClass() + "*水费");
                  } else if (StrUtil.equals(product.getErpCode(), "electricity_bill")) {
                     mergeVo.setItemName("*" + product.getTaxClass() + "*电费");
                  } else if (StrUtil.equals(product.getErpCode(), "house_rental")) {
                     mergeVo.setItemName("*" + product.getTaxClass() + "*房屋租赁");
                  } else {
                     mergeVo.setItemName("*" + product.getTaxClass() + "*" + subjectMatter.getMaterialName());
                  }
               }
               mergeOutBounds.add(mergeVo);
            } else {
               List<ContractOutBoundMergeVo> filteredList = mergeOutBounds.stream()
                     .filter(item -> item.getMaterialCode().equals(subjectMatter.getMaterialCode() + subjectMatter.getUnitPrice())) // 防止空指针,建议常量在前
                     .collect(Collectors.toList());
               if (CollUtil.isNotEmpty(filteredList)) {
                  filteredList.get(0).setQuantity(filteredList.get(0).getQuantity().add(contractOutBound.getOutBoundNum()));
               }
            }
         });
         if (CollUtil.isNotEmpty(mergeOutBounds)) {
            invoiceItemVoList = BeanUtil.copyToList(mergeOutBounds, InvoiceItemVo.class);
            invoiceItemVoList.stream().filter(invoiceItem -> invoiceItem.getUnitPrice().compareTo(new BigDecimal("0")) > 0)
                  .forEach(invoiceItemVo -> {
//            for (InvoiceItemVo invoiceItemVo:invoiceItemVoList) {
                     BigDecimal itemPrice = invoiceItemVo.getUnitPrice().multiply(invoiceItemVo.getQuantity());
                     BigDecimal itemPriceTax = itemPrice.divide(new BigDecimal("100").add(invoiceItemVo.getTaxRate()), 2, RoundingMode.HALF_UP).multiply(invoiceItemVo.getTaxRate());
                     invoiceItemVo.setPriceTax(itemPriceTax);
                     invoiceItemVo.setPriceNoTax(itemPrice.subtract(itemPriceTax));
                     invoiceItemVo.setPriceAndTax(itemPrice);
                  });
            vo.setTotalPrice(invoiceItemVoList.stream().map(item -> item.getPriceAndTax()).reduce(BigDecimal.ZERO, BigDecimal::add));
            vo.setTotalTax(invoiceItemVoList.stream().map(item -> item.getPriceTax()).reduce(BigDecimal.ZERO, BigDecimal::add));
            vo.setInvoiceItemList(invoiceItemVoList);
            vo.setPriceWords(Convert.digitToChinese(vo.getTotalPrice()));
         }
      }
      return vo;
   }
   private GenInvoiceInfoVo transBipVo(GenInvoiceInfoVo vo,SysDept comp,BusinessCustomer customer) {
@@ -385,6 +532,71 @@
      List<InvoiceItemVo> invoiceItemVoList = new ArrayList<>();
      List<String> erpCode = new ArrayList<>();
      List<ContractOutBoundMergeVo> mergeOutBounds = new ArrayList<>();
      if (genInvoiceInfoDTO.getInvoiceType().equals("3")){
         String[] vehiclenNoList = genInvoiceInfoDTO.getVehicleNo().split(",");
         for (int i = 0; i < vehiclenNoList.length; i++) {
            ContractOutBound contractOutBound = contractOutBoundMapper.selectById(genInvoiceInfoDTO.getOutBoundInvoiceList().get(0).getContractOutBoundId());
            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"));
            ContractOutBoundMergeVo mergeVo = new ContractOutBoundMergeVo();
            mergeVo.setMaterialCode(contractOutBound.getSubjectMatterCode() + subjectMatter.getUnitPrice());
            mergeVo.setMaterialInternalName(contractOutBound.getSubjectMatterName());
            mergeVo.setQuantity(new BigDecimal("1"));
            Product product = new Product();
            List<String> smjList = new ArrayList<>();
            smjList.add(smj);
            smjList.add(smjsc);
            smjList.add(smjzt);
            if (("" + SecurityUtils.getUser().getCompId()).equals(smj) || ("" + SecurityUtils.getUser().getCompId()).equals(smjsc) ||
                  ("" + SecurityUtils.getUser().getCompId()).equals(smjzt)) {
               product = productMapper.selectOne(Wrappers.<Product>lambdaQuery().eq(Product::getErpCode, contractOutBound.getSubjectMatterCode())
                     .in(Product::getCompId, smjList).last("limit 1"));
            } else {
               product = productMapper.selectOne(Wrappers.<Product>lambdaQuery().eq(Product::getErpCode, contractOutBound.getSubjectMatterCode())
                     .eq(Product::getCompId, SecurityUtils.getUser().getCompId()).last("limit 1"));
            }
            if (ObjUtil.isNotNull(product)) {
               mergeVo.setTaxClass(product.getTaxClass());
               mergeVo.setSpecification(vehiclenNoList[i]);
               mergeVo.setTaxRate(new BigDecimal(product.getTaxRate()));
               mergeVo.setTaxCode(product.getTaxCode());
            }
            if (ObjUtil.isNotNull(subjectMatter)) {
               mergeVo.setUnitPrice(subjectMatter.getUnitPrice());
               mergeVo.setUnit(subjectMatter.getUnit());
               if (StrUtil.equals(product.getErpCode(), "water_bill")) {
                  mergeVo.setItemName("*" + product.getTaxClass() + "*水费");
               } else if (StrUtil.equals(product.getErpCode(), "electricity_bill")) {
                  mergeVo.setItemName("*" + product.getTaxClass() + "*电费");
               } else if (StrUtil.equals(product.getErpCode(), "house_rental")) {
                  mergeVo.setItemName("*" + product.getTaxClass() + "*房屋租赁");
               } else {
                  mergeVo.setItemName("*" + product.getTaxClass() + "*" + subjectMatter.getMaterialName());
               }
            }
            mergeOutBounds.add(mergeVo);
         }
         if (CollUtil.isNotEmpty(mergeOutBounds)) {
            invoiceItemVoList = BeanUtil.copyToList(mergeOutBounds, InvoiceItemVo.class);
            invoiceItemVoList.stream().filter(invoiceItem -> invoiceItem.getUnitPrice().compareTo(new BigDecimal("0")) > 0)
                  .forEach(invoiceItemVo -> {
//            for (InvoiceItemVo invoiceItemVo:invoiceItemVoList) {
                     BigDecimal itemPrice = invoiceItemVo.getUnitPrice().multiply(invoiceItemVo.getQuantity());
                     BigDecimal itemPriceTax = itemPrice.divide(new BigDecimal("100").add(invoiceItemVo.getTaxRate()), 2, RoundingMode.HALF_UP).multiply(invoiceItemVo.getTaxRate());
                     invoiceItemVo.setPriceTax(itemPriceTax);
                     invoiceItemVo.setPriceNoTax(itemPrice.subtract(itemPriceTax));
                     invoiceItemVo.setPriceAndTax(itemPrice);
                  });
            vo.setTotalPrice(invoiceItemVoList.stream().map(item -> item.getPriceAndTax()).reduce(BigDecimal.ZERO, BigDecimal::add));
            vo.setTotalTax(invoiceItemVoList.stream().map(item -> item.getPriceTax()).reduce(BigDecimal.ZERO, BigDecimal::add));
            vo.setInvoiceItemList(invoiceItemVoList);
            vo.setPriceWords(Convert.digitToChinese(vo.getTotalPrice()));
         }
         return vo;
      }
//      List<ContractOutBound> outBounds = contractOutBoundMapper.selectList(Wrappers.<ContractOutBound>lambdaQuery()
//            .in(ContractOutBound::getId, genInvoiceInfoDTO.getOutBoundInvoiceList().stream().map(item->item.getContractOutBoundId()).collect(Collectors.toList())));
      if (!StrUtil.equals(contract.getContractCategory(), "water_house")) {
@@ -644,7 +856,7 @@
         }
         GenInvoiceInfoVo vo = BeanUtil.copyProperties(toInvoiceDTO,GenInvoiceInfoVo.class);
         //发票保存
         ContractInvoice invoice = saveContractInvoice(vo, contract, toInvoiceDTO.getOutBoundInvoiceList(), DateUtil.current() + "", "1");
         ContractInvoice invoice = saveContractInvoice(vo, contract, toInvoiceDTO.getOutBoundInvoiceList(), DateUtil.current() + "", "1",null);
         //启动流程
         ProcessInstanceParamDto dto = new ProcessInstanceParamDto();
         Map<String, Object> map = BeanUtil.beanToMap(invoice);
@@ -664,7 +876,7 @@
      }
      GenInvoiceInfoVo vo = BeanUtil.copyProperties(toInvoiceDTO,GenInvoiceInfoVo.class);
      //发票保存
      ContractInvoice invoice = saveContractInvoice(vo, contract, toInvoiceDTO.getOutBoundInvoiceList(), DateUtil.current() + "", "0");
      ContractInvoice invoice = saveContractInvoice(vo, contract, toInvoiceDTO.getOutBoundInvoiceList(), DateUtil.current() + "", "0",null);
      //启动流程
      ProcessInstanceParamDto dto = new ProcessInstanceParamDto();
      Map<String, Object> map = BeanUtil.beanToMap(invoice);
@@ -693,6 +905,32 @@
            contractPaymentScheduleMapper.updateById(schedule);
         });
      }
      return R.ok();
   }
   @Override
   public R toMergeInvoice(ToMergeInvoiceDTO toMergeInvoiceDTO) {
      GenInvoiceInfoVo vo = BeanUtil.copyProperties(toMergeInvoiceDTO, GenInvoiceInfoVo.class);
      //发票保存
      ContractInvoice invoice = saveContractInvoice(vo, null, toMergeInvoiceDTO.getOutBoundInvoiceList(),
            DateUtil.current() + "", "0", toMergeInvoiceDTO.getContractIds());
      //启动流程
      ProcessInstanceParamDto dto = new ProcessInstanceParamDto();
      Map<String, Object> map = BeanUtil.beanToMap(invoice);
      dto.setParamMap(map);
      dto.setFlowName(FlowNameEnum.销售挂账审批.name());
      R r1 = remoteFlowProcessService.startProcessInstance(dto);
      if (r1.getCode() == 1) {
         return R.failed("流程启动失败");
      }
      //更新合同出库单状态
      toMergeInvoiceDTO.getOutBoundInvoiceList().stream().forEach(contractOutBoundInvoiceVo -> {
         ContractOutBound contractOutBound = contractOutBoundMapper.selectById(contractOutBoundInvoiceVo.getContractOutBoundId());
         contractOutBound.setInvoiceStatus("1");
         contractOutBound.setInvoiceNum(contractOutBound.getInvoiceNum().add(contractOutBoundInvoiceVo.getInvoiceNum()));
         contractOutBoundMapper.updateById(contractOutBound);
      });
      return R.ok();
   }
@@ -761,9 +999,12 @@
   }
   private ContractInvoice saveContractInvoice(GenInvoiceInfoVo genInvoiceInfoVo, Contract contract,
                                    List<ContractOutBoundInvoiceVo> outBoundInvoiceVoList, String serialNo, String advanceFlag) {
                                    List<ContractOutBoundInvoiceVo> outBoundInvoiceVoList, String serialNo, String advanceFlag,
                                    List<String> contractIds) {
      ContractInvoice invoice = new ContractInvoice();
      if (ObjUtil.isNotNull(contract)) {
      invoice.setContractId(contract.getId());
      }
      invoice.setInvoiceCategory("1");
      invoice.setRedReversal("0");
      invoice.setSerialNo(serialNo);
@@ -782,6 +1023,10 @@
            .collect(Collectors.joining(",")));
      invoice.setInvoicePreviewParam(JSONObject.toJSONString(outBoundInvoiceVoList));
      invoice.setAdvanceFlag(advanceFlag);
      if (CollUtil.isNotEmpty(contractIds)) {
         invoice.setIsMerge(true);
         invoice.setContractIds(contractIds.toString());
      }
      baseMapper.insert(invoice);
      return invoice;
   }
@@ -790,6 +1035,8 @@
   public R getContractInvoiceList(Long contractId) {
      List<ContractInvoice> invoiceList = baseMapper.selectList(Wrappers.<ContractInvoice>lambdaQuery()
            .eq(ContractInvoice::getContractId, contractId)
            .or()
            .like(ContractInvoice::getContractIds,contractId)
            .orderByDesc(ContractInvoice::getCreateTime));
      return R.ok(invoiceList);
   }
@@ -799,9 +1046,14 @@
      ContractInvoice blueInvoice = baseMapper.selectById(invoiceId);
      blueInvoice.setRedReversal("2");
      baseMapper.updateById(blueInvoice);
      Contract contract = contractMapper.selectById(blueInvoice.getContractId());
      ContractInvoice invoice = new ContractInvoice();
      if (!blueInvoice.getIsMerge()){
      invoice.setContractId(blueInvoice.getContractId());
      }
      if (blueInvoice.getIsMerge()){
         invoice.setContractIds(blueInvoice.getContractIds());
         invoice.setIsMerge(true);
      }
      invoice.setTaxNo(blueInvoice.getTaxNo());
      invoice.setSellerTaxName(blueInvoice.getSellerTaxName());
      invoice.setBuyerTaxName(blueInvoice.getBuyerTaxName());
@@ -839,8 +1091,11 @@
      ProcessInstanceParamDto dto = new ProcessInstanceParamDto();
      Map<String, Object> map = BeanUtil.beanToMap(invoice);
      dto.setParamMap(map);
      if (!blueInvoice.getIsMerge()){
         Contract contract = contractMapper.selectById(blueInvoice.getContractId());
      if (StrUtil.equals(contract.getContractCategory(), "water_house")) {
         map.put("contractCategory", "water_house");
         }
      }
      dto.setParamMap(map);
      dto.setFlowName(FlowNameEnum.销售挂账审批.name());
@@ -852,7 +1107,7 @@
      return R.ok();
   }
   private JSONObject genRedInvoice(Contract contract, ContractInvoice blueInvoice,ContractInvoice redInvoice, String redConfirmSerialNo) {
   private JSONObject genRedInvoice(ContractInvoice blueInvoice, ContractInvoice redInvoice, String redConfirmSerialNo) {
      JSONObject jsonObject = new JSONObject();
      jsonObject.put("taxNo", redInvoice.getTaxNo());
      jsonObject.put("redConfirmSerialNo", redConfirmSerialNo);
@@ -886,7 +1141,7 @@
//         invoiceDetail.put("goodsPrice", invoiceItemVo.getUnitPrice());
         invoiceDetail.put("goodsTotalPrice", invoiceItemVo.getPriceNoTax());
         invoiceDetail.put("goodsTotalTax", invoiceItemVo.getPriceTax());
         if (vo.getInvoiceVehicleType().equals("3")||vo.getInvoiceVehicleType().equals("4")){
         if (StrUtil.isNotBlank(vo.getInvoiceVehicleType())&&(vo.getInvoiceVehicleType().equals("3") || vo.getInvoiceVehicleType().equals("4"))) {
            invoiceDetail.put("goodsSpecification", invoiceItemVo.getSpecification());
         }
         invoiceDetailsList.add(invoiceDetail);
@@ -1282,9 +1537,9 @@
         return;
      }
      SysDept dept = r.getData();
      Contract contract = contractMapper.selectById(invoice.getContractId());
      GenInvoiceInfoVo vo = BeanUtil.copyProperties(JSONObject.parseObject(invoice.getInvoicePreview()), GenInvoiceInfoVo.class);
      vo.setContractCategory(contract.getContractCategory());
      invoice.setApprovalStatus("1");
      baseMapper.updateById(invoice);
      invoice.setBwRequestResult("0");
@@ -1292,7 +1547,7 @@
      //蓝票
      if (invoice.getInvoiceCategory().equals("1")) {
         // TODO 开蓝票接口机动车销售
         if (StrUtil.equals(vo.getInvoiceVehicleType(), "4")) {
         if (StrUtil.equals(vo.getInvoiceVehicleType(), "4")&&bwFlag) {
            JSONObject request = genBlueInvoiceVehicle(vo, invoice.getSerialNo(), dept);
            String requestId = IdUtil.fastUUID();
            invoice.setBwRequestId(requestId);
@@ -1318,7 +1573,7 @@
         }
         // TODO 开蓝票接口机动车专用
         if (StrUtil.equals(vo.getInvoiceVehicleType(), "3")) {
         if (StrUtil.equals(vo.getInvoiceVehicleType(), "3")&&bwFlag) {
            JSONObject request = genBlueInvoiceVehiclePre(vo, invoice.getSerialNo(), dept);
            String requestId = IdUtil.fastUUID();
            invoice.setBwRequestId(requestId);
@@ -1344,7 +1599,7 @@
         }
         // TODO 开蓝票接口 非机动车
         if (!StrUtil.equals(vo.getInvoiceVehicleType(), "3")&&!StrUtil.equals(vo.getInvoiceVehicleType(), "4")) {
         if (!StrUtil.equals(vo.getInvoiceVehicleType(), "3") && !StrUtil.equals(vo.getInvoiceVehicleType(), "4")&&bwFlag) {
            JSONObject request = genBlueInvoice(vo, invoice.getSerialNo(), dept);
            String requestId = IdUtil.fastUUID();
            invoice.setBwRequestId(requestId);
@@ -1372,6 +1627,10 @@
               }
            }
         }
         //非合并开票
         if (!invoice.getIsMerge()) {
            Contract contract = contractMapper.selectById(invoice.getContractId());
            vo.setContractCategory(contract.getContractCategory());
         if (StrUtil.isNotBlank(vo.getAdvanceInvoice())) {
            contract.setBillingAmout(vo.getTotalPrice().add(contract.getBillingAmout()));
            contractMapper.updateById(contract);
@@ -1443,12 +1702,54 @@
            contractMapper.updateById(contract);
         }
         baseMapper.updateById(invoice);
            return;
         } else {
            //合并开票
            List<String> result = Arrays.stream(invoice.getContractIds().replace("[", "").replace("]", "").split(","))
                  .map(String::trim)
                  .filter(s -> !s.isEmpty())
                  .collect(Collectors.toList());
            if (CollUtil.isNotEmpty(result)) {
               for (String cid : result) {
                  Contract contract = contractMapper.selectById(cid);
                  BigDecimal contractBillAmount = new BigDecimal("0");
                  if (CollUtil.isNotEmpty(vo.getOutBoundInvoiceList())) {
                     for (ContractOutBoundInvoiceVo outBoundInvoiceVo : vo.getOutBoundInvoiceList()) {
                        ContractOutBound contractOutBound = contractOutBoundMapper.selectById(outBoundInvoiceVo.getContractOutBoundId());
                        if (ObjUtil.isNotNull(contractOutBound) && contractOutBound.getContractId().equals(contract.getId())) {
                           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 (ObjUtil.isNotNull(subjectMatter) && ObjUtil.isNotNull(subjectMatter.getUnitPrice())) {
                              contractBillAmount = contractBillAmount.add(outBoundInvoiceVo.getInvoiceNum().multiply(subjectMatter.getUnitPrice()));
                           }
                        }
                     }
                  }
                  contract.setBillingAmout(contract.getBillingAmout().add(contractBillAmount));
                  contract.setBillingStatus("1");
                  List<ContractOutBound> outBoundList = contractOutBoundMapper.selectList(Wrappers.<ContractOutBound>lambdaQuery()
                        .eq(ContractOutBound::getContractId, contract.getId())
                        .apply("invoice_num < out_bound_num"));
                  if (CollUtil.isNotEmpty(outBoundList)) {
                     contract.setBillingStatus("2");
                  }
                  if (contract.getBillingAmout().compareTo(contract.getAmount()) == 0) {
                     contract.setBillingStatus("3");
                  }
                  contractMapper.updateById(contract);
               }
            }
         }
      }
      //红票
      if (invoice.getInvoiceCategory().equals("2")) {
         // TODO 开红票接口
         ContractInvoice blueInvoice = baseMapper.selectById(invoice.getBlueInvoiceId());
         JSONObject request = genRedInvoice(contract,blueInvoice, invoice, invoice.getSerialNo());
         JSONObject request = genRedInvoice(blueInvoice, invoice, invoice.getSerialNo());
         String requestId = IdUtil.fastUUID();
         invoice.setBwRequestId(requestId);
         invoice.setBwRequestParam(request.toJSONString());
@@ -1480,6 +1781,8 @@
               }
            }
         }
         if (!invoice.getIsMerge()) {
            Contract contract = contractMapper.selectById(invoice.getContractId());
         baseMapper.updateById(invoice);
         contract.setBillingAmout(contract.getBillingAmout().add(invoice.getInvoiceTotalPriceTax()));
         //防止重复审开票审核通过后 开票金额为负
@@ -1530,8 +1833,48 @@
               });
            }
         }
         }else {
            blueInvoice.setRedReversal("1");
            baseMapper.updateById(blueInvoice);
            //合并开票
            List<String> result = Arrays.stream(invoice.getContractIds().replace("[", "").replace("]", "").split(","))
                  .map(String::trim)
                  .filter(s -> !s.isEmpty())
                  .collect(Collectors.toList());
            if (CollUtil.isNotEmpty(result)) {
               for (String cid : result) {
                  Contract contract = contractMapper.selectById(cid);
                  if (CollUtil.isNotEmpty(vo.getOutBoundInvoiceList())) {
                     for (ContractOutBoundInvoiceVo outBoundInvoiceVo : vo.getOutBoundInvoiceList()) {
                        BigDecimal contractBillAmount = new BigDecimal("0");
                        if (CollUtil.isNotEmpty(vo.getOutBoundInvoiceList())) {
                              ContractOutBound contractOutBound = contractOutBoundMapper.selectById(outBoundInvoiceVo.getContractOutBoundId());
                              if (ObjUtil.isNotNull(contractOutBound) && contractOutBound.getContractId().equals(contract.getId())) {
                                 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 (ObjUtil.isNotNull(subjectMatter) && ObjUtil.isNotNull(subjectMatter.getUnitPrice())) {
                                    contractBillAmount = contractBillAmount.add(outBoundInvoiceVo.getInvoiceNum().multiply(subjectMatter.getUnitPrice()));
      }
                                 contractOutBound.setInvoiceNum(contractOutBound.getInvoiceNum().subtract(outBoundInvoiceVo.getInvoiceNum()));
                                 contractOutBoundMapper.updateById(contractOutBound);
                              }
                        }
                        //更新合同开票状态 前提出库完成
                        contract.setBillingStatus("2");
                        contract.setBillingAmout(contract.getBillingAmout().add(invoice.getInvoiceTotalPriceTax()));
                        //防止重复审开票审核通过后 开票金额为负
                        if (contract.getBillingAmout().compareTo(new BigDecimal("0")) < 0) {
                           contract.setBillingAmout(new BigDecimal("0"));
                        }
                        contractMapper.updateById(contract);
                     }
                  }
               }
            }
         }
      }
   }
   private JSONObject genBlueInvoiceVehiclePre(GenInvoiceInfoVo vo, String serialNo, SysDept dept) {