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

172 lines
7.3 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;
use App\Jobs\CheckOrder;
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-01-24 14:03:11 +00:00
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Rooyesh\Wallet\interfaces\TransactionInterface;
use Rooyesh\Wallet\interfaces\WalletInterface;
use Rooyesh\Wallet\models\Wallet;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
class OrderController extends Controller
{
public function store(Request $request){
$user= auth()->user();
if ($user->is_legal == 0 or $user->id==4) {
2026-05-23 13:30:05 +03:30
return response()->json(['massage' => 'لطفا ابتدا فرآیند احراز هویت خود را تکمیل کنید'], 422);
2026-01-24 14:03:11 +00:00
}
// 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('امکان سفارش برای شما غیرفعال است.');
}
$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);
}
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
$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;
});
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);
}
}