🔥 移除账单导出模板收款账户一致性校验

This commit is contained in:
caojia
2026-11-20 14:56:44 +08:00
parent 0caeb2ce88
commit 4fdb1d5802
4 changed files with 12 additions and 71 deletions
@@ -112,38 +112,20 @@ class ArBillTemplateSelectionTest {
} }
@Test @Test
void fixedPayeeTemplatesRejectMismatchesBeforeCreatingExportTasks() { void fixedPayeeTemplatesAllowDifferentBillAccountSnapshots() {
ArExportTemplate fixed = template(3L, ArBillWorkbookWriters.GUANGZHOU_TEMPLATE_CODE); bill.setBankAccountName("其他收款单位");
fixed.setStorageKey("templates/receivable/ar_bill_template_gz.xlsx");
when(templates.selectEnabledById(3L, "AR_BILL", bill.getBillDate())).thenReturn(fixed);
bill.setBankAccountName("威狮国际供应链(深圳)有限公司广州分公司");
bill.setBankAccountNo("44050129162500001250");
bill.setBankName("中国建设银行股份有限公司广州设计之都支行");
assertEquals(3L, service.exportBill(7L, 3L, "admin", "matching").getTemplateId());
bill.setBankAccountNo("1234567890"); bill.setBankAccountNo("1234567890");
ServiceException error = assertThrows(ServiceException.class,
() -> service.exportBill(7L, 3L, "admin", "mismatch"));
assertTrue(error.getMessage().contains("收款信息"));
verify(exports, times(1)).insert(any());
}
@Test
void fixedPayeeTemplateValidationChecksNameAccountAndBankForBothTemplates() {
ArBillWorkbookWriters writers = new ArBillWorkbookWriters(new DefaultArBillWorkbookWriter(), new SummaryArBillWorkbookWriter());
ArExportTemplate fixed = template(3L, ArBillWorkbookWriters.JIYUN_TEMPLATE_CODE);
fixed.setStorageKey("templates/receivable/ar_bill_template_jy.xlsx");
bill.setBankAccountName("威狮国际集运物流(深圳)有限公司");
bill.setBankAccountNo("86021110000892050");
bill.setBankName("宁波银行深圳后海支行");
assertDoesNotThrow(() -> writers.validatePayee(bill, fixed));
bill.setBankName("其他银行"); bill.setBankName("其他银行");
assertThrows(ServiceException.class, () -> writers.validatePayee(bill, fixed)); ArExportTemplate guangzhou = template(3L, ArBillWorkbookWriters.GUANGZHOU_TEMPLATE_CODE);
bill.setBankName("宁波银行深圳后海支行"); guangzhou.setStorageKey("templates/receivable/ar_bill_template_gz.xlsx");
bill.setBankAccountName("其他公司"); ArExportTemplate jiyun = template(4L, ArBillWorkbookWriters.JIYUN_TEMPLATE_CODE);
assertThrows(ServiceException.class, () -> writers.validatePayee(bill, fixed)); jiyun.setStorageKey("templates/receivable/ar_bill_template_jy.xlsx");
bill.setBankAccountName(null); when(templates.selectEnabledById(3L, "AR_BILL", bill.getBillDate())).thenReturn(guangzhou);
assertThrows(ServiceException.class, () -> writers.validatePayee(bill, fixed)); when(templates.selectEnabledById(4L, "AR_BILL", bill.getBillDate())).thenReturn(jiyun);
assertEquals(3L, service.exportBill(7L, 3L, "admin", "gz-export").getTemplateId());
assertEquals(4L, service.exportBill(7L, 4L, "admin", "jy-export").getTemplateId());
verify(exports, times(2)).insert(any());
} }
@Test @Test
@@ -1,8 +1,5 @@
package com.bms.receivable.export; package com.bms.receivable.export;
import java.io.IOException;
import com.bms.common.exception.ServiceException;
import com.bms.receivable.domain.ArBill;
import com.bms.receivable.domain.ArExportTemplate; import com.bms.receivable.domain.ArExportTemplate;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@@ -28,16 +25,4 @@ public class ArBillWorkbookWriters {
return SUMMARY_TEMPLATE_CODE.equals(template.getTemplateCode()); return SUMMARY_TEMPLATE_CODE.equals(template.getTemplateCode());
} }
/** 固定收款信息的模板必须与账单快照一致,避免导出错误的收款账户。 */
public void validatePayee(ArBill bill, ArExportTemplate template) {
if (!GUANGZHOU_TEMPLATE_CODE.equals(template.getTemplateCode())
&& !JIYUN_TEMPLATE_CODE.equals(template.getTemplateCode())) return;
try {
if (!defaultWriter.matchesTemplatePayee(bill, template)) {
throw new ServiceException("所选模板的固定收款信息与账单收款账户不一致,请选择匹配的模板");
}
} catch (IOException e) {
throw new ServiceException("无法读取所选模板的收款信息,请检查模板配置");
}
}
} }
@@ -19,10 +19,8 @@ import java.util.zip.ZipOutputStream;
import org.apache.poi.ss.usermodel.BuiltinFormats; import org.apache.poi.ss.usermodel.BuiltinFormats;
import org.apache.poi.ss.usermodel.FillPatternType; import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.xssf.model.StylesTable; import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import com.bms.common.utils.StringUtils; import com.bms.common.utils.StringUtils;
import com.bms.receivable.domain.ArBill; import com.bms.receivable.domain.ArBill;
@@ -73,28 +71,6 @@ public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport {
int formula, int failedFormula) { int formula, int failedFormula) {
} }
/** 首表 C7:C9 是固定开户名、账号和开户行;空值不得通过匹配。 */
public boolean matchesTemplatePayee(ArBill bill, ArExportTemplate template) throws IOException {
try (InputStream input = openTemplateStream(template); XSSFWorkbook workbook = new XSSFWorkbook(input)) {
if (workbook.getNumberOfSheets() == 0) return false;
var sheet = workbook.getSheetAt(0);
DataFormatter formatter = new DataFormatter();
String[] expected = {bill.getBankAccountName(), bill.getBankAccountNo(), bill.getBankName()};
for (int i = 0; i < expected.length; i++) {
var row = sheet.getRow(6 + i);
var cell = row == null ? null : row.getCell(2);
String actual = cell == null ? "" : formatter.formatCellValue(cell);
String snapshot = normalizePayeeValue(expected[i]);
if (snapshot.isEmpty() || !snapshot.equals(normalizePayeeValue(actual))) return false;
}
return true;
}
}
private String normalizePayeeValue(String value) {
return value == null ? "" : value.replaceAll("[\\s\\p{Z}]+", "");
}
@Override @Override
public void write(ArBill bill, ArExportTemplate template, Path target) throws IOException { public void write(ArBill bill, ArExportTemplate template, Path target) throws IOException {
// 样式表单独读取;不加载整张工作簿,且不依赖 styles.xml 和 sheet1.xml 在 ZIP 中的先后顺序。 // 样式表单独读取;不加载整张工作簿,且不依赖 styles.xml 和 sheet1.xml 在 ZIP 中的先后顺序。
@@ -1051,7 +1051,6 @@ public class ArBillServiceImpl implements IArBillService {
bill.getStatus(), templateId == null ? "未配置启用中的应收账单默认导出模板" bill.getStatus(), templateId == null ? "未配置启用中的应收账单默认导出模板"
: "所选应收账单模板不存在、已停用或不在生效期内,请重新选择", operator, requestId); : "所选应收账单模板不存在、已停用或不在生效期内,请重新选择", operator, requestId);
} }
workbookWriters.validatePayee(bill, template);
ArBillExport processing = exportMapper.selectProcessingByBillAndTemplate(arBillId, template.getTemplateId()); ArBillExport processing = exportMapper.selectProcessingByBillAndTemplate(arBillId, template.getTemplateId());
if (processing != null) { if (processing != null) {
log.info("应收账单导出任务已存在,operator={}, requestId={}, arBillId={}, exportId={}, status={}, costMs={}", log.info("应收账单导出任务已存在,operator={}, requestId={}, arBillId={}, exportId={}, status={}, costMs={}",
@@ -1129,7 +1128,6 @@ public class ArBillServiceImpl implements IArBillService {
String objectKey = "ar/bill/" + DateUtils.datePath() + "/" + IdUtils.fastSimpleUUID() + ".xlsx"; String objectKey = "ar/bill/" + DateUtils.datePath() + "/" + IdUtils.fastSimpleUUID() + ".xlsx";
temp = Files.createTempFile("ar-bill-export-", ".xlsx"); temp = Files.createTempFile("ar-bill-export-", ".xlsx");
ArExportTemplate template = resolveExportTemplate(export, bill); ArExportTemplate template = resolveExportTemplate(export, bill);
workbookWriters.validatePayee(bill, template);
workbookWriters.select(template).write(bill, template, temp); workbookWriters.select(template).write(bill, template, temp);
export.setFileSize(Files.size(temp)); export.setFileSize(Files.size(temp));
export.setFileHash(sha256(temp)); export.setFileHash(sha256(temp));