104 lines
3.5 KiB
Go
104 lines
3.5 KiB
Go
package distribution
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"hfb_jysh/backend/internal/model"
|
|
)
|
|
|
|
// TestApplyRefundTxUsesAgreedCheckoutActualRent 覆盖 ApplyRefundTx 的结账单分支:
|
|
// 只有已达成一致的结账单(accepted / settled)才按其实际消费租金与号主实得重算,
|
|
// 仲裁结案的 settled 属于同一口径;未达成一致的 submitted 结账单不得被采用。
|
|
func TestApplyRefundTxUsesAgreedCheckoutActualRent(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
checkoutStatus string
|
|
wantRentRefund int64
|
|
}{
|
|
{name: "accepted 结账单按实际租金重算", checkoutStatus: checkoutStatusAccepted, wantRentRefund: 2000},
|
|
{name: "settled 结账单(仲裁结案)同口径", checkoutStatus: checkoutStatusSettled, wantRentRefund: 2000},
|
|
{name: "submitted 结账单不被采用", checkoutStatus: "submitted", wantRentRefund: 1000},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
financial := snapshotAndRefundWithCheckout(t, tc.checkoutStatus)
|
|
if financial.RefundAmountCent != tc.wantRentRefund {
|
|
t.Fatalf("refund_amount_cent = %d, want %d", financial.RefundAmountCent, tc.wantRentRefund)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// snapshotAndRefundWithCheckout 构造一笔分销订单(租金 10000、号主底价 6000、无押金)、
|
|
// 挂一张租金 8000 / 号主实得 5000 的结账单,快照后按 1000 退款,返回重算后的财务快照。
|
|
func snapshotAndRefundWithCheckout(t *testing.T, checkoutStatus string) model.DistributionOrderFinancial {
|
|
t.Helper()
|
|
db := distributionTestDB(t)
|
|
|
|
now := time.Now()
|
|
site := model.DistributionSite{
|
|
SiteCode: "site_refund_" + checkoutStatus,
|
|
SiteName: "退款口径测试站",
|
|
Domain: "refund-" + checkoutStatus + ".dist.com",
|
|
Status: "active",
|
|
DisplayAccountEnabled: true,
|
|
}
|
|
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-REFUND-" + checkoutStatus,
|
|
ListingID: 9101,
|
|
AccountID: 9102,
|
|
OwnerID: 11,
|
|
RenterID: 12,
|
|
Status: "renting",
|
|
RentAmountCent: 10000,
|
|
OwnerRentAmountCent: 6000,
|
|
PlatformFeeCent: 4000,
|
|
DepositAmountCent: 0,
|
|
DistributionSiteID: &site.ID,
|
|
DistributionRuleVersionID: &rule.ID,
|
|
}
|
|
if err := db.Create(&order).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
checkout := model.OrderCheckout{
|
|
OrderID: order.ID,
|
|
InitiatedBy: 12,
|
|
Status: checkoutStatus,
|
|
RentAmountCent: 8000,
|
|
OwnerRentAmountCent: 5000,
|
|
}
|
|
if err := db.Create(&checkout).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := SnapshotPaidOrderTx(db, order.ID, 10000); err != nil {
|
|
t.Fatalf("SnapshotPaidOrderTx failed: %v", err)
|
|
}
|
|
if err := ApplyRefundTx(db, order.ID, 1000); 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)
|
|
}
|
|
return financial
|
|
}
|