From 56ad502aac38e7bb50013335bf12dbeffc9a63da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=90=9D=E5=8D=9C?= Date: Sun, 15 Mar 2026 23:50:07 +0800 Subject: [PATCH] dashboard: add quick links for reconcile mismatch and refund inconsistent --- .../Controllers/Admin/DashboardController.php | 64 ++++++++++ resources/views/admin/dashboard.blade.php | 2 + ...tchAndRefundInconsistentQuickLinksTest.php | 120 ++++++++++++++++++ 3 files changed, 186 insertions(+) create mode 100644 tests/Feature/AdminDashboardBillingWorkbenchShouldIncludeReconcileMismatchAndRefundInconsistentQuickLinksTest.php diff --git a/app/Http/Controllers/Admin/DashboardController.php b/app/Http/Controllers/Admin/DashboardController.php index 0f9d680..2081418 100644 --- a/app/Http/Controllers/Admin/DashboardController.php +++ b/app/Http/Controllers/Admin/DashboardController.php @@ -87,6 +87,70 @@ class DashboardController extends Controller ->whereRaw("JSON_EXTRACT(meta, '$.payment_summary.total_amount') IS NULL") ->whereRaw("JSON_EXTRACT(meta, '$.payment_receipts[0].amount') IS NULL") ->count(), + // 对账不一致(基于 paid_amount vs 回执总额,容差见 config('saasshop.amounts.tolerance')) + 'platform_orders_reconcile_mismatch' => (function () { + $tol = (float) config('saasshop.amounts.tolerance', 0.01); + $tolCents = (int) round($tol * 100); + $tolCents = max(1, $tolCents); + + $q = PlatformOrder::query(); + $driver = $q->getQuery()->getConnection()->getDriverName(); + + // 重要:与 PlatformOrder::isReconcileMismatch() 口径一致:若无回执证据,不判定 mismatch(而是走“无回执”治理集合) + $q->where(function ($b) { + $b->whereRaw("JSON_EXTRACT(meta, '$.payment_summary.total_amount') IS NOT NULL") + ->orWhereRaw("JSON_EXTRACT(meta, '$.payment_receipts[0].amount') IS NOT NULL"); + }); + + if ($driver === 'sqlite') { + $q->whereRaw("ABS(ROUND((CASE WHEN JSON_EXTRACT(meta, '$.payment_summary.total_amount') IS NOT NULL THEN CAST(JSON_EXTRACT(meta, '$.payment_summary.total_amount') AS REAL) ELSE (SELECT IFNULL(SUM(CAST(JSON_EXTRACT(value, '$.amount') AS REAL)), 0) FROM json_each(COALESCE(JSON_EXTRACT(meta, '$.payment_receipts'), '[]'))) END) * 100) - ROUND(paid_amount * 100)) >= {$tolCents}"); + } else { + $q->whereRaw("ABS(ROUND((CASE WHEN JSON_EXTRACT(meta, '$.payment_summary.total_amount') IS NOT NULL THEN CAST(JSON_UNQUOTE(JSON_EXTRACT(meta, '$.payment_summary.total_amount')) AS DECIMAL(12,2)) ELSE (SELECT IFNULL(SUM(j.amount), 0) FROM JSON_TABLE(meta, '$.payment_receipts[*]' COLUMNS(amount DECIMAL(12,2) PATH '$.amount')) j) END) * 100) - ROUND(paid_amount * 100)) >= {$tolCents}"); + } + + return $q->count(); + })(), + // 退款数据不一致(口径与平台订单列表 refund_inconsistent=1 一致) + 'platform_orders_refund_inconsistent' => (function () { + $tol = (float) config('saasshop.amounts.tolerance', 0.01); + $tolCents = (int) round($tol * 100); + $tolCents = max(1, $tolCents); + + $q = PlatformOrder::query(); + $driver = $q->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)"; + + $q->where(function ($b) use ($refundTotalExpr, $tolCents) { + $b->where(function ($q2) use ($refundTotalExpr, $tolCents) { + $q2->where('payment_status', 'refunded') + ->whereRaw('paid_amount > 0') + ->whereRaw("(ROUND($refundTotalExpr * 100) + {$tolCents}) < ROUND(paid_amount * 100)"); + })->orWhere(function ($q2) use ($refundTotalExpr, $tolCents) { + $q2->where('payment_status', '!=', 'refunded') + ->whereRaw('paid_amount > 0') + ->whereRaw("ROUND($refundTotalExpr * 100) >= (ROUND(paid_amount * 100) + {$tolCents})"); + }); + }); + } 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)"; + + $q->where(function ($b) use ($refundTotalExpr, $tolCents) { + $b->where(function ($q2) use ($refundTotalExpr, $tolCents) { + $q2->where('payment_status', 'refunded') + ->whereRaw('paid_amount > 0') + ->whereRaw("(ROUND($refundTotalExpr * 100) + {$tolCents}) < ROUND(paid_amount * 100)"); + })->orWhere(function ($q2) use ($refundTotalExpr, $tolCents) { + $q2->where('payment_status', '!=', 'refunded') + ->whereRaw('paid_amount > 0') + ->whereRaw("ROUND($refundTotalExpr * 100) >= (ROUND(paid_amount * 100) + {$tolCents})"); + }); + }); + } + + return $q->count(); + })(), // 站点治理 'active_merchants' => Merchant::query()->where('status', 'active')->count(), diff --git a/resources/views/admin/dashboard.blade.php b/resources/views/admin/dashboard.blade.php index 3f0807b..d09e5d6 100644 --- a/resources/views/admin/dashboard.blade.php +++ b/resources/views/admin/dashboard.blade.php @@ -175,6 +175,8 @@ 续费缺订阅({{ (int) ($stats['platform_orders_renewal_missing_subscription'] ?? 0) }}) BMPA失败({{ (int) ($stats['platform_orders_bmpa_failed'] ?? 0) }}) 无回执({{ (int) ($stats['platform_orders_paid_no_receipt'] ?? 0) }}) + 对账不一致({{ (int) ($stats['platform_orders_reconcile_mismatch'] ?? 0) }}) + 退款不一致({{ (int) ($stats['platform_orders_refund_inconsistent'] ?? 0) }}) diff --git a/tests/Feature/AdminDashboardBillingWorkbenchShouldIncludeReconcileMismatchAndRefundInconsistentQuickLinksTest.php b/tests/Feature/AdminDashboardBillingWorkbenchShouldIncludeReconcileMismatchAndRefundInconsistentQuickLinksTest.php new file mode 100644 index 0000000..fb470f4 --- /dev/null +++ b/tests/Feature/AdminDashboardBillingWorkbenchShouldIncludeReconcileMismatchAndRefundInconsistentQuickLinksTest.php @@ -0,0 +1,120 @@ +seed(); + + $this->post('/admin/login', [ + 'email' => 'platform.admin@demo.local', + 'password' => 'Platform@123456', + ])->assertRedirect('/admin'); + } + + public function test_dashboard_billing_workbench_should_include_reconcile_mismatch_and_refund_inconsistent_quick_links(): void + { + Cache::flush(); + + $this->loginAsPlatformAdmin(); + + // 清理 seed 订单,避免 seed 口径变化导致该用例不稳定。 + PlatformOrder::query()->delete(); + + $merchant = Merchant::query()->firstOrFail(); + + $plan = Plan::query()->create([ + 'code' => 'dash_governance_mismatch_test', + 'name' => '仪表盘治理入口对账/退款不一致测试套餐', + 'billing_cycle' => 'monthly', + 'price' => 10, + 'list_price' => 10, + 'status' => 'active', + 'sort' => 10, + 'published_at' => now(), + ]); + + // 1) 对账不一致:paid_amount=10,但 payment_summary.total_amount=9.99 + PlatformOrder::query()->create([ + 'merchant_id' => $merchant->id, + 'plan_id' => $plan->id, + 'order_no' => 'PO_DASH_RECON_MISMATCH_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' => [ + 'payment_summary' => [ + 'count' => 1, + 'total_amount' => 9.99, + 'last_at' => now()->toDateTimeString(), + 'last_amount' => 9.99, + 'last_channel' => 'bank', + ], + ], + ]); + + // 2) 退款不一致:状态=refunded,但退款总额不足 + PlatformOrder::query()->create([ + 'merchant_id' => $merchant->id, + 'plan_id' => $plan->id, + 'order_no' => 'PO_DASH_REFUND_INCONSISTENT_0001', + 'order_type' => 'new_purchase', + 'status' => 'activated', + 'payment_status' => '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(), + 'refunded_at' => now(), + 'meta' => [ + 'refund_summary' => [ + 'count' => 1, + 'total_amount' => 9.98, + 'last_at' => now()->toDateTimeString(), + 'last_amount' => 9.98, + 'last_channel' => 'bank', + ], + ], + ]); + + Cache::flush(); + + $res = $this->get('/admin'); + $res->assertOk(); + + $res->assertSee('对账不一致(1)'); + $res->assertSee('退款不一致(1)'); + + $res->assertSee('reconcile_mismatch=1', false); + $res->assertSee('refund_inconsistent=1', false); + + // 链接应携带 back,并且不应出现 &back= + $res->assertSee('back=%2Fadmin', false); + $res->assertDontSee('&back=', false); + } +}