From b1ae6e159e9ab7cc6e138db81ee2431503ab76cb Mon Sep 17 00:00:00 2001 From: adonis Date: Mon, 15 Jun 2026 12:05:36 +0330 Subject: [PATCH] wallet handling --- .../Customer/CustomerController.php | 11 +++ app/Services/AssetSummaryService.php | 83 ++++++++++++++++--- 2 files changed, 83 insertions(+), 11 deletions(-) diff --git a/app/Http/Controllers/Customer/CustomerController.php b/app/Http/Controllers/Customer/CustomerController.php index d4a3082..76a529b 100644 --- a/app/Http/Controllers/Customer/CustomerController.php +++ b/app/Http/Controllers/Customer/CustomerController.php @@ -119,6 +119,17 @@ class CustomerController extends Controller } } + $mainWalletGoldWeight = (float) ($wallet->count_gold_wallet ?? 0); + if ($mainWalletGoldWeight > 0) { + $liveGold = (int) app(\App\Services\ProductDisplayPriceService::class)->resolveAppBasePrice(); + if ($liveGold <= 0) { + $liveGold = (int) (\App\Models\Price::latest('id')->value('price') ?? 0); + } + if ($liveGold > 0) { + $count_cash_from_gold += $mainWalletGoldWeight * $liveGold; + } + } + $count_cash_total = $count_cash_from_gold + $count_cash_from_silver + $count_cash_from_copper + $count_cash_pure; $wallet->count_cash_wallets = $count_cash_total - $wallet->count_cash_wallet; diff --git a/app/Services/AssetSummaryService.php b/app/Services/AssetSummaryService.php index 9308551..38fda1b 100644 --- a/app/Services/AssetSummaryService.php +++ b/app/Services/AssetSummaryService.php @@ -3,6 +3,7 @@ namespace App\Services; use App\Http\Controllers\Controller; +use App\Models\Price; use App\Models\Product; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Schema; @@ -52,7 +53,12 @@ class AssetSummaryService ); $metalTotals = $this->buildMetalTotals($userWallets, $mainWallet, $metalIds, $weightUnit, $catalogById, $metalsWithProductWallet, $mainWalletProductIds); $cashWallet = (float) ($mainWallet->cash ?? 0); - $assetsRialValue = $this->sumAssetsRialValue($userWallets, $catalogById); + $assetsRialValue = $this->sumAssetsRialValue( + $metalTotals, + $userWallets, + $catalogById, + $mainWalletProductIds + ); $totalRialValue = $cashWallet + $assetsRialValue; return [ @@ -427,29 +433,84 @@ class AssetSummaryService return (int) $metalId === 1 ? $mainWalletGrams : 0.0; } - private function sumAssetsRialValue(Collection $userWallets, Collection $catalogById): float - { + /** + * ارزش ریالی دارایی‌های فلزی: + * • وزن کل هر فلز × قیمت زنده + * • سکه‌ها (خارج از جمع وزن) × قیمت محصول + */ + private function sumAssetsRialValue( + array $metalTotals, + Collection $userWallets, + Collection $catalogById, + array $mainWalletProductIds = [] + ): float { $total = 0.0; + foreach ($metalTotals as $metal) { + $weight = (float) ($metal['total_weight'] ?? 0); + if ($weight <= 0) { + continue; + } + + $livePrice = $this->resolveLivePricePerUnit((int) ($metal['metal_id'] ?? 0)); + if ($livePrice > 0) { + $total += $weight * $livePrice; + } + } + foreach ($userWallets as $walletLine) { - if ($walletLine->product_id === null || !$walletLine->product) { + if ($walletLine->product_id === null || !$walletLine->product || (float) $walletLine->gold <= 0) { + continue; + } + + if (in_array((int) $walletLine->product_id, $mainWalletProductIds, true)) { continue; } $catalogProduct = $this->catalogProduct($walletLine->product, $catalogById); - $qty = (float) $walletLine->gold; - $metalId = (int) ($catalogProduct->metal_id ?? 0); - - if ($metalId === 1) { - $total += (float) ($catalogProduct->price ?? 0) * $qty; - } elseif ($metalId === 2 || $metalId === 3) { - $total += ((float) ($catalogProduct->price ?? 0) * (float) ($catalogProduct->amount ?? 1)) * $qty; + if ((string) ($catalogProduct->group_type ?? '') !== 'سکه') { + continue; } + + $total += $this->productDisplayUnitPrice($catalogProduct) * (float) $walletLine->gold; } return $total; } + private function resolveLivePricePerUnit(int $metalId): float + { + if ($metalId === 1) { + $live = (int) app(ProductDisplayPriceService::class)->resolveAppBasePrice(); + if ($live > 0) { + return (float) $live; + } + + return (float) (Price::latest('id')->value('price') ?? 0); + } + + $product = Product::query() + ->where('active', 1) + ->where('metal_id', $metalId) + ->where('price', '>', 0) + ->orderBy('id') + ->first(); + + if (!$product) { + return 0.0; + } + + $unitPrice = $this->productDisplayUnitPrice($product); + $amount = (float) ($product->amount ?? 1); + + return $amount > 0 ? $unitPrice * $amount : $unitPrice; + } + + private function productDisplayUnitPrice(Product $product): float + { + return (float) ($product->price ?? 0) + (float) ($product->price_profit ?? 0); + } + private function shouldShowInAssets(Product $product): bool { if (!Product::hasShowInAssetsColumn()) {