Dashboard: bmpa processable count excludes refund trace and renewal missing subscription

This commit is contained in:
萝卜
2026-03-18 01:35:00 +08:00
parent c78ae01558
commit 6e59ae3eb6
2 changed files with 123 additions and 4 deletions

View File

@@ -48,10 +48,31 @@ class DashboardController extends Controller
->where('ends_at', '<', now()->addDays(7))
->count(),
'platform_orders' => PlatformOrder::count(),
'platform_orders_unpaid_pending' => PlatformOrder::query()
->where('payment_status', 'unpaid')
->where('status', 'pending')
->count(),
// 可BMPA处理尽量贴近后端治理安全阀
// - pending + unpaid
// - 排除存在退款轨迹refund_total > 0
// - 排除续费缺订阅order_type=renewal 且 site_subscription_id 为空)
'platform_orders_unpaid_pending' => (function () {
$q = PlatformOrder::query()
->where('payment_status', 'unpaid')
->where('status', 'pending')
->where(function ($b) {
$b->where('order_type', '!=', 'renewal')
->orWhereNotNull('site_subscription_id');
});
$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->whereRaw("ROUND(($refundTotalExpr) * 100) <= 0");
} 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->whereRaw("ROUND(($refundTotalExpr) * 100) <= 0");
}
return $q->count();
})(),
'platform_orders_paid_pending' => PlatformOrder::query()
->where('payment_status', 'paid')
->where('status', 'pending')