From a0974f04b5a83e3ab152ce79e53f180e7b496165 Mon Sep 17 00:00:00 2001
From: shiyunteng <shiyunteng@example.com>
Date: 星期一, 08 六月 2026 15:00:51 +0800
Subject: [PATCH] fix:发票管理模块

---
 platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/ContractServiceImpl.java |  317 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 315 insertions(+), 2 deletions(-)

diff --git a/platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/ContractServiceImpl.java b/platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/ContractServiceImpl.java
index 6634811..bec5082 100644
--- a/platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/ContractServiceImpl.java
+++ b/platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/ContractServiceImpl.java
@@ -36,6 +36,7 @@
 import java.io.IOException;
 import java.io.OutputStream;
 import java.math.BigDecimal;
+import java.math.RoundingMode;
 import java.util.*;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicReference;
@@ -58,7 +59,7 @@
 	private final MeterReadRecordMapper meterReadRecordMapper;
 	private final ContractExecDateMapper contractExecDateMapper;
 	private final RemoteFlowProcessService remoteFlowProcessService;
-	private final ContractTemplateMapper contractTemplateMapper;
+	private final ProductMapper productMapper;
 	private final RemoteDeptService remoteDeptService;
 
 	@Value("${file.local.base-path}")
@@ -235,6 +236,10 @@
 		detailVo.setContractSubjectMatter(subjectMatterList);
 		List<ContractPaymentSchedule> paymentScheduleList = contractPaymentScheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery().eq(ContractPaymentSchedule::getContractId, contract.getId()));
 		detailVo.setContractPaymentSchedule(paymentScheduleList);
+		BusinessCustomer a = businessCustomerMapper.selectById(contract.getPartyAId());
+		if (ObjUtil.isNotNull(a)){
+			detailVo.setPartyAOrgCode(a.getCreditCode());
+		}
 		return detailVo;
 	}
 
@@ -657,7 +662,7 @@
 		try {
 			// 1. 鍔犺浇妯$増
 			// 鍋囪妯$増鍦� resources/templates/template.docx
-			ClassPathResource resource = new ClassPathResource("template/ymjjgclht.docx");
+			ClassPathResource resource = new ClassPathResource("template/ymj/ymjjgclht.docx");
 			// 閰嶇疆鍒楄〃绛栫暐
 			Configure config = Configure.builder()
 					.bind("items", new LoopRowTableRenderPolicy()) // 灏� items 缁戝畾鍒拌寰幆绛栫暐
@@ -760,4 +765,312 @@
 		}
 		return R.ok();
 	}
+
+	@Override
+	public void exportContractYMJGYPMMWord(Long id, HttpServletResponse response) {
+		Contract contract = baseMapper.selectById(id);
+		List<ContractSubjectMatter> subjectMatterList = contractSubjectMatterMapper.selectList(Wrappers.<ContractSubjectMatter>lambdaQuery()
+				.eq(ContractSubjectMatter::getContractId, id).orderByAsc(ContractSubjectMatter::getCreateTime));
+		Map<String, Object> map = new HashMap<>();
+		//鍚堝悓鍩烘湰淇℃伅
+		map.put("partyA", contract.getPartyA());
+		map.put("partyB", contract.getPartyB());
+		map.put("contractNo", contract.getContractNo());
+		map.put("signPlace", contract.getSignPlace());
+		map.put("signDate", DateUtil.formatDate(contract.getSignDate()));
+		map.put("signDateYMD", DateUtil.format(contract.getSignDate(), DatePattern.CHINESE_DATE_PATTERN));
+		map.put("total", contract.getAmount());
+		map.put("amountWords", Convert.digitToChinese(contract.getAmount()));
+		//鏍囩殑鐗�
+		List<Map<String, Object>> items = new ArrayList<>();
+		List<RowRenderData> dataList = new ArrayList<>();
+		AtomicReference<Integer> no = new AtomicReference<>(0);
+		subjectMatterList.stream().forEach(contractSubjectMatter -> {
+			Map<String, Object> item = new HashMap<>();
+			item.put("no", no);
+			item.put("materialName", contractSubjectMatter.getMaterialName());
+			item.put("guige", contractSubjectMatter.getSpecification());
+			item.put("unit", contractSubjectMatter.getUnit());
+			item.put("quantity", contractSubjectMatter.getQuantity());
+			item.put("danzhong", contractSubjectMatter.getSingleWight());
+			item.put("zongzhong", contractSubjectMatter.getTotalWight());
+			item.put("unitPrice", contractSubjectMatter.getUnitPrice());
+			item.put("price", contractSubjectMatter.getTotalAmount());
+			if (no.get().equals(Integer.valueOf(0))){
+				item.put("deliveryCycle",contract.getDeliveryCycle());
+			}
+			items.add(item);
+			no.updateAndGet(v -> v + 1);
+		});
+		map.put("items", items);
+		//瀹氬埗鏂�
+		BusinessCustomer customer = businessCustomerMapper.selectById(contract.getPartyAId());
+		map.put("companyName", customer.getCompanyName());
+		map.put("legalPerson", customer.getLegalPerson());
+		map.put("contactPhone", customer.getContactPhone());
+		map.put("bankName", customer.getBankName());
+		map.put("bankAccount", customer.getBankAccount());
+
+		//鎵挎徑鏂�
+		R<SysDept> r = remoteDeptService.getById(contract.getPartyBId());
+		SysDept dept = r.getData();
+		map.put("orgName", dept.getOrgName());
+		map.put("legalPerson1", StrUtil.isNotEmpty(dept.getLegalPerson()) ? dept.getLegalPerson() : "");
+		map.put("entrustedAgent", StrUtil.isNotEmpty(dept.getLegalPerson()) ? "" : dept.getEntrustedAgent());
+		map.put("orgContact", dept.getOrgContact());
+		map.put("orgBank", dept.getOrgBank());
+		map.put("orgBankAccount", dept.getOrgBankAccount());
+
+		//鐢熸垚鏂囦欢鍚�
+		Long time = new Date().getTime();
+		// 鐢熸垚鐨剋ord鏍煎紡
+		String formatSuffix = ".docx";
+		// 鎷兼帴鍚庣殑鏂囦欢鍚�
+		String fileName = time + formatSuffix;//鏂囦欢鍚�  甯﹀悗缂�
+		//瀵煎嚭word
+		try {
+			// 1. 鍔犺浇妯$増
+			// 鍋囪妯$増鍦� resources/templates/template.docx
+			ClassPathResource resource = new ClassPathResource("template/ymj/ymjgypmmht.docx");
+			// 閰嶇疆鍒楄〃绛栫暐
+			Configure config = Configure.builder()
+					.bind("items", new LoopRowTableRenderPolicy()) // 灏� items 缁戝畾鍒拌寰幆绛栫暐
+					.build();
+			// 2. 缂栬瘧骞舵覆鏌撴暟鎹�
+			XWPFTemplate xwpfTemplate = XWPFTemplate.compile(resource.getInputStream(), config).render(map);
+
+			// 3. 璁剧疆鍝嶅簲澶�
+			response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
+			response.setCharacterEncoding("utf-8");
+			response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".docx");
+
+			// 4. 鍐欏叆杈撳嚭娴�
+			OutputStream out = response.getOutputStream();
+			xwpfTemplate.write(out);
+			out.flush();
+			out.close();
+			xwpfTemplate.close(); // 閲嶈锛氬叧闂ā鐗堥噴鏀捐祫婧�
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+	}
+
+	@Override
+	public void exportContractYMJGKCPMMWord(Long id, HttpServletResponse response) {
+		Contract contract = baseMapper.selectById(id);
+		List<ContractSubjectMatter> subjectMatterList = contractSubjectMatterMapper.selectList(Wrappers.<ContractSubjectMatter>lambdaQuery()
+				.eq(ContractSubjectMatter::getContractId, id).orderByAsc(ContractSubjectMatter::getCreateTime));
+		Map<String, Object> map = new HashMap<>();
+		//鍚堝悓鍩烘湰淇℃伅
+		map.put("partyA", contract.getPartyA());
+		map.put("partyB", contract.getPartyB());
+		map.put("contractNo", contract.getContractNo());
+		map.put("signPlace", contract.getSignPlace());
+		map.put("signDate", DateUtil.formatDate(contract.getSignDate()));
+		map.put("signDateYMD", DateUtil.format(contract.getSignDate(), DatePattern.CHINESE_DATE_PATTERN));
+		map.put("total", contract.getAmount());
+		map.put("amountWords", Convert.digitToChinese(contract.getAmount()));
+		map.put("deliveryCycle",contract.getDeliveryCycle());
+		//鏍囩殑鐗�
+		List<Map<String, Object>> items = new ArrayList<>();
+		AtomicReference<Integer> no = new AtomicReference<>(0);
+		subjectMatterList.stream().forEach(contractSubjectMatter -> {
+			Map<String, Object> item = new HashMap<>();
+			item.put("no", no);
+			item.put("materialName", contractSubjectMatter.getMaterialName());
+			item.put("guige", contractSubjectMatter.getSpecification());
+			item.put("unit", contractSubjectMatter.getUnit());
+			item.put("quantity", contractSubjectMatter.getQuantity());
+			item.put("danzhong", contractSubjectMatter.getSingleWight());
+			item.put("zongzhong", contractSubjectMatter.getTotalWight());
+			item.put("unitPrice", contractSubjectMatter.getUnitPrice());
+			item.put("price", contractSubjectMatter.getTotalAmount());
+			items.add(item);
+			no.updateAndGet(v -> v + 1);
+		});
+		map.put("items", items);
+		//瀹氬埗鏂�
+		BusinessCustomer customer = businessCustomerMapper.selectById(contract.getPartyAId());
+		map.put("companyName", customer.getCompanyName());
+		map.put("legalPerson", customer.getLegalPerson());
+		map.put("contactPhone", customer.getContactPhone());
+		map.put("bankName", customer.getBankName());
+		map.put("bankAccount", customer.getBankAccount());
+
+		//鎵挎徑鏂�
+		R<SysDept> r = remoteDeptService.getById(contract.getPartyBId());
+		SysDept dept = r.getData();
+		map.put("orgName", dept.getOrgName());
+		map.put("legalPerson1", StrUtil.isNotEmpty(dept.getLegalPerson()) ? dept.getLegalPerson() : "");
+		map.put("entrustedAgent", StrUtil.isNotEmpty(dept.getLegalPerson()) ? "" : dept.getEntrustedAgent());
+		map.put("orgContact", dept.getOrgContact());
+		map.put("orgBank", dept.getOrgBank());
+		map.put("orgBankAccount", dept.getOrgBankAccount());
+
+		//鐢熸垚鏂囦欢鍚�
+		Long time = new Date().getTime();
+		// 鐢熸垚鐨剋ord鏍煎紡
+		String formatSuffix = ".docx";
+		// 鎷兼帴鍚庣殑鏂囦欢鍚�
+		String fileName = time + formatSuffix;//鏂囦欢鍚�  甯﹀悗缂�
+		//瀵煎嚭word
+		try {
+			// 1. 鍔犺浇妯$増
+			// 鍋囪妯$増鍦� resources/templates/template.docx
+			ClassPathResource resource = new ClassPathResource("template/ymj/ymjgkcpmmht.docx");
+			// 閰嶇疆鍒楄〃绛栫暐
+			Configure config = Configure.builder()
+					.bind("items", new LoopRowTableRenderPolicy()) // 灏� items 缁戝畾鍒拌寰幆绛栫暐
+					.build();
+			// 2. 缂栬瘧骞舵覆鏌撴暟鎹�
+			XWPFTemplate xwpfTemplate = XWPFTemplate.compile(resource.getInputStream(), config).render(map);
+
+			// 3. 璁剧疆鍝嶅簲澶�
+			response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
+			response.setCharacterEncoding("utf-8");
+			response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".docx");
+
+			// 4. 鍐欏叆杈撳嚭娴�
+			OutputStream out = response.getOutputStream();
+			xwpfTemplate.write(out);
+			out.flush();
+			out.close();
+			xwpfTemplate.close(); // 閲嶈锛氬叧闂ā鐗堥噴鏀捐祫婧�
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+	}
+
+	@Override
+	public void exportContractYMJCGWord(Long id, HttpServletResponse response) {
+		Contract contract = baseMapper.selectById(id);
+		List<ContractSubjectMatter> subjectMatterList = contractSubjectMatterMapper.selectList(Wrappers.<ContractSubjectMatter>lambdaQuery()
+				.eq(ContractSubjectMatter::getContractId, id).orderByAsc(ContractSubjectMatter::getCreateTime));
+		Map<String, Object> map = new HashMap<>();
+		//鍚堝悓鍩烘湰淇℃伅
+		map.put("partyA", contract.getPartyA());
+		map.put("partyB", contract.getPartyB());
+		map.put("contractNo", contract.getContractNo());
+		map.put("signPlace", contract.getSignPlace());
+		map.put("signDate", DateUtil.formatDate(contract.getSignDate()));
+		map.put("signDateYMD", DateUtil.format(contract.getSignDate(), DatePattern.CHINESE_DATE_PATTERN));
+		map.put("total", contract.getAmount());
+		map.put("amountWords", Convert.digitToChinese(contract.getAmount()));
+		map.put("environment",contract.getEnvironment());
+		map.put("standard",contract.getStandard());
+		map.put("useMonth",contract.getUseMonth());
+		map.put("afterShipMonth",contract.getAfterShipMonth());
+		map.put("shipMethod",contract.getShipMethod());
+		map.put("shipAddress",contract.getShipAddress());
+		map.put("packaging",contract.getPackaging());
+		map.put("repairPeriod",contract.getRepairPeriod());
+		map.put("repairBreachAmount",contract.getRepairBreachAmount());
+		map.put("goodsShort",contract.getGoodsShort());
+		map.put("goodsShortBreachAmount",contract.getGoodsShortBreachAmount());
+		map.put("overdueBreachAmount",contract.getOverdueBreachAmount());
+		map.put("terminateContract",contract.getTerminateContract());
+		map.put("court",contract.getCourt());
+		map.put("contractAttchment",contract.getContractAttchment());
+		map.put("invoiceNotice",contract.getInvoiceNotice());
+
+		//鏍囩殑鐗�
+		List<Map<String, Object>> items = new ArrayList<>();
+		AtomicReference<Integer> no = new AtomicReference<>(0);
+		subjectMatterList.stream().forEach(contractSubjectMatter -> {
+			Product product = productMapper.selectOne(Wrappers.<Product>lambdaQuery().eq(Product::getErpCode,contractSubjectMatter.getMaterialCode()).last("limit 1"));
+			Map<String, Object> item = new HashMap<>();
+			item.put("no", no);
+			item.put("materialName", contractSubjectMatter.getMaterialName());
+			item.put("guige", contractSubjectMatter.getSpecification());
+			item.put("unit", contractSubjectMatter.getUnit());
+			item.put("quantity", contractSubjectMatter.getQuantity());
+			item.put("unitPrice", contractSubjectMatter.getUnitPrice());
+			item.put("priceTax", contractSubjectMatter.getUnitPrice().multiply(contractSubjectMatter.getQuantity()).setScale(2, RoundingMode.HALF_UP));
+			item.put("priceNoTax", contractSubjectMatter.getUnitPrice().multiply(contractSubjectMatter.getQuantity())
+					.subtract(contractSubjectMatter.getUnitPrice().multiply(contractSubjectMatter.getQuantity())
+							.divide(new BigDecimal("100").add(new BigDecimal(product.getTaxRate())),2, RoundingMode.HALF_UP).multiply(new BigDecimal(product.getTaxRate())))
+					.setScale(2, RoundingMode.HALF_UP));
+			item.put("deliveryDate", DateUtil.formatDate(contractSubjectMatter.getPlannedDeliveryDate()));
+			item.put("remarks", contractSubjectMatter.getRemarks());
+			map.put("taxRate",product.getTaxRate());
+			items.add(item);
+			no.updateAndGet(v -> v + 1);
+		});
+
+		//浠樻
+		List<ContractPaymentSchedule> scheduleList = contractPaymentScheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery()
+				.eq(ContractPaymentSchedule::getContractId, id).orderByAsc(ContractPaymentSchedule::getCreateTime));
+		scheduleList.stream().forEach(contractPaymentSchedule -> {
+			if (contractPaymentSchedule.getStageName().equals("鍚堝悓绛捐")){
+				map.put("htqdbl",contractPaymentSchedule.getPaymentRatio());
+				map.put("htqdje",contractPaymentSchedule.getPlannedAmount());
+				map.put("htqdjr",contractPaymentSchedule.getAgreedDays());
+			}
+			if (contractPaymentSchedule.getStageName().equals("鍙戣揣鍓�")){
+				map.put("fhqbl",contractPaymentSchedule.getPaymentRatio());
+				map.put("fhqje",contractPaymentSchedule.getPlannedAmount());
+			}
+			if (contractPaymentSchedule.getStageName().equals("璋冭瘯瀹屾垚鎴栭獙鏀�")){
+				map.put("ysbl",contractPaymentSchedule.getPaymentRatio());
+				map.put("ysje",contractPaymentSchedule.getPlannedAmount());
+				map.put("ysjr",contractPaymentSchedule.getAgreedDays());
+			}
+			if (contractPaymentSchedule.getStageName().equals("璐ㄤ繚閲�")){
+				map.put("zbjbl",contractPaymentSchedule.getPaymentRatio());
+				map.put("zbjje",contractPaymentSchedule.getPlannedAmount());
+				map.put("zbjjr",contractPaymentSchedule.getAgreedDays());
+			}
+		});
+		map.put("items", items);
+		//瀹氬埗鏂�
+		BusinessCustomer customer = businessCustomerMapper.selectById(contract.getPartyAId());
+		map.put("companyName", customer.getCompanyName());
+		map.put("legalPerson", customer.getLegalPerson());
+		map.put("contactPhone", customer.getContactPhone());
+		map.put("bankName", customer.getBankName());
+		map.put("bankAccount", customer.getBankAccount());
+
+		//鎵挎徑鏂�
+		R<SysDept> r = remoteDeptService.getById(contract.getPartyBId());
+		SysDept dept = r.getData();
+		map.put("orgName", dept.getOrgName());
+		map.put("legalPerson1", StrUtil.isNotEmpty(dept.getLegalPerson()) ? dept.getLegalPerson() : "");
+		map.put("entrustedAgent", StrUtil.isNotEmpty(dept.getLegalPerson()) ? "" : dept.getEntrustedAgent());
+		map.put("orgContact", dept.getOrgContact());
+		map.put("orgBank", dept.getOrgBank());
+		map.put("orgBankAccount", dept.getOrgBankAccount());
+
+		//鐢熸垚鏂囦欢鍚�
+		Long time = new Date().getTime();
+		// 鐢熸垚鐨剋ord鏍煎紡
+		String formatSuffix = ".docx";
+		// 鎷兼帴鍚庣殑鏂囦欢鍚�
+		String fileName = time + formatSuffix;//鏂囦欢鍚�  甯﹀悗缂�
+		//瀵煎嚭word
+		try {
+			// 1. 鍔犺浇妯$増
+			// 鍋囪妯$増鍦� resources/templates/template.docx
+			ClassPathResource resource = new ClassPathResource("template/ymj/ymjcght.docx");
+			// 閰嶇疆鍒楄〃绛栫暐
+			Configure config = Configure.builder()
+					.bind("items", new LoopRowTableRenderPolicy()) // 灏� items 缁戝畾鍒拌寰幆绛栫暐
+					.build();
+			// 2. 缂栬瘧骞舵覆鏌撴暟鎹�
+			XWPFTemplate xwpfTemplate = XWPFTemplate.compile(resource.getInputStream(), config).render(map);
+
+			// 3. 璁剧疆鍝嶅簲澶�
+			response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
+			response.setCharacterEncoding("utf-8");
+			response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".docx");
+
+			// 4. 鍐欏叆杈撳嚭娴�
+			OutputStream out = response.getOutputStream();
+			xwpfTemplate.write(out);
+			out.flush();
+			out.close();
+			xwpfTemplate.close(); // 閲嶈锛氬叧闂ā鐗堥噴鏀捐祫婧�
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+	}
 }

--
Gitblit v1.9.1