Files
boursecu_server/app/Http/Controllers/Customer/OrderController.php
T

85 lines
3.1 KiB
PHP
Raw Normal View History

2026-01-24 14:03:11 +00:00
<?php
namespace App\Http\Controllers\Customer;
use App\DB\Te\TableEngine;
use App\Http\Controllers\Controller;
2026-07-10 00:11:08 +03:30
use App\Http\Controllers\Single\SingleOrderController;
2026-06-04 12:18:53 +03:30
use App\Models\Customer;
2026-01-24 14:03:11 +00:00
use App\Models\Order;
use App\Models\Price;
2026-06-04 12:18:53 +03:30
use App\Models\Setting;
2026-07-18 15:04:08 +03:30
use App\Services\WalletBalanceService;
2026-01-24 14:03:11 +00:00
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
class OrderController extends Controller
{
2026-07-10 00:11:08 +03:30
public function store(Request $request)
{
$user = auth()->user();
if ($user->is_legal == 0 || $user->id == 4) {
2026-05-23 13:30:05 +03:30
return response()->json(['massage' => 'لطفا ابتدا فرآیند احراز هویت خود را تکمیل کنید'], 422);
2026-01-24 14:03:11 +00:00
}
2026-07-10 00:11:08 +03:30
if (!auth('customer')->user()->trade_active) {
2026-01-24 14:03:11 +00:00
throw new UnprocessableEntityHttpException('امکان سفارش برای شما غیرفعال است.');
}
2026-07-10 00:11:08 +03:30
// مسیر واحد سفارش — abshode_id / product_id و کیف محصول جدا
return app(SingleOrderController::class)->store($request);
2026-01-24 14:03:11 +00:00
}
2026-06-04 12:18:53 +03:30
public function profit_gold()
{
$customerId = auth('customer')->id();
$customer = Customer::with(['wallet'])
->findOrFail($customerId);
2026-06-04 12:18:53 +03:30
$lastPrice = (float) (Price::latest('created_at')->value('price') ?? 0);
2026-06-04 12:18:53 +03:30
2026-07-18 15:04:08 +03:30
$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;
});
2026-06-04 12:18:53 +03:30
$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']),
]);
}
2026-01-24 14:03:11 +00:00
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);
}
}