2026-01-24 14:03:11 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Customer;
|
|
|
|
|
|
|
|
|
|
use App\DB\Te\TableEngine;
|
|
|
|
|
use App\Http\Controllers\Admin\CustomerController as adm_cu;
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
use App\Models\Address;
|
|
|
|
|
use App\Models\Customer;
|
|
|
|
|
use App\Models\User;
|
|
|
|
|
use App\Models\TransactionsAdmin;
|
|
|
|
|
use App\Models\Withdraw;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Rooyesh\Wallet\interfaces\TransactionInterface;
|
|
|
|
|
use Rooyesh\Wallet\interfaces\WalletInterface;
|
|
|
|
|
use Rooyesh\Wallet\models\Wallet;
|
|
|
|
|
use Rooyesh\Wallet\models\Transaction;
|
2026-01-31 09:09:32 +00:00
|
|
|
use App\Models\Setting;
|
2026-02-08 08:02:11 +00:00
|
|
|
use App\Http\Controllers\Admin\TahesabController;
|
2026-06-06 13:59:49 +03:30
|
|
|
use App\Services\ConfigService;
|
2026-07-01 22:27:51 +03:30
|
|
|
use App\Services\ReferralGiftService;
|
2026-07-05 13:04:00 +03:30
|
|
|
use App\Services\WalletBalanceService;
|
2026-07-16 11:36:57 +03:30
|
|
|
use App\Support\PersianDigits;
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
|
use Illuminate\Support\Facades\Log;
|
2026-06-04 12:26:12 +03:30
|
|
|
use Illuminate\Support\Facades\Schema;
|
2026-01-24 14:03:11 +00:00
|
|
|
|
|
|
|
|
class CustomerController extends Controller
|
|
|
|
|
{
|
|
|
|
|
public function me()
|
|
|
|
|
{
|
2026-06-04 12:26:12 +03:30
|
|
|
$select = ['*', Customer::$SELECT_CAJ, Customer::$SELECT_TYPE_STR];
|
|
|
|
|
|
|
|
|
|
$with = [];
|
|
|
|
|
if (Schema::hasTable('customers_settings')) {
|
|
|
|
|
$with[] = 'customer_settings.customer';
|
|
|
|
|
}
|
|
|
|
|
if (Schema::hasColumn((new Customer)->getTable(), 'agent_id')) {
|
|
|
|
|
$with[] = 'agent';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$query = Customer::query();
|
|
|
|
|
if ($with !== []) {
|
|
|
|
|
$query->with($with);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$user = $query->select($select)->findOrFail(auth('customer')->id());
|
2026-01-24 14:03:11 +00:00
|
|
|
|
|
|
|
|
$wallet = Wallet::where('owner', $user->id)
|
|
|
|
|
->where('owner_type', $user->type)
|
|
|
|
|
->whereNull('fund_id')
|
|
|
|
|
->first() ?? new Wallet();
|
2026-06-09 12:43:41 +03:30
|
|
|
|
2026-06-10 17:31:38 +03:30
|
|
|
// کیفهای قدیمیِ محصولات متصل به کیف اصلی (main_wallet_products) در محاسبات نمیآیند؛ موجودیشان همان gold کیف اصلی است
|
|
|
|
|
$mainWalletProductIds = app(\App\Services\ConfigService::class)->mainWalletProductIds();
|
|
|
|
|
|
2026-06-09 12:43:41 +03:30
|
|
|
$treasureGrams = 0;
|
2026-07-05 13:04:00 +03:30
|
|
|
$wallet->wp = Wallet::with('product')
|
|
|
|
|
->where('owner', $user->id)
|
|
|
|
|
->where('owner_type', $user->type)
|
|
|
|
|
->get();
|
2026-06-09 12:43:41 +03:30
|
|
|
foreach ($wallet->wp as $row) {
|
2026-06-10 17:31:38 +03:30
|
|
|
if ($row->product_id != null && !in_array((int) $row->product_id, $mainWalletProductIds, true)) {
|
2026-06-09 12:43:41 +03:30
|
|
|
$treasureGrams += self::productLineWeightToGrams($row->product, $row->gold);
|
2026-01-24 14:03:11 +00:00
|
|
|
}
|
|
|
|
|
}
|
2026-06-09 12:43:41 +03:30
|
|
|
|
2026-07-05 13:04:00 +03:30
|
|
|
$goldAbGrams = self::weightToGrams((float) Wallet::where('owner', $user->id)
|
|
|
|
|
->where('owner_type', $user->type)
|
|
|
|
|
->where('product_id', null)
|
|
|
|
|
->sum('gold'));
|
2026-06-09 12:43:41 +03:30
|
|
|
$userGoldGrams = self::weightToGrams((float) $user->gold);
|
|
|
|
|
|
|
|
|
|
$wallet->treasure = round(self::gramsToTradeUnit($treasureGrams), 3);
|
2026-06-11 14:54:30 +03:30
|
|
|
// وقتی 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);
|
|
|
|
|
}
|
2026-06-09 12:43:41 +03:30
|
|
|
$wallet->gold_ab = round(self::gramsToTradeUnit($goldAbGrams), 2);
|
2026-01-24 14:03:11 +00:00
|
|
|
|
2026-07-05 13:04:00 +03:30
|
|
|
$user->wallets = Wallet::with(['product'])
|
|
|
|
|
->where('owner', $user->id)
|
|
|
|
|
->where('owner_type', $user->type)
|
|
|
|
|
->get() ?? new Wallet();
|
2026-01-24 14:03:11 +00:00
|
|
|
|
2026-06-09 12:43:41 +03:30
|
|
|
$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;
|
2026-02-16 15:48:58 +03:30
|
|
|
|
2026-06-09 12:43:41 +03:30
|
|
|
$count_product = 0;
|
2026-01-24 14:03:11 +00:00
|
|
|
foreach ($user->wallets as $walletSingle) {
|
|
|
|
|
if ($walletSingle->product_id) {
|
2026-06-10 17:31:38 +03:30
|
|
|
if (in_array((int) $walletSingle->product_id, $mainWalletProductIds, true)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2026-02-16 15:48:58 +03:30
|
|
|
|
|
|
|
|
if ($walletSingle->product->metal_id == 1) {
|
2026-06-09 12:43:41 +03:30
|
|
|
if (!($walletSingle->product->group_type == "سکه")) {
|
|
|
|
|
$count_gold += self::productLineWeightToGrams($walletSingle->product, $walletSingle->gold);
|
|
|
|
|
}
|
2026-02-16 15:48:58 +03:30
|
|
|
$count_cash_from_gold += $walletSingle->product->price * $walletSingle->gold;
|
|
|
|
|
$count_product += $walletSingle->gold;
|
2026-06-09 12:43:41 +03:30
|
|
|
} elseif ($walletSingle->product->metal_id == 2) {
|
|
|
|
|
if (!($walletSingle->product->group_type == "سکه")) {
|
|
|
|
|
$count_silver += self::productLineWeightToGrams($walletSingle->product, $walletSingle->gold);
|
2026-02-16 15:48:58 +03:30
|
|
|
}
|
2026-06-17 14:05:44 +03:30
|
|
|
$count_cash_from_silver += ($walletSingle->product->price * self::productPriceMultiplier($walletSingle->product)) * $walletSingle->gold;
|
2026-02-16 15:48:58 +03:30
|
|
|
$count_product += $walletSingle->gold;
|
2026-06-09 12:43:41 +03:30
|
|
|
} elseif ($walletSingle->product->metal_id == 3) {
|
|
|
|
|
if (!($walletSingle->product->group_type == "سکه")) {
|
|
|
|
|
$count_copper += self::productLineWeightToGrams($walletSingle->product, $walletSingle->gold);
|
2026-02-16 15:48:58 +03:30
|
|
|
}
|
2026-06-17 14:05:44 +03:30
|
|
|
$count_cash_from_copper += ($walletSingle->product->price * self::productPriceMultiplier($walletSingle->product)) * $walletSingle->gold;
|
2026-02-16 15:48:58 +03:30
|
|
|
$count_product += $walletSingle->gold;
|
|
|
|
|
}
|
2026-07-05 13:04:00 +03:30
|
|
|
} elseif ($walletSingle->fund_id === null) {
|
2026-06-09 12:43:41 +03:30
|
|
|
$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);
|
2026-01-24 14:03:11 +00:00
|
|
|
}
|
|
|
|
|
}
|
2026-02-16 15:48:58 +03:30
|
|
|
|
2026-06-15 12:05:36 +03:30
|
|
|
$mainWalletGoldWeight = (float) ($wallet->count_gold_wallet ?? 0);
|
|
|
|
|
if ($mainWalletGoldWeight > 0) {
|
|
|
|
|
$liveGold = (int) app(\App\Services\ProductDisplayPriceService::class)->resolveAppBasePrice();
|
|
|
|
|
if ($liveGold <= 0) {
|
|
|
|
|
$liveGold = (int) (\App\Models\Price::latest('id')->value('price') ?? 0);
|
|
|
|
|
}
|
|
|
|
|
if ($liveGold > 0) {
|
|
|
|
|
$count_cash_from_gold += $mainWalletGoldWeight * $liveGold;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-16 15:48:58 +03:30
|
|
|
$count_cash_total = $count_cash_from_gold + $count_cash_from_silver + $count_cash_from_copper + $count_cash_pure;
|
|
|
|
|
|
2026-07-05 13:04:00 +03:30
|
|
|
$mainWalletCash = app(WalletBalanceService::class)->mainWalletCash($user->wallets);
|
|
|
|
|
$wallet->count_cash_wallet = $mainWalletCash;
|
|
|
|
|
$wallet->count_cash_wallets = $count_cash_total - $mainWalletCash;
|
2026-06-09 12:43:41 +03:30
|
|
|
$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);
|
2026-01-24 14:03:11 +00:00
|
|
|
$wallet->count_product = $count_product;
|
2026-02-16 15:48:58 +03:30
|
|
|
$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;
|
2026-06-11 21:17:40 +03:30
|
|
|
// cash در پاسخ = موجودی ریالی کیف اصلی (نه کیف legacy محصولات main_wallet_products)
|
2026-07-05 13:04:00 +03:30
|
|
|
$wallet->cash = $mainWalletCash;
|
|
|
|
|
$wallet->count_cash = $mainWalletCash;
|
2026-02-16 15:48:58 +03:30
|
|
|
|
2026-06-11 14:54:30 +03:30
|
|
|
// جمع واقعی طلا از کیفها — همتراز wallet/me و کارت محصول (بدون دوبارهشماری customers.gold)
|
|
|
|
|
$wallet->gold_sum = round((float) $wallet->count_gold, 2);
|
2026-01-24 14:03:11 +00:00
|
|
|
|
|
|
|
|
$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
|
2026-06-10 17:31:38 +03:30
|
|
|
->when($mainWalletProductIds !== [], function ($q) use ($mainWalletProductIds) {
|
|
|
|
|
// کیف قدیمی محصولات متصل به کیف اصلی نمایش داده نمیشود
|
|
|
|
|
$q->whereNotIn('product_id', $mainWalletProductIds);
|
|
|
|
|
})
|
2026-01-24 14:03:11 +00:00
|
|
|
->get() ?? new Wallet(); // If no wallet is found, assigns a new Wallet instance to the attribute
|
|
|
|
|
|
2026-01-31 09:09:32 +00:00
|
|
|
$forbiddenWords = ['tahesab', 'token']; // کلمات ممنوعه
|
|
|
|
|
$query = Setting::query();
|
|
|
|
|
// حذف رکوردهایی که key شامل کلمات ممنوعه است
|
|
|
|
|
foreach ($forbiddenWords as $word) {
|
|
|
|
|
$query->where('key', 'not like', "%{$word}%");
|
|
|
|
|
}$settings = $query->get();
|
|
|
|
|
$user->settings= $settings;
|
|
|
|
|
|
2026-01-24 14:03:11 +00:00
|
|
|
return response()->json($user);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function updateMe(Request $request)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$customer = Customer::findOrFail(auth('customer')->id());
|
2026-06-07 19:23:03 +03:30
|
|
|
$wasLegal = (int) $customer->is_legal;
|
|
|
|
|
$updateData = $request->except(['referral_id', 'referral_to_id', 'is_legal']);
|
2026-07-16 11:36:57 +03:30
|
|
|
$updateData = PersianDigits::toEnglish($updateData);
|
|
|
|
|
if (!empty($updateData['birth_date'])) {
|
|
|
|
|
try {
|
|
|
|
|
$updateData['birth_date'] = Carbon::parse($updateData['birth_date'])->format('Y-m-d');
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
return response()->json(['message' => 'تاریخ تولد نامعتبر است.'], 422);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-01-24 14:03:11 +00:00
|
|
|
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);
|
2026-02-08 08:02:11 +00:00
|
|
|
|
|
|
|
|
// $tah = new TahesabController();
|
|
|
|
|
// $tah->store(["customer_id" => $customer->id]);
|
2026-06-06 13:59:49 +03:30
|
|
|
$cfg = app(ConfigService::class);
|
2026-06-07 19:23:03 +03:30
|
|
|
if (!$wasLegal && $cfg->getBool('register_jibit_verify', false)) {
|
2026-06-06 13:59:49 +03:30
|
|
|
$response = $this->jibitNcode($customer->n_code, $customer->mobile);
|
2026-01-24 14:03:11 +00:00
|
|
|
|
|
|
|
|
if (isset($response['matched']) and $response['matched'] == true) {
|
|
|
|
|
$customer->is_legal = 1;
|
|
|
|
|
$customer->save();
|
2026-04-23 15:18:43 +00:00
|
|
|
if ($this->Config('has_page_earn_money') == 'true') {
|
2026-06-07 18:27:04 +03:30
|
|
|
$referrer = $customer->referrer();
|
2026-07-01 22:27:51 +03:30
|
|
|
$gift_permision = app(ReferralGiftService::class)->resolveReferrerGiftPermission($referrer);
|
|
|
|
|
app(ReferralGiftService::class)->tryGiveOnRegistration(
|
|
|
|
|
$customer,
|
|
|
|
|
$referrer,
|
|
|
|
|
$gift_permision,
|
|
|
|
|
true
|
|
|
|
|
);
|
2026-01-24 14:03:11 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
2026-06-06 13:59:49 +03:30
|
|
|
if (isset($response['matched']) && $response['matched'] === false) {
|
|
|
|
|
return response()->json([
|
|
|
|
|
'message' => 'کد ملی و شماره موبایل با یکدیگر مطابقت ندارند. لطفاً هر دو را با دقت بررسی کرده و دوباره ثبت کنید.',
|
|
|
|
|
], 422);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
|
'message' => 'کد ملی ثبت شد، اما در حال حاضر سیستم نتوانست آن را تأیید کند. لطفاً منتظر بررسی و تأیید مدیر بمانید.',
|
|
|
|
|
], 422);
|
2026-01-24 14:03:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2026-06-06 13:59:49 +03:30
|
|
|
} elseif (!$customer->is_legal && !$cfg->getBool('register_jibit_verify', false)) {
|
|
|
|
|
return response()->json([
|
2026-06-06 16:05:40 +03:30
|
|
|
'message' => 'اطلاعات شما با موفقیت ثبت شد. لطفاً برای تکمیل احراز هویت با پشتیبانی یا مدیر سامانه تماس بگیرید.',
|
2026-06-06 13:59:49 +03:30
|
|
|
], 422);
|
2026-01-24 14:03:11 +00:00
|
|
|
}
|
|
|
|
|
return response()->json(true);
|
|
|
|
|
} catch (\Exception $e) {
|
2026-07-16 11:36:57 +03:30
|
|
|
Log::error('customer updateMe failed', [
|
|
|
|
|
'customer_id' => auth('customer')->id(),
|
|
|
|
|
'message' => $e->getMessage(),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return response()->json(['message' => 'خطا در ذخیره اطلاعات. لطفاً دوباره تلاش کنید.'], 422);
|
2026-01-24 14:03:11 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|