id) { return null; } $existing = TransactionsAdmin::where('order_id', $order->id)->first(); if ($existing) { return $existing; } try { return TransactionsAdmin::create([ 'customer_id' => $order->customer_id, 'customer_to_id' => $order->customer_id, 'order_id' => $order->id, 'user_id' => $order->customer_id, 'wallet' => null, 'transaction_id' => null, 'cash' => (int) round((float) ($order->total_price ?? 0)), 'note' => $this->tradeNote((int) ($order->direction ?? 0), $order), 'type' => 1, 'gold' => (float) ($order->amount ?? 0), 'balance_cash' => 0, 'balance_gold' => 0, ]); } catch (Throwable $e) { report($e); return null; } } /** توضیح سند معامله — بر اساس نام محصول سفارش */ public function tradeNote(int $direction, ?Order $order = null): string { $productTitle = null; if ($order) { $productTitle = $order->product?->title; if (! $productTitle && $order->product_id) { $productTitle = Product::query()->whereKey($order->product_id)->value('title'); } } $productTitle = trim((string) $productTitle); if ($productTitle === '') { $productTitle = Controller::mainWalletProductLabel() ?: 'مس'; } return $direction === 1 ? ('خرید ' . $productTitle) : ($direction === 2 ? ('فروش ' . $productTitle) : 'معامله'); } }