李白
1 天以前 e7e6d39b095b6a763e54ab34631171c3de32ec9e
platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/OutBoundServiceImpl.java
@@ -40,6 +40,7 @@
   private final PaymentConfirmMapper paymentConfirmMapper;
   private final ContractMapper contractMapper;
   private final ContractPaymentScheduleProcessMapper scheduleProcessMapper;
   private final ContractOutBoundMapper contractOutBoundMapper;
   @Override
   public R add(OutBoundAddDTO addDTO) {
@@ -49,6 +50,7 @@
      MPJLambdaWrapper<ContractSubjectMatter> wrapper = new MPJLambdaWrapper<ContractSubjectMatter>()
            .selectAll(ContractSubjectMatter.class)
            .ne(ContractSubjectMatter::getDeliveryStatus, 2)
            .in(ContractSubjectMatter::getMaterialCode,addDTO.getSubjectMatterList().stream().map(item->item.getSubjectMatterCode()).collect(Collectors.toList()))
            .exists(Contract.class, contractQuery ->
                  contractQuery.select(ContractSubjectMatter::getId)
                        .eq(Contract::getId, ContractSubjectMatter::getContractId)
@@ -80,7 +82,7 @@
      addDTO.getSubjectMatterList().stream().forEach(outSubjectMatterAddDTO -> {
         BigDecimal remainNum = outSubjectMatterAddDTO.getOutBoundNum();
         for (ContractSubjectMatter subjectMatter : subjectMatterList) {
            Contract contract = contractMapper.selectById(subjectMatter.getContractId());
            if (subjectMatter.getDeliveredQuantity().compareTo(new BigDecimal("0")) > 0) {
               remainNum = remainNum.subtract(subjectMatter.getRemainingQuantity());
               //部分交付状态下 全部出库
@@ -93,15 +95,19 @@
                  subjectMatter.setLastDeliveredQuantity(currentOut);
                  contractSubjectMatterMapper.updateById(subjectMatter);
                  currentOutList.add(subjectMatter);
                  //新增合同出库历史
                  saveContractOutBound(contract,addDTO,subjectMatter);
               } else {
                  BigDecimal currentOut = remainNum.add(subjectMatter.getRemainingQuantity());
                  subjectMatter.setDeliveredQuantity(subjectMatter.getQuantity().add(remainNum));
                  subjectMatter.setRemainingQuantity(remainNum.multiply(new BigDecimal("-1")));
                  subjectMatter.setRemainingQuantity(subjectMatter.getRemainingQuantity().subtract(currentOut));
                  subjectMatter.setActualDeliveryDate(DateUtil.today());
                  subjectMatter.setDeliveryStatus(1);
                  subjectMatter.setLastDeliveredQuantity(currentOut);
                  contractSubjectMatterMapper.updateById(subjectMatter);
                  currentOutList.add(subjectMatter);
                  //新增合同出库历史
                  saveContractOutBound(contract,addDTO,subjectMatter);
                  break;
               }
@@ -113,9 +119,11 @@
                  subjectMatter.setRemainingQuantity(new BigDecimal("0"));
                  subjectMatter.setActualDeliveryDate(DateUtil.today());
                  subjectMatter.setDeliveryStatus(2);
                  contractSubjectMatterMapper.updateById(subjectMatter);
                  subjectMatter.setLastDeliveredQuantity(subjectMatter.getQuantity());
                  contractSubjectMatterMapper.updateById(subjectMatter);
                  currentOutList.add(subjectMatter);
                  //新增合同出库历史
                  saveContractOutBound(contract,addDTO,subjectMatter);
               } else {
                  BigDecimal currentOut = remainNum.add(subjectMatter.getQuantity());
                  subjectMatter.setDeliveredQuantity(subjectMatter.getQuantity().add(remainNum));
@@ -125,6 +133,8 @@
                  subjectMatter.setLastDeliveredQuantity(currentOut);
                  contractSubjectMatterMapper.updateById(subjectMatter);
                  currentOutList.add(subjectMatter);
                  //新增合同出库历史
                  saveContractOutBound(contract,addDTO,subjectMatter);
                  break;
               }
            }
@@ -148,7 +158,7 @@
            //当前出库金额
            BigDecimal outPirce = value.stream()
                  .filter(item -> item.getLastDeliveredQuantity() != null && item.getUnitPrice() != null)
                  .map(item -> item.getLastDeliveredQuantity().multiply(item.getUnitPrice()))
                  .map(item ->(item.getDeliveryStatus()==1?item.getDeliveredQuantity():item.getQuantity()).multiply(item.getUnitPrice()))
                  .reduce(BigDecimal.ZERO, BigDecimal::add);
            //合同金额
            Contract contract = contractMapper.selectById(key);
@@ -162,7 +172,6 @@
               contractMapper.updateById(contract);
            }
            if (ObjUtil.isNotNull(schedule)) {
               //按出库资金比例 计算发货前应收更新资金明细
               PaymentConfirm paymentConfirm = new PaymentConfirm();
               paymentConfirm.setBusinessType("发货前应收");
@@ -215,43 +224,56 @@
               //查询是否有后续阶段
               List<ContractPaymentSchedule> afterSchedule = scheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery()
                     .eq(ContractPaymentSchedule::getContractId, schedule.getContractId())
                     .gt(ContractPaymentSchedule::getStageOrder, schedule.getStageOrder()));
                     .gt(ContractPaymentSchedule::getStageOrder, schedule.getStageOrder())
                     .orderByAsc(ContractPaymentSchedule::getCreateTime));
               if (ArrayUtil.isNotEmpty(afterSchedule.toArray())&&afterSchedule.size()==1){
                  //最后阶段生效时间
                  ContractPaymentSchedule endSchedule = afterSchedule.get(0);
                  endSchedule.setEffectiveDate(DateUtil.offsetDay(addDTO.getOutBoundTime(),endSchedule.getAgreedDays()));
                  endSchedule.setEffectiveEndDate(contract.getExpirationDate());
                  scheduleMapper.updateById(endSchedule);
                  //当前阶段生效时间
                  schedule.setEffectiveDate(DateUtil.offsetDay(addDTO.getOutBoundTime(),schedule.getAgreedDays()));
                  schedule.setEffectiveEndDate(endSchedule.getEffectiveDate());
                  scheduleMapper.updateById(schedule);
                  //最后阶段应收
                  PaymentConfirm newConfim = new PaymentConfirm();
                  newConfim.setBusinessType(endSchedule.getStageName()+"应收");
                  newConfim.setBusGuestId(contract.getPartyAId());
                  newConfim.setBusGuestName(contract.getPartyA());
                  newConfim.setContractId(contract.getId());
                  newConfim.setContractName(contract.getContractName());
                  newConfim.setContractNo(contract.getContractNo());
                  newConfim.setScheduleId(endSchedule.getId());
                  newConfim.setScheduleName(schedule.getStageName());
                  newConfim.setConfirmTime(addDTO.getOutBoundTime());
                  newConfim.setTransationAmount(endSchedule.getPlannedAmount());
                  newConfim.setReceivableAmount(endSchedule.getPlannedAmount());
                  PaymentConfirm lastNewConfirm = paymentConfirmMapper.selectOne(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getContractId,contract.getId())
                        .orderByDesc(PaymentConfirm::getCreateTime).last("limit 1"));
                  BigDecimal lastNewTotal = new BigDecimal("0");
                  if(ObjUtil.isNotNull(lastNewConfirm)){
                     lastNewTotal = lastNewConfirm.getTotalAmount();
                  if (StrUtil.equals(endSchedule.getStageName(),"质保金")) {
                     endSchedule.setEffectiveDate(DateUtil.offsetDay(addDTO.getOutBoundTime(), endSchedule.getAgreedDays()));
                     endSchedule.setEffectiveEndDate(contract.getExpirationDate());
                     scheduleMapper.updateById(endSchedule);
                     //当前阶段生效时间
                     schedule.setEffectiveDate(DateUtil.offsetDay(addDTO.getOutBoundTime(), schedule.getAgreedDays()));
                     schedule.setEffectiveEndDate(endSchedule.getEffectiveDate());
                     scheduleMapper.updateById(schedule);
                     //最后阶段应收
                     PaymentConfirm newConfim = new PaymentConfirm();
                     newConfim.setBusinessType(endSchedule.getStageName() + "应收");
                     newConfim.setBusGuestId(contract.getPartyAId());
                     newConfim.setBusGuestName(contract.getPartyA());
                     newConfim.setContractId(contract.getId());
                     newConfim.setContractName(contract.getContractName());
                     newConfim.setContractNo(contract.getContractNo());
                     newConfim.setScheduleId(endSchedule.getId());
                     newConfim.setScheduleName(schedule.getStageName());
                     newConfim.setConfirmTime(addDTO.getOutBoundTime());
                     newConfim.setTransationAmount(endSchedule.getPlannedAmount());
                     newConfim.setReceivableAmount(endSchedule.getPlannedAmount());
                     PaymentConfirm lastNewConfirm = paymentConfirmMapper.selectOne(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getContractId, contract.getId())
                           .orderByDesc(PaymentConfirm::getCreateTime).last("limit 1"));
                     BigDecimal lastNewTotal = new BigDecimal("0");
                     if (ObjUtil.isNotNull(lastNewConfirm)) {
                        lastNewTotal = lastNewConfirm.getTotalAmount();
                     }
                     newConfim.setTotalAmount(lastNewTotal.subtract(newConfim.getReceivableAmount()));
                     paymentConfirmMapper.insert(newConfim);
                  }
                  newConfim.setTotalAmount(lastNewTotal.subtract(newConfim.getReceivableAmount()));
                  paymentConfirmMapper.insert(newConfim);
                  //更新合同下个阶段
                  contract.setNextScheduleName(endSchedule.getStageName());
                  contractMapper.updateById(contract);
               }
               if(ArrayUtil.isNotEmpty(afterSchedule.toArray())&&afterSchedule.size()>1){
                  //更新合同下个阶段
                  contract.setNextScheduleName(afterSchedule.get(0).getStageName());
                  contractMapper.updateById(contract);
               }
               if(ArrayUtil.isEmpty(afterSchedule.toArray())){
                  schedule.setEffectiveEndDate(contract.getExpirationDate());
                  scheduleMapper.updateById(schedule);
                  //更新合同下个阶段
                  contract.setNextScheduleName("无");
                  contractMapper.updateById(contract);
               }
            }
         });
@@ -259,4 +281,19 @@
      return R.ok();
   }
   private void saveContractOutBound(Contract contract, OutBoundAddDTO addDTO, ContractSubjectMatter subjectMatter) {
      ContractOutBound contractOutBound = new ContractOutBound();
      contractOutBound.setContractId(contract.getId());
      contractOutBound.setContractName(contract.getContractName());
      contractOutBound.setBusGuestId(contract.getPartyAId());
      contractOutBound.setBusGuestName(contract.getPartyA());
      contractOutBound.setSubjectMatterCode(subjectMatter.getMaterialCode());
      contractOutBound.setSubjectMatterName(subjectMatter.getMaterialInternalName());
      contractOutBound.setOutBoundTime(addDTO.getOutBoundTime());
      contractOutBound.setOutBoundNum(subjectMatter.getLastDeliveredQuantity());
      contractOutBound.setOutBoundAttNames(addDTO.getOutBoundAttNames());
      contractOutBound.setOutBoundAttPaths(addDTO.getOutBoundAttPaths());
      contractOutBoundMapper.insert(contractOutBound);
   }
}