平台订单:新增退款不一致 refund_inconsistent 筛选与摘要卡

This commit is contained in:
萝卜
2026-03-11 04:29:52 +00:00
parent 9de9a44318
commit 35ab780f8c
3 changed files with 205 additions and 0 deletions

View File

@@ -155,6 +155,8 @@ class PlatformOrderController extends Controller
'receipt_status' => trim((string) $request->query('receipt_status', '')), 'receipt_status' => trim((string) $request->query('receipt_status', '')),
// 退款轨迹筛选has有退款/none无退款 // 退款轨迹筛选has有退款/none无退款
'refund_status' => trim((string) $request->query('refund_status', '')), 'refund_status' => trim((string) $request->query('refund_status', '')),
// 退款数据不一致(可治理):基于 refund_summary.total_amount 与 paid_amount 对比
'refund_inconsistent' => (string) $request->query('refund_inconsistent', ''),
]; ];
@@ -328,6 +330,13 @@ class PlatformOrderController extends Controller
return $this->applyFilters(PlatformOrder::query(), $mismatchFilters)->count(); return $this->applyFilters(PlatformOrder::query(), $mismatchFilters)->count();
})(), })(),
// 退款数据不一致订单数(在当前筛选范围基础上叠加 inconsistent 口径)
'refund_inconsistent_orders' => (function () use ($filters) {
$f = $filters;
$f['refund_inconsistent'] = '1';
return $this->applyFilters(PlatformOrder::query(), $f)->count();
})(),
], ],
'failedReasonStats' => $failedReasonStats, 'failedReasonStats' => $failedReasonStats,
]); ]);
@@ -634,6 +643,8 @@ class PlatformOrderController extends Controller
'receipt_status' => trim((string) $request->query('receipt_status', '')), 'receipt_status' => trim((string) $request->query('receipt_status', '')),
// 退款轨迹筛选has有退款/none无退款 // 退款轨迹筛选has有退款/none无退款
'refund_status' => trim((string) $request->query('refund_status', '')), 'refund_status' => trim((string) $request->query('refund_status', '')),
// 退款数据不一致(可治理):基于 refund_summary.total_amount 与 paid_amount 对比
'refund_inconsistent' => (string) $request->query('refund_inconsistent', ''),
]; ];
@@ -823,6 +834,7 @@ class PlatformOrderController extends Controller
'reconcile_mismatch' => (string) $request->input('reconcile_mismatch', ''), 'reconcile_mismatch' => (string) $request->input('reconcile_mismatch', ''),
'receipt_status' => trim((string) $request->input('receipt_status', '')), 'receipt_status' => trim((string) $request->input('receipt_status', '')),
'refund_status' => trim((string) $request->input('refund_status', '')), 'refund_status' => trim((string) $request->input('refund_status', '')),
'refund_inconsistent' => (string) $request->input('refund_inconsistent', ''),
]; ];
// 防误操作:批量同步默认要求先勾选“只看可同步”,避免无意识扩大处理范围 // 防误操作:批量同步默认要求先勾选“只看可同步”,避免无意识扩大处理范围
@@ -967,6 +979,7 @@ class PlatformOrderController extends Controller
'reconcile_mismatch' => (string) $request->input('reconcile_mismatch', ''), 'reconcile_mismatch' => (string) $request->input('reconcile_mismatch', ''),
'receipt_status' => trim((string) $request->input('receipt_status', '')), 'receipt_status' => trim((string) $request->input('receipt_status', '')),
'refund_status' => trim((string) $request->input('refund_status', '')), 'refund_status' => trim((string) $request->input('refund_status', '')),
'refund_inconsistent' => (string) $request->input('refund_inconsistent', ''),
]; ];
// 防误操作:批量“仅标记为已生效”默认要求当前筛选口径为「已支付 + 待处理(pending)」 // 防误操作:批量“仅标记为已生效”默认要求当前筛选口径为「已支付 + 待处理(pending)」
@@ -1088,6 +1101,7 @@ class PlatformOrderController extends Controller
'reconcile_mismatch' => (string) $request->input('reconcile_mismatch', ''), 'reconcile_mismatch' => (string) $request->input('reconcile_mismatch', ''),
'receipt_status' => trim((string) $request->input('receipt_status', '')), 'receipt_status' => trim((string) $request->input('receipt_status', '')),
'refund_status' => trim((string) $request->input('refund_status', '')), 'refund_status' => trim((string) $request->input('refund_status', '')),
'refund_inconsistent' => (string) $request->input('refund_inconsistent', ''),
]; ];
$query = PlatformOrder::query() $query = PlatformOrder::query()
@@ -1277,6 +1291,46 @@ class PlatformOrderController extends Controller
$builder->whereRaw("JSON_EXTRACT(meta, '$.refund_summary.total_amount') IS NULL") $builder->whereRaw("JSON_EXTRACT(meta, '$.refund_summary.total_amount') IS NULL")
->whereRaw("JSON_EXTRACT(meta, '$.refund_receipts[0].amount') IS NULL"); ->whereRaw("JSON_EXTRACT(meta, '$.refund_receipts[0].amount') IS NULL");
} }
})
->when(($filters['refund_inconsistent'] ?? '') !== '', function (Builder $builder) {
// 退款数据不一致(可治理):
// - 状态=refunded 但 退款总额 < 已付金额(允许 0.01 容差)
// - 状态!=refunded 且 已付金额>0 且 退款总额 >= 已付金额
// 退款总额口径:优先 refund_summary.total_amount缺省回退汇总 refund_receipts[].amount
$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)";
$builder->where(function (Builder $q) use ($refundTotalExpr) {
// refunded 但退款不够
$q->where(function (Builder $q2) use ($refundTotalExpr) {
$q2->where('payment_status', 'refunded')
->whereRaw("paid_amount > 0")
->whereRaw("(ROUND($refundTotalExpr * 100) + 1) < ROUND(paid_amount * 100)");
})
// 非 refunded 但退款已达到/超过已付
->orWhere(function (Builder $q2) use ($refundTotalExpr) {
$q2->where('payment_status', '!=', 'refunded')
->whereRaw("paid_amount > 0")
->whereRaw("ROUND($refundTotalExpr * 100) >= ROUND(paid_amount * 100)");
});
});
} 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)";
$builder->where(function (Builder $q) use ($refundTotalExpr) {
$q->where(function (Builder $q2) use ($refundTotalExpr) {
$q2->where('payment_status', 'refunded')
->whereRaw("paid_amount > 0")
->whereRaw("(ROUND($refundTotalExpr * 100) + 1) < ROUND(paid_amount * 100)");
})->orWhere(function (Builder $q2) use ($refundTotalExpr) {
$q2->where('payment_status', '!=', 'refunded')
->whereRaw("paid_amount > 0")
->whereRaw("ROUND($refundTotalExpr * 100) >= ROUND(paid_amount * 100)");
});
});
}
}); });
} }

View File

@@ -34,6 +34,8 @@
<a href="/admin/platform-orders?refund_status=none" class="muted">无退款</a> <a href="/admin/platform-orders?refund_status=none" class="muted">无退款</a>
<span class="muted"></span> <span class="muted"></span>
<a href="/admin/platform-orders?reconcile_mismatch=1" class="muted">对账不一致</a> <a href="/admin/platform-orders?reconcile_mismatch=1" class="muted">对账不一致</a>
<span class="muted"></span>
<a href="/admin/platform-orders?refund_inconsistent=1" class="muted">退款不一致</a>
</div> </div>
</div> </div>
@@ -106,6 +108,10 @@
<input type="checkbox" name="reconcile_mismatch" value="1" @checked(($filters['reconcile_mismatch'] ?? '') === '1')> <input type="checkbox" name="reconcile_mismatch" value="1" @checked(($filters['reconcile_mismatch'] ?? '') === '1')>
<span>只看对账不一致(回执总额≠已付金额)</span> <span>只看对账不一致(回执总额≠已付金额)</span>
</label> </label>
<label class="form-inline-row">
<input type="checkbox" name="refund_inconsistent" value="1" @checked(($filters['refund_inconsistent'] ?? '') === '1')>
<span>只看退款不一致(状态 vs 退款总额)</span>
</label>
<input type="text" name="keyword" placeholder="关键词:订单号/站点/订阅号" value="{{ $filters['keyword'] ?? '' }}"> <input type="text" name="keyword" placeholder="关键词:订单号/站点/订阅号" value="{{ $filters['keyword'] ?? '' }}">
<input type="text" name="sync_error_keyword" placeholder="失败原因关键词(可选)" value="{{ $filters['sync_error_keyword'] ?? '' }}"> <input type="text" name="sync_error_keyword" placeholder="失败原因关键词(可选)" value="{{ $filters['sync_error_keyword'] ?? '' }}">
<div> <div>
@@ -207,6 +213,13 @@
<div class="muted text-danger mt-6">提示:差额非 0,可能存在回执金额与订单金额不一致的订单。</div> <div class="muted text-danger mt-6">提示:差额非 0,可能存在回执金额与订单金额不一致的订单。</div>
@endif @endif
</div> </div>
<div class="card">
<h3>退款不一致订单</h3>
<div class="metric-number">
<a class="link" href="{{ request()->fullUrlWithQuery(['refund_inconsistent' => '1', 'page' => null]) }}">{{ $summaryStats['refund_inconsistent_orders'] ?? 0 }}</a>
</div>
<div class="muted muted-xs">口径:状态=refunded 但退款总额不足;或状态!=refunded 但退款总额已达/超已付</div>
</div>
<div class="card"> <div class="card">
<h3>同步失败原因 TOP5</h3> <h3>同步失败原因 TOP5</h3>
@php $failedReasonStats = $failedReasonStats ?? []; @endphp @php $failedReasonStats = $failedReasonStats ?? []; @endphp
@@ -247,6 +260,7 @@
<input type="hidden" name="keyword" value="{{ $filters['keyword'] ?? '' }}"> <input type="hidden" name="keyword" value="{{ $filters['keyword'] ?? '' }}">
<input type="hidden" name="sync_error_keyword" value="{{ $filters['sync_error_keyword'] ?? '' }}"> <input type="hidden" name="sync_error_keyword" value="{{ $filters['sync_error_keyword'] ?? '' }}">
<input type="hidden" name="reconcile_mismatch" value="{{ $filters['reconcile_mismatch'] ?? '' }}"> <input type="hidden" name="reconcile_mismatch" value="{{ $filters['reconcile_mismatch'] ?? '' }}">
<input type="hidden" name="refund_inconsistent" value="{{ $filters['refund_inconsistent'] ?? '' }}">
<label class="form-inline-row mb-8"> <label class="form-inline-row mb-8">
<input type="checkbox" name="include_meta" value="1"> <input type="checkbox" name="include_meta" value="1">
@@ -275,6 +289,7 @@
<input type="hidden" name="keyword" value="{{ $filters['keyword'] ?? '' }}"> <input type="hidden" name="keyword" value="{{ $filters['keyword'] ?? '' }}">
<input type="hidden" name="sync_error_keyword" value="{{ $filters['sync_error_keyword'] ?? '' }}"> <input type="hidden" name="sync_error_keyword" value="{{ $filters['sync_error_keyword'] ?? '' }}">
<input type="hidden" name="reconcile_mismatch" value="{{ $filters['reconcile_mismatch'] ?? '' }}"> <input type="hidden" name="reconcile_mismatch" value="{{ $filters['reconcile_mismatch'] ?? '' }}">
<input type="hidden" name="refund_inconsistent" value="{{ $filters['refund_inconsistent'] ?? '' }}">
<label class="muted form-inline-row mb-8"> <label class="muted form-inline-row mb-8">
<span>本次最多处理</span> <span>本次最多处理</span>
@@ -305,6 +320,7 @@
<input type="hidden" name="keyword" value="{{ $filters['keyword'] ?? '' }}"> <input type="hidden" name="keyword" value="{{ $filters['keyword'] ?? '' }}">
<input type="hidden" name="sync_error_keyword" value="{{ $filters['sync_error_keyword'] ?? '' }}"> <input type="hidden" name="sync_error_keyword" value="{{ $filters['sync_error_keyword'] ?? '' }}">
<input type="hidden" name="reconcile_mismatch" value="{{ $filters['reconcile_mismatch'] ?? '' }}"> <input type="hidden" name="reconcile_mismatch" value="{{ $filters['reconcile_mismatch'] ?? '' }}">
<input type="hidden" name="refund_inconsistent" value="{{ $filters['refund_inconsistent'] ?? '' }}">
<label class="muted form-inline-row mb-8"> <label class="muted form-inline-row mb-8">
<span>确认输入</span> <span>确认输入</span>
<input type="text" name="confirm" placeholder="YES" class="w-140"> <input type="text" name="confirm" placeholder="YES" class="w-140">
@@ -337,6 +353,7 @@
<input type="hidden" name="keyword" value="{{ $filters['keyword'] ?? '' }}"> <input type="hidden" name="keyword" value="{{ $filters['keyword'] ?? '' }}">
<input type="hidden" name="sync_error_keyword" value="{{ $filters['sync_error_keyword'] ?? '' }}"> <input type="hidden" name="sync_error_keyword" value="{{ $filters['sync_error_keyword'] ?? '' }}">
<input type="hidden" name="reconcile_mismatch" value="{{ $filters['reconcile_mismatch'] ?? '' }}"> <input type="hidden" name="reconcile_mismatch" value="{{ $filters['reconcile_mismatch'] ?? '' }}">
<input type="hidden" name="refund_inconsistent" value="{{ $filters['refund_inconsistent'] ?? '' }}">
<label class="muted form-inline-row mb-8"> <label class="muted form-inline-row mb-8">
<span>本次最多处理</span> <span>本次最多处理</span>
@@ -383,6 +400,7 @@
<input type="hidden" name="keyword" value="{{ $filters['keyword'] ?? '' }}"> <input type="hidden" name="keyword" value="{{ $filters['keyword'] ?? '' }}">
<input type="hidden" name="sync_error_keyword" value="{{ $filters['sync_error_keyword'] ?? '' }}"> <input type="hidden" name="sync_error_keyword" value="{{ $filters['sync_error_keyword'] ?? '' }}">
<input type="hidden" name="reconcile_mismatch" value="{{ $filters['reconcile_mismatch'] ?? '' }}"> <input type="hidden" name="reconcile_mismatch" value="{{ $filters['reconcile_mismatch'] ?? '' }}">
<input type="hidden" name="refund_inconsistent" value="{{ $filters['refund_inconsistent'] ?? '' }}">
<button type="submit">清除当前筛选范围的失败标记</button> <button type="submit">清除当前筛选范围的失败标记</button>
</form> </form>

View File

@@ -0,0 +1,133 @@
<?php
namespace Tests\Feature;
use App\Models\Merchant;
use App\Models\Plan;
use App\Models\PlatformOrder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class AdminPlatformOrderRefundInconsistentFilterTest 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_platform_orders_page_can_filter_refund_inconsistent(): void
{
$this->loginAsPlatformAdmin();
$merchant = Merchant::query()->firstOrFail();
$plan = Plan::query()->create([
'code' => 'refund_inconsistent_test',
'name' => '退款不一致筛选测试套餐',
'billing_cycle' => 'monthly',
'price' => 10,
'list_price' => 10,
'status' => 'active',
'sort' => 10,
'published_at' => now(),
]);
// 1) 不一致:状态=refunded但退款总额不足
PlatformOrder::query()->create([
'merchant_id' => $merchant->id,
'plan_id' => $plan->id,
'order_no' => 'PO_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.99,
'last_channel' => 'bank',
],
],
]);
// 2) 不一致:状态!=refunded但退款总额已达到/超过已付金额
PlatformOrder::query()->create([
'merchant_id' => $merchant->id,
'plan_id' => $plan->id,
'order_no' => 'PO_REFUND_INCONSISTENT_0002',
'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' => 1,
'total_amount' => 10.00,
'last_at' => now()->toDateTimeString(),
'last_amount' => 10.00,
'last_channel' => 'bank',
],
],
]);
// 3) 一致refunded 且退款总额=已付金额
PlatformOrder::query()->create([
'merchant_id' => $merchant->id,
'plan_id' => $plan->id,
'order_no' => 'PO_REFUND_CONSISTENT_0003',
'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' => 10.00,
'last_at' => now()->toDateTimeString(),
'last_amount' => 10.00,
'last_channel' => 'bank',
],
],
]);
$this->get('/admin/platform-orders?refund_inconsistent=1')
->assertOk()
->assertSee('PO_REFUND_INCONSISTENT_0001')
->assertSee('PO_REFUND_INCONSISTENT_0002')
->assertDontSee('PO_REFUND_CONSISTENT_0003');
}
}