✨ 支持集运应收账单模板导出并保留固定收款信息

This commit is contained in:
caojia
2026-11-19 23:19:54 +08:00
parent d64f343ecd
commit 77e407482a
5 changed files with 115 additions and 17 deletions
@@ -8,6 +8,7 @@ import org.springframework.stereotype.Component;
public class ArBillWorkbookWriters {
public static final String SUMMARY_TEMPLATE_CODE = "AR_BILL_SUMMARY";
public static final String GUANGZHOU_TEMPLATE_CODE = "AR_BILL_GZ";
public static final String JIYUN_TEMPLATE_CODE = "AR_BILL_JY";
private final DefaultArBillWorkbookWriter defaultWriter;
private final SummaryArBillWorkbookWriter summaryWriter;
@@ -31,7 +31,7 @@ import com.bms.receivable.domain.ArBillDetail;
import com.bms.receivable.domain.ArBillDetailFee;
import com.bms.receivable.domain.ArExportTemplate;
/** 默认及广州应收账单模板共用的 21 列明细和银行页脚排版。 */
/** 默认、广州及集运应收账单模板共用的 21 列明细和银行页脚排版。 */
@Component
public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport {
private static final int TEMPLATE_DATA_START_ROW = 5;
@@ -79,9 +79,10 @@ public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport {
public void write(ArBill bill, ArExportTemplate template, Path target) throws IOException {
// 样式表单独读取;不加载整张工作簿,且不依赖 styles.xml 和 sheet1.xml 在 ZIP 中的先后顺序。
StylesTable styles = readExportStyles(template);
boolean guangzhouTemplate = ArBillWorkbookWriters.GUANGZHOU_TEMPLATE_CODE.equals(template.getTemplateCode());
// 广州模板的样式编号与默认模板不同,使用其明细合计单元格的完整边框样式。
int detailStyleIndex = guangzhouTemplate ? readGuangzhouDetailStyleIndex(template) : TEMPLATE_DETAIL_STYLE_INDEX;
boolean preserveTemplatePayee = ArBillWorkbookWriters.GUANGZHOU_TEMPLATE_CODE.equals(template.getTemplateCode())
|| ArBillWorkbookWriters.JIYUN_TEMPLATE_CODE.equals(template.getTemplateCode());
// 固定收款信息的模板使用自身合计单元格样式,不依赖默认模板的样式编号。
int detailStyleIndex = preserveTemplatePayee ? readTemplateDetailStyleIndex(template) : TEMPLATE_DETAIL_STYLE_INDEX;
if (detailStyleIndex >= styles.getNumCellStyles()) {
throw new IOException("应收账单导出模板明细样式不存在: " + detailStyleIndex);
}
@@ -157,7 +158,7 @@ public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport {
}
if (TEMPLATE_SHEET_XML.equals(entry.getName())) {
output.putNextEntry(new ZipEntry(entryName));
int rowOffset = writeOoxmlSheet(readUtf8(templateZip), bill, output, exportStyleIndexes, guangzhouTemplate);
int rowOffset = writeOoxmlSheet(readUtf8(templateZip), bill, output, exportStyleIndexes, preserveTemplatePayee);
output.closeEntry();
replaced = true;
drawingRowOffset = Integer.valueOf(rowOffset);
@@ -184,7 +185,7 @@ public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport {
}
}
private int readGuangzhouDetailStyleIndex(ArExportTemplate template) throws IOException {
private int readTemplateDetailStyleIndex(ArExportTemplate template) throws IOException {
try (InputStream input = openTemplateStream(template); ZipInputStream zip = new ZipInputStream(input)) {
for (ZipEntry entry; (entry = zip.getNextEntry()) != null; ) {
if (TEMPLATE_SHEET_XML.equals(entry.getName())) {
@@ -199,12 +200,12 @@ public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport {
}
}
}
throw new IOException("广州应收账单模板缺少 S6 合计单元格样式");
throw new IOException("应收账单模板缺少 S6 合计单元格样式: " + template.getTemplateCode());
}
zip.closeEntry();
}
}
throw new IOException("广州应收账单模板缺少运费工作表");
throw new IOException("应收账单模板缺少运费工作表: " + template.getTemplateCode());
}
private String readTemplateDrawingPartPath(ArExportTemplate template) throws IOException {
@@ -274,7 +275,7 @@ public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport {
}
private int writeOoxmlSheet(String templateXml, ArBill bill, OutputStream output,
ExportStyleIndexes styleIndexes, boolean guangzhouTemplate) throws IOException {
ExportStyleIndexes styleIndexes, boolean preserveTemplatePayee) throws IOException {
int sheetDataStart = templateXml.indexOf("<sheetData");
int sheetDataOpenEnd = templateXml.indexOf('>', sheetDataStart);
int sheetDataEnd = templateXml.indexOf("</sheetData>", sheetDataOpenEnd);
@@ -288,7 +289,7 @@ public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport {
removeSheetDimension(templateXml.substring(0, sheetDataOpenEnd + 1)));
writeUtf8(output, prefix);
String headerRow = guangzhouTemplate ? templateRows.get(1) : replaceCellXml(templateRows.get(1), "A1",
String headerRow = preserveTemplatePayee ? templateRows.get(1) : replaceCellXml(templateRows.get(1), "A1",
StringUtils.isEmpty(bill.getBankAccountName()) ? "威狮国际供应链(深圳)有限公司" : bill.getBankAccountName(), false);
writeUtf8(output, headerRow);
writeUtf8(output, templateRows.get(2));
@@ -316,7 +317,7 @@ public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport {
int rowOffset = lastDetailRow - TEMPLATE_LAST_DETAIL_ROW + EXPORT_FOOTER_GAP_ROWS;
writeUtf8(output, "<row r=\"" + (lastDetailRow + 1) + "\" ht=\""
+ EXPORT_DETAIL_ROW_HEIGHT + "\" customHeight=\"true\"/>");
writeOoxmlFooter(output, templateRows, rowOffset, bill, styleIndexes.generalNumber(), guangzhouTemplate);
writeOoxmlFooter(output, templateRows, rowOffset, bill, styleIndexes.generalNumber(), preserveTemplatePayee);
String suffix = shiftFooterMerges(templateXml.substring(sheetDataEnd), rowOffset);
writeUtf8(output, suffix);
return rowOffset;
@@ -419,7 +420,7 @@ public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport {
}
private void writeOoxmlFooter(OutputStream output, Map<Integer, String> templateRows, int rowOffset,
ArBill bill, int generalNumberStyleIndex, boolean guangzhouTemplate) throws IOException {
ArBill bill, int generalNumberStyleIndex, boolean preserveTemplatePayee) throws IOException {
String totalAmountFormula = "SUM(S" + TEMPLATE_DATA_START_ROW + ":INDEX(S:S,ROW()-1))";
for (int offset = 0; offset < 4; offset++) {
int sourceRowNumber = TEMPLATE_LAST_DETAIL_ROW + offset + 1;
@@ -432,7 +433,7 @@ public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport {
nvl(bill.getTotalAmount()), generalNumberStyleIndex);
} else if (offset == 1) {
row = replaceCellXml(row, "B" + targetRowNumber, "开户名", false);
if (!guangzhouTemplate) {
if (!preserveTemplatePayee) {
row = replaceCellXml(row, "C" + targetRowNumber, bill.getBankAccountName(), false);
}
row = replaceCellXml(row, "P" + targetRowNumber, "本期已核销金额", false);
@@ -440,7 +441,7 @@ public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport {
generalNumberStyleIndex);
} else if (offset == 2) {
row = replaceCellXml(row, "B" + targetRowNumber, "账号", false);
if (!guangzhouTemplate) {
if (!preserveTemplatePayee) {
row = replaceCellXml(row, "C" + targetRowNumber, exportBankAccountNo(bill.getBankAccountNo()), false);
}
row = replaceCellXml(row, "P" + targetRowNumber, "本期未核销金额", false);
@@ -449,7 +450,7 @@ public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport {
nvl(bill.getUnwrittenAmount()), generalNumberStyleIndex);
} else {
row = replaceCellXml(row, "B" + targetRowNumber, "开户行", false);
if (!guangzhouTemplate) {
if (!preserveTemplatePayee) {
row = replaceCellXml(row, "C" + targetRowNumber, bill.getBankName(), false);
}
}