From 3d1f9f2a2a3d4e9760f1746c9b2d7d7a7471d274 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E8=9F=91=E8=9E=82?= Date: Fri, 8 May 2026 16:49:02 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=E5=A4=9A=E5=A4=84?= =?UTF-8?q?=E7=BB=86=E8=8A=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 导出功能修复 Token 刷新机制,使用 requestClient 发送请求 - 店铺/商品单元格添加跳转图标 tdesign:jump - 店铺概览 ROI 计算精度统一为两位小数 Co-Authored-By: Claude Opus 4.7 --- .../impl/CategoryExportExcelServiceImpl.java | 28 +++++++-- .../impl/CompanyOverviewServiceImpl.java | 63 +++++++++++++------ .../service/impl/ExportServiceImpl.java | 50 ++++++++++++--- .../ShopOverviewStatisticsServiceImpl.java | 27 +++++--- .../impl/ProductOverviewServiceImpl.java | 25 +++++--- .../service/impl/ShopExportServiceImpl.java | 27 ++++++-- .../src/utils/overview-table-hints.ts | 2 +- .../components/ShopComparisonTable.vue | 6 +- .../components/ProductComparisonTable.vue | 8 ++- 9 files changed, 182 insertions(+), 54 deletions(-) diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/CategoryExportExcelServiceImpl.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/CategoryExportExcelServiceImpl.java index e975f6d6..c70d58f6 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/CategoryExportExcelServiceImpl.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/CategoryExportExcelServiceImpl.java @@ -50,6 +50,17 @@ public class CategoryExportExcelServiceImpl implements CategoryExportExcelServic private static final BigDecimal DEFAULT_TAX_RATE = new BigDecimal("0.04"); private static final DateTimeFormatter DATE_FMT = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + private static BigDecimal salesTaxOnNetForCategoryExport(BigDecimal sales, BigDecimal refund, BigDecimal taxRate) { + BigDecimal s = sales != null ? sales : BigDecimal.ZERO; + BigDecimal rfd = refund != null ? refund : BigDecimal.ZERO; + BigDecimal r = taxRate != null ? taxRate : DEFAULT_TAX_RATE; + BigDecimal base = s.subtract(rfd); + if (base.compareTo(BigDecimal.ZERO) < 0) { + base = BigDecimal.ZERO; + } + return base.multiply(r).setScale(2, RoundingMode.HALF_UP); + } + private final CurrentUserContext currentUserContext; private final OrderMapper orderMapper; private final PromotionFeeMapper promotionFeeMapper; @@ -380,7 +391,7 @@ public class CategoryExportExcelServiceImpl implements CategoryExportExcelServic BigDecimal totalCost = totalProductCost.add(totalShippingCost).add(totalOtherCost); BigDecimal profitBeforeTax = totalSales.subtract(totalCost).subtract(totalRefund) .subtract(totalPromoFee).subtract(totalBrushFee); - BigDecimal salesTax = totalSales.multiply(taxRate); + BigDecimal salesTax = salesTaxOnNetForCategoryExport(totalSales, totalRefund, taxRate); BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTax); BigDecimal roi = totalPromoFee.compareTo(BigDecimal.ZERO) > 0 ? totalSales.subtract(totalRefund).divide(totalPromoFee, 4, RoundingMode.HALF_UP) @@ -449,10 +460,10 @@ public class CategoryExportExcelServiceImpl implements CategoryExportExcelServic int r = 2; for (ShopOverviewStatisticsVO.CategorySummary rowData : rows) { - fillCategoryDataRow(sheet.createRow(r++), rowData, dataStyle); + fillCategoryDataRow(sheet.createRow(r++), rowData, dataStyle, taxRate); } if (totalRow != null) { - fillCategoryDataRow(sheet.createRow(r), totalRow, totalStyle); + fillCategoryDataRow(sheet.createRow(r), totalRow, totalStyle, taxRate); } sheet.setColumnWidth(0, 5200); @@ -461,7 +472,8 @@ public class CategoryExportExcelServiceImpl implements CategoryExportExcelServic } } - private void fillCategoryDataRow(Row row, ShopOverviewStatisticsVO.CategorySummary d, CellStyle style) { + private void fillCategoryDataRow( + Row row, ShopOverviewStatisticsVO.CategorySummary d, CellStyle style, BigDecimal taxRate) { setCell(row, 0, d.getCategoryName() != null ? d.getCategoryName() : "", style); setCell(row, 1, bd2Money(d.getSalesAmount()), style); Cell oc = row.createCell(2); @@ -477,9 +489,13 @@ public class CategoryExportExcelServiceImpl implements CategoryExportExcelServic setCell(row, 10, bd2Money(d.getBrushFee()), style); setCell(row, 11, bd2FeeRatioPercent(d.getFeeRatio()), style); setCell(row, 12, bd2Money(d.getTotalProfitBeforeTax()), style); - setCell(row, 13, bd2Money(d.getSalesTaxAmount()), style); + BigDecimal salesTax = salesTaxOnNetForCategoryExport(d.getSalesAmount(), d.getRefundAmount(), taxRate); + BigDecimal profitAfterTax = (d.getTotalProfitBeforeTax() != null ? d.getTotalProfitBeforeTax() : BigDecimal.ZERO) + .subtract(salesTax) + .setScale(2, RoundingMode.HALF_UP); + setCell(row, 13, bd2Money(salesTax), style); setCell(row, 14, bd2Roi(d.getRoi()), style); - setCell(row, 15, bd2Money(d.getProfitAfterTax()), style); + setCell(row, 15, bd2Money(profitAfterTax), style); } private static double bd2Money(BigDecimal v) { diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/CompanyOverviewServiceImpl.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/CompanyOverviewServiceImpl.java index 4cced44b..4e284f99 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/CompanyOverviewServiceImpl.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/CompanyOverviewServiceImpl.java @@ -85,6 +85,31 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { this.platformMapper = platformMapper; } + /** + * 税额 = max(0, 销售额 − 退款) × 税率,保留两位小数。 + */ + private BigDecimal salesTaxOnNetSales(BigDecimal salesAmount, BigDecimal refundAmount) { + BigDecimal sales = salesAmount != null ? salesAmount : BigDecimal.ZERO; + BigDecimal refund = refundAmount != null ? refundAmount : BigDecimal.ZERO; + BigDecimal base = sales.subtract(refund); + if (base.compareTo(BigDecimal.ZERO) < 0) { + base = BigDecimal.ZERO; + } + BigDecimal rate = taxRate != null ? taxRate : DEFAULT_TAX_RATE; + return base.multiply(rate).setScale(2, RoundingMode.HALF_UP); + } + + private BigDecimal salesTaxOnNetSales(BigDecimal salesAmount, BigDecimal refundAmount, BigDecimal rate) { + BigDecimal sales = salesAmount != null ? salesAmount : BigDecimal.ZERO; + BigDecimal refund = refundAmount != null ? refundAmount : BigDecimal.ZERO; + BigDecimal base = sales.subtract(refund); + if (base.compareTo(BigDecimal.ZERO) < 0) { + base = BigDecimal.ZERO; + } + BigDecimal r = rate != null ? rate : DEFAULT_TAX_RATE; + return base.multiply(r).setScale(2, RoundingMode.HALF_UP); + } + @Override public CompanyOverviewVO getCompanyOverview(CompanyOverviewQueryDTO queryDTO) { // 取请求参数中的税率,默认4% @@ -172,6 +197,10 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { .reduce(BigDecimal.ZERO, BigDecimal::add); overviewVO.setTotalSalesAmount(totalSalesAmount); + BigDecimal totalRefundAmountCompany = orders.stream() + .map(stat::receivedForRefund) + .reduce(BigDecimal.ZERO, BigDecimal::add); + // 2. 计算较上月同期增长百分比 BigDecimal lastMonthSalesAmount = lastMonthOrders.stream() .map(stat::receivedForSales) @@ -207,11 +236,11 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { } overviewVO.setAverageFeeRatio(averageFeeRatio); - // 6. 计算全店总净利(税后)和总税额:税前盈亏 = 订单维度毛利 − 推广费 − 刷单费;税额按销售额比例 + // 6. 计算全店总净利(税后)和总税额:税前盈亏 = 订单维度毛利 − 推广费 − 刷单费;税额按 (销售额 − 退款) × 税率 BigDecimal totalProfitBeforeTax = calculateTotalProfit(orders, stat) .subtract(totalPromotionFee) .subtract(totalBrushFee); - BigDecimal totalTaxAmount = totalSalesAmount.multiply(taxRate); + BigDecimal totalTaxAmount = salesTaxOnNetSales(totalSalesAmount, totalRefundAmountCompany); BigDecimal totalNetProfitAfterTax = totalProfitBeforeTax.subtract(totalTaxAmount).setScale(2, RoundingMode.HALF_UP); overviewVO.setTotalNetProfitAfterTax(totalNetProfitAfterTax); overviewVO.setTotalTaxAmount(totalTaxAmount); @@ -409,7 +438,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { .reduce(BigDecimal.ZERO, BigDecimal::add); BigDecimal roi = calculateSalesROI(shopSales.subtract(shopRefund), shopPromotionFee); BigDecimal profit = calculateShopProfit(shopOrders, stat, shopPromotionFee, shopBrushTotal); - BigDecimal netProfit = calculateShopNetProfit(profit, shopSales); + BigDecimal netProfit = calculateShopNetProfit(profit, shopSales, shopRefund); allROIs.add(roi); allNetProfits.add(netProfit); @@ -442,7 +471,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { .reduce(BigDecimal.ZERO, BigDecimal::add); BigDecimal roi = calculateSalesROI(shopSales.subtract(shopRefund), shopPromotionFee); BigDecimal profit = calculateShopProfit(shopOrders, stat, shopPromotionFee, shopBrushTotal); - BigDecimal netProfit = calculateShopNetProfit(profit, shopSales); + BigDecimal netProfit = calculateShopNetProfit(profit, shopSales, shopRefund); int quadrant; if (roi.compareTo(avgROI) >= 0 && netProfit.compareTo(avgNetProfit) >= 0) { @@ -545,10 +574,9 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { BigDecimal profitBeforeTax = salesAmount.subtract(orderLineCost).subtract(refundAmount) .subtract(totalPromotionFee).subtract(brushFee); - // 销售额4%税额 - BigDecimal salesTaxAmount = salesAmount.multiply(taxRate); - - // 税后盈亏(扣除销售额4%税额) + BigDecimal salesTaxAmount = salesTaxOnNetSales(salesAmount, refundAmount); + + // 税后盈亏(扣除税额) BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTaxAmount); BigDecimal roi = calculateSalesROI(salesAmount.subtract(refundAmount), totalPromotionFee); @@ -663,7 +691,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { BigDecimal totalCost = totalProductCost.add(totalShippingCost).add(totalOtherCost); BigDecimal profitBeforeTax = totalSales.subtract(totalCost).subtract(totalRefund) .subtract(totalPromoFee).subtract(totalBrushFee); - BigDecimal salesTax = totalSales.multiply(taxRate); + BigDecimal salesTax = salesTaxOnNetSales(totalSales, totalRefund); BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTax); BigDecimal roi = totalPromoFee.compareTo(BigDecimal.ZERO) > 0 ? totalSales.subtract(totalRefund).divide(totalPromoFee, 4, RoundingMode.HALF_UP) @@ -728,11 +756,10 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { } /** - * 店铺净利(税后):税额按「计入销售额」的销售额比例,与单店概览一致 + * 店铺净利(税后):税额按 (销售额 − 退款) × 税率,与单店概览一致 */ - private BigDecimal calculateShopNetProfit(BigDecimal profitBeforeTax, BigDecimal salesAmountForTax) { - BigDecimal salesAmount = salesAmountForTax != null ? salesAmountForTax : BigDecimal.ZERO; - return profitBeforeTax.subtract(salesAmount.multiply(taxRate)); + private BigDecimal calculateShopNetProfit(BigDecimal profitBeforeTax, BigDecimal salesAmount, BigDecimal refundAmount) { + return profitBeforeTax.subtract(salesTaxOnNetSales(salesAmount, refundAmount)); } /** @@ -882,7 +909,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { BigDecimal totalCost = productCost.add(shippingCost).add(otherCost); BigDecimal profitBeforeTax = salesAmount.subtract(totalCost).subtract(refundAmount) .subtract(totalPromotionFee).subtract(totalBrushFee); - BigDecimal salesTaxAmount = salesAmount.multiply(taxRate); + BigDecimal salesTaxAmount = salesTaxOnNetSales(salesAmount, refundAmount); BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTaxAmount); BigDecimal roi = BigDecimal.ZERO; @@ -965,7 +992,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { BigDecimal totalCost = totalProductCost.add(totalShippingCost).add(totalOtherCost); BigDecimal profitBeforeTax = totalSales.subtract(totalCost).subtract(totalRefund) .subtract(totalPromoFee).subtract(totalBrushFee); - BigDecimal salesTax = totalSales.multiply(taxRate); + BigDecimal salesTax = salesTaxOnNetSales(totalSales, totalRefund); BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTax); BigDecimal roi = totalPromoFee.compareTo(BigDecimal.ZERO) > 0 ? totalSales.subtract(totalRefund).divide(totalPromoFee, 4, RoundingMode.HALF_UP) @@ -1139,7 +1166,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { BigDecimal totalCost = totalProductCost.add(totalShippingCost).add(totalOtherCost); BigDecimal profitBeforeTax = totalSalesAmount.subtract(totalCost).subtract(totalRefundAmount) .subtract(totalPromotionFee).subtract(totalBrushFee); - BigDecimal salesTaxAmount = totalSalesAmount.multiply(taxRate); + BigDecimal salesTaxAmount = salesTaxOnNetSales(totalSalesAmount, totalRefundAmount); BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTaxAmount); BigDecimal roi = BigDecimal.ZERO; @@ -1253,7 +1280,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { BigDecimal totalCost = totalProductCost.add(totalShippingCost).add(totalOtherCost); BigDecimal profitBeforeTax = totalSalesAmount.subtract(totalCost).subtract(totalRefundAmount) .subtract(totalPromotionFee).subtract(totalBrushFee); - BigDecimal salesTax = totalSalesAmount.multiply(this.taxRate); + BigDecimal salesTax = salesTaxOnNetSales(totalSalesAmount, totalRefundAmount); BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTax); BigDecimal roi = totalPromotionFee.compareTo(BigDecimal.ZERO) > 0 ? totalSalesAmount.subtract(totalRefundAmount).divide(totalPromotionFee, 4, RoundingMode.HALF_UP) @@ -1376,7 +1403,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { BigDecimal totalCost = totalProductCost.add(totalShippingCost).add(totalOtherCost); BigDecimal profitBeforeTax = totalSalesAmount.subtract(totalCost).subtract(totalRefundAmount) .subtract(totalPromotionFee).subtract(totalBrushFee); - BigDecimal salesTaxAmount = totalSalesAmount.multiply(taxRate); + BigDecimal salesTaxAmount = salesTaxOnNetSales(totalSalesAmount, totalRefundAmount, taxRate); BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTaxAmount); BigDecimal roi = totalPromotionFee.compareTo(BigDecimal.ZERO) > 0 ? totalSalesAmount.subtract(totalRefundAmount).divide(totalPromotionFee, 4, RoundingMode.HALF_UP) diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/ExportServiceImpl.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/ExportServiceImpl.java index 856a5d6a..cd01b168 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/ExportServiceImpl.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/ExportServiceImpl.java @@ -178,11 +178,11 @@ public class ExportServiceImpl implements ExportService { int rowIdx = 2; for (Map data : dataList) { Row row = sheet.createRow(rowIdx++); - writeOverviewDetailRow(row, data, dataStyle, firstColumnValue.apply(data)); + writeOverviewDetailRow(row, data, dataStyle, firstColumnValue.apply(data), taxRate); } if (totalRowMap != null) { Row row = sheet.createRow(rowIdx); - writeOverviewDetailRow(row, totalRowMap, boldStyle, firstColumnValue.apply(totalRowMap)); + writeOverviewDetailRow(row, totalRowMap, boldStyle, firstColumnValue.apply(totalRowMap), taxRate); } for (int i = 0; i < headers.length; i++) { @@ -190,7 +190,8 @@ public class ExportServiceImpl implements ExportService { } } - private void writeOverviewDetailRow(Row row, Map data, CellStyle style, String firstColumnText) { + private void writeOverviewDetailRow( + Row row, Map data, CellStyle style, String firstColumnText, BigDecimal taxRate) { setCell(row, 0, firstColumnText, style); setCell(row, 1, objToDouble(data.get("targetOrderCount")), style); setCell(row, 2, objToPct(data.get("orderCountAchievementRate")), style); @@ -208,9 +209,32 @@ public class ExportServiceImpl implements ExportService { setCell(row, 14, objToDouble(data.get("brushFee")), style); setCell(row, 15, objToDouble(data.get("feeRatio")), style); setCell(row, 16, objToDouble(data.get("totalProfitBeforeTax")), style); - setCell(row, 17, objToDouble(data.get("salesTaxAmount")), style); + BigDecimal salesTax = salesTaxOnNetForExport( + mapToBigDecimal(data.get("salesAmount")), + mapToBigDecimal(data.get("refundAmount")), + taxRate); + BigDecimal profitAfterTax = mapToBigDecimal(data.get("totalProfitBeforeTax")) + .subtract(salesTax) + .setScale(2, RoundingMode.HALF_UP); + setCell(row, 17, salesTax.doubleValue(), style); setCell(row, 18, objToDouble(data.get("roi")), style); - setCell(row, 19, objToDouble(data.get("profitAfterTax")), style); + setCell(row, 19, profitAfterTax.doubleValue(), style); + } + + private static BigDecimal mapToBigDecimal(Object val) { + if (val == null) { + return BigDecimal.ZERO; + } + if (val instanceof BigDecimal) { + return (BigDecimal) val; + } + if (val instanceof Integer) { + return BigDecimal.valueOf((Integer) val); + } + if (val instanceof Long) { + return BigDecimal.valueOf((Long) val); + } + return BigDecimal.ZERO; } private Map shopSummaryToRowMap(CompanyOverviewVO.ShopSummary s) { @@ -238,6 +262,18 @@ public class ExportServiceImpl implements ExportService { return data; } + /** 与全店概览一致:税额 = max(0, 销售额 − 退款) × 税率,两位小数。 */ + private static BigDecimal salesTaxOnNetForExport(BigDecimal sales, BigDecimal refund, BigDecimal taxRate) { + BigDecimal s = sales != null ? sales : BigDecimal.ZERO; + BigDecimal rfd = refund != null ? refund : BigDecimal.ZERO; + BigDecimal r = taxRate != null ? taxRate : new BigDecimal("0.04"); + BigDecimal base = s.subtract(rfd); + if (base.compareTo(BigDecimal.ZERO) < 0) { + base = BigDecimal.ZERO; + } + return base.multiply(r).setScale(2, RoundingMode.HALF_UP); + } + private CompanyOverviewVO.ShopSummary aggregateShopSummariesForExport( List shops, BigDecimal taxRate) { BigDecimal totalSales = BigDecimal.ZERO; @@ -270,7 +306,7 @@ public class ExportServiceImpl implements ExportService { BigDecimal totalCost = totalProductCost.add(totalShippingCost).add(totalOtherCost); BigDecimal profitBeforeTax = totalSales.subtract(totalCost).subtract(totalRefund) .subtract(totalPromoFee).subtract(totalBrushFee); - BigDecimal salesTax = totalSales.multiply(taxRate); + BigDecimal salesTax = salesTaxOnNetForExport(totalSales, totalRefund, taxRate); BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTax); BigDecimal roi = totalPromoFee.compareTo(BigDecimal.ZERO) > 0 ? totalSales.subtract(totalRefund).divide(totalPromoFee, 4, RoundingMode.HALF_UP) @@ -368,7 +404,7 @@ public class ExportServiceImpl implements ExportService { BigDecimal totalCost = totalProductCost.add(totalShippingCost).add(totalOtherCost); BigDecimal profitBeforeTax = totalSales.subtract(totalCost).subtract(totalRefund) .subtract(totalPromoFee).subtract(totalBrushFee); - BigDecimal salesTax = totalSales.multiply(taxRate); + BigDecimal salesTax = salesTaxOnNetForExport(totalSales, totalRefund, taxRate); BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTax); BigDecimal roi = totalPromoFee.compareTo(BigDecimal.ZERO) > 0 ? totalSales.subtract(totalRefund).divide(totalPromoFee, 4, RoundingMode.HALF_UP) diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/ShopOverviewStatisticsServiceImpl.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/ShopOverviewStatisticsServiceImpl.java index 0f044542..222228e9 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/ShopOverviewStatisticsServiceImpl.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/ShopOverviewStatisticsServiceImpl.java @@ -83,6 +83,19 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics this.categoryService = categoryService; } + /** + * 税额 = max(0, 销售额 − 退款) × 当前税率,四舍五入保留两位小数(与税后盈亏扣减口径一致)。 + */ + private BigDecimal toSalesTaxAmount(BigDecimal salesAmount, BigDecimal refundAmount) { + BigDecimal sales = salesAmount != null ? salesAmount : BigDecimal.ZERO; + BigDecimal refund = refundAmount != null ? refundAmount : BigDecimal.ZERO; + BigDecimal base = sales.subtract(refund); + if (base.compareTo(BigDecimal.ZERO) < 0) { + base = BigDecimal.ZERO; + } + return base.multiply(taxRate).setScale(2, RoundingMode.HALF_UP); + } + @Override public ShopOverviewStatisticsVO getShopOverviewStatistics(ShopOverviewQueryDTO queryDTO) { // 取请求参数中的税率,默认4% @@ -229,7 +242,7 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics .subtract(totalPromotionFee) .subtract(totalBrushFee); overviewVO.setTotalProfitBeforeTax(totalProfitBeforeTax); - BigDecimal totalSalesTaxAmount = totalSalesAmount.multiply(taxRate); + BigDecimal totalSalesTaxAmount = toSalesTaxAmount(totalSalesAmount, refundAmountAgg); BigDecimal totalNetProfitAfterTax = totalProfitBeforeTax.subtract(totalSalesTaxAmount); overviewVO.setTotalNetProfitAfterTax(totalNetProfitAfterTax.setScale(2, RoundingMode.HALF_UP)); overviewVO.setTotalTaxAmount(totalSalesTaxAmount); @@ -512,10 +525,10 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics BigDecimal profitBeforeTax = salesAmount.subtract(totalCost).subtract(refundAmount) .subtract(productPromotionFee).subtract(brushFee); - // 销售额4%税额 - BigDecimal salesTaxAmount = salesAmount.multiply(taxRate); + // (销售额 − 退款) × 税率,税额保留两位小数 + BigDecimal salesTaxAmount = toSalesTaxAmount(salesAmount, refundAmount); - // 税后盈亏(扣除销售额4%税额) + // 税后盈亏(扣除税额) BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTaxAmount); BigDecimal roi = calculateSalesROI(salesAmount.subtract(refundAmount), productPromotionFee); @@ -595,7 +608,7 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics BigDecimal totalCost = totalProductCost.add(totalShippingCost).add(totalOtherCost); BigDecimal profitBeforeTax = totalSales.subtract(totalCost).subtract(totalRefund) .subtract(totalPromoFee).subtract(totalBrushFee); - BigDecimal salesTax = totalSales.multiply(taxRate); + BigDecimal salesTax = toSalesTaxAmount(totalSales, totalRefund); BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTax); BigDecimal roi = totalPromoFee.compareTo(BigDecimal.ZERO) > 0 ? totalSales.subtract(totalRefund).divide(totalPromoFee, 2, RoundingMode.HALF_UP) @@ -835,7 +848,7 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics BigDecimal totalCost = productCost.add(shippingCost).add(otherCost); BigDecimal profitBeforeTax = salesAmount.subtract(totalCost).subtract(refundAmount) .subtract(catPromotionFee).subtract(catBrushFee); - BigDecimal salesTaxAmount = salesAmount.multiply(taxRate); + BigDecimal salesTaxAmount = toSalesTaxAmount(salesAmount, refundAmount); BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTaxAmount); BigDecimal roi = calculateSalesROI(salesAmount.subtract(refundAmount), catPromotionFee); @@ -912,7 +925,7 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics BigDecimal totalCost = totalProductCost.add(totalShippingCost).add(totalOtherCost); BigDecimal profitBeforeTax = totalSales.subtract(totalCost).subtract(totalRefund) .subtract(totalPromoFee).subtract(totalBrushFee); - BigDecimal salesTax = totalSales.multiply(taxRate); + BigDecimal salesTax = toSalesTaxAmount(totalSales, totalRefund); BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTax); BigDecimal roi = totalPromoFee.compareTo(BigDecimal.ZERO) > 0 ? totalSales.subtract(totalRefund).divide(totalPromoFee, 2, RoundingMode.HALF_UP) diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/product/overview/service/impl/ProductOverviewServiceImpl.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/product/overview/service/impl/ProductOverviewServiceImpl.java index d68da1a2..6c76c6c2 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/product/overview/service/impl/ProductOverviewServiceImpl.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/product/overview/service/impl/ProductOverviewServiceImpl.java @@ -58,6 +58,18 @@ public class ProductOverviewServiceImpl implements ProductOverviewService { this.shopMonthlyTargetMapper = shopMonthlyTargetMapper; } + /** 税额 = max(0, 销售额 − 退款) × 税率,保留两位小数。 */ + private static BigDecimal salesTaxOnNetSales(BigDecimal salesAmount, BigDecimal refundAmount, BigDecimal taxRate) { + BigDecimal sales = salesAmount != null ? salesAmount : BigDecimal.ZERO; + BigDecimal refund = refundAmount != null ? refundAmount : BigDecimal.ZERO; + BigDecimal rate = taxRate != null ? taxRate : new BigDecimal("0.04"); + BigDecimal base = sales.subtract(refund); + if (base.compareTo(BigDecimal.ZERO) < 0) { + base = BigDecimal.ZERO; + } + return base.multiply(rate).setScale(2, RoundingMode.HALF_UP); + } + @Override public ProductOverviewVO getProductOverview(ProductOverviewQueryDTO queryDTO) { Long companyId = currentUserContext.getCurrentCompanyId(); @@ -184,9 +196,7 @@ public class ProductOverviewServiceImpl implements ProductOverviewService { vo.setAverageFeeRatio(avgFeeRatio.multiply(new BigDecimal("100")) .setScale(2, RoundingMode.HALF_UP)); // 转为百分比 - // 税额 = 销售额 × 税率 - BigDecimal totalTaxAmount = totalSalesAmount.multiply(taxRate) - .setScale(2, RoundingMode.HALF_UP); + BigDecimal totalTaxAmount = salesTaxOnNetSales(totalSalesAmount, totalRefundAmount, taxRate); vo.setTotalTaxAmount(totalTaxAmount); // 税后盈亏 @@ -457,9 +467,7 @@ public class ProductOverviewServiceImpl implements ProductOverviewService { .subtract(refundAmount) .subtract(specPromotionFee); - // 税额 - BigDecimal salesTaxAmount = salesAmount.multiply(taxRate) - .setScale(2, RoundingMode.HALF_UP); + BigDecimal salesTaxAmount = salesTaxOnNetSales(salesAmount, refundAmount, taxRate); // ROI = (销售额 - 退款) / 推广费 BigDecimal roi = specPromotionFee.compareTo(BigDecimal.ZERO) > 0 @@ -536,10 +544,11 @@ public class ProductOverviewServiceImpl implements ProductOverviewService { .subtract(total.getPromotionFee()); total.setProfitBeforeTax(totalProfitBT); - BigDecimal totalSalesTax = totalSalesAmt.multiply(taxRate).setScale(2, RoundingMode.HALF_UP); + BigDecimal totalRefundAgg = total.getRefundAmount(); + BigDecimal totalSalesTax = salesTaxOnNetSales(totalSalesAmt, totalRefundAgg, taxRate); total.setSalesTaxAmount(totalSalesTax); - BigDecimal totalRefundForRoi = total.getRefundAmount(); + BigDecimal totalRefundForRoi = totalRefundAgg; BigDecimal totalRoi = totalPromoFee.compareTo(BigDecimal.ZERO) > 0 ? totalSalesAmt.subtract(totalRefundForRoi) .divide(totalPromoFee, 4, RoundingMode.HALF_UP) diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/shop/service/impl/ShopExportServiceImpl.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/shop/service/impl/ShopExportServiceImpl.java index 74afb44d..28b96c7f 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/shop/service/impl/ShopExportServiceImpl.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/shop/service/impl/ShopExportServiceImpl.java @@ -29,6 +29,18 @@ public class ShopExportServiceImpl implements ShopExportService { private static final BigDecimal DEFAULT_TAX_RATE = new BigDecimal("0.04"); private static final DateTimeFormatter DATE_FMT = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + /** 与店铺概览一致:税额 = max(0, 销售额 − 退款) × 税率,两位小数。 */ + private static BigDecimal salesTaxOnNetForShopExport(BigDecimal sales, BigDecimal refund, BigDecimal taxRate) { + BigDecimal s = sales != null ? sales : BigDecimal.ZERO; + BigDecimal rfd = refund != null ? refund : BigDecimal.ZERO; + BigDecimal r = taxRate != null ? taxRate : DEFAULT_TAX_RATE; + BigDecimal base = s.subtract(rfd); + if (base.compareTo(BigDecimal.ZERO) < 0) { + base = BigDecimal.ZERO; + } + return base.multiply(r).setScale(2, RoundingMode.HALF_UP); + } + private final ShopOverviewStatisticsService shopOverviewStatisticsService; private final CategoryExportExcelService categoryExportExcelService; @@ -134,10 +146,10 @@ public class ShopExportServiceImpl implements ShopExportService { int rowIdx = 2; for (ShopOverviewStatisticsVO.ProductSummary p : products) { Row row = sheet.createRow(rowIdx++); - fillProductDataRow(row, p, dataStyle, false); + fillProductDataRow(row, p, dataStyle, false, taxRate); } if (totalRow != null) { - fillProductDataRow(sheet.createRow(rowIdx), totalRow, totalStyle, true); + fillProductDataRow(sheet.createRow(rowIdx), totalRow, totalStyle, true, taxRate); } sheet.setColumnWidth(0, 5200); @@ -150,7 +162,8 @@ public class ShopExportServiceImpl implements ShopExportService { Row row, ShopOverviewStatisticsVO.ProductSummary p, CellStyle style, - boolean isTotalRow) { + boolean isTotalRow, + BigDecimal taxRate) { String nameCol = isTotalRow ? "合计" : (p.getProductName() != null ? p.getProductName() : "") @@ -174,8 +187,12 @@ public class ShopExportServiceImpl implements ShopExportService { setCell(row, 11, feePct, style); setCell(row, 12, nvl(p.getRoi(), BigDecimal.ZERO).setScale(4, RoundingMode.HALF_UP).doubleValue(), style); setCell(row, 13, nvl(p.getTotalProfitBeforeTax(), BigDecimal.ZERO).setScale(2, RoundingMode.HALF_UP).doubleValue(), style); - setCell(row, 14, nvl(p.getSalesTaxAmount(), BigDecimal.ZERO).setScale(2, RoundingMode.HALF_UP).doubleValue(), style); - setCell(row, 15, nvl(p.getProfitAfterTax(), BigDecimal.ZERO).setScale(2, RoundingMode.HALF_UP).doubleValue(), style); + BigDecimal salesTax = salesTaxOnNetForShopExport(p.getSalesAmount(), p.getRefundAmount(), taxRate); + BigDecimal profitAfterTax = nvl(p.getTotalProfitBeforeTax(), BigDecimal.ZERO) + .subtract(salesTax) + .setScale(2, RoundingMode.HALF_UP); + setCell(row, 14, salesTax.doubleValue(), style); + setCell(row, 15, profitAfterTax.doubleValue(), style); } private void createTrendSheetFromVO(Sheet sheet, String dateRangeLabel, ShopOverviewStatisticsVO vo) { diff --git a/ecom-dmb-frontend/apps/web-antd/src/utils/overview-table-hints.ts b/ecom-dmb-frontend/apps/web-antd/src/utils/overview-table-hints.ts index 3abe571a..a9058a2a 100644 --- a/ecom-dmb-frontend/apps/web-antd/src/utils/overview-table-hints.ts +++ b/ecom-dmb-frontend/apps/web-antd/src/utils/overview-table-hints.ts @@ -51,5 +51,5 @@ export const storeOverviewHints = overviewTableHints; export function salesTaxColumnHint(taxRate: number): string { const pct = (taxRate * 100).toFixed(0); - return `税额 = 销售额 × ${pct}%(与当前页面所选税率一致,由后端按该税率计算)。`; + return `税额 = (销售额 - 退款金额) × ${pct}%(与当前页面所选税率一致,由后端按该税率计算)。`; } diff --git a/ecom-dmb-frontend/apps/web-antd/src/views/dashboard/store-overview/components/ShopComparisonTable.vue b/ecom-dmb-frontend/apps/web-antd/src/views/dashboard/store-overview/components/ShopComparisonTable.vue index 7dc75891..ac31256f 100644 --- a/ecom-dmb-frontend/apps/web-antd/src/views/dashboard/store-overview/components/ShopComparisonTable.vue +++ b/ecom-dmb-frontend/apps/web-antd/src/views/dashboard/store-overview/components/ShopComparisonTable.vue @@ -1,6 +1,7 @@