dashboard: add quick links for reconcile mismatch and refund inconsistent
This commit is contained in:
@@ -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(),
|
||||
|
||||
@@ -175,6 +175,8 @@
|
||||
<a class="btn btn-secondary btn-sm" href="{!! \App\Support\BackUrl::withBack('/admin/platform-orders?renewal_missing_subscription=1', $selfWithoutBack) !!}">续费缺订阅({{ (int) ($stats['platform_orders_renewal_missing_subscription'] ?? 0) }})</a>
|
||||
<a class="btn btn-secondary btn-sm" href="{!! \App\Support\BackUrl::withBack('/admin/platform-orders?bmpa_failed_only=1', $selfWithoutBack) !!}">BMPA失败({{ (int) ($stats['platform_orders_bmpa_failed'] ?? 0) }})</a>
|
||||
<a class="btn btn-secondary btn-sm" href="{!! \App\Support\BackUrl::withBack('/admin/platform-orders?payment_status=paid&receipt_status=none', $selfWithoutBack) !!}">无回执({{ (int) ($stats['platform_orders_paid_no_receipt'] ?? 0) }})</a>
|
||||
<a class="btn btn-secondary btn-sm" href="{!! \App\Support\BackUrl::withBack('/admin/platform-orders?reconcile_mismatch=1', $selfWithoutBack) !!}">对账不一致({{ (int) ($stats['platform_orders_reconcile_mismatch'] ?? 0) }})</a>
|
||||
<a class="btn btn-secondary btn-sm" href="{!! \App\Support\BackUrl::withBack('/admin/platform-orders?refund_inconsistent=1', $selfWithoutBack) !!}">退款不一致({{ (int) ($stats['platform_orders_refund_inconsistent'] ?? 0) }})</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\Merchant;
|
||||
use App\Models\Plan;
|
||||
use App\Models\PlatformOrder;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Tests\TestCase;
|
||||
|
||||
class AdminDashboardBillingWorkbenchShouldIncludeReconcileMismatchAndRefundInconsistentQuickLinksTest 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_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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user