From b1b3da5393c75ced1afa1467be69d60af5403878 Mon Sep 17 00:00:00 2001
From: shiyunteng <shiyunteng@example.com>
Date: 星期四, 27 八月 2026 14:25:16 +0800
Subject: [PATCH] feat:合同审核、出库及履约阶段代码优化

---
 platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/ContractInvoiceServiceImpl.java |  158 +++++++++++++++++++++++++++-------------------------
 1 files changed, 82 insertions(+), 76 deletions(-)

diff --git a/platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/ContractInvoiceServiceImpl.java b/platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/ContractInvoiceServiceImpl.java
index 9085c51..f652c00 100644
--- a/platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/ContractInvoiceServiceImpl.java
+++ b/platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/ContractInvoiceServiceImpl.java
@@ -5,7 +5,6 @@
 import cn.hutool.core.convert.Convert;
 import cn.hutool.core.date.DatePattern;
 import cn.hutool.core.date.DateUtil;
-import cn.hutool.core.io.StreamProgress;
 import cn.hutool.core.util.ArrayUtil;
 import cn.hutool.core.util.IdUtil;
 import cn.hutool.core.util.ObjUtil;
@@ -104,8 +103,11 @@
 						.eq(ContractSubjectMatter::getMaterialCode, contractInvoiceOutBoundVo.getSubjectMatterCode())
 						.eq(StrUtil.isNotBlank(contractInvoiceOutBoundVo.getLineNo()), ContractSubjectMatter::getLineNo, contractInvoiceOutBoundVo.getLineNo())
 						.last("limit 1"));
-				if (ObjUtil.isNotNull(subjectMatter) && subjectMatter.getAdvanceNum().compareTo(new BigDecimal("0")) > 0) {
-					contractInvoiceOutBoundVo.setInvoiceNum(contractInvoiceOutBoundVo.getOutBoundNum().subtract(subjectMatter.getAdvanceNum()));
+				if (ObjUtil.isNotNull(subjectMatter)) {
+					contractInvoiceOutBoundVo.setUnitPrice(subjectMatter.getUnitPrice());
+					if (subjectMatter.getAdvanceNum().compareTo(new BigDecimal("0")) > 0) {
+						contractInvoiceOutBoundVo.setInvoiceNum(contractInvoiceOutBoundVo.getOutBoundNum().subtract(subjectMatter.getAdvanceNum()));
+					}
 				}
 				contractInvoiceOutBoundVo.setToInvoiceNum(contractInvoiceOutBoundVo.getInvoiceNum());
 			});
@@ -145,6 +147,11 @@
 	public R genInvoiceInfo(GenInvoiceInfoDTO genInvoiceInfoDTO) {
 		if (CollUtil.isEmpty(genInvoiceInfoDTO.getOutBoundInvoiceList())) {
 			return R.failed("璇烽�夋嫨寮�绁ㄥ嚭搴撳崟鍚庡啀鎿嶄綔");
+		}
+		genInvoiceInfoDTO.setOutBoundInvoiceList(genInvoiceInfoDTO.getOutBoundInvoiceList().stream().filter(item->
+				item.getInvoiceNum().compareTo(new BigDecimal("0"))>0).collect(Collectors.toList()));
+		if (CollUtil.isEmpty(genInvoiceInfoDTO.getOutBoundInvoiceList())) {
+			return R.failed("璇烽�夋嫨寮�绁ㄥ嚭搴撳崟鍚庢垨璋冩暣寮�绁ㄦ暟閲忓啀鎿嶄綔");
 		}
 		Long contractId = null;
 		//棰勬敹娆惧紑绁�
@@ -318,11 +325,14 @@
 			if (CollUtil.isNotEmpty(genInvoiceInfoDTO.getOutBoundInvoiceList())) {
 				genInvoiceInfoDTO.getOutBoundInvoiceList().stream().forEach(contractOutBoundInvoiceVo -> {
 					ContractOutBound contractOutBound = contractOutBoundMapper.selectById(contractOutBoundInvoiceVo.getContractOutBoundId());
-
-					if (!CollUtil.contains(erpCode, contractOutBound.getSubjectMatterCode())) {
-						erpCode.add(contractOutBound.getSubjectMatterCode());
+					ContractSubjectMatter subjectMatter = subjectMatterMapper.selectOne(Wrappers.<ContractSubjectMatter>lambdaQuery()
+							.eq(ContractSubjectMatter::getMaterialCode, contractOutBound.getSubjectMatterCode())
+							.eq(StrUtil.isNotBlank(contractOutBound.getLineNo()),ContractSubjectMatter::getLineNo,contractOutBound.getLineNo())
+							.eq(ContractSubjectMatter::getContractId, contract.getId()).last("limit 1"));
+					if (!CollUtil.contains(erpCode, contractOutBound.getSubjectMatterCode()+subjectMatter.getUnitPrice())) {
+						erpCode.add(contractOutBound.getSubjectMatterCode()+subjectMatter.getUnitPrice());
 						ContractOutBoundMergeVo mergeVo = new ContractOutBoundMergeVo();
-						mergeVo.setMaterialCode(contractOutBound.getSubjectMatterCode());
+						mergeVo.setMaterialCode(contractOutBound.getSubjectMatterCode()+subjectMatter.getUnitPrice());
 						mergeVo.setMaterialInternalName(contractOutBound.getSubjectMatterName());
 						mergeVo.setQuantity(contractOutBoundInvoiceVo.getInvoiceNum());
 						Product product = productMapper.selectOne(Wrappers.<Product>lambdaQuery().eq(Product::getErpCode, contractOutBound.getSubjectMatterCode())
@@ -333,9 +343,6 @@
 							mergeVo.setTaxRate(new BigDecimal(product.getTaxRate()));
 							mergeVo.setTaxCode(product.getTaxCode());
 						}
-						ContractSubjectMatter subjectMatter = subjectMatterMapper.selectOne(Wrappers.<ContractSubjectMatter>lambdaQuery()
-								.eq(ContractSubjectMatter::getMaterialCode, contractOutBound.getSubjectMatterCode())
-								.eq(ContractSubjectMatter::getContractId, contract.getId()).last("limit 1"));
 						if (ObjUtil.isNotNull(subjectMatter)) {
 							mergeVo.setUnitPrice(subjectMatter.getUnitPrice());
 							mergeVo.setUnit(subjectMatter.getUnit());
@@ -352,7 +359,7 @@
 						mergeOutBounds.add(mergeVo);
 					} else {
 						List<ContractOutBoundMergeVo> filteredList = mergeOutBounds.stream()
-								.filter(item -> contractOutBound.getSubjectMatterCode().equals(item.getMaterialCode())) // 闃叉绌烘寚閽堬紝寤鸿甯搁噺鍦ㄥ墠
+								.filter(item -> item.getMaterialCode().equals(subjectMatter.getMaterialCode()+subjectMatter.getUnitPrice())) // 闃叉绌烘寚閽堬紝寤鸿甯搁噺鍦ㄥ墠
 								.collect(Collectors.toList());
 						if (ArrayUtil.isNotEmpty(filteredList)) {
 							filteredList.get(0).setQuantity(filteredList.get(0).getQuantity().add(contractOutBound.getOutBoundNum()));
@@ -394,27 +401,24 @@
 							mergeVo.setSpecification(product.getProductType());
 							mergeVo.setTaxRate(new BigDecimal(product.getTaxRate()));
 							mergeVo.setTaxCode(product.getTaxCode());
-							Product productClass = productMapper.selectById(product.getParentId());
-							if (ObjUtil.isNotNull(product)) {
-								if (StrUtil.equals(product.getErpCode(), "water_bill")) {
-									mergeVo.setItemName("*" + product.getTaxClass() + "*姘磋垂");
-									mergeVo.setUnit("鍚�");
-								} else if (StrUtil.equals(product.getErpCode(), "electricity_bill")) {
-									mergeVo.setItemName("*" + product.getTaxClass() + "*鐢佃垂");
-									mergeVo.setUnit("搴�");
-								} else if (StrUtil.equals(product.getErpCode(), "house_rental")) {
-									mergeVo.setItemName("*" + product.getTaxClass() + "*鎴垮眿绉熻祦");
-								} else {
-									mergeVo.setItemName("*" + product.getTaxClass() + "*" + productClass.getProductName());
-								}
-
-							}
 						}
 						ContractSubjectMatter subjectMatter = subjectMatterMapper.selectOne(Wrappers.<ContractSubjectMatter>lambdaQuery()
 								.eq(ContractSubjectMatter::getMaterialCode, schedule.getSubjectMatterCode())
 								.eq(ContractSubjectMatter::getContractId, contract.getId()).last("limit 1"));
 						if (ObjUtil.isNotNull(subjectMatter)) {
 							mergeVo.setUnitPrice(subjectMatter.getUnitPrice());
+							if (StrUtil.equals(product.getErpCode(), "water_bill")) {
+								mergeVo.setItemName("*" + product.getTaxClass() + "*姘磋垂");
+								mergeVo.setUnit("鍚�");
+							} else if (StrUtil.equals(product.getErpCode(), "electricity_bill")) {
+								mergeVo.setItemName("*" + product.getTaxClass() + "*鐢佃垂");
+								mergeVo.setUnit("搴�");
+							} else if (StrUtil.equals(product.getErpCode(), "house_rental")) {
+								mergeVo.setItemName("*" + product.getTaxClass() + "*鎴垮眿绉熻祦");
+							} else {
+								mergeVo.setItemName("*" + product.getTaxClass() + "*" + subjectMatter.getMaterialName());
+							}
+
 						}
 						mergeOutBounds.add(mergeVo);
 					} else {
@@ -465,7 +469,7 @@
 			genInvoiceInfoDTO.getOutBoundInvoiceList().stream().forEach(contractOutBoundInvoiceVo -> {
 				ContractSubjectMatter subjectMatter = contractSubjectMatterMapper.selectById(contractOutBoundInvoiceVo.getContractOutBoundId());
 				if (ObjUtil.isNotNull(subjectMatter)) {
-					if (!CollUtil.contains(erpCode, subjectMatter.getMaterialCode())) {
+					if (!CollUtil.contains(erpCode, subjectMatter.getMaterialCode()+subjectMatter.getUnitPrice())) {
 						erpCode.add(subjectMatter.getMaterialCode());
 						ContractOutBoundMergeVo mergeVo = new ContractOutBoundMergeVo();
 						mergeVo.setMaterialCode(subjectMatter.getMaterialCode());
@@ -478,21 +482,20 @@
 							mergeVo.setSpecification(product.getProductType());
 							mergeVo.setTaxRate(new BigDecimal(product.getTaxRate()));
 							mergeVo.setTaxCode(product.getTaxCode());
-							Product productClass = productMapper.selectById(product.getParentId());
-							if (ObjUtil.isNotNull(product)) {
-								if (StrUtil.equals(product.getErpCode(), "water_bill")) {
-									mergeVo.setItemName("*" + product.getTaxClass() + "*姘磋垂");
-									mergeVo.setUnit("鍚�");
-								} else if (StrUtil.equals(product.getErpCode(), "electricity_bill")) {
-									mergeVo.setItemName("*" + product.getTaxClass() + "*鐢佃垂");
-									mergeVo.setUnit("搴�");
-								} else if (StrUtil.equals(product.getErpCode(), "house_rental")) {
-									mergeVo.setItemName("*" + product.getTaxClass() + "*鎴垮眿绉熻祦");
-								} else {
-									mergeVo.setItemName("*" + product.getTaxClass() + "*" + productClass.getProductName());
-								}
-
+						}
+						if (ObjUtil.isNotNull(product)) {
+							if (StrUtil.equals(product.getErpCode(), "water_bill")) {
+								mergeVo.setItemName("*" + product.getTaxClass() + "*姘磋垂");
+								mergeVo.setUnit("鍚�");
+							} else if (StrUtil.equals(product.getErpCode(), "electricity_bill")) {
+								mergeVo.setItemName("*" + product.getTaxClass() + "*鐢佃垂");
+								mergeVo.setUnit("搴�");
+							} else if (StrUtil.equals(product.getErpCode(), "house_rental")) {
+								mergeVo.setItemName("*" + product.getTaxClass() + "*鎴垮眿绉熻祦");
+							} else {
+								mergeVo.setItemName("*" + product.getTaxClass() + "*" + subjectMatter.getMaterialName());
 							}
+
 						}
 						mergeVo.setUnitPrice(subjectMatter.getUnitPrice());
 						mergeVo.setUnit(subjectMatter.getUnit());
@@ -523,8 +526,13 @@
 
 	@Override
 	public R toInvoice(GenInvoiceInfoDTO genInvoiceInfoDTO) {
-		if (ArrayUtil.isEmpty(genInvoiceInfoDTO.getOutBoundInvoiceList())) {
+		if (CollUtil.isEmpty(genInvoiceInfoDTO.getOutBoundInvoiceList())) {
 			return R.failed("璇烽�夋嫨寮�绁ㄥ嚭搴撳崟鍚庡啀鎿嶄綔");
+		}
+		genInvoiceInfoDTO.setOutBoundInvoiceList(genInvoiceInfoDTO.getOutBoundInvoiceList().stream().filter(item->
+				item.getInvoiceNum().compareTo(new BigDecimal("0"))>0).collect(Collectors.toList()));
+		if (CollUtil.isEmpty(genInvoiceInfoDTO.getOutBoundInvoiceList())) {
+			return R.failed("璇烽�夋嫨寮�绁ㄥ嚭搴撳崟鍚庢垨璋冩暣寮�绁ㄦ暟閲忓啀鎿嶄綔");
 		}
 		if (StrUtil.equals(genInvoiceInfoDTO.getInvoiceType(), "3") || StrUtil.equals(genInvoiceInfoDTO.getInvoiceType(), "4")) {
 			if (StrUtil.isBlank(genInvoiceInfoDTO.getVehicleNo())) {
@@ -646,9 +654,7 @@
 			genInvoiceInfoDTO.getOutBoundInvoiceList().stream().forEach(contractOutBoundInvoiceVo -> {
 				ContractOutBound contractOutBound = contractOutBoundMapper.selectById(contractOutBoundInvoiceVo.getContractOutBoundId());
 				contractOutBound.setInvoiceStatus("1");
-				if (contractOutBoundInvoiceVo.getInvoiceNum().compareTo(contractOutBound.getOutBoundNum().subtract(contractOutBound.getInvoiceNum())) <= 0) {
-					contractOutBound.setInvoiceNum(contractOutBound.getInvoiceNum().add(contractOutBoundInvoiceVo.getInvoiceNum()));
-				}
+				contractOutBound.setInvoiceNum(contractOutBound.getInvoiceNum().add(contractOutBoundInvoiceVo.getInvoiceNum()));
 				contractOutBoundMapper.updateById(contractOutBound);
 			});
 		} else {
@@ -1220,29 +1226,30 @@
 			}
 			if (!StrUtil.equals(contract.getContractCategory(), "water_house")) {
 				//鍚堝悓鐘舵��
-				if (StrUtil.equals(contract.getBillingStatus(), "2")) {
-					contract.setBillingAmout(vo.getTotalPrice().add(contract.getBillingAmout()));
-					if (contract.getBillingAmout().compareTo(contract.getAmount()) == 0 &&
-							contract.getPaidAmount().compareTo(contract.getAmount().subtract(contract.getDropAmount())) == 0) {
-						contract.setBillingStatus("3");
-						contract.setContractStatus(3);
-					}
-					if (contract.getBillingAmout().compareTo(contract.getAmount()) < 0) {
-						contract.setBillingStatus("1");
-						List<ContractOutBound> outBoundList = contractOutBoundMapper.selectList(Wrappers.<ContractOutBound>lambdaQuery()
-								.eq(ContractOutBound::getContractId, contract.getId())
-								.apply("invoice_num < out_bound_num"));
-						if (CollUtil.isNotEmpty(outBoundList)) {
-							contract.setBillingStatus("2");
-						}
-
-					}
-					//闃叉閲嶅瀹″紑绁ㄥ鏍搁�氳繃鍚� 寮�绁ㄩ噾棰濊秴鍑哄悎鍚岄噾棰�
-					if (contract.getBillingAmout().compareTo(contract.getAmount()) > 0) {
-						contract.setBillingAmout(contract.getAmount());
-					}
-					contractMapper.updateById(contract);
+				contract.setBillingAmout(vo.getTotalPrice().add(contract.getBillingAmout()));
+				if (contract.getBillingAmout().compareTo(contract.getAmount()) == 0) {
+					contract.setBillingStatus("3");
 				}
+				if (contract.getPaidAmount().compareTo(contract.getAmount().subtract(contract.getDropAmount())) >= 0&&
+						contract.getBillingAmout().compareTo(contract.getAmount()) == 0){
+					contract.setContractStatus(3);
+				}
+				if (contract.getBillingAmout().compareTo(contract.getAmount()) < 0) {
+					contract.setBillingStatus("1");
+					List<ContractOutBound> outBoundList = contractOutBoundMapper.selectList(Wrappers.<ContractOutBound>lambdaQuery()
+							.eq(ContractOutBound::getContractId, contract.getId())
+							.apply("invoice_num < out_bound_num"));
+					if (CollUtil.isNotEmpty(outBoundList)) {
+						contract.setBillingStatus("2");
+					}
+
+				}
+				//闃叉閲嶅瀹″紑绁ㄥ鏍搁�氳繃鍚� 寮�绁ㄩ噾棰濊秴鍑哄悎鍚岄噾棰�
+				if (contract.getBillingAmout().compareTo(contract.getAmount()) > 0) {
+					contract.setBillingAmout(contract.getAmount());
+				}
+				contractMapper.updateById(contract);
+
 			} else {
 				//鍒ゆ柇 寰幆鏄惁瀹屾垚
 				List<ContractExecDate> execDateList = contractExecDateMapper.selectList(Wrappers.<ContractExecDate>lambdaQuery().eq(ContractExecDate::getContractId, contract.getId())
@@ -1257,8 +1264,11 @@
 						contract.setBillingStatus("2");
 					}
 					contract.setBillingAmout(vo.getTotalPrice().add(contract.getBillingAmout()));
+					if (contract.getBillingAmout().compareTo(contract.getAmount()) == 0) {
+						contract.setBillingStatus("3");
+					}
 					if (contract.getBillingAmout().compareTo(contract.getAmount()) == 0 &&
-							contract.getPaidAmount().compareTo(contract.getAmount().subtract(contract.getDropAmount())) == 0) {
+							contract.getPaidAmount().compareTo(contract.getAmount().subtract(contract.getDropAmount())) >= 0) {
 						contract.setBillingStatus("3");
 						contract.setContractStatus(3);
 					}
@@ -1352,10 +1362,8 @@
 				if (ArrayUtil.isNotEmpty(outBoundInvoiceVoList)) {
 					outBoundInvoiceVoList.stream().forEach(contractOutBoundInvoiceVo -> {
 						ContractOutBound contractOutBound = contractOutBoundMapper.selectById(contractOutBoundInvoiceVo.getContractOutBoundId());
-						if (contractOutBoundInvoiceVo.getInvoiceNum().compareTo(contractOutBound.getOutBoundNum().subtract(contractOutBound.getInvoiceNum())) <= 0) {
-							contractOutBound.setInvoiceNum(contractOutBound.getInvoiceNum().subtract(contractOutBoundInvoiceVo.getInvoiceNum()));
-							contractOutBoundMapper.updateById(contractOutBound);
-						}
+						contractOutBound.setInvoiceNum(contractOutBound.getInvoiceNum().subtract(contractOutBoundInvoiceVo.getInvoiceNum()));
+						contractOutBoundMapper.updateById(contractOutBound);
 					});
 				}
 			} else {
@@ -1437,10 +1445,8 @@
 				if (ArrayUtil.isNotEmpty(outBoundInvoiceVoList)) {
 					outBoundInvoiceVoList.stream().forEach(contractOutBoundInvoiceVo -> {
 						ContractOutBound contractOutBound = contractOutBoundMapper.selectById(contractOutBoundInvoiceVo.getContractOutBoundId());
-						if (contractOutBoundInvoiceVo.getInvoiceNum().compareTo(contractOutBound.getOutBoundNum().subtract(contractOutBound.getInvoiceNum())) <= 0) {
-							contractOutBound.setInvoiceNum(contractOutBound.getInvoiceNum().subtract(contractOutBoundInvoiceVo.getInvoiceNum()));
-							contractOutBoundMapper.updateById(contractOutBound);
-						}
+						contractOutBound.setInvoiceNum(contractOutBound.getInvoiceNum().subtract(contractOutBoundInvoiceVo.getInvoiceNum()));
+						contractOutBoundMapper.updateById(contractOutBound);
 					});
 				}
 			} else {

--
Gitblit v1.9.1