Files
boursecu_server/app/Http/Controllers/Admin/CustomerController.php
T
2026-07-01 22:27:51 +03:30

317 lines
13 KiB
PHP

<?php
namespace App\Http\Controllers\Admin;
use App\DB\Te\TableEngine;
use App\Http\Controllers\SuperController;
use App\Models\Cnt;
use App\Models\Customer;
use Carbon\Carbon;
use Hekmatinasser\Verta\Verta;
use Illuminate\Http\Request;
use Rooyesh\Wallet\models\Wallet;
use App\Models\Setting;
use App\Services\AssetSummaryService;
use App\Services\BalanceReportService;
use App\Services\ConfigService;
use App\Services\ReferralGiftService;
use App\Services\WalletBalanceService;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
class CustomerController extends SuperController
{
/** فیلدهای محصول لازم برای محاسبهٔ صحیح وزن موجودی */
private const WALLET_PRODUCT_COLUMNS = [
'id', 'metal_id', 'unit', 'amount', 'group_type', 'trade_by_quantity', 'direct_price', 'title',
];
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)
{
$mainWalletProductIds = app(\App\Services\ConfigService::class)->mainWalletProductIds();
// فقط کیف محصول‌ها (نه کیف اصلی) — هر ردیف = موجودی واقعی یک محصول/فلز
$query_table = Wallet::with(['product:id,title,metal_id,unit'])
->where('owner', $customer_id)
->whereNotNull('product_id');
if ($mainWalletProductIds !== []) {
// کیف‌های قدیمیِ محصولات متصل به کیف اصلی نمایش داده نمی‌شوند
$query_table->whereNotIn('product_id', $mainWalletProductIds);
}
TableEngine::create($query_table, $request)
->initSearch($this->search)
->select(['id', 'owner', 'cash', 'gold', 'fund_id', 'product_id', 'created_at', 'updated_at'])
->export($query_table);
// محصولات متصل به کیف اصلی: یک ردیف نماینده با موجودی کیف اصلی
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;
}
}
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){
$productSelect = 'product:'.implode(',', self::WALLET_PRODUCT_COLUMNS);
$query_table = Customer::with(['wallet.'.$productSelect, 'referral'])->where('is_archive', false);
TableEngine::create($query_table, $request)
->initSearch($this->search)
->select(['*',Customer::$SELECT_TYPE_STR])
->export($query_table);
$balanceService = app(WalletBalanceService::class);
$balanceReport = app(BalanceReportService::class);
$catalogMetalIds = $balanceService->catalogMetalIds();
$query_table['catalogMetals'] = $balanceReport->catalogMetalsMeta();
if (isset($query_table['rows'])) {
$query_table['rows'] = collect($query_table['rows'])->map(function ($row) use ($balanceService, $catalogMetalIds) {
$wallets = collect($row['wallet'] ?? []);
$row['wallet_cash'] = $balanceService->mainWalletCash($wallets);
$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);
return $row;
})->values();
}
return response()->json($query_table);
}
public function assetsSummary($customerId, AssetSummaryService $assetSummaryService)
{
$customer = Customer::findOrFail($customerId);
return response()->json(
$assetSummaryService->buildForCustomer((int) $customer->id, (int) $customer->type)
);
}
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();
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
) {
$gift_permision = app(ReferralGiftService::class)->resolveReferrerGiftPermission($referrer);
app(ReferralGiftService::class)->tryGiveOnRegistration(
$customer,
$referrer,
$gift_permision,
false
);
}
}
$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('ارتباط با سامانه قطع میباشد.');
}
}
}