fix(ecom): 修正全店/品类/商品各维度利润计算,推广费与刷单费应计入税前扣减

- 税前盈亏计算公式统一为:订单毛利 − 推广费 − 刷单费(之前漏减了推广费和刷单费)
- ShopOverviewStatisticsServiceImpl 各层聚合(店铺总览/品类对比/商品对比/合计行)均同步修正
- CompanyOverviewServiceImpl 部分方法同步修正口径
This commit is contained in:
小蟑螂
2026-05-06 00:43:53 +08:00
parent 65739ecfac
commit 6e8b0d9e18
2 changed files with 63 additions and 32 deletions
@@ -196,6 +196,10 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
.reduce(BigDecimal.ZERO, BigDecimal::add);
overviewVO.setTotalPromotionFee(totalPromotionFee);
BigDecimal totalBrushFee = brushOrders.stream()
.map(b -> b.getBrushAmount() != null ? b.getBrushAmount() : BigDecimal.ZERO)
.reduce(BigDecimal.ZERO, BigDecimal::add);
// 5. 计算平均费比
BigDecimal averageFeeRatio = BigDecimal.ZERO;
if (totalSalesAmount.compareTo(BigDecimal.ZERO) > 0) {
@@ -203,8 +207,10 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
}
overviewVO.setAverageFeeRatio(averageFeeRatio);
// 6. 计算全店总净利(税后)和总税额(利润口径与单店一致;税额按销售额比例)
BigDecimal totalProfitBeforeTax = calculateTotalProfit(orders, stat);
// 6. 计算全店总净利(税后)和总税额:税前盈亏 = 订单维度毛利 − 推广费 − 刷单费;税额按销售额比例
BigDecimal totalProfitBeforeTax = calculateTotalProfit(orders, stat)
.subtract(totalPromotionFee)
.subtract(totalBrushFee);
BigDecimal totalTaxAmount = totalSalesAmount.multiply(taxRate);
BigDecimal totalNetProfitAfterTax = totalProfitBeforeTax.subtract(totalTaxAmount);
overviewVO.setTotalNetProfitAfterTax(totalNetProfitAfterTax);
@@ -321,7 +327,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
}
/**
* 计算总利润(税前):与单店概览一致,按订单状态各维度计入销售额与各项成本
* 订单维度毛利(未扣推广费/刷单费);全公司税前盈亏在入口再减总推广费与总刷单费
*/
private BigDecimal calculateTotalProfit(List<OrderPO> orders, OrderStatusStatSets stat) {
return orders.stream()
@@ -398,8 +404,11 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
BigDecimal shopPromotionFee = shopPromotionFees.stream()
.map(fee -> fee.getFeeAmount() != null ? fee.getFeeAmount() : BigDecimal.ZERO)
.reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal shopBrushTotal = shopBrushOrders.stream()
.map(brush -> brush.getBrushAmount() != null ? brush.getBrushAmount() : BigDecimal.ZERO)
.reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal roi = calculateSalesROI(shopSales.subtract(shopRefund), shopPromotionFee);
BigDecimal profit = calculateShopProfit(shopOrders, stat);
BigDecimal profit = calculateShopProfit(shopOrders, stat, shopPromotionFee, shopBrushTotal);
BigDecimal netProfit = calculateShopNetProfit(profit, shopSales);
allROIs.add(roi);
@@ -428,8 +437,11 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
BigDecimal shopPromotionFee = shopPromotionFees.stream()
.map(fee -> fee.getFeeAmount() != null ? fee.getFeeAmount() : BigDecimal.ZERO)
.reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal shopBrushTotal = shopBrushOrders.stream()
.map(brush -> brush.getBrushAmount() != null ? brush.getBrushAmount() : BigDecimal.ZERO)
.reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal roi = calculateSalesROI(shopSales.subtract(shopRefund), shopPromotionFee);
BigDecimal profit = calculateShopProfit(shopOrders, stat);
BigDecimal profit = calculateShopProfit(shopOrders, stat, shopPromotionFee, shopBrushTotal);
BigDecimal netProfit = calculateShopNetProfit(profit, shopSales);
int quadrant;
@@ -530,7 +542,8 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
.reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal orderLineCost = productCost.add(shippingCost).add(otherCost);
BigDecimal profitBeforeTax = salesAmount.subtract(orderLineCost).subtract(refundAmount);
BigDecimal profitBeforeTax = salesAmount.subtract(orderLineCost).subtract(refundAmount)
.subtract(totalPromotionFee).subtract(brushFee);
// 销售额4%税额
BigDecimal salesTaxAmount = salesAmount.multiply(taxRate);
@@ -648,7 +661,8 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
? totalSales.divide(new BigDecimal(totalOrders), 2, RoundingMode.HALF_UP)
: BigDecimal.ZERO;
BigDecimal totalCost = totalProductCost.add(totalShippingCost).add(totalOtherCost);
BigDecimal profitBeforeTax = totalSales.subtract(totalCost).subtract(totalRefund);
BigDecimal profitBeforeTax = totalSales.subtract(totalCost).subtract(totalRefund)
.subtract(totalPromoFee).subtract(totalBrushFee);
BigDecimal salesTax = totalSales.multiply(taxRate);
BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTax);
BigDecimal roi = totalPromoFee.compareTo(BigDecimal.ZERO) > 0
@@ -691,13 +705,16 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
}
/**
* 店铺维度税前利润(与品类/单店口径一致:计入销售额的实收 − 各成本维度 − 计入退款的实收)
* 店铺维度税前利润:订单统计口径毛利 − 推广费 − 刷单费
*/
private BigDecimal calculateShopProfit(List<OrderPO> orders, OrderStatusStatSets stat) {
private BigDecimal calculateShopProfit(List<OrderPO> orders, OrderStatusStatSets stat,
BigDecimal promotionFee, BigDecimal brushFee) {
BigDecimal salesAmount = orders.stream().map(stat::receivedForSales).reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal orderLineCost = orders.stream().map(stat::totalCostForStats).reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal refundAmount = orders.stream().map(stat::receivedForRefund).reduce(BigDecimal.ZERO, BigDecimal::add);
return salesAmount.subtract(orderLineCost).subtract(refundAmount);
BigDecimal p = promotionFee != null ? promotionFee : BigDecimal.ZERO;
BigDecimal b = brushFee != null ? brushFee : BigDecimal.ZERO;
return salesAmount.subtract(orderLineCost).subtract(refundAmount).subtract(p).subtract(b);
}
/**
@@ -863,7 +880,8 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
}
BigDecimal totalCost = productCost.add(shippingCost).add(otherCost);
BigDecimal profitBeforeTax = salesAmount.subtract(totalCost).subtract(refundAmount);
BigDecimal profitBeforeTax = salesAmount.subtract(totalCost).subtract(refundAmount)
.subtract(totalPromotionFee).subtract(totalBrushFee);
BigDecimal salesTaxAmount = salesAmount.multiply(taxRate);
BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTaxAmount);
@@ -945,7 +963,8 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
? totalSales.divide(new BigDecimal(totalOrders), 2, RoundingMode.HALF_UP)
: BigDecimal.ZERO;
BigDecimal totalCost = totalProductCost.add(totalShippingCost).add(totalOtherCost);
BigDecimal profitBeforeTax = totalSales.subtract(totalCost).subtract(totalRefund);
BigDecimal profitBeforeTax = totalSales.subtract(totalCost).subtract(totalRefund)
.subtract(totalPromoFee).subtract(totalBrushFee);
BigDecimal salesTax = totalSales.multiply(taxRate);
BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTax);
BigDecimal roi = totalPromoFee.compareTo(BigDecimal.ZERO) > 0
@@ -1118,7 +1137,8 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
}
BigDecimal totalCost = totalProductCost.add(totalShippingCost).add(totalOtherCost);
BigDecimal profitBeforeTax = totalSalesAmount.subtract(totalCost).subtract(totalRefundAmount);
BigDecimal profitBeforeTax = totalSalesAmount.subtract(totalCost).subtract(totalRefundAmount)
.subtract(totalPromotionFee).subtract(totalBrushFee);
BigDecimal salesTaxAmount = totalSalesAmount.multiply(taxRate);
BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTaxAmount);
@@ -1231,7 +1251,8 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
? totalSalesAmount.divide(new BigDecimal(totalOrderCount), 2, RoundingMode.HALF_UP)
: BigDecimal.ZERO;
BigDecimal totalCost = totalProductCost.add(totalShippingCost).add(totalOtherCost);
BigDecimal profitBeforeTax = totalSalesAmount.subtract(totalCost).subtract(totalRefundAmount);
BigDecimal profitBeforeTax = totalSalesAmount.subtract(totalCost).subtract(totalRefundAmount)
.subtract(totalPromotionFee).subtract(totalBrushFee);
BigDecimal salesTax = totalSalesAmount.multiply(this.taxRate);
BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTax);
BigDecimal roi = totalPromotionFee.compareTo(BigDecimal.ZERO) > 0
@@ -1353,7 +1374,8 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
? totalSalesAmount.divide(new BigDecimal(totalOrderCount), 2, RoundingMode.HALF_UP)
: BigDecimal.ZERO;
BigDecimal totalCost = totalProductCost.add(totalShippingCost).add(totalOtherCost);
BigDecimal profitBeforeTax = totalSalesAmount.subtract(totalCost).subtract(totalRefundAmount);
BigDecimal profitBeforeTax = totalSalesAmount.subtract(totalCost).subtract(totalRefundAmount)
.subtract(totalPromotionFee).subtract(totalBrushFee);
BigDecimal salesTaxAmount = totalSalesAmount.multiply(taxRate);
BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTaxAmount);
BigDecimal roi = totalPromotionFee.compareTo(BigDecimal.ZERO) > 0
@@ -211,6 +211,10 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
.reduce(BigDecimal.ZERO, BigDecimal::add);
overviewVO.setTotalPromotionFee(totalPromotionFee);
BigDecimal totalBrushFee = brushOrders.stream()
.map(b -> b.getBrushAmount() != null ? b.getBrushAmount() : BigDecimal.ZERO)
.reduce(BigDecimal.ZERO, BigDecimal::add);
// 5. 计算平均费比
BigDecimal averageFeeRatio = BigDecimal.ZERO;
if (totalSalesAmount.compareTo(BigDecimal.ZERO) > 0) {
@@ -218,8 +222,10 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
}
overviewVO.setAverageFeeRatio(averageFeeRatio);
// 6. 计算店铺总净利(税后)和总税额
BigDecimal totalProfitBeforeTax = calculateTotalProfit(orders, stat);
// 6. 计算店铺总净利(税后)和总税额:税前盈亏 = 订单维度毛利 − 推广费 − 刷单费
BigDecimal totalProfitBeforeTax = calculateTotalProfit(orders, stat)
.subtract(totalPromotionFee)
.subtract(totalBrushFee);
overviewVO.setTotalProfitBeforeTax(totalProfitBeforeTax);
BigDecimal totalSalesTaxAmount = totalSalesAmount.multiply(taxRate);
BigDecimal totalNetProfitAfterTax = totalProfitBeforeTax.subtract(totalSalesTaxAmount);
@@ -254,7 +260,7 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
}
/**
* 计算总利润(税前)
* 订单维度毛利(税前、未扣推广费/刷单费),全店税前盈亏需再减推广费与刷单费
*/
private BigDecimal calculateTotalProfit(List<OrderPO> orders, OrderStatusStatSets stat) {
return orders.stream()
@@ -454,7 +460,8 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
.reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal totalCost = calculateProductTotalCost(productOrders, stat);
BigDecimal profitBeforeTax = salesAmount.subtract(totalCost).subtract(refundAmount);
BigDecimal profitBeforeTax = salesAmount.subtract(totalCost).subtract(refundAmount)
.subtract(productPromotionFee).subtract(brushFee);
// 销售额4%税额
BigDecimal salesTaxAmount = salesAmount.multiply(taxRate);
@@ -537,7 +544,8 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
? totalSales.divide(new BigDecimal(totalOrders), 2, RoundingMode.HALF_UP)
: BigDecimal.ZERO;
BigDecimal totalCost = totalProductCost.add(totalShippingCost).add(totalOtherCost);
BigDecimal profitBeforeTax = totalSales.subtract(totalCost).subtract(totalRefund);
BigDecimal profitBeforeTax = totalSales.subtract(totalCost).subtract(totalRefund)
.subtract(totalPromoFee).subtract(totalBrushFee);
BigDecimal salesTax = totalSales.multiply(taxRate);
BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTax);
BigDecimal roi = totalPromoFee.compareTo(BigDecimal.ZERO) > 0
@@ -704,6 +712,9 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
BigDecimal totalSalesAmount = orders.stream()
.map(stat::receivedForSales)
.reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal totalBrushFeeAll = brushOrders.stream()
.map(b -> b.getBrushAmount() != null ? b.getBrushAmount() : BigDecimal.ZERO)
.reduce(BigDecimal.ZERO, BigDecimal::add);
// 按品类聚合
List<ShopOverviewStatisticsVO.CategoryComparison> results = new ArrayList<>();
@@ -728,6 +739,12 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
salesAmount.divide(totalSalesAmount, 4, RoundingMode.HALF_UP));
}
BigDecimal catBrushFee = BigDecimal.ZERO;
if (totalSalesAmount.compareTo(BigDecimal.ZERO) > 0) {
catBrushFee = totalBrushFeeAll.multiply(
salesAmount.divide(totalSalesAmount, 4, RoundingMode.HALF_UP));
}
BigDecimal productCost = catOrders.stream()
.map(stat::productCostForStats)
.reduce(BigDecimal.ZERO, BigDecimal::add);
@@ -741,7 +758,8 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
.reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal totalCost = productCost.add(shippingCost).add(otherCost);
BigDecimal profitBeforeTax = salesAmount.subtract(totalCost).subtract(refundAmount);
BigDecimal profitBeforeTax = salesAmount.subtract(totalCost).subtract(refundAmount)
.subtract(catPromotionFee).subtract(catBrushFee);
BigDecimal salesTaxAmount = salesAmount.multiply(taxRate);
BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTaxAmount);
@@ -756,16 +774,6 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
? salesAmount.divide(new BigDecimal(orderCount), 2, RoundingMode.HALF_UP)
: BigDecimal.ZERO;
// 刷单费(无法按品类分,全量均摊)
BigDecimal totalBrushFee = brushOrders.stream()
.map(b -> b.getBrushAmount() != null ? b.getBrushAmount() : BigDecimal.ZERO)
.reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal catBrushFee = BigDecimal.ZERO;
if (totalSalesAmount.compareTo(BigDecimal.ZERO) > 0) {
catBrushFee = totalBrushFee.multiply(
salesAmount.divide(totalSalesAmount, 4, RoundingMode.HALF_UP));
}
ShopOverviewStatisticsVO.CategoryComparison cc = new ShopOverviewStatisticsVO.CategoryComparison();
cc.setCategoryId(categoryId);
cc.setCategoryName(categoryId == 0L ? "未分类" : categoryMap.get(categoryId).getName());
@@ -827,7 +835,8 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
? totalSales.divide(new BigDecimal(totalOrders), 2, RoundingMode.HALF_UP)
: BigDecimal.ZERO;
BigDecimal totalCost = totalProductCost.add(totalShippingCost).add(totalOtherCost);
BigDecimal profitBeforeTax = totalSales.subtract(totalCost).subtract(totalRefund);
BigDecimal profitBeforeTax = totalSales.subtract(totalCost).subtract(totalRefund)
.subtract(totalPromoFee).subtract(totalBrushFee);
BigDecimal salesTax = totalSales.multiply(taxRate);
BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTax);
BigDecimal roi = totalPromoFee.compareTo(BigDecimal.ZERO) > 0