From 8f3fb35b2bcd5792ab93fd64f9fa6adefb86ec39 Mon Sep 17 00:00:00 2001 From: adonis Date: Tue, 14 Jul 2026 12:35:03 +0330 Subject: [PATCH] fix some bugs --- .../Controllers/Admin/CustomerController.php | 18 ++++++++++++--- app/Models/Customer.php | 2 +- app/Services/WalletBalanceService.php | 23 +++++++++++++------ 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/app/Http/Controllers/Admin/CustomerController.php b/app/Http/Controllers/Admin/CustomerController.php index 2496d80..c206560 100644 --- a/app/Http/Controllers/Admin/CustomerController.php +++ b/app/Http/Controllers/Admin/CustomerController.php @@ -39,8 +39,20 @@ class CustomerController extends SuperController * @return \Illuminate\Http\JsonResponse */ public function listUser(){ - $user = Customer::with(['wallet'])->select('id','name','mobile','type')->get(); - return response()->json($user); + $balanceService = app(WalletBalanceService::class); + $catalogMetalIds = $balanceService->catalogMetalIds(); + + $users = Customer::with(['wallet'])->select('id', 'name', 'mobile', 'type')->get()->map(function ($user) use ($balanceService, $catalogMetalIds) { + $wallets = collect($user->wallet ?? []); + $ownerType = (int) $user->type; + $user->wallet_cash = $balanceService->mainWalletCash($wallets, $ownerType); + $metalMap = $balanceService->buildCustomerWalletMetalDisplayMap($wallets, $catalogMetalIds); + $user->wallet_gold = array_sum($metalMap); + + return $user; + }); + + return response()->json($users); } public function updateAdmin(Request $request , $id) { @@ -147,7 +159,7 @@ class CustomerController extends SuperController 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); + $row['wallet_cash'] = $balanceService->mainWalletCash($wallets, (int) ($row['type'] ?? 0)); $metalMap = $balanceService->buildCustomerWalletMetalDisplayMap($wallets, $catalogMetalIds); $row['wallet_metal_gold'] = $metalMap; foreach ($catalogMetalIds as $metalId) { diff --git a/app/Models/Customer.php b/app/Models/Customer.php index ec098f1..4f0151b 100644 --- a/app/Models/Customer.php +++ b/app/Models/Customer.php @@ -91,7 +91,7 @@ class Customer extends Authenticatable implements JWTSubject public function wallet() { - return $this->hasMany(Wallet::class ,'owner')->with(['fund','product'])->select(['cash', 'fund_id','product_id','gold', 'owner']); + return $this->hasMany(Wallet::class ,'owner')->with(['fund','product'])->select(['cash', 'fund_id','product_id','gold', 'owner', 'owner_type']); } public function orders() diff --git a/app/Services/WalletBalanceService.php b/app/Services/WalletBalanceService.php index bc6f963..60c31ee 100644 --- a/app/Services/WalletBalanceService.php +++ b/app/Services/WalletBalanceService.php @@ -27,17 +27,17 @@ class WalletBalanceService } /** @param Collection|array|null $wallets */ - public function mainWalletCash(Collection|array|null $wallets): float + public function mainWalletCash(Collection|array|null $wallets, ?int $ownerType = null): float { - $line = $this->mainWalletLine($wallets); + $line = $this->mainWalletLine($wallets, $ownerType); return (float) ($this->lineValue($line, 'cash') ?? 0); } /** @param Collection|array|null $wallets */ - public function mainWalletGold(Collection|array|null $wallets): float + public function mainWalletGold(Collection|array|null $wallets, ?int $ownerType = null): float { - $line = $this->mainWalletLine($wallets); + $line = $this->mainWalletLine($wallets, $ownerType); return (float) ($this->lineValue($line, 'gold') ?? 0); } @@ -389,12 +389,21 @@ class WalletBalanceService } /** @param Collection|array|null $wallets */ - private function mainWalletLine(Collection|array|null $wallets): mixed + private function mainWalletLine(Collection|array|null $wallets, ?int $ownerType = null): mixed { foreach ($this->walletLines($wallets) as $line) { - if ($this->lineValue($line, 'fund_id') === null && $this->lineValue($line, 'product_id') === null) { - return $line; + if ($this->lineValue($line, 'fund_id') !== null || $this->lineValue($line, 'product_id') !== null) { + continue; } + + if ($ownerType !== null) { + $lineOwnerType = $this->lineValue($line, 'owner_type'); + if ($lineOwnerType !== null && (int) $lineOwnerType !== $ownerType) { + continue; + } + } + + return $line; } return null;