From 0ee46d33cc339f3fd17593e6bfe2f033afe6019a Mon Sep 17 00:00:00 2001
From: shiyunteng <shiyunteng@example.com>
Date: 星期三, 17 六月 2026 15:14:13 +0800
Subject: [PATCH] feat:合同新增时显示供应属性 出库新增根据合同出库 资金账目新增时间条件检索 新增延期出货菜单

---
 platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/OutBoundServiceImpl.java |  421 ++++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 354 insertions(+), 67 deletions(-)

diff --git a/platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/OutBoundServiceImpl.java b/platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/OutBoundServiceImpl.java
index ba953e1..79dd3bc 100644
--- a/platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/OutBoundServiceImpl.java
+++ b/platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/OutBoundServiceImpl.java
@@ -40,6 +40,7 @@
 	private final ContractMapper contractMapper;
 	private final ContractPaymentScheduleProcessMapper scheduleProcessMapper;
 	private final ContractOutBoundMapper contractOutBoundMapper;
+	private final DeliveryOverdueMapper deliveryOverdueMapper;
 
 	@Override
 	public R add(OutBoundAddDTO addDTO) {
@@ -52,6 +53,7 @@
 				.in(ContractSubjectMatter::getMaterialCode, addDTO.getSubjectMatterList().stream().map(item -> item.getSubjectMatterCode()).collect(Collectors.toList()))
 				.exists(Contract.class, contractQuery ->
 						contractQuery.select(ContractSubjectMatter::getId)
+								.eq(Contract::getContractStatus, 2)
 								.eq(Contract::getId, ContractSubjectMatter::getContractId)
 								.eq(StrUtil.isNotBlank(addDTO.getBusGuestName()), Contract::getPartyA, addDTO.getBusGuestName())
 								.eq(ObjUtil.isNotNull(addDTO.getBusGuestId()), Contract::getPartyAId, addDTO.getBusGuestId())
@@ -62,11 +64,11 @@
 			return R.failed("娌℃湁鏌ヨ鍒扮浉鍏冲悎鍚岃鍗�");
 		}
 		List<String> erpCodeList = subjectMatterList.stream().map(ContractSubjectMatter::getMaterialCode).collect(Collectors.toList());
-		BigDecimal orderNum = subjectMatterList.stream().filter(item ->item.getDeliveryStatus()!=2)
-				.map(item ->item.getDeliveryStatus()==0?item.getQuantity():item.getRemainingQuantity())
+		BigDecimal orderNum = subjectMatterList.stream().filter(item -> item.getDeliveryStatus() != 2)
+				.map(item -> item.getDeliveryStatus() == 0 ? item.getQuantity() : item.getRemainingQuantity())
 				.reduce(BigDecimal.ZERO, BigDecimal::add);
 		BigDecimal outTotal = addDTO.getSubjectMatterList().stream()
-				.filter(item->erpCodeList.contains(item.getSubjectMatterCode()))
+				.filter(item -> erpCodeList.contains(item.getSubjectMatterCode()))
 				.map(OutSubjectMatterAddDTO::getOutBoundNum)
 				.filter(num -> num != null) // 杩囨护鎺� null 鍊硷紝闃叉 NullPointerException
 				.reduce(BigDecimal.ZERO, BigDecimal::add);
@@ -92,55 +94,55 @@
 					if (subjectMatter.getDeliveredQuantity().compareTo(new BigDecimal("0")) > 0) {
 						remainNum = remainNum.subtract(subjectMatter.getRemainingQuantity());
 						//閮ㄥ垎浜や粯鐘舵�佷笅 鍏ㄩ儴鍑哄簱
-						if (remainNum.compareTo(new BigDecimal("0")) >= 0) {
+						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.setActualDeliveryDate(addDTO.getOutBoundTime());
 							subjectMatter.setDeliveryStatus(2);
 							subjectMatter.setLastDeliveredQuantity(currentOut);
 							contractSubjectMatterMapper.updateById(subjectMatter);
 							currentOutMap.put(subjectMatter.getContractId(), subjectMatter);
 							//鏂板鍚堝悓鍑哄簱鍘嗗彶
-							saveContractOutBound(contract, addDTO, subjectMatter,outBound.getId());
+							saveContractOutBound(contract, addDTO, subjectMatter, outBound.getId());
 						} else {
 							BigDecimal currentOut = remainNum.add(subjectMatter.getRemainingQuantity());
 							subjectMatter.setDeliveredQuantity(subjectMatter.getQuantity().add(remainNum));
 							subjectMatter.setRemainingQuantity(subjectMatter.getRemainingQuantity().subtract(currentOut));
-							subjectMatter.setActualDeliveryDate(DateUtil.today());
+							subjectMatter.setActualDeliveryDate(addDTO.getOutBoundTime());
 							subjectMatter.setDeliveryStatus(1);
 							subjectMatter.setLastDeliveredQuantity(currentOut);
 							contractSubjectMatterMapper.updateById(subjectMatter);
 							currentOutMap.put(subjectMatter.getContractId(), subjectMatter);
 							//鏂板鍚堝悓鍑哄簱鍘嗗彶
-							saveContractOutBound(contract, addDTO, subjectMatter,outBound.getId());
+							saveContractOutBound(contract, addDTO, subjectMatter, outBound.getId());
 							break;
 						}
 
 					} else {
 						//鏈氦浠樼姸鎬佷笅
 						remainNum = remainNum.subtract(subjectMatter.getQuantity());
-						if (remainNum.compareTo(new BigDecimal("0")) >= 0) {
+						if (remainNum.compareTo(new BigDecimal("0")) > 0) {
 							subjectMatter.setDeliveredQuantity(subjectMatter.getQuantity());
 							subjectMatter.setRemainingQuantity(new BigDecimal("0"));
-							subjectMatter.setActualDeliveryDate(DateUtil.today());
+							subjectMatter.setActualDeliveryDate(addDTO.getOutBoundTime());
 							subjectMatter.setDeliveryStatus(2);
 							subjectMatter.setLastDeliveredQuantity(subjectMatter.getQuantity());
 							contractSubjectMatterMapper.updateById(subjectMatter);
 							currentOutMap.put(subjectMatter.getContractId(), subjectMatter);
 							//鏂板鍚堝悓鍑哄簱鍘嗗彶
-							saveContractOutBound(contract, addDTO, subjectMatter,outBound.getId());
+							saveContractOutBound(contract, addDTO, subjectMatter, outBound.getId());
 						} 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.setActualDeliveryDate(addDTO.getOutBoundTime());
 							subjectMatter.setDeliveryStatus(1);
 							subjectMatter.setLastDeliveredQuantity(currentOut);
 							contractSubjectMatterMapper.updateById(subjectMatter);
 							currentOutMap.put(subjectMatter.getContractId(), subjectMatter);
 							//鏂板鍚堝悓鍑哄簱鍘嗗彶
-							saveContractOutBound(contract, addDTO, subjectMatter,outBound.getId());
+							saveContractOutBound(contract, addDTO, subjectMatter, outBound.getId());
 							break;
 						}
 					}
@@ -200,41 +202,41 @@
 
 							//鏌ヨ涔嬪墠鎵�鏈夊彂璐у墠搴旀敹
 							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);
+									.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()));
-							if(schedule.getPaymentRatio().compareTo(new BigDecimal("0"))>0) {
+							if (schedule.getPaymentRatio().compareTo(new BigDecimal("0")) > 0) {
 								paymentConfirmMapper.insert(paymentConfirm);
 							}
 						} else {
 							//涓嶅瓨鍦ㄥ彂璐у簲鏀� 鍑哄簱鎬婚
 							paymentConfirm.setTotalAmount(lastConfirm.getTotalAmount().subtract(paymentConfirm.getTransationAmount()));
-							if(schedule.getPaymentRatio().compareTo(new BigDecimal("0"))>0) {
+							if (schedule.getPaymentRatio().compareTo(new BigDecimal("0")) > 0) {
 								paymentConfirmMapper.insert(paymentConfirm);
 							}
 						}
 					} else {
 						//鏃犱箣鍓嶉樁娈靛簲鏀�
 						paymentConfirm.setTotalAmount(paymentConfirm.getTransationAmount().multiply(new BigDecimal("-1")));
-						if(schedule.getPaymentRatio().compareTo(new BigDecimal("0"))>0) {
+						if (schedule.getPaymentRatio().compareTo(new BigDecimal("0")) > 0) {
 							paymentConfirmMapper.insert(paymentConfirm);
 							//瀹㈡埛浠樻瀹屾垚鍚堝悓
 							List<Contract> customerCompleteContractList = contractMapper.selectList(Wrappers.<Contract>lambdaQuery().eq(Contract::getPartyAId, contract.getPartyAId())
 									.eq(Contract::getPartyBId, SecurityUtils.getUser().getCompId()).apply(" amount = paid_amount"));
-							if (ArrayUtil.isNotEmpty(customerCompleteContractList)){
+							if (ArrayUtil.isNotEmpty(customerCompleteContractList)) {
 								List<BigDecimal> outAmountList = new ArrayList<>();
-								for (Contract completeContract:customerCompleteContractList
+								for (Contract completeContract : customerCompleteContractList
 								) {
-									PaymentConfirm contractLastConfirm = paymentConfirmMapper.selectOne(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getContractId,completeContract.getId())
+									PaymentConfirm contractLastConfirm = paymentConfirmMapper.selectOne(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getContractId, completeContract.getId())
 											.orderByDesc(PaymentConfirm::getCreateTime).last("limit 1"));
 									//鏌ヨ瀹㈡埛浠樻瀹屾垚鍚堝悓涓渶鍚庢槑缁嗛噾棰� 鏈夐鏀惰浆鍏ュ綋鍓嶅悎鍚�
-									if (contractLastConfirm.getTotalAmount().compareTo(new BigDecimal("0"))>0){
+									if (contractLastConfirm.getTotalAmount().compareTo(new BigDecimal("0")) > 0) {
 										//鏂板瀹屾垚浠樻鍚堝悓 璧勯噾杞嚭
-										PaymentConfirm outConfirm = BeanUtil.copyProperties(contractLastConfirm,PaymentConfirm.class,"id","transationAmount",
-												"advanceAmount","receivableAmount","overdueAmount","totalAmount");
+										PaymentConfirm outConfirm = BeanUtil.copyProperties(contractLastConfirm, PaymentConfirm.class, "id", "transationAmount",
+												"advanceAmount", "receivableAmount", "overdueAmount", "totalAmount");
 										outConfirm.setBusinessType("璧勯噾杞嚭");
 										outConfirm.setConfirmTime(new Date());
 										outConfirm.setTransationAmount(contractLastConfirm.getTotalAmount());
@@ -243,25 +245,28 @@
 										outAmountList.add(outConfirm.getTransationAmount());
 									}
 								}
-								if (ArrayUtil.isNotEmpty(outAmountList.toArray())){
-									BigDecimal currentSum = new BigDecimal("0");
-									for (BigDecimal currentAmount:outAmountList
-									) {
-										currentSum=currentSum.add(currentAmount);
-										//鏂板褰撳墠鍚堝悓 璧勯噾杞叆
-										PaymentConfirm inConfirm = BeanUtil.copyProperties(paymentConfirm,PaymentConfirm.class,"id","transationAmount",
-												"advanceAmount","receivableAmount","overdueAmount","totalAmount");
-										inConfirm.setBusinessType("璧勯噾杞叆");
-										inConfirm.setConfirmTime(new Date());
-										inConfirm.setTransationAmount(currentSum.subtract(currentAmount));
-										inConfirm.setTotalAmount(paymentConfirm.getTotalAmount().add(currentSum));
-										if (inConfirm.getTotalAmount().compareTo(new BigDecimal("0"))>0){
-											inConfirm.setAdvanceAmount(inConfirm.getTotalAmount());
-										}else if (inConfirm.getTotalAmount().compareTo(new BigDecimal("0"))<0){
-											inConfirm.setReceivableAmount(inConfirm.getTotalAmount().multiply(new BigDecimal("-1")));
-										}
-										paymentConfirmMapper.insert(inConfirm);
+								if (ArrayUtil.isNotEmpty(outAmountList.toArray())) {
+									BigDecimal currentInSum = outAmountList.stream().reduce(BigDecimal.ZERO, BigDecimal::add);
+									//鏂板褰撳墠鍚堝悓 璧勯噾杞叆
+									PaymentConfirm inConfirm = BeanUtil.copyProperties(paymentConfirm, PaymentConfirm.class, "id", "transationAmount",
+											"advanceAmount", "receivableAmount", "overdueAmount", "totalAmount");
+									inConfirm.setBusinessType("璧勯噾杞叆");
+									inConfirm.setConfirmTime(new Date());
+									inConfirm.setTransationAmount(currentInSum);
+									inConfirm.setTotalAmount(paymentConfirm.getTotalAmount().add(currentInSum));
+									//鏇存柊褰撳墠闃舵瀹為檯鏀舵 鍜� 鏀舵鐘舵��
+									schedule.setActualAmount(inConfirm.getTransationAmount());
+									schedule.setPaymentStatus(2);
+									if (inConfirm.getTotalAmount().compareTo(new BigDecimal("0")) > 0) {
+										inConfirm.setAdvanceAmount(inConfirm.getTotalAmount());
+									} else if (inConfirm.getTotalAmount().compareTo(new BigDecimal("0")) < 0) {
+										inConfirm.setReceivableAmount(inConfirm.getTotalAmount().multiply(new BigDecimal("-1")));
+										schedule.setActualAmount(paymentConfirm.getTotalAmount().subtract(currentInSum));
+										schedule.setPaymentStatus(1);
 									}
+									paymentConfirmMapper.insert(inConfirm);
+									//鏇存柊褰撳墠闃舵瀹為檯鏀舵 鍜� 鏀舵鐘舵��
+									scheduleMapper.updateById(schedule);
 								}
 							}
 						}
@@ -269,21 +274,21 @@
 
 					//鏇存柊鍙戣揣鍓嶆敹娆剧敓鏁堟椂闂�
 					if (ObjUtil.isNull(schedule.getEffectiveDate())) {
-						schedule.setEffectiveDate(DateUtil.offsetDay(addDTO.getOutBoundTime(), schedule.getAgreedDays()));
+						schedule.setEffectiveEndDate(DateUtil.offsetDay(addDTO.getOutBoundTime(), schedule.getAgreedDays()));
 						//鏌ヨ鏄惁鏈夊悗缁樁娈�
-						List<ContractPaymentSchedule> afterSchedule = scheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery().eq(ContractPaymentSchedule::getContractId, contractId)
-								.gt(ContractPaymentSchedule::getStageOrder, schedule.getStageOrder()));
-						if (ArrayUtil.isEmpty(afterSchedule.toArray())) {
-							schedule.setEffectiveEndDate(contract.getExpirationDate());
-						}
+//						List<ContractPaymentSchedule> afterSchedule = scheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery().eq(ContractPaymentSchedule::getContractId, contractId)
+//								.gt(ContractPaymentSchedule::getStageOrder, schedule.getStageOrder()));
+//						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(schedule.getEffectiveDate());
-							scheduleMapper.updateById(beforeSchedule);
-						}
+//						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(schedule.getEffectiveDate());
+//							scheduleMapper.updateById(beforeSchedule);
+//						}
 						//鏇存柊灞ョ害
 						ContractPaymentScheduleProcess contractPaymentScheduleProcess = new ContractPaymentScheduleProcess();
 						contractPaymentScheduleProcess.setContractId(schedule.getContractId());
@@ -291,7 +296,11 @@
 						contractPaymentScheduleProcess.setScheduleId(schedule.getId());
 						contractPaymentScheduleProcess.setScheduleName(schedule.getStageName());
 						contractPaymentScheduleProcess.setProcessDate(addDTO.getOutBoundTime());
-						scheduleProcessMapper.insert(contractPaymentScheduleProcess);
+						ContractPaymentScheduleProcess lastProcess = scheduleProcessMapper.selectOne(Wrappers.<ContractPaymentScheduleProcess>lambdaQuery().eq(ContractPaymentScheduleProcess::getContractId, schedule.getContractId())
+								.eq(ContractPaymentScheduleProcess::getScheduleId, schedule.getId()).last("limit 1"));
+						if (ObjUtil.isNull(lastProcess)) {
+							scheduleProcessMapper.insert(contractPaymentScheduleProcess);
+						}
 					}
 
 					//鏌ヨ鏄惁鏈夊悗缁樁娈�
@@ -303,13 +312,11 @@
 						//鏈�鍚庨樁娈电敓鏁堟椂闂�
 						ContractPaymentSchedule endSchedule = afterSchedule.get(0);
 						if (StrUtil.equals(endSchedule.getStageName(), "璐ㄤ繚閲�")) {
-							endSchedule.setEffectiveDate(DateUtil.offsetDay(addDTO.getOutBoundTime(), endSchedule.getAgreedDays()));
-							endSchedule.setEffectiveEndDate(contract.getExpirationDate());
-							scheduleMapper.updateById(endSchedule);
+//							endSchedule.setEffectiveEndDate(DateUtil.offsetDay(addDTO.getOutBoundTime(), endSchedule.getAgreedDays()));
+//							scheduleMapper.updateById(endSchedule);
 							//褰撳墠闃舵鐢熸晥鏃堕棿
-							schedule.setEffectiveDate(DateUtil.offsetDay(addDTO.getOutBoundTime(), schedule.getAgreedDays()));
-							schedule.setEffectiveEndDate(endSchedule.getEffectiveDate());
-							scheduleMapper.updateById(schedule);
+//							schedule.setEffectiveEndDate(DateUtil.offsetDay(addDTO.getOutBoundTime(), schedule.getAgreedDays()));
+//							scheduleMapper.updateById(schedule);
 							//鏈�鍚庨樁娈靛簲鏀�
 							PaymentConfirm newConfim = new PaymentConfirm();
 							newConfim.setBusinessType(endSchedule.getStageName() + "搴旀敹");
@@ -330,7 +337,7 @@
 								lastNewTotal = lastNewConfirm.getTotalAmount();
 							}
 							newConfim.setTotalAmount(lastNewTotal.subtract(newConfim.getReceivableAmount()));
-							if(endSchedule.getPaymentRatio().compareTo(new BigDecimal("0"))>0) {
+							if (endSchedule.getPaymentRatio().compareTo(new BigDecimal("0")) > 0) {
 								paymentConfirmMapper.insert(newConfim);
 							}
 						}
@@ -344,15 +351,15 @@
 						contractMapper.updateById(contract);
 					}
 					if (ArrayUtil.isEmpty(afterSchedule.toArray())) {
-						schedule.setEffectiveEndDate(contract.getExpirationDate());
-						scheduleMapper.updateById(schedule);
+//						schedule.setEffectiveEndDate(contract.getExpirationDate());
+//						scheduleMapper.updateById(schedule);
 						//鏇存柊鍚堝悓涓嬩釜闃舵
 						contract.setNextScheduleName("鏃�");
 						contractMapper.updateById(contract);
 						//褰撳墠涓哄悎鍚屾渶鍚庨樁娈�
-						PaymentConfirm lastNewConfirm = paymentConfirmMapper.selectOne(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getContractId,contract.getId())
+						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){
+						if (lastNewConfirm.getTotalAmount().compareTo(new BigDecimal("0")) >= 0) {
 							//鏈夐浠樹笖瓒呰繃搴旀敹 鍏抽棴鍚堝悓鐘舵��
 							contract.setContractStatus(3);
 							contractMapper.updateById(contract);
@@ -365,7 +372,7 @@
 		return R.ok();
 	}
 
-	private void saveContractOutBound(Contract contract, OutBoundAddDTO addDTO, ContractSubjectMatter subjectMatter,Long outBoundId) {
+	private void saveContractOutBound(Contract contract, OutBoundAddDTO addDTO, ContractSubjectMatter subjectMatter, Long outBoundId) {
 		ContractOutBound contractOutBound = new ContractOutBound();
 		contractOutBound.setContractId(contract.getId());
 		contractOutBound.setContractName(contract.getContractName());
@@ -378,6 +385,286 @@
 		contractOutBound.setOutBoundAttNames(addDTO.getOutBoundAttNames());
 		contractOutBound.setOutBoundAttPaths(addDTO.getOutBoundAttPaths());
 		contractOutBound.setOutBoundId(outBoundId);
+		contractOutBound.setInvoiceStatus("0");
+		contractOutBound.setInvoiceNum(new BigDecimal("0"));
 		contractOutBoundMapper.insert(contractOutBound);
+		if (StrUtil.isEmpty(contract.getContractCategory())) {
+			if (DateUtil.compare(addDTO.getOutBoundTime(), contract.getExpirationDate()) > 0) {
+				saveOverdueOutBound(contract, addDTO, subjectMatter);
+			}
+		}
+	}
+
+	private void saveOverdueOutBound(Contract contract, OutBoundAddDTO addDTO, ContractSubjectMatter subjectMatter) {
+		DeliveryOverdue overdue = new DeliveryOverdue();
+		overdue.setContractId(contract.getId());
+		overdue.setContractName(contract.getContractName());
+		overdue.setBusGuestId(contract.getPartyAId());
+		overdue.setBusGuestName(contract.getPartyA());
+		overdue.setMaterialName(subjectMatter.getMaterialName());
+		overdue.setMaterialInternalName(subjectMatter.getMaterialInternalName());
+		overdue.setMaterialCode(subjectMatter.getMaterialCode());
+		overdue.setOverdueNum(subjectMatter.getLastDeliveredQuantity());
+		overdue.setTotalAmount(subjectMatter.getUnitPrice().multiply(subjectMatter.getLastDeliveredQuantity()));
+		overdue.setOverdueDuration(new BigDecimal(DateUtil.betweenDay(addDTO.getOutBoundTime(), contract.getExpirationDate(), true)));
+		deliveryOverdueMapper.insert(overdue);
+	}
+
+	@Override
+	public R addContractOut(OutBoundAddDTO addDTO) {
+		if (ArrayUtil.isEmpty(addDTO.getSubjectMatterList().toArray())) {
+			return R.failed("鍑哄簱鏍囩殑鐗╀笉鑳戒负绌�");
+		}
+		Contract contract = contractMapper.selectOne(Wrappers.<Contract>lambdaQuery().eq(Contract::getContractNo, addDTO.getContractNo()).eq(Contract::getContractStatus, 2)
+				.last("limit 1"));
+		if (ObjUtil.isNull(contract)) {
+			return R.failed("鍚堝悓淇℃伅鏌ヨ澶辫触");
+		}
+		//鍑哄簱鎵规
+		String batchNumber = DateUtil.format(new Date(), DatePattern.PURE_DATETIME_MS_PATTERN);
+
+		addDTO.getSubjectMatterList().stream().forEach(outSubjectMatterAddDTO -> {
+			ContractSubjectMatter subjectMatter = contractSubjectMatterMapper.selectById(outSubjectMatterAddDTO.getId());
+			subjectMatter.setLastDeliveredQuantity(outSubjectMatterAddDTO.getOutBoundNum());
+			if (subjectMatter.getDeliveryStatus() == 0) {
+				subjectMatter.setDeliveredQuantity(outSubjectMatterAddDTO.getOutBoundNum());
+				subjectMatter.setRemainingQuantity(subjectMatter.getQuantity().subtract(outSubjectMatterAddDTO.getOutBoundNum()));
+			}
+			if (subjectMatter.getDeliveryStatus() == 1) {
+				subjectMatter.setDeliveredQuantity(subjectMatter.getDeliveredQuantity().add(outSubjectMatterAddDTO.getOutBoundNum()));
+				subjectMatter.setRemainingQuantity(subjectMatter.getRemainingQuantity().subtract(outSubjectMatterAddDTO.getOutBoundNum()));
+			}
+			subjectMatter.setDeliveryStatus(1);
+			if (subjectMatter.getRemainingQuantity().compareTo(new BigDecimal("0")) == 0) {
+				subjectMatter.setDeliveryStatus(2);
+			}
+			subjectMatter.setActualDeliveryDate(addDTO.getOutBoundTime());
+			contractSubjectMatterMapper.updateById(subjectMatter);
+			//
+			OutBound outBound = new OutBound();
+			outBound.setBatchNumber(batchNumber);
+			outBound.setOutBoundNum(outSubjectMatterAddDTO.getOutBoundNum());
+			outBound.setOutBoundAttNames(addDTO.getOutBoundAttNames());
+			outBound.setOutBoundAttPaths(addDTO.getOutBoundAttPaths());
+			outBound.setBusGuestName(contract.getPartyA());
+			outBound.setBusGuestId(contract.getPartyAId());
+			outBound.setOutBoundTime(addDTO.getOutBoundTime());
+			outBound.setSubjectMatterCode(outSubjectMatterAddDTO.getSubjectMatterCode());
+			outBound.setSubjectMatterName(outSubjectMatterAddDTO.getSubjectMatterName());
+			baseMapper.insert(outBound);
+			//
+			saveContractOutBound(contract, addDTO, subjectMatter, outBound.getId());
+		});
+		//褰撳墠鍑哄簱鍚堝悓鍑哄簱鏍囩殑鐗�
+		List<ContractSubjectMatter> contractSubjectMatterList = contractSubjectMatterMapper.selectList(Wrappers.<ContractSubjectMatter>lambdaQuery()
+				.eq(ContractSubjectMatter::getContractId, contract.getId()).ne(ContractSubjectMatter::getDeliveryStatus, 0));
+		//鏌ヨ鍚堝悓鏄惁鏈夊嚭搴撻樁娈�
+		ContractPaymentSchedule schedule = scheduleMapper.selectOne(Wrappers.<ContractPaymentSchedule>lambdaQuery().eq(ContractPaymentSchedule::getContractId, contract.getId())
+				.eq(ContractPaymentSchedule::getStageName, "鍙戣揣鍓�"));
+		//瀛樺湪鍑哄簱闃舵
+		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);
+			//鍑哄簱鏄惁瀹屾垚
+			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.setBusGuestName(contract.getPartyA());
+			paymentConfirm.setContractId(contract.getId());
+			paymentConfirm.setContractName(contract.getContractName());
+			paymentConfirm.setContractNo(contract.getContractNo());
+			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 lastConfirm = paymentConfirmMapper.selectOne(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getContractId, contract.getId())
+					.orderByDesc(PaymentConfirm::getCreateTime).last("limit 1"));
+
+			if (ObjUtil.isNotNull(lastConfirm)) {
+				//宸插瓨鍦ㄥ彂璐у簲鏀�
+				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()));
+					if (schedule.getPaymentRatio().compareTo(new BigDecimal("0")) > 0) {
+						paymentConfirmMapper.insert(paymentConfirm);
+					}
+				} else {
+					//涓嶅瓨鍦ㄥ彂璐у簲鏀� 鍑哄簱鎬婚
+					paymentConfirm.setTotalAmount(lastConfirm.getTotalAmount().subtract(paymentConfirm.getTransationAmount()));
+					if (schedule.getPaymentRatio().compareTo(new BigDecimal("0")) > 0) {
+						paymentConfirmMapper.insert(paymentConfirm);
+					}
+				}
+			} else {
+				//鏃犱箣鍓嶉樁娈靛簲鏀�
+				paymentConfirm.setTotalAmount(paymentConfirm.getTransationAmount().multiply(new BigDecimal("-1")));
+				if (schedule.getPaymentRatio().compareTo(new BigDecimal("0")) > 0) {
+					paymentConfirmMapper.insert(paymentConfirm);
+					//瀹㈡埛浠樻瀹屾垚鍚堝悓
+					List<Contract> customerCompleteContractList = contractMapper.selectList(Wrappers.<Contract>lambdaQuery().eq(Contract::getPartyAId, contract.getPartyAId())
+							.eq(Contract::getPartyBId, SecurityUtils.getUser().getCompId()).apply(" amount = paid_amount"));
+					if (ArrayUtil.isNotEmpty(customerCompleteContractList)) {
+						List<BigDecimal> outAmountList = new ArrayList<>();
+						for (Contract completeContract : customerCompleteContractList
+						) {
+							PaymentConfirm contractLastConfirm = paymentConfirmMapper.selectOne(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getContractId, completeContract.getId())
+									.orderByDesc(PaymentConfirm::getCreateTime).last("limit 1"));
+							//鏌ヨ瀹㈡埛浠樻瀹屾垚鍚堝悓涓渶鍚庢槑缁嗛噾棰� 鏈夐鏀惰浆鍏ュ綋鍓嶅悎鍚�
+							if (contractLastConfirm.getTotalAmount().compareTo(new BigDecimal("0")) > 0) {
+								//鏂板瀹屾垚浠樻鍚堝悓 璧勯噾杞嚭
+								PaymentConfirm outConfirm = BeanUtil.copyProperties(contractLastConfirm, PaymentConfirm.class, "id", "transationAmount",
+										"advanceAmount", "receivableAmount", "overdueAmount", "totalAmount");
+								outConfirm.setBusinessType("璧勯噾杞嚭");
+								outConfirm.setConfirmTime(new Date());
+								outConfirm.setTransationAmount(contractLastConfirm.getTotalAmount());
+								outConfirm.setTotalAmount(new BigDecimal("0"));
+								paymentConfirmMapper.insert(outConfirm);
+								outAmountList.add(outConfirm.getTransationAmount());
+							}
+						}
+						if (ArrayUtil.isNotEmpty(outAmountList.toArray())) {
+							BigDecimal currentInSum = outAmountList.stream().reduce(BigDecimal.ZERO, BigDecimal::add);
+							//鏂板褰撳墠鍚堝悓 璧勯噾杞叆
+							PaymentConfirm inConfirm = BeanUtil.copyProperties(paymentConfirm, PaymentConfirm.class, "id", "transationAmount",
+									"advanceAmount", "receivableAmount", "overdueAmount", "totalAmount");
+							inConfirm.setBusinessType("璧勯噾杞叆");
+							inConfirm.setConfirmTime(new Date());
+							inConfirm.setTransationAmount(currentInSum);
+							inConfirm.setTotalAmount(paymentConfirm.getTotalAmount().add(currentInSum));
+							//鏇存柊褰撳墠闃舵瀹為檯鏀舵 鍜� 鏀舵鐘舵��
+							schedule.setActualAmount(inConfirm.getTransationAmount());
+							schedule.setPaymentStatus(2);
+							if (inConfirm.getTotalAmount().compareTo(new BigDecimal("0")) > 0) {
+								inConfirm.setAdvanceAmount(inConfirm.getTotalAmount());
+							} else if (inConfirm.getTotalAmount().compareTo(new BigDecimal("0")) < 0) {
+								inConfirm.setReceivableAmount(inConfirm.getTotalAmount().multiply(new BigDecimal("-1")));
+								schedule.setActualAmount(paymentConfirm.getTotalAmount().subtract(currentInSum));
+								schedule.setPaymentStatus(1);
+							}
+							paymentConfirmMapper.insert(inConfirm);
+							//鏇存柊褰撳墠闃舵瀹為檯鏀舵 鍜� 鏀舵鐘舵��
+							scheduleMapper.updateById(schedule);
+						}
+					}
+				}
+			}
+
+			//鏇存柊鍙戣揣鍓嶆敹娆剧敓鏁堟椂闂�
+			if (ObjUtil.isNull(schedule.getEffectiveDate())) {
+				schedule.setEffectiveEndDate(DateUtil.offsetDay(addDTO.getOutBoundTime(), schedule.getAgreedDays()));
+				//鏌ヨ鏄惁鏈夊悗缁樁娈�
+//						List<ContractPaymentSchedule> afterSchedule = scheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery().eq(ContractPaymentSchedule::getContractId, contractId)
+//								.gt(ContractPaymentSchedule::getStageOrder, schedule.getStageOrder()));
+//						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(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());
+				ContractPaymentScheduleProcess lastProcess = scheduleProcessMapper.selectOne(Wrappers.<ContractPaymentScheduleProcess>lambdaQuery().eq(ContractPaymentScheduleProcess::getContractId, schedule.getContractId())
+						.eq(ContractPaymentScheduleProcess::getScheduleId, schedule.getId()).last("limit 1"));
+				if (ObjUtil.isNull(lastProcess)) {
+					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) {
+				//鏈�鍚庨樁娈电敓鏁堟椂闂�
+				ContractPaymentSchedule endSchedule = afterSchedule.get(0);
+				if (StrUtil.equals(endSchedule.getStageName(), "璐ㄤ繚閲�")) {
+//							endSchedule.setEffectiveEndDate(DateUtil.offsetDay(addDTO.getOutBoundTime(), endSchedule.getAgreedDays()));
+//							scheduleMapper.updateById(endSchedule);
+					//褰撳墠闃舵鐢熸晥鏃堕棿
+//							schedule.setEffectiveEndDate(DateUtil.offsetDay(addDTO.getOutBoundTime(), schedule.getAgreedDays()));
+//							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()));
+					if (endSchedule.getPaymentRatio().compareTo(new BigDecimal("0")) > 0) {
+						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);
+				//褰撳墠涓哄悎鍚屾渶鍚庨樁娈�
+				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();
 	}
 }
\ No newline at end of file

--
Gitblit v1.9.1