| | |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | 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())) |
| | | .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) |
| | |
| | | return R.failed("出库数量超出合同订单数量"); |
| | | } |
| | | //当前入库标的物 |
| | | List<ContractSubjectMatter> currentOutList = new ArrayList<>(); |
| | | Map<Long, Object> currentOutMap = new HashMap<>(); |
| | | //出库 |
| | | 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()); |
| | | //部分交付状态下 全部出库 |
| | | if (remainNum.compareTo(new BigDecimal("0")) >= 0) { |
| | | BigDecimal currentOut = subjectMatter.getRemainingQuantity(); |
| | | subjectMatter.setDeliveredQuantity(subjectMatter.getQuantity()); |
| | | subjectMatter.setRemainingQuantity(new BigDecimal("0")); |
| | | subjectMatter.setActualDeliveryDate(DateUtil.today()); |
| | | subjectMatter.setDeliveryStatus(2); |
| | | 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(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; |
| | | } |
| | | if (outSubjectMatterAddDTO.getSubjectMatterCode().equals(subjectMatter.getMaterialCode())) { |
| | | Contract contract = contractMapper.selectById(subjectMatter.getContractId()); |
| | | if (subjectMatter.getDeliveredQuantity().compareTo(new BigDecimal("0")) > 0) { |
| | | remainNum = remainNum.subtract(subjectMatter.getRemainingQuantity()); |
| | | //部分交付状态下 全部出库 |
| | | if (remainNum.compareTo(new BigDecimal("0")) >= 0) { |
| | | BigDecimal currentOut = subjectMatter.getRemainingQuantity(); |
| | | subjectMatter.setDeliveredQuantity(subjectMatter.getQuantity()); |
| | | subjectMatter.setRemainingQuantity(new BigDecimal("0")); |
| | | subjectMatter.setActualDeliveryDate(DateUtil.today()); |
| | | subjectMatter.setDeliveryStatus(2); |
| | | subjectMatter.setLastDeliveredQuantity(currentOut); |
| | | contractSubjectMatterMapper.updateById(subjectMatter); |
| | | currentOutMap.put(subjectMatter.getContractId(), subjectMatter); |
| | | //新增合同出库历史 |
| | | saveContractOutBound(contract, addDTO, subjectMatter); |
| | | } else { |
| | | BigDecimal currentOut = remainNum.add(subjectMatter.getRemainingQuantity()); |
| | | subjectMatter.setDeliveredQuantity(subjectMatter.getQuantity().add(remainNum)); |
| | | subjectMatter.setRemainingQuantity(subjectMatter.getRemainingQuantity().subtract(currentOut)); |
| | | subjectMatter.setActualDeliveryDate(DateUtil.today()); |
| | | subjectMatter.setDeliveryStatus(1); |
| | | subjectMatter.setLastDeliveredQuantity(currentOut); |
| | | contractSubjectMatterMapper.updateById(subjectMatter); |
| | | currentOutMap.put(subjectMatter.getContractId(), subjectMatter); |
| | | //新增合同出库历史 |
| | | saveContractOutBound(contract, addDTO, subjectMatter); |
| | | break; |
| | | } |
| | | |
| | | } else { |
| | | //未交付状态下 |
| | | remainNum = remainNum.subtract(subjectMatter.getQuantity()); |
| | | if (remainNum.compareTo(new BigDecimal("0")) >= 0) { |
| | | subjectMatter.setDeliveredQuantity(subjectMatter.getQuantity()); |
| | | subjectMatter.setRemainingQuantity(new BigDecimal("0")); |
| | | subjectMatter.setActualDeliveryDate(DateUtil.today()); |
| | | subjectMatter.setDeliveryStatus(2); |
| | | 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)); |
| | | subjectMatter.setRemainingQuantity(remainNum.multiply(new BigDecimal("-1"))); |
| | | subjectMatter.setActualDeliveryDate(DateUtil.today()); |
| | | subjectMatter.setDeliveryStatus(1); |
| | | subjectMatter.setLastDeliveredQuantity(currentOut); |
| | | contractSubjectMatterMapper.updateById(subjectMatter); |
| | | currentOutList.add(subjectMatter); |
| | | //新增合同出库历史 |
| | | saveContractOutBound(contract,addDTO,subjectMatter); |
| | | break; |
| | | //未交付状态下 |
| | | remainNum = remainNum.subtract(subjectMatter.getQuantity()); |
| | | if (remainNum.compareTo(new BigDecimal("0")) >= 0) { |
| | | subjectMatter.setDeliveredQuantity(subjectMatter.getQuantity()); |
| | | subjectMatter.setRemainingQuantity(new BigDecimal("0")); |
| | | subjectMatter.setActualDeliveryDate(DateUtil.today()); |
| | | subjectMatter.setDeliveryStatus(2); |
| | | subjectMatter.setLastDeliveredQuantity(subjectMatter.getQuantity()); |
| | | contractSubjectMatterMapper.updateById(subjectMatter); |
| | | currentOutMap.put(subjectMatter.getContractId(), subjectMatter); |
| | | //新增合同出库历史 |
| | | saveContractOutBound(contract, addDTO, subjectMatter); |
| | | } else { |
| | | BigDecimal currentOut = remainNum.add(subjectMatter.getQuantity()); |
| | | subjectMatter.setDeliveredQuantity(subjectMatter.getQuantity().add(remainNum)); |
| | | subjectMatter.setRemainingQuantity(remainNum.multiply(new BigDecimal("-1"))); |
| | | subjectMatter.setActualDeliveryDate(DateUtil.today()); |
| | | subjectMatter.setDeliveryStatus(1); |
| | | subjectMatter.setLastDeliveredQuantity(currentOut); |
| | | contractSubjectMatterMapper.updateById(subjectMatter); |
| | | currentOutMap.put(subjectMatter.getContractId(), subjectMatter); |
| | | //新增合同出库历史 |
| | | saveContractOutBound(contract, addDTO, subjectMatter); |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | } |
| | |
| | | }); |
| | | |
| | | //出库应收款明细 |
| | | if (ArrayUtil.isNotEmpty(currentOutList.toArray())) { |
| | | //根据合同id拆分应收明细 |
| | | Map<Long, List<ContractSubjectMatter>> conSubMatMap = currentOutList.stream().collect(Collectors.groupingBy(ContractSubjectMatter::getContractId)); |
| | | |
| | | conSubMatMap.forEach((key, value) -> { |
| | | if (ArrayUtil.isNotEmpty(currentOutMap.keySet())) { |
| | | for (Long contractId : currentOutMap.keySet()) { |
| | | //当前出库合同出库标的物 |
| | | List<ContractSubjectMatter> contractSubjectMatterList = contractSubjectMatterMapper.selectList(Wrappers.<ContractSubjectMatter>lambdaQuery() |
| | | .eq(ContractSubjectMatter::getContractId, contractId).ne(ContractSubjectMatter::getDeliveryStatus, 0)); |
| | | //查询合同是否有出库阶段 |
| | | ContractPaymentSchedule schedule = scheduleMapper.selectOne(Wrappers.<ContractPaymentSchedule>lambdaQuery().eq(ContractPaymentSchedule::getContractId, key) |
| | | ContractPaymentSchedule schedule = scheduleMapper.selectOne(Wrappers.<ContractPaymentSchedule>lambdaQuery().eq(ContractPaymentSchedule::getContractId, contractId) |
| | | .eq(ContractPaymentSchedule::getStageName, "发货前")); |
| | | //当前出库金额 |
| | | BigDecimal outPirce = value.stream() |
| | | .filter(item -> item.getLastDeliveredQuantity() != null && item.getUnitPrice() != null) |
| | | .map(item ->(item.getDeliveryStatus()==1?item.getDeliveredQuantity():item.getQuantity()).multiply(item.getUnitPrice())) |
| | | .reduce(BigDecimal.ZERO, BigDecimal::add); |
| | | //合同金额 |
| | | Contract contract = contractMapper.selectById(key); |
| | | //出库是否完成 |
| | | if (outPirce.compareTo(contract.getAmount())<0){ |
| | | contract.setBillingStatus("1"); |
| | | contractMapper.updateById(contract); |
| | | } |
| | | if (outPirce.compareTo(contract.getAmount())==0){ |
| | | contract.setBillingStatus("2"); |
| | | contractMapper.updateById(contract); |
| | | } |
| | | //存在出库阶段 |
| | | if (ObjUtil.isNotNull(schedule)) { |
| | | //按出库资金比例 计算发货前应收更新资金明细 |
| | | //当前出库金额 |
| | | BigDecimal outPirce = contractSubjectMatterList.stream() |
| | | .map(item -> (item.getDeliveryStatus() == 1 ? item.getDeliveredQuantity().multiply(item.getUnitPrice()) : item.getQuantity().multiply(item.getUnitPrice()))) |
| | | .reduce(BigDecimal.ZERO, BigDecimal::add); |
| | | //合同金额 |
| | | Contract contract = contractMapper.selectById(contractId); |
| | | //出库是否完成 |
| | | if (outPirce.compareTo(contract.getAmount()) < 0) { |
| | | contract.setBillingStatus("1"); |
| | | contractMapper.updateById(contract); |
| | | } |
| | | if (outPirce.compareTo(contract.getAmount()) == 0) { |
| | | contract.setBillingStatus("2"); |
| | | contractMapper.updateById(contract); |
| | | } |
| | | //计算发货前应收更新资金明细 |
| | | PaymentConfirm paymentConfirm = new PaymentConfirm(); |
| | | paymentConfirm.setBusinessType("发货前应收"); |
| | | paymentConfirm.setBusGuestId(contract.getPartyAId()); |
| | |
| | | paymentConfirm.setScheduleId(schedule.getId()); |
| | | paymentConfirm.setScheduleName(schedule.getStageName()); |
| | | paymentConfirm.setConfirmTime(addDTO.getOutBoundTime()); |
| | | //按出库资金比例 计算发货前应收 |
| | | paymentConfirm.setTransationAmount(outPirce.divide(contract.getAmount(), 10, RoundingMode.HALF_UP).multiply(schedule.getPlannedAmount())); |
| | | paymentConfirm.setReceivableAmount(paymentConfirm.getTransationAmount()); |
| | | paymentConfirm.setTotalAmount(paymentConfirm.getTransationAmount().multiply(new BigDecimal("-1"))); |
| | | |
| | | //最近应收 |
| | | PaymentConfirm lastConfirm = paymentConfirmMapper.selectOne(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getContractId, contract.getId()) |
| | | .orderByDesc(PaymentConfirm::getCreateTime).last("limit 1")); |
| | | |
| | | if (ObjUtil.isNotNull(lastConfirm)) { |
| | | paymentConfirm.setTotalAmount(paymentConfirm.getTotalAmount().add(lastConfirm.getTotalAmount())); |
| | | paymentConfirmMapper.insert(paymentConfirm); |
| | | //已存在发货应收 |
| | | if (StrUtil.equals(lastConfirm.getBusinessType(), paymentConfirm.getBusinessType())) { |
| | | |
| | | //查询之前所有发货前应收 |
| | | List<PaymentConfirm> fhConfirmList = paymentConfirmMapper.selectList(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getContractId, contract.getId()) |
| | | .eq(PaymentConfirm::getScheduleId,schedule.getId()).orderByDesc(PaymentConfirm::getCreateTime)); |
| | | BigDecimal fhTotalAmount = fhConfirmList.stream().map(item->item.getTransationAmount()).reduce(BigDecimal.ZERO,BigDecimal::add); |
| | | //本次发货前应收 出库总额 - 所有发货前应收 |
| | | paymentConfirm.setTransationAmount(paymentConfirm.getTransationAmount().subtract(fhTotalAmount)); |
| | | paymentConfirm.setReceivableAmount(paymentConfirm.getTransationAmount()); |
| | | paymentConfirm.setTotalAmount(lastConfirm.getTotalAmount().subtract(paymentConfirm.getTransationAmount())); |
| | | paymentConfirmMapper.insert(paymentConfirm); |
| | | } else { |
| | | //不存在发货应收 出库总额 |
| | | paymentConfirm.setTotalAmount(lastConfirm.getTotalAmount().subtract(paymentConfirm.getTransationAmount())); |
| | | paymentConfirmMapper.insert(paymentConfirm); |
| | | } |
| | | } else { |
| | | //无之前阶段应收 |
| | | paymentConfirm.setTotalAmount(paymentConfirm.getTransationAmount().multiply(new BigDecimal("-1"))); |
| | | paymentConfirmMapper.insert(paymentConfirm); |
| | | } |
| | | |
| | | //更新发货前收款生效时间 |
| | | if (ObjUtil.isNull(schedule.getEffectiveDate())){ |
| | | schedule.setEffectiveDate(DateUtil.offsetDay(addDTO.getOutBoundTime(),schedule.getAgreedDays())); |
| | | if (ObjUtil.isNull(schedule.getEffectiveDate())) { |
| | | schedule.setEffectiveDate(DateUtil.offsetDay(addDTO.getOutBoundTime(), schedule.getAgreedDays())); |
| | | //查询是否有后续阶段 |
| | | List<ContractPaymentSchedule> afterSchedule = scheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery().eq(ContractPaymentSchedule::getContractId, key) |
| | | List<ContractPaymentSchedule> afterSchedule = scheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery().eq(ContractPaymentSchedule::getContractId, contractId) |
| | | .gt(ContractPaymentSchedule::getStageOrder, schedule.getStageOrder())); |
| | | if (ArrayUtil.isEmpty(afterSchedule.toArray())){ |
| | | if (ArrayUtil.isEmpty(afterSchedule.toArray())) { |
| | | schedule.setEffectiveEndDate(contract.getExpirationDate()); |
| | | } |
| | | scheduleMapper.updateById(schedule); |
| | | //查询是否有之前阶段 |
| | | ContractPaymentSchedule beforeSchedule = scheduleMapper.selectOne(Wrappers.<ContractPaymentSchedule>lambdaQuery().eq(ContractPaymentSchedule::getContractId, schedule.getContractId()) |
| | | .lt(ContractPaymentSchedule::getStageOrder, schedule.getStageOrder()).orderByDesc(ContractPaymentSchedule::getCreateTime).last("limit 1")); |
| | | if (ObjUtil.isNotNull(beforeSchedule)){ |
| | | beforeSchedule.setEffectiveEndDate(DateUtil.offsetDay(addDTO.getOutBoundTime(),beforeSchedule.getAgreedDays())); |
| | | if (ObjUtil.isNotNull(beforeSchedule)) { |
| | | beforeSchedule.setEffectiveEndDate(schedule.getEffectiveDate()); |
| | | scheduleMapper.updateById(beforeSchedule); |
| | | } |
| | | //更新履约 |
| | | ContractPaymentScheduleProcess contractPaymentScheduleProcess = new ContractPaymentScheduleProcess(); |
| | | contractPaymentScheduleProcess.setContractId(schedule.getContractId()); |
| | | contractPaymentScheduleProcess.setContractName(schedule.getContractName()); |
| | | contractPaymentScheduleProcess.setScheduleId(schedule.getId()); |
| | | contractPaymentScheduleProcess.setScheduleName(schedule.getStageName()); |
| | | contractPaymentScheduleProcess.setProcessDate(addDTO.getOutBoundTime()); |
| | | scheduleProcessMapper.insert(contractPaymentScheduleProcess); |
| | | } |
| | | //更新履约 |
| | | ContractPaymentScheduleProcess contractPaymentScheduleProcess = new ContractPaymentScheduleProcess(); |
| | | contractPaymentScheduleProcess.setContractId(schedule.getContractId()); |
| | | contractPaymentScheduleProcess.setContractName(schedule.getContractName()); |
| | | contractPaymentScheduleProcess.setScheduleId(schedule.getId()); |
| | | contractPaymentScheduleProcess.setScheduleName(schedule.getStageName()); |
| | | contractPaymentScheduleProcess.setProcessDate(addDTO.getOutBoundTime()); |
| | | scheduleProcessMapper.insert(contractPaymentScheduleProcess); |
| | | |
| | | //查询是否有后续阶段 |
| | | List<ContractPaymentSchedule> afterSchedule = scheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery() |
| | | .eq(ContractPaymentSchedule::getContractId, schedule.getContractId()) |
| | | .gt(ContractPaymentSchedule::getStageOrder, schedule.getStageOrder()) |
| | | .orderByAsc(ContractPaymentSchedule::getCreateTime)); |
| | | if (ArrayUtil.isNotEmpty(afterSchedule.toArray())&&afterSchedule.size()==1){ |
| | | if (ArrayUtil.isNotEmpty(afterSchedule.toArray()) && afterSchedule.size() == 1) { |
| | | //最后阶段生效时间 |
| | | ContractPaymentSchedule endSchedule = afterSchedule.get(0); |
| | | if (StrUtil.equals(endSchedule.getStageName(),"质保金")) { |
| | | if (StrUtil.equals(endSchedule.getStageName(), "质保金")) { |
| | | endSchedule.setEffectiveDate(DateUtil.offsetDay(addDTO.getOutBoundTime(), endSchedule.getAgreedDays())); |
| | | endSchedule.setEffectiveEndDate(contract.getExpirationDate()); |
| | | scheduleMapper.updateById(endSchedule); |
| | |
| | | contract.setNextScheduleName(endSchedule.getStageName()); |
| | | contractMapper.updateById(contract); |
| | | } |
| | | if(ArrayUtil.isNotEmpty(afterSchedule.toArray())&&afterSchedule.size()>1){ |
| | | if (ArrayUtil.isNotEmpty(afterSchedule.toArray()) && afterSchedule.size() > 1) { |
| | | //更新合同下个阶段 |
| | | contract.setNextScheduleName(afterSchedule.get(0).getStageName()); |
| | | contractMapper.updateById(contract); |
| | | } |
| | | if(ArrayUtil.isEmpty(afterSchedule.toArray())){ |
| | | if (ArrayUtil.isEmpty(afterSchedule.toArray())) { |
| | | schedule.setEffectiveEndDate(contract.getExpirationDate()); |
| | | scheduleMapper.updateById(schedule); |
| | | //更新合同下个阶段 |
| | | contract.setNextScheduleName("无"); |
| | | contractMapper.updateById(contract); |
| | | //当前为合同最后阶段 |
| | | PaymentConfirm lastNewConfirm = paymentConfirmMapper.selectOne(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getContractId,contract.getId()) |
| | | .orderByDesc(PaymentConfirm::getCreateTime).last("limit 1")); |
| | | if (lastNewConfirm.getTotalAmount().compareTo(new BigDecimal("0"))>=0){ |
| | | //有预付且超过应收 关闭合同状态 |
| | | contract.setContractStatus(3); |
| | | contractMapper.updateById(contract); |
| | | } |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | } |
| | | |
| | | return R.ok(); |