feat(platform-orders): 支持退款留痕与支付状态自动推进(meta.refund_receipts)
This commit is contained in:
@@ -380,6 +380,60 @@ class PlatformOrderController extends Controller
|
||||
return redirect()->back()->with('success', '已追加支付回执记录(仅用于对账留痕,不自动改状态)。');
|
||||
}
|
||||
|
||||
public function addRefundReceipt(Request $request, PlatformOrder $order): RedirectResponse
|
||||
{
|
||||
$admin = $this->ensurePlatformAdmin($request);
|
||||
|
||||
$data = $request->validate([
|
||||
'type' => ['required', 'string', 'max:30'],
|
||||
'channel' => ['nullable', 'string', 'max:30'],
|
||||
'amount' => ['required', 'numeric', 'min:0'],
|
||||
'refunded_at' => ['nullable', 'date'],
|
||||
'note' => ['nullable', 'string', 'max:2000'],
|
||||
]);
|
||||
|
||||
$now = now();
|
||||
|
||||
$meta = (array) ($order->meta ?? []);
|
||||
$refunds = (array) (data_get($meta, 'refund_receipts', []) ?? []);
|
||||
|
||||
$refunds[] = [
|
||||
'type' => (string) $data['type'],
|
||||
'channel' => (string) ($data['channel'] ?? ''),
|
||||
'amount' => (float) $data['amount'],
|
||||
'refunded_at' => $data['refunded_at'] ? (string) $data['refunded_at'] : null,
|
||||
'note' => (string) ($data['note'] ?? ''),
|
||||
'created_at' => $now->toDateTimeString(),
|
||||
'admin_id' => $admin->id,
|
||||
];
|
||||
|
||||
data_set($meta, 'refund_receipts', $refunds);
|
||||
|
||||
// 可治理辅助:自动推进退款标记(仅当订单本身已支付,且退款金额>0 时)
|
||||
if ($order->payment_status === 'paid' && (float) $data['amount'] > 0) {
|
||||
$totalRefunded = 0.0;
|
||||
foreach ($refunds as $r) {
|
||||
$totalRefunded += (float) (data_get($r, 'amount') ?? 0);
|
||||
}
|
||||
|
||||
$paidAmount = (float) ($order->paid_amount ?? 0);
|
||||
|
||||
// 退款总额 >= 已付金额 => 视为已退款;否则视为部分退款
|
||||
if ($paidAmount > 0 && $totalRefunded >= $paidAmount) {
|
||||
$order->payment_status = 'refunded';
|
||||
$order->refunded_at = $order->refunded_at ?: now();
|
||||
} else {
|
||||
$order->payment_status = 'partially_refunded';
|
||||
$order->refunded_at = $order->refunded_at ?: now();
|
||||
}
|
||||
}
|
||||
|
||||
$order->meta = $meta;
|
||||
$order->save();
|
||||
|
||||
return redirect()->back()->with('success', '已追加退款记录(用于退款轨迹留痕)。');
|
||||
}
|
||||
|
||||
public function markActivated(Request $request, PlatformOrder $order): RedirectResponse
|
||||
{
|
||||
$admin = $this->ensurePlatformAdmin($request);
|
||||
@@ -479,6 +533,10 @@ class PlatformOrderController extends Controller
|
||||
'最近回执时间',
|
||||
'最近回执金额',
|
||||
'最近回执渠道',
|
||||
'退款记录数',
|
||||
'最近退款时间',
|
||||
'最近退款金额',
|
||||
'最近退款渠道',
|
||||
];
|
||||
|
||||
if ($includeMeta) {
|
||||
@@ -504,6 +562,10 @@ class PlatformOrderController extends Controller
|
||||
$receiptCount = count($receipts);
|
||||
$latestReceipt = $receiptCount > 0 ? end($receipts) : null;
|
||||
|
||||
$refunds = (array) (data_get($order->meta, 'refund_receipts', []) ?? []);
|
||||
$refundCount = count($refunds);
|
||||
$latestRefund = $refundCount > 0 ? end($refunds) : null;
|
||||
|
||||
$row = [
|
||||
$order->id,
|
||||
$order->order_no,
|
||||
@@ -530,6 +592,10 @@ class PlatformOrderController extends Controller
|
||||
(string) (data_get($latestReceipt, 'paid_at') ?? ''),
|
||||
(float) (data_get($latestReceipt, 'amount') ?? 0),
|
||||
(string) (data_get($latestReceipt, 'channel') ?? ''),
|
||||
$refundCount,
|
||||
(string) (data_get($latestRefund, 'refunded_at') ?? ''),
|
||||
(float) (data_get($latestRefund, 'amount') ?? 0),
|
||||
(string) (data_get($latestRefund, 'channel') ?? ''),
|
||||
];
|
||||
|
||||
if ($includeMeta) {
|
||||
|
||||
Reference in New Issue
Block a user