✨ 支持生成账单时选择收款账户

This commit is contained in:
caojia
2026-11-20 11:31:27 +08:00
parent dc6160db10
commit 2729f3f7c8
10 changed files with 253 additions and 15 deletions
@@ -40,6 +40,8 @@ public class ArBillGenerateRequest
@JsonDeserialize(using = StrictDateDeserializer.class)
private Date billDate;
private String currency = "CNY";
/** 收款账户配置 ID;旧调用方未传时沿用币种默认账户。 */
private Long bankAccountId;
/** 订单号、运单号或转单号原始输入,生成时会拆分并再次校验筛选条件。 */
private String orderOrTrackingNo;
private String bankAccountName;
@@ -1,5 +1,8 @@
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 org.springframework.stereotype.Component;
@@ -24,4 +27,17 @@ public class ArBillWorkbookWriters {
public boolean isSummary(ArExportTemplate template) {
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,8 +19,10 @@ import java.util.zip.ZipOutputStream;
import org.apache.poi.ss.usermodel.BuiltinFormats;
import org.apache.poi.ss.usermodel.FillPatternType;
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.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Component;
import com.bms.common.utils.StringUtils;
import com.bms.receivable.domain.ArBill;
@@ -71,6 +73,28 @@ public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport {
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
public void write(ArBill bill, ArExportTemplate template, Path target) throws IOException {
// 样式表单独读取;不加载整张工作簿,且不依赖 styles.xml 和 sheet1.xml 在 ZIP 中的先后顺序。
@@ -42,6 +42,8 @@ public interface IArBillService
List<ArBankAccount> selectEnabledBankAccounts(String currency);
List<ArBankAccount> selectMaskedBankAccounts(String currency);
List<PrcOrderBill> previewBill(ArBillGenerateQuery query);
List<PrcOrderBill> previewBill(ArBillGenerateQuery query, ArBillRowScope scope);
@@ -261,6 +261,13 @@ public class ArBillServiceImpl implements IArBillService {
return accounts;
}
@Override
public List<ArBankAccount> selectMaskedBankAccounts(String currency) {
List<ArBankAccount> accounts = selectEnabledBankAccounts(currency);
accounts.forEach(account -> account.setAccountNo(maskAccount(account.getAccountNo())));
return accounts;
}
/**
* 分页预览可生成订单;多个订单号、运单号或转单号按 OR 条件精确匹配。
*/
@@ -383,10 +390,16 @@ public class ArBillServiceImpl implements IArBillService {
if (generatingBill != null) {
throw new ServiceException("该客户当前账期已有账单正在生成,请稍后再试");
}
ArBankAccount bankAccount = bankAccountMapper.selectDefaultByCurrency(currency);
ArBankAccount bankAccount = request.getBankAccountId() == null
? bankAccountMapper.selectDefaultByCurrency(currency)
: bankAccountMapper.selectEnabledById(request.getBankAccountId());
if (bankAccount == null) {
throw failure(null, ArLogObjectType.BILL, "", ArLogOperationType.GENERATE, null,
"未配置 " + currency + " 币种的公司默认收款账户,请先完成收款账户配置", operator, requestId);
request.getBankAccountId() == null ? "未配置 " + currency + " 币种的公司默认收款账户,请先完成收款账户配置"
: "所选收款账户不存在或已停用,请重新选择", operator, requestId);
}
if (!currency.equalsIgnoreCase(bankAccount.getCurrency())) {
throw new ServiceException("所选收款账户币种与账单币种不一致,请重新选择");
}
Date now = new Date();
@@ -1038,6 +1051,7 @@ public class ArBillServiceImpl implements IArBillService {
bill.getStatus(), templateId == null ? "未配置启用中的应收账单默认导出模板"
: "所选应收账单模板不存在、已停用或不在生效期内,请重新选择", operator, requestId);
}
workbookWriters.validatePayee(bill, template);
ArBillExport processing = exportMapper.selectProcessingByBillAndTemplate(arBillId, template.getTemplateId());
if (processing != null) {
log.info("应收账单导出任务已存在,operator={}, requestId={}, arBillId={}, exportId={}, status={}, costMs={}",
@@ -1115,6 +1129,7 @@ public class ArBillServiceImpl implements IArBillService {
String objectKey = "ar/bill/" + DateUtils.datePath() + "/" + IdUtils.fastSimpleUUID() + ".xlsx";
temp = Files.createTempFile("ar-bill-export-", ".xlsx");
ArExportTemplate template = resolveExportTemplate(export, bill);
workbookWriters.validatePayee(bill, template);
workbookWriters.select(template).write(bill, template, temp);
export.setFileSize(Files.size(temp));
export.setFileHash(sha256(temp));
@@ -1359,7 +1374,8 @@ public class ArBillServiceImpl implements IArBillService {
private String maskAccount(String value) {
if (StringUtils.isEmpty(value)) return "";
return value.length() <= 4 ? "****" : "****" + value.substring(value.length() - 4);
String normalized = value.replaceAll("[\\s\\p{Z}]+", "");
return normalized.length() <= 4 ? "****" : "****" + normalized.substring(normalized.length() - 4);
}
private String sha256(Path file) throws IOException, NoSuchAlgorithmException {