diff --git a/app/Services/AssetSummaryService.php b/app/Services/AssetSummaryService.php index e465b69..aa748db 100644 --- a/app/Services/AssetSummaryService.php +++ b/app/Services/AssetSummaryService.php @@ -50,7 +50,7 @@ class AssetSummaryService $metalsWithProductWallet = $this->metalIdsWithProductWallet($userWallets, $catalogById, $mainWalletProductIds, $mainWallet); $inventoryCards = array_merge( - $this->buildMeltedInventoryCards($catalog, $catalogById, $mainWallet, $metalIds, $metalsWithProductWallet, $mainWalletProductIds), + $this->buildMeltedInventoryCards($catalog, $catalogById, $mainWallet, $metalIds, $metalsWithProductWallet, $mainWalletProductIds, $userWallets), $this->buildInventoryCards($userWallets, $catalogById, $mainWalletProductIds, $mainWallet), ); $metalTotals = $this->buildMetalTotals($userWallets, $mainWallet, $metalIds, $weightUnit, $catalogById, $metalsWithProductWallet, $mainWalletProductIds); @@ -190,10 +190,13 @@ class AssetSummaryService ?Wallet $mainWallet, array $metalIds, array $metalsWithProductWallet = [], - array $mainWalletProductIds = [] + array $mainWalletProductIds = [], + Collection $userWallets ): array { $cards = []; - $mainWalletGrams = Controller::weightToGrams((float) ($mainWallet?->gold ?? 0)); + $mainWalletGrams = Controller::weightToGrams( + $this->effectiveMainWalletGold($userWallets, $mainWallet, $mainWalletProductIds) + ); $assignedMetalIds = []; $mainProductId = $this->configService->mainProductId(); @@ -204,7 +207,7 @@ class AssetSummaryService return []; } - $mainWalletWeight = (float) ($mainWallet?->gold ?? 0); + $mainWalletWeight = $this->effectiveMainWalletGold($userWallets, $mainWallet, $mainWalletProductIds); if ($mainWalletWeight <= 0) { return []; } @@ -327,7 +330,8 @@ class AssetSummaryService } /** - * کیف legacy محصول آبشده/کاتد — وقتی همان فلز روی کیف اصلی موجودی دارد نباید دوباره شمرده شود. + * کیف legacy محصول آبشده/کاتد — فقط همان محصول نماینده یا main_product؛ + * محصولات آبشدهٔ جدا (مثل آبشده سرمایه‌گذاری) نباید پنهان شوند. */ private function shouldIgnoreLegacyProductWalletLine( Product $catalogProduct, @@ -335,7 +339,9 @@ class AssetSummaryService Collection $catalogById, array $mainWalletProductIds ): bool { - if (in_array((int) $catalogProduct->id, $mainWalletProductIds, true)) { + $productId = (int) $catalogProduct->id; + + if (in_array($productId, $mainWalletProductIds, true)) { return true; } @@ -347,9 +353,17 @@ class AssetSummaryService return false; } - $mainMetalId = $this->resolveMainWalletMetalId($mainWallet, $catalogById, $mainWalletProductIds); + $representative = $this->resolveMainWalletRepresentative($catalogById, $mainWalletProductIds); + if ($representative && $productId === (int) $representative->id) { + return true; + } - return $mainMetalId > 0 && (int) ($catalogProduct->metal_id ?? 0) === $mainMetalId; + $mainProductId = $this->configService->mainProductId(); + if ($mainProductId && $productId === (int) $mainProductId) { + return true; + } + + return false; } private function resolveMainWalletMetalId( @@ -565,7 +579,9 @@ class AssetSummaryService return []; } - $mainWalletGrams = Controller::weightToGrams((float) ($mainWallet?->gold ?? 0)); + $mainWalletGrams = Controller::weightToGrams( + $this->effectiveMainWalletGold($userWallets, $mainWallet, $mainWalletProductIds) + ); $totals = []; // فلز نمایندهٔ کیف اصلی در حالت main_wallet_products @@ -740,6 +756,32 @@ class AssetSummaryService return (bool) ($product->show_in_assets ?? false); } + /** + * طلای کیف اصلی + کیف legacy محصولات main_wallet_products (خریدهای قبل از اتصال). + */ + private function effectiveMainWalletGold( + Collection $userWallets, + ?Wallet $mainWallet, + array $mainWalletProductIds + ): float { + $total = (float) ($mainWallet?->gold ?? 0); + + if ($mainWalletProductIds === []) { + return $total; + } + + foreach ($userWallets as $walletLine) { + $productId = (int) ($walletLine->product_id ?? 0); + if ($productId <= 0 || !in_array($productId, $mainWalletProductIds, true)) { + continue; + } + + $total += (float) ($walletLine->gold ?? 0); + } + + return $total; + } + private function isCountBasedProduct(Product $product): bool { return Controller::isCountBasedProduct($this->productPayload($product));