320 lines
15 KiB
PHP
320 lines
15 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Customer;
|
|
|
|
use App\DB\Te\TableEngine;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Address;
|
|
use App\Models\Customer;
|
|
use App\Models\Order;
|
|
use App\Models\User;
|
|
use App\Models\Withdraw;
|
|
use App\Models\BankAccount;
|
|
use App\Models\Price;
|
|
use App\Http\Controllers\Admin\TahesabController;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
use Rooyesh\Wallet\interfaces\TransactionInterface;
|
|
use Rooyesh\Wallet\interfaces\WalletInterface;
|
|
use Rooyesh\Wallet\models\Transaction;
|
|
use Rooyesh\Wallet\models\Wallet;
|
|
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
|
|
|
class WalletController extends Controller
|
|
{
|
|
public function withdrawTable(Request $request)
|
|
{
|
|
$records = Withdraw::with(['transaction'])->where('customer_id', auth('customer')->id())->whereIn('sts', [2,1])->orderBy('id','desc');
|
|
TableEngine::create($records, $request->all())
|
|
->initSearch(['customer.name'])
|
|
->select([
|
|
'*',Withdraw::$SELECT_STS_STR,Withdraw::$SELECT_DAJ
|
|
])->export($records);
|
|
return response()->json($records);
|
|
}
|
|
|
|
public function walletMe()
|
|
{
|
|
$wallet = Wallet::select(['owner', 'id', 'gold', 'cash'])->where('owner', auth('customer')->id())
|
|
->first();
|
|
// $wallet['gold_sum'] = Wallet::where('owner', auth('customer')->id())->get();
|
|
|
|
$wallet['gold_sum'] = round(Wallet::where('owner', auth('customer')->id())->where('product_id', null)->sum('gold'), 2);
|
|
return response()->json($wallet);
|
|
}
|
|
|
|
public function priceData(Request $request)
|
|
{
|
|
return $response = Http::get('https://milli.gold/api/v1/public/milli-price/widget/external');
|
|
$prise = Price::with('order');
|
|
TableEngine::create($prise, $request)
|
|
->initSearch(['price'])
|
|
->select(['*'])
|
|
->export($prise);
|
|
return response()->json($prise);
|
|
}
|
|
|
|
public function transactionTable(Request $request)
|
|
{
|
|
if (isset($request->wallet_id)){
|
|
$wallet = Wallet::with('product')->where('id', $request->wallet_id)->where('owner', auth('customer')->id())->get();
|
|
}else{
|
|
$wallet = Wallet::with('product')->where('owner', auth('customer')->id())->get();
|
|
}
|
|
|
|
if ($wallet->isEmpty()) {
|
|
$records = ['query' => ['total' => 0], 'rows' => []];
|
|
} else {
|
|
$records = Transaction::with([
|
|
'order' => function ($q) {
|
|
$q->select(['*', Order::$SELECT_STS_STR]);
|
|
},
|
|
'withdraw' => function ($q) {
|
|
$q->select(['*', Order::$SELECT_STS_STR]);
|
|
},
|
|
'wallet.product'
|
|
])->whereIn('wallet', $wallet->pluck('id'))->whereNull('fund_id');
|
|
|
|
TableEngine::create($records, $request->all())
|
|
->initSearch(['customer.name'])
|
|
->select([
|
|
'*', Transaction::$SELECT_CAJ, Transaction::$SELECT_TYPE_STR, Transaction::$SELECT_SAJ, Transaction::$SELECT_EAJ
|
|
])->export($records);
|
|
}
|
|
|
|
foreach ($records['rows'] as &$record) {
|
|
if (empty($record->title) or $record->title == "تعریف نشده"){
|
|
$record->title = $record->type_str ;
|
|
$record->unit = "گرم";
|
|
if (isset($record->wallet)) {
|
|
$wallet = Wallet::with('product')->where('id',$record->wallet)->first();
|
|
if (isset($wallet->product)) {
|
|
if ($record->gold > 0){
|
|
$record->title = " خرید " ;
|
|
|
|
}else{
|
|
$record->title = " فروش " ;
|
|
|
|
}
|
|
$record->title .= $wallet->product->title ;
|
|
$record->unit = "عدد";
|
|
|
|
}
|
|
}
|
|
$record->save();
|
|
}
|
|
|
|
}
|
|
|
|
return response()->json($records);
|
|
}
|
|
|
|
public function transactionTableb(Request $request)
|
|
{
|
|
$wallet = Wallet::with('product')->where('owner', auth('customer')->id())->whereNull('fund_id')->get();
|
|
if (is_null($wallet)) {
|
|
$records = ['query' => ['total' => 0], 'rows' => []];
|
|
} else {
|
|
$records = Transaction::with(['order' => function ($q) {
|
|
return $q->select(['*', Order::$SELECT_STS_STR]);
|
|
}, 'withdraw' => function ($q) {
|
|
return $q->select(['*', Order::$SELECT_STS_STR]);
|
|
}, 'wallet.product'])->whereIn('wallet', $wallet->pluck('id'))->whereNull('fund_id');
|
|
TableEngine::create($records, $request->all())
|
|
->initSearch(['customer.name'])
|
|
->select([
|
|
'*', Transaction::$SELECT_CAJ, Transaction::$SELECT_TYPE_STR, Transaction::$SELECT_SAJ, Transaction::$SELECT_EAJ
|
|
])->export($records);
|
|
}
|
|
foreach ($records['rows'] as &$record) {
|
|
return $record['wallet'];
|
|
if (isset($record->wallet->product->title)) {
|
|
$record->title = $record->wallet->product->title."22";
|
|
$record->save();
|
|
} else {
|
|
$record->title = $record->type_str."22";
|
|
$record->save();
|
|
}
|
|
}
|
|
|
|
return response()->json($records);
|
|
}
|
|
|
|
public function withdraw(Request $request)
|
|
{
|
|
$customer = auth('customer')->user();
|
|
$data = $request->all();
|
|
if (!isset($data['card_id'])) {
|
|
return response()->json(['message' => 'کارت انتخاب نشده'], 422);
|
|
}
|
|
$bCart = BankAccount::where('id', $data['card_id'])->first();
|
|
if ($bCart->sts != 2) {
|
|
return response()->json(['message' => 'کارت انتخاب شده غیر فعال است یا ردشده'], 422);
|
|
}
|
|
|
|
$wallet_interface_fund = new WalletInterface(auth('customer')->id(), auth('customer')->user()->type);
|
|
$wallet = $wallet_interface_fund->wallet;
|
|
if ($data['value'] < 0) {
|
|
throw new UnprocessableEntityHttpException('مقدار برداشت باید مثبت باشد');
|
|
}
|
|
if ($data['value'] > $wallet->cash) {
|
|
throw new UnprocessableEntityHttpException('مقدار برداشت بیشتر از موجودی است');
|
|
}
|
|
$Withdraw = Withdraw::create([
|
|
'customer_id' => auth('customer')->id(),
|
|
"cash" => $data['value'],
|
|
"note" => "برداشت ریالی",
|
|
"bank_account_id" => $data['card_id'],
|
|
'sts' => 1,
|
|
'type' => 2
|
|
]);
|
|
$transaction = new TransactionInterface($wallet_interface_fund, $_inp = [
|
|
"cash" => $data['value'],
|
|
'expire_at' => now(),
|
|
'withdraw_id' => $Withdraw->id,
|
|
'active' => true
|
|
], 4, false, false);
|
|
$userMobile = $customer->mobile; // User's mobile number
|
|
$userName = $customer->name; // User's name
|
|
$withdrawAmount = $data['value']; // amount
|
|
|
|
$tahesab = new TahesabController();
|
|
$tahesab->wallet([
|
|
"customer_id" => auth('customer')->id(),
|
|
"value" => $data['value'],
|
|
"direction" => 1,
|
|
]);
|
|
|
|
$companyName = $this->Config('name_fa'); // Company name from configuration
|
|
$message = "کاربر $userName با شماره موبایل $userMobile درخواست برداشت مبلغ " . number_format($withdrawAmount) . " ریال را دارد. با تشکر، $companyName";
|
|
if (isset($request->otp)) {
|
|
if (Customer::where("id", $customer->id)->first()->token_sms != $request->otp) {
|
|
return response()->json(['message' => 'رمز دوم اشتباه میباشد'], 422);
|
|
}
|
|
try {
|
|
$response = Http::withHeaders([
|
|
'Authorization' => 'Bearer ' . $this->Config('liman_token')
|
|
])->post('https://limangold.ir/kernel/customer/fin', [
|
|
'receive_bank_shaba' => $bCart->shaba_number,
|
|
'receive_bank_card' => $bCart->cart_number,
|
|
'receive_name' => $customer->name,
|
|
'receive_account_number' => $bCart->account_number,
|
|
'description' => $this->Config('name_fa') . ' | ' . $customer->mobile . ' | ' . $customer->name . ' | ' . $Withdraw->id,
|
|
'amount' => '1000'
|
|
])->json();
|
|
if (isset($response['id'])) {
|
|
$Withdraw->note = $response['id'];
|
|
$Withdraw->save();
|
|
$message = "کاربر $userName با شماره موبایل $userMobile درخواست برداشت مبلغ " . number_format($withdrawAmount) . " ثبت کرد و به لیمان منتقل شد. با تشکر، $companyName";
|
|
} else {
|
|
$message = "کاربر $userName با شماره موبایل $userMobile درخواست برداشت مبلغ " . number_format($withdrawAmount) . " ریال را دارد. با تشکر، $companyName";
|
|
}
|
|
} catch (Exception $e) {
|
|
$message = "کاربر $userName با شماره موبایل $userMobile درخواست برداشت مبلغ " . number_format($withdrawAmount) . " ریال را دارد. با تشکر، $companyName";
|
|
}
|
|
}
|
|
User::where('send_sms', 1)->get()->each(function ($admin) use ($message) {
|
|
$this->Send($admin->mobile, $message . ' ' . now()->format('H:i:s'));
|
|
});
|
|
|
|
return response()->json($Withdraw);
|
|
}
|
|
|
|
public function withdraw_physical(Request $request)
|
|
{
|
|
$data = $request->all();
|
|
|
|
$data['customer'] = Customer::with([/*"wallet"*/])->where('id', auth('customer')->id())->first();
|
|
$data['customer']['wallet_interface'] = new WalletInterface(auth('customer')->id(), auth('customer')->user()->type);
|
|
|
|
if (!isset($data['address_id'])) {
|
|
return response()->json(['message' => 'آدرس انتخاب نشده است'], 422);
|
|
}
|
|
|
|
$data['adress'] = Address::find($data['address_id']);
|
|
if (!$data['adress'] || $data['adress']->customer_id != $data['customer']->id) {
|
|
return response()->json(['message' => 'آدرس معتبر نیست یا متعلق به شما نمیباشد'], 403);
|
|
}
|
|
foreach ($data['wallets'] as $key => $wallet) {
|
|
$data['wallets'][$key]['wallet'] = Wallet::with(["product"])->find($wallet['wallet_id']);
|
|
if (!$data['wallets'][$key]['wallet']) {
|
|
return response()->json(['message' => 'ولت مورد نظر یافت نشد.'], 422);
|
|
}
|
|
if ($data['wallets'][$key]['wallet']->owner != $data['customer']['id']) {
|
|
return response()->json(['message' => 'ولت انتخاب شده متعلق به شما نمیباشد.'], 422);
|
|
}
|
|
// بررسی وجود محصول در ولت
|
|
if (!$data['wallets'][$key]['wallet']['product']) {
|
|
return response()->json(['message' => 'محصولی در ولت مورد نظر یافت نشد.'], 422);
|
|
}
|
|
if ($data['wallets'][$key]['value'] > $data['wallets'][$key]['wallet']['gold']){
|
|
return response()->json([
|
|
'message' => "شما به اندازه کافی از {$data['wallets'][$key]['wallet']['product']['title']} ندارید"
|
|
], 422);
|
|
}
|
|
|
|
if ($data['wallets'][$key]['value'] == 0) {
|
|
return response()->json([
|
|
'message' => "مقدار انتخابی نمیتواند صفر باشد. لطفاً مقدار صحیحی را برای {$data['wallets'][$key]['wallet']['product']['title']} وارد کنید."
|
|
], 422);
|
|
}
|
|
|
|
|
|
$data['wallets'][$key]['wallet_interface'] = new WalletInterface(auth('customer')->id(), auth('customer')->user()->type, null, $data['wallets'][$key]['wallet']["product_id"]);
|
|
}
|
|
$data['withdraw'] = new Withdraw(); // Create a new instance of the Withdraw model
|
|
$data['withdraw']->note = "برداشت فیزیکی"; // 50 for phisicals
|
|
$data['withdraw']->address_id = $data['address_id'];
|
|
$data['withdraw']->customer_id = $data['customer']['id'];
|
|
$data['withdraw']->sts = 50;
|
|
$data['withdraw']->save();
|
|
foreach ($data['wallets'] as $key => $wallet) {
|
|
$data['wallets'][$key]['transaction'][] = new TransactionInterface($data['wallets'][$key]['wallet_interface'], $_inp = [
|
|
"gold" => $data['wallets'][$key]['value'],
|
|
"number" => $data['wallets'][$key]['wallet']['product']['amount']*$data['wallets'][$key]['value'],
|
|
"product_id" => $data['wallets'][$key]['wallet']['product']['id'],
|
|
'expire_at' => now(),
|
|
'active' => true,
|
|
'title' => " برداشت {$data['wallets'][$key]['wallet']['product']['title']}",
|
|
'note' => " برداشت محصول {$data['wallets'][$key]['wallet']['product']['title']} شماره سند {$data['withdraw']->id}",
|
|
'withdraw_id' => $data['withdraw']->id,
|
|
], 6, false);
|
|
}
|
|
|
|
return response()->json($data['withdraw']['id']);
|
|
}
|
|
public function indexWithdraw(Request $request){
|
|
$records = Withdraw::with(['customer','bank_account','Address',"Transaction"])->where("customer_id",auth('customer')->id());
|
|
TableEngine::create($records, $request->all())
|
|
->initSearch(['customer.name'])
|
|
->select([
|
|
'*',Withdraw::$SELECT_STS_STR,Withdraw::$SELECT_DAJ
|
|
])->export($records);
|
|
return response()->json($records);
|
|
}
|
|
public function index_withdraw_physical(Request $request){
|
|
$records = Withdraw::with(['customer','bank_account','Address',"Transaction.product"])->where("customer_id",auth('customer')->id())->whereBetween('sts', [49, 70]);
|
|
TableEngine::create($records, $request->all())
|
|
->initSearch(['customer.name'])
|
|
->select([
|
|
'*',Withdraw::$SELECT_STS_STR,Withdraw::$SELECT_DAJ
|
|
])->export($records);
|
|
return response()->json($records);
|
|
}
|
|
|
|
public function withdrawOtp(Request $request)
|
|
{
|
|
$customer = auth('customer')->user();
|
|
$otp = rand(1000, 9999);
|
|
$customer->token_sms = $otp;
|
|
$customer->save();
|
|
$this->Send(
|
|
$customer->mobile,
|
|
'رمز عبور یکبار مصرف شما جهت برداشت پول از اپلیکیشن ' . $this->Config('name_fa') . ' می باشد: ' . $otp
|
|
);
|
|
return response()->json(true);
|
|
}
|
|
|
|
}
|