getTable(), 'agent_id')) { $with[] = 'agent'; } $query = Customer::query(); if ($with !== []) { $query->with($with); } $user = $query->select($select)->findOrFail(auth('customer')->id()); $wallet = Wallet::where('owner', $user->id) ->where('owner_type', $user->type) ->whereNull('fund_id') ->first() ?? new Wallet(); // کیف‌های قدیمیِ محصولات متصل به کیف اصلی (main_wallet_products) در محاسبات نمی‌آیند؛ موجودی‌شان همان gold کیف اصلی است $mainWalletProductIds = app(\App\Services\ConfigService::class)->mainWalletProductIds(); $treasureGrams = 0; $wallet->wp = Wallet::with('product')->where('owner', $user->id)->get(); foreach ($wallet->wp as $row) { if ($row->product_id != null && !in_array((int) $row->product_id, $mainWalletProductIds, true)) { $treasureGrams += self::productLineWeightToGrams($row->product, $row->gold); } } $goldAbGrams = self::weightToGrams((float) Wallet::where('owner', $user->id)->where('product_id', null)->sum('gold')); $userGoldGrams = self::weightToGrams((float) $user->gold); $wallet->treasure = round(self::gramsToTradeUnit($treasureGrams), 3); // وقتی main_wallet_products فعال است، customers.gold (legacy) همان طلای کیف اصلی است // و نباید دوباره به treasure_sum اضافه شود — وگرنه gold_sum بعد از رفرش ~۱ گرم بیشتر نشان می‌دهد. if ($mainWalletProductIds !== []) { $wallet->treasure_sum = round(self::gramsToTradeUnit($treasureGrams), 3); } else { $wallet->treasure_sum = round(self::gramsToTradeUnit($userGoldGrams + $treasureGrams), 3); } $wallet->gold_ab = round(self::gramsToTradeUnit($goldAbGrams), 2); $user->wallets = Wallet::with(['product'])->where('owner', $user->id)->get() ?? new Wallet(); $count_gold = 0; $count_silver = 0; $count_copper = 0; $count_cash_from_gold = 0; $count_cash_from_silver = 0; $count_cash_from_copper = 0; $count_cash_pure = 0; $count_product = 0; foreach ($user->wallets as $walletSingle) { if ($walletSingle->product_id) { if (in_array((int) $walletSingle->product_id, $mainWalletProductIds, true)) { continue; } if ($walletSingle->product->metal_id == 1) { if (!($walletSingle->product->group_type == "سکه")) { $count_gold += self::productLineWeightToGrams($walletSingle->product, $walletSingle->gold); } $count_cash_from_gold += $walletSingle->product->price * $walletSingle->gold; $count_product += $walletSingle->gold; } elseif ($walletSingle->product->metal_id == 2) { if (!($walletSingle->product->group_type == "سکه")) { $count_silver += self::productLineWeightToGrams($walletSingle->product, $walletSingle->gold); } $count_cash_from_silver += ($walletSingle->product->price * $walletSingle->product->amount) * $walletSingle->gold; $count_product += $walletSingle->gold; } elseif ($walletSingle->product->metal_id == 3) { if (!($walletSingle->product->group_type == "سکه")) { $count_copper += self::productLineWeightToGrams($walletSingle->product, $walletSingle->gold); } $count_cash_from_copper += ($walletSingle->product->price * $walletSingle->product->amount) * $walletSingle->gold; $count_product += $walletSingle->gold; } } else { $count_gold += self::weightToGrams((float) $walletSingle->gold); $count_cash_pure += $walletSingle->cash; $wallet->count_cash_wallet = $walletSingle->cash; $wallet->count_gold_wallet = round(self::gramsToTradeUnit(self::weightToGrams((float) $walletSingle->gold)), 3); } } $count_cash_total = $count_cash_from_gold + $count_cash_from_silver + $count_cash_from_copper + $count_cash_pure; $wallet->count_cash_wallets = $count_cash_total - $wallet->count_cash_wallet; $wallet->count_gold = round(self::gramsToTradeUnit($count_gold), 3); $wallet->count_gold_wallets = round($wallet->count_gold - $wallet->count_gold_wallet, 3); $wallet->count_silver = round(self::gramsToTradeUnit($count_silver), 3); $wallet->count_copper = round(self::gramsToTradeUnit($count_copper), 3); $wallet->count_cash = $count_cash_pure; $wallet->count_product = $count_product; $wallet->cash_from_gold = $count_cash_from_gold; $wallet->cash_from_silver = $count_cash_from_silver; $wallet->cash_from_copper = $count_cash_from_copper; $wallet->count_cash_total = $count_cash_total; // cash در پاسخ = موجودی ریالی کیف اصلی (نه کیف legacy محصولات main_wallet_products) $wallet->cash = $count_cash_pure; // جمع واقعی طلا از کیف‌ها — هم‌تراز wallet/me و کارت محصول (بدون دوباره‌شماری customers.gold) $wallet->gold_sum = round((float) $wallet->count_gold, 2); $user->wallet = $wallet; $user->pwallet = Wallet::with("product")->where('owner', $user->id) ->where('owner_type', $user->type) // Ensures the wallet's owner type matches the user's type ->whereNotNull('product_id') // Filters wallets that have a non-null product_id ->when($mainWalletProductIds !== [], function ($q) use ($mainWalletProductIds) { // کیف قدیمی محصولات متصل به کیف اصلی نمایش داده نمی‌شود $q->whereNotIn('product_id', $mainWalletProductIds); }) ->get() ?? new Wallet(); // If no wallet is found, assigns a new Wallet instance to the attribute $forbiddenWords = ['tahesab', 'token']; // کلمات ممنوعه $query = Setting::query(); // حذف رکوردهایی که key شامل کلمات ممنوعه است foreach ($forbiddenWords as $word) { $query->where('key', 'not like', "%{$word}%"); }$settings = $query->get(); $user->settings= $settings; return response()->json($user); } public function updateMe(Request $request) { try { $customer = Customer::findOrFail(auth('customer')->id()); $wasLegal = (int) $customer->is_legal; $updateData = $request->except(['referral_id', 'referral_to_id', 'is_legal']); if (isset($request->fname) && isset($request->lname)) { $updateData['name'] = "$request->fname $request->lname"; } if ($request->has('credential_id')) { $updateData['credential_id'] = $request->credential_id; } $customer->update($updateData); // $tah = new TahesabController(); // $tah->store(["customer_id" => $customer->id]); $cfg = app(ConfigService::class); if (!$wasLegal && $cfg->getBool('register_jibit_verify', false)) { $response = $this->jibitNcode($customer->n_code, $customer->mobile); if (isset($response['matched']) and $response['matched'] == true) { $customer->is_legal = 1; $customer->save(); if ($this->Config('has_page_earn_money') == 'true') { $gift_for_inviter = $this->Config('gift_for_referrer'); $gift_for_invited = $this->Config('gift_for_referral'); $companyName = $this->Config('name_fa'); $companyurl = $this->Config('app_url'); $referrer = $customer->referrer(); $gift_permision = true; if ($referrer) { $referral_limit = Setting::where('key', 'referral_limit')->first(); $limitValue = $referral_limit->value ?? null; if ( $referral_limit && $referral_limit->is_active && $limitValue !== null && $limitValue !== '' && is_numeric($limitValue) ) { $total_referrals = Customer::where('referral_to_id', $referrer->referral_id) ->where('is_legal', '!=', 0) ->count(); $gift_permision = $total_referrals < (int) $limitValue; } } if ($referrer && $gift_permision && $gift_for_inviter > 0) { // dd($gift_for_inviter > 0, $gift_for_invited > 0); $Withdraw = Withdraw::create([ 'user_id' => $customer->id, 'customer_id' => $referrer->id, "cash" => 0, "gold" => $gift_for_inviter, 'sts' => 5, ]); $wallet_interface = new WalletInterface($referrer->id, 2); new TransactionInterface($wallet_interface, $_inp = [ "cash" => 0, "gold" => $gift_for_inviter, 'expire_at' => now(), 'active' => true, 'title' => 'پاداش بابت معرفی', 'withdraw_id' => $Withdraw->id, 'start_at' => now(), ], 8, true, false); $transaction = Transaction::where('customer_id', $referrer->id)->latest()->first(); TransactionsAdmin::create([ 'customer_id' => $customer->id, 'customer_to_id' => $referrer->id, 'user_id' => $customer->id, 'wallet' => null, // یا مقدار مناسب دیگری 'transaction_id' => $transaction->id, 'cash' => 0, 'note' => 'پاداش بابت معرفی', 'type' => 1, // یا مقدار مناسب دیگری 'gold' => $gift_for_inviter, 'balance_cash' => 0, // یا مقدار مناسب دیگری 'withdraw_id' => $Withdraw->id, ]); $referrerWallet = Wallet::where('owner', $referrer->id)->first(); if($this->Config('send_to_tahesab') == "true"){ try { $tahesab = new TahesabController(); $tahesab->referrerGoldReward( $customer, (float) $gift_for_inviter, 'پاداش بابت معرفی' ); }catch (\Exception $e){ return response($e->getMessage(), 500); } } $message = "سلام {$referrer->name} عزیز،\n\n{$gift_for_inviter} گرم طلا بابت معرفی {$customer->name} به حساب شما واریز شد.\nموجودی فعلی: {$referrerWallet->gold} گرم.\n\nاطلاعات بیشتر: $companyurl\n\nبا احترام،\n$companyName"; $this->Send($referrer->mobile, $message); } if($gift_for_invited > 0 ){ $Withdraw = Withdraw::create([ 'user_id' => $customer->id, 'customer_id' => $customer->id, "cash" => 0, "gold" => $gift_for_invited, 'sts' => 5, ]); $wallet_interface = new WalletInterface($customer->id, 2); new TransactionInterface($wallet_interface, $_inp = [ "cash" => 0, "gold" => $gift_for_invited, 'expire_at' => now(), 'active' => true, 'title' => 'پاداش بابت ثبت نام', 'withdraw_id' => $Withdraw->id, 'start_at' => now(), ], 8, true, false); $transaction = Transaction::where('customer_id', $customer->id)->latest()->first(); TransactionsAdmin::create([ 'customer_id' => $customer->id, 'customer_to_id' => $customer->id, 'user_id' => $customer->id, 'wallet' => null, // یا مقدار مناسب دیگری 'transaction_id' => $transaction->id, 'cash' => 0, 'note' => 'پاداش بابت ثبت نام', 'type' => 1, // یا مقدار مناسب دیگری 'gold' => (float)$gift_for_invited, 'balance_cash' => 0, // یا مقدار مناسب دیگری 'withdraw_id' => $Withdraw->id, ]); $referrerWallet = Wallet::where('owner', $customer->id)->first(); if($this->Config('send_to_tahesab') == "true"){ try { $tahesab = new TahesabController(); $tahesab->goldReward([ 'customer_id' => $customer->id, 'value' => $gift_for_invited, 'desc' => 'پاداش بابت ثبت نام', ]); }catch (\Exception $e){ return response($e->getMessage(), 500); } } $message = "سلام " . $customer->name . " عزیز،\n\n" . $gift_for_invited . " گرم طلا بابت ثبت نام " . " به حساب شما واریز شد.\n" . "موجودی فعلی: " . $referrerWallet->gold . " گرم.\n\n" . "اطلاعات بیشتر: " . $companyurl . "\n\n" . "با احترام،\n" . $companyName; $this->Send($customer->mobile, $message); } } } else { if (isset($response['matched']) && $response['matched'] === false) { return response()->json([ 'message' => 'کد ملی و شماره موبایل با یکدیگر مطابقت ندارند. لطفاً هر دو را با دقت بررسی کرده و دوباره ثبت کنید.', ], 422); } return response()->json([ 'message' => 'کد ملی ثبت شد، اما در حال حاضر سیستم نتوانست آن را تأیید کند. لطفاً منتظر بررسی و تأیید مدیر بمانید.', ], 422); } } elseif (!$customer->is_legal && !$cfg->getBool('register_jibit_verify', false)) { return response()->json([ 'message' => 'اطلاعات شما با موفقیت ثبت شد. لطفاً برای تکمیل احراز هویت با پشتیبانی یا مدیر سامانه تماس بگیرید.', ], 422); } return response()->json(true); } catch (\Exception $e) { // نمایش 400 کاراکتر اول از پیام خطا $errorMessage = substr($e->getMessage(), 0, 400); return response()->json(['error' => $errorMessage], 500); } } public function active_finger() { $customer = Customer::findOrFail(auth('customer')->id()); if ($customer->finger_print) { $customer->finger_print = false; // Set to false if currently true } else { $customer->finger_print = true; // Set to true if currently false } $customer->save(); return response()->json($customer); } public function imFrom($code) { $referrer = Customer::where('referral_id', $code)->first(); if ($referrer) { // Define the reward amount $rewardAmount = 200000; // Adjusted reward amount as per the new requirement // Update the referred customer with the referral code $newCustomer = auth('customer')->user(); // Assuming the new customer is authenticated $newCustomer->referral_id = $code; $newCustomer->save(); // Construct the SMS message $message = sprintf( "%s با کد معرف شما ثبت نام کرد.\nکیف پول شما %d ریال به عنوان پاداش شارژ شد.\n%s", $newCustomer->name, $rewardAmount, $this->Config('name_fa') ); // Send SMS to the referrer $this->Send($referrer->mobile, $message); return response()->json(['message' => 'Referral reward processed and SMS sent to referrer.'], 200); } else { return response()->json(['error' => 'کد نامعتبر میباشد'], 422); } } public function i_have(Request $request) { $user = auth('customer')->user(); $customer_referral_id = Customer::where("id", $user['id'])->first()->referral_id; $return['users'] = Customer::where("referral_to_id", $customer_referral_id)->get(); $return['gifts'] = TransactionsAdmin::with(['withdraw', 'order', "user", 'customerTo', 'customer'])->where("customer_to_id", $user->id)->get(); $return['usersSum'] = $return['users']->count(); $return['giftsSum'] = number_format($return['gifts']->sum('gold'), 3); return response()->json($return); } }