fix(distribution): 修复分销结账退款分成计算逻辑与利润统计,优化分销订单表格列宽与展示体验

This commit is contained in:
yml2213
2026-09-09 19:34:04 +08:00
parent 19b18324a2
commit b16c45ce6f
12 changed files with 403 additions and 88 deletions
@@ -399,3 +399,76 @@ func TestSnapshotPaidOrderTxSplitsPercentOfProfit(t *testing.T) {
t.Fatalf("platform receive = %+v", platform)
}
}
func TestApplyRefundTxWithCheckoutAndDeposit(t *testing.T) {
db := distributionTestDB(t)
if err := db.AutoMigrate(&model.OrderCheckout{}); err != nil {
t.Fatal(err)
}
now := time.Now()
site := model.DistributionSite{SiteCode: "checkout_site", SiteName: "结账测试站", Status: "active"}
if err := db.Create(&site).Error; err != nil {
t.Fatal(err)
}
rule := model.DistributionRuleVersion{
SiteID: &site.ID, Mode: ModeTwoPartyDistributorReceive, Version: 1, Status: "active",
DistributorReceiveRatio: 35, PlatformSellRatio: 40,
ServiceFeeValue: 25, ServiceFeeUnit: ServiceFeeUnitPercent, PaymentFeeBPS: 50,
EffectiveFrom: now.Add(-time.Hour),
}
if err := db.Create(&rule).Error; err != nil {
t.Fatal(err)
}
order := model.RentalOrder{
OrderNo: "ORD-CHECKOUT-REFUND", ListingID: 10, AccountID: 20, OwnerID: 30, RenterID: 40, Status: "renting",
RentAmountCent: 63130, DepositAmountCent: 15500, OwnerRentAmountCent: 55950,
DistributionSiteID: &site.ID, DistributionRuleVersionID: &rule.ID, DistributionMode: ModeTwoPartyDistributorReceive,
}
if err := db.Create(&order).Error; err != nil {
t.Fatal(err)
}
if err := SnapshotPaidOrderTx(db, order.ID, 63130); err != nil {
t.Fatal(err)
}
checkout := model.OrderCheckout{
OrderID: order.ID,
Status: "accepted",
RentAmountCent: 53330,
OwnerRentAmountCent: 46150,
DepositAmountCent: 15500,
RenterRefundAmountCent: 25300,
OwnerIncomeAmountCent: 46150,
}
if err := db.Create(&checkout).Error; err != nil {
t.Fatal(err)
}
// 退款 25300(包含押金全退 15500 + 租金退款 9800)
if err := ApplyRefundTx(db, order.ID, 25300); err != nil {
t.Fatalf("ApplyRefundTx failed: %v", err)
}
var financial model.DistributionOrderFinancial
if err := db.Where("order_id = ?", order.ID).First(&financial).Error; err != nil {
t.Fatal(err)
}
if financial.RefundAmountCent != 9800 {
t.Fatalf("financial.RefundAmountCent = %d, want 9800 (only rent refund)", financial.RefundAmountCent)
}
if financial.NetPaidAmountCent != 53330 {
t.Fatalf("financial.NetPaidAmountCent = %d, want 53330", financial.NetPaidAmountCent)
}
if financial.SplitBaseAmountCent != 6913 {
t.Fatalf("financial.SplitBaseAmountCent = %d, want 6913", financial.SplitBaseAmountCent)
}
var distributorParty model.DistributionOrderParty
if err := db.Where("order_id = ? AND party_role = 'distributor_receive'", order.ID).First(&distributorParty).Error; err != nil {
t.Fatal(err)
}
if distributorParty.IncomeAmountCent != 2420 {
t.Fatalf("distributorParty.IncomeAmountCent = %d, want 2420", distributorParty.IncomeAmountCent)
}
}