diff --git a/bms-back/bms-system/src/main/java/com/bms/receivable/export/ArBillWorkbookWriters.java b/bms-back/bms-system/src/main/java/com/bms/receivable/export/ArBillWorkbookWriters.java index 091ed6c4..74c8afe3 100644 --- a/bms-back/bms-system/src/main/java/com/bms/receivable/export/ArBillWorkbookWriters.java +++ b/bms-back/bms-system/src/main/java/com/bms/receivable/export/ArBillWorkbookWriters.java @@ -7,6 +7,7 @@ import org.springframework.stereotype.Component; @Component public class ArBillWorkbookWriters { public static final String SUMMARY_TEMPLATE_CODE = "AR_BILL_SUMMARY"; + public static final String GUANGZHOU_TEMPLATE_CODE = "AR_BILL_GZ"; private final DefaultArBillWorkbookWriter defaultWriter; private final SummaryArBillWorkbookWriter summaryWriter; diff --git a/bms-back/bms-system/src/main/java/com/bms/receivable/export/DefaultArBillWorkbookWriter.java b/bms-back/bms-system/src/main/java/com/bms/receivable/export/DefaultArBillWorkbookWriter.java index b0d3f916..9c323736 100644 --- a/bms-back/bms-system/src/main/java/com/bms/receivable/export/DefaultArBillWorkbookWriter.java +++ b/bms-back/bms-system/src/main/java/com/bms/receivable/export/DefaultArBillWorkbookWriter.java @@ -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; @@ -68,7 +68,7 @@ public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport { private static final Pattern MERGE_REF_PATTERN = Pattern.compile( "(]*\\bref=\")([A-Z]+)(\\d+)(?::([A-Z]+)(\\d+))?(\")"); - private record ExportStyleIndexes(int failedDetail, int detailNo, int failedDetailNo, + private record ExportStyleIndexes(int detail, int failedDetail, int detailNo, int failedDetailNo, int customerOrderNo, int failedCustomerOrderNo, int trackingNo, int failedTrackingNo, int generalNumber, int failedGeneralNumber, @@ -79,18 +79,24 @@ 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; + if (detailStyleIndex >= styles.getNumCellStyles()) { + throw new IOException("应收账单导出模板明细样式不存在: " + detailStyleIndex); + } String drawingPartPath = readTemplateDrawingPartPath(template); XSSFCellStyle detailNoStyle = styles.createCellStyle(); - detailNoStyle.cloneStyleFrom(styles.getStyleAt(TEMPLATE_DETAIL_STYLE_INDEX)); + detailNoStyle.cloneStyleFrom(styles.getStyleAt(detailStyleIndex)); detailNoStyle.setDataFormat(BuiltinFormats.getBuiltinFormat("0")); int detailNoStyleIndex = Short.toUnsignedInt(detailNoStyle.getIndex()); XSSFCellStyle failedDetailStyle = styles.createCellStyle(); - failedDetailStyle.cloneStyleFrom(styles.getStyleAt(TEMPLATE_DETAIL_STYLE_INDEX)); + failedDetailStyle.cloneStyleFrom(styles.getStyleAt(detailStyleIndex)); failedDetailStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex()); failedDetailStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); int failedDetailStyleIndex = Short.toUnsignedInt(failedDetailStyle.getIndex()); XSSFCellStyle customerOrderNoStyle = styles.createCellStyle(); - customerOrderNoStyle.cloneStyleFrom(styles.getStyleAt(TEMPLATE_DETAIL_STYLE_INDEX)); + customerOrderNoStyle.cloneStyleFrom(styles.getStyleAt(detailStyleIndex)); customerOrderNoStyle.setDataFormat(BuiltinFormats.getBuiltinFormat("@")); int customerOrderNoStyleIndex = Short.toUnsignedInt(customerOrderNoStyle.getIndex()); XSSFCellStyle failedCustomerOrderNoStyle = styles.createCellStyle(); @@ -98,7 +104,7 @@ public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport { failedCustomerOrderNoStyle.setDataFormat(BuiltinFormats.getBuiltinFormat("@")); int failedCustomerOrderNoStyleIndex = Short.toUnsignedInt(failedCustomerOrderNoStyle.getIndex()); XSSFCellStyle trackingNoStyle = styles.createCellStyle(); - trackingNoStyle.cloneStyleFrom(styles.getStyleAt(TEMPLATE_DETAIL_STYLE_INDEX)); + trackingNoStyle.cloneStyleFrom(styles.getStyleAt(detailStyleIndex)); trackingNoStyle.setDataFormat(BuiltinFormats.getBuiltinFormat("@")); int trackingNoStyleIndex = Short.toUnsignedInt(trackingNoStyle.getIndex()); XSSFCellStyle failedTrackingNoStyle = styles.createCellStyle(); @@ -106,7 +112,7 @@ public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport { failedTrackingNoStyle.setDataFormat(BuiltinFormats.getBuiltinFormat("@")); int failedTrackingNoStyleIndex = Short.toUnsignedInt(failedTrackingNoStyle.getIndex()); XSSFCellStyle generalNumberStyle = styles.createCellStyle(); - generalNumberStyle.cloneStyleFrom(styles.getStyleAt(TEMPLATE_DETAIL_STYLE_INDEX)); + generalNumberStyle.cloneStyleFrom(styles.getStyleAt(detailStyleIndex)); generalNumberStyle.setDataFormat(BuiltinFormats.getBuiltinFormat("General")); int generalNumberStyleIndex = Short.toUnsignedInt(generalNumberStyle.getIndex()); XSSFCellStyle failedGeneralNumberStyle = styles.createCellStyle(); @@ -118,7 +124,7 @@ public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport { failedDetailNoStyle.setDataFormat(BuiltinFormats.getBuiltinFormat("0")); int failedDetailNoStyleIndex = Short.toUnsignedInt(failedDetailNoStyle.getIndex()); XSSFCellStyle formulaStyle = styles.createCellStyle(); - formulaStyle.cloneStyleFrom(styles.getStyleAt(TEMPLATE_DETAIL_STYLE_INDEX)); + formulaStyle.cloneStyleFrom(styles.getStyleAt(detailStyleIndex)); formulaStyle.setShrinkToFit(true); int formulaStyleIndex = Short.toUnsignedInt(formulaStyle.getIndex()); XSSFCellStyle failedFormulaStyle = styles.createCellStyle(); @@ -126,7 +132,7 @@ public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport { failedFormulaStyle.setShrinkToFit(true); int failedFormulaStyleIndex = Short.toUnsignedInt(failedFormulaStyle.getIndex()); ExportStyleIndexes exportStyleIndexes = new ExportStyleIndexes( - failedDetailStyleIndex, detailNoStyleIndex, failedDetailNoStyleIndex, + detailStyleIndex, failedDetailStyleIndex, detailNoStyleIndex, failedDetailNoStyleIndex, customerOrderNoStyleIndex, failedCustomerOrderNoStyleIndex, trackingNoStyleIndex, failedTrackingNoStyleIndex, generalNumberStyleIndex, failedGeneralNumberStyleIndex, @@ -151,7 +157,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); + int rowOffset = writeOoxmlSheet(readUtf8(templateZip), bill, output, exportStyleIndexes, guangzhouTemplate); output.closeEntry(); replaced = true; drawingRowOffset = Integer.valueOf(rowOffset); @@ -178,6 +184,29 @@ public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport { } } + private int readGuangzhouDetailStyleIndex(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())) { + String row = extractTemplateRows(readUtf8(zip)).get(6); + if (row != null) { + Matcher cells = CELL_PATTERN.matcher(row); + while (cells.find()) { + Matcher reference = CELL_REF_PATTERN.matcher(cells.group(1)); + Matcher style = CELL_STYLE_PATTERN.matcher(cells.group(1)); + if (reference.find() && "S6".equalsIgnoreCase(reference.group(1)) && style.find()) { + return Integer.parseInt(style.group(1)); + } + } + } + throw new IOException("广州应收账单模板缺少 S6 合计单元格样式"); + } + zip.closeEntry(); + } + } + throw new IOException("广州应收账单模板缺少运费工作表"); + } + private String readTemplateDrawingPartPath(ArExportTemplate template) throws IOException { try (InputStream input = openTemplateStream(template); ZipInputStream templateZip = new ZipInputStream(input)) { for (ZipEntry entry; (entry = templateZip.getNextEntry()) != null; ) { @@ -236,11 +265,7 @@ public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport { try (InputStream input = openTemplateStream(template); ZipInputStream templateZip = new ZipInputStream(input)) { for (ZipEntry entry; (entry = templateZip.getNextEntry()) != null; ) { if (TEMPLATE_STYLES_XML.equals(entry.getName())) { - StylesTable styles = new StylesTable(templateZip); - if (styles.getNumCellStyles() <= TEMPLATE_DETAIL_STYLE_INDEX) { - throw new IOException("应收账单导出模板缺少明细样式: " + TEMPLATE_DETAIL_STYLE_INDEX); - } - return styles; + return new StylesTable(templateZip); } templateZip.closeEntry(); } @@ -249,7 +274,7 @@ public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport { } private int writeOoxmlSheet(String templateXml, ArBill bill, OutputStream output, - ExportStyleIndexes styleIndexes) throws IOException { + ExportStyleIndexes styleIndexes, boolean guangzhouTemplate) throws IOException { int sheetDataStart = templateXml.indexOf("', sheetDataStart); int sheetDataEnd = templateXml.indexOf("", sheetDataOpenEnd); @@ -263,7 +288,7 @@ public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport { removeSheetDimension(templateXml.substring(0, sheetDataOpenEnd + 1))); writeUtf8(output, prefix); - String headerRow = replaceCellXml(templateRows.get(1), "A1", + String headerRow = guangzhouTemplate ? templateRows.get(1) : replaceCellXml(templateRows.get(1), "A1", StringUtils.isEmpty(bill.getBankAccountName()) ? "威狮国际供应链(深圳)有限公司" : bill.getBankAccountName(), false); writeUtf8(output, headerRow); writeUtf8(output, templateRows.get(2)); @@ -291,7 +316,7 @@ public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport { int rowOffset = lastDetailRow - TEMPLATE_LAST_DETAIL_ROW + EXPORT_FOOTER_GAP_ROWS; writeUtf8(output, ""); - writeOoxmlFooter(output, templateRows, rowOffset, bill, styleIndexes.generalNumber()); + writeOoxmlFooter(output, templateRows, rowOffset, bill, styleIndexes.generalNumber(), guangzhouTemplate); String suffix = shiftFooterMerges(templateXml.substring(sheetDataEnd), rowOffset); writeUtf8(output, suffix); return rowOffset; @@ -394,7 +419,7 @@ public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport { } private void writeOoxmlFooter(OutputStream output, Map templateRows, int rowOffset, - ArBill bill, int generalNumberStyleIndex) throws IOException { + ArBill bill, int generalNumberStyleIndex, boolean guangzhouTemplate) 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; @@ -407,20 +432,26 @@ public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport { nvl(bill.getTotalAmount()), generalNumberStyleIndex); } else if (offset == 1) { row = replaceCellXml(row, "B" + targetRowNumber, "开户名", false); - row = replaceCellXml(row, "C" + targetRowNumber, bill.getBankAccountName(), false); + if (!guangzhouTemplate) { + row = replaceCellXml(row, "C" + targetRowNumber, bill.getBankAccountName(), false); + } row = replaceCellXml(row, "P" + targetRowNumber, "本期已核销金额", false); row = replaceCellXml(row, "S" + targetRowNumber, bill.getWrittenOffAmount(), true, generalNumberStyleIndex); } else if (offset == 2) { row = replaceCellXml(row, "B" + targetRowNumber, "账号", false); - row = replaceCellXml(row, "C" + targetRowNumber, exportBankAccountNo(bill.getBankAccountNo()), false); + if (!guangzhouTemplate) { + row = replaceCellXml(row, "C" + targetRowNumber, exportBankAccountNo(bill.getBankAccountNo()), false); + } row = replaceCellXml(row, "P" + targetRowNumber, "本期未核销金额", false); String unwrittenAmountFormula = "S" + (targetRowNumber - 2) + "-S" + (targetRowNumber - 1); row = replaceFormulaCellXml(row, "S" + targetRowNumber, unwrittenAmountFormula, nvl(bill.getUnwrittenAmount()), generalNumberStyleIndex); } else { row = replaceCellXml(row, "B" + targetRowNumber, "开户行", false); - row = replaceCellXml(row, "C" + targetRowNumber, bill.getBankName(), false); + if (!guangzhouTemplate) { + row = replaceCellXml(row, "C" + targetRowNumber, bill.getBankName(), false); + } } writeUtf8(output, row); } @@ -430,7 +461,7 @@ public class DefaultArBillWorkbookWriter extends ArBillWorkbookSupport { ExportStyleIndexes styleIndexes) throws IOException { String failureDescription = exportBillingFailDescription(detail.getBillingFailStatus()); boolean failedDetail = !failureDescription.isEmpty(); - int styleIndex = failedDetail ? styleIndexes.failedDetail() : TEMPLATE_DETAIL_STYLE_INDEX; + int styleIndex = failedDetail ? styleIndexes.failedDetail() : styleIndexes.detail(); int sequenceStyleIndex = failedDetail ? styleIndexes.failedDetailNo() : styleIndexes.detailNo(); Object[] values = { detail.getDetailNo(), diff --git a/bms-back/bms-system/src/main/resources/templates/receivable/ar_bill_template_gz.xlsx b/bms-back/bms-system/src/main/resources/templates/receivable/ar_bill_template_gz.xlsx index b8a665d4..415b0f68 100644 Binary files a/bms-back/bms-system/src/main/resources/templates/receivable/ar_bill_template_gz.xlsx and b/bms-back/bms-system/src/main/resources/templates/receivable/ar_bill_template_gz.xlsx differ