From 470f8fe2f5fbe9d09eca7ae773ee7d3d9c08d73b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=90=9D=E5=8D=9C?= Date: Fri, 13 Mar 2026 20:47:12 +0000 Subject: [PATCH] =?UTF-8?q?=E5=B9=B3=E5=8F=B0=E8=AE=A2=E5=8D=95=EF=BC=9A?= =?UTF-8?q?=E5=8D=95=E7=AC=94=E5=AF=B9=E8=B4=A6=E6=98=8E=E7=BB=86CSV?= =?UTF-8?q?=E5=AF=BC=E5=87=BA=EF=BC=88=E6=94=AF=E4=BB=98=E5=9B=9E=E6=89=A7?= =?UTF-8?q?+=E9=80=80=E6=AC=BE=E8=AE=B0=E5=BD=95=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Admin/PlatformOrderController.php | 59 ++++++++ .../admin/platform_orders/show.blade.php | 5 +- routes/web.php | 1 + .../AdminPlatformOrderExportLedgerTest.php | 136 ++++++++++++++++++ 4 files changed, 200 insertions(+), 1 deletion(-) create mode 100644 tests/Feature/AdminPlatformOrderExportLedgerTest.php diff --git a/app/Http/Controllers/Admin/PlatformOrderController.php b/app/Http/Controllers/Admin/PlatformOrderController.php index ce03ab8..1b3dc58 100644 --- a/app/Http/Controllers/Admin/PlatformOrderController.php +++ b/app/Http/Controllers/Admin/PlatformOrderController.php @@ -413,6 +413,65 @@ class PlatformOrderController extends Controller ]); } + /** + * 导出单笔订单的“对账明细”(支付回执 + 退款记录)CSV。 + * 用于运营线下对账沟通/留档,不依赖订单列表的筛选口径。 + */ + public function exportLedger(Request $request, PlatformOrder $order): StreamedResponse + { + $this->ensurePlatformAdmin($request); + + $order->loadMissing(['merchant', 'plan', 'siteSubscription']); + + $paymentReceipts = (array) (data_get($order->meta, 'payment_receipts', []) ?? []); + $refundReceipts = (array) (data_get($order->meta, 'refund_receipts', []) ?? []); + + $filename = 'platform_order_' . $order->id . '_ledger_' . now()->format('Ymd_His') . '.csv'; + + return response()->streamDownload(function () use ($order, $paymentReceipts, $refundReceipts) { + $out = fopen('php://output', 'w'); + + // UTF-8 BOM,避免 Excel 打开中文乱码 + fwrite($out, "\xEF\xBB\xBF"); + + // 订单摘要(两行) + fputcsv($out, ['order_id', (string) $order->id]); + fputcsv($out, ['order_no', (string) $order->order_no]); + fputcsv($out, []); + + // 明细表头 + fputcsv($out, ['record_type', 'channel', 'amount', 'biz_time', 'created_at', 'admin_id', 'note']); + + foreach ($paymentReceipts as $r) { + fputcsv($out, [ + 'payment', + (string) (data_get($r, 'channel') ?? ''), + (string) (data_get($r, 'amount') ?? ''), + (string) (data_get($r, 'paid_at') ?? ''), + (string) (data_get($r, 'created_at') ?? ''), + (string) (data_get($r, 'admin_id') ?? ''), + (string) (data_get($r, 'note') ?? ''), + ]); + } + + foreach ($refundReceipts as $r) { + fputcsv($out, [ + 'refund', + (string) (data_get($r, 'channel') ?? ''), + (string) (data_get($r, 'amount') ?? ''), + (string) (data_get($r, 'refunded_at') ?? ''), + (string) (data_get($r, 'created_at') ?? ''), + (string) (data_get($r, 'admin_id') ?? ''), + (string) (data_get($r, 'note') ?? ''), + ]); + } + + fclose($out); + }, $filename, [ + 'Content-Type' => 'text/csv; charset=UTF-8', + ]); + } + public function activateSubscription(Request $request, PlatformOrder $order, SubscriptionActivationService $service): RedirectResponse { $admin = $this->ensurePlatformAdmin($request); diff --git a/resources/views/admin/platform_orders/show.blade.php b/resources/views/admin/platform_orders/show.blade.php index e6e9394..b37ef38 100644 --- a/resources/views/admin/platform_orders/show.blade.php +++ b/resources/views/admin/platform_orders/show.blade.php @@ -324,7 +324,10 @@
-

支付回执(对账留痕)

+
+

支付回执(对账留痕)

+ 导出对账明细(CSV) +

用于“线下收款/转账/人工核对”的留痕记录(当前阶段先落 meta,不引入独立表)。

@php diff --git a/routes/web.php b/routes/web.php index e26ae4f..59e8f3d 100644 --- a/routes/web.php +++ b/routes/web.php @@ -109,6 +109,7 @@ Route::prefix('admin')->group(function () { Route::post('/platform-orders/clear-sync-errors', [PlatformOrderController::class, 'clearSyncErrors']); Route::post('/platform-orders/clear-bmpa-errors', [PlatformOrderController::class, 'clearBmpaErrors']); Route::get('/platform-orders/{order}', [PlatformOrderController::class, 'show']); + Route::get('/platform-orders/{order}/export-ledger', [PlatformOrderController::class, 'exportLedger']); Route::post('/platform-orders/{order}/activate-subscription', [PlatformOrderController::class, 'activateSubscription']); Route::post('/platform-orders/{order}/mark-paid-and-activate', [PlatformOrderController::class, 'markPaidAndActivate']); Route::post('/platform-orders/{order}/add-payment-receipt', [PlatformOrderController::class, 'addPaymentReceipt']); diff --git a/tests/Feature/AdminPlatformOrderExportLedgerTest.php b/tests/Feature/AdminPlatformOrderExportLedgerTest.php new file mode 100644 index 0000000..f01c738 --- /dev/null +++ b/tests/Feature/AdminPlatformOrderExportLedgerTest.php @@ -0,0 +1,136 @@ +seed(); + + $this->post('/admin/login', [ + 'email' => 'platform.admin@demo.local', + 'password' => 'Platform@123456', + ])->assertRedirect('/admin'); + } + + public function test_export_ledger_should_download_csv_with_bom_and_headers(): void + { + $this->loginAsPlatformAdmin(); + + $merchant = Merchant::query()->firstOrFail(); + $plan = Plan::query()->create([ + 'code' => 'po_export_ledger_plan', + 'name' => '平台订单导出对账明细测试套餐', + 'billing_cycle' => 'monthly', + 'price' => 10, + 'list_price' => 10, + 'status' => 'active', + 'sort' => 10, + 'published_at' => now(), + ]); + + $order = PlatformOrder::query()->create([ + 'merchant_id' => $merchant->id, + 'plan_id' => $plan->id, + 'order_no' => 'PO_EXPORT_LEDGER_0001', + 'order_type' => 'new_purchase', + 'status' => 'pending', + 'payment_status' => 'unpaid', + 'plan_name' => $plan->name, + 'billing_cycle' => $plan->billing_cycle, + 'period_months' => 1, + 'quantity' => 1, + 'payable_amount' => 10, + 'paid_amount' => 0, + 'placed_at' => now(), + 'meta' => [ + 'payment_receipts' => [ + [ + 'type' => 'bank_transfer', + 'channel' => 'offline', + 'amount' => 10, + 'paid_at' => now()->toDateTimeString(), + 'note' => 'test-pay', + 'created_at' => now()->toDateTimeString(), + 'admin_id' => 1, + ], + ], + 'refund_receipts' => [ + [ + 'type' => 'refund', + 'channel' => 'offline', + 'amount' => 1, + 'refunded_at' => now()->toDateTimeString(), + 'note' => 'test-refund', + 'created_at' => now()->toDateTimeString(), + 'admin_id' => 1, + ], + ], + ], + ]); + + $res = $this->get('/admin/platform-orders/' . $order->id . '/export-ledger'); + $res->assertOk(); + + $content = $res->streamedContent(); + + // UTF-8 BOM + $this->assertStringStartsWith("\xEF\xBB\xBF", $content); + + // 核心表头 + $this->assertStringContainsString('record_type,channel,amount,biz_time,created_at,admin_id,note', $content); + + // 至少包含一条 payment 与一条 refund 行 + $this->assertStringContainsString('payment,offline,10', $content); + $this->assertStringContainsString('refund,offline,1', $content); + } + + public function test_show_page_should_render_export_ledger_link(): void + { + $this->loginAsPlatformAdmin(); + + $merchant = Merchant::query()->firstOrFail(); + $plan = Plan::query()->create([ + 'code' => 'po_export_ledger_link_plan', + 'name' => '平台订单详情导出对账明细链接测试套餐', + 'billing_cycle' => 'monthly', + 'price' => 10, + 'list_price' => 10, + 'status' => 'active', + 'sort' => 10, + 'published_at' => now(), + ]); + + $order = PlatformOrder::query()->create([ + 'merchant_id' => $merchant->id, + 'plan_id' => $plan->id, + 'order_no' => 'PO_EXPORT_LEDGER_LINK_0001', + 'order_type' => 'new_purchase', + 'status' => 'pending', + 'payment_status' => 'unpaid', + 'plan_name' => $plan->name, + 'billing_cycle' => $plan->billing_cycle, + 'period_months' => 1, + 'quantity' => 1, + 'payable_amount' => 10, + 'paid_amount' => 0, + 'placed_at' => now(), + 'meta' => [], + ]); + + $res = $this->get('/admin/platform-orders/' . $order->id); + $res->assertOk(); + + $res->assertSee('/admin/platform-orders/' . $order->id . '/export-ledger', false); + $res->assertSee('导出对账明细(CSV)', false); + } +}