user(); if ($user->is_legal == 0 || $user->id == 4) { return response()->json(['massage' => 'لطفا ابتدا فرآیند احراز هویت خود را تکمیل کنید'], 422); } if (!auth('customer')->user()->trade_active) { throw new UnprocessableEntityHttpException('امکان سفارش برای شما غیرفعال است.'); } // مسیر واحد سفارش — abshode_id / product_id و کیف محصول جدا return app(SingleOrderController::class)->store($request); } public function profit_gold() { $customerId = auth('customer')->id(); $customer = Customer::with(['wallet']) ->findOrFail($customerId); $lastPrice = (float) (Price::latest('created_at')->value('price') ?? 0); $mainWallet = $customer->wallet->first( fn ($wallet) => is_null($wallet->product_id) && is_null($wallet->fund_id) ) ?? $customer->wallet->first(); $goldAmount = (float) ($mainWallet->gold ?? 0); $orders = Order::query() ->where('customer_id', $customerId) ->where('type', 1) ->where('sts', 2) ->get(); $averagePrice = (float) ($orders->avg('price') ?? 0); $averageFee = (float) ($orders->avg('fee') ?? 0); $totalProfit = $orders->sum(function (Order $order) use ($lastPrice) { $profitPerMoment = ($lastPrice - (float) $order->price) * (float) $order->amount; return (int) $order->direction === 1 ? $profitPerMoment : -$profitPerMoment; }); $feeSingle = (float) (Setting::where('key', 'fee_single')->value('value') ?? config('fee.single', 0)); $currentPriceWithFee = $lastPrice + $feeSingle; $averagePriceWithFee = $averagePrice + $averageFee; return response()->json([ 'current_portfolio_value' => $currentPriceWithFee * $goldAmount, 'average_portfolio_value' => $averagePriceWithFee * $goldAmount, 'unrealized_profit' => ($currentPriceWithFee - $averagePriceWithFee) * $goldAmount, 'average_price' => $averagePrice, 'average_fee' => $averageFee, 'total_profit' => $totalProfit, 'gold_holdings' => $goldAmount, 'customer' => $customer->makeHidden(['password', 'token_sms']), ]); } public function table(Request $request){ $records = Order::where('customer_id',auth('customer')->id()) ->where('type',1); TableEngine::create($records, $request->all()) ->initSearch([]) ->select([ '*',Order::$SELECT_STS_STR ])->export($records); return response()->json($records); } }