user(); if ($user->is_legal == 0 or $user->id==4) { return response()->json(['massage' => 'لطفا ابتدا فرآیند احراز هویت خود را تکمیل کنید'], 422); } // if () $data = $request->all(); $data['customer_id'] = auth('customer')->id(); $data['sts'] = 2; $data['payment_sts'] = 4; $PriceMain = Price::orderBy('id','desc')->first(); $data['price'] = $PriceMain->price; $add_sell_price = config('add_sell_price.major'); $customer = auth('customer')->user(); if (!auth('customer')->user()->trade_active){ throw new UnprocessableEntityHttpException('امکان سفارش برای شما غیرفعال است.'); } app(ConfigService::class)->assertTradingOpen(); $wallet = Wallet::where('owner',auth('customer')->id())->whereNull('fund_id') ->first(); $wallet_interface = new WalletInterface($data['customer_id'], 1); if ($data['direction'] == 1){ // $order = Order::where('customer_id',auth('customer')->id())->where('sts',1)->first(); // if (!is_null($order)){ // throw new UnprocessableEntityHttpException('شما یک سفارش باز دارید,ابتدا نسبت به تسویه آن اقدام نمایید و مجددا تلاش کنید'); // } $gold_block = 0; $data['price_original'] = $data['price']; $data['total_price_original'] = $data['amount']* $data['price_original']; $data['price'] += $add_sell_price; $data['total_price'] = $data['amount']* $data['price']; $data['payment_remind'] = 0; $data['payment_amount'] = $data['total_price']; $data['fee'] = $add_sell_price; $data['total_fee'] = $data['amount']* $data['fee']; if ($wallet->cash < $data['total_price']){ if ($data['amount'] > $customer->gram){ throw new UnprocessableEntityHttpException('مقدار وارد شده بیش از سقف اعتبار شماست'); } $data['sts'] = 1; $data['payment_remind'] = $data['total_price']; $data['payment_amount'] = 0; $gold_block = $data['amount']; } $data['price_id'] = $PriceMain->id; $order = Order::create($data); new TransactionInterface($wallet_interface, $_inp = [ "cash" => $data['total_price'], "gold"=> $data['amount'], 'expire_at'=>now(), 'active'=>true, 'order_id'=>$order->id, 'start_at'=>now(), 'end_at'=>now()->addDay() ], 1, true,false,$gold_block); if ($data['sts'] == 1){ CheckOrder::dispatch($order->id)->onConnection('database')->delay(now()->addMinutes(5)); } $message = auth('customer')->user()->name." عزیز" ."\n" .'در تاریخ '.verta()->format('Y/n/j H:i:s')." کیف طلایی شما "."\n" .$data['amount']." گرم شارژ شد"."\n" ."با تشکر".$this->Config('name_fa'); }else{ $data['price_original'] = $data['price']; $data['total_price_original'] = $data['amount']* $data['price_original']; $data['price'] -= $add_sell_price; $data['total_price'] = $data['amount']* $data['price']; $data['payment_remind'] = 0; $data['payment_amount'] = $data['total_price']; $data['fee'] = $add_sell_price; $data['total_fee'] = $data['amount']* $data['fee']; if (($wallet->gold - $wallet->gold_block) < $data['amount']){ throw new UnprocessableEntityHttpException('موجودی شما کمتر از میزان سفارش است'); } $order = Order::create($data); new TransactionInterface($wallet_interface, $_inp = [ "gold" => $data['amount'], "cash" => $data['total_price'], 'expire_at'=>now(), 'active'=>true, 'order_id'=>$order->id ], 2, false,true); $message = auth('customer')->user()->name." عزیز" ."\n" .'در تاریخ '.verta()->format('Y/n/j H:i:s')." کیف طلایی شما "."\n" .$data['amount']." گرم کم شد"."\n" ."با تشکر".$this->Config('name_fa'); } $this->Send(auth('customer')->user()->mobile,$message); return response()->json($order); } 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); } }