Files
boursecu_server/app/Http/Controllers/Customer/OrderController.php
T
2026-07-18 15:04:08 +03:30

85 lines
3.1 KiB
PHP

<?php
namespace App\Http\Controllers\Customer;
use App\DB\Te\TableEngine;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Single\SingleOrderController;
use App\Models\Customer;
use App\Models\Order;
use App\Models\Price;
use App\Models\Setting;
use App\Services\WalletBalanceService;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
class OrderController extends Controller
{
public function store(Request $request)
{
$user = auth()->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);
$goldAmount = app(WalletBalanceService::class)->effectiveMainWalletGold((int) $customerId);
$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);
}
}