From c94e6b04248fb88187f36ce524d88c1aa2cb181c Mon Sep 17 00:00:00 2001 From: adonis Date: Mon, 15 Jun 2026 14:38:58 +0330 Subject: [PATCH] wallet handling --- .../Controllers/Admin/CustomerController.php | 15 +++ app/Services/WalletBalanceService.php | 120 ++++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 app/Services/WalletBalanceService.php diff --git a/app/Http/Controllers/Admin/CustomerController.php b/app/Http/Controllers/Admin/CustomerController.php index 477182c..4007671 100644 --- a/app/Http/Controllers/Admin/CustomerController.php +++ b/app/Http/Controllers/Admin/CustomerController.php @@ -16,6 +16,8 @@ use Rooyesh\Wallet\interfaces\WalletInterface; use Rooyesh\Wallet\models\Transaction; use Rooyesh\Wallet\models\Wallet; use App\Models\Setting; +use App\Services\ConfigService; +use App\Services\WalletBalanceService; use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException; class CustomerController extends SuperController @@ -136,6 +138,19 @@ class CustomerController extends SuperController ->initSearch($this->search) ->select(['*',Customer::$SELECT_TYPE_STR]) ->export($query_table); + + $balanceService = app(WalletBalanceService::class); + $mainWalletProductIds = app(ConfigService::class)->mainWalletProductIds(); + if (isset($query_table['rows'])) { + $query_table['rows'] = collect($query_table['rows'])->map(function ($row) use ($balanceService, $mainWalletProductIds) { + $wallets = collect($row['wallet'] ?? []); + $row['wallet_cash'] = $balanceService->mainWalletCash($wallets); + $row['wallet_gold'] = $balanceService->totalGoldWeight($wallets, $mainWalletProductIds); + + return $row; + })->values(); + } + return response()->json($query_table); } diff --git a/app/Services/WalletBalanceService.php b/app/Services/WalletBalanceService.php new file mode 100644 index 0000000..d61db65 --- /dev/null +++ b/app/Services/WalletBalanceService.php @@ -0,0 +1,120 @@ +configService->mainWalletProductIds(); + } + + /** @param Collection|array|null $wallets */ + public function mainWalletCash(Collection|array|null $wallets): float + { + $line = $this->mainWalletLine($wallets); + + return (float) ($this->lineValue($line, 'cash') ?? 0); + } + + /** @param Collection|array|null $wallets */ + public function mainWalletGold(Collection|array|null $wallets): float + { + $line = $this->mainWalletLine($wallets); + + return (float) ($this->lineValue($line, 'gold') ?? 0); + } + + /** + * جمع طلای قابل‌نمایش: کیف اصلی + کیف محصولات + قلک‌ها. + * کیف‌های legacy محصولات main_wallet_products شمرده نمی‌شوند. + * + * @param Collection|array|null $wallets + */ + public function totalGoldWeight( + Collection|array|null $wallets, + ?array $mainWalletProductIds = null + ): float { + $mainWalletProductIds ??= $this->mainWalletProductIds(); + $sum = 0.0; + + foreach ($this->walletLines($wallets) as $line) { + $fundId = $this->lineValue($line, 'fund_id'); + $productId = (int) ($this->lineValue($line, 'product_id') ?? 0); + $gold = (float) ($this->lineValue($line, 'gold') ?? 0); + + if ($gold <= 0) { + continue; + } + + if ($fundId !== null) { + $sum += $gold; + continue; + } + + if ($productId === 0) { + $sum += $gold; + continue; + } + + if ($mainWalletProductIds !== [] && in_array($productId, $mainWalletProductIds, true)) { + continue; + } + + $sum += $gold; + } + + return $sum; + } + + /** @param Collection|array|null $wallets */ + private function mainWalletLine(Collection|array|null $wallets): mixed + { + foreach ($this->walletLines($wallets) as $line) { + if ($this->lineValue($line, 'fund_id') === null && $this->lineValue($line, 'product_id') === null) { + return $line; + } + } + + return null; + } + + /** @param Collection|array|null $wallets */ + private function walletLines(Collection|array|null $wallets): array + { + if ($wallets === null) { + return []; + } + + if ($wallets instanceof Collection) { + return $wallets->all(); + } + + return is_array($wallets) ? $wallets : []; + } + + private function lineValue(mixed $line, string $key): mixed + { + if (is_array($line)) { + return $line[$key] ?? null; + } + + if (is_object($line)) { + return $line->{$key} ?? null; + } + + return null; + } +}