平台订单:单笔对账明细CSV导出(支付回执+退款记录)

This commit is contained in:
萝卜
2026-03-13 20:47:12 +00:00
parent 9f89536933
commit 470f8fe2f5
4 changed files with 200 additions and 1 deletions

View File

@@ -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);

View File

@@ -324,7 +324,10 @@
</div>
<div class="card mb-20" id="payment-receipts">
<h3>支付回执(对账留痕)</h3>
<div class="flex-between" style="align-items:center;">
<h3 style="margin:0;">支付回执(对账留痕)</h3>
<a class="muted" href="/admin/platform-orders/{{ $order->id }}/export-ledger">导出对账明细CSV</a>
</div>
<p class="muted muted-tight">用于“线下收款/转账/人工核对”的留痕记录(当前阶段先落 meta不引入独立表</p>
@php

View File

@@ -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']);

View File

@@ -0,0 +1,136 @@
<?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 AdminPlatformOrderExportLedgerTest 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_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);
}
}