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, ZaarboxPriceService $zaarboxPriceService) { return response()->json([ 'code' => 0, 'message' => 'عملیات با موفقیت انجام شد.', 'data' => [ 'prices' => $zaarboxPriceService->getAllPrices(), ], ]); } 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(['*', Withdraw::$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) { // اولویت اول: واریز توسط ادمین // حتی اگر title قبلاً برداشت ریالی ذخیره شده باشد، باید اصلاح شود if ( isset($record->withdraw) && (int) $record->withdraw->sts === 4 ) { $record->title = "واریز ریالی"; // اگر می‌خوای تو دیتابیس هم اصلاح بشه $record->save(); continue; } // بقیه رکوردها فقط اگر title ندارند یا تعریف نشده هستند اصلاح شوند if (empty($record->title) || $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)) { $direction = $record->order->direction ?? null; if ((int) $direction === 1) { $record->title = self::productBuyTitle($wallet->product->title); } elseif ((int) $direction === 2) { $record->title = self::productSellTitle($wallet->product->title); } else { $record->title = ($record->gold > 0 ? self::productBuyTitle($wallet->product->title) : self::productSellTitle($wallet->product->title)); } $record->unit = "عدد"; } else { $record->title = $this->resolveMainWalletTransactionTitle($record); } } else { $record->title = $record->type_str; } $record->save(); } $this->applyMetalTypeTransactionTitle($record); $this->applyProductTransactionTitle($record); $this->applyProductOrderDisplayFields($record); } 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 if($this->Config('send_to_tahesab') == "true") { $tahesab = new TahesabController(); $result = $tahesab->wallet([ "customer_id" => auth('customer')->id(), "value" => $data['value'], "direction" => 1, ]); \Log::info('Tahesab withdraw result', [ 'customer_id' => auth('customer')->id(), 'value' => $data['value'], 'result' => $result, ]); } $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->ConfigService('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->ConfigService('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); } private function applyMetalTypeTransactionTitle($record): void { if (!empty($record->product_id)) { return; } $wallet = $record->wallet ?? null; if ($wallet && !empty($wallet->product)) { return; } if (self::isCashTransactionTitle($record->title)) { return; } if (!self::isGenericMetalTransactionTitle($record->title)) { return; } if (preg_match('/^(موجودی|واریز)/u', (string) $record->title)) { $record->title = self::meltedGoldDepositTitle(); } elseif (preg_match('/^برداشت /u', (string) $record->title)) { $record->title = self::meltedGoldWithdrawTitle(); } elseif (preg_match('/^خرید /u', (string) $record->title)) { $record->title = self::meltedGoldBuyTitle(); } elseif (preg_match('/^فروش /u', (string) $record->title)) { $record->title = self::meltedGoldSellTitle(); } } private function applyProductTransactionTitle($record): void { if (empty($record->product_id)) { return; } if (self::isCashTransactionTitle($record->title)) { return; } $productTitle = $record->wallet->product->title ?? $record->product->title ?? null; if (!$productTitle) { return; } $expectedBuy = self::productBuyTitle($productTitle); $expectedSell = self::productSellTitle($productTitle); if ($record->title === $expectedBuy || $record->title === $expectedSell) { return; } if (!self::isGenericMetalTransactionTitle($record->title)) { return; } $direction = $record->order->direction ?? null; if ((int) $direction === 1) { $record->title = $expectedBuy; return; } if ((int) $direction === 2) { $record->title = $expectedSell; } } private function resolveMainWalletTransactionTitle($record): string { if ((int) $record->type === 1) { return self::meltedGoldBuyTitle(); } if ((int) $record->type === 2) { return self::meltedGoldSellTitle(); } return $record->type_str; } private function applyProductOrderDisplayFields($record): void { if (empty($record->order_id) || empty($record->order) || empty($record->product_id)) { return; } $order = $record->order; $totalPrice = (int) ($order->total_price ?? $order->payment_amount ?? 0); if ($totalPrice <= 0) { return; } if ((int) ($record->cash ?? 0) === 0) { $direction = (int) ($order->direction ?? 0); $record->cash = $direction === 1 ? -$totalPrice : $totalPrice; } if ($this->isProductWalletTransaction($record)) { $mainBalanceCash = $this->resolveMainWalletBalanceCashForOrder($record); if ($mainBalanceCash !== null) { $record->balance_cash = $mainBalanceCash; } } if (!empty($record->wallet?->product)) { $record->unit = $record->wallet->product->unit ?: 'عدد'; } } private function isProductWalletTransaction($record): bool { return !empty($record->wallet?->product); } private function resolveMainWalletBalanceCashForOrder($record): ?int { $customerId = $record->customer_id ?? auth('customer')->id(); $mainWalletIds = Wallet::where('owner', $customerId) ->whereNull('product_id') ->whereNull('fund_id') ->pluck('id'); if ($mainWalletIds->isEmpty()) { return null; } $mainTx = Transaction::where('order_id', $record->order_id) ->whereIn('wallet', $mainWalletIds) ->whereNotNull('balance_cash') ->orderBy('id') ->first(); if ($mainTx) { return (int) $mainTx->balance_cash; } $previousTx = Transaction::whereIn('wallet', $mainWalletIds) ->where('id', '<', $record->id) ->whereNotNull('balance_cash') ->orderByDesc('id') ->first(); return $previousTx ? (int) $previousTx->balance_cash : null; } }