Files
boursecu_server/app/Http/Controllers/Customer/CustomerController.php
T

381 lines
19 KiB
PHP
Raw Normal View History

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;
2026-04-26 20:14:23 +00:00
use App\Models\Cnt;
2026-01-24 14:03:11 +00:00
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;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Http;
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-01-24 14:03:11 +00:00
//use App\Models\Price;
//use Carbon\Carbon;
//use Illuminate\Support\Facades\Http;
class CustomerController extends Controller
{
public function me()
{
2026-01-31 09:09:32 +00:00
$select = ['*',Customer::$SELECT_CAJ,Customer::$SELECT_TYPE_STR];
$user = Customer::with(['customer_settings.customer',"agent"])->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();
$wallet->gold_ab = round(Wallet::where('owner', $user->id)->where('product_id', null)->sum('gold'), 2);
$wallet->wp = Wallet::with('product')->where('owner', $user->id)->get();
foreach ($wallet->wp as $row){
if ($row->product_id != null){
$wallet->treasure +=$row->product->amount*$row->gold;
}
$wallet->treasure_sum =$user->gold+$wallet->treasure;
}
$wallet->gold_sum = $wallet->treasure_sum+$wallet->gold_ab;
$user->wallets = Wallet::with(['product'])->where('owner', $user->id)->get() ?? new Wallet();
$count_gold =0;
2026-02-16 15:48:58 +03:30
$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-01-24 14:03:11 +00:00
$count_product =0;
foreach ($user->wallets as $walletSingle) {
if ($walletSingle->product_id) {
2026-02-16 15:48:58 +03:30
// محاسبه برای طلا (metal_id = 1)
if ($walletSingle->product->metal_id == 1) {
2026-01-24 14:03:11 +00:00
if (!($walletSingle->product->group_type == "سکه")){
(float)$count_gold += $walletSingle->product->amount*$walletSingle->gold;
}
2026-02-16 15:48:58 +03:30
$count_cash_from_gold += $walletSingle->product->price * $walletSingle->gold;
$count_product += $walletSingle->gold;
}
// محاسبه برای نقره (metal_id = 2)
elseif ($walletSingle->product->metal_id == 2) {
if (!($walletSingle->product->group_type == "سکه")){
(float)$count_silver += $walletSingle->product->amount * $walletSingle->gold;
}
$count_cash_from_silver += ($walletSingle->product->price * $walletSingle->product->amount) * $walletSingle->gold;
$count_product += $walletSingle->gold;
}
// محاسبه برای مس (metal_id = 3)
elseif ($walletSingle->product->metal_id == 3) {
if (!($walletSingle->product->group_type == "سکه")){
(float)$count_copper += $walletSingle->product->amount * $walletSingle->gold;
}
$count_cash_from_copper += ($walletSingle->product->price * $walletSingle->product->amount) * $walletSingle->gold;
$count_product += $walletSingle->gold;
}
2026-01-24 14:03:11 +00:00
} else {
(float)$count_gold += $walletSingle->gold;
2026-02-16 15:48:58 +03:30
(float)$count_cash_pure += $walletSingle->cash;
2026-01-24 14:03:11 +00:00
$wallet->count_cash_wallet = $walletSingle->cash;
$wallet->count_gold_wallet = $walletSingle->gold;
}
}
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;
$wallet->count_cash_wallets = $count_cash_total - $wallet->count_cash_wallet;
$wallet->count_gold_wallets = $count_gold - $wallet->count_gold_wallet;
2026-01-24 14:03:11 +00:00
$wallet->count_gold = $count_gold;
2026-02-16 15:48:58 +03:30
$wallet->count_silver = $count_silver;
$wallet->count_copper = $count_copper;
$wallet->count_cash = $count_cash_pure;
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-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
->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());
$updateData = $request->except(['referral_id', 'referral_to_id']);
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-01-24 14:03:11 +00:00
if (!$customer->is_legal) {
2026-04-26 20:14:23 +00:00
$counter = Cnt::where('key', 'jibit_counter')->first();
if(empty($counter)){
$counter = new Cnt();
$counter->key = "jibit_counter";
$counter->value = 0;
$counter->save();
}
$nationalCode = $customer->n_code;
$mobileNumber = $customer->mobile;
// Storage::put('accessTokenGibit', ['accessToken']);
// Storage::put('refreshTokenGibit', ['refreshToken']);
$response = Http::withoutVerifying()->withToken(Storage::get('accessTokenGibit'))
->get("https://napi.jibit.ir/ide/v1/services/matching?nationalCode=".$nationalCode."&mobileNumber=".$mobileNumber)->json();
if (isset($response['code']) && $response['code']== "forbidden"){
$response = Http::withoutVerifying()->withToken(Storage::get('refreshTokenGibit'))
->post("https://napi.jibit.ir/ide/v1/tokens/generate",[
'apiKey' => "J5WxrPxPOg",
'secretKey' => "7dgeE70xQXsQL7r36j3wif6X9",
])->json();
Storage::put('accessTokenGibit', $response['accessToken']);
Storage::put('refreshTokenGibit', $response['refreshToken']);
$response = Http::withoutVerifying()->withToken(Storage::get('accessTokenGibit'))
->get("https://napi.jibit.ir/ide/v1/services/matching?nationalCode=".$nationalCode."&mobileNumber=".$mobileNumber)->json();
}
2026-04-26 20:14:23 +00:00
//update jibit counter
$counter->value = (int)$counter->value + 1;
$counter->save();
// $response = Http::withHeaders([
// 'Authorization' => 'Bearer mob83EBzqU7GWBmnc-6gXIp6r2G57P_94',
// 'Content-Type' => 'application/json',
// ])->post('http://185.97.116.24:20205/check_user', [
// 'number' => $customer->mobile,
// 'national' => $customer->n_code,
// 'host' => $_SERVER['HTTP_HOST'],
// ]);
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') {
$gift_for_inviter = $this->Config('gift_for_referrer');
$gift_for_invited = $this->Config('gift_for_referral');
2026-01-24 14:03:11 +00:00
$companyName = $this->Config('name_fa');
$companyurl = $this->Config('app_url');
$referrer = $customer->referral;
2026-04-30 15:36:04 +00:00
$total_referrals = Customer::where('referral_to_id', $referrer->referral_id)->where('is_legal', '!=', 0)->get()->count();
$referral_limit = Setting::where('key', 'referral_limit')->first();
$gift_permision = $referral_limit->is_active ? $total_referrals < $referral_limit->value ? true : false : true ;
2026-04-30 15:36:04 +00:00
if ($gift_permision && $gift_for_inviter > 0) {
2026-04-23 15:18:43 +00:00
// dd($gift_for_inviter > 0, $gift_for_invited > 0);
2026-01-24 14:03:11 +00:00
$Withdraw = Withdraw::create([
'user_id' => $customer->id,
'customer_id' => $customer->referral->id,
"cash" => 0,
2026-04-23 15:18:43 +00:00
"gold" => $gift_for_inviter,
2026-01-24 14:03:11 +00:00
'sts' => 5,
]);
$wallet_interface = new WalletInterface($customer->referral->id, 2);
new TransactionInterface($wallet_interface, $_inp = [
"cash" => 0,
2026-04-23 15:18:43 +00:00
"gold" => $gift_for_inviter,
2026-01-24 14:03:11 +00:00
'expire_at' => now(),
'active' => true,
'title' => 'پاداش بابت معرفی',
'withdraw_id' => $Withdraw->id,
'start_at' => now(),
], 8, true, false);
$transaction = Transaction::where('customer_id', $customer->referral->id)->latest()->first();
TransactionsAdmin::create([
'customer_id' => $customer->id,
'customer_to_id' => $customer->referral->id,
'user_id' => $customer->id,
'wallet' => null, // یا مقدار مناسب دیگری
'transaction_id' => $transaction->id,
'cash' => 0,
'note' => 'پاداش بابت معرفی',
'type' => 1, // یا مقدار مناسب دیگری
2026-04-23 15:18:43 +00:00
'gold' => $gift_for_inviter,
2026-01-24 14:03:11 +00:00
'balance_cash' => 0, // یا مقدار مناسب دیگری
'withdraw_id' => $Withdraw->id,
]);
$referrerWallet = Wallet::where('owner', $customer->referral->id)->first();
2026-05-28 11:53:27 +03:30
if($this->Config('send_to_tahesab') == "true"){
try {
$tahesab = new TahesabController();
$tahesab->walletGold([
'customer_id'=> $customer->id,
'value'=> $gift_for_inviter,
'direction'=> 0
]);
}catch (\Exception $e){
return response($e->getMessage(), 500);
}
}
2026-04-23 15:18:43 +00:00
$message = "سلام {$referrer->name} عزیز،\n\n{$gift_for_inviter} گرم طلا بابت معرفی {$customer->name} به حساب شما واریز شد.\nموجودی فعلی: {$referrerWallet->gold} گرم.\n\nاطلاعات بیشتر: $companyurl\n\nبا احترام،\n$companyName";
2026-01-24 14:03:11 +00:00
$this->Send($referrer->mobile, $message);
}
2026-04-23 15:18:43 +00:00
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);
2026-01-24 14:03:11 +00:00
2026-04-23 15:18:43 +00:00
$transaction = Transaction::where('customer_id', $customer->id)->latest()->first();
TransactionsAdmin::create([
'customer_id' => $customer->id,
'customer_to_id' => $customer->referal->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();
2026-05-28 11:53:27 +03:30
if($this->Config('send_to_tahesab') == "true"){
try {
$tahesab = new TahesabController();
$tahesab->walletGold([
'customer_id'=> $customer->id,
'value'=> $gift_for_inviter,
'direction'=> 0
]);
}catch (\Exception $e){
return response($e->getMessage(), 500);
}
}
2026-04-23 15:18:43 +00:00
$message = "سلام " . $customer->name . " عزیز،\n\n" . $gift_for_invited . " گرم طلا بابت ثبت نام " . " به حساب شما واریز شد.\n" . "موجودی فعلی: " . $referrerWallet->gold . " گرم.\n\n" . "اطلاعات بیشتر: " . $companyurl . "\n\n" . "با احترام،\n" . $companyName;
$this->Send($customer->mobile, $message);
}
2026-01-24 14:03:11 +00:00
}
} else {
// This block will handle the case where the code doesn't match
// if (isset($response['code']) and $response['code'] === 'mobileNumber.not_valid') {
// return response()->json(['message' => "کد ملی تطابق ندارد",'response' =>$response], 422);
// } else {
// // This block will handle other errors from the $response variable
// return response()->json(['response' => $response], 422);
// }
return response()->json(['message' => "کد ملی ثبت شد، اما در حال حاضر سیستم نتوانست آن را تأیید کند. لطفاً منتظر بررسی و تأیید مدیر بمانید.",'response' =>$response], 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);
}
}