李白
昨天 e7e6d39b095b6a763e54ab34631171c3de32ec9e
platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/ContractServiceImpl.java
@@ -166,19 +166,6 @@
      baseMapper.updateById(contract);
      ContractPaymentSchedule fitstSchedule = contractPaymentScheduleMapper.selectOne(Wrappers.<ContractPaymentSchedule>lambdaQuery().eq(ContractPaymentSchedule::getContractId,contract.getId())
            .orderByAsc(ContractPaymentSchedule::getCreateTime).last("limit 1"));
//      PaymentConfirm entity = new PaymentConfirm();
//      entity.setContractId(contract.getId());
//      entity.setContractName(contract.getContractName());
//      entity.setContractNo(contract.getContractNo());
//      entity.setBusGuestId(contract.getPartyAId());
//      entity.setBusGuestName(contract.getPartyA());
//      entity.setBusinessType(fitstSchedule.getStageName()+"应收");
//      entity.setTransationAmount(new BigDecimal(fitstSchedule.getPlannedAmount()));
//      entity.setReceivableAmount(new BigDecimal(fitstSchedule.getPlannedAmount()));
//      entity.setTotalAmount(new BigDecimal("0").subtract(new BigDecimal(fitstSchedule.getPlannedAmount())));
//      entity.setConfirmTime(new Date());
//      paymentConfirmMapper.insert(entity);
      if (fitstSchedule.getStageName().equals("合同签订")){
         //新增应收
         PaymentConfirm paymentConfirm = new PaymentConfirm();
@@ -207,43 +194,57 @@
      //查询是否有后续阶段
      List<ContractPaymentSchedule> afterSchedule = contractPaymentScheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery()
            .eq(ContractPaymentSchedule::getContractId, fitstSchedule.getContractId())
            .gt(ContractPaymentSchedule::getStageOrder, fitstSchedule.getStageOrder()));
            .gt(ContractPaymentSchedule::getStageOrder, fitstSchedule.getStageOrder())
            .orderByAsc(ContractPaymentSchedule::getCreateTime));
      if (ArrayUtil.isNotEmpty(afterSchedule.toArray())&&afterSchedule.size()==1){
         //最后阶段生效时间
         ContractPaymentSchedule endSchedule = afterSchedule.get(0);
         endSchedule.setEffectiveDate(DateUtil.offsetDay(new Date(),endSchedule.getAgreedDays()));
         endSchedule.setEffectiveEndDate(contract.getExpirationDate());
         contractPaymentScheduleMapper.updateById(endSchedule);
         //第一阶段生效时间
         fitstSchedule.setEffectiveDate(DateUtil.offsetDay(new Date(),fitstSchedule.getAgreedDays()));
         fitstSchedule.setEffectiveEndDate(endSchedule.getEffectiveDate());
         contractPaymentScheduleMapper.updateById(fitstSchedule);
         if (StrUtil.equals(endSchedule.getStageName(),"质保金")){
            endSchedule.setEffectiveDate(DateUtil.offsetDay(new Date(),endSchedule.getAgreedDays()));
            endSchedule.setEffectiveEndDate(contract.getExpirationDate());
            contractPaymentScheduleMapper.updateById(endSchedule);
            //第一阶段生效时间
            fitstSchedule.setEffectiveDate(DateUtil.offsetDay(new Date(),fitstSchedule.getAgreedDays()));
            fitstSchedule.setEffectiveEndDate(endSchedule.getEffectiveDate());
            contractPaymentScheduleMapper.updateById(fitstSchedule);
         //最后阶段应收
         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(endSchedule.getStageName());
         newConfim.setConfirmTime(new Date());
         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();
            //最后阶段应收
            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(endSchedule.getStageName());
            newConfim.setConfirmTime(new Date());
            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());
         baseMapper.updateById(contract);
      }
      if(ArrayUtil.isNotEmpty(afterSchedule.toArray())&&afterSchedule.size()>1){
         //更新合同下个阶段
         contract.setNextScheduleName(afterSchedule.get(0).getStageName());
         baseMapper.updateById(contract);
      }
      if(ArrayUtil.isEmpty(afterSchedule.toArray())){
         fitstSchedule.setEffectiveEndDate(contract.getExpirationDate());
         contractPaymentScheduleMapper.updateById(fitstSchedule);
         //更新合同下个阶段
         contract.setNextScheduleName("无");
         baseMapper.updateById(contract);
      }
      return Boolean.TRUE;
   }