platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/ContractInvoiceServiceImpl.java
@@ -25,10 +25,7 @@
import com.by4cloud.platformx.business.mapper.*;
import com.by4cloud.platformx.business.service.ContractInvoiceService;
import com.by4cloud.platformx.business.service.PaymentConfirmService;
import com.by4cloud.platformx.business.vo.ContractInvoiceOutBoundVo;
import com.by4cloud.platformx.business.vo.ContractOutBoundMergeVo;
import com.by4cloud.platformx.business.vo.GenInvoiceInfoVo;
import com.by4cloud.platformx.business.vo.InvoiceItemVo;
import com.by4cloud.platformx.business.vo.*;
import com.by4cloud.platformx.common.core.util.R;
import com.by4cloud.platformx.common.data.mybatis.BaseModel;
import com.by4cloud.platformx.common.security.util.SecurityUtils;
@@ -88,15 +85,21 @@
            .selectAll(ContractOutBound.class)
            .eq(ContractOutBound::getContractId, contractId);
      List<ContractInvoiceOutBoundVo> invoiceOutBoundVoList = contractOutBoundMapper.selectJoinList(ContractInvoiceOutBoundVo.class, wrapper);
      if (ArrayUtil.isNotEmpty(invoiceOutBoundVoList.toArray())){
         invoiceOutBoundVoList.stream().forEach(contractInvoiceOutBoundVo -> {
            contractInvoiceOutBoundVo.setInvoiceNum(contractInvoiceOutBoundVo.getOutBoundNum().subtract(contractInvoiceOutBoundVo.getInvoiceNum()));
            contractInvoiceOutBoundVo.setToInvoiceNum(contractInvoiceOutBoundVo.getInvoiceNum());
         });
      }
      return R.ok(invoiceOutBoundVoList);
   }
   @Override
   public R genInvoiceInfo(GenInvoiceInfoDTO genInvoiceInfoDTO) {
      if (ArrayUtil.isEmpty(genInvoiceInfoDTO.getContractOutBoundIds())) {
      if (ArrayUtil.isEmpty(genInvoiceInfoDTO.getOutBoundInvoiceList())) {
         return R.failed("请选择开票出库单后再操作");
      }
      ContractOutBound outBound = contractOutBoundMapper.selectById(genInvoiceInfoDTO.getContractOutBoundIds().get(0));
      ContractOutBound outBound = contractOutBoundMapper.selectById(genInvoiceInfoDTO.getOutBoundInvoiceList().get(0).getContractOutBoundId());
      Contract contract = contractMapper.selectById(outBound.getContractId());
      if (ObjUtil.isNull(contract)) {
         return R.failed("合同信息异常,请联系技术人员");
@@ -114,7 +117,7 @@
   private GenInvoiceInfoVo genInvoiceInfoVo(GenInvoiceInfoDTO genInvoiceInfoDTO, Contract contract, SysDept b) {
      GenInvoiceInfoVo vo = new GenInvoiceInfoVo();
      BusinessCustomer a = businessCustomerMapper.selectById(contract.getPartyAId());
      vo.setPartyA(a.getCompanyName());
      vo.setPartyA(a.getRegisterName());
      vo.setPartyAOrgCode(a.getCreditCode());
      vo.setPartyAOpenBank(a.getBankName());
      vo.setPartyABankAccount(a.getBankAccount());
@@ -125,16 +128,18 @@
      List<InvoiceItemVo> invoiceItemVoList = new ArrayList<>();
      List<String> erpCode = new ArrayList<>();
      List<ContractOutBoundMergeVo> mergeOutBounds = new ArrayList<>();
      List<ContractOutBound> outBounds = contractOutBoundMapper.selectList(Wrappers.<ContractOutBound>lambdaQuery()
            .in(ContractOutBound::getId, genInvoiceInfoDTO.getContractOutBoundIds()));
      if (ArrayUtil.isNotEmpty(outBounds.toArray())) {
         outBounds.stream().forEach(contractOutBound -> {
//      List<ContractOutBound> outBounds = contractOutBoundMapper.selectList(Wrappers.<ContractOutBound>lambdaQuery()
//            .in(ContractOutBound::getId, genInvoiceInfoDTO.getOutBoundInvoiceList().stream().map(item->item.getContractOutBoundId()).collect(Collectors.toList())));
      if (ArrayUtil.isNotEmpty(genInvoiceInfoDTO.getOutBoundInvoiceList().toArray())) {
         genInvoiceInfoDTO.getOutBoundInvoiceList().stream().forEach(contractOutBoundInvoiceVo -> {
            ContractOutBound contractOutBound = contractOutBoundMapper.selectById(contractOutBoundInvoiceVo.getContractOutBoundId());
            if (!ArrayUtil.contains(erpCode.toArray(), contractOutBound.getSubjectMatterCode())) {
               erpCode.add(contractOutBound.getSubjectMatterCode());
               ContractOutBoundMergeVo mergeVo = new ContractOutBoundMergeVo();
               mergeVo.setMaterialCode(contractOutBound.getSubjectMatterCode());
               mergeVo.setMaterialInternalName(contractOutBound.getSubjectMatterName());
               mergeVo.setQuantity(contractOutBound.getOutBoundNum());
               mergeVo.setQuantity(contractOutBoundInvoiceVo.getInvoiceNum());
               Product product = productMapper.selectOne(Wrappers.<Product>lambdaQuery().eq(Product::getErpCode, contractOutBound.getSubjectMatterCode())
                     .eq(Product::getCompId, SecurityUtils.getUser().getCompId()).last("limit 1"));
               if (ObjUtil.isNotNull(product)) {
@@ -183,10 +188,10 @@
   @Override
   public R toInvoice(GenInvoiceInfoDTO genInvoiceInfoDTO) {
      if (ArrayUtil.isEmpty(genInvoiceInfoDTO.getContractOutBoundIds().toArray())) {
      if (ArrayUtil.isEmpty(genInvoiceInfoDTO.getOutBoundInvoiceList().toArray())) {
         return R.failed("请选择开票出库单后再操作");
      }
      ContractOutBound outBound = contractOutBoundMapper.selectById(genInvoiceInfoDTO.getContractOutBoundIds().get(0));
      ContractOutBound outBound = contractOutBoundMapper.selectById(genInvoiceInfoDTO.getOutBoundInvoiceList().get(0).getContractOutBoundId());
      Contract contract = contractMapper.selectById(outBound.getContractId());
      if (ObjUtil.isNull(contract)) {
         return R.failed("合同信息异常,请联系技术人员");
@@ -198,7 +203,7 @@
      SysDept b = r.getData();
      GenInvoiceInfoVo vo = genInvoiceInfoVo(genInvoiceInfoDTO, contract, b);
      //发票保存
      ContractInvoice invoice = saveContractInvoice(vo, contract, genInvoiceInfoDTO.getContractOutBoundIds(), DateUtil.current() + "");
      ContractInvoice invoice = saveContractInvoice(vo, contract, genInvoiceInfoDTO.getOutBoundInvoiceList(), DateUtil.current() + "");
      //启动流程
      ProcessInstanceParamDto dto = new ProcessInstanceParamDto();
      Map<String, Object> map = BeanUtil.beanToMap(invoice);
@@ -209,9 +214,10 @@
         return R.failed("流程启动失败");
      }
      //更新合同出库单状态
      genInvoiceInfoDTO.getContractOutBoundIds().stream().forEach(outBoundId -> {
         ContractOutBound bound = contractOutBoundMapper.selectById(outBoundId);
      genInvoiceInfoDTO.getOutBoundInvoiceList().stream().forEach(contractOutBoundInvoiceVo -> {
         ContractOutBound bound = contractOutBoundMapper.selectById(contractOutBoundInvoiceVo.getContractOutBoundId());
         bound.setInvoiceStatus("1");
         bound.setInvoiceNum(bound.getInvoiceNum().add(contractOutBoundInvoiceVo.getInvoiceNum()));
         contractOutBoundMapper.updateById(bound);
      });
      return R.ok();
@@ -271,7 +277,7 @@
      return jsonObject;
   }
   private ContractInvoice saveContractInvoice(GenInvoiceInfoVo genInvoiceInfoVo, Contract contract, List<Long> outBoundIds, String serialNo) {
   private ContractInvoice saveContractInvoice(GenInvoiceInfoVo genInvoiceInfoVo, Contract contract, List<ContractOutBoundInvoiceVo> outBoundInvoiceVoList, String serialNo) {
      ContractInvoice invoice = new ContractInvoice();
      invoice.setContractId(contract.getId());
      invoice.setInvoiceCategory("1");
@@ -286,9 +292,10 @@
      invoice.setInvoicePreview(JSONObject.toJSONString(genInvoiceInfoVo));
      invoice.setInvoiceStatus("1");
      invoice.setApprovalStatus("0");
      invoice.setOutBoundId(outBoundIds.stream()
            .map(String::valueOf) // 或者 .map(Object::toString)
      invoice.setOutBoundId(outBoundInvoiceVoList.stream()
            .map(item-> String.valueOf(item.getContractOutBoundId())) // 或者 .map(Object::toString)
            .collect(Collectors.joining(",")));
      invoice.setInvoicePreviewParam(JSONObject.toJSONString(outBoundInvoiceVoList));
      baseMapper.insert(invoice);
      return invoice;
   }
@@ -327,6 +334,7 @@
      invoice.setSerialNo(DateUtil.current() + "");
      invoice.setBlueInvoiceId(invoiceId);
      invoice.setApprovalStatus("0");
      invoice.setInvoicePreviewParam(blueInvoice.getInvoicePreviewParam());
      baseMapper.insert(invoice);
      //启动流程
      ProcessInstanceParamDto dto = new ProcessInstanceParamDto();
@@ -621,16 +629,21 @@
         }
         //更新合同开票状态
         contract.setBillingStatus("2");
         contract.setBillingAmout(contract.getAmount().subtract(invoice.getInvoiceTotalPriceTax()));
         contract.setBillingAmout(contract.getBillingAmout().subtract(invoice.getInvoiceTotalPriceTax()));
         contractMapper.updateById(contract);
         blueInvoice.setRedReversal("1");
         baseMapper.updateById(blueInvoice);
         //更新合同出库单状态
         String[] outBoundIds = blueInvoice.getOutBoundId().split(",");
         Arrays.stream(outBoundIds).forEach(outBoundId -> {
            ContractOutBound bound = contractOutBoundMapper.selectById(outBoundId);
            bound.setInvoiceStatus("0");
            contractOutBoundMapper.updateById(bound);
         });
         List<ContractOutBoundInvoiceVo> outBoundInvoiceVoList = BeanUtil.copyToList(JSONArray.parseArray(blueInvoice.getInvoicePreviewParam()),ContractOutBoundInvoiceVo.class);
         if (ArrayUtil.isNotEmpty(outBoundInvoiceVoList.toArray())){
            outBoundInvoiceVoList.stream().forEach(contractOutBoundInvoiceVo -> {
               ContractOutBound contractOutBound = contractOutBoundMapper.selectById(contractOutBoundInvoiceVo.getContractOutBoundId());
               contractOutBound.setInvoiceNum(contractOutBound.getInvoiceNum().subtract(contractOutBoundInvoiceVo.getInvoiceNum()));
               contractOutBoundMapper.updateById(contractOutBound);
            });
         }
      }
      invoice.setApprovalStatus("1");
      baseMapper.updateById(invoice);
@@ -648,5 +661,9 @@
      });
      invoice.setApprovalStatus("2");
      baseMapper.updateById(invoice);
      ContractInvoice blueInvoice = baseMapper.selectById(invoice.getBlueInvoiceId());
      blueInvoice.setRedReversal("0");
      baseMapper.updateById(blueInvoice);
   }
}