Fix refund_status filter to use refund total > 0; add test
This commit is contained in:
@@ -112,6 +112,7 @@ class DashboardController extends Controller
|
||||
return $q->count();
|
||||
})(),
|
||||
// 退款数据不一致(口径与平台订单列表 refund_inconsistent=1 一致)
|
||||
// 注意:仅当退款总额 > 0 时才参与不一致判定(避免 refund_summary.total_amount=0 的“空字段”噪音被计入)。
|
||||
'platform_orders_refund_inconsistent' => (function () {
|
||||
$tol = (float) config('saasshop.amounts.tolerance', 0.01);
|
||||
$tolCents = (int) round($tol * 100);
|
||||
@@ -123,6 +124,9 @@ class DashboardController extends Controller
|
||||
if ($driver === 'sqlite') {
|
||||
$refundTotalExpr = "(CASE WHEN JSON_EXTRACT(meta, '$.refund_summary.total_amount') IS NOT NULL THEN CAST(JSON_EXTRACT(meta, '$.refund_summary.total_amount') AS REAL) ELSE (SELECT IFNULL(SUM(CAST(JSON_EXTRACT(value, '$.amount') AS REAL)), 0) FROM json_each(COALESCE(JSON_EXTRACT(meta, '$.refund_receipts'), '[]'))) END)";
|
||||
|
||||
// 只统计退款总额 > 0 的订单
|
||||
$q->whereRaw("ROUND(($refundTotalExpr) * 100) > 0");
|
||||
|
||||
$q->where(function ($b) use ($refundTotalExpr, $tolCents) {
|
||||
$b->where(function ($q2) use ($refundTotalExpr, $tolCents) {
|
||||
$q2->where('payment_status', 'refunded')
|
||||
@@ -137,6 +141,9 @@ class DashboardController extends Controller
|
||||
} else {
|
||||
$refundTotalExpr = "(CASE WHEN JSON_EXTRACT(meta, '$.refund_summary.total_amount') IS NOT NULL THEN CAST(JSON_UNQUOTE(JSON_EXTRACT(meta, '$.refund_summary.total_amount')) AS DECIMAL(12,2)) ELSE (SELECT IFNULL(SUM(j.amount), 0) FROM JSON_TABLE(meta, '$.refund_receipts[*]' COLUMNS(amount DECIMAL(12,2) PATH '$.amount')) j) END)";
|
||||
|
||||
// 只统计退款总额 > 0 的订单
|
||||
$q->whereRaw("ROUND(($refundTotalExpr) * 100) > 0");
|
||||
|
||||
$q->where(function ($b) use ($refundTotalExpr, $tolCents) {
|
||||
$b->where(function ($q2) use ($refundTotalExpr, $tolCents) {
|
||||
$q2->where('payment_status', 'refunded')
|
||||
|
||||
@@ -2501,18 +2501,24 @@ class PlatformOrderController extends Controller
|
||||
})
|
||||
->when(($filters['refund_status'] ?? '') !== '', function (Builder $builder) use ($filters) {
|
||||
// 退款轨迹筛选:
|
||||
// - has:有退款(refund_summary.total_amount 存在 或 refund_receipts[0].amount 存在)
|
||||
// - none:无退款(两者都不存在)
|
||||
// - has:有退款(退款总额 > 0)
|
||||
// - none:无退款(退款总额 <= 0,或无任何退款字段)
|
||||
// 退款总额口径:优先 refund_summary.total_amount;缺省回退汇总 refund_receipts[].amount
|
||||
// 说明:这里不再用“字段是否存在”判断 has/none,避免出现 total_amount=0 但被误判为“有退款”。
|
||||
$status = (string) ($filters['refund_status'] ?? '');
|
||||
|
||||
$driver = $builder->getQuery()->getConnection()->getDriverName();
|
||||
|
||||
if ($driver === 'sqlite') {
|
||||
$refundTotalExpr = "(CASE WHEN JSON_EXTRACT(meta, '$.refund_summary.total_amount') IS NOT NULL THEN CAST(JSON_EXTRACT(meta, '$.refund_summary.total_amount') AS REAL) ELSE (SELECT IFNULL(SUM(CAST(JSON_EXTRACT(value, '$.amount') AS REAL)), 0) FROM json_each(COALESCE(JSON_EXTRACT(meta, '$.refund_receipts'), '[]'))) END)";
|
||||
} else {
|
||||
$refundTotalExpr = "(CASE WHEN JSON_EXTRACT(meta, '$.refund_summary.total_amount') IS NOT NULL THEN CAST(JSON_UNQUOTE(JSON_EXTRACT(meta, '$.refund_summary.total_amount')) AS DECIMAL(12,2)) ELSE (SELECT IFNULL(SUM(j.amount), 0) FROM JSON_TABLE(meta, '$.refund_receipts[*]' COLUMNS(amount DECIMAL(12,2) PATH '$.amount')) j) END)";
|
||||
}
|
||||
|
||||
if ($status === 'has') {
|
||||
$builder->where(function (Builder $q) {
|
||||
$q->whereRaw("JSON_EXTRACT(meta, '$.refund_summary.total_amount') IS NOT NULL")
|
||||
->orWhereRaw("JSON_EXTRACT(meta, '$.refund_receipts[0].amount') IS NOT NULL");
|
||||
});
|
||||
$builder->whereRaw("ROUND(($refundTotalExpr) * 100) > 0");
|
||||
} elseif ($status === 'none') {
|
||||
$builder->whereRaw("JSON_EXTRACT(meta, '$.refund_summary.total_amount') IS NULL")
|
||||
->whereRaw("JSON_EXTRACT(meta, '$.refund_receipts[0].amount') IS NULL");
|
||||
$builder->whereRaw("ROUND(($refundTotalExpr) * 100) <= 0");
|
||||
}
|
||||
})
|
||||
->when(($filters['refund_inconsistent'] ?? '') !== '', function (Builder $builder) {
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\Merchant;
|
||||
use App\Models\Plan;
|
||||
use App\Models\PlatformOrder;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* 退款轨迹筛选(refund_status)应按“退款总额是否>0”判定。
|
||||
* 目的:避免 refund_summary.total_amount=0 这种“空字段”被误判为“有退款”。
|
||||
*/
|
||||
class AdminPlatformOrderRefundStatusFilterShouldTreatZeroRefundAsNoneTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
protected function loginAsPlatformAdmin(): void
|
||||
{
|
||||
$this->seed();
|
||||
|
||||
$this->post('/admin/login', [
|
||||
'email' => 'platform.admin@demo.local',
|
||||
'password' => 'Platform@123456',
|
||||
])->assertRedirect('/admin');
|
||||
}
|
||||
|
||||
public function test_refund_status_filter_should_treat_zero_refund_summary_as_none(): void
|
||||
{
|
||||
$this->loginAsPlatformAdmin();
|
||||
|
||||
$merchant = Merchant::query()->firstOrFail();
|
||||
|
||||
$plan = Plan::query()->create([
|
||||
'code' => 'refund_status_zero_should_be_none',
|
||||
'name' => '退款轨迹筛选:0 退款应判定为 none',
|
||||
'billing_cycle' => 'monthly',
|
||||
'price' => 10,
|
||||
'list_price' => 10,
|
||||
'status' => 'active',
|
||||
'sort' => 10,
|
||||
'published_at' => now(),
|
||||
]);
|
||||
|
||||
// 1) refund_summary.total_amount=0:应视为“无退款”
|
||||
PlatformOrder::query()->create([
|
||||
'merchant_id' => $merchant->id,
|
||||
'plan_id' => $plan->id,
|
||||
'order_no' => 'PO_REFUND_SUMMARY_ZERO_0001',
|
||||
'order_type' => 'new_purchase',
|
||||
'status' => 'activated',
|
||||
'payment_status' => 'paid',
|
||||
'plan_name' => $plan->name,
|
||||
'billing_cycle' => $plan->billing_cycle,
|
||||
'period_months' => 1,
|
||||
'quantity' => 1,
|
||||
'payable_amount' => 10,
|
||||
'paid_amount' => 10,
|
||||
'placed_at' => now(),
|
||||
'paid_at' => now(),
|
||||
'activated_at' => now(),
|
||||
'meta' => [
|
||||
'refund_summary' => [
|
||||
'count' => 0,
|
||||
'total_amount' => 0,
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
// 2) refund_receipts 有金额:应视为“有退款”
|
||||
PlatformOrder::query()->create([
|
||||
'merchant_id' => $merchant->id,
|
||||
'plan_id' => $plan->id,
|
||||
'order_no' => 'PO_REFUND_RECEIPT_POSITIVE_0002',
|
||||
'order_type' => 'new_purchase',
|
||||
'status' => 'activated',
|
||||
'payment_status' => 'partially_refunded',
|
||||
'plan_name' => $plan->name,
|
||||
'billing_cycle' => $plan->billing_cycle,
|
||||
'period_months' => 1,
|
||||
'quantity' => 1,
|
||||
'payable_amount' => 10,
|
||||
'paid_amount' => 10,
|
||||
'placed_at' => now(),
|
||||
'paid_at' => now(),
|
||||
'activated_at' => now(),
|
||||
'meta' => [
|
||||
'refund_receipts' => [
|
||||
['amount' => 1.00, 'refunded_at' => now()->toDateTimeString()],
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
$this->get('/admin/platform-orders?refund_status=has')
|
||||
->assertOk()
|
||||
->assertSee('PO_REFUND_RECEIPT_POSITIVE_0002')
|
||||
->assertDontSee('PO_REFUND_SUMMARY_ZERO_0001');
|
||||
|
||||
$this->get('/admin/platform-orders?refund_status=none')
|
||||
->assertOk()
|
||||
->assertSee('PO_REFUND_SUMMARY_ZERO_0001')
|
||||
->assertDontSee('PO_REFUND_RECEIPT_POSITIVE_0002');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user