fix some bugs

This commit is contained in:
2026-07-14 12:35:03 +03:30
parent 277cf31d93
commit 8f3fb35b2b
3 changed files with 32 additions and 11 deletions
@@ -39,8 +39,20 @@ class CustomerController extends SuperController
* @return \Illuminate\Http\JsonResponse * @return \Illuminate\Http\JsonResponse
*/ */
public function listUser(){ public function listUser(){
$user = Customer::with(['wallet'])->select('id','name','mobile','type')->get(); $balanceService = app(WalletBalanceService::class);
return response()->json($user); $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) public function updateAdmin(Request $request , $id)
{ {
@@ -147,7 +159,7 @@ class CustomerController extends SuperController
if (isset($query_table['rows'])) { if (isset($query_table['rows'])) {
$query_table['rows'] = collect($query_table['rows'])->map(function ($row) use ($balanceService, $catalogMetalIds) { $query_table['rows'] = collect($query_table['rows'])->map(function ($row) use ($balanceService, $catalogMetalIds) {
$wallets = collect($row['wallet'] ?? []); $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); $metalMap = $balanceService->buildCustomerWalletMetalDisplayMap($wallets, $catalogMetalIds);
$row['wallet_metal_gold'] = $metalMap; $row['wallet_metal_gold'] = $metalMap;
foreach ($catalogMetalIds as $metalId) { foreach ($catalogMetalIds as $metalId) {
+1 -1
View File
@@ -91,7 +91,7 @@ class Customer extends Authenticatable implements JWTSubject
public function wallet() 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() public function orders()
+16 -7
View File
@@ -27,17 +27,17 @@ class WalletBalanceService
} }
/** @param Collection|array<int, mixed>|null $wallets */ /** @param Collection|array<int, mixed>|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); return (float) ($this->lineValue($line, 'cash') ?? 0);
} }
/** @param Collection|array<int, mixed>|null $wallets */ /** @param Collection|array<int, mixed>|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); return (float) ($this->lineValue($line, 'gold') ?? 0);
} }
@@ -389,12 +389,21 @@ class WalletBalanceService
} }
/** @param Collection|array<int, mixed>|null $wallets */ /** @param Collection|array<int, mixed>|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) { foreach ($this->walletLines($wallets) as $line) {
if ($this->lineValue($line, 'fund_id') === null && $this->lineValue($line, 'product_id') === null) { if ($this->lineValue($line, 'fund_id') !== null || $this->lineValue($line, 'product_id') !== null) {
return $line; continue;
} }
if ($ownerType !== null) {
$lineOwnerType = $this->lineValue($line, 'owner_type');
if ($lineOwnerType !== null && (int) $lineOwnerType !== $ownerType) {
continue;
}
}
return $line;
} }
return null; return null;