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

433 lines
19 KiB
PHP
Raw Normal View History

2026-01-24 14:03:11 +00:00
<?php
namespace App\Http\Controllers\Admin;
use App\DB\Te\TableEngine;
use App\Http\Controllers\SuperController;
use App\Models\Cnt;
2026-01-24 14:03:11 +00:00
use App\Models\Customer;
use App\Models\TransactionsAdmin;
use App\Models\Withdraw;
use Carbon\Carbon;
use Hekmatinasser\Verta\Verta;
2026-01-24 14:03:11 +00:00
use Illuminate\Http\Request;
use Rooyesh\Wallet\interfaces\TransactionInterface;
use Rooyesh\Wallet\interfaces\WalletInterface;
use Rooyesh\Wallet\models\Transaction;
use Rooyesh\Wallet\models\Wallet;
2026-05-28 12:52:30 +03:30
use App\Models\Setting;
2026-06-26 22:19:51 +03:30
use App\Services\AssetSummaryService;
2026-06-23 21:04:29 +03:30
use App\Services\BalanceReportService;
2026-06-15 14:38:58 +03:30
use App\Services\ConfigService;
use App\Services\WalletBalanceService;
2026-01-24 14:03:11 +00:00
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
class CustomerController extends SuperController
{
2026-06-26 22:19:51 +03:30
/** فیلدهای محصول لازم برای محاسبهٔ صحیح وزن موجودی */
private const WALLET_PRODUCT_COLUMNS = [
'id', 'metal_id', 'unit', 'amount', 'group_type', 'trade_by_quantity', 'direct_price', 'title',
];
2026-01-24 14:03:11 +00:00
public function __construct()
{
$this->with = [];
$this->select = ['*'];
$search=['name','mobile'];
$this->query = Customer::with($this->with,'wallet')->where('is_archive',false)->where('type',1);
// $this->query = Customer::where('is_archive',false)->get();
parent::__construct(Customer::class, $this->with, $this->select, 'customer', $this->query, $search);
}
/**
* @param $id
* @return \Illuminate\Http\JsonResponse
*/
public function listUser(){
$user = Customer::with(['wallet'])->select('id','name','mobile','type')->get();
return response()->json($user);
}
public function updateAdmin(Request $request , $id)
{
$data = $request->all();
if (isset($data['mobile']) && !is_null($data['mobile'])){
$check_mobile = Customer::where('mobile',$data['mobile'])
->where('id','<>',$id)
->first();
if (!is_null($check_mobile)){
throw new UnprocessableEntityHttpException('این شماره همراه توسط شخص دیگری در حال استفاده است');
}
}
if (isset($data['gold'])){
$user = Customer::find($id);
if ($user) {
$user->gold = $data['gold'];
$user->save();
$user["g"]=true;
}
}
$user["u"] = Customer::findOrFail($id)->update($data);
return response()->json($user);
}
public function referrals(Request $request,$customer_id)
{
$customer_referral_id = Customer::where("id",$customer_id)->first()->referral_id;
$query_table = Customer::where("referral_to_id",$customer_referral_id);
TableEngine::create($query_table, $request)
->initSearch($this->search)
->select(['*',Customer::$SELECT_TYPE_STR])
->export($query_table);
return response()->json($query_table);
}
public function funds(Request $request,$customer_id)
{
2026-06-10 17:31:38 +03:30
$mainWalletProductIds = app(\App\Services\ConfigService::class)->mainWalletProductIds();
2026-01-24 14:03:11 +00:00
2026-06-10 16:57:31 +03:30
// فقط کیف محصول‌ها (نه کیف اصلی) — هر ردیف = موجودی واقعی یک محصول/فلز
$query_table = Wallet::with(['product:id,title,metal_id,unit'])
->where('owner', $customer_id)
->whereNotNull('product_id');
2026-06-10 17:31:38 +03:30
if ($mainWalletProductIds !== []) {
// کیف‌های قدیمیِ محصولات متصل به کیف اصلی نمایش داده نمی‌شوند
$query_table->whereNotIn('product_id', $mainWalletProductIds);
}
2026-01-24 14:03:11 +00:00
TableEngine::create($query_table, $request)
->initSearch($this->search)
2026-06-10 16:57:31 +03:30
->select(['id', 'owner', 'cash', 'gold', 'fund_id', 'product_id', 'created_at', 'updated_at'])
2026-01-24 14:03:11 +00:00
->export($query_table);
2026-06-10 17:31:38 +03:30
// محصولات متصل به کیف اصلی: یک ردیف نماینده با موجودی کیف اصلی
if ($mainWalletProductIds !== []) {
$representative = null;
foreach ($mainWalletProductIds as $pid) {
$representative = \App\Models\Product::select(['id', 'title', 'metal_id', 'unit'])->find($pid);
if ($representative) {
break;
}
}
$mainWallet = Wallet::where('owner', $customer_id)
->whereNull('fund_id')
->whereNull('product_id')
->first();
if ($representative && $mainWallet) {
$mainRow = [
'id' => $mainWallet->id,
'owner' => (int) $customer_id,
'cash' => 0,
'gold' => (float) $mainWallet->gold,
'fund_id' => null,
'product_id' => (int) $representative->id,
'created_at' => $mainWallet->created_at,
'updated_at' => $mainWallet->updated_at,
'product' => $representative,
'is_main_wallet' => true,
];
$query_table['rows'] = collect($query_table['rows'] ?? [])->prepend($mainRow)->values();
$query_table['query']['total'] = (int) ($query_table['query']['total'] ?? 0) + 1;
}
}
2026-01-24 14:03:11 +00:00
return response()->json($query_table);
}
public function archive($id){
$query = Customer::findOrFail($id);
$query->update([
'is_archive'=>!$query->is_archive
]);
return response()->json(true);
}
public function tableWallets(Request $request){
2026-06-26 22:19:51 +03:30
$productSelect = 'product:'.implode(',', self::WALLET_PRODUCT_COLUMNS);
$query_table = Customer::with(['wallet.'.$productSelect, 'referral'])->where('is_archive', false);
2026-01-24 14:03:11 +00:00
TableEngine::create($query_table, $request)
->initSearch($this->search)
->select(['*',Customer::$SELECT_TYPE_STR])
->export($query_table);
2026-06-15 14:38:58 +03:30
$balanceService = app(WalletBalanceService::class);
2026-06-23 21:04:29 +03:30
$balanceReport = app(BalanceReportService::class);
$catalogMetalIds = $balanceService->catalogMetalIds();
$query_table['catalogMetals'] = $balanceReport->catalogMetalsMeta();
2026-06-15 14:38:58 +03:30
if (isset($query_table['rows'])) {
2026-06-23 21:04:29 +03:30
$query_table['rows'] = collect($query_table['rows'])->map(function ($row) use ($balanceService, $catalogMetalIds) {
2026-06-15 14:38:58 +03:30
$wallets = collect($row['wallet'] ?? []);
$row['wallet_cash'] = $balanceService->mainWalletCash($wallets);
2026-06-23 21:04:29 +03:30
$metalMap = $balanceService->buildCustomerWalletMetalDisplayMap($wallets, $catalogMetalIds);
$row['wallet_metal_gold'] = $metalMap;
foreach ($catalogMetalIds as $metalId) {
$row['wallet_metal_gold_' . $metalId] = $metalMap[(string) $metalId] ?? 0.0;
}
$row['wallet_gold'] = array_sum($metalMap);
2026-06-15 14:38:58 +03:30
return $row;
})->values();
}
2026-01-24 14:03:11 +00:00
return response()->json($query_table);
}
2026-06-26 22:19:51 +03:30
public function assetsSummary($customerId, AssetSummaryService $assetSummaryService)
{
$customer = Customer::findOrFail($customerId);
return response()->json(
$assetSummaryService->buildForCustomer((int) $customer->id, (int) $customer->type)
);
}
2026-01-24 14:03:11 +00:00
public function archiveTable(Request $request)
{
$query_table = Customer::with($this->with)->where("is_archive",true)/*->where('type',1)*/;
TableEngine::create($query_table, $request)
->initSearch($this->search)
->select($this->select)
->export($query_table);
return response()->json($query_table);
}
/**
* @param $id
* @return \Illuminate\Http\JsonResponse
*/
public function active($id){
$query = Customer::findOrFail($id);
$active = !$query->active;
$query->update([
'active'=>!$query->active
]);
if ($active){
$message ='اکانت شما توسط کارشناس سیستم تایید شد '."\n".
'با رفتن به لینک زیر وارد حساب کاربری خود شوید'."\n".
$this->Config('app_url')."\n".
'با تشکر'.$this->Config('name_fa');
}
else{
$message ='اکانت شما توسط کارشناس سیستم غیر فعال شد '."\n".
'با کارشناسان ما تماس بگیرید'."\n".
'با تشکر'.$this->Config('name_fa');
}
$this->Send($query->mobile,$message);
return response()->json(true);
}
public function changeTradeActive($id){
$query = Customer::findOrFail($id);
$query->update([
'trade_active'=>!$query->trade_active
]);
return response()->json(true);
}
// single customer-------------------
public function singleArchiveTable(Request $request)
{
$query_table = Customer::with($this->with)->where("is_archive",true)->where('type',2);
TableEngine::create($query_table, $request)
->initSearch($this->search)
->select($this->select)
->export($query_table);
return response()->json($query_table);
}
public function singleCustomerTable(Request $request)
{
$query_table = Customer::with($this->with)->where("is_archive",false)->where('type',2);
TableEngine::create($query_table, $request)
->initSearch($this->search)
->select($this->select)
->export($query_table);
return response()->json($query_table);
}
public function MakeValidByAdmin(Request $request,$id)
{
$customer = Customer::with('referral')->where('id', $id)->first();
if ($customer->is_legal == 2 or $customer->is_legal == 3){
return response()->json(true);
}
$customer->is_legal = 2;
$customer->save();
2026-06-07 18:27:04 +03:30
if ($this->Config('has_page_earn_money') == 'true') {
$referrer = $customer->referrer();
if (
$referrer
&& Customer::where('referral_to_id', $referrer->referral_id)->where('is_legal', '!=', 0)->count() < 4
) {
2026-01-24 14:03:11 +00:00
2026-05-28 11:53:27 +03:30
$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');
$referral_limit = Setting::where('key', 'referral_limit')->first();
2026-06-07 19:23:03 +03:30
$limitValue = $referral_limit->value ?? null;
$gift_permision = true;
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;
}
2026-05-28 11:53:27 +03:30
if ($gift_permision && $gift_for_inviter > 0) {
// dd($gift_for_inviter > 0, $gift_for_invited > 0);
$Withdraw = Withdraw::create([
'user_id' => $customer->id,
2026-06-07 18:27:04 +03:30
'customer_id' => $referrer->id,
2026-05-28 11:53:27 +03:30
"cash" => 0,
"gold" => $gift_for_inviter,
'sts' => 5,
]);
2026-06-07 18:27:04 +03:30
$wallet_interface = new WalletInterface($referrer->id, 2);
2026-05-28 11:53:27 +03:30
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);
2026-06-07 18:27:04 +03:30
$transaction = Transaction::where('customer_id', $referrer->id)->latest()->first();
2026-05-28 11:53:27 +03:30
TransactionsAdmin::create([
'customer_id' => $customer->id,
2026-06-07 18:27:04 +03:30
'customer_to_id' => $referrer->id,
2026-05-28 11:53:27 +03:30
'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,
]);
2026-06-07 18:27:04 +03:30
$referrerWallet = Wallet::where('owner', $referrer->id)->first();
2026-05-28 11:53:27 +03:30
if($this->Config('send_to_tahesab') == "true"){
try {
2026-06-25 22:20:40 +03:30
app(TahesabController::class)->referrerGoldReward(
2026-06-07 18:27:04 +03:30
$customer,
(float) $gift_for_inviter,
'پاداش بابت معرفی'
);
2026-06-25 22:20:40 +03:30
} catch (\Exception $e) {
report($e);
2026-05-28 11:53:27 +03:30
}
}
$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->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();
if($this->Config('send_to_tahesab') == "true"){
try {
2026-06-25 22:20:40 +03:30
app(TahesabController::class)->goldReward([
2026-06-07 14:30:26 +03:30
'customer_id' => $customer->id,
'value' => $gift_for_invited,
'desc' => 'پاداش بابت ثبت نام',
2026-05-28 11:53:27 +03:30
]);
2026-06-25 22:20:40 +03:30
} catch (\Exception $e) {
report($e);
2026-05-28 11:53:27 +03:30
}
}
$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
2026-06-07 18:27:04 +03:30
}
2026-01-24 14:03:11 +00:00
}
$companyName = $this->Config('name_fa');
$companyurl = $this->Config('app_url');
$message ='حساب کاربری شما توسط کارشناس سیستم تایید شد '."\n".
'با رفتن به لینک زیر وارد حساب کاربری خود شوید'."\n".
$this->Config('app_url')."\n".
'با تشکر'.$this->Config('name_fa');
$this->Send($customer->mobile, $message);
$referrer = $customer->referral;
// if ($referrer) {
// if (Customer::where('referral_to_id', $referrer->referral_id)->get()->count()<=$referrer->add_user_limit) {
// $referrerWallet = Wallet::where('owner',$customer->referral->id)->first();
// $message = "سلام {$referrer->name} عزیز،\n\n{$gift_for_invite} گرم طلا بابت معرفی {$customer->name} به حساب شما واریز شد.\nموجودی فعلی: {$referrerWallet->gold} گرم.\n\nاطلاعات بیشتر: $companyurl\n\nبا احترام،\n$companyName";
// $this->Send($referrer->mobile, $message);
// }
// }
return response()->json(true);
}
public function cardChecker(Request $request){
if (strlen($request->cart_number) != 16) {
throw new UnprocessableEntityHttpException('شماره کارت اشتباه می‌باشد.');
}
if (empty($request->national_code)) {
throw new UnprocessableEntityHttpException('لطفا شماره ملی وارد کنید.');
}
if(!empty($request->birth_date)){
$birth_date = Verta::instance(Carbon::parse($request->birth_date))->format('Ymd');
}
$card_number = preg_replace('/\D/', '', $request->cart_number);
$response = $this->jibit("cartMatching", $card_number, $request->national_code, $birth_date ?? null);
if(isset($response['refresh'])){
unset($response['refresh']);
}
if(!empty($response)){
if(!empty($response['message'])){
throw new UnprocessableEntityHttpException($response['message']);
}else{
return response()->json($response);
}
}else{
throw new UnprocessableEntityHttpException('ارتباط با سامانه قطع میباشد.');
}
}
2026-01-24 14:03:11 +00:00
}