🐛 修正应收账单汇总导出并新增广州模板

This commit is contained in:
caojia
2026-11-19 16:39:05 +08:00
parent 837633be3c
commit daf36e39e1
5 changed files with 118 additions and 63 deletions
@@ -40,10 +40,11 @@ import org.springframework.stereotype.Component;
@Component
public class SummaryArBillWorkbookWriter extends ArBillWorkbookSupport {
private static final int DETAIL_START_ROW = 5;
private static final int DETAIL_COLUMN_COUNT = 20;
private static final int DETAIL_COLUMN_COUNT = 21;
private static final int SUMMARY_START_ROW = 4;
private static final String SUMMARY_REMARK = "国际货物运输代理服务费";
private static final String REMOTE_FEE_TYPE = "REMOTE";
private static final String SURCHARGE_FEE_TYPE = "SURCHARGE";
private static final String ROMANIA_FEE_TYPE = "REGISTRATION";
private static final String ROMANIA_FEE_NAME = "罗马尼亚费用";
private static final Pattern MERGE = Pattern.compile("(<mergeCell\\b[^>]*\\bref=\")([A-Z]+)(\\d+):([A-Z]+)(\\d+)(\")");
@@ -114,9 +115,15 @@ public class SummaryArBillWorkbookWriter extends ArBillWorkbookSupport {
requireRow(workbook.getSheetAt(0), 5);
requireRow(workbook.getSheetAt(1), 4);
requireRow(workbook.getSheetAt(1), 5);
XSSFCell explanationHeader = requireRow(workbook.getSheetAt(1), 3).getCell(DETAIL_COLUMN_COUNT - 1);
if (explanationHeader == null || !"说明".equals(explanationHeader.toString().trim())) {
throw new IOException("汇总模板运费明细缺少 T 列说明,请使用最新模板");
requireDetailHeader(workbook.getSheetAt(1), 14, "附加费");
requireDetailHeader(workbook.getSheetAt(1), 19, "备注");
requireDetailHeader(workbook.getSheetAt(1), 20, "说明");
}
private void requireDetailHeader(XSSFSheet sheet, int column, String label) throws IOException {
XSSFCell header = requireRow(sheet, 3).getCell(column);
if (header == null || !label.equals(header.toString().trim())) {
throw new IOException("汇总模板运费明细缺少 " + (char) ('A' + column) + " 列" + label + ",请使用最新模板");
}
}
@@ -139,34 +146,28 @@ public class SummaryArBillWorkbookWriter extends ArBillWorkbookSupport {
textCell(summary, 11, 1, exportBankAccountNo(bill.getBankAccountNo()));
textCell(summary, 12, 1, bill.getBankName());
textCell(detail, 2, 1, bill.getCustomerName());
textCell(detail, 2, 17, billPeriod(bill));
for (int column : new int[]{11, 12, 13, 14, 15}) {
textCell(detail, 2, 18, billPeriod(bill));
for (int column : new int[]{11, 12, 13, 15, 16}) {
XSSFCell header = detail.getRow(3).getCell(column);
header.setCellValue(header.getStringCellValue().replace("CNY", currency));
}
for (int column = 0; column < DETAIL_COLUMN_COUNT; column++) {
XSSFCell cell = detail.getRow(4).getCell(column, XSSFRow.MissingCellPolicy.CREATE_NULL_AS_BLANK);
boolean identifier = column == 0 || column == 2 || column == 3 || column == 6;
boolean money = column == 10 || column == 13 || column == 14 || column == 15 || column == 16;
if (!identifier && !money) continue;
XSSFCellStyle style = workbook.createCellStyle();
style.cloneStyleFrom(cell.getCellStyle());
boolean identifier = column == 0 || column == 2 || column == 3 || column == 6;
String format = identifier ? "@"
: (column == 10 || column == 13 || column == 14 || column == 15 ? "0.00" : "General");
String format = identifier ? "@" : "0.00";
style.setDataFormat(workbook.createDataFormat().getFormat(format));
cell.setCellStyle(style);
}
XSSFRow summaryRow = requireRow(summary, 3);
for (int column = 0; column < 7; column++) {
XSSFCell cell = summaryRow.getCell(column, XSSFRow.MissingCellPolicy.CREATE_NULL_AS_BLANK);
XSSFCellStyle style = workbook.createCellStyle();
style.cloneStyleFrom(cell.getCellStyle());
style.setDataFormat(workbook.createDataFormat().getFormat(column == 4 ? "0.00" : "General"));
cell.setCellStyle(style);
}
detail.getRow(4).setHeightInPoints(20);
summaryRow.setHeightInPoints(20);
// 订单号按文本输出并提供可读列宽;其他版式沿用用户模板。
detail.setColumnWidth(2, Math.max(detail.getColumnWidth(2), 20 * 256));
detail.setColumnWidth(3, Math.max(detail.getColumnWidth(3), 20 * 256));
XSSFCell summaryAmount = requireRow(summary, 3).getCell(4, XSSFRow.MissingCellPolicy.CREATE_NULL_AS_BLANK);
XSSFCellStyle summaryAmountStyle = workbook.createCellStyle();
summaryAmountStyle.cloneStyleFrom(summaryAmount.getCellStyle());
summaryAmountStyle.setDataFormat(workbook.createDataFormat().getFormat("0.00"));
summaryAmount.setCellStyle(summaryAmountStyle);
// 行高、列宽、字体和配色沿用用户模板。
workbook.setForceFormulaRecalculation(true);
}
@@ -189,7 +190,7 @@ public class SummaryArBillWorkbookWriter extends ArBillWorkbookSupport {
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
failed[column] = Short.toUnsignedInt(style.getIndex());
}
return new DetailStyles(normal, failed);
return new DetailStyles(normal, failed, workbook.getSheetAt(1).getRow(4).getHeightInPoints());
}
private DetailResult writeDetails(SheetXml sheet, ArBill bill, OutputStream output,
@@ -210,8 +211,9 @@ public class SummaryArBillWorkbookWriter extends ArBillWorkbookSupport {
if (!StringUtils.isEmpty(detail.getCurrency()) && !detail.getCurrency().equals(bill.getCurrency())) {
throw new IOException("账单明细币种与账单币种不一致,请检查账单");
}
writeUtf8(output, detailRow(rowNumber++, bill, detail, styles));
BigDecimal amount = nvl(detail.getTotalAmount());
DetailAmounts amounts = calculateDetailAmounts(detail);
writeUtf8(output, detailRow(rowNumber++, bill, detail, amounts, styles));
BigDecimal amount = amounts.total();
total = total.add(amount);
String key = StringUtils.isEmpty(detail.getProductCode())
? "NAME:" + detail.getProductName() : "CODE:" + detail.getProductCode();
@@ -226,13 +228,14 @@ public class SummaryArBillWorkbookWriter extends ArBillWorkbookSupport {
if (count == 0) writeUtf8(output, "<row r=\"5\"/>");
writeUtf8(output, "<row r=\"" + (lastRow + 1) + "\"/>");
String footer = shiftRowXml(sheet.rows().get(6), lastRow + 2);
footer = replaceFormulaCellXml(footer, "P" + (lastRow + 2), "SUM(P5:P" + lastRow + ")", total);
footer = replaceFormulaCellXml(footer, "Q" + (lastRow + 2), "SUM(Q5:Q" + lastRow + ")", total);
writeUtf8(output, footer);
writeUtf8(output, withFilter(sheet.suffix(), "A4:T" + lastRow));
writeUtf8(output, withFilter(sheet.suffix(), "A4:U" + lastRow));
return new DetailResult(count, lastRow, total);
}
private String detailRow(int rowNumber, ArBill bill, ArBillDetail detail, DetailStyles styles) {
private String detailRow(int rowNumber, ArBill bill, ArBillDetail detail,
DetailAmounts amounts, DetailStyles styles) {
String failure = exportBillingFailDescription(detail.getBillingFailStatus());
Object[] values = {
bill.getCustomerCode(),
@@ -240,23 +243,27 @@ public class SummaryArBillWorkbookWriter extends ArBillWorkbookSupport {
detail.getTransferNo(), detail.getTrackingNo(),
StringUtils.isEmpty(detail.getProductName()) ? detail.getProductCode() : detail.getProductName(),
StringUtils.isEmpty(detail.getCountryName()) ? detail.getCountryCode() : detail.getCountryName(),
detail.getDestPostcode(), null, detail.getReceiveWeight(), detail.getChargeWeight(),
exportFeeAmount(detail, REMOTE_FEE_TYPE, null),
exportUnitPrice(detail), exportProcessingFee(detail), null,
exportFeeAmount(detail, ROMANIA_FEE_TYPE, ROMANIA_FEE_NAME), detail.getTotalAmount(), null, null,
exportRemark(detail), failure
detail.getDestPostcode(), null, detail.getReceiveWeight(), nvl(detail.getChargeWeight()),
amounts.remote(), nvl(exportUnitPrice(detail)), amounts.processing(), amounts.freight(),
amounts.surcharge(), amounts.romania(), amounts.total(), null, null,
null, failure // 客户版汇总模板的 T 列备注固定留空,不输出内部备注或品名描述。
};
StringBuilder row = new StringBuilder(2048).append("<row r=\"").append(rowNumber)
.append("\" ht=\"20\" customHeight=\"true\" spans=\"1:").append(DETAIL_COLUMN_COUNT).append("\">");
.append("\" ht=\"").append(numberText(styles.rowHeight()))
.append("\" customHeight=\"true\" spans=\"1:").append(DETAIL_COLUMN_COUNT).append("\">");
for (int index = 0; index < values.length; index++) {
String reference = (char) ('A' + index) + String.valueOf(rowNumber);
int style = failure.isEmpty() ? styles.normal()[index] : styles.failed()[index];
if (index == 13) {
BigDecimal amount = nvl(detail.getChargeWeight()).multiply(nvl(exportUnitPrice(detail)))
.setScale(2, RoundingMode.HALF_UP);
String formula = switch (index) {
case 13 -> "ROUND(J" + rowNumber + "*L" + rowNumber + ",2)";
case 16 -> "N" + rowNumber + "+K" + rowNumber + "+M" + rowNumber
+ "+P" + rowNumber + "+O" + rowNumber;
default -> null;
};
if (formula != null) {
row.append("<c r=\"").append(reference).append("\" s=\"").append(style)
.append("\" t=\"n\"><f>ROUND(J").append(rowNumber).append("*L").append(rowNumber)
.append(",2)</f><v>").append(numberText(amount)).append("</v></c>");
.append("\" t=\"n\"><f>").append(xmlEscape(formula)).append("</f><v>")
.append(numberText((Number) values[index])).append("</v></c>");
} else {
appendOoxmlCell(row, reference, values[index], style);
}
@@ -264,6 +271,26 @@ public class SummaryArBillWorkbookWriter extends ArBillWorkbookSupport {
return row.append("</row>").toString();
}
/** 与工作表 N、Q 列的公式使用相同数值,缓存和产品汇总均复用本次计算结果。 */
private DetailAmounts calculateDetailAmounts(ArBillDetail detail) throws IOException {
BigDecimal freight = nvl(detail.getChargeWeight()).multiply(nvl(exportUnitPrice(detail)))
.setScale(2, RoundingMode.HALF_UP);
BigDecimal processing = nvl(exportProcessingFee(detail));
BigDecimal remote = exportFeeAmount(detail, REMOTE_FEE_TYPE, null);
BigDecimal surcharge = exportFeeAmount(detail, SURCHARGE_FEE_TYPE, null);
BigDecimal romania = exportFeeAmount(detail, ROMANIA_FEE_TYPE, ROMANIA_FEE_NAME);
BigDecimal total = freight.add(remote).add(processing).add(romania).add(surcharge);
BigDecimal snapshot = nvl(detail.getTotalAmount());
if (total.compareTo(snapshot) != 0) {
String order = StringUtils.isEmpty(detail.getOrderNo()) ? detail.getTransferNo() : detail.getOrderNo();
if (StringUtils.isEmpty(order)) order = "明细" + detail.getDetailNo();
throw new IOException("订单 " + order + " 总金额与账单快照不一致:公式金额 " + numberText(total)
+ ",账单金额 " + numberText(snapshot) + ",差额 " + numberText(total.subtract(snapshot))
+ ",请检查其他费用、退费或计费快照");
}
return new DetailAmounts(freight, processing, remote, surcharge, romania, total);
}
/** 从当前账单的费用快照汇总应收金额;无匹配项输出零,负金额照常输出。 */
private BigDecimal exportFeeAmount(ArBillDetail detail, String feeType, String feeName) {
if (detail.getFees() == null) return BigDecimal.ZERO;
@@ -363,7 +390,9 @@ public class SummaryArBillWorkbookWriter extends ArBillWorkbookSupport {
private record SheetXml(String prefix, Map<Integer, String> rows, String suffix) { }
private record DetailResult(int count, int lastRow, BigDecimal amount) { }
private record DetailStyles(int[] normal, int[] failed) { }
private record DetailStyles(int[] normal, int[] failed, float rowHeight) { }
private record DetailAmounts(BigDecimal freight, BigDecimal processing, BigDecimal remote,
BigDecimal surcharge, BigDecimal romania, BigDecimal total) { }
private static class ProductSummary {
private final String name;