diff --git a/app/Services/AssetSummaryService.php b/app/Services/AssetSummaryService.php index 43e853a..db2074a 100644 --- a/app/Services/AssetSummaryService.php +++ b/app/Services/AssetSummaryService.php @@ -46,10 +46,10 @@ class AssetSummaryService // محصولات متصل به کیف اصلی (main_wallet_products) — موجودی‌شان = gold کیف اصلی $mainWalletProductIds = $this->configService->mainWalletProductIds(); - $metalsWithProductWallet = $this->metalIdsWithProductWallet($userWallets, $catalogById, $mainWalletProductIds); + $metalsWithProductWallet = $this->metalIdsWithProductWallet($userWallets, $catalogById, $mainWalletProductIds, $mainWallet); $inventoryCards = array_merge( $this->buildMeltedInventoryCards($catalog, $catalogById, $mainWallet, $metalIds, $metalsWithProductWallet, $mainWalletProductIds), - $this->buildInventoryCards($userWallets, $catalogById, $mainWalletProductIds) + $this->buildInventoryCards($userWallets, $catalogById, $mainWalletProductIds, $mainWallet), ); $metalTotals = $this->buildMetalTotals($userWallets, $mainWallet, $metalIds, $weightUnit, $catalogById, $metalsWithProductWallet, $mainWalletProductIds); $cashWallet = (float) ($mainWallet?->cash ?? 0); @@ -193,6 +193,59 @@ class AssetSummaryService ); } + /** + * کیف legacy محصول آبشده/کاتد — وقتی همان فلز روی کیف اصلی موجودی دارد نباید دوباره شمرده شود. + */ + private function shouldIgnoreLegacyProductWalletLine( + Product $catalogProduct, + ?Wallet $mainWallet, + Collection $catalogById, + array $mainWalletProductIds + ): bool { + if (in_array((int) $catalogProduct->id, $mainWalletProductIds, true)) { + return true; + } + + if ((float) ($mainWallet?->gold ?? 0) <= 0) { + return false; + } + + if (!$this->isMeltedTradeProduct($catalogProduct)) { + return false; + } + + $mainMetalId = $this->resolveMainWalletMetalId($mainWallet, $catalogById, $mainWalletProductIds); + + return $mainMetalId > 0 && (int) ($catalogProduct->metal_id ?? 0) === $mainMetalId; + } + + private function resolveMainWalletMetalId( + ?Wallet $mainWallet, + Collection $catalogById, + array $mainWalletProductIds + ): int { + if ($mainWalletProductIds !== []) { + $representative = $this->resolveMainWalletRepresentative($catalogById, $mainWalletProductIds); + if ($representative) { + return max(1, (int) ($representative->metal_id ?? 1)); + } + } + + if ((float) ($mainWallet?->gold ?? 0) <= 0) { + return 0; + } + + $mainProductId = $this->configService->mainProductId(); + if ($mainProductId) { + $mainProduct = $catalogById->get((int) $mainProductId); + if ($mainProduct) { + return max(1, (int) ($mainProduct->metal_id ?? 1)); + } + } + + return 1; + } + private function catalogProduct(Product $product, Collection $catalogById): Product { return $catalogById->get((int) $product->id) ?? $product; @@ -253,9 +306,14 @@ class AssetSummaryService } /** فلزهایی که کاربر روی کیف محصول موجودی دارد — برای جلوگیری از کارت آبشدهٔ تکراری. */ - private function metalIdsWithProductWallet(Collection $userWallets, Collection $catalogById, array $mainWalletProductIds = []): array - { + private function metalIdsWithProductWallet( + Collection $userWallets, + Collection $catalogById, + array $mainWalletProductIds = [], + ?Wallet $mainWallet = null + ): array { $metalIds = []; + $mainWallet ??= $this->resolveMainWallet($userWallets); foreach ($userWallets as $walletLine) { if ($walletLine->product_id === null || (float) $walletLine->gold <= 0) { @@ -272,7 +330,12 @@ class AssetSummaryService continue; } - $metalId = (int) ($this->catalogProduct($product, $catalogById)->metal_id ?? 0); + $catalogProduct = $this->catalogProduct($product, $catalogById); + if ($this->shouldIgnoreLegacyProductWalletLine($catalogProduct, $mainWallet, $catalogById, $mainWalletProductIds)) { + continue; + } + + $metalId = (int) ($catalogProduct->metal_id ?? 0); if ($metalId > 0) { $metalIds[] = $metalId; } @@ -303,9 +366,14 @@ class AssetSummaryService return $catalog->first(fn (Product $product) => (int) ($product->metal_id ?? 0) === $metalId); } - private function buildInventoryCards(Collection $userWallets, Collection $catalogById, array $mainWalletProductIds = []): array - { + private function buildInventoryCards( + Collection $userWallets, + Collection $catalogById, + array $mainWalletProductIds = [], + ?Wallet $mainWallet = null + ): array { $cards = []; + $mainWallet ??= $this->resolveMainWallet($userWallets); foreach ($userWallets as $walletLine) { if ($walletLine->fund_id !== null) { @@ -327,6 +395,10 @@ class AssetSummaryService } $catalogProduct = $this->catalogProduct($product, $catalogById); + if ($this->shouldIgnoreLegacyProductWalletLine($catalogProduct, $mainWallet, $catalogById, $mainWalletProductIds)) { + continue; + } + $productPayload = $this->productPayload($catalogProduct); $isCountBased = $this->isCountBasedProduct($catalogProduct); @@ -384,6 +456,10 @@ class AssetSummaryService } $catalogProduct = $this->catalogProduct($walletLine->product, $catalogById); + if ($this->shouldIgnoreLegacyProductWalletLine($catalogProduct, $mainWallet, $catalogById, $mainWalletProductIds)) { + continue; + } + if ((int) ($catalogProduct->metal_id ?? 0) !== (int) $metalId) { continue; }