160 lines
6.9 KiB
PHP
160 lines
6.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Customer;
|
|
|
|
use App\DB\Te\TableEngine;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Jobs\CheckOrder;
|
|
use App\Models\Customer;
|
|
use App\Models\Order;
|
|
use App\Models\Price;
|
|
use App\Models\Setting;
|
|
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) {
|
|
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('امکان سفارش برای شما غیرفعال است.');
|
|
}
|
|
$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()
|
|
{
|
|
$customer = Customer::with(['wallet', 'orders'])
|
|
->findOrFail(auth('customer')->id());
|
|
|
|
$lastPrice = Price::latest('created_at')->value('price') ?? 0;
|
|
$goldAmount = $customer->wallet->first()->gold ?? 0;
|
|
$orders = $customer->orders;
|
|
|
|
$averagePrice = $orders->avg('price') ?? 0;
|
|
$averageFee = $orders->avg('fee') ?? 0;
|
|
$totalProfit = $orders->sum(fn ($order) => $order->direction === 1
|
|
? $order->profit_per_moment
|
|
: -$order->profit_per_moment);
|
|
|
|
$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);
|
|
}
|
|
|
|
}
|